xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 1c9de0c9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 #include <sys/bitmap.h>
49 
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/strsun.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_ftable.h>
81 #include <inet/ip_rts.h>
82 #include <inet/ip_ndp.h>
83 #include <inet/ip_if.h>
84 #include <inet/ip_impl.h>
85 #include <inet/tun.h>
86 #include <inet/sctp_ip.h>
87 #include <inet/ip_netinfo.h>
88 #include <inet/mib2.h>
89 
90 #include <net/pfkeyv2.h>
91 #include <inet/ipsec_info.h>
92 #include <inet/sadb.h>
93 #include <inet/ipsec_impl.h>
94 #include <sys/iphada.h>
95 
96 
97 #include <netinet/igmp.h>
98 #include <inet/ip_listutils.h>
99 #include <inet/ipclassifier.h>
100 #include <sys/mac.h>
101 
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
104 
105 #include <sys/tsol/tndb.h>
106 #include <sys/tsol/tnet.h>
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 typedef struct ip_sock_ar_s {
122 	union {
123 		area_t	ip_sock_area;
124 		ared_t	ip_sock_ared;
125 		areq_t	ip_sock_areq;
126 	} ip_sock_ar_u;
127 	queue_t	*ip_sock_ar_q;
128 } ip_sock_ar_t;
129 
130 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
131 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
132 		    char *value, caddr_t cp, cred_t *ioc_cr);
133 
134 static boolean_t ill_is_quiescent(ill_t *);
135 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
136 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
137 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
140     mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
142     queue_t *q, mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
144     mblk_t *mp, boolean_t need_up);
145 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
146     mblk_t *mp);
147 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
148     queue_t *q, mblk_t *mp, boolean_t need_up);
149 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160 		    boolean_t initialize);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_multicast_down(ipif_t *ipif);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
182 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
183 
184 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
185 static int	ill_arp_off(ill_t *ill);
186 static int	ill_arp_on(ill_t *ill);
187 static void	ill_delete_interface_type(ill_if_t *);
188 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
189 static void	ill_dl_down(ill_t *ill);
190 static void	ill_down(ill_t *ill);
191 static void	ill_downi(ire_t *ire, char *ill_arg);
192 static void	ill_free_mib(ill_t *ill);
193 static void	ill_glist_delete(ill_t *);
194 static boolean_t ill_has_usable_ipif(ill_t *);
195 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
196 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
197 static void	ill_phyint_free(ill_t *ill);
198 static void	ill_phyint_reinit(ill_t *ill);
199 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
200 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
201 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
202 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
203 static void	ill_stq_cache_delete(ire_t *, char *);
204 
205 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     in6_addr_t *);
209 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     ipaddr_t *);
211 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
212 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     in6_addr_t *);
214 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
215     ipaddr_t *);
216 
217 static void	ipif_save_ire(ipif_t *, ire_t *);
218 static void	ipif_remove_ire(ipif_t *, ire_t *);
219 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
220 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
221 
222 /*
223  * Per-ill IPsec capabilities management.
224  */
225 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
226 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
227 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
228 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
229 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
230 static void ill_capability_proto(ill_t *, int, mblk_t *);
231 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
232     boolean_t);
233 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
236 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
238 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
240 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
241     dl_capability_sub_t *);
242 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
243 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static void ill_capability_lso_reset(ill_t *, mblk_t **);
245 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
246 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
247 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
248 static void	ill_capability_dls_disable(ill_t *);
249 
250 static void	illgrp_cache_delete(ire_t *, char *);
251 static void	illgrp_delete(ill_t *ill);
252 static void	illgrp_reset_schednext(ill_t *ill);
253 
254 static ill_t	*ill_prev_usesrc(ill_t *);
255 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
256 static void	ill_disband_usesrc_group(ill_t *);
257 
258 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
259 
260 #ifdef DEBUG
261 static	void	ill_trace_cleanup(const ill_t *);
262 static	void	ipif_trace_cleanup(const ipif_t *);
263 #endif
264 
265 /*
266  * if we go over the memory footprint limit more than once in this msec
267  * interval, we'll start pruning aggressively.
268  */
269 int ip_min_frag_prune_time = 0;
270 
271 /*
272  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
273  * and the IPsec DOI
274  */
275 #define	MAX_IPSEC_ALGS	256
276 
277 #define	BITSPERBYTE	8
278 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
279 
280 #define	IPSEC_ALG_ENABLE(algs, algid) \
281 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
282 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
283 
284 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
285 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
286 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
287 
288 typedef uint8_t ipsec_capab_elem_t;
289 
290 /*
291  * Per-algorithm parameters.  Note that at present, only encryption
292  * algorithms have variable keysize (IKE does not provide a way to negotiate
293  * auth algorithm keysize).
294  *
295  * All sizes here are in bits.
296  */
297 typedef struct
298 {
299 	uint16_t	minkeylen;
300 	uint16_t	maxkeylen;
301 } ipsec_capab_algparm_t;
302 
303 /*
304  * Per-ill capabilities.
305  */
306 struct ill_ipsec_capab_s {
307 	ipsec_capab_elem_t *encr_hw_algs;
308 	ipsec_capab_elem_t *auth_hw_algs;
309 	uint32_t algs_size;	/* size of _hw_algs in bytes */
310 	/* algorithm key lengths */
311 	ipsec_capab_algparm_t *encr_algparm;
312 	uint32_t encr_algparm_size;
313 	uint32_t encr_algparm_end;
314 };
315 
316 /*
317  * The field values are larger than strictly necessary for simple
318  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
319  */
320 static area_t	ip_area_template = {
321 	AR_ENTRY_ADD,			/* area_cmd */
322 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
323 					/* area_name_offset */
324 	/* area_name_length temporarily holds this structure length */
325 	sizeof (area_t),			/* area_name_length */
326 	IP_ARP_PROTO_TYPE,		/* area_proto */
327 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
328 	IP_ADDR_LEN,			/* area_proto_addr_length */
329 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
330 					/* area_proto_mask_offset */
331 	0,				/* area_flags */
332 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
333 					/* area_hw_addr_offset */
334 	/* Zero length hw_addr_length means 'use your idea of the address' */
335 	0				/* area_hw_addr_length */
336 };
337 
338 /*
339  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
340  * support
341  */
342 static area_t	ip6_area_template = {
343 	AR_ENTRY_ADD,			/* area_cmd */
344 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
345 					/* area_name_offset */
346 	/* area_name_length temporarily holds this structure length */
347 	sizeof (area_t),			/* area_name_length */
348 	IP_ARP_PROTO_TYPE,		/* area_proto */
349 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
350 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
351 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
352 					/* area_proto_mask_offset */
353 	0,				/* area_flags */
354 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
355 					/* area_hw_addr_offset */
356 	/* Zero length hw_addr_length means 'use your idea of the address' */
357 	0				/* area_hw_addr_length */
358 };
359 
360 static ared_t	ip_ared_template = {
361 	AR_ENTRY_DELETE,
362 	sizeof (ared_t) + IP_ADDR_LEN,
363 	sizeof (ared_t),
364 	IP_ARP_PROTO_TYPE,
365 	sizeof (ared_t),
366 	IP_ADDR_LEN
367 };
368 
369 static ared_t	ip6_ared_template = {
370 	AR_ENTRY_DELETE,
371 	sizeof (ared_t) + IPV6_ADDR_LEN,
372 	sizeof (ared_t),
373 	IP_ARP_PROTO_TYPE,
374 	sizeof (ared_t),
375 	IPV6_ADDR_LEN
376 };
377 
378 /*
379  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
380  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
381  * areq is used).
382  */
383 static areq_t	ip_areq_template = {
384 	AR_ENTRY_QUERY,			/* cmd */
385 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
386 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
387 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
388 	sizeof (areq_t),			/* target addr offset */
389 	IP_ADDR_LEN,			/* target addr_length */
390 	0,				/* flags */
391 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
392 	IP_ADDR_LEN,			/* sender addr length */
393 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
394 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
395 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
396 	/* anything else filled in by the code */
397 };
398 
399 static arc_t	ip_aru_template = {
400 	AR_INTERFACE_UP,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_ard_template = {
406 	AR_INTERFACE_DOWN,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aron_template = {
412 	AR_INTERFACE_ON,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 static arc_t	ip_aroff_template = {
418 	AR_INTERFACE_OFF,
419 	sizeof (arc_t),		/* Name offset */
420 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
421 };
422 
423 
424 static arma_t	ip_arma_multi_template = {
425 	AR_MAPPING_ADD,
426 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
427 				/* Name offset */
428 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
429 	IP_ARP_PROTO_TYPE,
430 	sizeof (arma_t),			/* proto_addr_offset */
431 	IP_ADDR_LEN,				/* proto_addr_length */
432 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
433 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
434 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
435 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
436 	IP_MAX_HW_LEN,				/* hw_addr_length */
437 	0,					/* hw_mapping_start */
438 };
439 
440 static ipft_t	ip_ioctl_ftbl[] = {
441 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
442 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
443 		IPFT_F_NO_REPLY },
444 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
445 		IPFT_F_NO_REPLY },
446 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
447 	{ 0 }
448 };
449 
450 /* Simple ICMP IP Header Template */
451 static ipha_t icmp_ipha = {
452 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
453 };
454 
455 /* Flag descriptors for ip_ipif_report */
456 static nv_t	ipif_nv_tbl[] = {
457 	{ IPIF_UP,		"UP" },
458 	{ IPIF_BROADCAST,	"BROADCAST" },
459 	{ ILLF_DEBUG,		"DEBUG" },
460 	{ PHYI_LOOPBACK,	"LOOPBACK" },
461 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
462 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
463 	{ PHYI_RUNNING,		"RUNNING" },
464 	{ ILLF_NOARP,		"NOARP" },
465 	{ PHYI_PROMISC,		"PROMISC" },
466 	{ PHYI_ALLMULTI,	"ALLMULTI" },
467 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
468 	{ ILLF_MULTICAST,	"MULTICAST" },
469 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
470 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
471 	{ IPIF_DHCPRUNNING,	"DHCP" },
472 	{ IPIF_PRIVATE,		"PRIVATE" },
473 	{ IPIF_NOXMIT,		"NOXMIT" },
474 	{ IPIF_NOLOCAL,		"NOLOCAL" },
475 	{ IPIF_DEPRECATED,	"DEPRECATED" },
476 	{ IPIF_PREFERRED,	"PREFERRED" },
477 	{ IPIF_TEMPORARY,	"TEMPORARY" },
478 	{ IPIF_ADDRCONF,	"ADDRCONF" },
479 	{ PHYI_VIRTUAL,		"VIRTUAL" },
480 	{ ILLF_ROUTER,		"ROUTER" },
481 	{ ILLF_NONUD,		"NONUD" },
482 	{ IPIF_ANYCAST,		"ANYCAST" },
483 	{ ILLF_NORTEXCH,	"NORTEXCH" },
484 	{ ILLF_IPV4,		"IPV4" },
485 	{ ILLF_IPV6,		"IPV6" },
486 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
487 	{ PHYI_FAILED,		"FAILED" },
488 	{ PHYI_STANDBY,		"STANDBY" },
489 	{ PHYI_INACTIVE,	"INACTIVE" },
490 	{ PHYI_OFFLINE,		"OFFLINE" },
491 };
492 
493 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
494 
495 static ip_m_t	ip_m_tbl[] = {
496 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
497 	    ip_ether_v6intfid },
498 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
499 	    ip_nodef_v6intfid },
500 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
501 	    ip_nodef_v6intfid },
502 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
503 	    ip_nodef_v6intfid },
504 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
505 	    ip_ether_v6intfid },
506 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
507 	    ip_ib_v6intfid },
508 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
509 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
510 	    ip_nodef_v6intfid }
511 };
512 
513 static ill_t	ill_null;		/* Empty ILL for init. */
514 char	ipif_loopback_name[] = "lo0";
515 static char *ipv4_forward_suffix = ":ip_forwarding";
516 static char *ipv6_forward_suffix = ":ip6_forwarding";
517 static	sin6_t	sin6_null;	/* Zero address for quick clears */
518 static	sin_t	sin_null;	/* Zero address for quick clears */
519 
520 /* When set search for unused ipif_seqid */
521 static ipif_t	ipif_zero;
522 
523 /*
524  * ppa arena is created after these many
525  * interfaces have been plumbed.
526  */
527 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
528 
529 /*
530  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
531  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
532  * set through platform specific code (Niagara/Ontario).
533  */
534 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
535 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
536 
537 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
538 
539 static uint_t
540 ipif_rand(ip_stack_t *ipst)
541 {
542 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
543 	    12345;
544 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
545 }
546 
547 /*
548  * Allocate per-interface mibs.
549  * Returns true if ok. False otherwise.
550  *  ipsq  may not yet be allocated (loopback case ).
551  */
552 static boolean_t
553 ill_allocate_mibs(ill_t *ill)
554 {
555 	/* Already allocated? */
556 	if (ill->ill_ip_mib != NULL) {
557 		if (ill->ill_isv6)
558 			ASSERT(ill->ill_icmp6_mib != NULL);
559 		return (B_TRUE);
560 	}
561 
562 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
563 	    KM_NOSLEEP);
564 	if (ill->ill_ip_mib == NULL) {
565 		return (B_FALSE);
566 	}
567 
568 	/* Setup static information */
569 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
570 	    sizeof (mib2_ipIfStatsEntry_t));
571 	if (ill->ill_isv6) {
572 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
573 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
574 		    sizeof (mib2_ipv6AddrEntry_t));
575 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
576 		    sizeof (mib2_ipv6RouteEntry_t));
577 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
578 		    sizeof (mib2_ipv6NetToMediaEntry_t));
579 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
580 		    sizeof (ipv6_member_t));
581 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
582 		    sizeof (ipv6_grpsrc_t));
583 	} else {
584 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
585 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
586 		    sizeof (mib2_ipAddrEntry_t));
587 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
588 		    sizeof (mib2_ipRouteEntry_t));
589 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
590 		    sizeof (mib2_ipNetToMediaEntry_t));
591 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
592 		    sizeof (ip_member_t));
593 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
594 		    sizeof (ip_grpsrc_t));
595 
596 		/*
597 		 * For a v4 ill, we are done at this point, because per ill
598 		 * icmp mibs are only used for v6.
599 		 */
600 		return (B_TRUE);
601 	}
602 
603 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
604 	    KM_NOSLEEP);
605 	if (ill->ill_icmp6_mib == NULL) {
606 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
607 		ill->ill_ip_mib = NULL;
608 		return (B_FALSE);
609 	}
610 	/* static icmp info */
611 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
612 	    sizeof (mib2_ipv6IfIcmpEntry_t);
613 	/*
614 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
615 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
616 	 * -> ill_phyint_reinit
617 	 */
618 	return (B_TRUE);
619 }
620 
621 /*
622  * Common code for preparation of ARP commands.  Two points to remember:
623  * 	1) The ill_name is tacked on at the end of the allocated space so
624  *	   the templates name_offset field must contain the total space
625  *	   to allocate less the name length.
626  *
627  *	2) The templates name_length field should contain the *template*
628  *	   length.  We use it as a parameter to bcopy() and then write
629  *	   the real ill_name_length into the name_length field of the copy.
630  * (Always called as writer.)
631  */
632 mblk_t *
633 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
634 {
635 	arc_t	*arc = (arc_t *)template;
636 	char	*cp;
637 	int	len;
638 	mblk_t	*mp;
639 	uint_t	name_length = ill->ill_name_length;
640 	uint_t	template_len = arc->arc_name_length;
641 
642 	len = arc->arc_name_offset + name_length;
643 	mp = allocb(len, BPRI_HI);
644 	if (mp == NULL)
645 		return (NULL);
646 	cp = (char *)mp->b_rptr;
647 	mp->b_wptr = (uchar_t *)&cp[len];
648 	if (template_len)
649 		bcopy(template, cp, template_len);
650 	if (len > template_len)
651 		bzero(&cp[template_len], len - template_len);
652 	mp->b_datap->db_type = M_PROTO;
653 
654 	arc = (arc_t *)cp;
655 	arc->arc_name_length = name_length;
656 	cp = (char *)arc + arc->arc_name_offset;
657 	bcopy(ill->ill_name, cp, name_length);
658 
659 	if (addr) {
660 		area_t	*area = (area_t *)mp->b_rptr;
661 
662 		cp = (char *)area + area->area_proto_addr_offset;
663 		bcopy(addr, cp, area->area_proto_addr_length);
664 		if (area->area_cmd == AR_ENTRY_ADD) {
665 			cp = (char *)area;
666 			len = area->area_proto_addr_length;
667 			if (area->area_proto_mask_offset)
668 				cp += area->area_proto_mask_offset;
669 			else
670 				cp += area->area_proto_addr_offset + len;
671 			while (len-- > 0)
672 				*cp++ = (char)~0;
673 		}
674 	}
675 	return (mp);
676 }
677 
678 mblk_t *
679 ipif_area_alloc(ipif_t *ipif)
680 {
681 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
682 	    (char *)&ipif->ipif_lcl_addr));
683 }
684 
685 mblk_t *
686 ipif_ared_alloc(ipif_t *ipif)
687 {
688 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
689 	    (char *)&ipif->ipif_lcl_addr));
690 }
691 
692 mblk_t *
693 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
694 {
695 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
696 	    (char *)&addr));
697 }
698 
699 /*
700  * Completely vaporize a lower level tap and all associated interfaces.
701  * ill_delete is called only out of ip_close when the device control
702  * stream is being closed.
703  */
704 void
705 ill_delete(ill_t *ill)
706 {
707 	ipif_t	*ipif;
708 	ill_t	*prev_ill;
709 	ip_stack_t	*ipst = ill->ill_ipst;
710 
711 	/*
712 	 * ill_delete may be forcibly entering the ipsq. The previous
713 	 * ioctl may not have completed and may need to be aborted.
714 	 * ipsq_flush takes care of it. If we don't need to enter the
715 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
716 	 * ill_delete_tail is sufficient.
717 	 */
718 	ipsq_flush(ill);
719 
720 	/*
721 	 * Nuke all interfaces.  ipif_free will take down the interface,
722 	 * remove it from the list, and free the data structure.
723 	 * Walk down the ipif list and remove the logical interfaces
724 	 * first before removing the main ipif. We can't unplumb
725 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
726 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
727 	 * POINTOPOINT.
728 	 *
729 	 * If ill_ipif was not properly initialized (i.e low on memory),
730 	 * then no interfaces to clean up. In this case just clean up the
731 	 * ill.
732 	 */
733 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
734 		ipif_free(ipif);
735 
736 	/*
737 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
738 	 * So nobody can be using this mp now. Free the mp allocated for
739 	 * honoring ILLF_NOARP
740 	 */
741 	freemsg(ill->ill_arp_on_mp);
742 	ill->ill_arp_on_mp = NULL;
743 
744 	/* Clean up msgs on pending upcalls for mrouted */
745 	reset_mrt_ill(ill);
746 
747 	/*
748 	 * ipif_free -> reset_conn_ipif will remove all multicast
749 	 * references for IPv4. For IPv6, we need to do it here as
750 	 * it points only at ills.
751 	 */
752 	reset_conn_ill(ill);
753 
754 	/*
755 	 * ill_down will arrange to blow off any IRE's dependent on this
756 	 * ILL, and shut down fragmentation reassembly.
757 	 */
758 	ill_down(ill);
759 
760 	/* Let SCTP know, so that it can remove this from its list. */
761 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
762 
763 	/*
764 	 * If an address on this ILL is being used as a source address then
765 	 * clear out the pointers in other ILLs that point to this ILL.
766 	 */
767 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
768 	if (ill->ill_usesrc_grp_next != NULL) {
769 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
770 			ill_disband_usesrc_group(ill);
771 		} else {	/* consumer of the usesrc ILL */
772 			prev_ill = ill_prev_usesrc(ill);
773 			prev_ill->ill_usesrc_grp_next =
774 			    ill->ill_usesrc_grp_next;
775 		}
776 	}
777 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
778 }
779 
780 static void
781 ipif_non_duplicate(ipif_t *ipif)
782 {
783 	ill_t *ill = ipif->ipif_ill;
784 	mutex_enter(&ill->ill_lock);
785 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
786 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
787 		ASSERT(ill->ill_ipif_dup_count > 0);
788 		ill->ill_ipif_dup_count--;
789 	}
790 	mutex_exit(&ill->ill_lock);
791 }
792 
793 /*
794  * ill_delete_tail is called from ip_modclose after all references
795  * to the closing ill are gone. The wait is done in ip_modclose
796  */
797 void
798 ill_delete_tail(ill_t *ill)
799 {
800 	mblk_t	**mpp;
801 	ipif_t	*ipif;
802 	ip_stack_t	*ipst = ill->ill_ipst;
803 
804 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
805 		ipif_non_duplicate(ipif);
806 		ipif_down_tail(ipif);
807 	}
808 
809 	ASSERT(ill->ill_ipif_dup_count == 0 &&
810 	    ill->ill_arp_down_mp == NULL &&
811 	    ill->ill_arp_del_mapping_mp == NULL);
812 
813 	/*
814 	 * If polling capability is enabled (which signifies direct
815 	 * upcall into IP and driver has ill saved as a handle),
816 	 * we need to make sure that unbind has completed before we
817 	 * let the ill disappear and driver no longer has any reference
818 	 * to this ill.
819 	 */
820 	mutex_enter(&ill->ill_lock);
821 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
822 		cv_wait(&ill->ill_cv, &ill->ill_lock);
823 	mutex_exit(&ill->ill_lock);
824 
825 	/*
826 	 * Clean up polling and soft ring capabilities
827 	 */
828 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
829 		ill_capability_dls_disable(ill);
830 
831 	if (ill->ill_net_type != IRE_LOOPBACK)
832 		qprocsoff(ill->ill_rq);
833 
834 	/*
835 	 * We do an ipsq_flush once again now. New messages could have
836 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
837 	 * could also have landed up if an ioctl thread had looked up
838 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
839 	 * enqueued the ioctl when we did the ipsq_flush last time.
840 	 */
841 	ipsq_flush(ill);
842 
843 	/*
844 	 * Free capabilities.
845 	 */
846 	if (ill->ill_ipsec_capab_ah != NULL) {
847 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
848 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
849 		ill->ill_ipsec_capab_ah = NULL;
850 	}
851 
852 	if (ill->ill_ipsec_capab_esp != NULL) {
853 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
854 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
855 		ill->ill_ipsec_capab_esp = NULL;
856 	}
857 
858 	if (ill->ill_mdt_capab != NULL) {
859 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
860 		ill->ill_mdt_capab = NULL;
861 	}
862 
863 	if (ill->ill_hcksum_capab != NULL) {
864 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
865 		ill->ill_hcksum_capab = NULL;
866 	}
867 
868 	if (ill->ill_zerocopy_capab != NULL) {
869 		kmem_free(ill->ill_zerocopy_capab,
870 		    sizeof (ill_zerocopy_capab_t));
871 		ill->ill_zerocopy_capab = NULL;
872 	}
873 
874 	if (ill->ill_lso_capab != NULL) {
875 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
876 		ill->ill_lso_capab = NULL;
877 	}
878 
879 	if (ill->ill_dls_capab != NULL) {
880 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
881 		ill->ill_dls_capab->ill_unbind_conn = NULL;
882 		kmem_free(ill->ill_dls_capab,
883 		    sizeof (ill_dls_capab_t) +
884 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
885 		ill->ill_dls_capab = NULL;
886 	}
887 
888 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
889 
890 	while (ill->ill_ipif != NULL)
891 		ipif_free_tail(ill->ill_ipif);
892 
893 	/*
894 	 * We have removed all references to ilm from conn and the ones joined
895 	 * within the kernel.
896 	 *
897 	 * We don't walk conns, mrts and ires because
898 	 *
899 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
900 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
901 	 *    ill references.
902 	 */
903 	ASSERT(ilm_walk_ill(ill) == 0);
904 	/*
905 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
906 	 * could free the phyint. No more reference to the phyint after this
907 	 * point.
908 	 */
909 	(void) ill_glist_delete(ill);
910 
911 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
912 	if (ill->ill_ndd_name != NULL)
913 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
914 	rw_exit(&ipst->ips_ip_g_nd_lock);
915 
916 
917 	if (ill->ill_frag_ptr != NULL) {
918 		uint_t count;
919 
920 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
921 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
922 		}
923 		mi_free(ill->ill_frag_ptr);
924 		ill->ill_frag_ptr = NULL;
925 		ill->ill_frag_hash_tbl = NULL;
926 	}
927 
928 	freemsg(ill->ill_nd_lla_mp);
929 	/* Free all retained control messages. */
930 	mpp = &ill->ill_first_mp_to_free;
931 	do {
932 		while (mpp[0]) {
933 			mblk_t  *mp;
934 			mblk_t  *mp1;
935 
936 			mp = mpp[0];
937 			mpp[0] = mp->b_next;
938 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
939 				mp1->b_next = NULL;
940 				mp1->b_prev = NULL;
941 			}
942 			freemsg(mp);
943 		}
944 	} while (mpp++ != &ill->ill_last_mp_to_free);
945 
946 	ill_free_mib(ill);
947 
948 #ifdef DEBUG
949 	ill_trace_cleanup(ill);
950 #endif
951 
952 	/* Drop refcnt here */
953 	netstack_rele(ill->ill_ipst->ips_netstack);
954 	ill->ill_ipst = NULL;
955 }
956 
957 static void
958 ill_free_mib(ill_t *ill)
959 {
960 	ip_stack_t *ipst = ill->ill_ipst;
961 
962 	/*
963 	 * MIB statistics must not be lost, so when an interface
964 	 * goes away the counter values will be added to the global
965 	 * MIBs.
966 	 */
967 	if (ill->ill_ip_mib != NULL) {
968 		if (ill->ill_isv6) {
969 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
970 			    ill->ill_ip_mib);
971 		} else {
972 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
973 			    ill->ill_ip_mib);
974 		}
975 
976 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
977 		ill->ill_ip_mib = NULL;
978 	}
979 	if (ill->ill_icmp6_mib != NULL) {
980 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
981 		    ill->ill_icmp6_mib);
982 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
983 		ill->ill_icmp6_mib = NULL;
984 	}
985 }
986 
987 /*
988  * Concatenate together a physical address and a sap.
989  *
990  * Sap_lengths are interpreted as follows:
991  *   sap_length == 0	==>	no sap
992  *   sap_length > 0	==>	sap is at the head of the dlpi address
993  *   sap_length < 0	==>	sap is at the tail of the dlpi address
994  */
995 static void
996 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
997     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
998 {
999 	uint16_t sap_addr = (uint16_t)sap_src;
1000 
1001 	if (sap_length == 0) {
1002 		if (phys_src == NULL)
1003 			bzero(dst, phys_length);
1004 		else
1005 			bcopy(phys_src, dst, phys_length);
1006 	} else if (sap_length < 0) {
1007 		if (phys_src == NULL)
1008 			bzero(dst, phys_length);
1009 		else
1010 			bcopy(phys_src, dst, phys_length);
1011 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1012 	} else {
1013 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1014 		if (phys_src == NULL)
1015 			bzero((char *)dst + sap_length, phys_length);
1016 		else
1017 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1018 	}
1019 }
1020 
1021 /*
1022  * Generate a dl_unitdata_req mblk for the device and address given.
1023  * addr_length is the length of the physical portion of the address.
1024  * If addr is NULL include an all zero address of the specified length.
1025  * TRUE? In any case, addr_length is taken to be the entire length of the
1026  * dlpi address, including the absolute value of sap_length.
1027  */
1028 mblk_t *
1029 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1030 		t_scalar_t sap_length)
1031 {
1032 	dl_unitdata_req_t *dlur;
1033 	mblk_t	*mp;
1034 	t_scalar_t	abs_sap_length;		/* absolute value */
1035 
1036 	abs_sap_length = ABS(sap_length);
1037 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1038 	    DL_UNITDATA_REQ);
1039 	if (mp == NULL)
1040 		return (NULL);
1041 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1042 	/* HACK: accomodate incompatible DLPI drivers */
1043 	if (addr_length == 8)
1044 		addr_length = 6;
1045 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1046 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1047 	dlur->dl_priority.dl_min = 0;
1048 	dlur->dl_priority.dl_max = 0;
1049 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1050 	    (uchar_t *)&dlur[1]);
1051 	return (mp);
1052 }
1053 
1054 /*
1055  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1056  * Return an error if we already have 1 or more ioctls in progress.
1057  * This is used only for non-exclusive ioctls. Currently this is used
1058  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1059  * and thus need to use ipsq_pending_mp_add.
1060  */
1061 boolean_t
1062 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1063 {
1064 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1065 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1066 	/*
1067 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1068 	 */
1069 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1070 	    (add_mp->b_datap->db_type == M_IOCTL));
1071 
1072 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1073 	/*
1074 	 * Return error if the conn has started closing. The conn
1075 	 * could have finished cleaning up the pending mp list,
1076 	 * If so we should not add another mp to the list negating
1077 	 * the cleanup.
1078 	 */
1079 	if (connp->conn_state_flags & CONN_CLOSING)
1080 		return (B_FALSE);
1081 	/*
1082 	 * Add the pending mp to the head of the list, chained by b_next.
1083 	 * Note down the conn on which the ioctl request came, in b_prev.
1084 	 * This will be used to later get the conn, when we get a response
1085 	 * on the ill queue, from some other module (typically arp)
1086 	 */
1087 	add_mp->b_next = (void *)ill->ill_pending_mp;
1088 	add_mp->b_queue = CONNP_TO_WQ(connp);
1089 	ill->ill_pending_mp = add_mp;
1090 	if (connp != NULL)
1091 		connp->conn_oper_pending_ill = ill;
1092 	return (B_TRUE);
1093 }
1094 
1095 /*
1096  * Retrieve the ill_pending_mp and return it. We have to walk the list
1097  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1098  */
1099 mblk_t *
1100 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1101 {
1102 	mblk_t	*prev = NULL;
1103 	mblk_t	*curr = NULL;
1104 	uint_t	id;
1105 	conn_t	*connp;
1106 
1107 	/*
1108 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1109 	 * up the pending mp, but it does not know the ioc_id and
1110 	 * passes in a zero for it.
1111 	 */
1112 	mutex_enter(&ill->ill_lock);
1113 	if (ioc_id != 0)
1114 		*connpp = NULL;
1115 
1116 	/* Search the list for the appropriate ioctl based on ioc_id */
1117 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1118 	    prev = curr, curr = curr->b_next) {
1119 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1120 		connp = Q_TO_CONN(curr->b_queue);
1121 		/* Match based on the ioc_id or based on the conn */
1122 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1123 			break;
1124 	}
1125 
1126 	if (curr != NULL) {
1127 		/* Unlink the mblk from the pending mp list */
1128 		if (prev != NULL) {
1129 			prev->b_next = curr->b_next;
1130 		} else {
1131 			ASSERT(ill->ill_pending_mp == curr);
1132 			ill->ill_pending_mp = curr->b_next;
1133 		}
1134 
1135 		/*
1136 		 * conn refcnt must have been bumped up at the start of
1137 		 * the ioctl. So we can safely access the conn.
1138 		 */
1139 		ASSERT(CONN_Q(curr->b_queue));
1140 		*connpp = Q_TO_CONN(curr->b_queue);
1141 		curr->b_next = NULL;
1142 		curr->b_queue = NULL;
1143 	}
1144 
1145 	mutex_exit(&ill->ill_lock);
1146 
1147 	return (curr);
1148 }
1149 
1150 /*
1151  * Add the pending mp to the list. There can be only 1 pending mp
1152  * in the list. Any exclusive ioctl that needs to wait for a response
1153  * from another module or driver needs to use this function to set
1154  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1155  * the other module/driver. This is also used while waiting for the
1156  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1157  */
1158 boolean_t
1159 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1160     int waitfor)
1161 {
1162 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1163 
1164 	ASSERT(IAM_WRITER_IPIF(ipif));
1165 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1166 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1167 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1168 	/*
1169 	 * The caller may be using a different ipif than the one passed into
1170 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1171 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1172 	 * that `ipsq_current_ipif == ipif'.
1173 	 */
1174 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1175 
1176 	/*
1177 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1178 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1179 	 */
1180 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1181 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1182 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1183 
1184 	if (connp != NULL) {
1185 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1186 		/*
1187 		 * Return error if the conn has started closing. The conn
1188 		 * could have finished cleaning up the pending mp list,
1189 		 * If so we should not add another mp to the list negating
1190 		 * the cleanup.
1191 		 */
1192 		if (connp->conn_state_flags & CONN_CLOSING)
1193 			return (B_FALSE);
1194 	}
1195 	mutex_enter(&ipsq->ipsq_lock);
1196 	ipsq->ipsq_pending_ipif = ipif;
1197 	/*
1198 	 * Note down the queue in b_queue. This will be returned by
1199 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1200 	 * the processing
1201 	 */
1202 	add_mp->b_next = NULL;
1203 	add_mp->b_queue = q;
1204 	ipsq->ipsq_pending_mp = add_mp;
1205 	ipsq->ipsq_waitfor = waitfor;
1206 
1207 	if (connp != NULL)
1208 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1209 	mutex_exit(&ipsq->ipsq_lock);
1210 	return (B_TRUE);
1211 }
1212 
1213 /*
1214  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1215  * queued in the list.
1216  */
1217 mblk_t *
1218 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1219 {
1220 	mblk_t	*curr = NULL;
1221 
1222 	mutex_enter(&ipsq->ipsq_lock);
1223 	*connpp = NULL;
1224 	if (ipsq->ipsq_pending_mp == NULL) {
1225 		mutex_exit(&ipsq->ipsq_lock);
1226 		return (NULL);
1227 	}
1228 
1229 	/* There can be only 1 such excl message */
1230 	curr = ipsq->ipsq_pending_mp;
1231 	ASSERT(curr != NULL && curr->b_next == NULL);
1232 	ipsq->ipsq_pending_ipif = NULL;
1233 	ipsq->ipsq_pending_mp = NULL;
1234 	ipsq->ipsq_waitfor = 0;
1235 	mutex_exit(&ipsq->ipsq_lock);
1236 
1237 	if (CONN_Q(curr->b_queue)) {
1238 		/*
1239 		 * This mp did a refhold on the conn, at the start of the ioctl.
1240 		 * So we can safely return a pointer to the conn to the caller.
1241 		 */
1242 		*connpp = Q_TO_CONN(curr->b_queue);
1243 	} else {
1244 		*connpp = NULL;
1245 	}
1246 	curr->b_next = NULL;
1247 	curr->b_prev = NULL;
1248 	return (curr);
1249 }
1250 
1251 /*
1252  * Cleanup the ioctl mp queued in ipsq_pending_mp
1253  * - Called in the ill_delete path
1254  * - Called in the M_ERROR or M_HANGUP path on the ill.
1255  * - Called in the conn close path.
1256  */
1257 boolean_t
1258 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1259 {
1260 	mblk_t	*mp;
1261 	ipsq_t	*ipsq;
1262 	queue_t	*q;
1263 	ipif_t	*ipif;
1264 
1265 	ASSERT(IAM_WRITER_ILL(ill));
1266 	ipsq = ill->ill_phyint->phyint_ipsq;
1267 	mutex_enter(&ipsq->ipsq_lock);
1268 	/*
1269 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1270 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1271 	 * even if it is meant for another ill, since we have to enqueue
1272 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1273 	 * If connp is non-null we are called from the conn close path.
1274 	 */
1275 	mp = ipsq->ipsq_pending_mp;
1276 	if (mp == NULL || (connp != NULL &&
1277 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1278 		mutex_exit(&ipsq->ipsq_lock);
1279 		return (B_FALSE);
1280 	}
1281 	/* Now remove from the ipsq_pending_mp */
1282 	ipsq->ipsq_pending_mp = NULL;
1283 	q = mp->b_queue;
1284 	mp->b_next = NULL;
1285 	mp->b_prev = NULL;
1286 	mp->b_queue = NULL;
1287 
1288 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1289 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1290 	if (ill->ill_move_in_progress) {
1291 		ILL_CLEAR_MOVE(ill);
1292 	} else if (ill->ill_up_ipifs) {
1293 		ill_group_cleanup(ill);
1294 	}
1295 
1296 	ipif = ipsq->ipsq_pending_ipif;
1297 	ipsq->ipsq_pending_ipif = NULL;
1298 	ipsq->ipsq_waitfor = 0;
1299 	ipsq->ipsq_current_ipif = NULL;
1300 	ipsq->ipsq_current_ioctl = 0;
1301 	ipsq->ipsq_current_done = B_TRUE;
1302 	mutex_exit(&ipsq->ipsq_lock);
1303 
1304 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1305 		if (connp == NULL) {
1306 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1307 		} else {
1308 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1309 			mutex_enter(&ipif->ipif_ill->ill_lock);
1310 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1311 			mutex_exit(&ipif->ipif_ill->ill_lock);
1312 		}
1313 	} else {
1314 		/*
1315 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1316 		 * be just inet_freemsg. we have to restart it
1317 		 * otherwise the thread will be stuck.
1318 		 */
1319 		inet_freemsg(mp);
1320 	}
1321 	return (B_TRUE);
1322 }
1323 
1324 /*
1325  * The ill is closing. Cleanup all the pending mps. Called exclusively
1326  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1327  * knows this ill, and hence nobody can add an mp to this list
1328  */
1329 static void
1330 ill_pending_mp_cleanup(ill_t *ill)
1331 {
1332 	mblk_t	*mp;
1333 	queue_t	*q;
1334 
1335 	ASSERT(IAM_WRITER_ILL(ill));
1336 
1337 	mutex_enter(&ill->ill_lock);
1338 	/*
1339 	 * Every mp on the pending mp list originating from an ioctl
1340 	 * added 1 to the conn refcnt, at the start of the ioctl.
1341 	 * So bump it down now.  See comments in ip_wput_nondata()
1342 	 */
1343 	while (ill->ill_pending_mp != NULL) {
1344 		mp = ill->ill_pending_mp;
1345 		ill->ill_pending_mp = mp->b_next;
1346 		mutex_exit(&ill->ill_lock);
1347 
1348 		q = mp->b_queue;
1349 		ASSERT(CONN_Q(q));
1350 		mp->b_next = NULL;
1351 		mp->b_prev = NULL;
1352 		mp->b_queue = NULL;
1353 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1354 		mutex_enter(&ill->ill_lock);
1355 	}
1356 	ill->ill_pending_ipif = NULL;
1357 
1358 	mutex_exit(&ill->ill_lock);
1359 }
1360 
1361 /*
1362  * Called in the conn close path and ill delete path
1363  */
1364 static void
1365 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1366 {
1367 	ipsq_t	*ipsq;
1368 	mblk_t	*prev;
1369 	mblk_t	*curr;
1370 	mblk_t	*next;
1371 	queue_t	*q;
1372 	mblk_t	*tmp_list = NULL;
1373 
1374 	ASSERT(IAM_WRITER_ILL(ill));
1375 	if (connp != NULL)
1376 		q = CONNP_TO_WQ(connp);
1377 	else
1378 		q = ill->ill_wq;
1379 
1380 	ipsq = ill->ill_phyint->phyint_ipsq;
1381 	/*
1382 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1383 	 * In the case of ioctl from a conn, there can be only 1 mp
1384 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1385 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1386 	 * ioctls meant for this ill form conn's are not flushed. They will
1387 	 * be processed during ipsq_exit and will not find the ill and will
1388 	 * return error.
1389 	 */
1390 	mutex_enter(&ipsq->ipsq_lock);
1391 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1392 	    curr = next) {
1393 		next = curr->b_next;
1394 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1395 			/* Unlink the mblk from the pending mp list */
1396 			if (prev != NULL) {
1397 				prev->b_next = curr->b_next;
1398 			} else {
1399 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1400 				ipsq->ipsq_xopq_mphead = curr->b_next;
1401 			}
1402 			if (ipsq->ipsq_xopq_mptail == curr)
1403 				ipsq->ipsq_xopq_mptail = prev;
1404 			/*
1405 			 * Create a temporary list and release the ipsq lock
1406 			 * New elements are added to the head of the tmp_list
1407 			 */
1408 			curr->b_next = tmp_list;
1409 			tmp_list = curr;
1410 		} else {
1411 			prev = curr;
1412 		}
1413 	}
1414 	mutex_exit(&ipsq->ipsq_lock);
1415 
1416 	while (tmp_list != NULL) {
1417 		curr = tmp_list;
1418 		tmp_list = curr->b_next;
1419 		curr->b_next = NULL;
1420 		curr->b_prev = NULL;
1421 		curr->b_queue = NULL;
1422 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1423 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1424 			    CONN_CLOSE : NO_COPYOUT, NULL);
1425 		} else {
1426 			/*
1427 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1428 			 * this can't be just inet_freemsg. we have to
1429 			 * restart it otherwise the thread will be stuck.
1430 			 */
1431 			inet_freemsg(curr);
1432 		}
1433 	}
1434 }
1435 
1436 /*
1437  * This conn has started closing. Cleanup any pending ioctl from this conn.
1438  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1439  */
1440 void
1441 conn_ioctl_cleanup(conn_t *connp)
1442 {
1443 	mblk_t *curr;
1444 	ipsq_t	*ipsq;
1445 	ill_t	*ill;
1446 	boolean_t refheld;
1447 
1448 	/*
1449 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1450 	 * ioctl has not yet started, the mp is pending in the list headed by
1451 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1452 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1453 	 * is currently executing now the mp is not queued anywhere but
1454 	 * conn_oper_pending_ill is null. The conn close will wait
1455 	 * till the conn_ref drops to zero.
1456 	 */
1457 	mutex_enter(&connp->conn_lock);
1458 	ill = connp->conn_oper_pending_ill;
1459 	if (ill == NULL) {
1460 		mutex_exit(&connp->conn_lock);
1461 		return;
1462 	}
1463 
1464 	curr = ill_pending_mp_get(ill, &connp, 0);
1465 	if (curr != NULL) {
1466 		mutex_exit(&connp->conn_lock);
1467 		CONN_DEC_REF(connp);
1468 		inet_freemsg(curr);
1469 		return;
1470 	}
1471 	/*
1472 	 * We may not be able to refhold the ill if the ill/ipif
1473 	 * is changing. But we need to make sure that the ill will
1474 	 * not vanish. So we just bump up the ill_waiter count.
1475 	 */
1476 	refheld = ill_waiter_inc(ill);
1477 	mutex_exit(&connp->conn_lock);
1478 	if (refheld) {
1479 		if (ipsq_enter(ill, B_TRUE)) {
1480 			ill_waiter_dcr(ill);
1481 			/*
1482 			 * Check whether this ioctl has started and is
1483 			 * pending now in ipsq_pending_mp. If it is not
1484 			 * found there then check whether this ioctl has
1485 			 * not even started and is in the ipsq_xopq list.
1486 			 */
1487 			if (!ipsq_pending_mp_cleanup(ill, connp))
1488 				ipsq_xopq_mp_cleanup(ill, connp);
1489 			ipsq = ill->ill_phyint->phyint_ipsq;
1490 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1491 			return;
1492 		}
1493 	}
1494 
1495 	/*
1496 	 * The ill is also closing and we could not bump up the
1497 	 * ill_waiter_count or we could not enter the ipsq. Leave
1498 	 * the cleanup to ill_delete
1499 	 */
1500 	mutex_enter(&connp->conn_lock);
1501 	while (connp->conn_oper_pending_ill != NULL)
1502 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1503 	mutex_exit(&connp->conn_lock);
1504 	if (refheld)
1505 		ill_waiter_dcr(ill);
1506 }
1507 
1508 /*
1509  * ipcl_walk function for cleaning up conn_*_ill fields.
1510  */
1511 static void
1512 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1513 {
1514 	ill_t	*ill = (ill_t *)arg;
1515 	ire_t	*ire;
1516 
1517 	mutex_enter(&connp->conn_lock);
1518 	if (connp->conn_multicast_ill == ill) {
1519 		/* Revert to late binding */
1520 		connp->conn_multicast_ill = NULL;
1521 		connp->conn_orig_multicast_ifindex = 0;
1522 	}
1523 	if (connp->conn_incoming_ill == ill)
1524 		connp->conn_incoming_ill = NULL;
1525 	if (connp->conn_outgoing_ill == ill)
1526 		connp->conn_outgoing_ill = NULL;
1527 	if (connp->conn_outgoing_pill == ill)
1528 		connp->conn_outgoing_pill = NULL;
1529 	if (connp->conn_nofailover_ill == ill)
1530 		connp->conn_nofailover_ill = NULL;
1531 	if (connp->conn_dhcpinit_ill == ill) {
1532 		connp->conn_dhcpinit_ill = NULL;
1533 		ASSERT(ill->ill_dhcpinit != 0);
1534 		atomic_dec_32(&ill->ill_dhcpinit);
1535 	}
1536 	if (connp->conn_ire_cache != NULL) {
1537 		ire = connp->conn_ire_cache;
1538 		/*
1539 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1540 		 * interface X and ipif coming from interface Y, if interface
1541 		 * X and Y are part of the same IPMPgroup. Thus whenever
1542 		 * interface X goes down, remove all references to it by
1543 		 * checking both on ire_ipif and ire_stq.
1544 		 */
1545 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1546 		    (ire->ire_type == IRE_CACHE &&
1547 		    ire->ire_stq == ill->ill_wq)) {
1548 			connp->conn_ire_cache = NULL;
1549 			mutex_exit(&connp->conn_lock);
1550 			ire_refrele_notr(ire);
1551 			return;
1552 		}
1553 	}
1554 	mutex_exit(&connp->conn_lock);
1555 
1556 }
1557 
1558 /* ARGSUSED */
1559 void
1560 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1561 {
1562 	ill_t	*ill = q->q_ptr;
1563 	ipif_t	*ipif;
1564 
1565 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1566 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1567 		ipif_non_duplicate(ipif);
1568 		ipif_down_tail(ipif);
1569 	}
1570 	freemsg(mp);
1571 	ipsq_current_finish(ipsq);
1572 }
1573 
1574 /*
1575  * ill_down_start is called when we want to down this ill and bring it up again
1576  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1577  * all interfaces, but don't tear down any plumbing.
1578  */
1579 boolean_t
1580 ill_down_start(queue_t *q, mblk_t *mp)
1581 {
1582 	ill_t	*ill = q->q_ptr;
1583 	ipif_t	*ipif;
1584 
1585 	ASSERT(IAM_WRITER_ILL(ill));
1586 
1587 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1588 		(void) ipif_down(ipif, NULL, NULL);
1589 
1590 	ill_down(ill);
1591 
1592 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1593 
1594 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1595 
1596 	/*
1597 	 * Atomically test and add the pending mp if references are active.
1598 	 */
1599 	mutex_enter(&ill->ill_lock);
1600 	if (!ill_is_quiescent(ill)) {
1601 		/* call cannot fail since `conn_t *' argument is NULL */
1602 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1603 		    mp, ILL_DOWN);
1604 		mutex_exit(&ill->ill_lock);
1605 		return (B_FALSE);
1606 	}
1607 	mutex_exit(&ill->ill_lock);
1608 	return (B_TRUE);
1609 }
1610 
1611 static void
1612 ill_down(ill_t *ill)
1613 {
1614 	ip_stack_t	*ipst = ill->ill_ipst;
1615 
1616 	/* Blow off any IREs dependent on this ILL. */
1617 	ire_walk(ill_downi, (char *)ill, ipst);
1618 
1619 	/* Remove any conn_*_ill depending on this ill */
1620 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1621 
1622 	if (ill->ill_group != NULL) {
1623 		illgrp_delete(ill);
1624 	}
1625 }
1626 
1627 /*
1628  * ire_walk routine used to delete every IRE that depends on queues
1629  * associated with 'ill'.  (Always called as writer.)
1630  */
1631 static void
1632 ill_downi(ire_t *ire, char *ill_arg)
1633 {
1634 	ill_t	*ill = (ill_t *)ill_arg;
1635 
1636 	/*
1637 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1638 	 * interface X and ipif coming from interface Y, if interface
1639 	 * X and Y are part of the same IPMP group. Thus whenever interface
1640 	 * X goes down, remove all references to it by checking both
1641 	 * on ire_ipif and ire_stq.
1642 	 */
1643 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1644 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1645 		ire_delete(ire);
1646 	}
1647 }
1648 
1649 /*
1650  * Remove ire/nce from the fastpath list.
1651  */
1652 void
1653 ill_fastpath_nack(ill_t *ill)
1654 {
1655 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1656 }
1657 
1658 /* Consume an M_IOCACK of the fastpath probe. */
1659 void
1660 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1661 {
1662 	mblk_t	*mp1 = mp;
1663 
1664 	/*
1665 	 * If this was the first attempt turn on the fastpath probing.
1666 	 */
1667 	mutex_enter(&ill->ill_lock);
1668 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1669 		ill->ill_dlpi_fastpath_state = IDS_OK;
1670 	mutex_exit(&ill->ill_lock);
1671 
1672 	/* Free the M_IOCACK mblk, hold on to the data */
1673 	mp = mp->b_cont;
1674 	freeb(mp1);
1675 	if (mp == NULL)
1676 		return;
1677 	if (mp->b_cont != NULL) {
1678 		/*
1679 		 * Update all IRE's or NCE's that are waiting for
1680 		 * fastpath update.
1681 		 */
1682 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1683 		mp1 = mp->b_cont;
1684 		freeb(mp);
1685 		mp = mp1;
1686 	} else {
1687 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1688 	}
1689 
1690 	freeb(mp);
1691 }
1692 
1693 /*
1694  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1695  * The data portion of the request is a dl_unitdata_req_t template for
1696  * what we would send downstream in the absence of a fastpath confirmation.
1697  */
1698 int
1699 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1700 {
1701 	struct iocblk	*ioc;
1702 	mblk_t	*mp;
1703 
1704 	if (dlur_mp == NULL)
1705 		return (EINVAL);
1706 
1707 	mutex_enter(&ill->ill_lock);
1708 	switch (ill->ill_dlpi_fastpath_state) {
1709 	case IDS_FAILED:
1710 		/*
1711 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1712 		 * support it.
1713 		 */
1714 		mutex_exit(&ill->ill_lock);
1715 		return (ENOTSUP);
1716 	case IDS_UNKNOWN:
1717 		/* This is the first probe */
1718 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1719 		break;
1720 	default:
1721 		break;
1722 	}
1723 	mutex_exit(&ill->ill_lock);
1724 
1725 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1726 		return (EAGAIN);
1727 
1728 	mp->b_cont = copyb(dlur_mp);
1729 	if (mp->b_cont == NULL) {
1730 		freeb(mp);
1731 		return (EAGAIN);
1732 	}
1733 
1734 	ioc = (struct iocblk *)mp->b_rptr;
1735 	ioc->ioc_count = msgdsize(mp->b_cont);
1736 
1737 	putnext(ill->ill_wq, mp);
1738 	return (0);
1739 }
1740 
1741 void
1742 ill_capability_probe(ill_t *ill)
1743 {
1744 	/*
1745 	 * Do so only if capabilities are still unknown.
1746 	 */
1747 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1748 		return;
1749 
1750 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1751 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1752 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1753 }
1754 
1755 void
1756 ill_capability_reset(ill_t *ill)
1757 {
1758 	mblk_t *sc_mp = NULL;
1759 	mblk_t *tmp;
1760 
1761 	/*
1762 	 * Note here that we reset the state to UNKNOWN, and later send
1763 	 * down the DL_CAPABILITY_REQ without first setting the state to
1764 	 * INPROGRESS.  We do this in order to distinguish the
1765 	 * DL_CAPABILITY_ACK response which may come back in response to
1766 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1767 	 * also handle the case where the driver doesn't send us back
1768 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1769 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1770 	 * features are turned off until the state reaches IDS_OK.
1771 	 */
1772 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1773 	ill->ill_capab_reneg = B_FALSE;
1774 
1775 	/*
1776 	 * Disable sub-capabilities and request a list of sub-capability
1777 	 * messages which will be sent down to the driver.  Each handler
1778 	 * allocates the corresponding dl_capability_sub_t inside an
1779 	 * mblk, and links it to the existing sc_mp mblk, or return it
1780 	 * as sc_mp if it's the first sub-capability (the passed in
1781 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1782 	 * sc_mp will be pulled-up, before passing it downstream.
1783 	 */
1784 	ill_capability_mdt_reset(ill, &sc_mp);
1785 	ill_capability_hcksum_reset(ill, &sc_mp);
1786 	ill_capability_zerocopy_reset(ill, &sc_mp);
1787 	ill_capability_ipsec_reset(ill, &sc_mp);
1788 	ill_capability_dls_reset(ill, &sc_mp);
1789 	ill_capability_lso_reset(ill, &sc_mp);
1790 
1791 	/* Nothing to send down in order to disable the capabilities? */
1792 	if (sc_mp == NULL)
1793 		return;
1794 
1795 	tmp = msgpullup(sc_mp, -1);
1796 	freemsg(sc_mp);
1797 	if ((sc_mp = tmp) == NULL) {
1798 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1799 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1800 		return;
1801 	}
1802 
1803 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1804 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1805 }
1806 
1807 /*
1808  * Request or set new-style hardware capabilities supported by DLS provider.
1809  */
1810 static void
1811 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1812 {
1813 	mblk_t *mp;
1814 	dl_capability_req_t *capb;
1815 	size_t size = 0;
1816 	uint8_t *ptr;
1817 
1818 	if (reqp != NULL)
1819 		size = MBLKL(reqp);
1820 
1821 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1822 	if (mp == NULL) {
1823 		freemsg(reqp);
1824 		return;
1825 	}
1826 	ptr = mp->b_rptr;
1827 
1828 	capb = (dl_capability_req_t *)ptr;
1829 	ptr += sizeof (dl_capability_req_t);
1830 
1831 	if (reqp != NULL) {
1832 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1833 		capb->dl_sub_length = size;
1834 		bcopy(reqp->b_rptr, ptr, size);
1835 		ptr += size;
1836 		mp->b_cont = reqp->b_cont;
1837 		freeb(reqp);
1838 	}
1839 	ASSERT(ptr == mp->b_wptr);
1840 
1841 	ill_dlpi_send(ill, mp);
1842 }
1843 
1844 static void
1845 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1846 {
1847 	dl_capab_id_t *id_ic;
1848 	uint_t sub_dl_cap = outers->dl_cap;
1849 	dl_capability_sub_t *inners;
1850 	uint8_t *capend;
1851 
1852 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1853 
1854 	/*
1855 	 * Note: range checks here are not absolutely sufficient to
1856 	 * make us robust against malformed messages sent by drivers;
1857 	 * this is in keeping with the rest of IP's dlpi handling.
1858 	 * (Remember, it's coming from something else in the kernel
1859 	 * address space)
1860 	 */
1861 
1862 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1863 	if (capend > mp->b_wptr) {
1864 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1865 		    "malformed sub-capability too long for mblk");
1866 		return;
1867 	}
1868 
1869 	id_ic = (dl_capab_id_t *)(outers + 1);
1870 
1871 	if (outers->dl_length < sizeof (*id_ic) ||
1872 	    (inners = &id_ic->id_subcap,
1873 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1874 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1875 		    "encapsulated capab type %d too long for mblk",
1876 		    inners->dl_cap);
1877 		return;
1878 	}
1879 
1880 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1881 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1882 		    "isn't as expected; pass-thru module(s) detected, "
1883 		    "discarding capability\n", inners->dl_cap));
1884 		return;
1885 	}
1886 
1887 	/* Process the encapsulated sub-capability */
1888 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1889 }
1890 
1891 /*
1892  * Process Multidata Transmit capability negotiation ack received from a
1893  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1894  * DL_CAPABILITY_ACK message.
1895  */
1896 static void
1897 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1898 {
1899 	mblk_t *nmp = NULL;
1900 	dl_capability_req_t *oc;
1901 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1902 	ill_mdt_capab_t **ill_mdt_capab;
1903 	uint_t sub_dl_cap = isub->dl_cap;
1904 	uint8_t *capend;
1905 
1906 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1907 
1908 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1909 
1910 	/*
1911 	 * Note: range checks here are not absolutely sufficient to
1912 	 * make us robust against malformed messages sent by drivers;
1913 	 * this is in keeping with the rest of IP's dlpi handling.
1914 	 * (Remember, it's coming from something else in the kernel
1915 	 * address space)
1916 	 */
1917 
1918 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1919 	if (capend > mp->b_wptr) {
1920 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1921 		    "malformed sub-capability too long for mblk");
1922 		return;
1923 	}
1924 
1925 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1926 
1927 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1928 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1929 		    "unsupported MDT sub-capability (version %d, expected %d)",
1930 		    mdt_ic->mdt_version, MDT_VERSION_2);
1931 		return;
1932 	}
1933 
1934 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1935 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1936 		    "capability isn't as expected; pass-thru module(s) "
1937 		    "detected, discarding capability\n"));
1938 		return;
1939 	}
1940 
1941 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1942 
1943 		if (*ill_mdt_capab == NULL) {
1944 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1945 			    KM_NOSLEEP);
1946 
1947 			if (*ill_mdt_capab == NULL) {
1948 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1949 				    "could not enable MDT version %d "
1950 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1951 				    ill->ill_name);
1952 				return;
1953 			}
1954 		}
1955 
1956 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1957 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1958 		    "header spaces, %d max pld bufs, %d span limit)\n",
1959 		    ill->ill_name, MDT_VERSION_2,
1960 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1961 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1962 
1963 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1964 		(*ill_mdt_capab)->ill_mdt_on = 1;
1965 		/*
1966 		 * Round the following values to the nearest 32-bit; ULP
1967 		 * may further adjust them to accomodate for additional
1968 		 * protocol headers.  We pass these values to ULP during
1969 		 * bind time.
1970 		 */
1971 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1972 		    roundup(mdt_ic->mdt_hdr_head, 4);
1973 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1974 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1975 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1976 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1977 
1978 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1979 	} else {
1980 		uint_t size;
1981 		uchar_t *rptr;
1982 
1983 		size = sizeof (dl_capability_req_t) +
1984 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1985 
1986 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1987 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1988 			    "could not enable MDT for %s (ENOMEM)\n",
1989 			    ill->ill_name);
1990 			return;
1991 		}
1992 
1993 		rptr = nmp->b_rptr;
1994 		/* initialize dl_capability_req_t */
1995 		oc = (dl_capability_req_t *)nmp->b_rptr;
1996 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1997 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1998 		    sizeof (dl_capab_mdt_t);
1999 		nmp->b_rptr += sizeof (dl_capability_req_t);
2000 
2001 		/* initialize dl_capability_sub_t */
2002 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2003 		nmp->b_rptr += sizeof (*isub);
2004 
2005 		/* initialize dl_capab_mdt_t */
2006 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2007 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2008 
2009 		nmp->b_rptr = rptr;
2010 
2011 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2012 		    "to enable MDT version %d\n", ill->ill_name,
2013 		    MDT_VERSION_2));
2014 
2015 		/* set ENABLE flag */
2016 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2017 
2018 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2019 		ill_dlpi_send(ill, nmp);
2020 	}
2021 }
2022 
2023 static void
2024 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2025 {
2026 	mblk_t *mp;
2027 	dl_capab_mdt_t *mdt_subcap;
2028 	dl_capability_sub_t *dl_subcap;
2029 	int size;
2030 
2031 	if (!ILL_MDT_CAPABLE(ill))
2032 		return;
2033 
2034 	ASSERT(ill->ill_mdt_capab != NULL);
2035 	/*
2036 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2037 	 * structure since it's possible that another thread is still
2038 	 * referring to it.  The structure only gets deallocated when
2039 	 * we destroy the ill.
2040 	 */
2041 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2042 
2043 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2044 
2045 	mp = allocb(size, BPRI_HI);
2046 	if (mp == NULL) {
2047 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2048 		    "request to disable MDT\n"));
2049 		return;
2050 	}
2051 
2052 	mp->b_wptr = mp->b_rptr + size;
2053 
2054 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2055 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2056 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2057 
2058 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2059 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2060 	mdt_subcap->mdt_flags = 0;
2061 	mdt_subcap->mdt_hdr_head = 0;
2062 	mdt_subcap->mdt_hdr_tail = 0;
2063 
2064 	if (*sc_mp != NULL)
2065 		linkb(*sc_mp, mp);
2066 	else
2067 		*sc_mp = mp;
2068 }
2069 
2070 /*
2071  * Send a DL_NOTIFY_REQ to the specified ill to enable
2072  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2073  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2074  * acceleration.
2075  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2076  */
2077 static boolean_t
2078 ill_enable_promisc_notify(ill_t *ill)
2079 {
2080 	mblk_t *mp;
2081 	dl_notify_req_t *req;
2082 
2083 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2084 
2085 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2086 	if (mp == NULL)
2087 		return (B_FALSE);
2088 
2089 	req = (dl_notify_req_t *)mp->b_rptr;
2090 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2091 	    DL_NOTE_PROMISC_OFF_PHYS;
2092 
2093 	ill_dlpi_send(ill, mp);
2094 
2095 	return (B_TRUE);
2096 }
2097 
2098 
2099 /*
2100  * Allocate an IPsec capability request which will be filled by our
2101  * caller to turn on support for one or more algorithms.
2102  */
2103 static mblk_t *
2104 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2105 {
2106 	mblk_t *nmp;
2107 	dl_capability_req_t	*ocap;
2108 	dl_capab_ipsec_t	*ocip;
2109 	dl_capab_ipsec_t	*icip;
2110 	uint8_t			*ptr;
2111 	icip = (dl_capab_ipsec_t *)(isub + 1);
2112 
2113 	/*
2114 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2115 	 * PROMISC_ON/OFF notification from the provider. We need to
2116 	 * do this before enabling the algorithms to avoid leakage of
2117 	 * cleartext packets.
2118 	 */
2119 
2120 	if (!ill_enable_promisc_notify(ill))
2121 		return (NULL);
2122 
2123 	/*
2124 	 * Allocate new mblk which will contain a new capability
2125 	 * request to enable the capabilities.
2126 	 */
2127 
2128 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2129 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2130 	if (nmp == NULL)
2131 		return (NULL);
2132 
2133 	ptr = nmp->b_rptr;
2134 
2135 	/* initialize dl_capability_req_t */
2136 	ocap = (dl_capability_req_t *)ptr;
2137 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2138 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2139 	ptr += sizeof (dl_capability_req_t);
2140 
2141 	/* initialize dl_capability_sub_t */
2142 	bcopy(isub, ptr, sizeof (*isub));
2143 	ptr += sizeof (*isub);
2144 
2145 	/* initialize dl_capab_ipsec_t */
2146 	ocip = (dl_capab_ipsec_t *)ptr;
2147 	bcopy(icip, ocip, sizeof (*icip));
2148 
2149 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2150 	return (nmp);
2151 }
2152 
2153 /*
2154  * Process an IPsec capability negotiation ack received from a DLS Provider.
2155  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2156  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2157  */
2158 static void
2159 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2160 {
2161 	dl_capab_ipsec_t	*icip;
2162 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2163 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2164 	uint_t cipher, nciphers;
2165 	mblk_t *nmp;
2166 	uint_t alg_len;
2167 	boolean_t need_sadb_dump;
2168 	uint_t sub_dl_cap = isub->dl_cap;
2169 	ill_ipsec_capab_t **ill_capab;
2170 	uint64_t ill_capab_flag;
2171 	uint8_t *capend, *ciphend;
2172 	boolean_t sadb_resync;
2173 
2174 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2175 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2176 
2177 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2178 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2179 		ill_capab_flag = ILL_CAPAB_AH;
2180 	} else {
2181 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2182 		ill_capab_flag = ILL_CAPAB_ESP;
2183 	}
2184 
2185 	/*
2186 	 * If the ill capability structure exists, then this incoming
2187 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2188 	 * If this is so, then we'd need to resynchronize the SADB
2189 	 * after re-enabling the offloaded ciphers.
2190 	 */
2191 	sadb_resync = (*ill_capab != NULL);
2192 
2193 	/*
2194 	 * Note: range checks here are not absolutely sufficient to
2195 	 * make us robust against malformed messages sent by drivers;
2196 	 * this is in keeping with the rest of IP's dlpi handling.
2197 	 * (Remember, it's coming from something else in the kernel
2198 	 * address space)
2199 	 */
2200 
2201 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2202 	if (capend > mp->b_wptr) {
2203 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2204 		    "malformed sub-capability too long for mblk");
2205 		return;
2206 	}
2207 
2208 	/*
2209 	 * There are two types of acks we process here:
2210 	 * 1. acks in reply to a (first form) generic capability req
2211 	 *    (no ENABLE flag set)
2212 	 * 2. acks in reply to a ENABLE capability req.
2213 	 *    (ENABLE flag set)
2214 	 *
2215 	 * We process the subcapability passed as argument as follows:
2216 	 * 1 do initializations
2217 	 *   1.1 initialize nmp = NULL
2218 	 *   1.2 set need_sadb_dump to B_FALSE
2219 	 * 2 for each cipher in subcapability:
2220 	 *   2.1 if ENABLE flag is set:
2221 	 *	2.1.1 update per-ill ipsec capabilities info
2222 	 *	2.1.2 set need_sadb_dump to B_TRUE
2223 	 *   2.2 if ENABLE flag is not set:
2224 	 *	2.2.1 if nmp is NULL:
2225 	 *		2.2.1.1 allocate and initialize nmp
2226 	 *		2.2.1.2 init current pos in nmp
2227 	 *	2.2.2 copy current cipher to current pos in nmp
2228 	 *	2.2.3 set ENABLE flag in nmp
2229 	 *	2.2.4 update current pos
2230 	 * 3 if nmp is not equal to NULL, send enable request
2231 	 *   3.1 send capability request
2232 	 * 4 if need_sadb_dump is B_TRUE
2233 	 *   4.1 enable promiscuous on/off notifications
2234 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2235 	 *	AH or ESP SA's to interface.
2236 	 */
2237 
2238 	nmp = NULL;
2239 	oalg = NULL;
2240 	need_sadb_dump = B_FALSE;
2241 	icip = (dl_capab_ipsec_t *)(isub + 1);
2242 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2243 
2244 	nciphers = icip->cip_nciphers;
2245 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2246 
2247 	if (ciphend > capend) {
2248 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2249 		    "too many ciphers for sub-capability len");
2250 		return;
2251 	}
2252 
2253 	for (cipher = 0; cipher < nciphers; cipher++) {
2254 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2255 
2256 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2257 			/*
2258 			 * TBD: when we provide a way to disable capabilities
2259 			 * from above, need to manage the request-pending state
2260 			 * and fail if we were not expecting this ACK.
2261 			 */
2262 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2263 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2264 
2265 			/*
2266 			 * Update IPsec capabilities for this ill
2267 			 */
2268 
2269 			if (*ill_capab == NULL) {
2270 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2271 				    ("ill_capability_ipsec_ack: "
2272 				    "allocating ipsec_capab for ill\n"));
2273 				*ill_capab = ill_ipsec_capab_alloc();
2274 
2275 				if (*ill_capab == NULL) {
2276 					cmn_err(CE_WARN,
2277 					    "ill_capability_ipsec_ack: "
2278 					    "could not enable IPsec Hardware "
2279 					    "acceleration for %s (ENOMEM)\n",
2280 					    ill->ill_name);
2281 					return;
2282 				}
2283 			}
2284 
2285 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2286 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2287 
2288 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2289 				cmn_err(CE_WARN,
2290 				    "ill_capability_ipsec_ack: "
2291 				    "malformed IPsec algorithm id %d",
2292 				    ialg->alg_prim);
2293 				continue;
2294 			}
2295 
2296 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2297 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2298 				    ialg->alg_prim);
2299 			} else {
2300 				ipsec_capab_algparm_t *alp;
2301 
2302 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2303 				    ialg->alg_prim);
2304 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2305 				    ialg->alg_prim)) {
2306 					cmn_err(CE_WARN,
2307 					    "ill_capability_ipsec_ack: "
2308 					    "no space for IPsec alg id %d",
2309 					    ialg->alg_prim);
2310 					continue;
2311 				}
2312 				alp = &((*ill_capab)->encr_algparm[
2313 				    ialg->alg_prim]);
2314 				alp->minkeylen = ialg->alg_minbits;
2315 				alp->maxkeylen = ialg->alg_maxbits;
2316 			}
2317 			ill->ill_capabilities |= ill_capab_flag;
2318 			/*
2319 			 * indicate that a capability was enabled, which
2320 			 * will be used below to kick off a SADB dump
2321 			 * to the ill.
2322 			 */
2323 			need_sadb_dump = B_TRUE;
2324 		} else {
2325 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2326 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2327 			    ialg->alg_prim));
2328 
2329 			if (nmp == NULL) {
2330 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2331 				if (nmp == NULL) {
2332 					/*
2333 					 * Sending the PROMISC_ON/OFF
2334 					 * notification request failed.
2335 					 * We cannot enable the algorithms
2336 					 * since the Provider will not
2337 					 * notify IP of promiscous mode
2338 					 * changes, which could lead
2339 					 * to leakage of packets.
2340 					 */
2341 					cmn_err(CE_WARN,
2342 					    "ill_capability_ipsec_ack: "
2343 					    "could not enable IPsec Hardware "
2344 					    "acceleration for %s (ENOMEM)\n",
2345 					    ill->ill_name);
2346 					return;
2347 				}
2348 				/* ptr to current output alg specifier */
2349 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2350 			}
2351 
2352 			/*
2353 			 * Copy current alg specifier, set ENABLE
2354 			 * flag, and advance to next output alg.
2355 			 * For now we enable all IPsec capabilities.
2356 			 */
2357 			ASSERT(oalg != NULL);
2358 			bcopy(ialg, oalg, alg_len);
2359 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2360 			nmp->b_wptr += alg_len;
2361 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2362 		}
2363 
2364 		/* move to next input algorithm specifier */
2365 		ialg = (dl_capab_ipsec_alg_t *)
2366 		    ((char *)ialg + alg_len);
2367 	}
2368 
2369 	if (nmp != NULL)
2370 		/*
2371 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2372 		 * IPsec hardware acceleration.
2373 		 */
2374 		ill_dlpi_send(ill, nmp);
2375 
2376 	if (need_sadb_dump)
2377 		/*
2378 		 * An acknowledgement corresponding to a request to
2379 		 * enable acceleration was received, notify SADB.
2380 		 */
2381 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2382 }
2383 
2384 /*
2385  * Given an mblk with enough space in it, create sub-capability entries for
2386  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2387  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2388  * in preparation for the reset the DL_CAPABILITY_REQ message.
2389  */
2390 static void
2391 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2392     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2393 {
2394 	dl_capab_ipsec_t *oipsec;
2395 	dl_capab_ipsec_alg_t *oalg;
2396 	dl_capability_sub_t *dl_subcap;
2397 	int i, k;
2398 
2399 	ASSERT(nciphers > 0);
2400 	ASSERT(ill_cap != NULL);
2401 	ASSERT(mp != NULL);
2402 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2403 
2404 	/* dl_capability_sub_t for "stype" */
2405 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2406 	dl_subcap->dl_cap = stype;
2407 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2408 	mp->b_wptr += sizeof (dl_capability_sub_t);
2409 
2410 	/* dl_capab_ipsec_t for "stype" */
2411 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2412 	oipsec->cip_version = 1;
2413 	oipsec->cip_nciphers = nciphers;
2414 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2415 
2416 	/* create entries for "stype" AUTH ciphers */
2417 	for (i = 0; i < ill_cap->algs_size; i++) {
2418 		for (k = 0; k < BITSPERBYTE; k++) {
2419 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2420 				continue;
2421 
2422 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2423 			bzero((void *)oalg, sizeof (*oalg));
2424 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2425 			oalg->alg_prim = k + (BITSPERBYTE * i);
2426 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2427 		}
2428 	}
2429 	/* create entries for "stype" ENCR ciphers */
2430 	for (i = 0; i < ill_cap->algs_size; i++) {
2431 		for (k = 0; k < BITSPERBYTE; k++) {
2432 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2433 				continue;
2434 
2435 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2436 			bzero((void *)oalg, sizeof (*oalg));
2437 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2438 			oalg->alg_prim = k + (BITSPERBYTE * i);
2439 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2440 		}
2441 	}
2442 }
2443 
2444 /*
2445  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2446  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2447  * POPC instruction, but our macro is more flexible for an arbitrary length
2448  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2449  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2450  * stays that way, we can reduce the number of iterations required.
2451  */
2452 #define	COUNT_1S(val, sum) {					\
2453 	uint8_t x = val & 0xff;					\
2454 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2455 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2456 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2457 }
2458 
2459 /* ARGSUSED */
2460 static void
2461 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2462 {
2463 	mblk_t *mp;
2464 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2465 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2466 	uint64_t ill_capabilities = ill->ill_capabilities;
2467 	int ah_cnt = 0, esp_cnt = 0;
2468 	int ah_len = 0, esp_len = 0;
2469 	int i, size = 0;
2470 
2471 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2472 		return;
2473 
2474 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2475 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2476 
2477 	/* Find out the number of ciphers for AH */
2478 	if (cap_ah != NULL) {
2479 		for (i = 0; i < cap_ah->algs_size; i++) {
2480 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2481 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2482 		}
2483 		if (ah_cnt > 0) {
2484 			size += sizeof (dl_capability_sub_t) +
2485 			    sizeof (dl_capab_ipsec_t);
2486 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2487 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2488 			size += ah_len;
2489 		}
2490 	}
2491 
2492 	/* Find out the number of ciphers for ESP */
2493 	if (cap_esp != NULL) {
2494 		for (i = 0; i < cap_esp->algs_size; i++) {
2495 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2496 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2497 		}
2498 		if (esp_cnt > 0) {
2499 			size += sizeof (dl_capability_sub_t) +
2500 			    sizeof (dl_capab_ipsec_t);
2501 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2502 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2503 			size += esp_len;
2504 		}
2505 	}
2506 
2507 	if (size == 0) {
2508 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2509 		    "there's nothing to reset\n"));
2510 		return;
2511 	}
2512 
2513 	mp = allocb(size, BPRI_HI);
2514 	if (mp == NULL) {
2515 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2516 		    "request to disable IPSEC Hardware Acceleration\n"));
2517 		return;
2518 	}
2519 
2520 	/*
2521 	 * Clear the capability flags for IPsec HA but retain the ill
2522 	 * capability structures since it's possible that another thread
2523 	 * is still referring to them.  The structures only get deallocated
2524 	 * when we destroy the ill.
2525 	 *
2526 	 * Various places check the flags to see if the ill is capable of
2527 	 * hardware acceleration, and by clearing them we ensure that new
2528 	 * outbound IPsec packets are sent down encrypted.
2529 	 */
2530 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2531 
2532 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2533 	if (ah_cnt > 0) {
2534 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2535 		    cap_ah, mp);
2536 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2537 	}
2538 
2539 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2540 	if (esp_cnt > 0) {
2541 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2542 		    cap_esp, mp);
2543 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2544 	}
2545 
2546 	/*
2547 	 * At this point we've composed a bunch of sub-capabilities to be
2548 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2549 	 * by the caller.  Upon receiving this reset message, the driver
2550 	 * must stop inbound decryption (by destroying all inbound SAs)
2551 	 * and let the corresponding packets come in encrypted.
2552 	 */
2553 
2554 	if (*sc_mp != NULL)
2555 		linkb(*sc_mp, mp);
2556 	else
2557 		*sc_mp = mp;
2558 }
2559 
2560 static void
2561 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2562     boolean_t encapsulated)
2563 {
2564 	boolean_t legacy = B_FALSE;
2565 
2566 	/*
2567 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2568 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2569 	 * instructed the driver to disable its advertised capabilities,
2570 	 * so there's no point in accepting any response at this moment.
2571 	 */
2572 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2573 		return;
2574 
2575 	/*
2576 	 * Note that only the following two sub-capabilities may be
2577 	 * considered as "legacy", since their original definitions
2578 	 * do not incorporate the dl_mid_t module ID token, and hence
2579 	 * may require the use of the wrapper sub-capability.
2580 	 */
2581 	switch (subp->dl_cap) {
2582 	case DL_CAPAB_IPSEC_AH:
2583 	case DL_CAPAB_IPSEC_ESP:
2584 		legacy = B_TRUE;
2585 		break;
2586 	}
2587 
2588 	/*
2589 	 * For legacy sub-capabilities which don't incorporate a queue_t
2590 	 * pointer in their structures, discard them if we detect that
2591 	 * there are intermediate modules in between IP and the driver.
2592 	 */
2593 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2594 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2595 		    "%d discarded; %d module(s) present below IP\n",
2596 		    subp->dl_cap, ill->ill_lmod_cnt));
2597 		return;
2598 	}
2599 
2600 	switch (subp->dl_cap) {
2601 	case DL_CAPAB_IPSEC_AH:
2602 	case DL_CAPAB_IPSEC_ESP:
2603 		ill_capability_ipsec_ack(ill, mp, subp);
2604 		break;
2605 	case DL_CAPAB_MDT:
2606 		ill_capability_mdt_ack(ill, mp, subp);
2607 		break;
2608 	case DL_CAPAB_HCKSUM:
2609 		ill_capability_hcksum_ack(ill, mp, subp);
2610 		break;
2611 	case DL_CAPAB_ZEROCOPY:
2612 		ill_capability_zerocopy_ack(ill, mp, subp);
2613 		break;
2614 	case DL_CAPAB_POLL:
2615 		if (!SOFT_RINGS_ENABLED())
2616 			ill_capability_dls_ack(ill, mp, subp);
2617 		break;
2618 	case DL_CAPAB_SOFT_RING:
2619 		if (SOFT_RINGS_ENABLED())
2620 			ill_capability_dls_ack(ill, mp, subp);
2621 		break;
2622 	case DL_CAPAB_LSO:
2623 		ill_capability_lso_ack(ill, mp, subp);
2624 		break;
2625 	default:
2626 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2627 		    subp->dl_cap));
2628 	}
2629 }
2630 
2631 /*
2632  * As part of negotiating polling capability, the driver tells us
2633  * the default (or normal) blanking interval and packet threshold
2634  * (the receive timer fires if blanking interval is reached or
2635  * the packet threshold is reached).
2636  *
2637  * As part of manipulating the polling interval, we always use our
2638  * estimated interval (avg service time * number of packets queued
2639  * on the squeue) but we try to blank for a minimum of
2640  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2641  * packet threshold during this time. When we are not in polling mode
2642  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2643  * rr_min_blank_ratio but up the packet cnt by a ratio of
2644  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2645  * possible although for a shorter interval.
2646  */
2647 #define	RR_MAX_BLANK_RATIO	20
2648 #define	RR_MIN_BLANK_RATIO	10
2649 #define	RR_MAX_PKT_CNT_RATIO	3
2650 #define	RR_MIN_PKT_CNT_RATIO	3
2651 
2652 /*
2653  * These can be tuned via /etc/system.
2654  */
2655 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2656 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2657 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2658 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2659 
2660 static mac_resource_handle_t
2661 ill_ring_add(void *arg, mac_resource_t *mrp)
2662 {
2663 	ill_t			*ill = (ill_t *)arg;
2664 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2665 	ill_rx_ring_t		*rx_ring;
2666 	int			ip_rx_index;
2667 
2668 	ASSERT(mrp != NULL);
2669 	if (mrp->mr_type != MAC_RX_FIFO) {
2670 		return (NULL);
2671 	}
2672 	ASSERT(ill != NULL);
2673 	ASSERT(ill->ill_dls_capab != NULL);
2674 
2675 	mutex_enter(&ill->ill_lock);
2676 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2677 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2678 		ASSERT(rx_ring != NULL);
2679 
2680 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2681 			time_t normal_blank_time =
2682 			    mrfp->mrf_normal_blank_time;
2683 			uint_t normal_pkt_cnt =
2684 			    mrfp->mrf_normal_pkt_count;
2685 
2686 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2687 
2688 	rx_ring->rr_blank = mrfp->mrf_blank;
2689 	rx_ring->rr_handle = mrfp->mrf_arg;
2690 	rx_ring->rr_ill = ill;
2691 	rx_ring->rr_normal_blank_time = normal_blank_time;
2692 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2693 
2694 			rx_ring->rr_max_blank_time =
2695 			    normal_blank_time * rr_max_blank_ratio;
2696 			rx_ring->rr_min_blank_time =
2697 			    normal_blank_time * rr_min_blank_ratio;
2698 			rx_ring->rr_max_pkt_cnt =
2699 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2700 			rx_ring->rr_min_pkt_cnt =
2701 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2702 
2703 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2704 			mutex_exit(&ill->ill_lock);
2705 
2706 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2707 			    (int), ip_rx_index);
2708 			return ((mac_resource_handle_t)rx_ring);
2709 		}
2710 	}
2711 
2712 	/*
2713 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2714 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2715 	 * should be made configurable. Meanwhile it cause no panic because
2716 	 * driver will pass ip_input a NULL handle which will make
2717 	 * IP allocate the default squeue and Polling mode will not
2718 	 * be used for this ring.
2719 	 */
2720 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2721 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2722 
2723 	mutex_exit(&ill->ill_lock);
2724 	return (NULL);
2725 }
2726 
2727 static boolean_t
2728 ill_capability_dls_init(ill_t *ill)
2729 {
2730 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2731 	conn_t 			*connp;
2732 	size_t			sz;
2733 	ip_stack_t *ipst = ill->ill_ipst;
2734 
2735 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2736 		if (ill_dls == NULL) {
2737 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2738 			    "soft_ring enabled for ill=%s (%p) but data "
2739 			    "structs uninitialized\n", ill->ill_name,
2740 			    (void *)ill);
2741 		}
2742 		return (B_TRUE);
2743 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2744 		if (ill_dls == NULL) {
2745 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2746 			    "polling enabled for ill=%s (%p) but data "
2747 			    "structs uninitialized\n", ill->ill_name,
2748 			    (void *)ill);
2749 		}
2750 		return (B_TRUE);
2751 	}
2752 
2753 	if (ill_dls != NULL) {
2754 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2755 		/* Soft_Ring or polling is being re-enabled */
2756 
2757 		connp = ill_dls->ill_unbind_conn;
2758 		ASSERT(rx_ring != NULL);
2759 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2760 		bzero((void *)rx_ring,
2761 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2762 		ill_dls->ill_ring_tbl = rx_ring;
2763 		ill_dls->ill_unbind_conn = connp;
2764 		return (B_TRUE);
2765 	}
2766 
2767 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2768 	    ipst->ips_netstack)) == NULL)
2769 		return (B_FALSE);
2770 
2771 	sz = sizeof (ill_dls_capab_t);
2772 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2773 
2774 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2775 	if (ill_dls == NULL) {
2776 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2777 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2778 		    (void *)ill);
2779 		CONN_DEC_REF(connp);
2780 		return (B_FALSE);
2781 	}
2782 
2783 	/* Allocate space to hold ring table */
2784 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2785 	ill->ill_dls_capab = ill_dls;
2786 	ill_dls->ill_unbind_conn = connp;
2787 	return (B_TRUE);
2788 }
2789 
2790 /*
2791  * ill_capability_dls_disable: disable soft_ring and/or polling
2792  * capability. Since any of the rings might already be in use, need
2793  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2794  * direct calls if necessary.
2795  */
2796 static void
2797 ill_capability_dls_disable(ill_t *ill)
2798 {
2799 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2800 
2801 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2802 		ip_squeue_clean_all(ill);
2803 		ill_dls->ill_tx = NULL;
2804 		ill_dls->ill_tx_handle = NULL;
2805 		ill_dls->ill_dls_change_status = NULL;
2806 		ill_dls->ill_dls_bind = NULL;
2807 		ill_dls->ill_dls_unbind = NULL;
2808 	}
2809 
2810 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2811 }
2812 
2813 static void
2814 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2815     dl_capability_sub_t *isub)
2816 {
2817 	uint_t			size;
2818 	uchar_t			*rptr;
2819 	dl_capab_dls_t	dls, *odls;
2820 	ill_dls_capab_t	*ill_dls;
2821 	mblk_t			*nmp = NULL;
2822 	dl_capability_req_t	*ocap;
2823 	uint_t			sub_dl_cap = isub->dl_cap;
2824 
2825 	if (!ill_capability_dls_init(ill))
2826 		return;
2827 	ill_dls = ill->ill_dls_capab;
2828 
2829 	/* Copy locally to get the members aligned */
2830 	bcopy((void *)idls, (void *)&dls,
2831 	    sizeof (dl_capab_dls_t));
2832 
2833 	/* Get the tx function and handle from dld */
2834 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2835 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2836 
2837 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2838 		ill_dls->ill_dls_change_status =
2839 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2840 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2841 		ill_dls->ill_dls_unbind =
2842 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2843 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2844 	}
2845 
2846 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2847 	    isub->dl_length;
2848 
2849 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2850 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2851 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2852 		    ill->ill_name, (void *)ill);
2853 		return;
2854 	}
2855 
2856 	/* initialize dl_capability_req_t */
2857 	rptr = nmp->b_rptr;
2858 	ocap = (dl_capability_req_t *)rptr;
2859 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2860 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2861 	rptr += sizeof (dl_capability_req_t);
2862 
2863 	/* initialize dl_capability_sub_t */
2864 	bcopy(isub, rptr, sizeof (*isub));
2865 	rptr += sizeof (*isub);
2866 
2867 	odls = (dl_capab_dls_t *)rptr;
2868 	rptr += sizeof (dl_capab_dls_t);
2869 
2870 	/* initialize dl_capab_dls_t to be sent down */
2871 	dls.dls_rx_handle = (uintptr_t)ill;
2872 	dls.dls_rx = (uintptr_t)ip_input;
2873 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2874 
2875 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2876 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2877 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2878 		dls.dls_flags = SOFT_RING_ENABLE;
2879 	} else {
2880 		dls.dls_flags = POLL_ENABLE;
2881 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2882 		    "to enable polling\n", ill->ill_name));
2883 	}
2884 	bcopy((void *)&dls, (void *)odls,
2885 	    sizeof (dl_capab_dls_t));
2886 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2887 	/*
2888 	 * nmp points to a DL_CAPABILITY_REQ message to
2889 	 * enable either soft_ring or polling
2890 	 */
2891 	ill_dlpi_send(ill, nmp);
2892 }
2893 
2894 static void
2895 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2896 {
2897 	mblk_t *mp;
2898 	dl_capab_dls_t *idls;
2899 	dl_capability_sub_t *dl_subcap;
2900 	int size;
2901 
2902 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2903 		return;
2904 
2905 	ASSERT(ill->ill_dls_capab != NULL);
2906 
2907 	size = sizeof (*dl_subcap) + sizeof (*idls);
2908 
2909 	mp = allocb(size, BPRI_HI);
2910 	if (mp == NULL) {
2911 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2912 		    "request to disable soft_ring\n"));
2913 		return;
2914 	}
2915 
2916 	mp->b_wptr = mp->b_rptr + size;
2917 
2918 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2919 	dl_subcap->dl_length = sizeof (*idls);
2920 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2921 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2922 	else
2923 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2924 
2925 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2926 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2927 		idls->dls_flags = SOFT_RING_DISABLE;
2928 	else
2929 		idls->dls_flags = POLL_DISABLE;
2930 
2931 	if (*sc_mp != NULL)
2932 		linkb(*sc_mp, mp);
2933 	else
2934 		*sc_mp = mp;
2935 }
2936 
2937 /*
2938  * Process a soft_ring/poll capability negotiation ack received
2939  * from a DLS Provider.isub must point to the sub-capability
2940  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2941  */
2942 static void
2943 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2944 {
2945 	dl_capab_dls_t		*idls;
2946 	uint_t			sub_dl_cap = isub->dl_cap;
2947 	uint8_t			*capend;
2948 
2949 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2950 	    sub_dl_cap == DL_CAPAB_POLL);
2951 
2952 	if (ill->ill_isv6)
2953 		return;
2954 
2955 	/*
2956 	 * Note: range checks here are not absolutely sufficient to
2957 	 * make us robust against malformed messages sent by drivers;
2958 	 * this is in keeping with the rest of IP's dlpi handling.
2959 	 * (Remember, it's coming from something else in the kernel
2960 	 * address space)
2961 	 */
2962 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2963 	if (capend > mp->b_wptr) {
2964 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2965 		    "malformed sub-capability too long for mblk");
2966 		return;
2967 	}
2968 
2969 	/*
2970 	 * There are two types of acks we process here:
2971 	 * 1. acks in reply to a (first form) generic capability req
2972 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2973 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2974 	 *    capability req.
2975 	 */
2976 	idls = (dl_capab_dls_t *)(isub + 1);
2977 
2978 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2979 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2980 		    "capability isn't as expected; pass-thru "
2981 		    "module(s) detected, discarding capability\n"));
2982 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2983 			/*
2984 			 * This is a capability renegotitation case.
2985 			 * The interface better be unusable at this
2986 			 * point other wise bad things will happen
2987 			 * if we disable direct calls on a running
2988 			 * and up interface.
2989 			 */
2990 			ill_capability_dls_disable(ill);
2991 		}
2992 		return;
2993 	}
2994 
2995 	switch (idls->dls_flags) {
2996 	default:
2997 		/* Disable if unknown flag */
2998 	case SOFT_RING_DISABLE:
2999 	case POLL_DISABLE:
3000 		ill_capability_dls_disable(ill);
3001 		break;
3002 	case SOFT_RING_CAPABLE:
3003 	case POLL_CAPABLE:
3004 		/*
3005 		 * If the capability was already enabled, its safe
3006 		 * to disable it first to get rid of stale information
3007 		 * and then start enabling it again.
3008 		 */
3009 		ill_capability_dls_disable(ill);
3010 		ill_capability_dls_capable(ill, idls, isub);
3011 		break;
3012 	case SOFT_RING_ENABLE:
3013 	case POLL_ENABLE:
3014 		mutex_enter(&ill->ill_lock);
3015 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3016 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3017 			ASSERT(ill->ill_dls_capab != NULL);
3018 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3019 		}
3020 		if (sub_dl_cap == DL_CAPAB_POLL &&
3021 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3022 			ASSERT(ill->ill_dls_capab != NULL);
3023 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3024 			ip1dbg(("ill_capability_dls_ack: interface %s "
3025 			    "has enabled polling\n", ill->ill_name));
3026 		}
3027 		mutex_exit(&ill->ill_lock);
3028 		break;
3029 	}
3030 }
3031 
3032 /*
3033  * Process a hardware checksum offload capability negotiation ack received
3034  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3035  * of a DL_CAPABILITY_ACK message.
3036  */
3037 static void
3038 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3039 {
3040 	dl_capability_req_t	*ocap;
3041 	dl_capab_hcksum_t	*ihck, *ohck;
3042 	ill_hcksum_capab_t	**ill_hcksum;
3043 	mblk_t			*nmp = NULL;
3044 	uint_t			sub_dl_cap = isub->dl_cap;
3045 	uint8_t			*capend;
3046 
3047 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3048 
3049 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3050 
3051 	/*
3052 	 * Note: range checks here are not absolutely sufficient to
3053 	 * make us robust against malformed messages sent by drivers;
3054 	 * this is in keeping with the rest of IP's dlpi handling.
3055 	 * (Remember, it's coming from something else in the kernel
3056 	 * address space)
3057 	 */
3058 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3059 	if (capend > mp->b_wptr) {
3060 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3061 		    "malformed sub-capability too long for mblk");
3062 		return;
3063 	}
3064 
3065 	/*
3066 	 * There are two types of acks we process here:
3067 	 * 1. acks in reply to a (first form) generic capability req
3068 	 *    (no ENABLE flag set)
3069 	 * 2. acks in reply to a ENABLE capability req.
3070 	 *    (ENABLE flag set)
3071 	 */
3072 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3073 
3074 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3075 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3076 		    "unsupported hardware checksum "
3077 		    "sub-capability (version %d, expected %d)",
3078 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3079 		return;
3080 	}
3081 
3082 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3083 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3084 		    "checksum capability isn't as expected; pass-thru "
3085 		    "module(s) detected, discarding capability\n"));
3086 		return;
3087 	}
3088 
3089 #define	CURR_HCKSUM_CAPAB				\
3090 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3091 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3092 
3093 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3094 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3095 		/* do ENABLE processing */
3096 		if (*ill_hcksum == NULL) {
3097 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3098 			    KM_NOSLEEP);
3099 
3100 			if (*ill_hcksum == NULL) {
3101 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3102 				    "could not enable hcksum version %d "
3103 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3104 				    ill->ill_name);
3105 				return;
3106 			}
3107 		}
3108 
3109 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3110 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3111 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3112 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3113 		    "has enabled hardware checksumming\n ",
3114 		    ill->ill_name));
3115 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3116 		/*
3117 		 * Enabling hardware checksum offload
3118 		 * Currently IP supports {TCP,UDP}/IPv4
3119 		 * partial and full cksum offload and
3120 		 * IPv4 header checksum offload.
3121 		 * Allocate new mblk which will
3122 		 * contain a new capability request
3123 		 * to enable hardware checksum offload.
3124 		 */
3125 		uint_t	size;
3126 		uchar_t	*rptr;
3127 
3128 		size = sizeof (dl_capability_req_t) +
3129 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3130 
3131 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3132 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3133 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3134 			    ill->ill_name);
3135 			return;
3136 		}
3137 
3138 		rptr = nmp->b_rptr;
3139 		/* initialize dl_capability_req_t */
3140 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3141 		ocap->dl_sub_offset =
3142 		    sizeof (dl_capability_req_t);
3143 		ocap->dl_sub_length =
3144 		    sizeof (dl_capability_sub_t) +
3145 		    isub->dl_length;
3146 		nmp->b_rptr += sizeof (dl_capability_req_t);
3147 
3148 		/* initialize dl_capability_sub_t */
3149 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3150 		nmp->b_rptr += sizeof (*isub);
3151 
3152 		/* initialize dl_capab_hcksum_t */
3153 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3154 		bcopy(ihck, ohck, sizeof (*ihck));
3155 
3156 		nmp->b_rptr = rptr;
3157 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3158 
3159 		/* Set ENABLE flag */
3160 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3161 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3162 
3163 		/*
3164 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3165 		 * hardware checksum acceleration.
3166 		 */
3167 		ill_dlpi_send(ill, nmp);
3168 	} else {
3169 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3170 		    "advertised %x hardware checksum capability flags\n",
3171 		    ill->ill_name, ihck->hcksum_txflags));
3172 	}
3173 }
3174 
3175 static void
3176 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3177 {
3178 	mblk_t *mp;
3179 	dl_capab_hcksum_t *hck_subcap;
3180 	dl_capability_sub_t *dl_subcap;
3181 	int size;
3182 
3183 	if (!ILL_HCKSUM_CAPABLE(ill))
3184 		return;
3185 
3186 	ASSERT(ill->ill_hcksum_capab != NULL);
3187 	/*
3188 	 * Clear the capability flag for hardware checksum offload but
3189 	 * retain the ill_hcksum_capab structure since it's possible that
3190 	 * another thread is still referring to it.  The structure only
3191 	 * gets deallocated when we destroy the ill.
3192 	 */
3193 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3194 
3195 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3196 
3197 	mp = allocb(size, BPRI_HI);
3198 	if (mp == NULL) {
3199 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3200 		    "request to disable hardware checksum offload\n"));
3201 		return;
3202 	}
3203 
3204 	mp->b_wptr = mp->b_rptr + size;
3205 
3206 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3207 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3208 	dl_subcap->dl_length = sizeof (*hck_subcap);
3209 
3210 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3211 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3212 	hck_subcap->hcksum_txflags = 0;
3213 
3214 	if (*sc_mp != NULL)
3215 		linkb(*sc_mp, mp);
3216 	else
3217 		*sc_mp = mp;
3218 }
3219 
3220 static void
3221 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3222 {
3223 	mblk_t *nmp = NULL;
3224 	dl_capability_req_t *oc;
3225 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3226 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3227 	uint_t sub_dl_cap = isub->dl_cap;
3228 	uint8_t *capend;
3229 
3230 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3231 
3232 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3233 
3234 	/*
3235 	 * Note: range checks here are not absolutely sufficient to
3236 	 * make us robust against malformed messages sent by drivers;
3237 	 * this is in keeping with the rest of IP's dlpi handling.
3238 	 * (Remember, it's coming from something else in the kernel
3239 	 * address space)
3240 	 */
3241 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3242 	if (capend > mp->b_wptr) {
3243 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3244 		    "malformed sub-capability too long for mblk");
3245 		return;
3246 	}
3247 
3248 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3249 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3250 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3251 		    "unsupported ZEROCOPY sub-capability (version %d, "
3252 		    "expected %d)", zc_ic->zerocopy_version,
3253 		    ZEROCOPY_VERSION_1);
3254 		return;
3255 	}
3256 
3257 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3258 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3259 		    "capability isn't as expected; pass-thru module(s) "
3260 		    "detected, discarding capability\n"));
3261 		return;
3262 	}
3263 
3264 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3265 		if (*ill_zerocopy_capab == NULL) {
3266 			*ill_zerocopy_capab =
3267 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3268 			    KM_NOSLEEP);
3269 
3270 			if (*ill_zerocopy_capab == NULL) {
3271 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3272 				    "could not enable Zero-copy version %d "
3273 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3274 				    ill->ill_name);
3275 				return;
3276 			}
3277 		}
3278 
3279 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3280 		    "supports Zero-copy version %d\n", ill->ill_name,
3281 		    ZEROCOPY_VERSION_1));
3282 
3283 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3284 		    zc_ic->zerocopy_version;
3285 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3286 		    zc_ic->zerocopy_flags;
3287 
3288 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3289 	} else {
3290 		uint_t size;
3291 		uchar_t *rptr;
3292 
3293 		size = sizeof (dl_capability_req_t) +
3294 		    sizeof (dl_capability_sub_t) +
3295 		    sizeof (dl_capab_zerocopy_t);
3296 
3297 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3298 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3299 			    "could not enable zerocopy for %s (ENOMEM)\n",
3300 			    ill->ill_name);
3301 			return;
3302 		}
3303 
3304 		rptr = nmp->b_rptr;
3305 		/* initialize dl_capability_req_t */
3306 		oc = (dl_capability_req_t *)rptr;
3307 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3308 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3309 		    sizeof (dl_capab_zerocopy_t);
3310 		rptr += sizeof (dl_capability_req_t);
3311 
3312 		/* initialize dl_capability_sub_t */
3313 		bcopy(isub, rptr, sizeof (*isub));
3314 		rptr += sizeof (*isub);
3315 
3316 		/* initialize dl_capab_zerocopy_t */
3317 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3318 		*zc_oc = *zc_ic;
3319 
3320 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3321 		    "to enable zero-copy version %d\n", ill->ill_name,
3322 		    ZEROCOPY_VERSION_1));
3323 
3324 		/* set VMSAFE_MEM flag */
3325 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3326 
3327 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3328 		ill_dlpi_send(ill, nmp);
3329 	}
3330 }
3331 
3332 static void
3333 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3334 {
3335 	mblk_t *mp;
3336 	dl_capab_zerocopy_t *zerocopy_subcap;
3337 	dl_capability_sub_t *dl_subcap;
3338 	int size;
3339 
3340 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3341 		return;
3342 
3343 	ASSERT(ill->ill_zerocopy_capab != NULL);
3344 	/*
3345 	 * Clear the capability flag for Zero-copy but retain the
3346 	 * ill_zerocopy_capab structure since it's possible that another
3347 	 * thread is still referring to it.  The structure only gets
3348 	 * deallocated when we destroy the ill.
3349 	 */
3350 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3351 
3352 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3353 
3354 	mp = allocb(size, BPRI_HI);
3355 	if (mp == NULL) {
3356 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3357 		    "request to disable Zero-copy\n"));
3358 		return;
3359 	}
3360 
3361 	mp->b_wptr = mp->b_rptr + size;
3362 
3363 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3364 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3365 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3366 
3367 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3368 	zerocopy_subcap->zerocopy_version =
3369 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3370 	zerocopy_subcap->zerocopy_flags = 0;
3371 
3372 	if (*sc_mp != NULL)
3373 		linkb(*sc_mp, mp);
3374 	else
3375 		*sc_mp = mp;
3376 }
3377 
3378 /*
3379  * Process Large Segment Offload capability negotiation ack received from a
3380  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3381  * DL_CAPABILITY_ACK message.
3382  */
3383 static void
3384 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3385 {
3386 	mblk_t *nmp = NULL;
3387 	dl_capability_req_t *oc;
3388 	dl_capab_lso_t *lso_ic, *lso_oc;
3389 	ill_lso_capab_t **ill_lso_capab;
3390 	uint_t sub_dl_cap = isub->dl_cap;
3391 	uint8_t *capend;
3392 
3393 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3394 
3395 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3396 
3397 	/*
3398 	 * Note: range checks here are not absolutely sufficient to
3399 	 * make us robust against malformed messages sent by drivers;
3400 	 * this is in keeping with the rest of IP's dlpi handling.
3401 	 * (Remember, it's coming from something else in the kernel
3402 	 * address space)
3403 	 */
3404 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3405 	if (capend > mp->b_wptr) {
3406 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3407 		    "malformed sub-capability too long for mblk");
3408 		return;
3409 	}
3410 
3411 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3412 
3413 	if (lso_ic->lso_version != LSO_VERSION_1) {
3414 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3415 		    "unsupported LSO sub-capability (version %d, expected %d)",
3416 		    lso_ic->lso_version, LSO_VERSION_1);
3417 		return;
3418 	}
3419 
3420 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3421 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3422 		    "capability isn't as expected; pass-thru module(s) "
3423 		    "detected, discarding capability\n"));
3424 		return;
3425 	}
3426 
3427 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3428 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3429 		if (*ill_lso_capab == NULL) {
3430 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3431 			    KM_NOSLEEP);
3432 
3433 			if (*ill_lso_capab == NULL) {
3434 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3435 				    "could not enable LSO version %d "
3436 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3437 				    ill->ill_name);
3438 				return;
3439 			}
3440 		}
3441 
3442 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3443 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3444 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3445 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3446 
3447 		ip1dbg(("ill_capability_lso_ack: interface %s "
3448 		    "has enabled LSO\n ", ill->ill_name));
3449 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3450 		uint_t size;
3451 		uchar_t *rptr;
3452 
3453 		size = sizeof (dl_capability_req_t) +
3454 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3455 
3456 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3457 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3458 			    "could not enable LSO for %s (ENOMEM)\n",
3459 			    ill->ill_name);
3460 			return;
3461 		}
3462 
3463 		rptr = nmp->b_rptr;
3464 		/* initialize dl_capability_req_t */
3465 		oc = (dl_capability_req_t *)nmp->b_rptr;
3466 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3467 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3468 		    sizeof (dl_capab_lso_t);
3469 		nmp->b_rptr += sizeof (dl_capability_req_t);
3470 
3471 		/* initialize dl_capability_sub_t */
3472 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3473 		nmp->b_rptr += sizeof (*isub);
3474 
3475 		/* initialize dl_capab_lso_t */
3476 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3477 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3478 
3479 		nmp->b_rptr = rptr;
3480 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3481 
3482 		/* set ENABLE flag */
3483 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3484 
3485 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3486 		ill_dlpi_send(ill, nmp);
3487 	} else {
3488 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3489 		    "advertised %x LSO capability flags\n",
3490 		    ill->ill_name, lso_ic->lso_flags));
3491 	}
3492 }
3493 
3494 
3495 static void
3496 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3497 {
3498 	mblk_t *mp;
3499 	dl_capab_lso_t *lso_subcap;
3500 	dl_capability_sub_t *dl_subcap;
3501 	int size;
3502 
3503 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3504 		return;
3505 
3506 	ASSERT(ill->ill_lso_capab != NULL);
3507 	/*
3508 	 * Clear the capability flag for LSO but retain the
3509 	 * ill_lso_capab structure since it's possible that another
3510 	 * thread is still referring to it.  The structure only gets
3511 	 * deallocated when we destroy the ill.
3512 	 */
3513 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3514 
3515 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3516 
3517 	mp = allocb(size, BPRI_HI);
3518 	if (mp == NULL) {
3519 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3520 		    "request to disable LSO\n"));
3521 		return;
3522 	}
3523 
3524 	mp->b_wptr = mp->b_rptr + size;
3525 
3526 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3527 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3528 	dl_subcap->dl_length = sizeof (*lso_subcap);
3529 
3530 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3531 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3532 	lso_subcap->lso_flags = 0;
3533 
3534 	if (*sc_mp != NULL)
3535 		linkb(*sc_mp, mp);
3536 	else
3537 		*sc_mp = mp;
3538 }
3539 
3540 /*
3541  * Consume a new-style hardware capabilities negotiation ack.
3542  * Called from ip_rput_dlpi_writer().
3543  */
3544 void
3545 ill_capability_ack(ill_t *ill, mblk_t *mp)
3546 {
3547 	dl_capability_ack_t *capp;
3548 	dl_capability_sub_t *subp, *endp;
3549 
3550 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3551 		ill->ill_dlpi_capab_state = IDS_OK;
3552 
3553 	capp = (dl_capability_ack_t *)mp->b_rptr;
3554 
3555 	if (capp->dl_sub_length == 0)
3556 		/* no new-style capabilities */
3557 		return;
3558 
3559 	/* make sure the driver supplied correct dl_sub_length */
3560 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3561 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3562 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3563 		return;
3564 	}
3565 
3566 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3567 	/*
3568 	 * There are sub-capabilities. Process the ones we know about.
3569 	 * Loop until we don't have room for another sub-cap header..
3570 	 */
3571 	for (subp = SC(capp, capp->dl_sub_offset),
3572 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3573 	    subp <= endp;
3574 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3575 
3576 		switch (subp->dl_cap) {
3577 		case DL_CAPAB_ID_WRAPPER:
3578 			ill_capability_id_ack(ill, mp, subp);
3579 			break;
3580 		default:
3581 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3582 			break;
3583 		}
3584 	}
3585 #undef SC
3586 }
3587 
3588 /*
3589  * This routine is called to scan the fragmentation reassembly table for
3590  * the specified ILL for any packets that are starting to smell.
3591  * dead_interval is the maximum time in seconds that will be tolerated.  It
3592  * will either be the value specified in ip_g_frag_timeout, or zero if the
3593  * ILL is shutting down and it is time to blow everything off.
3594  *
3595  * It returns the number of seconds (as a time_t) that the next frag timer
3596  * should be scheduled for, 0 meaning that the timer doesn't need to be
3597  * re-started.  Note that the method of calculating next_timeout isn't
3598  * entirely accurate since time will flow between the time we grab
3599  * current_time and the time we schedule the next timeout.  This isn't a
3600  * big problem since this is the timer for sending an ICMP reassembly time
3601  * exceeded messages, and it doesn't have to be exactly accurate.
3602  *
3603  * This function is
3604  * sometimes called as writer, although this is not required.
3605  */
3606 time_t
3607 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3608 {
3609 	ipfb_t	*ipfb;
3610 	ipfb_t	*endp;
3611 	ipf_t	*ipf;
3612 	ipf_t	*ipfnext;
3613 	mblk_t	*mp;
3614 	time_t	current_time = gethrestime_sec();
3615 	time_t	next_timeout = 0;
3616 	uint32_t	hdr_length;
3617 	mblk_t	*send_icmp_head;
3618 	mblk_t	*send_icmp_head_v6;
3619 	zoneid_t zoneid;
3620 	ip_stack_t *ipst = ill->ill_ipst;
3621 
3622 	ipfb = ill->ill_frag_hash_tbl;
3623 	if (ipfb == NULL)
3624 		return (B_FALSE);
3625 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3626 	/* Walk the frag hash table. */
3627 	for (; ipfb < endp; ipfb++) {
3628 		send_icmp_head = NULL;
3629 		send_icmp_head_v6 = NULL;
3630 		mutex_enter(&ipfb->ipfb_lock);
3631 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3632 			time_t frag_time = current_time - ipf->ipf_timestamp;
3633 			time_t frag_timeout;
3634 
3635 			if (frag_time < dead_interval) {
3636 				/*
3637 				 * There are some outstanding fragments
3638 				 * that will timeout later.  Make note of
3639 				 * the time so that we can reschedule the
3640 				 * next timeout appropriately.
3641 				 */
3642 				frag_timeout = dead_interval - frag_time;
3643 				if (next_timeout == 0 ||
3644 				    frag_timeout < next_timeout) {
3645 					next_timeout = frag_timeout;
3646 				}
3647 				break;
3648 			}
3649 			/* Time's up.  Get it out of here. */
3650 			hdr_length = ipf->ipf_nf_hdr_len;
3651 			ipfnext = ipf->ipf_hash_next;
3652 			if (ipfnext)
3653 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3654 			*ipf->ipf_ptphn = ipfnext;
3655 			mp = ipf->ipf_mp->b_cont;
3656 			for (; mp; mp = mp->b_cont) {
3657 				/* Extra points for neatness. */
3658 				IP_REASS_SET_START(mp, 0);
3659 				IP_REASS_SET_END(mp, 0);
3660 			}
3661 			mp = ipf->ipf_mp->b_cont;
3662 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3663 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3664 			ipfb->ipfb_count -= ipf->ipf_count;
3665 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3666 			ipfb->ipfb_frag_pkts--;
3667 			/*
3668 			 * We do not send any icmp message from here because
3669 			 * we currently are holding the ipfb_lock for this
3670 			 * hash chain. If we try and send any icmp messages
3671 			 * from here we may end up via a put back into ip
3672 			 * trying to get the same lock, causing a recursive
3673 			 * mutex panic. Instead we build a list and send all
3674 			 * the icmp messages after we have dropped the lock.
3675 			 */
3676 			if (ill->ill_isv6) {
3677 				if (hdr_length != 0) {
3678 					mp->b_next = send_icmp_head_v6;
3679 					send_icmp_head_v6 = mp;
3680 				} else {
3681 					freemsg(mp);
3682 				}
3683 			} else {
3684 				if (hdr_length != 0) {
3685 					mp->b_next = send_icmp_head;
3686 					send_icmp_head = mp;
3687 				} else {
3688 					freemsg(mp);
3689 				}
3690 			}
3691 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3692 			freeb(ipf->ipf_mp);
3693 		}
3694 		mutex_exit(&ipfb->ipfb_lock);
3695 		/*
3696 		 * Now need to send any icmp messages that we delayed from
3697 		 * above.
3698 		 */
3699 		while (send_icmp_head_v6 != NULL) {
3700 			ip6_t *ip6h;
3701 
3702 			mp = send_icmp_head_v6;
3703 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3704 			mp->b_next = NULL;
3705 			if (mp->b_datap->db_type == M_CTL)
3706 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3707 			else
3708 				ip6h = (ip6_t *)mp->b_rptr;
3709 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3710 			    ill, ipst);
3711 			if (zoneid == ALL_ZONES) {
3712 				freemsg(mp);
3713 			} else {
3714 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3715 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3716 				    B_FALSE, zoneid, ipst);
3717 			}
3718 		}
3719 		while (send_icmp_head != NULL) {
3720 			ipaddr_t dst;
3721 
3722 			mp = send_icmp_head;
3723 			send_icmp_head = send_icmp_head->b_next;
3724 			mp->b_next = NULL;
3725 
3726 			if (mp->b_datap->db_type == M_CTL)
3727 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3728 			else
3729 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3730 
3731 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3732 			if (zoneid == ALL_ZONES) {
3733 				freemsg(mp);
3734 			} else {
3735 				icmp_time_exceeded(ill->ill_wq, mp,
3736 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3737 				    ipst);
3738 			}
3739 		}
3740 	}
3741 	/*
3742 	 * A non-dying ILL will use the return value to decide whether to
3743 	 * restart the frag timer, and for how long.
3744 	 */
3745 	return (next_timeout);
3746 }
3747 
3748 /*
3749  * This routine is called when the approximate count of mblk memory used
3750  * for the specified ILL has exceeded max_count.
3751  */
3752 void
3753 ill_frag_prune(ill_t *ill, uint_t max_count)
3754 {
3755 	ipfb_t	*ipfb;
3756 	ipf_t	*ipf;
3757 	size_t	count;
3758 
3759 	/*
3760 	 * If we are here within ip_min_frag_prune_time msecs remove
3761 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3762 	 * ill_frag_free_num_pkts.
3763 	 */
3764 	mutex_enter(&ill->ill_lock);
3765 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3766 	    (ip_min_frag_prune_time != 0 ?
3767 	    ip_min_frag_prune_time : msec_per_tick)) {
3768 
3769 		ill->ill_frag_free_num_pkts++;
3770 
3771 	} else {
3772 		ill->ill_frag_free_num_pkts = 0;
3773 	}
3774 	ill->ill_last_frag_clean_time = lbolt;
3775 	mutex_exit(&ill->ill_lock);
3776 
3777 	/*
3778 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3779 	 */
3780 	if (ill->ill_frag_free_num_pkts != 0) {
3781 		int ix;
3782 
3783 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3784 			ipfb = &ill->ill_frag_hash_tbl[ix];
3785 			mutex_enter(&ipfb->ipfb_lock);
3786 			if (ipfb->ipfb_ipf != NULL) {
3787 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3788 				    ill->ill_frag_free_num_pkts);
3789 			}
3790 			mutex_exit(&ipfb->ipfb_lock);
3791 		}
3792 	}
3793 	/*
3794 	 * While the reassembly list for this ILL is too big, prune a fragment
3795 	 * queue by age, oldest first.
3796 	 */
3797 	while (ill->ill_frag_count > max_count) {
3798 		int	ix;
3799 		ipfb_t	*oipfb = NULL;
3800 		uint_t	oldest = UINT_MAX;
3801 
3802 		count = 0;
3803 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3804 			ipfb = &ill->ill_frag_hash_tbl[ix];
3805 			mutex_enter(&ipfb->ipfb_lock);
3806 			ipf = ipfb->ipfb_ipf;
3807 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3808 				oldest = ipf->ipf_gen;
3809 				oipfb = ipfb;
3810 			}
3811 			count += ipfb->ipfb_count;
3812 			mutex_exit(&ipfb->ipfb_lock);
3813 		}
3814 		if (oipfb == NULL)
3815 			break;
3816 
3817 		if (count <= max_count)
3818 			return;	/* Somebody beat us to it, nothing to do */
3819 		mutex_enter(&oipfb->ipfb_lock);
3820 		ipf = oipfb->ipfb_ipf;
3821 		if (ipf != NULL) {
3822 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3823 		}
3824 		mutex_exit(&oipfb->ipfb_lock);
3825 	}
3826 }
3827 
3828 /*
3829  * free 'free_cnt' fragmented packets starting at ipf.
3830  */
3831 void
3832 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3833 {
3834 	size_t	count;
3835 	mblk_t	*mp;
3836 	mblk_t	*tmp;
3837 	ipf_t **ipfp = ipf->ipf_ptphn;
3838 
3839 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3840 	ASSERT(ipfp != NULL);
3841 	ASSERT(ipf != NULL);
3842 
3843 	while (ipf != NULL && free_cnt-- > 0) {
3844 		count = ipf->ipf_count;
3845 		mp = ipf->ipf_mp;
3846 		ipf = ipf->ipf_hash_next;
3847 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3848 			IP_REASS_SET_START(tmp, 0);
3849 			IP_REASS_SET_END(tmp, 0);
3850 		}
3851 		atomic_add_32(&ill->ill_frag_count, -count);
3852 		ASSERT(ipfb->ipfb_count >= count);
3853 		ipfb->ipfb_count -= count;
3854 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3855 		ipfb->ipfb_frag_pkts--;
3856 		freemsg(mp);
3857 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3858 	}
3859 
3860 	if (ipf)
3861 		ipf->ipf_ptphn = ipfp;
3862 	ipfp[0] = ipf;
3863 }
3864 
3865 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3866 	"obsolete and may be removed in a future release of Solaris.  Use " \
3867 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3868 
3869 /*
3870  * For obsolete per-interface forwarding configuration;
3871  * called in response to ND_GET.
3872  */
3873 /* ARGSUSED */
3874 static int
3875 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3876 {
3877 	ill_t *ill = (ill_t *)cp;
3878 
3879 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3880 
3881 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3882 	return (0);
3883 }
3884 
3885 /*
3886  * For obsolete per-interface forwarding configuration;
3887  * called in response to ND_SET.
3888  */
3889 /* ARGSUSED */
3890 static int
3891 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3892     cred_t *ioc_cr)
3893 {
3894 	long value;
3895 	int retval;
3896 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3897 
3898 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3899 
3900 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3901 	    value < 0 || value > 1) {
3902 		return (EINVAL);
3903 	}
3904 
3905 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3906 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3907 	rw_exit(&ipst->ips_ill_g_lock);
3908 	return (retval);
3909 }
3910 
3911 /*
3912  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3913  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3914  * up RTS_IFINFO routing socket messages for each interface whose flags we
3915  * change.
3916  */
3917 int
3918 ill_forward_set(ill_t *ill, boolean_t enable)
3919 {
3920 	ill_group_t *illgrp;
3921 	ip_stack_t	*ipst = ill->ill_ipst;
3922 
3923 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3924 
3925 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3926 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3927 		return (0);
3928 
3929 	if (IS_LOOPBACK(ill))
3930 		return (EINVAL);
3931 
3932 	/*
3933 	 * If the ill is in an IPMP group, set the forwarding policy on all
3934 	 * members of the group to the same value.
3935 	 */
3936 	illgrp = ill->ill_group;
3937 	if (illgrp != NULL) {
3938 		ill_t *tmp_ill;
3939 
3940 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3941 		    tmp_ill = tmp_ill->ill_group_next) {
3942 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3943 			    (enable ? "Enabling" : "Disabling"),
3944 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3945 			    tmp_ill->ill_name));
3946 			mutex_enter(&tmp_ill->ill_lock);
3947 			if (enable)
3948 				tmp_ill->ill_flags |= ILLF_ROUTER;
3949 			else
3950 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3951 			mutex_exit(&tmp_ill->ill_lock);
3952 			if (tmp_ill->ill_isv6)
3953 				ill_set_nce_router_flags(tmp_ill, enable);
3954 			/* Notify routing socket listeners of this change. */
3955 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3956 		}
3957 	} else {
3958 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3959 		    (enable ? "Enabling" : "Disabling"),
3960 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3961 		mutex_enter(&ill->ill_lock);
3962 		if (enable)
3963 			ill->ill_flags |= ILLF_ROUTER;
3964 		else
3965 			ill->ill_flags &= ~ILLF_ROUTER;
3966 		mutex_exit(&ill->ill_lock);
3967 		if (ill->ill_isv6)
3968 			ill_set_nce_router_flags(ill, enable);
3969 		/* Notify routing socket listeners of this change. */
3970 		ip_rts_ifmsg(ill->ill_ipif);
3971 	}
3972 
3973 	return (0);
3974 }
3975 
3976 /*
3977  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3978  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3979  * set or clear.
3980  */
3981 static void
3982 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3983 {
3984 	ipif_t *ipif;
3985 	nce_t *nce;
3986 
3987 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3988 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3989 		if (nce != NULL) {
3990 			mutex_enter(&nce->nce_lock);
3991 			if (enable)
3992 				nce->nce_flags |= NCE_F_ISROUTER;
3993 			else
3994 				nce->nce_flags &= ~NCE_F_ISROUTER;
3995 			mutex_exit(&nce->nce_lock);
3996 			NCE_REFRELE(nce);
3997 		}
3998 	}
3999 }
4000 
4001 /*
4002  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4003  * for this ill.  Make sure the v6/v4 question has been answered about this
4004  * ill.  The creation of this ndd variable is only for backwards compatibility.
4005  * The preferred way to control per-interface IP forwarding is through the
4006  * ILLF_ROUTER interface flag.
4007  */
4008 static int
4009 ill_set_ndd_name(ill_t *ill)
4010 {
4011 	char *suffix;
4012 	ip_stack_t	*ipst = ill->ill_ipst;
4013 
4014 	ASSERT(IAM_WRITER_ILL(ill));
4015 
4016 	if (ill->ill_isv6)
4017 		suffix = ipv6_forward_suffix;
4018 	else
4019 		suffix = ipv4_forward_suffix;
4020 
4021 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4022 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4023 	/*
4024 	 * Copies over the '\0'.
4025 	 * Note that strlen(suffix) is always bounded.
4026 	 */
4027 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4028 	    strlen(suffix) + 1);
4029 
4030 	/*
4031 	 * Use of the nd table requires holding the reader lock.
4032 	 * Modifying the nd table thru nd_load/nd_unload requires
4033 	 * the writer lock.
4034 	 */
4035 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4036 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4037 	    nd_ill_forward_set, (caddr_t)ill)) {
4038 		/*
4039 		 * If the nd_load failed, it only meant that it could not
4040 		 * allocate a new bunch of room for further NDD expansion.
4041 		 * Because of that, the ill_ndd_name will be set to 0, and
4042 		 * this interface is at the mercy of the global ip_forwarding
4043 		 * variable.
4044 		 */
4045 		rw_exit(&ipst->ips_ip_g_nd_lock);
4046 		ill->ill_ndd_name = NULL;
4047 		return (ENOMEM);
4048 	}
4049 	rw_exit(&ipst->ips_ip_g_nd_lock);
4050 	return (0);
4051 }
4052 
4053 /*
4054  * Intializes the context structure and returns the first ill in the list
4055  * cuurently start_list and end_list can have values:
4056  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4057  * IP_V4_G_HEAD		Traverse IPV4 list only.
4058  * IP_V6_G_HEAD		Traverse IPV6 list only.
4059  */
4060 
4061 /*
4062  * We don't check for CONDEMNED ills here. Caller must do that if
4063  * necessary under the ill lock.
4064  */
4065 ill_t *
4066 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4067     ip_stack_t *ipst)
4068 {
4069 	ill_if_t *ifp;
4070 	ill_t *ill;
4071 	avl_tree_t *avl_tree;
4072 
4073 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4074 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4075 
4076 	/*
4077 	 * setup the lists to search
4078 	 */
4079 	if (end_list != MAX_G_HEADS) {
4080 		ctx->ctx_current_list = start_list;
4081 		ctx->ctx_last_list = end_list;
4082 	} else {
4083 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4084 		ctx->ctx_current_list = 0;
4085 	}
4086 
4087 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4088 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4089 		if (ifp != (ill_if_t *)
4090 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4091 			avl_tree = &ifp->illif_avl_by_ppa;
4092 			ill = avl_first(avl_tree);
4093 			/*
4094 			 * ill is guaranteed to be non NULL or ifp should have
4095 			 * not existed.
4096 			 */
4097 			ASSERT(ill != NULL);
4098 			return (ill);
4099 		}
4100 		ctx->ctx_current_list++;
4101 	}
4102 
4103 	return (NULL);
4104 }
4105 
4106 /*
4107  * returns the next ill in the list. ill_first() must have been called
4108  * before calling ill_next() or bad things will happen.
4109  */
4110 
4111 /*
4112  * We don't check for CONDEMNED ills here. Caller must do that if
4113  * necessary under the ill lock.
4114  */
4115 ill_t *
4116 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4117 {
4118 	ill_if_t *ifp;
4119 	ill_t *ill;
4120 	ip_stack_t	*ipst = lastill->ill_ipst;
4121 
4122 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4123 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4124 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4125 	    AVL_AFTER)) != NULL) {
4126 		return (ill);
4127 	}
4128 
4129 	/* goto next ill_ifp in the list. */
4130 	ifp = lastill->ill_ifptr->illif_next;
4131 
4132 	/* make sure not at end of circular list */
4133 	while (ifp ==
4134 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4135 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4136 			return (NULL);
4137 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4138 	}
4139 
4140 	return (avl_first(&ifp->illif_avl_by_ppa));
4141 }
4142 
4143 /*
4144  * Check interface name for correct format which is name+ppa.
4145  * name can contain characters and digits, the right most digits
4146  * make up the ppa number. use of octal is not allowed, name must contain
4147  * a ppa, return pointer to the start of ppa.
4148  * In case of error return NULL.
4149  */
4150 static char *
4151 ill_get_ppa_ptr(char *name)
4152 {
4153 	int namelen = mi_strlen(name);
4154 
4155 	int len = namelen;
4156 
4157 	name += len;
4158 	while (len > 0) {
4159 		name--;
4160 		if (*name < '0' || *name > '9')
4161 			break;
4162 		len--;
4163 	}
4164 
4165 	/* empty string, all digits, or no trailing digits */
4166 	if (len == 0 || len == (int)namelen)
4167 		return (NULL);
4168 
4169 	name++;
4170 	/* check for attempted use of octal */
4171 	if (*name == '0' && len != (int)namelen - 1)
4172 		return (NULL);
4173 	return (name);
4174 }
4175 
4176 /*
4177  * use avl tree to locate the ill.
4178  */
4179 static ill_t *
4180 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4181     ipsq_func_t func, int *error, ip_stack_t *ipst)
4182 {
4183 	char *ppa_ptr = NULL;
4184 	int len;
4185 	uint_t ppa;
4186 	ill_t *ill = NULL;
4187 	ill_if_t *ifp;
4188 	int list;
4189 	ipsq_t *ipsq;
4190 
4191 	if (error != NULL)
4192 		*error = 0;
4193 
4194 	/*
4195 	 * get ppa ptr
4196 	 */
4197 	if (isv6)
4198 		list = IP_V6_G_HEAD;
4199 	else
4200 		list = IP_V4_G_HEAD;
4201 
4202 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4203 		if (error != NULL)
4204 			*error = ENXIO;
4205 		return (NULL);
4206 	}
4207 
4208 	len = ppa_ptr - name + 1;
4209 
4210 	ppa = stoi(&ppa_ptr);
4211 
4212 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4213 
4214 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4215 		/*
4216 		 * match is done on len - 1 as the name is not null
4217 		 * terminated it contains ppa in addition to the interface
4218 		 * name.
4219 		 */
4220 		if ((ifp->illif_name_len == len) &&
4221 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4222 			break;
4223 		} else {
4224 			ifp = ifp->illif_next;
4225 		}
4226 	}
4227 
4228 
4229 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4230 		/*
4231 		 * Even the interface type does not exist.
4232 		 */
4233 		if (error != NULL)
4234 			*error = ENXIO;
4235 		return (NULL);
4236 	}
4237 
4238 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4239 	if (ill != NULL) {
4240 		/*
4241 		 * The block comment at the start of ipif_down
4242 		 * explains the use of the macros used below
4243 		 */
4244 		GRAB_CONN_LOCK(q);
4245 		mutex_enter(&ill->ill_lock);
4246 		if (ILL_CAN_LOOKUP(ill)) {
4247 			ill_refhold_locked(ill);
4248 			mutex_exit(&ill->ill_lock);
4249 			RELEASE_CONN_LOCK(q);
4250 			return (ill);
4251 		} else if (ILL_CAN_WAIT(ill, q)) {
4252 			ipsq = ill->ill_phyint->phyint_ipsq;
4253 			mutex_enter(&ipsq->ipsq_lock);
4254 			mutex_exit(&ill->ill_lock);
4255 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4256 			mutex_exit(&ipsq->ipsq_lock);
4257 			RELEASE_CONN_LOCK(q);
4258 			if (error != NULL)
4259 				*error = EINPROGRESS;
4260 			return (NULL);
4261 		}
4262 		mutex_exit(&ill->ill_lock);
4263 		RELEASE_CONN_LOCK(q);
4264 	}
4265 	if (error != NULL)
4266 		*error = ENXIO;
4267 	return (NULL);
4268 }
4269 
4270 /*
4271  * comparison function for use with avl.
4272  */
4273 static int
4274 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4275 {
4276 	uint_t ppa;
4277 	uint_t ill_ppa;
4278 
4279 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4280 
4281 	ppa = *((uint_t *)ppa_ptr);
4282 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4283 	/*
4284 	 * We want the ill with the lowest ppa to be on the
4285 	 * top.
4286 	 */
4287 	if (ill_ppa < ppa)
4288 		return (1);
4289 	if (ill_ppa > ppa)
4290 		return (-1);
4291 	return (0);
4292 }
4293 
4294 /*
4295  * remove an interface type from the global list.
4296  */
4297 static void
4298 ill_delete_interface_type(ill_if_t *interface)
4299 {
4300 	ASSERT(interface != NULL);
4301 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4302 
4303 	avl_destroy(&interface->illif_avl_by_ppa);
4304 	if (interface->illif_ppa_arena != NULL)
4305 		vmem_destroy(interface->illif_ppa_arena);
4306 
4307 	remque(interface);
4308 
4309 	mi_free(interface);
4310 }
4311 
4312 /*
4313  * remove ill from the global list.
4314  */
4315 static void
4316 ill_glist_delete(ill_t *ill)
4317 {
4318 	hook_nic_event_t *info;
4319 	ip_stack_t	*ipst;
4320 
4321 	if (ill == NULL)
4322 		return;
4323 	ipst = ill->ill_ipst;
4324 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4325 
4326 	/*
4327 	 * If the ill was never inserted into the AVL tree
4328 	 * we skip the if branch.
4329 	 */
4330 	if (ill->ill_ifptr != NULL) {
4331 		/*
4332 		 * remove from AVL tree and free ppa number
4333 		 */
4334 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4335 
4336 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4337 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4338 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4339 		}
4340 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4341 			ill_delete_interface_type(ill->ill_ifptr);
4342 		}
4343 
4344 		/*
4345 		 * Indicate ill is no longer in the list.
4346 		 */
4347 		ill->ill_ifptr = NULL;
4348 		ill->ill_name_length = 0;
4349 		ill->ill_name[0] = '\0';
4350 		ill->ill_ppa = UINT_MAX;
4351 	}
4352 
4353 	/*
4354 	 * Run the unplumb hook after the NIC has disappeared from being
4355 	 * visible so that attempts to revalidate its existance will fail.
4356 	 *
4357 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4358 	 * that the ordering of delivered events to listeners matches the
4359 	 * order of them in the kernel.
4360 	 */
4361 	info = ill->ill_nic_event_info;
4362 	if (info != NULL && info->hne_event == NE_DOWN) {
4363 		mutex_enter(&ill->ill_lock);
4364 		ill_nic_info_dispatch(ill);
4365 		mutex_exit(&ill->ill_lock);
4366 	}
4367 
4368 	/* Generate NE_UNPLUMB event for ill_name. */
4369 	(void) ill_hook_event_create(ill, 0, NE_UNPLUMB, ill->ill_name,
4370 	    ill->ill_name_length);
4371 
4372 	ill_phyint_free(ill);
4373 	rw_exit(&ipst->ips_ill_g_lock);
4374 }
4375 
4376 /*
4377  * allocate a ppa, if the number of plumbed interfaces of this type are
4378  * less than ill_no_arena do a linear search to find a unused ppa.
4379  * When the number goes beyond ill_no_arena switch to using an arena.
4380  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4381  * is the return value for an error condition, so allocation starts at one
4382  * and is decremented by one.
4383  */
4384 static int
4385 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4386 {
4387 	ill_t *tmp_ill;
4388 	uint_t start, end;
4389 	int ppa;
4390 
4391 	if (ifp->illif_ppa_arena == NULL &&
4392 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4393 		/*
4394 		 * Create an arena.
4395 		 */
4396 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4397 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4398 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4399 			/* allocate what has already been assigned */
4400 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4401 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4402 		    tmp_ill, AVL_AFTER)) {
4403 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4404 			    1,		/* size */
4405 			    1,		/* align/quantum */
4406 			    0,		/* phase */
4407 			    0,		/* nocross */
4408 			    /* minaddr */
4409 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4410 			    /* maxaddr */
4411 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4412 			    VM_NOSLEEP|VM_FIRSTFIT);
4413 			if (ppa == 0) {
4414 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4415 				    " failed while switching"));
4416 				vmem_destroy(ifp->illif_ppa_arena);
4417 				ifp->illif_ppa_arena = NULL;
4418 				break;
4419 			}
4420 		}
4421 	}
4422 
4423 	if (ifp->illif_ppa_arena != NULL) {
4424 		if (ill->ill_ppa == UINT_MAX) {
4425 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4426 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4427 			if (ppa == 0)
4428 				return (EAGAIN);
4429 			ill->ill_ppa = --ppa;
4430 		} else {
4431 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4432 			    1, 		/* size */
4433 			    1, 		/* align/quantum */
4434 			    0, 		/* phase */
4435 			    0, 		/* nocross */
4436 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4437 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4438 			    VM_NOSLEEP|VM_FIRSTFIT);
4439 			/*
4440 			 * Most likely the allocation failed because
4441 			 * the requested ppa was in use.
4442 			 */
4443 			if (ppa == 0)
4444 				return (EEXIST);
4445 		}
4446 		return (0);
4447 	}
4448 
4449 	/*
4450 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4451 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4452 	 */
4453 	if (ill->ill_ppa == UINT_MAX) {
4454 		end = UINT_MAX - 1;
4455 		start = 0;
4456 	} else {
4457 		end = start = ill->ill_ppa;
4458 	}
4459 
4460 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4461 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4462 		if (start++ >= end) {
4463 			if (ill->ill_ppa == UINT_MAX)
4464 				return (EAGAIN);
4465 			else
4466 				return (EEXIST);
4467 		}
4468 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4469 	}
4470 	ill->ill_ppa = start;
4471 	return (0);
4472 }
4473 
4474 /*
4475  * Insert ill into the list of configured ill's. Once this function completes,
4476  * the ill is globally visible and is available through lookups. More precisely
4477  * this happens after the caller drops the ill_g_lock.
4478  */
4479 static int
4480 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4481 {
4482 	ill_if_t *ill_interface;
4483 	avl_index_t where = 0;
4484 	int error;
4485 	int name_length;
4486 	int index;
4487 	boolean_t check_length = B_FALSE;
4488 	ip_stack_t	*ipst = ill->ill_ipst;
4489 
4490 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4491 
4492 	name_length = mi_strlen(name) + 1;
4493 
4494 	if (isv6)
4495 		index = IP_V6_G_HEAD;
4496 	else
4497 		index = IP_V4_G_HEAD;
4498 
4499 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4500 	/*
4501 	 * Search for interface type based on name
4502 	 */
4503 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4504 		if ((ill_interface->illif_name_len == name_length) &&
4505 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4506 			break;
4507 		}
4508 		ill_interface = ill_interface->illif_next;
4509 	}
4510 
4511 	/*
4512 	 * Interface type not found, create one.
4513 	 */
4514 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4515 
4516 		ill_g_head_t ghead;
4517 
4518 		/*
4519 		 * allocate ill_if_t structure
4520 		 */
4521 
4522 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4523 		if (ill_interface == NULL) {
4524 			return (ENOMEM);
4525 		}
4526 
4527 
4528 
4529 		(void) strcpy(ill_interface->illif_name, name);
4530 		ill_interface->illif_name_len = name_length;
4531 
4532 		avl_create(&ill_interface->illif_avl_by_ppa,
4533 		    ill_compare_ppa, sizeof (ill_t),
4534 		    offsetof(struct ill_s, ill_avl_byppa));
4535 
4536 		/*
4537 		 * link the structure in the back to maintain order
4538 		 * of configuration for ifconfig output.
4539 		 */
4540 		ghead = ipst->ips_ill_g_heads[index];
4541 		insque(ill_interface, ghead.ill_g_list_tail);
4542 
4543 	}
4544 
4545 	if (ill->ill_ppa == UINT_MAX)
4546 		check_length = B_TRUE;
4547 
4548 	error = ill_alloc_ppa(ill_interface, ill);
4549 	if (error != 0) {
4550 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4551 			ill_delete_interface_type(ill->ill_ifptr);
4552 		return (error);
4553 	}
4554 
4555 	/*
4556 	 * When the ppa is choosen by the system, check that there is
4557 	 * enough space to insert ppa. if a specific ppa was passed in this
4558 	 * check is not required as the interface name passed in will have
4559 	 * the right ppa in it.
4560 	 */
4561 	if (check_length) {
4562 		/*
4563 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4564 		 */
4565 		char buf[sizeof (uint_t) * 3];
4566 
4567 		/*
4568 		 * convert ppa to string to calculate the amount of space
4569 		 * required for it in the name.
4570 		 */
4571 		numtos(ill->ill_ppa, buf);
4572 
4573 		/* Do we have enough space to insert ppa ? */
4574 
4575 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4576 			/* Free ppa and interface type struct */
4577 			if (ill_interface->illif_ppa_arena != NULL) {
4578 				vmem_free(ill_interface->illif_ppa_arena,
4579 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4580 			}
4581 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4582 			    0) {
4583 				ill_delete_interface_type(ill->ill_ifptr);
4584 			}
4585 
4586 			return (EINVAL);
4587 		}
4588 	}
4589 
4590 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4591 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4592 
4593 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4594 	    &where);
4595 	ill->ill_ifptr = ill_interface;
4596 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4597 
4598 	ill_phyint_reinit(ill);
4599 	return (0);
4600 }
4601 
4602 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4603 static boolean_t
4604 ipsq_init(ill_t *ill)
4605 {
4606 	ipsq_t  *ipsq;
4607 
4608 	/* Init the ipsq and impicitly enter as writer */
4609 	ill->ill_phyint->phyint_ipsq =
4610 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4611 	if (ill->ill_phyint->phyint_ipsq == NULL)
4612 		return (B_FALSE);
4613 	ipsq = ill->ill_phyint->phyint_ipsq;
4614 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4615 	ill->ill_phyint->phyint_ipsq_next = NULL;
4616 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4617 	ipsq->ipsq_refs = 1;
4618 	ipsq->ipsq_writer = curthread;
4619 	ipsq->ipsq_reentry_cnt = 1;
4620 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4621 #ifdef DEBUG
4622 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4623 	    IPSQ_STACK_DEPTH);
4624 #endif
4625 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4626 	return (B_TRUE);
4627 }
4628 
4629 /*
4630  * ill_init is called by ip_open when a device control stream is opened.
4631  * It does a few initializations, and shoots a DL_INFO_REQ message down
4632  * to the driver.  The response is later picked up in ip_rput_dlpi and
4633  * used to set up default mechanisms for talking to the driver.  (Always
4634  * called as writer.)
4635  *
4636  * If this function returns error, ip_open will call ip_close which in
4637  * turn will call ill_delete to clean up any memory allocated here that
4638  * is not yet freed.
4639  */
4640 int
4641 ill_init(queue_t *q, ill_t *ill)
4642 {
4643 	int	count;
4644 	dl_info_req_t	*dlir;
4645 	mblk_t	*info_mp;
4646 	uchar_t *frag_ptr;
4647 
4648 	/*
4649 	 * The ill is initialized to zero by mi_alloc*(). In addition
4650 	 * some fields already contain valid values, initialized in
4651 	 * ip_open(), before we reach here.
4652 	 */
4653 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4654 
4655 	ill->ill_rq = q;
4656 	ill->ill_wq = WR(q);
4657 
4658 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4659 	    BPRI_HI);
4660 	if (info_mp == NULL)
4661 		return (ENOMEM);
4662 
4663 	/*
4664 	 * Allocate sufficient space to contain our fragment hash table and
4665 	 * the device name.
4666 	 */
4667 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4668 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4669 	if (frag_ptr == NULL) {
4670 		freemsg(info_mp);
4671 		return (ENOMEM);
4672 	}
4673 	ill->ill_frag_ptr = frag_ptr;
4674 	ill->ill_frag_free_num_pkts = 0;
4675 	ill->ill_last_frag_clean_time = 0;
4676 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4677 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4678 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4679 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4680 		    NULL, MUTEX_DEFAULT, NULL);
4681 	}
4682 
4683 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4684 	if (ill->ill_phyint == NULL) {
4685 		freemsg(info_mp);
4686 		mi_free(frag_ptr);
4687 		return (ENOMEM);
4688 	}
4689 
4690 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4691 	/*
4692 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4693 	 * at this point because of the following reason. If we can't
4694 	 * enter the ipsq at some point and cv_wait, the writer that
4695 	 * wakes us up tries to locate us using the list of all phyints
4696 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4697 	 * If we don't set it now, we risk a missed wakeup.
4698 	 */
4699 	ill->ill_phyint->phyint_illv4 = ill;
4700 	ill->ill_ppa = UINT_MAX;
4701 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4702 
4703 	if (!ipsq_init(ill)) {
4704 		freemsg(info_mp);
4705 		mi_free(frag_ptr);
4706 		mi_free(ill->ill_phyint);
4707 		return (ENOMEM);
4708 	}
4709 
4710 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4711 
4712 
4713 	/* Frag queue limit stuff */
4714 	ill->ill_frag_count = 0;
4715 	ill->ill_ipf_gen = 0;
4716 
4717 	ill->ill_global_timer = INFINITY;
4718 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4719 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4720 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4721 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4722 
4723 	/*
4724 	 * Initialize IPv6 configuration variables.  The IP module is always
4725 	 * opened as an IPv4 module.  Instead tracking down the cases where
4726 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4727 	 * here for convenience, this has no effect until the ill is set to do
4728 	 * IPv6.
4729 	 */
4730 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4731 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4732 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4733 	ill->ill_max_buf = ND_MAX_Q;
4734 	ill->ill_refcnt = 0;
4735 
4736 	/* Send down the Info Request to the driver. */
4737 	info_mp->b_datap->db_type = M_PCPROTO;
4738 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4739 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4740 	dlir->dl_primitive = DL_INFO_REQ;
4741 
4742 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4743 
4744 	qprocson(q);
4745 	ill_dlpi_send(ill, info_mp);
4746 
4747 	return (0);
4748 }
4749 
4750 /*
4751  * ill_dls_info
4752  * creates datalink socket info from the device.
4753  */
4754 int
4755 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4756 {
4757 	size_t	len;
4758 	ill_t	*ill = ipif->ipif_ill;
4759 
4760 	sdl->sdl_family = AF_LINK;
4761 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4762 	sdl->sdl_type = ill->ill_type;
4763 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4764 	len = strlen(sdl->sdl_data);
4765 	ASSERT(len < 256);
4766 	sdl->sdl_nlen = (uchar_t)len;
4767 	sdl->sdl_alen = ill->ill_phys_addr_length;
4768 	sdl->sdl_slen = 0;
4769 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4770 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4771 
4772 	return (sizeof (struct sockaddr_dl));
4773 }
4774 
4775 /*
4776  * ill_xarp_info
4777  * creates xarp info from the device.
4778  */
4779 static int
4780 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4781 {
4782 	sdl->sdl_family = AF_LINK;
4783 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4784 	sdl->sdl_type = ill->ill_type;
4785 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4786 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4787 	sdl->sdl_alen = ill->ill_phys_addr_length;
4788 	sdl->sdl_slen = 0;
4789 	return (sdl->sdl_nlen);
4790 }
4791 
4792 static int
4793 loopback_kstat_update(kstat_t *ksp, int rw)
4794 {
4795 	kstat_named_t *kn;
4796 	netstackid_t	stackid;
4797 	netstack_t	*ns;
4798 	ip_stack_t	*ipst;
4799 
4800 	if (ksp == NULL || ksp->ks_data == NULL)
4801 		return (EIO);
4802 
4803 	if (rw == KSTAT_WRITE)
4804 		return (EACCES);
4805 
4806 	kn = KSTAT_NAMED_PTR(ksp);
4807 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4808 
4809 	ns = netstack_find_by_stackid(stackid);
4810 	if (ns == NULL)
4811 		return (-1);
4812 
4813 	ipst = ns->netstack_ip;
4814 	if (ipst == NULL) {
4815 		netstack_rele(ns);
4816 		return (-1);
4817 	}
4818 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4819 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4820 	netstack_rele(ns);
4821 	return (0);
4822 }
4823 
4824 
4825 /*
4826  * Has ifindex been plumbed already.
4827  * Compares both phyint_ifindex and phyint_group_ifindex.
4828  */
4829 static boolean_t
4830 phyint_exists(uint_t index, ip_stack_t *ipst)
4831 {
4832 	phyint_t *phyi;
4833 
4834 	ASSERT(index != 0);
4835 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4836 	/*
4837 	 * Indexes are stored in the phyint - a common structure
4838 	 * to both IPv4 and IPv6.
4839 	 */
4840 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4841 	for (; phyi != NULL;
4842 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4843 	    phyi, AVL_AFTER)) {
4844 		if (phyi->phyint_ifindex == index ||
4845 		    phyi->phyint_group_ifindex == index)
4846 			return (B_TRUE);
4847 	}
4848 	return (B_FALSE);
4849 }
4850 
4851 /* Pick a unique ifindex */
4852 boolean_t
4853 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4854 {
4855 	uint_t starting_index;
4856 
4857 	if (!ipst->ips_ill_index_wrap) {
4858 		*indexp = ipst->ips_ill_index++;
4859 		if (ipst->ips_ill_index == 0) {
4860 			/* Reached the uint_t limit Next time wrap  */
4861 			ipst->ips_ill_index_wrap = B_TRUE;
4862 		}
4863 		return (B_TRUE);
4864 	}
4865 
4866 	/*
4867 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4868 	 * at this point and don't want to call any function that attempts
4869 	 * to get the lock again.
4870 	 */
4871 	starting_index = ipst->ips_ill_index++;
4872 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4873 		if (ipst->ips_ill_index != 0 &&
4874 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4875 			/* found unused index - use it */
4876 			*indexp = ipst->ips_ill_index;
4877 			return (B_TRUE);
4878 		}
4879 	}
4880 
4881 	/*
4882 	 * all interface indicies are inuse.
4883 	 */
4884 	return (B_FALSE);
4885 }
4886 
4887 /*
4888  * Assign a unique interface index for the phyint.
4889  */
4890 static boolean_t
4891 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4892 {
4893 	ASSERT(phyi->phyint_ifindex == 0);
4894 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4895 }
4896 
4897 /*
4898  * Return a pointer to the ill which matches the supplied name.  Note that
4899  * the ill name length includes the null termination character.  (May be
4900  * called as writer.)
4901  * If do_alloc and the interface is "lo0" it will be automatically created.
4902  * Cannot bump up reference on condemned ills. So dup detect can't be done
4903  * using this func.
4904  */
4905 ill_t *
4906 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4907     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4908     ip_stack_t *ipst)
4909 {
4910 	ill_t	*ill;
4911 	ipif_t	*ipif;
4912 	kstat_named_t	*kn;
4913 	boolean_t isloopback;
4914 	ipsq_t *old_ipsq;
4915 	in6_addr_t ov6addr;
4916 
4917 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4918 
4919 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4920 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4921 	rw_exit(&ipst->ips_ill_g_lock);
4922 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4923 		return (ill);
4924 
4925 	/*
4926 	 * Couldn't find it.  Does this happen to be a lookup for the
4927 	 * loopback device and are we allowed to allocate it?
4928 	 */
4929 	if (!isloopback || !do_alloc)
4930 		return (NULL);
4931 
4932 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4933 
4934 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4935 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4936 		rw_exit(&ipst->ips_ill_g_lock);
4937 		return (ill);
4938 	}
4939 
4940 	/* Create the loopback device on demand */
4941 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4942 	    sizeof (ipif_loopback_name), BPRI_MED));
4943 	if (ill == NULL)
4944 		goto done;
4945 
4946 	*ill = ill_null;
4947 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4948 	ill->ill_ipst = ipst;
4949 	netstack_hold(ipst->ips_netstack);
4950 	/*
4951 	 * For exclusive stacks we set the zoneid to zero
4952 	 * to make IP operate as if in the global zone.
4953 	 */
4954 	ill->ill_zoneid = GLOBAL_ZONEID;
4955 
4956 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4957 	if (ill->ill_phyint == NULL)
4958 		goto done;
4959 
4960 	if (isv6)
4961 		ill->ill_phyint->phyint_illv6 = ill;
4962 	else
4963 		ill->ill_phyint->phyint_illv4 = ill;
4964 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4965 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4966 	/* Add room for tcp+ip headers */
4967 	if (isv6) {
4968 		ill->ill_isv6 = B_TRUE;
4969 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4970 	} else {
4971 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4972 	}
4973 	if (!ill_allocate_mibs(ill))
4974 		goto done;
4975 	ill->ill_max_mtu = ill->ill_max_frag;
4976 	/*
4977 	 * ipif_loopback_name can't be pointed at directly because its used
4978 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4979 	 * from the glist, ill_glist_delete() sets the first character of
4980 	 * ill_name to '\0'.
4981 	 */
4982 	ill->ill_name = (char *)ill + sizeof (*ill);
4983 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4984 	ill->ill_name_length = sizeof (ipif_loopback_name);
4985 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4986 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4987 
4988 	ill->ill_global_timer = INFINITY;
4989 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4990 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4991 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4992 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4993 
4994 	/* No resolver here. */
4995 	ill->ill_net_type = IRE_LOOPBACK;
4996 
4997 	/* Initialize the ipsq */
4998 	if (!ipsq_init(ill))
4999 		goto done;
5000 
5001 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5002 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5003 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5004 #ifdef DEBUG
5005 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5006 #endif
5007 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5008 	if (ipif == NULL)
5009 		goto done;
5010 
5011 	ill->ill_flags = ILLF_MULTICAST;
5012 
5013 	ov6addr = ipif->ipif_v6lcl_addr;
5014 	/* Set up default loopback address and mask. */
5015 	if (!isv6) {
5016 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5017 
5018 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5019 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5020 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5021 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5022 		    ipif->ipif_v6subnet);
5023 		ill->ill_flags |= ILLF_IPV4;
5024 	} else {
5025 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5026 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5027 		ipif->ipif_v6net_mask = ipv6_all_ones;
5028 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5029 		    ipif->ipif_v6subnet);
5030 		ill->ill_flags |= ILLF_IPV6;
5031 	}
5032 
5033 	/*
5034 	 * Chain us in at the end of the ill list. hold the ill
5035 	 * before we make it globally visible. 1 for the lookup.
5036 	 */
5037 	ill->ill_refcnt = 0;
5038 	ill_refhold(ill);
5039 
5040 	ill->ill_frag_count = 0;
5041 	ill->ill_frag_free_num_pkts = 0;
5042 	ill->ill_last_frag_clean_time = 0;
5043 
5044 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5045 
5046 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5047 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5048 
5049 	/* Let SCTP know so that it can add this to its list */
5050 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5051 
5052 	/*
5053 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5054 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5055 	 * requires to be after ill_glist_insert() since we need the
5056 	 * ill_index set. Pass on ipv6_loopback as the old address.
5057 	 */
5058 	sctp_update_ipif_addr(ipif, ov6addr);
5059 
5060 	/*
5061 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5062 	 */
5063 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5064 		/* Loopback ills aren't in any IPMP group */
5065 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5066 		ipsq_delete(old_ipsq);
5067 	}
5068 
5069 	/*
5070 	 * Delay this till the ipif is allocated as ipif_allocate
5071 	 * de-references ill_phyint for getting the ifindex. We
5072 	 * can't do this before ipif_allocate because ill_phyint_reinit
5073 	 * -> phyint_assign_ifindex expects ipif to be present.
5074 	 */
5075 	mutex_enter(&ill->ill_phyint->phyint_lock);
5076 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5077 	mutex_exit(&ill->ill_phyint->phyint_lock);
5078 
5079 	if (ipst->ips_loopback_ksp == NULL) {
5080 		/* Export loopback interface statistics */
5081 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5082 		    ipif_loopback_name, "net",
5083 		    KSTAT_TYPE_NAMED, 2, 0,
5084 		    ipst->ips_netstack->netstack_stackid);
5085 		if (ipst->ips_loopback_ksp != NULL) {
5086 			ipst->ips_loopback_ksp->ks_update =
5087 			    loopback_kstat_update;
5088 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5089 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5090 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5091 			ipst->ips_loopback_ksp->ks_private =
5092 			    (void *)(uintptr_t)ipst->ips_netstack->
5093 			    netstack_stackid;
5094 			kstat_install(ipst->ips_loopback_ksp);
5095 		}
5096 	}
5097 
5098 	if (error != NULL)
5099 		*error = 0;
5100 	*did_alloc = B_TRUE;
5101 	rw_exit(&ipst->ips_ill_g_lock);
5102 	return (ill);
5103 done:
5104 	if (ill != NULL) {
5105 		if (ill->ill_phyint != NULL) {
5106 			ipsq_t	*ipsq;
5107 
5108 			ipsq = ill->ill_phyint->phyint_ipsq;
5109 			if (ipsq != NULL) {
5110 				ipsq->ipsq_ipst = NULL;
5111 				kmem_free(ipsq, sizeof (ipsq_t));
5112 			}
5113 			mi_free(ill->ill_phyint);
5114 		}
5115 		ill_free_mib(ill);
5116 		if (ill->ill_ipst != NULL)
5117 			netstack_rele(ill->ill_ipst->ips_netstack);
5118 		mi_free(ill);
5119 	}
5120 	rw_exit(&ipst->ips_ill_g_lock);
5121 	if (error != NULL)
5122 		*error = ENOMEM;
5123 	return (NULL);
5124 }
5125 
5126 /*
5127  * For IPP calls - use the ip_stack_t for global stack.
5128  */
5129 ill_t *
5130 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5131     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5132 {
5133 	ip_stack_t	*ipst;
5134 	ill_t		*ill;
5135 
5136 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5137 	if (ipst == NULL) {
5138 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5139 		return (NULL);
5140 	}
5141 
5142 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5143 	netstack_rele(ipst->ips_netstack);
5144 	return (ill);
5145 }
5146 
5147 /*
5148  * Return a pointer to the ill which matches the index and IP version type.
5149  */
5150 ill_t *
5151 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5152     ipsq_func_t func, int *err, ip_stack_t *ipst)
5153 {
5154 	ill_t	*ill;
5155 	ipsq_t  *ipsq;
5156 	phyint_t *phyi;
5157 
5158 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5159 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5160 
5161 	if (err != NULL)
5162 		*err = 0;
5163 
5164 	/*
5165 	 * Indexes are stored in the phyint - a common structure
5166 	 * to both IPv4 and IPv6.
5167 	 */
5168 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5169 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5170 	    (void *) &index, NULL);
5171 	if (phyi != NULL) {
5172 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5173 		if (ill != NULL) {
5174 			/*
5175 			 * The block comment at the start of ipif_down
5176 			 * explains the use of the macros used below
5177 			 */
5178 			GRAB_CONN_LOCK(q);
5179 			mutex_enter(&ill->ill_lock);
5180 			if (ILL_CAN_LOOKUP(ill)) {
5181 				ill_refhold_locked(ill);
5182 				mutex_exit(&ill->ill_lock);
5183 				RELEASE_CONN_LOCK(q);
5184 				rw_exit(&ipst->ips_ill_g_lock);
5185 				return (ill);
5186 			} else if (ILL_CAN_WAIT(ill, q)) {
5187 				ipsq = ill->ill_phyint->phyint_ipsq;
5188 				mutex_enter(&ipsq->ipsq_lock);
5189 				rw_exit(&ipst->ips_ill_g_lock);
5190 				mutex_exit(&ill->ill_lock);
5191 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5192 				mutex_exit(&ipsq->ipsq_lock);
5193 				RELEASE_CONN_LOCK(q);
5194 				if (err != NULL)
5195 					*err = EINPROGRESS;
5196 				return (NULL);
5197 			}
5198 			RELEASE_CONN_LOCK(q);
5199 			mutex_exit(&ill->ill_lock);
5200 		}
5201 	}
5202 	rw_exit(&ipst->ips_ill_g_lock);
5203 	if (err != NULL)
5204 		*err = ENXIO;
5205 	return (NULL);
5206 }
5207 
5208 /*
5209  * Return the ifindex next in sequence after the passed in ifindex.
5210  * If there is no next ifindex for the given protocol, return 0.
5211  */
5212 uint_t
5213 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5214 {
5215 	phyint_t *phyi;
5216 	phyint_t *phyi_initial;
5217 	uint_t   ifindex;
5218 
5219 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5220 
5221 	if (index == 0) {
5222 		phyi = avl_first(
5223 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5224 	} else {
5225 		phyi = phyi_initial = avl_find(
5226 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5227 		    (void *) &index, NULL);
5228 	}
5229 
5230 	for (; phyi != NULL;
5231 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5232 	    phyi, AVL_AFTER)) {
5233 		/*
5234 		 * If we're not returning the first interface in the tree
5235 		 * and we still haven't moved past the phyint_t that
5236 		 * corresponds to index, avl_walk needs to be called again
5237 		 */
5238 		if (!((index != 0) && (phyi == phyi_initial))) {
5239 			if (isv6) {
5240 				if ((phyi->phyint_illv6) &&
5241 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5242 				    (phyi->phyint_illv6->ill_isv6 == 1))
5243 					break;
5244 			} else {
5245 				if ((phyi->phyint_illv4) &&
5246 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5247 				    (phyi->phyint_illv4->ill_isv6 == 0))
5248 					break;
5249 			}
5250 		}
5251 	}
5252 
5253 	rw_exit(&ipst->ips_ill_g_lock);
5254 
5255 	if (phyi != NULL)
5256 		ifindex = phyi->phyint_ifindex;
5257 	else
5258 		ifindex = 0;
5259 
5260 	return (ifindex);
5261 }
5262 
5263 
5264 /*
5265  * Return the ifindex for the named interface.
5266  * If there is no next ifindex for the interface, return 0.
5267  */
5268 uint_t
5269 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5270 {
5271 	phyint_t	*phyi;
5272 	avl_index_t	where = 0;
5273 	uint_t		ifindex;
5274 
5275 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5276 
5277 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5278 	    name, &where)) == NULL) {
5279 		rw_exit(&ipst->ips_ill_g_lock);
5280 		return (0);
5281 	}
5282 
5283 	ifindex = phyi->phyint_ifindex;
5284 
5285 	rw_exit(&ipst->ips_ill_g_lock);
5286 
5287 	return (ifindex);
5288 }
5289 
5290 
5291 /*
5292  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5293  * that gives a running thread a reference to the ill. This reference must be
5294  * released by the thread when it is done accessing the ill and related
5295  * objects. ill_refcnt can not be used to account for static references
5296  * such as other structures pointing to an ill. Callers must generally
5297  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5298  * or be sure that the ill is not being deleted or changing state before
5299  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5300  * ill won't change any of its critical state such as address, netmask etc.
5301  */
5302 void
5303 ill_refhold(ill_t *ill)
5304 {
5305 	mutex_enter(&ill->ill_lock);
5306 	ill->ill_refcnt++;
5307 	ILL_TRACE_REF(ill);
5308 	mutex_exit(&ill->ill_lock);
5309 }
5310 
5311 void
5312 ill_refhold_locked(ill_t *ill)
5313 {
5314 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5315 	ill->ill_refcnt++;
5316 	ILL_TRACE_REF(ill);
5317 }
5318 
5319 int
5320 ill_check_and_refhold(ill_t *ill)
5321 {
5322 	mutex_enter(&ill->ill_lock);
5323 	if (ILL_CAN_LOOKUP(ill)) {
5324 		ill_refhold_locked(ill);
5325 		mutex_exit(&ill->ill_lock);
5326 		return (0);
5327 	}
5328 	mutex_exit(&ill->ill_lock);
5329 	return (ILL_LOOKUP_FAILED);
5330 }
5331 
5332 /*
5333  * Must not be called while holding any locks. Otherwise if this is
5334  * the last reference to be released, there is a chance of recursive mutex
5335  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5336  * to restart an ioctl.
5337  */
5338 void
5339 ill_refrele(ill_t *ill)
5340 {
5341 	mutex_enter(&ill->ill_lock);
5342 	ASSERT(ill->ill_refcnt != 0);
5343 	ill->ill_refcnt--;
5344 	ILL_UNTRACE_REF(ill);
5345 	if (ill->ill_refcnt != 0) {
5346 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5347 		mutex_exit(&ill->ill_lock);
5348 		return;
5349 	}
5350 
5351 	/* Drops the ill_lock */
5352 	ipif_ill_refrele_tail(ill);
5353 }
5354 
5355 /*
5356  * Obtain a weak reference count on the ill. This reference ensures the
5357  * ill won't be freed, but the ill may change any of its critical state
5358  * such as netmask, address etc. Returns an error if the ill has started
5359  * closing.
5360  */
5361 boolean_t
5362 ill_waiter_inc(ill_t *ill)
5363 {
5364 	mutex_enter(&ill->ill_lock);
5365 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5366 		mutex_exit(&ill->ill_lock);
5367 		return (B_FALSE);
5368 	}
5369 	ill->ill_waiters++;
5370 	mutex_exit(&ill->ill_lock);
5371 	return (B_TRUE);
5372 }
5373 
5374 void
5375 ill_waiter_dcr(ill_t *ill)
5376 {
5377 	mutex_enter(&ill->ill_lock);
5378 	ill->ill_waiters--;
5379 	if (ill->ill_waiters == 0)
5380 		cv_broadcast(&ill->ill_cv);
5381 	mutex_exit(&ill->ill_lock);
5382 }
5383 
5384 /*
5385  * Named Dispatch routine to produce a formatted report on all ILLs.
5386  * This report is accessed by using the ndd utility to "get" ND variable
5387  * "ip_ill_status".
5388  */
5389 /* ARGSUSED */
5390 int
5391 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5392 {
5393 	ill_t		*ill;
5394 	ill_walk_context_t ctx;
5395 	ip_stack_t	*ipst;
5396 
5397 	ipst = CONNQ_TO_IPST(q);
5398 
5399 	(void) mi_mpprintf(mp,
5400 	    "ILL      " MI_COL_HDRPAD_STR
5401 	/*   01234567[89ABCDEF] */
5402 	    "rq       " MI_COL_HDRPAD_STR
5403 	/*   01234567[89ABCDEF] */
5404 	    "wq       " MI_COL_HDRPAD_STR
5405 	/*   01234567[89ABCDEF] */
5406 	    "upcnt mxfrg err name");
5407 	/*   12345 12345 123 xxxxxxxx  */
5408 
5409 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5410 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5411 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5412 		(void) mi_mpprintf(mp,
5413 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5414 		    "%05u %05u %03d %s",
5415 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5416 		    ill->ill_ipif_up_count,
5417 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5418 	}
5419 	rw_exit(&ipst->ips_ill_g_lock);
5420 
5421 	return (0);
5422 }
5423 
5424 /*
5425  * Named Dispatch routine to produce a formatted report on all IPIFs.
5426  * This report is accessed by using the ndd utility to "get" ND variable
5427  * "ip_ipif_status".
5428  */
5429 /* ARGSUSED */
5430 int
5431 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5432 {
5433 	char	buf1[INET6_ADDRSTRLEN];
5434 	char	buf2[INET6_ADDRSTRLEN];
5435 	char	buf3[INET6_ADDRSTRLEN];
5436 	char	buf4[INET6_ADDRSTRLEN];
5437 	char	buf5[INET6_ADDRSTRLEN];
5438 	char	buf6[INET6_ADDRSTRLEN];
5439 	char	buf[LIFNAMSIZ];
5440 	ill_t	*ill;
5441 	ipif_t	*ipif;
5442 	nv_t	*nvp;
5443 	uint64_t flags;
5444 	zoneid_t zoneid;
5445 	ill_walk_context_t ctx;
5446 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5447 
5448 	(void) mi_mpprintf(mp,
5449 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5450 	    "\tlocal address\n"
5451 	    "\tsrc address\n"
5452 	    "\tsubnet\n"
5453 	    "\tmask\n"
5454 	    "\tbroadcast\n"
5455 	    "\tp-p-dst");
5456 
5457 	ASSERT(q->q_next == NULL);
5458 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5459 
5460 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5461 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5462 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5463 		for (ipif = ill->ill_ipif; ipif != NULL;
5464 		    ipif = ipif->ipif_next) {
5465 			if (zoneid != GLOBAL_ZONEID &&
5466 			    zoneid != ipif->ipif_zoneid &&
5467 			    ipif->ipif_zoneid != ALL_ZONES)
5468 				continue;
5469 
5470 			ipif_get_name(ipif, buf, sizeof (buf));
5471 			(void) mi_mpprintf(mp,
5472 			    MI_COL_PTRFMT_STR
5473 			    "%04u %05u %u/%u/%u %s %d",
5474 			    (void *)ipif,
5475 			    ipif->ipif_metric, ipif->ipif_mtu,
5476 			    ipif->ipif_ib_pkt_count,
5477 			    ipif->ipif_ob_pkt_count,
5478 			    ipif->ipif_fo_pkt_count,
5479 			    buf,
5480 			    ipif->ipif_zoneid);
5481 
5482 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5483 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5484 
5485 		/* Tack on text strings for any flags. */
5486 		nvp = ipif_nv_tbl;
5487 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5488 			if (nvp->nv_value & flags)
5489 				(void) mi_mpprintf_nr(mp, " %s",
5490 				    nvp->nv_name);
5491 		}
5492 		(void) mi_mpprintf(mp,
5493 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5494 		    inet_ntop(AF_INET6,
5495 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5496 		    inet_ntop(AF_INET6,
5497 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5498 		    inet_ntop(AF_INET6,
5499 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5500 		    inet_ntop(AF_INET6,
5501 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5502 		    inet_ntop(AF_INET6,
5503 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5504 		    inet_ntop(AF_INET6,
5505 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5506 		}
5507 	}
5508 	rw_exit(&ipst->ips_ill_g_lock);
5509 	return (0);
5510 }
5511 
5512 /*
5513  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5514  * driver.  We construct best guess defaults for lower level information that
5515  * we need.  If an interface is brought up without injection of any overriding
5516  * information from outside, we have to be ready to go with these defaults.
5517  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5518  * we primarely want the dl_provider_style.
5519  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5520  * at which point we assume the other part of the information is valid.
5521  */
5522 void
5523 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5524 {
5525 	uchar_t		*brdcst_addr;
5526 	uint_t		brdcst_addr_length, phys_addr_length;
5527 	t_scalar_t	sap_length;
5528 	dl_info_ack_t	*dlia;
5529 	ip_m_t		*ipm;
5530 	dl_qos_cl_sel1_t *sel1;
5531 
5532 	ASSERT(IAM_WRITER_ILL(ill));
5533 
5534 	/*
5535 	 * Till the ill is fully up ILL_CHANGING will be set and
5536 	 * the ill is not globally visible. So no need for a lock.
5537 	 */
5538 	dlia = (dl_info_ack_t *)mp->b_rptr;
5539 	ill->ill_mactype = dlia->dl_mac_type;
5540 
5541 	ipm = ip_m_lookup(dlia->dl_mac_type);
5542 	if (ipm == NULL) {
5543 		ipm = ip_m_lookup(DL_OTHER);
5544 		ASSERT(ipm != NULL);
5545 	}
5546 	ill->ill_media = ipm;
5547 
5548 	/*
5549 	 * When the new DLPI stuff is ready we'll pull lengths
5550 	 * from dlia.
5551 	 */
5552 	if (dlia->dl_version == DL_VERSION_2) {
5553 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5554 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5555 		    brdcst_addr_length);
5556 		if (brdcst_addr == NULL) {
5557 			brdcst_addr_length = 0;
5558 		}
5559 		sap_length = dlia->dl_sap_length;
5560 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5561 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5562 		    brdcst_addr_length, sap_length, phys_addr_length));
5563 	} else {
5564 		brdcst_addr_length = 6;
5565 		brdcst_addr = ip_six_byte_all_ones;
5566 		sap_length = -2;
5567 		phys_addr_length = brdcst_addr_length;
5568 	}
5569 
5570 	ill->ill_bcast_addr_length = brdcst_addr_length;
5571 	ill->ill_phys_addr_length = phys_addr_length;
5572 	ill->ill_sap_length = sap_length;
5573 	ill->ill_max_frag = dlia->dl_max_sdu;
5574 	ill->ill_max_mtu = ill->ill_max_frag;
5575 
5576 	ill->ill_type = ipm->ip_m_type;
5577 
5578 	if (!ill->ill_dlpi_style_set) {
5579 		if (dlia->dl_provider_style == DL_STYLE2)
5580 			ill->ill_needs_attach = 1;
5581 
5582 		/*
5583 		 * Allocate the first ipif on this ill. We don't delay it
5584 		 * further as ioctl handling assumes atleast one ipif to
5585 		 * be present.
5586 		 *
5587 		 * At this point we don't know whether the ill is v4 or v6.
5588 		 * We will know this whan the SIOCSLIFNAME happens and
5589 		 * the correct value for ill_isv6 will be assigned in
5590 		 * ipif_set_values(). We need to hold the ill lock and
5591 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5592 		 * the wakeup.
5593 		 */
5594 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5595 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5596 		mutex_enter(&ill->ill_lock);
5597 		ASSERT(ill->ill_dlpi_style_set == 0);
5598 		ill->ill_dlpi_style_set = 1;
5599 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5600 		cv_broadcast(&ill->ill_cv);
5601 		mutex_exit(&ill->ill_lock);
5602 		freemsg(mp);
5603 		return;
5604 	}
5605 	ASSERT(ill->ill_ipif != NULL);
5606 	/*
5607 	 * We know whether it is IPv4 or IPv6 now, as this is the
5608 	 * second DL_INFO_ACK we are recieving in response to the
5609 	 * DL_INFO_REQ sent in ipif_set_values.
5610 	 */
5611 	if (ill->ill_isv6)
5612 		ill->ill_sap = IP6_DL_SAP;
5613 	else
5614 		ill->ill_sap = IP_DL_SAP;
5615 	/*
5616 	 * Set ipif_mtu which is used to set the IRE's
5617 	 * ire_max_frag value. The driver could have sent
5618 	 * a different mtu from what it sent last time. No
5619 	 * need to call ipif_mtu_change because IREs have
5620 	 * not yet been created.
5621 	 */
5622 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5623 	/*
5624 	 * Clear all the flags that were set based on ill_bcast_addr_length
5625 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5626 	 * changed now and we need to re-evaluate.
5627 	 */
5628 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5629 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5630 
5631 	/*
5632 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5633 	 * changed now.
5634 	 */
5635 	if (ill->ill_bcast_addr_length == 0) {
5636 		if (ill->ill_resolver_mp != NULL)
5637 			freemsg(ill->ill_resolver_mp);
5638 		if (ill->ill_bcast_mp != NULL)
5639 			freemsg(ill->ill_bcast_mp);
5640 		if (ill->ill_flags & ILLF_XRESOLV)
5641 			ill->ill_net_type = IRE_IF_RESOLVER;
5642 		else
5643 			ill->ill_net_type = IRE_IF_NORESOLVER;
5644 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5645 		    ill->ill_phys_addr_length,
5646 		    ill->ill_sap,
5647 		    ill->ill_sap_length);
5648 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5649 
5650 		if (ill->ill_isv6)
5651 			/*
5652 			 * Note: xresolv interfaces will eventually need NOARP
5653 			 * set here as well, but that will require those
5654 			 * external resolvers to have some knowledge of
5655 			 * that flag and act appropriately. Not to be changed
5656 			 * at present.
5657 			 */
5658 			ill->ill_flags |= ILLF_NONUD;
5659 		else
5660 			ill->ill_flags |= ILLF_NOARP;
5661 
5662 		if (ill->ill_phys_addr_length == 0) {
5663 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5664 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5665 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5666 			} else {
5667 				/* pt-pt supports multicast. */
5668 				ill->ill_flags |= ILLF_MULTICAST;
5669 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5670 			}
5671 		}
5672 	} else {
5673 		ill->ill_net_type = IRE_IF_RESOLVER;
5674 		if (ill->ill_bcast_mp != NULL)
5675 			freemsg(ill->ill_bcast_mp);
5676 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5677 		    ill->ill_bcast_addr_length, ill->ill_sap,
5678 		    ill->ill_sap_length);
5679 		/*
5680 		 * Later detect lack of DLPI driver multicast
5681 		 * capability by catching DL_ENABMULTI errors in
5682 		 * ip_rput_dlpi.
5683 		 */
5684 		ill->ill_flags |= ILLF_MULTICAST;
5685 		if (!ill->ill_isv6)
5686 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5687 	}
5688 	/* By default an interface does not support any CoS marking */
5689 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5690 
5691 	/*
5692 	 * If we get QoS information in DL_INFO_ACK, the device supports
5693 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5694 	 */
5695 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5696 	    dlia->dl_qos_length);
5697 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5698 		ill->ill_flags |= ILLF_COS_ENABLED;
5699 	}
5700 
5701 	/* Clear any previous error indication. */
5702 	ill->ill_error = 0;
5703 	freemsg(mp);
5704 }
5705 
5706 /*
5707  * Perform various checks to verify that an address would make sense as a
5708  * local, remote, or subnet interface address.
5709  */
5710 static boolean_t
5711 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5712 {
5713 	ipaddr_t	net_mask;
5714 
5715 	/*
5716 	 * Don't allow all zeroes, or all ones, but allow
5717 	 * all ones netmask.
5718 	 */
5719 	if ((net_mask = ip_net_mask(addr)) == 0)
5720 		return (B_FALSE);
5721 	/* A given netmask overrides the "guess" netmask */
5722 	if (subnet_mask != 0)
5723 		net_mask = subnet_mask;
5724 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5725 	    (addr == (addr | ~net_mask)))) {
5726 		return (B_FALSE);
5727 	}
5728 
5729 	/*
5730 	 * Even if the netmask is all ones, we do not allow address to be
5731 	 * 255.255.255.255
5732 	 */
5733 	if (addr == INADDR_BROADCAST)
5734 		return (B_FALSE);
5735 
5736 	if (CLASSD(addr))
5737 		return (B_FALSE);
5738 
5739 	return (B_TRUE);
5740 }
5741 
5742 #define	V6_IPIF_LINKLOCAL(p)	\
5743 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5744 
5745 /*
5746  * Compare two given ipifs and check if the second one is better than
5747  * the first one using the order of preference (not taking deprecated
5748  * into acount) specified in ipif_lookup_multicast().
5749  */
5750 static boolean_t
5751 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5752 {
5753 	/* Check the least preferred first. */
5754 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5755 		/* If both ipifs are the same, use the first one. */
5756 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5757 			return (B_FALSE);
5758 		else
5759 			return (B_TRUE);
5760 	}
5761 
5762 	/* For IPv6, check for link local address. */
5763 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5764 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5765 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5766 			/* The second one is equal or less preferred. */
5767 			return (B_FALSE);
5768 		} else {
5769 			return (B_TRUE);
5770 		}
5771 	}
5772 
5773 	/* Then check for point to point interface. */
5774 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5775 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5776 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5777 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5778 			return (B_FALSE);
5779 		} else {
5780 			return (B_TRUE);
5781 		}
5782 	}
5783 
5784 	/* old_ipif is a normal interface, so no need to use the new one. */
5785 	return (B_FALSE);
5786 }
5787 
5788 /*
5789  * Find any non-virtual, not condemned, and up multicast capable interface
5790  * given an IP instance and zoneid.  Order of preference is:
5791  *
5792  * 1. normal
5793  * 1.1 normal, but deprecated
5794  * 2. point to point
5795  * 2.1 point to point, but deprecated
5796  * 3. link local
5797  * 3.1 link local, but deprecated
5798  * 4. loopback.
5799  */
5800 ipif_t *
5801 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5802 {
5803 	ill_t			*ill;
5804 	ill_walk_context_t	ctx;
5805 	ipif_t			*ipif;
5806 	ipif_t			*saved_ipif = NULL;
5807 	ipif_t			*dep_ipif = NULL;
5808 
5809 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5810 	if (isv6)
5811 		ill = ILL_START_WALK_V6(&ctx, ipst);
5812 	else
5813 		ill = ILL_START_WALK_V4(&ctx, ipst);
5814 
5815 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5816 		mutex_enter(&ill->ill_lock);
5817 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5818 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5819 			mutex_exit(&ill->ill_lock);
5820 			continue;
5821 		}
5822 		for (ipif = ill->ill_ipif; ipif != NULL;
5823 		    ipif = ipif->ipif_next) {
5824 			if (zoneid != ipif->ipif_zoneid &&
5825 			    zoneid != ALL_ZONES &&
5826 			    ipif->ipif_zoneid != ALL_ZONES) {
5827 				continue;
5828 			}
5829 			if (!(ipif->ipif_flags & IPIF_UP) ||
5830 			    !IPIF_CAN_LOOKUP(ipif)) {
5831 				continue;
5832 			}
5833 
5834 			/*
5835 			 * Found one candidate.  If it is deprecated,
5836 			 * remember it in dep_ipif.  If it is not deprecated,
5837 			 * remember it in saved_ipif.
5838 			 */
5839 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5840 				if (dep_ipif == NULL) {
5841 					dep_ipif = ipif;
5842 				} else if (ipif_comp_multi(dep_ipif, ipif,
5843 				    isv6)) {
5844 					/*
5845 					 * If the previous dep_ipif does not
5846 					 * belong to the same ill, we've done
5847 					 * a ipif_refhold() on it.  So we need
5848 					 * to release it.
5849 					 */
5850 					if (dep_ipif->ipif_ill != ill)
5851 						ipif_refrele(dep_ipif);
5852 					dep_ipif = ipif;
5853 				}
5854 				continue;
5855 			}
5856 			if (saved_ipif == NULL) {
5857 				saved_ipif = ipif;
5858 			} else {
5859 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5860 					if (saved_ipif->ipif_ill != ill)
5861 						ipif_refrele(saved_ipif);
5862 					saved_ipif = ipif;
5863 				}
5864 			}
5865 		}
5866 		/*
5867 		 * Before going to the next ill, do a ipif_refhold() on the
5868 		 * saved ones.
5869 		 */
5870 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5871 			ipif_refhold_locked(saved_ipif);
5872 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5873 			ipif_refhold_locked(dep_ipif);
5874 		mutex_exit(&ill->ill_lock);
5875 	}
5876 	rw_exit(&ipst->ips_ill_g_lock);
5877 
5878 	/*
5879 	 * If we have only the saved_ipif, return it.  But if we have both
5880 	 * saved_ipif and dep_ipif, check to see which one is better.
5881 	 */
5882 	if (saved_ipif != NULL) {
5883 		if (dep_ipif != NULL) {
5884 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5885 				ipif_refrele(saved_ipif);
5886 				return (dep_ipif);
5887 			} else {
5888 				ipif_refrele(dep_ipif);
5889 				return (saved_ipif);
5890 			}
5891 		}
5892 		return (saved_ipif);
5893 	} else {
5894 		return (dep_ipif);
5895 	}
5896 }
5897 
5898 /*
5899  * This function is called when an application does not specify an interface
5900  * to be used for multicast traffic (joining a group/sending data).  It
5901  * calls ire_lookup_multi() to look for an interface route for the
5902  * specified multicast group.  Doing this allows the administrator to add
5903  * prefix routes for multicast to indicate which interface to be used for
5904  * multicast traffic in the above scenario.  The route could be for all
5905  * multicast (224.0/4), for a single multicast group (a /32 route) or
5906  * anything in between.  If there is no such multicast route, we just find
5907  * any multicast capable interface and return it.  The returned ipif
5908  * is refhold'ed.
5909  */
5910 ipif_t *
5911 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5912 {
5913 	ire_t			*ire;
5914 	ipif_t			*ipif;
5915 
5916 	ire = ire_lookup_multi(group, zoneid, ipst);
5917 	if (ire != NULL) {
5918 		ipif = ire->ire_ipif;
5919 		ipif_refhold(ipif);
5920 		ire_refrele(ire);
5921 		return (ipif);
5922 	}
5923 
5924 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5925 }
5926 
5927 /*
5928  * Look for an ipif with the specified interface address and destination.
5929  * The destination address is used only for matching point-to-point interfaces.
5930  */
5931 ipif_t *
5932 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5933     ipsq_func_t func, int *error, ip_stack_t *ipst)
5934 {
5935 	ipif_t	*ipif;
5936 	ill_t	*ill;
5937 	ill_walk_context_t ctx;
5938 	ipsq_t	*ipsq;
5939 
5940 	if (error != NULL)
5941 		*error = 0;
5942 
5943 	/*
5944 	 * First match all the point-to-point interfaces
5945 	 * before looking at non-point-to-point interfaces.
5946 	 * This is done to avoid returning non-point-to-point
5947 	 * ipif instead of unnumbered point-to-point ipif.
5948 	 */
5949 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5950 	ill = ILL_START_WALK_V4(&ctx, ipst);
5951 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5952 		GRAB_CONN_LOCK(q);
5953 		mutex_enter(&ill->ill_lock);
5954 		for (ipif = ill->ill_ipif; ipif != NULL;
5955 		    ipif = ipif->ipif_next) {
5956 			/* Allow the ipif to be down */
5957 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5958 			    (ipif->ipif_lcl_addr == if_addr) &&
5959 			    (ipif->ipif_pp_dst_addr == dst)) {
5960 				/*
5961 				 * The block comment at the start of ipif_down
5962 				 * explains the use of the macros used below
5963 				 */
5964 				if (IPIF_CAN_LOOKUP(ipif)) {
5965 					ipif_refhold_locked(ipif);
5966 					mutex_exit(&ill->ill_lock);
5967 					RELEASE_CONN_LOCK(q);
5968 					rw_exit(&ipst->ips_ill_g_lock);
5969 					return (ipif);
5970 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5971 					ipsq = ill->ill_phyint->phyint_ipsq;
5972 					mutex_enter(&ipsq->ipsq_lock);
5973 					mutex_exit(&ill->ill_lock);
5974 					rw_exit(&ipst->ips_ill_g_lock);
5975 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5976 					    ill);
5977 					mutex_exit(&ipsq->ipsq_lock);
5978 					RELEASE_CONN_LOCK(q);
5979 					if (error != NULL)
5980 						*error = EINPROGRESS;
5981 					return (NULL);
5982 				}
5983 			}
5984 		}
5985 		mutex_exit(&ill->ill_lock);
5986 		RELEASE_CONN_LOCK(q);
5987 	}
5988 	rw_exit(&ipst->ips_ill_g_lock);
5989 
5990 	/* lookup the ipif based on interface address */
5991 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5992 	    ipst);
5993 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5994 	return (ipif);
5995 }
5996 
5997 /*
5998  * Look for an ipif with the specified address. For point-point links
5999  * we look for matches on either the destination address and the local
6000  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6001  * is set.
6002  * Matches on a specific ill if match_ill is set.
6003  */
6004 ipif_t *
6005 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6006     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6007 {
6008 	ipif_t  *ipif;
6009 	ill_t   *ill;
6010 	boolean_t ptp = B_FALSE;
6011 	ipsq_t	*ipsq;
6012 	ill_walk_context_t	ctx;
6013 
6014 	if (error != NULL)
6015 		*error = 0;
6016 
6017 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6018 	/*
6019 	 * Repeat twice, first based on local addresses and
6020 	 * next time for pointopoint.
6021 	 */
6022 repeat:
6023 	ill = ILL_START_WALK_V4(&ctx, ipst);
6024 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6025 		if (match_ill != NULL && ill != match_ill) {
6026 			continue;
6027 		}
6028 		GRAB_CONN_LOCK(q);
6029 		mutex_enter(&ill->ill_lock);
6030 		for (ipif = ill->ill_ipif; ipif != NULL;
6031 		    ipif = ipif->ipif_next) {
6032 			if (zoneid != ALL_ZONES &&
6033 			    zoneid != ipif->ipif_zoneid &&
6034 			    ipif->ipif_zoneid != ALL_ZONES)
6035 				continue;
6036 			/* Allow the ipif to be down */
6037 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6038 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6039 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6040 			    (ipif->ipif_pp_dst_addr == addr))) {
6041 				/*
6042 				 * The block comment at the start of ipif_down
6043 				 * explains the use of the macros used below
6044 				 */
6045 				if (IPIF_CAN_LOOKUP(ipif)) {
6046 					ipif_refhold_locked(ipif);
6047 					mutex_exit(&ill->ill_lock);
6048 					RELEASE_CONN_LOCK(q);
6049 					rw_exit(&ipst->ips_ill_g_lock);
6050 					return (ipif);
6051 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6052 					ipsq = ill->ill_phyint->phyint_ipsq;
6053 					mutex_enter(&ipsq->ipsq_lock);
6054 					mutex_exit(&ill->ill_lock);
6055 					rw_exit(&ipst->ips_ill_g_lock);
6056 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6057 					    ill);
6058 					mutex_exit(&ipsq->ipsq_lock);
6059 					RELEASE_CONN_LOCK(q);
6060 					if (error != NULL)
6061 						*error = EINPROGRESS;
6062 					return (NULL);
6063 				}
6064 			}
6065 		}
6066 		mutex_exit(&ill->ill_lock);
6067 		RELEASE_CONN_LOCK(q);
6068 	}
6069 
6070 	/* If we already did the ptp case, then we are done */
6071 	if (ptp) {
6072 		rw_exit(&ipst->ips_ill_g_lock);
6073 		if (error != NULL)
6074 			*error = ENXIO;
6075 		return (NULL);
6076 	}
6077 	ptp = B_TRUE;
6078 	goto repeat;
6079 }
6080 
6081 /*
6082  * Look for an ipif with the specified address. For point-point links
6083  * we look for matches on either the destination address and the local
6084  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6085  * is set.
6086  * Matches on a specific ill if match_ill is set.
6087  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6088  */
6089 zoneid_t
6090 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6091 {
6092 	zoneid_t zoneid;
6093 	ipif_t  *ipif;
6094 	ill_t   *ill;
6095 	boolean_t ptp = B_FALSE;
6096 	ill_walk_context_t	ctx;
6097 
6098 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6099 	/*
6100 	 * Repeat twice, first based on local addresses and
6101 	 * next time for pointopoint.
6102 	 */
6103 repeat:
6104 	ill = ILL_START_WALK_V4(&ctx, ipst);
6105 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6106 		if (match_ill != NULL && ill != match_ill) {
6107 			continue;
6108 		}
6109 		mutex_enter(&ill->ill_lock);
6110 		for (ipif = ill->ill_ipif; ipif != NULL;
6111 		    ipif = ipif->ipif_next) {
6112 			/* Allow the ipif to be down */
6113 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6114 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6115 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6116 			    (ipif->ipif_pp_dst_addr == addr)) &&
6117 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6118 				zoneid = ipif->ipif_zoneid;
6119 				mutex_exit(&ill->ill_lock);
6120 				rw_exit(&ipst->ips_ill_g_lock);
6121 				/*
6122 				 * If ipif_zoneid was ALL_ZONES then we have
6123 				 * a trusted extensions shared IP address.
6124 				 * In that case GLOBAL_ZONEID works to send.
6125 				 */
6126 				if (zoneid == ALL_ZONES)
6127 					zoneid = GLOBAL_ZONEID;
6128 				return (zoneid);
6129 			}
6130 		}
6131 		mutex_exit(&ill->ill_lock);
6132 	}
6133 
6134 	/* If we already did the ptp case, then we are done */
6135 	if (ptp) {
6136 		rw_exit(&ipst->ips_ill_g_lock);
6137 		return (ALL_ZONES);
6138 	}
6139 	ptp = B_TRUE;
6140 	goto repeat;
6141 }
6142 
6143 /*
6144  * Look for an ipif that matches the specified remote address i.e. the
6145  * ipif that would receive the specified packet.
6146  * First look for directly connected interfaces and then do a recursive
6147  * IRE lookup and pick the first ipif corresponding to the source address in the
6148  * ire.
6149  * Returns: held ipif
6150  */
6151 ipif_t *
6152 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6153 {
6154 	ipif_t	*ipif;
6155 	ire_t	*ire;
6156 	ip_stack_t	*ipst = ill->ill_ipst;
6157 
6158 	ASSERT(!ill->ill_isv6);
6159 
6160 	/*
6161 	 * Someone could be changing this ipif currently or change it
6162 	 * after we return this. Thus  a few packets could use the old
6163 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6164 	 * will atomically be updated or cleaned up with the new value
6165 	 * Thus we don't need a lock to check the flags or other attrs below.
6166 	 */
6167 	mutex_enter(&ill->ill_lock);
6168 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6169 		if (!IPIF_CAN_LOOKUP(ipif))
6170 			continue;
6171 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6172 		    ipif->ipif_zoneid != ALL_ZONES)
6173 			continue;
6174 		/* Allow the ipif to be down */
6175 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6176 			if ((ipif->ipif_pp_dst_addr == addr) ||
6177 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6178 			    ipif->ipif_lcl_addr == addr)) {
6179 				ipif_refhold_locked(ipif);
6180 				mutex_exit(&ill->ill_lock);
6181 				return (ipif);
6182 			}
6183 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6184 			ipif_refhold_locked(ipif);
6185 			mutex_exit(&ill->ill_lock);
6186 			return (ipif);
6187 		}
6188 	}
6189 	mutex_exit(&ill->ill_lock);
6190 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6191 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6192 	if (ire != NULL) {
6193 		/*
6194 		 * The callers of this function wants to know the
6195 		 * interface on which they have to send the replies
6196 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6197 		 * derived from different ills, we really don't care
6198 		 * what we return here.
6199 		 */
6200 		ipif = ire->ire_ipif;
6201 		if (ipif != NULL) {
6202 			ipif_refhold(ipif);
6203 			ire_refrele(ire);
6204 			return (ipif);
6205 		}
6206 		ire_refrele(ire);
6207 	}
6208 	/* Pick the first interface */
6209 	ipif = ipif_get_next_ipif(NULL, ill);
6210 	return (ipif);
6211 }
6212 
6213 /*
6214  * This func does not prevent refcnt from increasing. But if
6215  * the caller has taken steps to that effect, then this func
6216  * can be used to determine whether the ill has become quiescent
6217  */
6218 static boolean_t
6219 ill_is_quiescent(ill_t *ill)
6220 {
6221 	ipif_t	*ipif;
6222 
6223 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6224 
6225 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6226 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6227 			return (B_FALSE);
6228 		}
6229 	}
6230 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6231 		return (B_FALSE);
6232 	}
6233 	return (B_TRUE);
6234 }
6235 
6236 boolean_t
6237 ill_is_freeable(ill_t *ill)
6238 {
6239 	ipif_t	*ipif;
6240 
6241 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6242 
6243 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6244 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6245 			return (B_FALSE);
6246 		}
6247 	}
6248 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6249 		return (B_FALSE);
6250 	}
6251 	return (B_TRUE);
6252 }
6253 
6254 /*
6255  * This func does not prevent refcnt from increasing. But if
6256  * the caller has taken steps to that effect, then this func
6257  * can be used to determine whether the ipif has become quiescent
6258  */
6259 static boolean_t
6260 ipif_is_quiescent(ipif_t *ipif)
6261 {
6262 	ill_t *ill;
6263 
6264 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6265 
6266 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6267 		return (B_FALSE);
6268 	}
6269 
6270 	ill = ipif->ipif_ill;
6271 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6272 	    ill->ill_logical_down) {
6273 		return (B_TRUE);
6274 	}
6275 
6276 	/* This is the last ipif going down or being deleted on this ill */
6277 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6278 		return (B_FALSE);
6279 	}
6280 
6281 	return (B_TRUE);
6282 }
6283 
6284 /*
6285  * return true if the ipif can be destroyed: the ipif has to be quiescent
6286  * with zero references from ire/nce/ilm to it.
6287  */
6288 static boolean_t
6289 ipif_is_freeable(ipif_t *ipif)
6290 {
6291 
6292 	ill_t *ill;
6293 
6294 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6295 
6296 	if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6297 		return (B_FALSE);
6298 	}
6299 
6300 	ill = ipif->ipif_ill;
6301 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6302 	    ill->ill_logical_down) {
6303 		return (B_TRUE);
6304 	}
6305 
6306 	/* This is the last ipif going down or being deleted on this ill */
6307 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6308 		return (B_FALSE);
6309 	}
6310 
6311 	return (B_TRUE);
6312 }
6313 
6314 /*
6315  * This func does not prevent refcnt from increasing. But if
6316  * the caller has taken steps to that effect, then this func
6317  * can be used to determine whether the ipifs marked with IPIF_MOVING
6318  * have become quiescent and can be moved in a failover/failback.
6319  */
6320 static ipif_t *
6321 ill_quiescent_to_move(ill_t *ill)
6322 {
6323 	ipif_t  *ipif;
6324 
6325 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6326 
6327 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6328 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6329 			if (ipif->ipif_refcnt != 0 ||
6330 			    !IPIF_DOWN_OK(ipif)) {
6331 				return (ipif);
6332 			}
6333 		}
6334 	}
6335 	return (NULL);
6336 }
6337 
6338 /*
6339  * The ipif/ill/ire has been refreled. Do the tail processing.
6340  * Determine if the ipif or ill in question has become quiescent and if so
6341  * wakeup close and/or restart any queued pending ioctl that is waiting
6342  * for the ipif_down (or ill_down)
6343  */
6344 void
6345 ipif_ill_refrele_tail(ill_t *ill)
6346 {
6347 	mblk_t	*mp;
6348 	conn_t	*connp;
6349 	ipsq_t	*ipsq;
6350 	ipif_t	*ipif;
6351 	dl_notify_ind_t *dlindp;
6352 
6353 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6354 
6355 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6356 	    ill_is_freeable(ill)) {
6357 		/* ill_close may be waiting */
6358 		cv_broadcast(&ill->ill_cv);
6359 	}
6360 
6361 	/* ipsq can't change because ill_lock  is held */
6362 	ipsq = ill->ill_phyint->phyint_ipsq;
6363 	if (ipsq->ipsq_waitfor == 0) {
6364 		/* Not waiting for anything, just return. */
6365 		mutex_exit(&ill->ill_lock);
6366 		return;
6367 	}
6368 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6369 	    ipsq->ipsq_pending_ipif != NULL);
6370 	/*
6371 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6372 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6373 	 * be zero for restarting an ioctl that ends up downing the ill.
6374 	 */
6375 	ipif = ipsq->ipsq_pending_ipif;
6376 	if (ipif->ipif_ill != ill) {
6377 		/* The ioctl is pending on some other ill. */
6378 		mutex_exit(&ill->ill_lock);
6379 		return;
6380 	}
6381 
6382 	switch (ipsq->ipsq_waitfor) {
6383 	case IPIF_DOWN:
6384 		if (!ipif_is_quiescent(ipif)) {
6385 			mutex_exit(&ill->ill_lock);
6386 			return;
6387 		}
6388 		break;
6389 	case IPIF_FREE:
6390 		if (!ipif_is_freeable(ipif)) {
6391 			mutex_exit(&ill->ill_lock);
6392 			return;
6393 		}
6394 		break;
6395 
6396 	case ILL_DOWN:
6397 		if (!ill_is_quiescent(ill)) {
6398 			mutex_exit(&ill->ill_lock);
6399 			return;
6400 		}
6401 		break;
6402 	case ILL_FREE:
6403 		/*
6404 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6405 		 * waits synchronously in ip_close, and no message is queued in
6406 		 * ipsq_pending_mp at all in this case
6407 		 */
6408 		if (!ill_is_freeable(ill)) {
6409 			mutex_exit(&ill->ill_lock);
6410 			return;
6411 		}
6412 		break;
6413 
6414 	case ILL_MOVE_OK:
6415 		if (ill_quiescent_to_move(ill) != NULL) {
6416 			mutex_exit(&ill->ill_lock);
6417 			return;
6418 		}
6419 		break;
6420 	default:
6421 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6422 		    (void *)ipsq, ipsq->ipsq_waitfor);
6423 	}
6424 
6425 	/*
6426 	 * Incr refcnt for the qwriter_ip call below which
6427 	 * does a refrele
6428 	 */
6429 	ill_refhold_locked(ill);
6430 	mp = ipsq_pending_mp_get(ipsq, &connp);
6431 	mutex_exit(&ill->ill_lock);
6432 
6433 	ASSERT(mp != NULL);
6434 	/*
6435 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6436 	 * we can only get here when the current operation decides it
6437 	 * it needs to quiesce via ipsq_pending_mp_add().
6438 	 */
6439 	switch (mp->b_datap->db_type) {
6440 	case M_PCPROTO:
6441 	case M_PROTO:
6442 		/*
6443 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6444 		 */
6445 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6446 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6447 
6448 		switch (dlindp->dl_notification) {
6449 		case DL_NOTE_PHYS_ADDR:
6450 			qwriter_ip(ill, ill->ill_rq, mp,
6451 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6452 			return;
6453 		default:
6454 			ASSERT(0);
6455 		}
6456 		break;
6457 
6458 	case M_ERROR:
6459 	case M_HANGUP:
6460 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6461 		    B_TRUE);
6462 		return;
6463 
6464 	case M_IOCTL:
6465 	case M_IOCDATA:
6466 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6467 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6468 		return;
6469 
6470 	default:
6471 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6472 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6473 	}
6474 }
6475 
6476 #ifdef DEBUG
6477 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6478 static void
6479 th_trace_rrecord(th_trace_t *th_trace)
6480 {
6481 	tr_buf_t *tr_buf;
6482 	uint_t lastref;
6483 
6484 	lastref = th_trace->th_trace_lastref;
6485 	lastref++;
6486 	if (lastref == TR_BUF_MAX)
6487 		lastref = 0;
6488 	th_trace->th_trace_lastref = lastref;
6489 	tr_buf = &th_trace->th_trbuf[lastref];
6490 	tr_buf->tr_time = lbolt;
6491 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6492 }
6493 
6494 static void
6495 th_trace_free(void *value)
6496 {
6497 	th_trace_t *th_trace = value;
6498 
6499 	ASSERT(th_trace->th_refcnt == 0);
6500 	kmem_free(th_trace, sizeof (*th_trace));
6501 }
6502 
6503 /*
6504  * Find or create the per-thread hash table used to track object references.
6505  * The ipst argument is NULL if we shouldn't allocate.
6506  *
6507  * Accesses per-thread data, so there's no need to lock here.
6508  */
6509 static mod_hash_t *
6510 th_trace_gethash(ip_stack_t *ipst)
6511 {
6512 	th_hash_t *thh;
6513 
6514 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6515 		mod_hash_t *mh;
6516 		char name[256];
6517 		size_t objsize, rshift;
6518 		int retv;
6519 
6520 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6521 			return (NULL);
6522 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6523 
6524 		/*
6525 		 * We use mod_hash_create_extended here rather than the more
6526 		 * obvious mod_hash_create_ptrhash because the latter has a
6527 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6528 		 * block.
6529 		 */
6530 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6531 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6532 		rshift = highbit(objsize);
6533 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6534 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6535 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6536 		if (mh == NULL) {
6537 			kmem_free(thh, sizeof (*thh));
6538 			return (NULL);
6539 		}
6540 		thh->thh_hash = mh;
6541 		thh->thh_ipst = ipst;
6542 		/*
6543 		 * We trace ills, ipifs, ires, and nces.  All of these are
6544 		 * per-IP-stack, so the lock on the thread list is as well.
6545 		 */
6546 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6547 		list_insert_tail(&ip_thread_list, thh);
6548 		rw_exit(&ip_thread_rwlock);
6549 		retv = tsd_set(ip_thread_data, thh);
6550 		ASSERT(retv == 0);
6551 	}
6552 	return (thh != NULL ? thh->thh_hash : NULL);
6553 }
6554 
6555 boolean_t
6556 th_trace_ref(const void *obj, ip_stack_t *ipst)
6557 {
6558 	th_trace_t *th_trace;
6559 	mod_hash_t *mh;
6560 	mod_hash_val_t val;
6561 
6562 	if ((mh = th_trace_gethash(ipst)) == NULL)
6563 		return (B_FALSE);
6564 
6565 	/*
6566 	 * Attempt to locate the trace buffer for this obj and thread.
6567 	 * If it does not exist, then allocate a new trace buffer and
6568 	 * insert into the hash.
6569 	 */
6570 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6571 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6572 		if (th_trace == NULL)
6573 			return (B_FALSE);
6574 
6575 		th_trace->th_id = curthread;
6576 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6577 		    (mod_hash_val_t)th_trace) != 0) {
6578 			kmem_free(th_trace, sizeof (th_trace_t));
6579 			return (B_FALSE);
6580 		}
6581 	} else {
6582 		th_trace = (th_trace_t *)val;
6583 	}
6584 
6585 	ASSERT(th_trace->th_refcnt >= 0 &&
6586 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6587 
6588 	th_trace->th_refcnt++;
6589 	th_trace_rrecord(th_trace);
6590 	return (B_TRUE);
6591 }
6592 
6593 /*
6594  * For the purpose of tracing a reference release, we assume that global
6595  * tracing is always on and that the same thread initiated the reference hold
6596  * is releasing.
6597  */
6598 void
6599 th_trace_unref(const void *obj)
6600 {
6601 	int retv;
6602 	mod_hash_t *mh;
6603 	th_trace_t *th_trace;
6604 	mod_hash_val_t val;
6605 
6606 	mh = th_trace_gethash(NULL);
6607 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6608 	ASSERT(retv == 0);
6609 	th_trace = (th_trace_t *)val;
6610 
6611 	ASSERT(th_trace->th_refcnt > 0);
6612 	th_trace->th_refcnt--;
6613 	th_trace_rrecord(th_trace);
6614 }
6615 
6616 /*
6617  * If tracing has been disabled, then we assume that the reference counts are
6618  * now useless, and we clear them out before destroying the entries.
6619  */
6620 void
6621 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6622 {
6623 	th_hash_t	*thh;
6624 	mod_hash_t	*mh;
6625 	mod_hash_val_t	val;
6626 	th_trace_t	*th_trace;
6627 	int		retv;
6628 
6629 	rw_enter(&ip_thread_rwlock, RW_READER);
6630 	for (thh = list_head(&ip_thread_list); thh != NULL;
6631 	    thh = list_next(&ip_thread_list, thh)) {
6632 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6633 		    &val) == 0) {
6634 			th_trace = (th_trace_t *)val;
6635 			if (trace_disable)
6636 				th_trace->th_refcnt = 0;
6637 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6638 			ASSERT(retv == 0);
6639 		}
6640 	}
6641 	rw_exit(&ip_thread_rwlock);
6642 }
6643 
6644 void
6645 ipif_trace_ref(ipif_t *ipif)
6646 {
6647 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6648 
6649 	if (ipif->ipif_trace_disable)
6650 		return;
6651 
6652 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6653 		ipif->ipif_trace_disable = B_TRUE;
6654 		ipif_trace_cleanup(ipif);
6655 	}
6656 }
6657 
6658 void
6659 ipif_untrace_ref(ipif_t *ipif)
6660 {
6661 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6662 
6663 	if (!ipif->ipif_trace_disable)
6664 		th_trace_unref(ipif);
6665 }
6666 
6667 void
6668 ill_trace_ref(ill_t *ill)
6669 {
6670 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6671 
6672 	if (ill->ill_trace_disable)
6673 		return;
6674 
6675 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6676 		ill->ill_trace_disable = B_TRUE;
6677 		ill_trace_cleanup(ill);
6678 	}
6679 }
6680 
6681 void
6682 ill_untrace_ref(ill_t *ill)
6683 {
6684 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6685 
6686 	if (!ill->ill_trace_disable)
6687 		th_trace_unref(ill);
6688 }
6689 
6690 /*
6691  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6692  * failure, ipif_trace_disable is set.
6693  */
6694 static void
6695 ipif_trace_cleanup(const ipif_t *ipif)
6696 {
6697 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6698 }
6699 
6700 /*
6701  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6702  * failure, ill_trace_disable is set.
6703  */
6704 static void
6705 ill_trace_cleanup(const ill_t *ill)
6706 {
6707 	th_trace_cleanup(ill, ill->ill_trace_disable);
6708 }
6709 #endif /* DEBUG */
6710 
6711 void
6712 ipif_refhold_locked(ipif_t *ipif)
6713 {
6714 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6715 	ipif->ipif_refcnt++;
6716 	IPIF_TRACE_REF(ipif);
6717 }
6718 
6719 void
6720 ipif_refhold(ipif_t *ipif)
6721 {
6722 	ill_t	*ill;
6723 
6724 	ill = ipif->ipif_ill;
6725 	mutex_enter(&ill->ill_lock);
6726 	ipif->ipif_refcnt++;
6727 	IPIF_TRACE_REF(ipif);
6728 	mutex_exit(&ill->ill_lock);
6729 }
6730 
6731 /*
6732  * Must not be called while holding any locks. Otherwise if this is
6733  * the last reference to be released there is a chance of recursive mutex
6734  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6735  * to restart an ioctl.
6736  */
6737 void
6738 ipif_refrele(ipif_t *ipif)
6739 {
6740 	ill_t	*ill;
6741 
6742 	ill = ipif->ipif_ill;
6743 
6744 	mutex_enter(&ill->ill_lock);
6745 	ASSERT(ipif->ipif_refcnt != 0);
6746 	ipif->ipif_refcnt--;
6747 	IPIF_UNTRACE_REF(ipif);
6748 	if (ipif->ipif_refcnt != 0) {
6749 		mutex_exit(&ill->ill_lock);
6750 		return;
6751 	}
6752 
6753 	/* Drops the ill_lock */
6754 	ipif_ill_refrele_tail(ill);
6755 }
6756 
6757 ipif_t *
6758 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6759 {
6760 	ipif_t	*ipif;
6761 
6762 	mutex_enter(&ill->ill_lock);
6763 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6764 	    ipif != NULL; ipif = ipif->ipif_next) {
6765 		if (!IPIF_CAN_LOOKUP(ipif))
6766 			continue;
6767 		ipif_refhold_locked(ipif);
6768 		mutex_exit(&ill->ill_lock);
6769 		return (ipif);
6770 	}
6771 	mutex_exit(&ill->ill_lock);
6772 	return (NULL);
6773 }
6774 
6775 /*
6776  * TODO: make this table extendible at run time
6777  * Return a pointer to the mac type info for 'mac_type'
6778  */
6779 static ip_m_t *
6780 ip_m_lookup(t_uscalar_t mac_type)
6781 {
6782 	ip_m_t	*ipm;
6783 
6784 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6785 		if (ipm->ip_m_mac_type == mac_type)
6786 			return (ipm);
6787 	return (NULL);
6788 }
6789 
6790 /*
6791  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6792  * ipif_arg is passed in to associate it with the correct interface.
6793  * We may need to restart this operation if the ipif cannot be looked up
6794  * due to an exclusive operation that is currently in progress. The restart
6795  * entry point is specified by 'func'
6796  */
6797 int
6798 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6799     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6800     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6801     struct rtsa_s *sp, ip_stack_t *ipst)
6802 {
6803 	ire_t	*ire;
6804 	ire_t	*gw_ire = NULL;
6805 	ipif_t	*ipif = NULL;
6806 	boolean_t ipif_refheld = B_FALSE;
6807 	uint_t	type;
6808 	int	match_flags = MATCH_IRE_TYPE;
6809 	int	error;
6810 	tsol_gc_t *gc = NULL;
6811 	tsol_gcgrp_t *gcgrp = NULL;
6812 	boolean_t gcgrp_xtraref = B_FALSE;
6813 
6814 	ip1dbg(("ip_rt_add:"));
6815 
6816 	if (ire_arg != NULL)
6817 		*ire_arg = NULL;
6818 
6819 	/*
6820 	 * If this is the case of RTF_HOST being set, then we set the netmask
6821 	 * to all ones (regardless if one was supplied).
6822 	 */
6823 	if (flags & RTF_HOST)
6824 		mask = IP_HOST_MASK;
6825 
6826 	/*
6827 	 * Prevent routes with a zero gateway from being created (since
6828 	 * interfaces can currently be plumbed and brought up no assigned
6829 	 * address).
6830 	 */
6831 	if (gw_addr == 0)
6832 		return (ENETUNREACH);
6833 	/*
6834 	 * Get the ipif, if any, corresponding to the gw_addr
6835 	 */
6836 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6837 	    ipst);
6838 	if (ipif != NULL) {
6839 		if (IS_VNI(ipif->ipif_ill)) {
6840 			ipif_refrele(ipif);
6841 			return (EINVAL);
6842 		}
6843 		ipif_refheld = B_TRUE;
6844 	} else if (error == EINPROGRESS) {
6845 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6846 		return (EINPROGRESS);
6847 	} else {
6848 		error = 0;
6849 	}
6850 
6851 	if (ipif != NULL) {
6852 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6853 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6854 	} else {
6855 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6856 	}
6857 
6858 	/*
6859 	 * GateD will attempt to create routes with a loopback interface
6860 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6861 	 * these routes to be added, but create them as interface routes
6862 	 * since the gateway is an interface address.
6863 	 */
6864 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6865 		flags &= ~RTF_GATEWAY;
6866 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6867 		    mask == IP_HOST_MASK) {
6868 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6869 			    ALL_ZONES, NULL, match_flags, ipst);
6870 			if (ire != NULL) {
6871 				ire_refrele(ire);
6872 				if (ipif_refheld)
6873 					ipif_refrele(ipif);
6874 				return (EEXIST);
6875 			}
6876 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6877 			    "for 0x%x\n", (void *)ipif,
6878 			    ipif->ipif_ire_type,
6879 			    ntohl(ipif->ipif_lcl_addr)));
6880 			ire = ire_create(
6881 			    (uchar_t *)&dst_addr,	/* dest address */
6882 			    (uchar_t *)&mask,		/* mask */
6883 			    (uchar_t *)&ipif->ipif_src_addr,
6884 			    NULL,			/* no gateway */
6885 			    &ipif->ipif_mtu,
6886 			    NULL,
6887 			    ipif->ipif_rq,		/* recv-from queue */
6888 			    NULL,			/* no send-to queue */
6889 			    ipif->ipif_ire_type,	/* LOOPBACK */
6890 			    ipif,
6891 			    0,
6892 			    0,
6893 			    0,
6894 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6895 			    RTF_PRIVATE : 0,
6896 			    &ire_uinfo_null,
6897 			    NULL,
6898 			    NULL,
6899 			    ipst);
6900 
6901 			if (ire == NULL) {
6902 				if (ipif_refheld)
6903 					ipif_refrele(ipif);
6904 				return (ENOMEM);
6905 			}
6906 			error = ire_add(&ire, q, mp, func, B_FALSE);
6907 			if (error == 0)
6908 				goto save_ire;
6909 			if (ipif_refheld)
6910 				ipif_refrele(ipif);
6911 			return (error);
6912 
6913 		}
6914 	}
6915 
6916 	/*
6917 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6918 	 * and the gateway address provided is one of the system's interface
6919 	 * addresses.  By using the routing socket interface and supplying an
6920 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6921 	 * specifying an interface route to be created is available which uses
6922 	 * the interface index that specifies the outgoing interface rather than
6923 	 * the address of an outgoing interface (which may not be able to
6924 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6925 	 * flag, routes can be specified which not only specify the next-hop to
6926 	 * be used when routing to a certain prefix, but also which outgoing
6927 	 * interface should be used.
6928 	 *
6929 	 * Previously, interfaces would have unique addresses assigned to them
6930 	 * and so the address assigned to a particular interface could be used
6931 	 * to identify a particular interface.  One exception to this was the
6932 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6933 	 *
6934 	 * With the advent of IPv6 and its link-local addresses, this
6935 	 * restriction was relaxed and interfaces could share addresses between
6936 	 * themselves.  In fact, typically all of the link-local interfaces on
6937 	 * an IPv6 node or router will have the same link-local address.  In
6938 	 * order to differentiate between these interfaces, the use of an
6939 	 * interface index is necessary and this index can be carried inside a
6940 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6941 	 * of using the interface index, however, is that all of the ipif's that
6942 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6943 	 * cannot be used to differentiate between ipif's (or logical
6944 	 * interfaces) that belong to the same ill (physical interface).
6945 	 *
6946 	 * For example, in the following case involving IPv4 interfaces and
6947 	 * logical interfaces
6948 	 *
6949 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6950 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6951 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6952 	 *
6953 	 * the ipif's corresponding to each of these interface routes can be
6954 	 * uniquely identified by the "gateway" (actually interface address).
6955 	 *
6956 	 * In this case involving multiple IPv6 default routes to a particular
6957 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6958 	 * default route is of interest:
6959 	 *
6960 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6961 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6962 	 */
6963 
6964 	/* RTF_GATEWAY not set */
6965 	if (!(flags & RTF_GATEWAY)) {
6966 		queue_t	*stq;
6967 
6968 		if (sp != NULL) {
6969 			ip2dbg(("ip_rt_add: gateway security attributes "
6970 			    "cannot be set with interface route\n"));
6971 			if (ipif_refheld)
6972 				ipif_refrele(ipif);
6973 			return (EINVAL);
6974 		}
6975 
6976 		/*
6977 		 * As the interface index specified with the RTA_IFP sockaddr is
6978 		 * the same for all ipif's off of an ill, the matching logic
6979 		 * below uses MATCH_IRE_ILL if such an index was specified.
6980 		 * This means that routes sharing the same prefix when added
6981 		 * using a RTA_IFP sockaddr must have distinct interface
6982 		 * indices (namely, they must be on distinct ill's).
6983 		 *
6984 		 * On the other hand, since the gateway address will usually be
6985 		 * different for each ipif on the system, the matching logic
6986 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6987 		 * route.  This means that interface routes for the same prefix
6988 		 * can be created if they belong to distinct ipif's and if a
6989 		 * RTA_IFP sockaddr is not present.
6990 		 */
6991 		if (ipif_arg != NULL) {
6992 			if (ipif_refheld)  {
6993 				ipif_refrele(ipif);
6994 				ipif_refheld = B_FALSE;
6995 			}
6996 			ipif = ipif_arg;
6997 			match_flags |= MATCH_IRE_ILL;
6998 		} else {
6999 			/*
7000 			 * Check the ipif corresponding to the gw_addr
7001 			 */
7002 			if (ipif == NULL)
7003 				return (ENETUNREACH);
7004 			match_flags |= MATCH_IRE_IPIF;
7005 		}
7006 		ASSERT(ipif != NULL);
7007 
7008 		/*
7009 		 * We check for an existing entry at this point.
7010 		 *
7011 		 * Since a netmask isn't passed in via the ioctl interface
7012 		 * (SIOCADDRT), we don't check for a matching netmask in that
7013 		 * case.
7014 		 */
7015 		if (!ioctl_msg)
7016 			match_flags |= MATCH_IRE_MASK;
7017 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7018 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7019 		if (ire != NULL) {
7020 			ire_refrele(ire);
7021 			if (ipif_refheld)
7022 				ipif_refrele(ipif);
7023 			return (EEXIST);
7024 		}
7025 
7026 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7027 		    ? ipif->ipif_rq : ipif->ipif_wq;
7028 
7029 		/*
7030 		 * Create a copy of the IRE_LOOPBACK,
7031 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7032 		 * the modified address and netmask.
7033 		 */
7034 		ire = ire_create(
7035 		    (uchar_t *)&dst_addr,
7036 		    (uint8_t *)&mask,
7037 		    (uint8_t *)&ipif->ipif_src_addr,
7038 		    NULL,
7039 		    &ipif->ipif_mtu,
7040 		    NULL,
7041 		    NULL,
7042 		    stq,
7043 		    ipif->ipif_net_type,
7044 		    ipif,
7045 		    0,
7046 		    0,
7047 		    0,
7048 		    flags,
7049 		    &ire_uinfo_null,
7050 		    NULL,
7051 		    NULL,
7052 		    ipst);
7053 		if (ire == NULL) {
7054 			if (ipif_refheld)
7055 				ipif_refrele(ipif);
7056 			return (ENOMEM);
7057 		}
7058 
7059 		/*
7060 		 * Some software (for example, GateD and Sun Cluster) attempts
7061 		 * to create (what amount to) IRE_PREFIX routes with the
7062 		 * loopback address as the gateway.  This is primarily done to
7063 		 * set up prefixes with the RTF_REJECT flag set (for example,
7064 		 * when generating aggregate routes.)
7065 		 *
7066 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7067 		 * IRE_LOOPBACK, then we map the request into a
7068 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7069 		 * these interface routes, by definition, can only be that.
7070 		 *
7071 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7072 		 * routine, but rather using ire_create() directly.
7073 		 *
7074 		 */
7075 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7076 			ire->ire_type = IRE_IF_NORESOLVER;
7077 			ire->ire_flags |= RTF_BLACKHOLE;
7078 		}
7079 
7080 		error = ire_add(&ire, q, mp, func, B_FALSE);
7081 		if (error == 0)
7082 			goto save_ire;
7083 
7084 		/*
7085 		 * In the result of failure, ire_add() will have already
7086 		 * deleted the ire in question, so there is no need to
7087 		 * do that here.
7088 		 */
7089 		if (ipif_refheld)
7090 			ipif_refrele(ipif);
7091 		return (error);
7092 	}
7093 	if (ipif_refheld) {
7094 		ipif_refrele(ipif);
7095 		ipif_refheld = B_FALSE;
7096 	}
7097 
7098 	/*
7099 	 * Get an interface IRE for the specified gateway.
7100 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7101 	 * gateway, it is currently unreachable and we fail the request
7102 	 * accordingly.
7103 	 */
7104 	ipif = ipif_arg;
7105 	if (ipif_arg != NULL)
7106 		match_flags |= MATCH_IRE_ILL;
7107 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7108 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7109 	if (gw_ire == NULL)
7110 		return (ENETUNREACH);
7111 
7112 	/*
7113 	 * We create one of three types of IREs as a result of this request
7114 	 * based on the netmask.  A netmask of all ones (which is automatically
7115 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7116 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7117 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7118 	 * destination prefix.
7119 	 */
7120 	if (mask == IP_HOST_MASK)
7121 		type = IRE_HOST;
7122 	else if (mask == 0)
7123 		type = IRE_DEFAULT;
7124 	else
7125 		type = IRE_PREFIX;
7126 
7127 	/* check for a duplicate entry */
7128 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7129 	    NULL, ALL_ZONES, 0, NULL,
7130 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7131 	if (ire != NULL) {
7132 		ire_refrele(gw_ire);
7133 		ire_refrele(ire);
7134 		return (EEXIST);
7135 	}
7136 
7137 	/* Security attribute exists */
7138 	if (sp != NULL) {
7139 		tsol_gcgrp_addr_t ga;
7140 
7141 		/* find or create the gateway credentials group */
7142 		ga.ga_af = AF_INET;
7143 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7144 
7145 		/* we hold reference to it upon success */
7146 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7147 		if (gcgrp == NULL) {
7148 			ire_refrele(gw_ire);
7149 			return (ENOMEM);
7150 		}
7151 
7152 		/*
7153 		 * Create and add the security attribute to the group; a
7154 		 * reference to the group is made upon allocating a new
7155 		 * entry successfully.  If it finds an already-existing
7156 		 * entry for the security attribute in the group, it simply
7157 		 * returns it and no new reference is made to the group.
7158 		 */
7159 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7160 		if (gc == NULL) {
7161 			/* release reference held by gcgrp_lookup */
7162 			GCGRP_REFRELE(gcgrp);
7163 			ire_refrele(gw_ire);
7164 			return (ENOMEM);
7165 		}
7166 	}
7167 
7168 	/* Create the IRE. */
7169 	ire = ire_create(
7170 	    (uchar_t *)&dst_addr,		/* dest address */
7171 	    (uchar_t *)&mask,			/* mask */
7172 	    /* src address assigned by the caller? */
7173 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7174 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7175 	    (uchar_t *)&gw_addr,		/* gateway address */
7176 	    &gw_ire->ire_max_frag,
7177 	    NULL,				/* no src nce */
7178 	    NULL,				/* no recv-from queue */
7179 	    NULL,				/* no send-to queue */
7180 	    (ushort_t)type,			/* IRE type */
7181 	    ipif_arg,
7182 	    0,
7183 	    0,
7184 	    0,
7185 	    flags,
7186 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7187 	    gc,					/* security attribute */
7188 	    NULL,
7189 	    ipst);
7190 
7191 	/*
7192 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7193 	 * reference to the 'gcgrp'. We can now release the extra reference
7194 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7195 	 */
7196 	if (gcgrp_xtraref)
7197 		GCGRP_REFRELE(gcgrp);
7198 	if (ire == NULL) {
7199 		if (gc != NULL)
7200 			GC_REFRELE(gc);
7201 		ire_refrele(gw_ire);
7202 		return (ENOMEM);
7203 	}
7204 
7205 	/*
7206 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7207 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7208 	 */
7209 
7210 	/* Add the new IRE. */
7211 	error = ire_add(&ire, q, mp, func, B_FALSE);
7212 	if (error != 0) {
7213 		/*
7214 		 * In the result of failure, ire_add() will have already
7215 		 * deleted the ire in question, so there is no need to
7216 		 * do that here.
7217 		 */
7218 		ire_refrele(gw_ire);
7219 		return (error);
7220 	}
7221 
7222 	if (flags & RTF_MULTIRT) {
7223 		/*
7224 		 * Invoke the CGTP (multirouting) filtering module
7225 		 * to add the dst address in the filtering database.
7226 		 * Replicated inbound packets coming from that address
7227 		 * will be filtered to discard the duplicates.
7228 		 * It is not necessary to call the CGTP filter hook
7229 		 * when the dst address is a broadcast or multicast,
7230 		 * because an IP source address cannot be a broadcast
7231 		 * or a multicast.
7232 		 */
7233 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7234 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7235 		if (ire_dst != NULL) {
7236 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7237 			ire_refrele(ire_dst);
7238 			goto save_ire;
7239 		}
7240 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7241 		    !CLASSD(ire->ire_addr)) {
7242 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7243 			    ipst->ips_netstack->netstack_stackid,
7244 			    ire->ire_addr,
7245 			    ire->ire_gateway_addr,
7246 			    ire->ire_src_addr,
7247 			    gw_ire->ire_src_addr);
7248 			if (res != 0) {
7249 				ire_refrele(gw_ire);
7250 				ire_delete(ire);
7251 				return (res);
7252 			}
7253 		}
7254 	}
7255 
7256 	/*
7257 	 * Now that the prefix IRE entry has been created, delete any
7258 	 * existing gateway IRE cache entries as well as any IRE caches
7259 	 * using the gateway, and force them to be created through
7260 	 * ip_newroute.
7261 	 */
7262 	if (gc != NULL) {
7263 		ASSERT(gcgrp != NULL);
7264 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7265 	}
7266 
7267 save_ire:
7268 	if (gw_ire != NULL) {
7269 		ire_refrele(gw_ire);
7270 	}
7271 	if (ipif != NULL) {
7272 		/*
7273 		 * Save enough information so that we can recreate the IRE if
7274 		 * the interface goes down and then up.  The metrics associated
7275 		 * with the route will be saved as well when rts_setmetrics() is
7276 		 * called after the IRE has been created.  In the case where
7277 		 * memory cannot be allocated, none of this information will be
7278 		 * saved.
7279 		 */
7280 		ipif_save_ire(ipif, ire);
7281 	}
7282 	if (ioctl_msg)
7283 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7284 	if (ire_arg != NULL) {
7285 		/*
7286 		 * Store the ire that was successfully added into where ire_arg
7287 		 * points to so that callers don't have to look it up
7288 		 * themselves (but they are responsible for ire_refrele()ing
7289 		 * the ire when they are finished with it).
7290 		 */
7291 		*ire_arg = ire;
7292 	} else {
7293 		ire_refrele(ire);		/* Held in ire_add */
7294 	}
7295 	if (ipif_refheld)
7296 		ipif_refrele(ipif);
7297 	return (0);
7298 }
7299 
7300 /*
7301  * ip_rt_delete is called to delete an IPv4 route.
7302  * ipif_arg is passed in to associate it with the correct interface.
7303  * We may need to restart this operation if the ipif cannot be looked up
7304  * due to an exclusive operation that is currently in progress. The restart
7305  * entry point is specified by 'func'
7306  */
7307 /* ARGSUSED4 */
7308 int
7309 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7310     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7311     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7312 {
7313 	ire_t	*ire = NULL;
7314 	ipif_t	*ipif;
7315 	boolean_t ipif_refheld = B_FALSE;
7316 	uint_t	type;
7317 	uint_t	match_flags = MATCH_IRE_TYPE;
7318 	int	err = 0;
7319 
7320 	ip1dbg(("ip_rt_delete:"));
7321 	/*
7322 	 * If this is the case of RTF_HOST being set, then we set the netmask
7323 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7324 	 */
7325 	if (flags & RTF_HOST) {
7326 		mask = IP_HOST_MASK;
7327 		match_flags |= MATCH_IRE_MASK;
7328 	} else if (rtm_addrs & RTA_NETMASK) {
7329 		match_flags |= MATCH_IRE_MASK;
7330 	}
7331 
7332 	/*
7333 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7334 	 * we check if the gateway address is one of our interfaces first,
7335 	 * and fall back on RTF_GATEWAY routes.
7336 	 *
7337 	 * This makes it possible to delete an original
7338 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7339 	 *
7340 	 * As the interface index specified with the RTA_IFP sockaddr is the
7341 	 * same for all ipif's off of an ill, the matching logic below uses
7342 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7343 	 * sharing the same prefix and interface index as the the route
7344 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7345 	 * is specified in the request.
7346 	 *
7347 	 * On the other hand, since the gateway address will usually be
7348 	 * different for each ipif on the system, the matching logic
7349 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7350 	 * route.  This means that interface routes for the same prefix can be
7351 	 * uniquely identified if they belong to distinct ipif's and if a
7352 	 * RTA_IFP sockaddr is not present.
7353 	 *
7354 	 * For more detail on specifying routes by gateway address and by
7355 	 * interface index, see the comments in ip_rt_add().
7356 	 */
7357 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7358 	    ipst);
7359 	if (ipif != NULL)
7360 		ipif_refheld = B_TRUE;
7361 	else if (err == EINPROGRESS)
7362 		return (err);
7363 	else
7364 		err = 0;
7365 	if (ipif != NULL) {
7366 		if (ipif_arg != NULL) {
7367 			if (ipif_refheld) {
7368 				ipif_refrele(ipif);
7369 				ipif_refheld = B_FALSE;
7370 			}
7371 			ipif = ipif_arg;
7372 			match_flags |= MATCH_IRE_ILL;
7373 		} else {
7374 			match_flags |= MATCH_IRE_IPIF;
7375 		}
7376 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7377 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7378 			    ALL_ZONES, NULL, match_flags, ipst);
7379 		}
7380 		if (ire == NULL) {
7381 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7382 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7383 			    match_flags, ipst);
7384 		}
7385 	}
7386 
7387 	if (ire == NULL) {
7388 		/*
7389 		 * At this point, the gateway address is not one of our own
7390 		 * addresses or a matching interface route was not found.  We
7391 		 * set the IRE type to lookup based on whether
7392 		 * this is a host route, a default route or just a prefix.
7393 		 *
7394 		 * If an ipif_arg was passed in, then the lookup is based on an
7395 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7396 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7397 		 * set as the route being looked up is not a traditional
7398 		 * interface route.
7399 		 */
7400 		match_flags &= ~MATCH_IRE_IPIF;
7401 		match_flags |= MATCH_IRE_GW;
7402 		if (ipif_arg != NULL)
7403 			match_flags |= MATCH_IRE_ILL;
7404 		if (mask == IP_HOST_MASK)
7405 			type = IRE_HOST;
7406 		else if (mask == 0)
7407 			type = IRE_DEFAULT;
7408 		else
7409 			type = IRE_PREFIX;
7410 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7411 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7412 	}
7413 
7414 	if (ipif_refheld)
7415 		ipif_refrele(ipif);
7416 
7417 	/* ipif is not refheld anymore */
7418 	if (ire == NULL)
7419 		return (ESRCH);
7420 
7421 	if (ire->ire_flags & RTF_MULTIRT) {
7422 		/*
7423 		 * Invoke the CGTP (multirouting) filtering module
7424 		 * to remove the dst address from the filtering database.
7425 		 * Packets coming from that address will no longer be
7426 		 * filtered to remove duplicates.
7427 		 */
7428 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7429 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7430 			    ipst->ips_netstack->netstack_stackid,
7431 			    ire->ire_addr, ire->ire_gateway_addr);
7432 		}
7433 		ip_cgtp_bcast_delete(ire, ipst);
7434 	}
7435 
7436 	ipif = ire->ire_ipif;
7437 	if (ipif != NULL)
7438 		ipif_remove_ire(ipif, ire);
7439 	if (ioctl_msg)
7440 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7441 	ire_delete(ire);
7442 	ire_refrele(ire);
7443 	return (err);
7444 }
7445 
7446 /*
7447  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7448  */
7449 /* ARGSUSED */
7450 int
7451 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7452     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7453 {
7454 	ipaddr_t dst_addr;
7455 	ipaddr_t gw_addr;
7456 	ipaddr_t mask;
7457 	int error = 0;
7458 	mblk_t *mp1;
7459 	struct rtentry *rt;
7460 	ipif_t *ipif = NULL;
7461 	ip_stack_t	*ipst;
7462 
7463 	ASSERT(q->q_next == NULL);
7464 	ipst = CONNQ_TO_IPST(q);
7465 
7466 	ip1dbg(("ip_siocaddrt:"));
7467 	/* Existence of mp1 verified in ip_wput_nondata */
7468 	mp1 = mp->b_cont->b_cont;
7469 	rt = (struct rtentry *)mp1->b_rptr;
7470 
7471 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7472 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7473 
7474 	/*
7475 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7476 	 * to a particular host address.  In this case, we set the netmask to
7477 	 * all ones for the particular destination address.  Otherwise,
7478 	 * determine the netmask to be used based on dst_addr and the interfaces
7479 	 * in use.
7480 	 */
7481 	if (rt->rt_flags & RTF_HOST) {
7482 		mask = IP_HOST_MASK;
7483 	} else {
7484 		/*
7485 		 * Note that ip_subnet_mask returns a zero mask in the case of
7486 		 * default (an all-zeroes address).
7487 		 */
7488 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7489 	}
7490 
7491 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7492 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7493 	if (ipif != NULL)
7494 		ipif_refrele(ipif);
7495 	return (error);
7496 }
7497 
7498 /*
7499  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7500  */
7501 /* ARGSUSED */
7502 int
7503 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7504     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7505 {
7506 	ipaddr_t dst_addr;
7507 	ipaddr_t gw_addr;
7508 	ipaddr_t mask;
7509 	int error;
7510 	mblk_t *mp1;
7511 	struct rtentry *rt;
7512 	ipif_t *ipif = NULL;
7513 	ip_stack_t	*ipst;
7514 
7515 	ASSERT(q->q_next == NULL);
7516 	ipst = CONNQ_TO_IPST(q);
7517 
7518 	ip1dbg(("ip_siocdelrt:"));
7519 	/* Existence of mp1 verified in ip_wput_nondata */
7520 	mp1 = mp->b_cont->b_cont;
7521 	rt = (struct rtentry *)mp1->b_rptr;
7522 
7523 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7524 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7525 
7526 	/*
7527 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7528 	 * to a particular host address.  In this case, we set the netmask to
7529 	 * all ones for the particular destination address.  Otherwise,
7530 	 * determine the netmask to be used based on dst_addr and the interfaces
7531 	 * in use.
7532 	 */
7533 	if (rt->rt_flags & RTF_HOST) {
7534 		mask = IP_HOST_MASK;
7535 	} else {
7536 		/*
7537 		 * Note that ip_subnet_mask returns a zero mask in the case of
7538 		 * default (an all-zeroes address).
7539 		 */
7540 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7541 	}
7542 
7543 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7544 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7545 	    mp, ip_process_ioctl, ipst);
7546 	if (ipif != NULL)
7547 		ipif_refrele(ipif);
7548 	return (error);
7549 }
7550 
7551 /*
7552  * Enqueue the mp onto the ipsq, chained by b_next.
7553  * b_prev stores the function to be executed later, and b_queue the queue
7554  * where this mp originated.
7555  */
7556 void
7557 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7558     ill_t *pending_ill)
7559 {
7560 	conn_t	*connp = NULL;
7561 
7562 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7563 	ASSERT(func != NULL);
7564 
7565 	mp->b_queue = q;
7566 	mp->b_prev = (void *)func;
7567 	mp->b_next = NULL;
7568 
7569 	switch (type) {
7570 	case CUR_OP:
7571 		if (ipsq->ipsq_mptail != NULL) {
7572 			ASSERT(ipsq->ipsq_mphead != NULL);
7573 			ipsq->ipsq_mptail->b_next = mp;
7574 		} else {
7575 			ASSERT(ipsq->ipsq_mphead == NULL);
7576 			ipsq->ipsq_mphead = mp;
7577 		}
7578 		ipsq->ipsq_mptail = mp;
7579 		break;
7580 
7581 	case NEW_OP:
7582 		if (ipsq->ipsq_xopq_mptail != NULL) {
7583 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7584 			ipsq->ipsq_xopq_mptail->b_next = mp;
7585 		} else {
7586 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7587 			ipsq->ipsq_xopq_mphead = mp;
7588 		}
7589 		ipsq->ipsq_xopq_mptail = mp;
7590 		break;
7591 	default:
7592 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7593 	}
7594 
7595 	if (CONN_Q(q) && pending_ill != NULL) {
7596 		connp = Q_TO_CONN(q);
7597 
7598 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7599 		connp->conn_oper_pending_ill = pending_ill;
7600 	}
7601 }
7602 
7603 /*
7604  * Return the mp at the head of the ipsq. After emptying the ipsq
7605  * look at the next ioctl, if this ioctl is complete. Otherwise
7606  * return, we will resume when we complete the current ioctl.
7607  * The current ioctl will wait till it gets a response from the
7608  * driver below.
7609  */
7610 static mblk_t *
7611 ipsq_dq(ipsq_t *ipsq)
7612 {
7613 	mblk_t	*mp;
7614 
7615 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7616 
7617 	mp = ipsq->ipsq_mphead;
7618 	if (mp != NULL) {
7619 		ipsq->ipsq_mphead = mp->b_next;
7620 		if (ipsq->ipsq_mphead == NULL)
7621 			ipsq->ipsq_mptail = NULL;
7622 		mp->b_next = NULL;
7623 		return (mp);
7624 	}
7625 	if (ipsq->ipsq_current_ipif != NULL)
7626 		return (NULL);
7627 	mp = ipsq->ipsq_xopq_mphead;
7628 	if (mp != NULL) {
7629 		ipsq->ipsq_xopq_mphead = mp->b_next;
7630 		if (ipsq->ipsq_xopq_mphead == NULL)
7631 			ipsq->ipsq_xopq_mptail = NULL;
7632 		mp->b_next = NULL;
7633 		return (mp);
7634 	}
7635 	return (NULL);
7636 }
7637 
7638 /*
7639  * Enter the ipsq corresponding to ill, by waiting synchronously till
7640  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7641  * will have to drain completely before ipsq_enter returns success.
7642  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7643  * and the ipsq_exit logic will start the next enqueued ioctl after
7644  * completion of the current ioctl. If 'force' is used, we don't wait
7645  * for the enqueued ioctls. This is needed when a conn_close wants to
7646  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7647  * of an ill can also use this option. But we dont' use it currently.
7648  */
7649 #define	ENTER_SQ_WAIT_TICKS 100
7650 boolean_t
7651 ipsq_enter(ill_t *ill, boolean_t force)
7652 {
7653 	ipsq_t	*ipsq;
7654 	boolean_t waited_enough = B_FALSE;
7655 
7656 	/*
7657 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7658 	 * Since the <ill-ipsq> assocs could change while we wait for the
7659 	 * writer, it is easier to wait on a fixed global rather than try to
7660 	 * cv_wait on a changing ipsq.
7661 	 */
7662 	mutex_enter(&ill->ill_lock);
7663 	for (;;) {
7664 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7665 			mutex_exit(&ill->ill_lock);
7666 			return (B_FALSE);
7667 		}
7668 
7669 		ipsq = ill->ill_phyint->phyint_ipsq;
7670 		mutex_enter(&ipsq->ipsq_lock);
7671 		if (ipsq->ipsq_writer == NULL &&
7672 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7673 			break;
7674 		} else if (ipsq->ipsq_writer != NULL) {
7675 			mutex_exit(&ipsq->ipsq_lock);
7676 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7677 		} else {
7678 			mutex_exit(&ipsq->ipsq_lock);
7679 			if (force) {
7680 				(void) cv_timedwait(&ill->ill_cv,
7681 				    &ill->ill_lock,
7682 				    lbolt + ENTER_SQ_WAIT_TICKS);
7683 				waited_enough = B_TRUE;
7684 				continue;
7685 			} else {
7686 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7687 			}
7688 		}
7689 	}
7690 
7691 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7692 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7693 	ipsq->ipsq_writer = curthread;
7694 	ipsq->ipsq_reentry_cnt++;
7695 #ifdef DEBUG
7696 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7697 #endif
7698 	mutex_exit(&ipsq->ipsq_lock);
7699 	mutex_exit(&ill->ill_lock);
7700 	return (B_TRUE);
7701 }
7702 
7703 /*
7704  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7705  * certain critical operations like plumbing (i.e. most set ioctls),
7706  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7707  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7708  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7709  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7710  * threads executing in the ipsq. Responses from the driver pertain to the
7711  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7712  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7713  *
7714  * If a thread does not want to reenter the ipsq when it is already writer,
7715  * it must make sure that the specified reentry point to be called later
7716  * when the ipsq is empty, nor any code path starting from the specified reentry
7717  * point must never ever try to enter the ipsq again. Otherwise it can lead
7718  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7719  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7720  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7721  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7722  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7723  * ioctl if the current ioctl has completed. If the current ioctl is still
7724  * in progress it simply returns. The current ioctl could be waiting for
7725  * a response from another module (arp_ or the driver or could be waiting for
7726  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7727  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7728  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7729  * ipsq_current_ipif is clear which happens only on ioctl completion.
7730  */
7731 
7732 /*
7733  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7734  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7735  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7736  * completion.
7737  */
7738 ipsq_t *
7739 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7740     ipsq_func_t func, int type, boolean_t reentry_ok)
7741 {
7742 	ipsq_t	*ipsq;
7743 
7744 	/* Only 1 of ipif or ill can be specified */
7745 	ASSERT((ipif != NULL) ^ (ill != NULL));
7746 	if (ipif != NULL)
7747 		ill = ipif->ipif_ill;
7748 
7749 	/*
7750 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7751 	 * ipsq of an ill can't change when ill_lock is held.
7752 	 */
7753 	GRAB_CONN_LOCK(q);
7754 	mutex_enter(&ill->ill_lock);
7755 	ipsq = ill->ill_phyint->phyint_ipsq;
7756 	mutex_enter(&ipsq->ipsq_lock);
7757 
7758 	/*
7759 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7760 	 *    (Note: If the caller does not specify reentry_ok then neither
7761 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7762 	 *    again. Otherwise it can lead to an infinite loop
7763 	 * 2. Enter the ipsq if there is no current writer and this attempted
7764 	 *    entry is part of the current ioctl or operation
7765 	 * 3. Enter the ipsq if there is no current writer and this is a new
7766 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7767 	 *    empty and there is no ioctl (or operation) currently in progress
7768 	 */
7769 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7770 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7771 	    ipsq->ipsq_current_ipif == NULL))) ||
7772 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7773 		/* Success. */
7774 		ipsq->ipsq_reentry_cnt++;
7775 		ipsq->ipsq_writer = curthread;
7776 		mutex_exit(&ipsq->ipsq_lock);
7777 		mutex_exit(&ill->ill_lock);
7778 		RELEASE_CONN_LOCK(q);
7779 #ifdef DEBUG
7780 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7781 		    IPSQ_STACK_DEPTH);
7782 #endif
7783 		return (ipsq);
7784 	}
7785 
7786 	ipsq_enq(ipsq, q, mp, func, type, ill);
7787 
7788 	mutex_exit(&ipsq->ipsq_lock);
7789 	mutex_exit(&ill->ill_lock);
7790 	RELEASE_CONN_LOCK(q);
7791 	return (NULL);
7792 }
7793 
7794 /*
7795  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7796  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7797  * cannot be entered, the mp is queued for completion.
7798  */
7799 void
7800 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7801     boolean_t reentry_ok)
7802 {
7803 	ipsq_t	*ipsq;
7804 
7805 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7806 
7807 	/*
7808 	 * Drop the caller's refhold on the ill.  This is safe since we either
7809 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7810 	 * IPSQ, in which case we return without accessing ill anymore.  This
7811 	 * is needed because func needs to see the correct refcount.
7812 	 * e.g. removeif can work only then.
7813 	 */
7814 	ill_refrele(ill);
7815 	if (ipsq != NULL) {
7816 		(*func)(ipsq, q, mp, NULL);
7817 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7818 	}
7819 }
7820 
7821 /*
7822  * If there are more than ILL_GRP_CNT ills in a group,
7823  * we use kmem alloc'd buffers, else use the stack
7824  */
7825 #define	ILL_GRP_CNT	14
7826 /*
7827  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7828  * Called by a thread that is currently exclusive on this ipsq.
7829  */
7830 void
7831 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7832 {
7833 	queue_t	*q;
7834 	mblk_t	*mp;
7835 	ipsq_func_t	func;
7836 	int	next;
7837 	ill_t	**ill_list = NULL;
7838 	size_t	ill_list_size = 0;
7839 	int	cnt = 0;
7840 	boolean_t need_ipsq_free = B_FALSE;
7841 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7842 
7843 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7844 	mutex_enter(&ipsq->ipsq_lock);
7845 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7846 	if (ipsq->ipsq_reentry_cnt != 1) {
7847 		ipsq->ipsq_reentry_cnt--;
7848 		mutex_exit(&ipsq->ipsq_lock);
7849 		return;
7850 	}
7851 
7852 	mp = ipsq_dq(ipsq);
7853 	while (mp != NULL) {
7854 again:
7855 		mutex_exit(&ipsq->ipsq_lock);
7856 		func = (ipsq_func_t)mp->b_prev;
7857 		q = (queue_t *)mp->b_queue;
7858 		mp->b_prev = NULL;
7859 		mp->b_queue = NULL;
7860 
7861 		/*
7862 		 * If 'q' is an conn queue, it is valid, since we did a
7863 		 * a refhold on the connp, at the start of the ioctl.
7864 		 * If 'q' is an ill queue, it is valid, since close of an
7865 		 * ill will clean up the 'ipsq'.
7866 		 */
7867 		(*func)(ipsq, q, mp, NULL);
7868 
7869 		mutex_enter(&ipsq->ipsq_lock);
7870 		mp = ipsq_dq(ipsq);
7871 	}
7872 
7873 	mutex_exit(&ipsq->ipsq_lock);
7874 
7875 	/*
7876 	 * Need to grab the locks in the right order. Need to
7877 	 * atomically check (under ipsq_lock) that there are no
7878 	 * messages before relinquishing the ipsq. Also need to
7879 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7880 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7881 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7882 	 * to grab ill_g_lock as writer.
7883 	 */
7884 	rw_enter(&ipst->ips_ill_g_lock,
7885 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7886 
7887 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7888 	if (ipsq->ipsq_refs != 0) {
7889 		/* At most 2 ills v4/v6 per phyint */
7890 		cnt = ipsq->ipsq_refs << 1;
7891 		ill_list_size = cnt * sizeof (ill_t *);
7892 		/*
7893 		 * If memory allocation fails, we will do the split
7894 		 * the next time ipsq_exit is called for whatever reason.
7895 		 * As long as the ipsq_split flag is set the need to
7896 		 * split is remembered.
7897 		 */
7898 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7899 		if (ill_list != NULL)
7900 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7901 	}
7902 	mutex_enter(&ipsq->ipsq_lock);
7903 	mp = ipsq_dq(ipsq);
7904 	if (mp != NULL) {
7905 		/* oops, some message has landed up, we can't get out */
7906 		if (ill_list != NULL)
7907 			ill_unlock_ills(ill_list, cnt);
7908 		rw_exit(&ipst->ips_ill_g_lock);
7909 		if (ill_list != NULL)
7910 			kmem_free(ill_list, ill_list_size);
7911 		ill_list = NULL;
7912 		ill_list_size = 0;
7913 		cnt = 0;
7914 		goto again;
7915 	}
7916 
7917 	/*
7918 	 * Split only if no ioctl is pending and if memory alloc succeeded
7919 	 * above.
7920 	 */
7921 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7922 	    ill_list != NULL) {
7923 		/*
7924 		 * No new ill can join this ipsq since we are holding the
7925 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7926 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7927 		 * If so we will retry on the next ipsq_exit.
7928 		 */
7929 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7930 	}
7931 
7932 	/*
7933 	 * We are holding the ipsq lock, hence no new messages can
7934 	 * land up on the ipsq, and there are no messages currently.
7935 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7936 	 * atomically while holding ill locks.
7937 	 */
7938 	ipsq->ipsq_writer = NULL;
7939 	ipsq->ipsq_reentry_cnt--;
7940 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7941 #ifdef DEBUG
7942 	ipsq->ipsq_depth = 0;
7943 #endif
7944 	mutex_exit(&ipsq->ipsq_lock);
7945 	/*
7946 	 * For IPMP this should wake up all ills in this ipsq.
7947 	 * We need to hold the ill_lock while waking up waiters to
7948 	 * avoid missed wakeups. But there is no need to acquire all
7949 	 * the ill locks and then wakeup. If we have not acquired all
7950 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7951 	 * wakes up ills one at a time after getting the right ill_lock
7952 	 */
7953 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7954 	if (ill_list != NULL)
7955 		ill_unlock_ills(ill_list, cnt);
7956 	if (ipsq->ipsq_refs == 0)
7957 		need_ipsq_free = B_TRUE;
7958 	rw_exit(&ipst->ips_ill_g_lock);
7959 	if (ill_list != 0)
7960 		kmem_free(ill_list, ill_list_size);
7961 
7962 	if (need_ipsq_free) {
7963 		/*
7964 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7965 		 * looked up. ipsq can be looked up only thru ill or phyint
7966 		 * and there are no ills/phyint on this ipsq.
7967 		 */
7968 		ipsq_delete(ipsq);
7969 	}
7970 	/*
7971 	 * Now start any igmp or mld timers that could not be started
7972 	 * while inside the ipsq. The timers can't be started while inside
7973 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7974 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7975 	 * there could be a deadlock since the timeout handlers
7976 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7977 	 * wait in ipsq_enter() trying to get the ipsq.
7978 	 *
7979 	 * However there is one exception to the above. If this thread is
7980 	 * itself the igmp/mld timeout handler thread, then we don't want
7981 	 * to start any new timer until the current handler is done. The
7982 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7983 	 * all others pass B_TRUE.
7984 	 */
7985 	if (start_igmp_timer) {
7986 		mutex_enter(&ipst->ips_igmp_timer_lock);
7987 		next = ipst->ips_igmp_deferred_next;
7988 		ipst->ips_igmp_deferred_next = INFINITY;
7989 		mutex_exit(&ipst->ips_igmp_timer_lock);
7990 
7991 		if (next != INFINITY)
7992 			igmp_start_timers(next, ipst);
7993 	}
7994 
7995 	if (start_mld_timer) {
7996 		mutex_enter(&ipst->ips_mld_timer_lock);
7997 		next = ipst->ips_mld_deferred_next;
7998 		ipst->ips_mld_deferred_next = INFINITY;
7999 		mutex_exit(&ipst->ips_mld_timer_lock);
8000 
8001 		if (next != INFINITY)
8002 			mld_start_timers(next, ipst);
8003 	}
8004 }
8005 
8006 /*
8007  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8008  * and `ioccmd'.
8009  */
8010 void
8011 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8012 {
8013 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8014 
8015 	mutex_enter(&ipsq->ipsq_lock);
8016 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8017 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8018 	ipsq->ipsq_current_done = B_FALSE;
8019 	ipsq->ipsq_current_ipif = ipif;
8020 	ipsq->ipsq_current_ioctl = ioccmd;
8021 	mutex_exit(&ipsq->ipsq_lock);
8022 }
8023 
8024 /*
8025  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8026  * the next exclusive operation to begin once we ipsq_exit().  However, if
8027  * pending DLPI operations remain, then we will wait for the queue to drain
8028  * before allowing the next exclusive operation to begin.  This ensures that
8029  * DLPI operations from one exclusive operation are never improperly processed
8030  * as part of a subsequent exclusive operation.
8031  */
8032 void
8033 ipsq_current_finish(ipsq_t *ipsq)
8034 {
8035 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8036 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8037 
8038 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8039 
8040 	/*
8041 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8042 	 * (but in that case, IPIF_CHANGING will already be clear and no
8043 	 * pending DLPI messages can remain).
8044 	 */
8045 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8046 		ill_t *ill = ipif->ipif_ill;
8047 
8048 		mutex_enter(&ill->ill_lock);
8049 		dlpi_pending = ill->ill_dlpi_pending;
8050 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8051 		/* Send any queued event */
8052 		ill_nic_info_dispatch(ill);
8053 		mutex_exit(&ill->ill_lock);
8054 	}
8055 
8056 	mutex_enter(&ipsq->ipsq_lock);
8057 	ipsq->ipsq_current_ioctl = 0;
8058 	ipsq->ipsq_current_done = B_TRUE;
8059 	if (dlpi_pending == DL_PRIM_INVAL)
8060 		ipsq->ipsq_current_ipif = NULL;
8061 	mutex_exit(&ipsq->ipsq_lock);
8062 }
8063 
8064 /*
8065  * The ill is closing. Flush all messages on the ipsq that originated
8066  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8067  * for this ill since ipsq_enter could not have entered until then.
8068  * New messages can't be queued since the CONDEMNED flag is set.
8069  */
8070 static void
8071 ipsq_flush(ill_t *ill)
8072 {
8073 	queue_t	*q;
8074 	mblk_t	*prev;
8075 	mblk_t	*mp;
8076 	mblk_t	*mp_next;
8077 	ipsq_t	*ipsq;
8078 
8079 	ASSERT(IAM_WRITER_ILL(ill));
8080 	ipsq = ill->ill_phyint->phyint_ipsq;
8081 	/*
8082 	 * Flush any messages sent up by the driver.
8083 	 */
8084 	mutex_enter(&ipsq->ipsq_lock);
8085 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8086 		mp_next = mp->b_next;
8087 		q = mp->b_queue;
8088 		if (q == ill->ill_rq || q == ill->ill_wq) {
8089 			/* Remove the mp from the ipsq */
8090 			if (prev == NULL)
8091 				ipsq->ipsq_mphead = mp->b_next;
8092 			else
8093 				prev->b_next = mp->b_next;
8094 			if (ipsq->ipsq_mptail == mp) {
8095 				ASSERT(mp_next == NULL);
8096 				ipsq->ipsq_mptail = prev;
8097 			}
8098 			inet_freemsg(mp);
8099 		} else {
8100 			prev = mp;
8101 		}
8102 	}
8103 	mutex_exit(&ipsq->ipsq_lock);
8104 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8105 	ipsq_xopq_mp_cleanup(ill, NULL);
8106 	ill_pending_mp_cleanup(ill);
8107 }
8108 
8109 /* ARGSUSED */
8110 int
8111 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8112     ip_ioctl_cmd_t *ipip, void *ifreq)
8113 {
8114 	ill_t	*ill;
8115 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8116 	boolean_t isv6;
8117 	conn_t	*connp;
8118 	ip_stack_t	*ipst;
8119 
8120 	connp = Q_TO_CONN(q);
8121 	ipst = connp->conn_netstack->netstack_ip;
8122 	isv6 = connp->conn_af_isv6;
8123 	/*
8124 	 * Set original index.
8125 	 * Failover and failback move logical interfaces
8126 	 * from one physical interface to another.  The
8127 	 * original index indicates the parent of a logical
8128 	 * interface, in other words, the physical interface
8129 	 * the logical interface will be moved back to on
8130 	 * failback.
8131 	 */
8132 
8133 	/*
8134 	 * Don't allow the original index to be changed
8135 	 * for non-failover addresses, autoconfigured
8136 	 * addresses, or IPv6 link local addresses.
8137 	 */
8138 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8139 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8140 		return (EINVAL);
8141 	}
8142 	/*
8143 	 * The new original index must be in use by some
8144 	 * physical interface.
8145 	 */
8146 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8147 	    NULL, NULL, ipst);
8148 	if (ill == NULL)
8149 		return (ENXIO);
8150 	ill_refrele(ill);
8151 
8152 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8153 	/*
8154 	 * When this ipif gets failed back, don't
8155 	 * preserve the original id, as it is no
8156 	 * longer applicable.
8157 	 */
8158 	ipif->ipif_orig_ipifid = 0;
8159 	/*
8160 	 * For IPv4, change the original index of any
8161 	 * multicast addresses associated with the
8162 	 * ipif to the new value.
8163 	 */
8164 	if (!isv6) {
8165 		ilm_t *ilm;
8166 
8167 		mutex_enter(&ipif->ipif_ill->ill_lock);
8168 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8169 		    ilm = ilm->ilm_next) {
8170 			if (ilm->ilm_ipif == ipif) {
8171 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8172 			}
8173 		}
8174 		mutex_exit(&ipif->ipif_ill->ill_lock);
8175 	}
8176 	return (0);
8177 }
8178 
8179 /* ARGSUSED */
8180 int
8181 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8182     ip_ioctl_cmd_t *ipip, void *ifreq)
8183 {
8184 	struct lifreq *lifr = (struct lifreq *)ifreq;
8185 
8186 	/*
8187 	 * Get the original interface index i.e the one
8188 	 * before FAILOVER if it ever happened.
8189 	 */
8190 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8191 	return (0);
8192 }
8193 
8194 /*
8195  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8196  * refhold and return the associated ipif
8197  */
8198 /* ARGSUSED */
8199 int
8200 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8201     cmd_info_t *ci, ipsq_func_t func)
8202 {
8203 	boolean_t exists;
8204 	struct iftun_req *ta;
8205 	ipif_t	*ipif;
8206 	ill_t	*ill;
8207 	boolean_t isv6;
8208 	mblk_t	*mp1;
8209 	int	error;
8210 	conn_t	*connp;
8211 	ip_stack_t	*ipst;
8212 
8213 	/* Existence verified in ip_wput_nondata */
8214 	mp1 = mp->b_cont->b_cont;
8215 	ta = (struct iftun_req *)mp1->b_rptr;
8216 	/*
8217 	 * Null terminate the string to protect against buffer
8218 	 * overrun. String was generated by user code and may not
8219 	 * be trusted.
8220 	 */
8221 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8222 
8223 	connp = Q_TO_CONN(q);
8224 	isv6 = connp->conn_af_isv6;
8225 	ipst = connp->conn_netstack->netstack_ip;
8226 
8227 	/* Disallows implicit create */
8228 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8229 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8230 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8231 	if (ipif == NULL)
8232 		return (error);
8233 
8234 	if (ipif->ipif_id != 0) {
8235 		/*
8236 		 * We really don't want to set/get tunnel parameters
8237 		 * on virtual tunnel interfaces.  Only allow the
8238 		 * base tunnel to do these.
8239 		 */
8240 		ipif_refrele(ipif);
8241 		return (EINVAL);
8242 	}
8243 
8244 	/*
8245 	 * Send down to tunnel mod for ioctl processing.
8246 	 * Will finish ioctl in ip_rput_other().
8247 	 */
8248 	ill = ipif->ipif_ill;
8249 	if (ill->ill_net_type == IRE_LOOPBACK) {
8250 		ipif_refrele(ipif);
8251 		return (EOPNOTSUPP);
8252 	}
8253 
8254 	if (ill->ill_wq == NULL) {
8255 		ipif_refrele(ipif);
8256 		return (ENXIO);
8257 	}
8258 	/*
8259 	 * Mark the ioctl as coming from an IPv6 interface for
8260 	 * tun's convenience.
8261 	 */
8262 	if (ill->ill_isv6)
8263 		ta->ifta_flags |= 0x80000000;
8264 	ci->ci_ipif = ipif;
8265 	return (0);
8266 }
8267 
8268 /*
8269  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8270  * and return the associated ipif.
8271  * Return value:
8272  *	Non zero: An error has occurred. ci may not be filled out.
8273  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8274  *	a held ipif in ci.ci_ipif.
8275  */
8276 int
8277 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8278     cmd_info_t *ci, ipsq_func_t func)
8279 {
8280 	sin_t		*sin;
8281 	sin6_t		*sin6;
8282 	char		*name;
8283 	struct ifreq    *ifr;
8284 	struct lifreq    *lifr;
8285 	ipif_t		*ipif = NULL;
8286 	ill_t		*ill;
8287 	conn_t		*connp;
8288 	boolean_t	isv6;
8289 	boolean_t	exists;
8290 	int		err;
8291 	mblk_t		*mp1;
8292 	zoneid_t	zoneid;
8293 	ip_stack_t	*ipst;
8294 
8295 	if (q->q_next != NULL) {
8296 		ill = (ill_t *)q->q_ptr;
8297 		isv6 = ill->ill_isv6;
8298 		connp = NULL;
8299 		zoneid = ALL_ZONES;
8300 		ipst = ill->ill_ipst;
8301 	} else {
8302 		ill = NULL;
8303 		connp = Q_TO_CONN(q);
8304 		isv6 = connp->conn_af_isv6;
8305 		zoneid = connp->conn_zoneid;
8306 		if (zoneid == GLOBAL_ZONEID) {
8307 			/* global zone can access ipifs in all zones */
8308 			zoneid = ALL_ZONES;
8309 		}
8310 		ipst = connp->conn_netstack->netstack_ip;
8311 	}
8312 
8313 	/* Has been checked in ip_wput_nondata */
8314 	mp1 = mp->b_cont->b_cont;
8315 
8316 	if (ipip->ipi_cmd_type == IF_CMD) {
8317 		/* This a old style SIOC[GS]IF* command */
8318 		ifr = (struct ifreq *)mp1->b_rptr;
8319 		/*
8320 		 * Null terminate the string to protect against buffer
8321 		 * overrun. String was generated by user code and may not
8322 		 * be trusted.
8323 		 */
8324 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8325 		sin = (sin_t *)&ifr->ifr_addr;
8326 		name = ifr->ifr_name;
8327 		ci->ci_sin = sin;
8328 		ci->ci_sin6 = NULL;
8329 		ci->ci_lifr = (struct lifreq *)ifr;
8330 	} else {
8331 		/* This a new style SIOC[GS]LIF* command */
8332 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8333 		lifr = (struct lifreq *)mp1->b_rptr;
8334 		/*
8335 		 * Null terminate the string to protect against buffer
8336 		 * overrun. String was generated by user code and may not
8337 		 * be trusted.
8338 		 */
8339 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8340 		name = lifr->lifr_name;
8341 		sin = (sin_t *)&lifr->lifr_addr;
8342 		sin6 = (sin6_t *)&lifr->lifr_addr;
8343 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8344 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8345 			    LIFNAMSIZ);
8346 		}
8347 		ci->ci_sin = sin;
8348 		ci->ci_sin6 = sin6;
8349 		ci->ci_lifr = lifr;
8350 	}
8351 
8352 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8353 		/*
8354 		 * The ioctl will be failed if the ioctl comes down
8355 		 * an conn stream
8356 		 */
8357 		if (ill == NULL) {
8358 			/*
8359 			 * Not an ill queue, return EINVAL same as the
8360 			 * old error code.
8361 			 */
8362 			return (ENXIO);
8363 		}
8364 		ipif = ill->ill_ipif;
8365 		ipif_refhold(ipif);
8366 	} else {
8367 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8368 		    &exists, isv6, zoneid,
8369 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8370 		    ipst);
8371 		if (ipif == NULL) {
8372 			if (err == EINPROGRESS)
8373 				return (err);
8374 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8375 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8376 				/*
8377 				 * Need to try both v4 and v6 since this
8378 				 * ioctl can come down either v4 or v6
8379 				 * socket. The lifreq.lifr_family passed
8380 				 * down by this ioctl is AF_UNSPEC.
8381 				 */
8382 				ipif = ipif_lookup_on_name(name,
8383 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8384 				    zoneid, (connp == NULL) ? q :
8385 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8386 				if (err == EINPROGRESS)
8387 					return (err);
8388 			}
8389 			err = 0;	/* Ensure we don't use it below */
8390 		}
8391 	}
8392 
8393 	/*
8394 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8395 	 */
8396 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8397 		ipif_refrele(ipif);
8398 		return (ENXIO);
8399 	}
8400 
8401 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8402 	    name[0] == '\0') {
8403 		/*
8404 		 * Handle a or a SIOC?IF* with a null name
8405 		 * during plumb (on the ill queue before the I_PLINK).
8406 		 */
8407 		ipif = ill->ill_ipif;
8408 		ipif_refhold(ipif);
8409 	}
8410 
8411 	if (ipif == NULL)
8412 		return (ENXIO);
8413 
8414 	/*
8415 	 * Allow only GET operations if this ipif has been created
8416 	 * temporarily due to a MOVE operation.
8417 	 */
8418 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8419 		ipif_refrele(ipif);
8420 		return (EINVAL);
8421 	}
8422 
8423 	ci->ci_ipif = ipif;
8424 	return (0);
8425 }
8426 
8427 /*
8428  * Return the total number of ipifs.
8429  */
8430 static uint_t
8431 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8432 {
8433 	uint_t numifs = 0;
8434 	ill_t	*ill;
8435 	ill_walk_context_t	ctx;
8436 	ipif_t	*ipif;
8437 
8438 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8439 	ill = ILL_START_WALK_V4(&ctx, ipst);
8440 
8441 	while (ill != NULL) {
8442 		for (ipif = ill->ill_ipif; ipif != NULL;
8443 		    ipif = ipif->ipif_next) {
8444 			if (ipif->ipif_zoneid == zoneid ||
8445 			    ipif->ipif_zoneid == ALL_ZONES)
8446 				numifs++;
8447 		}
8448 		ill = ill_next(&ctx, ill);
8449 	}
8450 	rw_exit(&ipst->ips_ill_g_lock);
8451 	return (numifs);
8452 }
8453 
8454 /*
8455  * Return the total number of ipifs.
8456  */
8457 static uint_t
8458 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8459 {
8460 	uint_t numifs = 0;
8461 	ill_t	*ill;
8462 	ipif_t	*ipif;
8463 	ill_walk_context_t	ctx;
8464 
8465 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8466 
8467 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8468 	if (family == AF_INET)
8469 		ill = ILL_START_WALK_V4(&ctx, ipst);
8470 	else if (family == AF_INET6)
8471 		ill = ILL_START_WALK_V6(&ctx, ipst);
8472 	else
8473 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8474 
8475 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8476 		for (ipif = ill->ill_ipif; ipif != NULL;
8477 		    ipif = ipif->ipif_next) {
8478 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8479 			    !(lifn_flags & LIFC_NOXMIT))
8480 				continue;
8481 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8482 			    !(lifn_flags & LIFC_TEMPORARY))
8483 				continue;
8484 			if (((ipif->ipif_flags &
8485 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8486 			    IPIF_DEPRECATED)) ||
8487 			    IS_LOOPBACK(ill) ||
8488 			    !(ipif->ipif_flags & IPIF_UP)) &&
8489 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8490 				continue;
8491 
8492 			if (zoneid != ipif->ipif_zoneid &&
8493 			    ipif->ipif_zoneid != ALL_ZONES &&
8494 			    (zoneid != GLOBAL_ZONEID ||
8495 			    !(lifn_flags & LIFC_ALLZONES)))
8496 				continue;
8497 
8498 			numifs++;
8499 		}
8500 	}
8501 	rw_exit(&ipst->ips_ill_g_lock);
8502 	return (numifs);
8503 }
8504 
8505 uint_t
8506 ip_get_lifsrcofnum(ill_t *ill)
8507 {
8508 	uint_t numifs = 0;
8509 	ill_t	*ill_head = ill;
8510 	ip_stack_t	*ipst = ill->ill_ipst;
8511 
8512 	/*
8513 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8514 	 * other thread may be trying to relink the ILLs in this usesrc group
8515 	 * and adjusting the ill_usesrc_grp_next pointers
8516 	 */
8517 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8518 	if ((ill->ill_usesrc_ifindex == 0) &&
8519 	    (ill->ill_usesrc_grp_next != NULL)) {
8520 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8521 		    ill = ill->ill_usesrc_grp_next)
8522 			numifs++;
8523 	}
8524 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8525 
8526 	return (numifs);
8527 }
8528 
8529 /* Null values are passed in for ipif, sin, and ifreq */
8530 /* ARGSUSED */
8531 int
8532 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8533     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8534 {
8535 	int *nump;
8536 	conn_t *connp = Q_TO_CONN(q);
8537 
8538 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8539 
8540 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8541 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8542 
8543 	*nump = ip_get_numifs(connp->conn_zoneid,
8544 	    connp->conn_netstack->netstack_ip);
8545 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8546 	return (0);
8547 }
8548 
8549 /* Null values are passed in for ipif, sin, and ifreq */
8550 /* ARGSUSED */
8551 int
8552 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8553     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8554 {
8555 	struct lifnum *lifn;
8556 	mblk_t	*mp1;
8557 	conn_t *connp = Q_TO_CONN(q);
8558 
8559 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8560 
8561 	/* Existence checked in ip_wput_nondata */
8562 	mp1 = mp->b_cont->b_cont;
8563 
8564 	lifn = (struct lifnum *)mp1->b_rptr;
8565 	switch (lifn->lifn_family) {
8566 	case AF_UNSPEC:
8567 	case AF_INET:
8568 	case AF_INET6:
8569 		break;
8570 	default:
8571 		return (EAFNOSUPPORT);
8572 	}
8573 
8574 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8575 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8576 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8577 	return (0);
8578 }
8579 
8580 /* ARGSUSED */
8581 int
8582 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8583     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8584 {
8585 	STRUCT_HANDLE(ifconf, ifc);
8586 	mblk_t *mp1;
8587 	struct iocblk *iocp;
8588 	struct ifreq *ifr;
8589 	ill_walk_context_t	ctx;
8590 	ill_t	*ill;
8591 	ipif_t	*ipif;
8592 	struct sockaddr_in *sin;
8593 	int32_t	ifclen;
8594 	zoneid_t zoneid;
8595 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8596 
8597 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8598 
8599 	ip1dbg(("ip_sioctl_get_ifconf"));
8600 	/* Existence verified in ip_wput_nondata */
8601 	mp1 = mp->b_cont->b_cont;
8602 	iocp = (struct iocblk *)mp->b_rptr;
8603 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8604 
8605 	/*
8606 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8607 	 * the user buffer address and length into which the list of struct
8608 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8609 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8610 	 * the SIOCGIFCONF operation was redefined to simply provide
8611 	 * a large output buffer into which we are supposed to jam the ifreq
8612 	 * array.  The same ioctl command code was used, despite the fact that
8613 	 * both the applications and the kernel code had to change, thus making
8614 	 * it impossible to support both interfaces.
8615 	 *
8616 	 * For reasons not good enough to try to explain, the following
8617 	 * algorithm is used for deciding what to do with one of these:
8618 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8619 	 * form with the output buffer coming down as the continuation message.
8620 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8621 	 * and we have to copy in the ifconf structure to find out how big the
8622 	 * output buffer is and where to copy out to.  Sure no problem...
8623 	 *
8624 	 */
8625 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8626 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8627 		int numifs = 0;
8628 		size_t ifc_bufsize;
8629 
8630 		/*
8631 		 * Must be (better be!) continuation of a TRANSPARENT
8632 		 * IOCTL.  We just copied in the ifconf structure.
8633 		 */
8634 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8635 		    (struct ifconf *)mp1->b_rptr);
8636 
8637 		/*
8638 		 * Allocate a buffer to hold requested information.
8639 		 *
8640 		 * If ifc_len is larger than what is needed, we only
8641 		 * allocate what we will use.
8642 		 *
8643 		 * If ifc_len is smaller than what is needed, return
8644 		 * EINVAL.
8645 		 *
8646 		 * XXX: the ill_t structure can hava 2 counters, for
8647 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8648 		 * number of interfaces for a device, so we don't need
8649 		 * to count them here...
8650 		 */
8651 		numifs = ip_get_numifs(zoneid, ipst);
8652 
8653 		ifclen = STRUCT_FGET(ifc, ifc_len);
8654 		ifc_bufsize = numifs * sizeof (struct ifreq);
8655 		if (ifc_bufsize > ifclen) {
8656 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8657 				/* old behaviour */
8658 				return (EINVAL);
8659 			} else {
8660 				ifc_bufsize = ifclen;
8661 			}
8662 		}
8663 
8664 		mp1 = mi_copyout_alloc(q, mp,
8665 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8666 		if (mp1 == NULL)
8667 			return (ENOMEM);
8668 
8669 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8670 	}
8671 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8672 	/*
8673 	 * the SIOCGIFCONF ioctl only knows about
8674 	 * IPv4 addresses, so don't try to tell
8675 	 * it about interfaces with IPv6-only
8676 	 * addresses. (Last parm 'isv6' is B_FALSE)
8677 	 */
8678 
8679 	ifr = (struct ifreq *)mp1->b_rptr;
8680 
8681 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8682 	ill = ILL_START_WALK_V4(&ctx, ipst);
8683 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8684 		for (ipif = ill->ill_ipif; ipif != NULL;
8685 		    ipif = ipif->ipif_next) {
8686 			if (zoneid != ipif->ipif_zoneid &&
8687 			    ipif->ipif_zoneid != ALL_ZONES)
8688 				continue;
8689 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8690 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8691 					/* old behaviour */
8692 					rw_exit(&ipst->ips_ill_g_lock);
8693 					return (EINVAL);
8694 				} else {
8695 					goto if_copydone;
8696 				}
8697 			}
8698 			ipif_get_name(ipif, ifr->ifr_name,
8699 			    sizeof (ifr->ifr_name));
8700 			sin = (sin_t *)&ifr->ifr_addr;
8701 			*sin = sin_null;
8702 			sin->sin_family = AF_INET;
8703 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8704 			ifr++;
8705 		}
8706 	}
8707 if_copydone:
8708 	rw_exit(&ipst->ips_ill_g_lock);
8709 	mp1->b_wptr = (uchar_t *)ifr;
8710 
8711 	if (STRUCT_BUF(ifc) != NULL) {
8712 		STRUCT_FSET(ifc, ifc_len,
8713 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8714 	}
8715 	return (0);
8716 }
8717 
8718 /*
8719  * Get the interfaces using the address hosted on the interface passed in,
8720  * as a source adddress
8721  */
8722 /* ARGSUSED */
8723 int
8724 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8725     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8726 {
8727 	mblk_t *mp1;
8728 	ill_t	*ill, *ill_head;
8729 	ipif_t	*ipif, *orig_ipif;
8730 	int	numlifs = 0;
8731 	size_t	lifs_bufsize, lifsmaxlen;
8732 	struct	lifreq *lifr;
8733 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8734 	uint_t	ifindex;
8735 	zoneid_t zoneid;
8736 	int err = 0;
8737 	boolean_t isv6 = B_FALSE;
8738 	struct	sockaddr_in	*sin;
8739 	struct	sockaddr_in6	*sin6;
8740 	STRUCT_HANDLE(lifsrcof, lifs);
8741 	ip_stack_t		*ipst;
8742 
8743 	ipst = CONNQ_TO_IPST(q);
8744 
8745 	ASSERT(q->q_next == NULL);
8746 
8747 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8748 
8749 	/* Existence verified in ip_wput_nondata */
8750 	mp1 = mp->b_cont->b_cont;
8751 
8752 	/*
8753 	 * Must be (better be!) continuation of a TRANSPARENT
8754 	 * IOCTL.  We just copied in the lifsrcof structure.
8755 	 */
8756 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8757 	    (struct lifsrcof *)mp1->b_rptr);
8758 
8759 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8760 		return (EINVAL);
8761 
8762 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8763 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8764 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8765 	    ip_process_ioctl, &err, ipst);
8766 	if (ipif == NULL) {
8767 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8768 		    ifindex));
8769 		return (err);
8770 	}
8771 
8772 
8773 	/* Allocate a buffer to hold requested information */
8774 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8775 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8776 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8777 	/* The actual size needed is always returned in lifs_len */
8778 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8779 
8780 	/* If the amount we need is more than what is passed in, abort */
8781 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8782 		ipif_refrele(ipif);
8783 		return (0);
8784 	}
8785 
8786 	mp1 = mi_copyout_alloc(q, mp,
8787 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8788 	if (mp1 == NULL) {
8789 		ipif_refrele(ipif);
8790 		return (ENOMEM);
8791 	}
8792 
8793 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8794 	bzero(mp1->b_rptr, lifs_bufsize);
8795 
8796 	lifr = (struct lifreq *)mp1->b_rptr;
8797 
8798 	ill = ill_head = ipif->ipif_ill;
8799 	orig_ipif = ipif;
8800 
8801 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8802 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8803 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8804 
8805 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8806 	for (; (ill != NULL) && (ill != ill_head);
8807 	    ill = ill->ill_usesrc_grp_next) {
8808 
8809 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8810 			break;
8811 
8812 		ipif = ill->ill_ipif;
8813 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8814 		if (ipif->ipif_isv6) {
8815 			sin6 = (sin6_t *)&lifr->lifr_addr;
8816 			*sin6 = sin6_null;
8817 			sin6->sin6_family = AF_INET6;
8818 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8819 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8820 			    &ipif->ipif_v6net_mask);
8821 		} else {
8822 			sin = (sin_t *)&lifr->lifr_addr;
8823 			*sin = sin_null;
8824 			sin->sin_family = AF_INET;
8825 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8826 			lifr->lifr_addrlen = ip_mask_to_plen(
8827 			    ipif->ipif_net_mask);
8828 		}
8829 		lifr++;
8830 	}
8831 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8832 	rw_exit(&ipst->ips_ill_g_lock);
8833 	ipif_refrele(orig_ipif);
8834 	mp1->b_wptr = (uchar_t *)lifr;
8835 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8836 
8837 	return (0);
8838 }
8839 
8840 /* ARGSUSED */
8841 int
8842 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8843     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8844 {
8845 	mblk_t *mp1;
8846 	int	list;
8847 	ill_t	*ill;
8848 	ipif_t	*ipif;
8849 	int	flags;
8850 	int	numlifs = 0;
8851 	size_t	lifc_bufsize;
8852 	struct	lifreq *lifr;
8853 	sa_family_t	family;
8854 	struct	sockaddr_in	*sin;
8855 	struct	sockaddr_in6	*sin6;
8856 	ill_walk_context_t	ctx;
8857 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8858 	int32_t	lifclen;
8859 	zoneid_t zoneid;
8860 	STRUCT_HANDLE(lifconf, lifc);
8861 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8862 
8863 	ip1dbg(("ip_sioctl_get_lifconf"));
8864 
8865 	ASSERT(q->q_next == NULL);
8866 
8867 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8868 
8869 	/* Existence verified in ip_wput_nondata */
8870 	mp1 = mp->b_cont->b_cont;
8871 
8872 	/*
8873 	 * An extended version of SIOCGIFCONF that takes an
8874 	 * additional address family and flags field.
8875 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8876 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8877 	 * interfaces are omitted.
8878 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8879 	 * unless LIFC_TEMPORARY is specified.
8880 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8881 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8882 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8883 	 * has priority over LIFC_NOXMIT.
8884 	 */
8885 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8886 
8887 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8888 		return (EINVAL);
8889 
8890 	/*
8891 	 * Must be (better be!) continuation of a TRANSPARENT
8892 	 * IOCTL.  We just copied in the lifconf structure.
8893 	 */
8894 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8895 
8896 	family = STRUCT_FGET(lifc, lifc_family);
8897 	flags = STRUCT_FGET(lifc, lifc_flags);
8898 
8899 	switch (family) {
8900 	case AF_UNSPEC:
8901 		/*
8902 		 * walk all ILL's.
8903 		 */
8904 		list = MAX_G_HEADS;
8905 		break;
8906 	case AF_INET:
8907 		/*
8908 		 * walk only IPV4 ILL's.
8909 		 */
8910 		list = IP_V4_G_HEAD;
8911 		break;
8912 	case AF_INET6:
8913 		/*
8914 		 * walk only IPV6 ILL's.
8915 		 */
8916 		list = IP_V6_G_HEAD;
8917 		break;
8918 	default:
8919 		return (EAFNOSUPPORT);
8920 	}
8921 
8922 	/*
8923 	 * Allocate a buffer to hold requested information.
8924 	 *
8925 	 * If lifc_len is larger than what is needed, we only
8926 	 * allocate what we will use.
8927 	 *
8928 	 * If lifc_len is smaller than what is needed, return
8929 	 * EINVAL.
8930 	 */
8931 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8932 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8933 	lifclen = STRUCT_FGET(lifc, lifc_len);
8934 	if (lifc_bufsize > lifclen) {
8935 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8936 			return (EINVAL);
8937 		else
8938 			lifc_bufsize = lifclen;
8939 	}
8940 
8941 	mp1 = mi_copyout_alloc(q, mp,
8942 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8943 	if (mp1 == NULL)
8944 		return (ENOMEM);
8945 
8946 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8947 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8948 
8949 	lifr = (struct lifreq *)mp1->b_rptr;
8950 
8951 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8952 	ill = ill_first(list, list, &ctx, ipst);
8953 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8954 		for (ipif = ill->ill_ipif; ipif != NULL;
8955 		    ipif = ipif->ipif_next) {
8956 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8957 			    !(flags & LIFC_NOXMIT))
8958 				continue;
8959 
8960 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8961 			    !(flags & LIFC_TEMPORARY))
8962 				continue;
8963 
8964 			if (((ipif->ipif_flags &
8965 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8966 			    IPIF_DEPRECATED)) ||
8967 			    IS_LOOPBACK(ill) ||
8968 			    !(ipif->ipif_flags & IPIF_UP)) &&
8969 			    (flags & LIFC_EXTERNAL_SOURCE))
8970 				continue;
8971 
8972 			if (zoneid != ipif->ipif_zoneid &&
8973 			    ipif->ipif_zoneid != ALL_ZONES &&
8974 			    (zoneid != GLOBAL_ZONEID ||
8975 			    !(flags & LIFC_ALLZONES)))
8976 				continue;
8977 
8978 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8979 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8980 					rw_exit(&ipst->ips_ill_g_lock);
8981 					return (EINVAL);
8982 				} else {
8983 					goto lif_copydone;
8984 				}
8985 			}
8986 
8987 			ipif_get_name(ipif, lifr->lifr_name,
8988 			    sizeof (lifr->lifr_name));
8989 			if (ipif->ipif_isv6) {
8990 				sin6 = (sin6_t *)&lifr->lifr_addr;
8991 				*sin6 = sin6_null;
8992 				sin6->sin6_family = AF_INET6;
8993 				sin6->sin6_addr =
8994 				    ipif->ipif_v6lcl_addr;
8995 				lifr->lifr_addrlen =
8996 				    ip_mask_to_plen_v6(
8997 				    &ipif->ipif_v6net_mask);
8998 			} else {
8999 				sin = (sin_t *)&lifr->lifr_addr;
9000 				*sin = sin_null;
9001 				sin->sin_family = AF_INET;
9002 				sin->sin_addr.s_addr =
9003 				    ipif->ipif_lcl_addr;
9004 				lifr->lifr_addrlen =
9005 				    ip_mask_to_plen(
9006 				    ipif->ipif_net_mask);
9007 			}
9008 			lifr++;
9009 		}
9010 	}
9011 lif_copydone:
9012 	rw_exit(&ipst->ips_ill_g_lock);
9013 
9014 	mp1->b_wptr = (uchar_t *)lifr;
9015 	if (STRUCT_BUF(lifc) != NULL) {
9016 		STRUCT_FSET(lifc, lifc_len,
9017 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9018 	}
9019 	return (0);
9020 }
9021 
9022 /* ARGSUSED */
9023 int
9024 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9025     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9026 {
9027 	ip_stack_t	*ipst;
9028 
9029 	if (q->q_next == NULL)
9030 		ipst = CONNQ_TO_IPST(q);
9031 	else
9032 		ipst = ILLQ_TO_IPST(q);
9033 
9034 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9035 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9036 	return (0);
9037 }
9038 
9039 static void
9040 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9041 {
9042 	ip6_asp_t *table;
9043 	size_t table_size;
9044 	mblk_t *data_mp;
9045 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9046 	ip_stack_t	*ipst;
9047 
9048 	if (q->q_next == NULL)
9049 		ipst = CONNQ_TO_IPST(q);
9050 	else
9051 		ipst = ILLQ_TO_IPST(q);
9052 
9053 	/* These two ioctls are I_STR only */
9054 	if (iocp->ioc_count == TRANSPARENT) {
9055 		miocnak(q, mp, 0, EINVAL);
9056 		return;
9057 	}
9058 
9059 	data_mp = mp->b_cont;
9060 	if (data_mp == NULL) {
9061 		/* The user passed us a NULL argument */
9062 		table = NULL;
9063 		table_size = iocp->ioc_count;
9064 	} else {
9065 		/*
9066 		 * The user provided a table.  The stream head
9067 		 * may have copied in the user data in chunks,
9068 		 * so make sure everything is pulled up
9069 		 * properly.
9070 		 */
9071 		if (MBLKL(data_mp) < iocp->ioc_count) {
9072 			mblk_t *new_data_mp;
9073 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9074 			    NULL) {
9075 				miocnak(q, mp, 0, ENOMEM);
9076 				return;
9077 			}
9078 			freemsg(data_mp);
9079 			data_mp = new_data_mp;
9080 			mp->b_cont = data_mp;
9081 		}
9082 		table = (ip6_asp_t *)data_mp->b_rptr;
9083 		table_size = iocp->ioc_count;
9084 	}
9085 
9086 	switch (iocp->ioc_cmd) {
9087 	case SIOCGIP6ADDRPOLICY:
9088 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9089 		if (iocp->ioc_rval == -1)
9090 			iocp->ioc_error = EINVAL;
9091 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9092 		else if (table != NULL &&
9093 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9094 			ip6_asp_t *src = table;
9095 			ip6_asp32_t *dst = (void *)table;
9096 			int count = table_size / sizeof (ip6_asp_t);
9097 			int i;
9098 
9099 			/*
9100 			 * We need to do an in-place shrink of the array
9101 			 * to match the alignment attributes of the
9102 			 * 32-bit ABI looking at it.
9103 			 */
9104 			/* LINTED: logical expression always true: op "||" */
9105 			ASSERT(sizeof (*src) > sizeof (*dst));
9106 			for (i = 1; i < count; i++)
9107 				bcopy(src + i, dst + i, sizeof (*dst));
9108 		}
9109 #endif
9110 		break;
9111 
9112 	case SIOCSIP6ADDRPOLICY:
9113 		ASSERT(mp->b_prev == NULL);
9114 		mp->b_prev = (void *)q;
9115 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9116 		/*
9117 		 * We pass in the datamodel here so that the ip6_asp_replace()
9118 		 * routine can handle converting from 32-bit to native formats
9119 		 * where necessary.
9120 		 *
9121 		 * A better way to handle this might be to convert the inbound
9122 		 * data structure here, and hang it off a new 'mp'; thus the
9123 		 * ip6_asp_replace() logic would always be dealing with native
9124 		 * format data structures..
9125 		 *
9126 		 * (An even simpler way to handle these ioctls is to just
9127 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9128 		 * and just recompile everything that depends on it.)
9129 		 */
9130 #endif
9131 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9132 		    iocp->ioc_flag & IOC_MODELS);
9133 		return;
9134 	}
9135 
9136 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9137 	qreply(q, mp);
9138 }
9139 
9140 static void
9141 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9142 {
9143 	mblk_t 		*data_mp;
9144 	struct dstinforeq	*dir;
9145 	uint8_t		*end, *cur;
9146 	in6_addr_t	*daddr, *saddr;
9147 	ipaddr_t	v4daddr;
9148 	ire_t		*ire;
9149 	char		*slabel, *dlabel;
9150 	boolean_t	isipv4;
9151 	int		match_ire;
9152 	ill_t		*dst_ill;
9153 	ipif_t		*src_ipif, *ire_ipif;
9154 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9155 	zoneid_t	zoneid;
9156 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9157 
9158 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9159 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9160 
9161 	/*
9162 	 * This ioctl is I_STR only, and must have a
9163 	 * data mblk following the M_IOCTL mblk.
9164 	 */
9165 	data_mp = mp->b_cont;
9166 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9167 		miocnak(q, mp, 0, EINVAL);
9168 		return;
9169 	}
9170 
9171 	if (MBLKL(data_mp) < iocp->ioc_count) {
9172 		mblk_t *new_data_mp;
9173 
9174 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9175 			miocnak(q, mp, 0, ENOMEM);
9176 			return;
9177 		}
9178 		freemsg(data_mp);
9179 		data_mp = new_data_mp;
9180 		mp->b_cont = data_mp;
9181 	}
9182 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9183 
9184 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9185 	    end - cur >= sizeof (struct dstinforeq);
9186 	    cur += sizeof (struct dstinforeq)) {
9187 		dir = (struct dstinforeq *)cur;
9188 		daddr = &dir->dir_daddr;
9189 		saddr = &dir->dir_saddr;
9190 
9191 		/*
9192 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9193 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9194 		 * and ipif_select_source[_v6]() do not.
9195 		 */
9196 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9197 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9198 
9199 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9200 		if (isipv4) {
9201 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9202 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9203 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9204 		} else {
9205 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9206 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9207 		}
9208 		if (ire == NULL) {
9209 			dir->dir_dreachable = 0;
9210 
9211 			/* move on to next dst addr */
9212 			continue;
9213 		}
9214 		dir->dir_dreachable = 1;
9215 
9216 		ire_ipif = ire->ire_ipif;
9217 		if (ire_ipif == NULL)
9218 			goto next_dst;
9219 
9220 		/*
9221 		 * We expect to get back an interface ire or a
9222 		 * gateway ire cache entry.  For both types, the
9223 		 * output interface is ire_ipif->ipif_ill.
9224 		 */
9225 		dst_ill = ire_ipif->ipif_ill;
9226 		dir->dir_dmactype = dst_ill->ill_mactype;
9227 
9228 		if (isipv4) {
9229 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9230 		} else {
9231 			src_ipif = ipif_select_source_v6(dst_ill,
9232 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9233 			    zoneid);
9234 		}
9235 		if (src_ipif == NULL)
9236 			goto next_dst;
9237 
9238 		*saddr = src_ipif->ipif_v6lcl_addr;
9239 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9240 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9241 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9242 		dir->dir_sdeprecated =
9243 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9244 		ipif_refrele(src_ipif);
9245 next_dst:
9246 		ire_refrele(ire);
9247 	}
9248 	miocack(q, mp, iocp->ioc_count, 0);
9249 }
9250 
9251 
9252 /*
9253  * Check if this is an address assigned to this machine.
9254  * Skips interfaces that are down by using ire checks.
9255  * Translates mapped addresses to v4 addresses and then
9256  * treats them as such, returning true if the v4 address
9257  * associated with this mapped address is configured.
9258  * Note: Applications will have to be careful what they do
9259  * with the response; use of mapped addresses limits
9260  * what can be done with the socket, especially with
9261  * respect to socket options and ioctls - neither IPv4
9262  * options nor IPv6 sticky options/ancillary data options
9263  * may be used.
9264  */
9265 /* ARGSUSED */
9266 int
9267 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9268     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9269 {
9270 	struct sioc_addrreq *sia;
9271 	sin_t *sin;
9272 	ire_t *ire;
9273 	mblk_t *mp1;
9274 	zoneid_t zoneid;
9275 	ip_stack_t	*ipst;
9276 
9277 	ip1dbg(("ip_sioctl_tmyaddr"));
9278 
9279 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9280 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9281 	ipst = CONNQ_TO_IPST(q);
9282 
9283 	/* Existence verified in ip_wput_nondata */
9284 	mp1 = mp->b_cont->b_cont;
9285 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9286 	sin = (sin_t *)&sia->sa_addr;
9287 	switch (sin->sin_family) {
9288 	case AF_INET6: {
9289 		sin6_t *sin6 = (sin6_t *)sin;
9290 
9291 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9292 			ipaddr_t v4_addr;
9293 
9294 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9295 			    v4_addr);
9296 			ire = ire_ctable_lookup(v4_addr, 0,
9297 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9298 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9299 		} else {
9300 			in6_addr_t v6addr;
9301 
9302 			v6addr = sin6->sin6_addr;
9303 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9304 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9305 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9306 		}
9307 		break;
9308 	}
9309 	case AF_INET: {
9310 		ipaddr_t v4addr;
9311 
9312 		v4addr = sin->sin_addr.s_addr;
9313 		ire = ire_ctable_lookup(v4addr, 0,
9314 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9315 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9316 		break;
9317 	}
9318 	default:
9319 		return (EAFNOSUPPORT);
9320 	}
9321 	if (ire != NULL) {
9322 		sia->sa_res = 1;
9323 		ire_refrele(ire);
9324 	} else {
9325 		sia->sa_res = 0;
9326 	}
9327 	return (0);
9328 }
9329 
9330 /*
9331  * Check if this is an address assigned on-link i.e. neighbor,
9332  * and makes sure it's reachable from the current zone.
9333  * Returns true for my addresses as well.
9334  * Translates mapped addresses to v4 addresses and then
9335  * treats them as such, returning true if the v4 address
9336  * associated with this mapped address is configured.
9337  * Note: Applications will have to be careful what they do
9338  * with the response; use of mapped addresses limits
9339  * what can be done with the socket, especially with
9340  * respect to socket options and ioctls - neither IPv4
9341  * options nor IPv6 sticky options/ancillary data options
9342  * may be used.
9343  */
9344 /* ARGSUSED */
9345 int
9346 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9347     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9348 {
9349 	struct sioc_addrreq *sia;
9350 	sin_t *sin;
9351 	mblk_t	*mp1;
9352 	ire_t *ire = NULL;
9353 	zoneid_t zoneid;
9354 	ip_stack_t	*ipst;
9355 
9356 	ip1dbg(("ip_sioctl_tonlink"));
9357 
9358 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9359 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9360 	ipst = CONNQ_TO_IPST(q);
9361 
9362 	/* Existence verified in ip_wput_nondata */
9363 	mp1 = mp->b_cont->b_cont;
9364 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9365 	sin = (sin_t *)&sia->sa_addr;
9366 
9367 	/*
9368 	 * Match addresses with a zero gateway field to avoid
9369 	 * routes going through a router.
9370 	 * Exclude broadcast and multicast addresses.
9371 	 */
9372 	switch (sin->sin_family) {
9373 	case AF_INET6: {
9374 		sin6_t *sin6 = (sin6_t *)sin;
9375 
9376 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9377 			ipaddr_t v4_addr;
9378 
9379 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9380 			    v4_addr);
9381 			if (!CLASSD(v4_addr)) {
9382 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9383 				    NULL, NULL, zoneid, NULL,
9384 				    MATCH_IRE_GW, ipst);
9385 			}
9386 		} else {
9387 			in6_addr_t v6addr;
9388 			in6_addr_t v6gw;
9389 
9390 			v6addr = sin6->sin6_addr;
9391 			v6gw = ipv6_all_zeros;
9392 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9393 				ire = ire_route_lookup_v6(&v6addr, 0,
9394 				    &v6gw, 0, NULL, NULL, zoneid,
9395 				    NULL, MATCH_IRE_GW, ipst);
9396 			}
9397 		}
9398 		break;
9399 	}
9400 	case AF_INET: {
9401 		ipaddr_t v4addr;
9402 
9403 		v4addr = sin->sin_addr.s_addr;
9404 		if (!CLASSD(v4addr)) {
9405 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9406 			    NULL, NULL, zoneid, NULL,
9407 			    MATCH_IRE_GW, ipst);
9408 		}
9409 		break;
9410 	}
9411 	default:
9412 		return (EAFNOSUPPORT);
9413 	}
9414 	sia->sa_res = 0;
9415 	if (ire != NULL) {
9416 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9417 		    IRE_LOCAL|IRE_LOOPBACK)) {
9418 			sia->sa_res = 1;
9419 		}
9420 		ire_refrele(ire);
9421 	}
9422 	return (0);
9423 }
9424 
9425 /*
9426  * TBD: implement when kernel maintaines a list of site prefixes.
9427  */
9428 /* ARGSUSED */
9429 int
9430 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9431     ip_ioctl_cmd_t *ipip, void *ifreq)
9432 {
9433 	return (ENXIO);
9434 }
9435 
9436 /* ARGSUSED */
9437 int
9438 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9439     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9440 {
9441 	ill_t  		*ill;
9442 	mblk_t		*mp1;
9443 	conn_t		*connp;
9444 	boolean_t	success;
9445 
9446 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9447 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9448 	/* ioctl comes down on an conn */
9449 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9450 	connp = Q_TO_CONN(q);
9451 
9452 	mp->b_datap->db_type = M_IOCTL;
9453 
9454 	/*
9455 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9456 	 * The original mp contains contaminated b_next values due to 'mi',
9457 	 * which is needed to do the mi_copy_done. Unfortunately if we
9458 	 * send down the original mblk itself and if we are popped due to an
9459 	 * an unplumb before the response comes back from tunnel,
9460 	 * the streamhead (which does a freemsg) will see this contaminated
9461 	 * message and the assertion in freemsg about non-null b_next/b_prev
9462 	 * will panic a DEBUG kernel.
9463 	 */
9464 	mp1 = copymsg(mp);
9465 	if (mp1 == NULL)
9466 		return (ENOMEM);
9467 
9468 	ill = ipif->ipif_ill;
9469 	mutex_enter(&connp->conn_lock);
9470 	mutex_enter(&ill->ill_lock);
9471 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9472 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9473 		    mp, 0);
9474 	} else {
9475 		success = ill_pending_mp_add(ill, connp, mp);
9476 	}
9477 	mutex_exit(&ill->ill_lock);
9478 	mutex_exit(&connp->conn_lock);
9479 
9480 	if (success) {
9481 		ip1dbg(("sending down tunparam request "));
9482 		putnext(ill->ill_wq, mp1);
9483 		return (EINPROGRESS);
9484 	} else {
9485 		/* The conn has started closing */
9486 		freemsg(mp1);
9487 		return (EINTR);
9488 	}
9489 }
9490 
9491 /*
9492  * ARP IOCTLs.
9493  * How does IP get in the business of fronting ARP configuration/queries?
9494  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9495  * are by tradition passed in through a datagram socket.  That lands in IP.
9496  * As it happens, this is just as well since the interface is quite crude in
9497  * that it passes in no information about protocol or hardware types, or
9498  * interface association.  After making the protocol assumption, IP is in
9499  * the position to look up the name of the ILL, which ARP will need, and
9500  * format a request that can be handled by ARP.  The request is passed up
9501  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9502  * back a response.  ARP supports its own set of more general IOCTLs, in
9503  * case anyone is interested.
9504  */
9505 /* ARGSUSED */
9506 int
9507 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9508     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9509 {
9510 	mblk_t *mp1;
9511 	mblk_t *mp2;
9512 	mblk_t *pending_mp;
9513 	ipaddr_t ipaddr;
9514 	area_t *area;
9515 	struct iocblk *iocp;
9516 	conn_t *connp;
9517 	struct arpreq *ar;
9518 	struct xarpreq *xar;
9519 	int flags, alength;
9520 	char *lladdr;
9521 	ip_stack_t	*ipst;
9522 	ill_t *ill = ipif->ipif_ill;
9523 	boolean_t if_arp_ioctl = B_FALSE;
9524 
9525 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9526 	connp = Q_TO_CONN(q);
9527 	ipst = connp->conn_netstack->netstack_ip;
9528 
9529 	if (ipip->ipi_cmd_type == XARP_CMD) {
9530 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9531 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9532 		ar = NULL;
9533 
9534 		flags = xar->xarp_flags;
9535 		lladdr = LLADDR(&xar->xarp_ha);
9536 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9537 		/*
9538 		 * Validate against user's link layer address length
9539 		 * input and name and addr length limits.
9540 		 */
9541 		alength = ill->ill_phys_addr_length;
9542 		if (ipip->ipi_cmd == SIOCSXARP) {
9543 			if (alength != xar->xarp_ha.sdl_alen ||
9544 			    (alength + xar->xarp_ha.sdl_nlen >
9545 			    sizeof (xar->xarp_ha.sdl_data)))
9546 				return (EINVAL);
9547 		}
9548 	} else {
9549 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9550 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9551 		xar = NULL;
9552 
9553 		flags = ar->arp_flags;
9554 		lladdr = ar->arp_ha.sa_data;
9555 		/*
9556 		 * Theoretically, the sa_family could tell us what link
9557 		 * layer type this operation is trying to deal with. By
9558 		 * common usage AF_UNSPEC means ethernet. We'll assume
9559 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9560 		 * for now. Our new SIOC*XARP ioctls can be used more
9561 		 * generally.
9562 		 *
9563 		 * If the underlying media happens to have a non 6 byte
9564 		 * address, arp module will fail set/get, but the del
9565 		 * operation will succeed.
9566 		 */
9567 		alength = 6;
9568 		if ((ipip->ipi_cmd != SIOCDARP) &&
9569 		    (alength != ill->ill_phys_addr_length)) {
9570 			return (EINVAL);
9571 		}
9572 	}
9573 
9574 	/*
9575 	 * We are going to pass up to ARP a packet chain that looks
9576 	 * like:
9577 	 *
9578 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9579 	 *
9580 	 * Get a copy of the original IOCTL mblk to head the chain,
9581 	 * to be sent up (in mp1). Also get another copy to store
9582 	 * in the ill_pending_mp list, for matching the response
9583 	 * when it comes back from ARP.
9584 	 */
9585 	mp1 = copyb(mp);
9586 	pending_mp = copymsg(mp);
9587 	if (mp1 == NULL || pending_mp == NULL) {
9588 		if (mp1 != NULL)
9589 			freeb(mp1);
9590 		if (pending_mp != NULL)
9591 			inet_freemsg(pending_mp);
9592 		return (ENOMEM);
9593 	}
9594 
9595 	ipaddr = sin->sin_addr.s_addr;
9596 
9597 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9598 	    (caddr_t)&ipaddr);
9599 	if (mp2 == NULL) {
9600 		freeb(mp1);
9601 		inet_freemsg(pending_mp);
9602 		return (ENOMEM);
9603 	}
9604 	/* Put together the chain. */
9605 	mp1->b_cont = mp2;
9606 	mp1->b_datap->db_type = M_IOCTL;
9607 	mp2->b_cont = mp;
9608 	mp2->b_datap->db_type = M_DATA;
9609 
9610 	iocp = (struct iocblk *)mp1->b_rptr;
9611 
9612 	/*
9613 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9614 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9615 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9616 	 * ioc_count field; set ioc_count to be correct.
9617 	 */
9618 	iocp->ioc_count = MBLKL(mp1->b_cont);
9619 
9620 	/*
9621 	 * Set the proper command in the ARP message.
9622 	 * Convert the SIOC{G|S|D}ARP calls into our
9623 	 * AR_ENTRY_xxx calls.
9624 	 */
9625 	area = (area_t *)mp2->b_rptr;
9626 	switch (iocp->ioc_cmd) {
9627 	case SIOCDARP:
9628 	case SIOCDXARP:
9629 		/*
9630 		 * We defer deleting the corresponding IRE until
9631 		 * we return from arp.
9632 		 */
9633 		area->area_cmd = AR_ENTRY_DELETE;
9634 		area->area_proto_mask_offset = 0;
9635 		break;
9636 	case SIOCGARP:
9637 	case SIOCGXARP:
9638 		area->area_cmd = AR_ENTRY_SQUERY;
9639 		area->area_proto_mask_offset = 0;
9640 		break;
9641 	case SIOCSARP:
9642 	case SIOCSXARP:
9643 		/*
9644 		 * Delete the corresponding ire to make sure IP will
9645 		 * pick up any change from arp.
9646 		 */
9647 		if (!if_arp_ioctl) {
9648 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9649 		} else {
9650 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9651 			if (ipif != NULL) {
9652 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9653 				    ipst);
9654 				ipif_refrele(ipif);
9655 			}
9656 		}
9657 		break;
9658 	}
9659 	iocp->ioc_cmd = area->area_cmd;
9660 
9661 	/*
9662 	 * Fill in the rest of the ARP operation fields.
9663 	 */
9664 	area->area_hw_addr_length = alength;
9665 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9666 
9667 	/* Translate the flags. */
9668 	if (flags & ATF_PERM)
9669 		area->area_flags |= ACE_F_PERMANENT;
9670 	if (flags & ATF_PUBL)
9671 		area->area_flags |= ACE_F_PUBLISH;
9672 	if (flags & ATF_AUTHORITY)
9673 		area->area_flags |= ACE_F_AUTHORITY;
9674 
9675 	/*
9676 	 * Before sending 'mp' to ARP, we have to clear the b_next
9677 	 * and b_prev. Otherwise if STREAMS encounters such a message
9678 	 * in freemsg(), (because ARP can close any time) it can cause
9679 	 * a panic. But mi code needs the b_next and b_prev values of
9680 	 * mp->b_cont, to complete the ioctl. So we store it here
9681 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9682 	 * when the response comes down from ARP.
9683 	 */
9684 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9685 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9686 	mp->b_cont->b_next = NULL;
9687 	mp->b_cont->b_prev = NULL;
9688 
9689 	mutex_enter(&connp->conn_lock);
9690 	mutex_enter(&ill->ill_lock);
9691 	/* conn has not yet started closing, hence this can't fail */
9692 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9693 	mutex_exit(&ill->ill_lock);
9694 	mutex_exit(&connp->conn_lock);
9695 
9696 	/*
9697 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9698 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9699 	 */
9700 	putnext(ill->ill_rq, mp1);
9701 	return (EINPROGRESS);
9702 }
9703 
9704 /*
9705  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9706  * the associated sin and refhold and return the associated ipif via `ci'.
9707  */
9708 int
9709 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9710     cmd_info_t *ci, ipsq_func_t func)
9711 {
9712 	mblk_t	*mp1;
9713 	int	err;
9714 	sin_t	*sin;
9715 	conn_t	*connp;
9716 	ipif_t	*ipif;
9717 	ire_t	*ire = NULL;
9718 	ill_t	*ill = NULL;
9719 	boolean_t exists;
9720 	ip_stack_t *ipst;
9721 	struct arpreq *ar;
9722 	struct xarpreq *xar;
9723 	struct sockaddr_dl *sdl;
9724 
9725 	/* ioctl comes down on a conn */
9726 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9727 	connp = Q_TO_CONN(q);
9728 	if (connp->conn_af_isv6)
9729 		return (ENXIO);
9730 
9731 	ipst = connp->conn_netstack->netstack_ip;
9732 
9733 	/* Verified in ip_wput_nondata */
9734 	mp1 = mp->b_cont->b_cont;
9735 
9736 	if (ipip->ipi_cmd_type == XARP_CMD) {
9737 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9738 		xar = (struct xarpreq *)mp1->b_rptr;
9739 		sin = (sin_t *)&xar->xarp_pa;
9740 		sdl = &xar->xarp_ha;
9741 
9742 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9743 			return (ENXIO);
9744 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9745 			return (EINVAL);
9746 	} else {
9747 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9748 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9749 		ar = (struct arpreq *)mp1->b_rptr;
9750 		sin = (sin_t *)&ar->arp_pa;
9751 	}
9752 
9753 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9754 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9755 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9756 		    mp, func, &err, ipst);
9757 		if (ipif == NULL)
9758 			return (err);
9759 		if (ipif->ipif_id != 0 ||
9760 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9761 			ipif_refrele(ipif);
9762 			return (ENXIO);
9763 		}
9764 	} else {
9765 		/*
9766 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9767 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9768 		 * case, a simple forwarding table lookup will return the
9769 		 * IRE_IF_RESOLVER for the first interface in the group, which
9770 		 * might not be the interface on which the requested IP
9771 		 * address was resolved due to the ill selection algorithm
9772 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9773 		 * lookup first: if the IRE cache entry for the IP address is
9774 		 * still there, it will contain the ill pointer for the right
9775 		 * interface, so we use that. If the cache entry has been
9776 		 * flushed, we fall back to the forwarding table lookup. This
9777 		 * should be rare enough since IRE cache entries have a longer
9778 		 * life expectancy than ARP cache entries.
9779 		 */
9780 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9781 		    ipst);
9782 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9783 		    ((ill = ire_to_ill(ire)) == NULL) ||
9784 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9785 			if (ire != NULL)
9786 				ire_refrele(ire);
9787 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9788 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9789 			    NULL, MATCH_IRE_TYPE, ipst);
9790 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9791 
9792 				if (ire != NULL)
9793 					ire_refrele(ire);
9794 				return (ENXIO);
9795 			}
9796 		}
9797 		ASSERT(ire != NULL && ill != NULL);
9798 		ipif = ill->ill_ipif;
9799 		ipif_refhold(ipif);
9800 		ire_refrele(ire);
9801 	}
9802 	ci->ci_sin = sin;
9803 	ci->ci_ipif = ipif;
9804 	return (0);
9805 }
9806 
9807 /*
9808  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9809  * atomically set/clear the muxids. Also complete the ioctl by acking or
9810  * naking it.  Note that the code is structured such that the link type,
9811  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9812  * its clones use the persistent link, while pppd(1M) and perhaps many
9813  * other daemons may use non-persistent link.  When combined with some
9814  * ill_t states, linking and unlinking lower streams may be used as
9815  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9816  */
9817 /* ARGSUSED */
9818 void
9819 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9820 {
9821 	mblk_t		*mp1, *mp2;
9822 	struct linkblk	*li;
9823 	struct ipmx_s	*ipmxp;
9824 	ill_t		*ill;
9825 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9826 	int		err = 0;
9827 	boolean_t	entered_ipsq = B_FALSE;
9828 	boolean_t	islink;
9829 	ip_stack_t	*ipst;
9830 
9831 	if (CONN_Q(q))
9832 		ipst = CONNQ_TO_IPST(q);
9833 	else
9834 		ipst = ILLQ_TO_IPST(q);
9835 
9836 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9837 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9838 
9839 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9840 
9841 	mp1 = mp->b_cont;	/* This is the linkblk info */
9842 	li = (struct linkblk *)mp1->b_rptr;
9843 
9844 	/*
9845 	 * ARP has added this special mblk, and the utility is asking us
9846 	 * to perform consistency checks, and also atomically set the
9847 	 * muxid. Ifconfig is an example.  It achieves this by using
9848 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9849 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9850 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9851 	 * and other comments in this routine for more details.
9852 	 */
9853 	mp2 = mp1->b_cont;	/* This is added by ARP */
9854 
9855 	/*
9856 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9857 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9858 	 * get the special mblk above.  For backward compatibility, we
9859 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9860 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9861 	 * not atomic, and can leave the streams unplumbable if the utility
9862 	 * is interrupted before it does the SIOCSLIFMUXID.
9863 	 */
9864 	if (mp2 == NULL) {
9865 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9866 		if (err == EINPROGRESS)
9867 			return;
9868 		goto done;
9869 	}
9870 
9871 	/*
9872 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9873 	 * ARP has appended this last mblk to tell us whether the lower stream
9874 	 * is an arp-dev stream or an IP module stream.
9875 	 */
9876 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9877 	if (ipmxp->ipmx_arpdev_stream) {
9878 		/*
9879 		 * The lower stream is the arp-dev stream.
9880 		 */
9881 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9882 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9883 		if (ill == NULL) {
9884 			if (err == EINPROGRESS)
9885 				return;
9886 			err = EINVAL;
9887 			goto done;
9888 		}
9889 
9890 		if (ipsq == NULL) {
9891 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9892 			    NEW_OP, B_TRUE);
9893 			if (ipsq == NULL) {
9894 				ill_refrele(ill);
9895 				return;
9896 			}
9897 			entered_ipsq = B_TRUE;
9898 		}
9899 		ASSERT(IAM_WRITER_ILL(ill));
9900 		ill_refrele(ill);
9901 
9902 		/*
9903 		 * To ensure consistency between IP and ARP, the following
9904 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9905 		 * This is because the muxid's are stored in the IP stream on
9906 		 * the ill.
9907 		 *
9908 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9909 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9910 		 * not yet plinked, and it also checks that the corresponding
9911 		 * IP stream is already plinked.
9912 		 *
9913 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9914 		 * punlinking the IP stream. IP does not allow punlink of the
9915 		 * IP stream unless the arp stream has been punlinked.
9916 		 */
9917 		if ((islink &&
9918 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9919 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9920 			err = EINVAL;
9921 			goto done;
9922 		}
9923 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9924 	} else {
9925 		/*
9926 		 * The lower stream is probably an IP module stream.  Do
9927 		 * consistency checking.
9928 		 */
9929 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9930 		if (err == EINPROGRESS)
9931 			return;
9932 	}
9933 done:
9934 	if (err == 0)
9935 		miocack(q, mp, 0, 0);
9936 	else
9937 		miocnak(q, mp, 0, err);
9938 
9939 	/* Conn was refheld in ip_sioctl_copyin_setup */
9940 	if (CONN_Q(q))
9941 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9942 	if (entered_ipsq)
9943 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9944 }
9945 
9946 /*
9947  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9948  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9949  * module stream).  If `doconsist' is set, then do the extended consistency
9950  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9951  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9952  * an error code on failure.
9953  */
9954 static int
9955 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9956     struct linkblk *li, boolean_t doconsist)
9957 {
9958 	ill_t  		*ill;
9959 	queue_t		*ipwq, *dwq;
9960 	const char	*name;
9961 	struct qinit	*qinfo;
9962 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9963 	boolean_t	entered_ipsq = B_FALSE;
9964 
9965 	/*
9966 	 * Walk the lower stream to verify it's the IP module stream.
9967 	 * The IP module is identified by its name, wput function,
9968 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9969 	 * (li->l_qbot) will not vanish until this ioctl completes.
9970 	 */
9971 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9972 		qinfo = ipwq->q_qinfo;
9973 		name = qinfo->qi_minfo->mi_idname;
9974 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9975 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9976 			break;
9977 		}
9978 	}
9979 
9980 	/*
9981 	 * If this isn't an IP module stream, bail.
9982 	 */
9983 	if (ipwq == NULL)
9984 		return (0);
9985 
9986 	ill = ipwq->q_ptr;
9987 	ASSERT(ill != NULL);
9988 
9989 	if (ipsq == NULL) {
9990 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9991 		    NEW_OP, B_TRUE);
9992 		if (ipsq == NULL)
9993 			return (EINPROGRESS);
9994 		entered_ipsq = B_TRUE;
9995 	}
9996 	ASSERT(IAM_WRITER_ILL(ill));
9997 
9998 	if (doconsist) {
9999 		/*
10000 		 * Consistency checking requires that I_{P}LINK occurs
10001 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10002 		 * occurs prior to clearing ill_arp_muxid.
10003 		 */
10004 		if ((islink && ill->ill_ip_muxid != 0) ||
10005 		    (!islink && ill->ill_arp_muxid != 0)) {
10006 			if (entered_ipsq)
10007 				ipsq_exit(ipsq, B_TRUE, B_TRUE);
10008 			return (EINVAL);
10009 		}
10010 	}
10011 
10012 	/*
10013 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10014 	 * the read queue of the module immediately below IP in the ill.
10015 	 * These are used during the capability negotiation below.
10016 	 */
10017 	ill->ill_lmod_rq = NULL;
10018 	ill->ill_lmod_cnt = 0;
10019 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10020 		ill->ill_lmod_rq = RD(dwq);
10021 		for (; dwq != NULL; dwq = dwq->q_next)
10022 			ill->ill_lmod_cnt++;
10023 	}
10024 
10025 	if (doconsist)
10026 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10027 
10028 	/*
10029 	 * If there's at least one up ipif on this ill, then we're bound to
10030 	 * the underlying driver via DLPI.  In that case, renegotiate
10031 	 * capabilities to account for any possible change in modules
10032 	 * interposed between IP and the driver.
10033 	 */
10034 	if (ill->ill_ipif_up_count > 0) {
10035 		if (islink)
10036 			ill_capability_probe(ill);
10037 		else
10038 			ill_capability_reset(ill);
10039 	}
10040 
10041 	if (entered_ipsq)
10042 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10043 
10044 	return (0);
10045 }
10046 
10047 /*
10048  * Search the ioctl command in the ioctl tables and return a pointer
10049  * to the ioctl command information. The ioctl command tables are
10050  * static and fully populated at compile time.
10051  */
10052 ip_ioctl_cmd_t *
10053 ip_sioctl_lookup(int ioc_cmd)
10054 {
10055 	int index;
10056 	ip_ioctl_cmd_t *ipip;
10057 	ip_ioctl_cmd_t *ipip_end;
10058 
10059 	if (ioc_cmd == IPI_DONTCARE)
10060 		return (NULL);
10061 
10062 	/*
10063 	 * Do a 2 step search. First search the indexed table
10064 	 * based on the least significant byte of the ioctl cmd.
10065 	 * If we don't find a match, then search the misc table
10066 	 * serially.
10067 	 */
10068 	index = ioc_cmd & 0xFF;
10069 	if (index < ip_ndx_ioctl_count) {
10070 		ipip = &ip_ndx_ioctl_table[index];
10071 		if (ipip->ipi_cmd == ioc_cmd) {
10072 			/* Found a match in the ndx table */
10073 			return (ipip);
10074 		}
10075 	}
10076 
10077 	/* Search the misc table */
10078 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10079 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10080 		if (ipip->ipi_cmd == ioc_cmd)
10081 			/* Found a match in the misc table */
10082 			return (ipip);
10083 	}
10084 
10085 	return (NULL);
10086 }
10087 
10088 /*
10089  * Wrapper function for resuming deferred ioctl processing
10090  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10091  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10092  */
10093 /* ARGSUSED */
10094 void
10095 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10096     void *dummy_arg)
10097 {
10098 	ip_sioctl_copyin_setup(q, mp);
10099 }
10100 
10101 /*
10102  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10103  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10104  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10105  * We establish here the size of the block to be copied in.  mi_copyin
10106  * arranges for this to happen, an processing continues in ip_wput with
10107  * an M_IOCDATA message.
10108  */
10109 void
10110 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10111 {
10112 	int	copyin_size;
10113 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10114 	ip_ioctl_cmd_t *ipip;
10115 	cred_t *cr;
10116 	ip_stack_t	*ipst;
10117 
10118 	if (CONN_Q(q))
10119 		ipst = CONNQ_TO_IPST(q);
10120 	else
10121 		ipst = ILLQ_TO_IPST(q);
10122 
10123 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10124 	if (ipip == NULL) {
10125 		/*
10126 		 * The ioctl is not one we understand or own.
10127 		 * Pass it along to be processed down stream,
10128 		 * if this is a module instance of IP, else nak
10129 		 * the ioctl.
10130 		 */
10131 		if (q->q_next == NULL) {
10132 			goto nak;
10133 		} else {
10134 			putnext(q, mp);
10135 			return;
10136 		}
10137 	}
10138 
10139 	/*
10140 	 * If this is deferred, then we will do all the checks when we
10141 	 * come back.
10142 	 */
10143 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10144 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10145 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10146 		return;
10147 	}
10148 
10149 	/*
10150 	 * Only allow a very small subset of IP ioctls on this stream if
10151 	 * IP is a module and not a driver. Allowing ioctls to be processed
10152 	 * in this case may cause assert failures or data corruption.
10153 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10154 	 * ioctls allowed on an IP module stream, after which this stream
10155 	 * normally becomes a multiplexor (at which time the stream head
10156 	 * will fail all ioctls).
10157 	 */
10158 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10159 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10160 			/*
10161 			 * Pass common Streams ioctls which the IP
10162 			 * module does not own or consume along to
10163 			 * be processed down stream.
10164 			 */
10165 			putnext(q, mp);
10166 			return;
10167 		} else {
10168 			goto nak;
10169 		}
10170 	}
10171 
10172 	/* Make sure we have ioctl data to process. */
10173 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10174 		goto nak;
10175 
10176 	/*
10177 	 * Prefer dblk credential over ioctl credential; some synthesized
10178 	 * ioctls have kcred set because there's no way to crhold()
10179 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10180 	 * the framework; the caller of ioctl needs to hold the reference
10181 	 * for the duration of the call).
10182 	 */
10183 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10184 
10185 	/* Make sure normal users don't send down privileged ioctls */
10186 	if ((ipip->ipi_flags & IPI_PRIV) &&
10187 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10188 		/* We checked the privilege earlier but log it here */
10189 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10190 		return;
10191 	}
10192 
10193 	/*
10194 	 * The ioctl command tables can only encode fixed length
10195 	 * ioctl data. If the length is variable, the table will
10196 	 * encode the length as zero. Such special cases are handled
10197 	 * below in the switch.
10198 	 */
10199 	if (ipip->ipi_copyin_size != 0) {
10200 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10201 		return;
10202 	}
10203 
10204 	switch (iocp->ioc_cmd) {
10205 	case O_SIOCGIFCONF:
10206 	case SIOCGIFCONF:
10207 		/*
10208 		 * This IOCTL is hilarious.  See comments in
10209 		 * ip_sioctl_get_ifconf for the story.
10210 		 */
10211 		if (iocp->ioc_count == TRANSPARENT)
10212 			copyin_size = SIZEOF_STRUCT(ifconf,
10213 			    iocp->ioc_flag);
10214 		else
10215 			copyin_size = iocp->ioc_count;
10216 		mi_copyin(q, mp, NULL, copyin_size);
10217 		return;
10218 
10219 	case O_SIOCGLIFCONF:
10220 	case SIOCGLIFCONF:
10221 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10222 		mi_copyin(q, mp, NULL, copyin_size);
10223 		return;
10224 
10225 	case SIOCGLIFSRCOF:
10226 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10227 		mi_copyin(q, mp, NULL, copyin_size);
10228 		return;
10229 	case SIOCGIP6ADDRPOLICY:
10230 		ip_sioctl_ip6addrpolicy(q, mp);
10231 		ip6_asp_table_refrele(ipst);
10232 		return;
10233 
10234 	case SIOCSIP6ADDRPOLICY:
10235 		ip_sioctl_ip6addrpolicy(q, mp);
10236 		return;
10237 
10238 	case SIOCGDSTINFO:
10239 		ip_sioctl_dstinfo(q, mp);
10240 		ip6_asp_table_refrele(ipst);
10241 		return;
10242 
10243 	case I_PLINK:
10244 	case I_PUNLINK:
10245 	case I_LINK:
10246 	case I_UNLINK:
10247 		/*
10248 		 * We treat non-persistent link similarly as the persistent
10249 		 * link case, in terms of plumbing/unplumbing, as well as
10250 		 * dynamic re-plumbing events indicator.  See comments
10251 		 * in ip_sioctl_plink() for more.
10252 		 *
10253 		 * Request can be enqueued in the 'ipsq' while waiting
10254 		 * to become exclusive. So bump up the conn ref.
10255 		 */
10256 		if (CONN_Q(q))
10257 			CONN_INC_REF(Q_TO_CONN(q));
10258 		ip_sioctl_plink(NULL, q, mp, NULL);
10259 		return;
10260 
10261 	case ND_GET:
10262 	case ND_SET:
10263 		/*
10264 		 * Use of the nd table requires holding the reader lock.
10265 		 * Modifying the nd table thru nd_load/nd_unload requires
10266 		 * the writer lock.
10267 		 */
10268 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10269 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10270 			rw_exit(&ipst->ips_ip_g_nd_lock);
10271 
10272 			if (iocp->ioc_error)
10273 				iocp->ioc_count = 0;
10274 			mp->b_datap->db_type = M_IOCACK;
10275 			qreply(q, mp);
10276 			return;
10277 		}
10278 		rw_exit(&ipst->ips_ip_g_nd_lock);
10279 		/*
10280 		 * We don't understand this subioctl of ND_GET / ND_SET.
10281 		 * Maybe intended for some driver / module below us
10282 		 */
10283 		if (q->q_next) {
10284 			putnext(q, mp);
10285 		} else {
10286 			iocp->ioc_error = ENOENT;
10287 			mp->b_datap->db_type = M_IOCNAK;
10288 			iocp->ioc_count = 0;
10289 			qreply(q, mp);
10290 		}
10291 		return;
10292 
10293 	case IP_IOCTL:
10294 		ip_wput_ioctl(q, mp);
10295 		return;
10296 	default:
10297 		cmn_err(CE_PANIC, "should not happen ");
10298 	}
10299 nak:
10300 	if (mp->b_cont != NULL) {
10301 		freemsg(mp->b_cont);
10302 		mp->b_cont = NULL;
10303 	}
10304 	iocp->ioc_error = EINVAL;
10305 	mp->b_datap->db_type = M_IOCNAK;
10306 	iocp->ioc_count = 0;
10307 	qreply(q, mp);
10308 }
10309 
10310 /* ip_wput hands off ARP IOCTL responses to us */
10311 void
10312 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10313 {
10314 	struct arpreq *ar;
10315 	struct xarpreq *xar;
10316 	area_t	*area;
10317 	mblk_t	*area_mp;
10318 	struct iocblk *iocp;
10319 	mblk_t	*orig_ioc_mp, *tmp;
10320 	struct iocblk	*orig_iocp;
10321 	ill_t *ill;
10322 	conn_t *connp = NULL;
10323 	uint_t ioc_id;
10324 	mblk_t *pending_mp;
10325 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10326 	int *flagsp;
10327 	char *storage = NULL;
10328 	sin_t *sin;
10329 	ipaddr_t addr;
10330 	int err;
10331 	ip_stack_t *ipst;
10332 
10333 	ill = q->q_ptr;
10334 	ASSERT(ill != NULL);
10335 	ipst = ill->ill_ipst;
10336 
10337 	/*
10338 	 * We should get back from ARP a packet chain that looks like:
10339 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10340 	 */
10341 	if (!(area_mp = mp->b_cont) ||
10342 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10343 	    !(orig_ioc_mp = area_mp->b_cont) ||
10344 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10345 		freemsg(mp);
10346 		return;
10347 	}
10348 
10349 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10350 
10351 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10352 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10353 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10354 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10355 		x_arp_ioctl = B_TRUE;
10356 		xar = (struct xarpreq *)tmp->b_rptr;
10357 		sin = (sin_t *)&xar->xarp_pa;
10358 		flagsp = &xar->xarp_flags;
10359 		storage = xar->xarp_ha.sdl_data;
10360 		if (xar->xarp_ha.sdl_nlen != 0)
10361 			ifx_arp_ioctl = B_TRUE;
10362 	} else {
10363 		ar = (struct arpreq *)tmp->b_rptr;
10364 		sin = (sin_t *)&ar->arp_pa;
10365 		flagsp = &ar->arp_flags;
10366 		storage = ar->arp_ha.sa_data;
10367 	}
10368 
10369 	iocp = (struct iocblk *)mp->b_rptr;
10370 
10371 	/*
10372 	 * Pick out the originating queue based on the ioc_id.
10373 	 */
10374 	ioc_id = iocp->ioc_id;
10375 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10376 	if (pending_mp == NULL) {
10377 		ASSERT(connp == NULL);
10378 		inet_freemsg(mp);
10379 		return;
10380 	}
10381 	ASSERT(connp != NULL);
10382 	q = CONNP_TO_WQ(connp);
10383 
10384 	/* Uncouple the internally generated IOCTL from the original one */
10385 	area = (area_t *)area_mp->b_rptr;
10386 	area_mp->b_cont = NULL;
10387 
10388 	/*
10389 	 * Restore the b_next and b_prev used by mi code. This is needed
10390 	 * to complete the ioctl using mi* functions. We stored them in
10391 	 * the pending mp prior to sending the request to ARP.
10392 	 */
10393 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10394 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10395 	inet_freemsg(pending_mp);
10396 
10397 	/*
10398 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10399 	 * Catch the case where there is an IRE_CACHE by no entry in the
10400 	 * arp table.
10401 	 */
10402 	addr = sin->sin_addr.s_addr;
10403 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10404 		ire_t			*ire;
10405 		dl_unitdata_req_t	*dlup;
10406 		mblk_t			*llmp;
10407 		int			addr_len;
10408 		ill_t			*ipsqill = NULL;
10409 
10410 		if (ifx_arp_ioctl) {
10411 			/*
10412 			 * There's no need to lookup the ill, since
10413 			 * we've already done that when we started
10414 			 * processing the ioctl and sent the message
10415 			 * to ARP on that ill.  So use the ill that
10416 			 * is stored in q->q_ptr.
10417 			 */
10418 			ipsqill = ill;
10419 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10420 			    ipsqill->ill_ipif, ALL_ZONES,
10421 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10422 		} else {
10423 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10424 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10425 			if (ire != NULL)
10426 				ipsqill = ire_to_ill(ire);
10427 		}
10428 
10429 		if ((x_arp_ioctl) && (ipsqill != NULL))
10430 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10431 
10432 		if (ire != NULL) {
10433 			/*
10434 			 * Since the ire obtained from cachetable is used for
10435 			 * mac addr copying below, treat an incomplete ire as if
10436 			 * as if we never found it.
10437 			 */
10438 			if (ire->ire_nce != NULL &&
10439 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10440 				ire_refrele(ire);
10441 				ire = NULL;
10442 				ipsqill = NULL;
10443 				goto errack;
10444 			}
10445 			*flagsp = ATF_INUSE;
10446 			llmp = (ire->ire_nce != NULL ?
10447 			    ire->ire_nce->nce_res_mp : NULL);
10448 			if (llmp != NULL && ipsqill != NULL) {
10449 				uchar_t *macaddr;
10450 
10451 				addr_len = ipsqill->ill_phys_addr_length;
10452 				if (x_arp_ioctl && ((addr_len +
10453 				    ipsqill->ill_name_length) >
10454 				    sizeof (xar->xarp_ha.sdl_data))) {
10455 					ire_refrele(ire);
10456 					freemsg(mp);
10457 					ip_ioctl_finish(q, orig_ioc_mp,
10458 					    EINVAL, NO_COPYOUT, NULL);
10459 					return;
10460 				}
10461 				*flagsp |= ATF_COM;
10462 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10463 				if (ipsqill->ill_sap_length < 0)
10464 					macaddr = llmp->b_rptr +
10465 					    dlup->dl_dest_addr_offset;
10466 				else
10467 					macaddr = llmp->b_rptr +
10468 					    dlup->dl_dest_addr_offset +
10469 					    ipsqill->ill_sap_length;
10470 				/*
10471 				 * For SIOCGARP, MAC address length
10472 				 * validation has already been done
10473 				 * before the ioctl was issued to ARP to
10474 				 * allow it to progress only on 6 byte
10475 				 * addressable (ethernet like) media. Thus
10476 				 * the mac address copying can not overwrite
10477 				 * the sa_data area below.
10478 				 */
10479 				bcopy(macaddr, storage, addr_len);
10480 			}
10481 			/* Ditch the internal IOCTL. */
10482 			freemsg(mp);
10483 			ire_refrele(ire);
10484 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10485 			return;
10486 		}
10487 	}
10488 
10489 	/*
10490 	 * Delete the coresponding IRE_CACHE if any.
10491 	 * Reset the error if there was one (in case there was no entry
10492 	 * in arp.)
10493 	 */
10494 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10495 		ipif_t *ipintf = NULL;
10496 
10497 		if (ifx_arp_ioctl) {
10498 			/*
10499 			 * There's no need to lookup the ill, since
10500 			 * we've already done that when we started
10501 			 * processing the ioctl and sent the message
10502 			 * to ARP on that ill.  So use the ill that
10503 			 * is stored in q->q_ptr.
10504 			 */
10505 			ipintf = ill->ill_ipif;
10506 		}
10507 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10508 			/*
10509 			 * The address in "addr" may be an entry for a
10510 			 * router. If that's true, then any off-net
10511 			 * IRE_CACHE entries that go through the router
10512 			 * with address "addr" must be clobbered. Use
10513 			 * ire_walk to achieve this goal.
10514 			 */
10515 			if (ifx_arp_ioctl)
10516 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10517 				    ire_delete_cache_gw, (char *)&addr, ill);
10518 			else
10519 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10520 				    ALL_ZONES, ipst);
10521 			iocp->ioc_error = 0;
10522 		}
10523 	}
10524 errack:
10525 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10526 		err = iocp->ioc_error;
10527 		freemsg(mp);
10528 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10529 		return;
10530 	}
10531 
10532 	/*
10533 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10534 	 * the area_t into the struct {x}arpreq.
10535 	 */
10536 	if (x_arp_ioctl) {
10537 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10538 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10539 		    sizeof (xar->xarp_ha.sdl_data)) {
10540 			freemsg(mp);
10541 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10542 			    NULL);
10543 			return;
10544 		}
10545 	}
10546 	*flagsp = ATF_INUSE;
10547 	if (area->area_flags & ACE_F_PERMANENT)
10548 		*flagsp |= ATF_PERM;
10549 	if (area->area_flags & ACE_F_PUBLISH)
10550 		*flagsp |= ATF_PUBL;
10551 	if (area->area_flags & ACE_F_AUTHORITY)
10552 		*flagsp |= ATF_AUTHORITY;
10553 	if (area->area_hw_addr_length != 0) {
10554 		*flagsp |= ATF_COM;
10555 		/*
10556 		 * For SIOCGARP, MAC address length validation has
10557 		 * already been done before the ioctl was issued to ARP
10558 		 * to allow it to progress only on 6 byte addressable
10559 		 * (ethernet like) media. Thus the mac address copying
10560 		 * can not overwrite the sa_data area below.
10561 		 */
10562 		bcopy((char *)area + area->area_hw_addr_offset,
10563 		    storage, area->area_hw_addr_length);
10564 	}
10565 
10566 	/* Ditch the internal IOCTL. */
10567 	freemsg(mp);
10568 	/* Complete the original. */
10569 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10570 }
10571 
10572 /*
10573  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10574  * interface) create the next available logical interface for this
10575  * physical interface.
10576  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10577  * ipif with the specified name.
10578  *
10579  * If the address family is not AF_UNSPEC then set the address as well.
10580  *
10581  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10582  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10583  *
10584  * Executed as a writer on the ill or ill group.
10585  * So no lock is needed to traverse the ipif chain, or examine the
10586  * phyint flags.
10587  */
10588 /* ARGSUSED */
10589 int
10590 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10591     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10592 {
10593 	mblk_t	*mp1;
10594 	struct lifreq *lifr;
10595 	boolean_t	isv6;
10596 	boolean_t	exists;
10597 	char 	*name;
10598 	char	*endp;
10599 	char	*cp;
10600 	int	namelen;
10601 	ipif_t	*ipif;
10602 	long	id;
10603 	ipsq_t	*ipsq;
10604 	ill_t	*ill;
10605 	sin_t	*sin;
10606 	int	err = 0;
10607 	boolean_t found_sep = B_FALSE;
10608 	conn_t	*connp;
10609 	zoneid_t zoneid;
10610 	int	orig_ifindex = 0;
10611 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10612 
10613 	ASSERT(q->q_next == NULL);
10614 	ip1dbg(("ip_sioctl_addif\n"));
10615 	/* Existence of mp1 has been checked in ip_wput_nondata */
10616 	mp1 = mp->b_cont->b_cont;
10617 	/*
10618 	 * Null terminate the string to protect against buffer
10619 	 * overrun. String was generated by user code and may not
10620 	 * be trusted.
10621 	 */
10622 	lifr = (struct lifreq *)mp1->b_rptr;
10623 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10624 	name = lifr->lifr_name;
10625 	ASSERT(CONN_Q(q));
10626 	connp = Q_TO_CONN(q);
10627 	isv6 = connp->conn_af_isv6;
10628 	zoneid = connp->conn_zoneid;
10629 	namelen = mi_strlen(name);
10630 	if (namelen == 0)
10631 		return (EINVAL);
10632 
10633 	exists = B_FALSE;
10634 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10635 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10636 		/*
10637 		 * Allow creating lo0 using SIOCLIFADDIF.
10638 		 * can't be any other writer thread. So can pass null below
10639 		 * for the last 4 args to ipif_lookup_name.
10640 		 */
10641 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10642 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10643 		/* Prevent any further action */
10644 		if (ipif == NULL) {
10645 			return (ENOBUFS);
10646 		} else if (!exists) {
10647 			/* We created the ipif now and as writer */
10648 			ipif_refrele(ipif);
10649 			return (0);
10650 		} else {
10651 			ill = ipif->ipif_ill;
10652 			ill_refhold(ill);
10653 			ipif_refrele(ipif);
10654 		}
10655 	} else {
10656 		/* Look for a colon in the name. */
10657 		endp = &name[namelen];
10658 		for (cp = endp; --cp > name; ) {
10659 			if (*cp == IPIF_SEPARATOR_CHAR) {
10660 				found_sep = B_TRUE;
10661 				/*
10662 				 * Reject any non-decimal aliases for plumbing
10663 				 * of logical interfaces. Aliases with leading
10664 				 * zeroes are also rejected as they introduce
10665 				 * ambiguity in the naming of the interfaces.
10666 				 * Comparing with "0" takes care of all such
10667 				 * cases.
10668 				 */
10669 				if ((strncmp("0", cp+1, 1)) == 0)
10670 					return (EINVAL);
10671 
10672 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10673 				    id <= 0 || *endp != '\0') {
10674 					return (EINVAL);
10675 				}
10676 				*cp = '\0';
10677 				break;
10678 			}
10679 		}
10680 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10681 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10682 		if (found_sep)
10683 			*cp = IPIF_SEPARATOR_CHAR;
10684 		if (ill == NULL)
10685 			return (err);
10686 	}
10687 
10688 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10689 	    B_TRUE);
10690 
10691 	/*
10692 	 * Release the refhold due to the lookup, now that we are excl
10693 	 * or we are just returning
10694 	 */
10695 	ill_refrele(ill);
10696 
10697 	if (ipsq == NULL)
10698 		return (EINPROGRESS);
10699 
10700 	/*
10701 	 * If the interface is failed, inactive or offlined, look for a working
10702 	 * interface in the ill group and create the ipif there. If we can't
10703 	 * find a good interface, create the ipif anyway so that in.mpathd can
10704 	 * move it to the first repaired interface.
10705 	 */
10706 	if ((ill->ill_phyint->phyint_flags &
10707 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10708 	    ill->ill_phyint->phyint_groupname_len != 0) {
10709 		phyint_t *phyi;
10710 		char *groupname = ill->ill_phyint->phyint_groupname;
10711 
10712 		/*
10713 		 * We're looking for a working interface, but it doesn't matter
10714 		 * if it's up or down; so instead of following the group lists,
10715 		 * we look at each physical interface and compare the groupname.
10716 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10717 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10718 		 * Otherwise we create the ipif on the failed interface.
10719 		 */
10720 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10721 		phyi = avl_first(&ipst->ips_phyint_g_list->
10722 		    phyint_list_avl_by_index);
10723 		for (; phyi != NULL;
10724 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10725 		    phyint_list_avl_by_index,
10726 		    phyi, AVL_AFTER)) {
10727 			if (phyi->phyint_groupname_len == 0)
10728 				continue;
10729 			ASSERT(phyi->phyint_groupname != NULL);
10730 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10731 			    !(phyi->phyint_flags &
10732 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10733 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10734 			    (phyi->phyint_illv4 != NULL))) {
10735 				break;
10736 			}
10737 		}
10738 		rw_exit(&ipst->ips_ill_g_lock);
10739 
10740 		if (phyi != NULL) {
10741 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10742 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10743 			    phyi->phyint_illv4);
10744 		}
10745 	}
10746 
10747 	/*
10748 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10749 	 * before or after us.
10750 	 */
10751 	ASSERT(IAM_WRITER_ILL(ill));
10752 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10753 
10754 	if (found_sep && orig_ifindex == 0) {
10755 		/* Now see if there is an IPIF with this unit number. */
10756 		for (ipif = ill->ill_ipif; ipif != NULL;
10757 		    ipif = ipif->ipif_next) {
10758 			if (ipif->ipif_id == id) {
10759 				err = EEXIST;
10760 				goto done;
10761 			}
10762 		}
10763 	}
10764 
10765 	/*
10766 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10767 	 * of lo0. We never come here when we plumb lo0:0. It
10768 	 * happens in ipif_lookup_on_name.
10769 	 * The specified unit number is ignored when we create the ipif on a
10770 	 * different interface. However, we save it in ipif_orig_ipifid below so
10771 	 * that the ipif fails back to the right position.
10772 	 */
10773 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10774 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10775 		err = ENOBUFS;
10776 		goto done;
10777 	}
10778 
10779 	/* Return created name with ioctl */
10780 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10781 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10782 	ip1dbg(("created %s\n", lifr->lifr_name));
10783 
10784 	/* Set address */
10785 	sin = (sin_t *)&lifr->lifr_addr;
10786 	if (sin->sin_family != AF_UNSPEC) {
10787 		err = ip_sioctl_addr(ipif, sin, q, mp,
10788 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10789 	}
10790 
10791 	/* Set ifindex and unit number for failback */
10792 	if (err == 0 && orig_ifindex != 0) {
10793 		ipif->ipif_orig_ifindex = orig_ifindex;
10794 		if (found_sep) {
10795 			ipif->ipif_orig_ipifid = id;
10796 		}
10797 	}
10798 
10799 done:
10800 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10801 	return (err);
10802 }
10803 
10804 /*
10805  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10806  * interface) delete it based on the IP address (on this physical interface).
10807  * Otherwise delete it based on the ipif_id.
10808  * Also, special handling to allow a removeif of lo0.
10809  */
10810 /* ARGSUSED */
10811 int
10812 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10813     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10814 {
10815 	conn_t		*connp;
10816 	ill_t		*ill = ipif->ipif_ill;
10817 	boolean_t	 success;
10818 	ip_stack_t	*ipst;
10819 
10820 	ipst = CONNQ_TO_IPST(q);
10821 
10822 	ASSERT(q->q_next == NULL);
10823 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10824 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10825 	ASSERT(IAM_WRITER_IPIF(ipif));
10826 
10827 	connp = Q_TO_CONN(q);
10828 	/*
10829 	 * Special case for unplumbing lo0 (the loopback physical interface).
10830 	 * If unplumbing lo0, the incoming address structure has been
10831 	 * initialized to all zeros. When unplumbing lo0, all its logical
10832 	 * interfaces must be removed too.
10833 	 *
10834 	 * Note that this interface may be called to remove a specific
10835 	 * loopback logical interface (eg, lo0:1). But in that case
10836 	 * ipif->ipif_id != 0 so that the code path for that case is the
10837 	 * same as any other interface (meaning it skips the code directly
10838 	 * below).
10839 	 */
10840 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10841 		if (sin->sin_family == AF_UNSPEC &&
10842 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10843 			/*
10844 			 * Mark it condemned. No new ref. will be made to ill.
10845 			 */
10846 			mutex_enter(&ill->ill_lock);
10847 			ill->ill_state_flags |= ILL_CONDEMNED;
10848 			for (ipif = ill->ill_ipif; ipif != NULL;
10849 			    ipif = ipif->ipif_next) {
10850 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10851 			}
10852 			mutex_exit(&ill->ill_lock);
10853 
10854 			ipif = ill->ill_ipif;
10855 			/* unplumb the loopback interface */
10856 			ill_delete(ill);
10857 			mutex_enter(&connp->conn_lock);
10858 			mutex_enter(&ill->ill_lock);
10859 			ASSERT(ill->ill_group == NULL);
10860 
10861 			/* Are any references to this ill active */
10862 			if (ill_is_freeable(ill)) {
10863 				mutex_exit(&ill->ill_lock);
10864 				mutex_exit(&connp->conn_lock);
10865 				ill_delete_tail(ill);
10866 				mutex_enter(&ill->ill_lock);
10867 				ill_nic_info_dispatch(ill);
10868 				mutex_exit(&ill->ill_lock);
10869 				mi_free(ill);
10870 				return (0);
10871 			}
10872 			success = ipsq_pending_mp_add(connp, ipif,
10873 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10874 			mutex_exit(&connp->conn_lock);
10875 			mutex_exit(&ill->ill_lock);
10876 			if (success)
10877 				return (EINPROGRESS);
10878 			else
10879 				return (EINTR);
10880 		}
10881 	}
10882 
10883 	/*
10884 	 * We are exclusive on the ipsq, so an ill move will be serialized
10885 	 * before or after us.
10886 	 */
10887 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10888 
10889 	if (ipif->ipif_id == 0) {
10890 
10891 		ipsq_t *ipsq;
10892 
10893 		/* Find based on address */
10894 		if (ipif->ipif_isv6) {
10895 			sin6_t *sin6;
10896 
10897 			if (sin->sin_family != AF_INET6)
10898 				return (EAFNOSUPPORT);
10899 
10900 			sin6 = (sin6_t *)sin;
10901 			/* We are a writer, so we should be able to lookup */
10902 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10903 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10904 			if (ipif == NULL) {
10905 				/*
10906 				 * Maybe the address in on another interface in
10907 				 * the same IPMP group? We check this below.
10908 				 */
10909 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10910 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10911 				    ipst);
10912 			}
10913 		} else {
10914 			ipaddr_t addr;
10915 
10916 			if (sin->sin_family != AF_INET)
10917 				return (EAFNOSUPPORT);
10918 
10919 			addr = sin->sin_addr.s_addr;
10920 			/* We are a writer, so we should be able to lookup */
10921 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10922 			    NULL, NULL, NULL, ipst);
10923 			if (ipif == NULL) {
10924 				/*
10925 				 * Maybe the address in on another interface in
10926 				 * the same IPMP group? We check this below.
10927 				 */
10928 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10929 				    NULL, NULL, NULL, NULL, ipst);
10930 			}
10931 		}
10932 		if (ipif == NULL) {
10933 			return (EADDRNOTAVAIL);
10934 		}
10935 
10936 		/*
10937 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10938 		 * lifr_name of the physical interface but with an ip address
10939 		 * lifr_addr of a logical interface plumbed over it.
10940 		 * So update ipsq_current_ipif once ipif points to the
10941 		 * correct interface after doing ipif_lookup_addr().
10942 		 */
10943 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10944 		ASSERT(ipsq != NULL);
10945 
10946 		mutex_enter(&ipsq->ipsq_lock);
10947 		ipsq->ipsq_current_ipif = ipif;
10948 		mutex_exit(&ipsq->ipsq_lock);
10949 
10950 		/*
10951 		 * When the address to be removed is hosted on a different
10952 		 * interface, we check if the interface is in the same IPMP
10953 		 * group as the specified one; if so we proceed with the
10954 		 * removal.
10955 		 * ill->ill_group is NULL when the ill is down, so we have to
10956 		 * compare the group names instead.
10957 		 */
10958 		if (ipif->ipif_ill != ill &&
10959 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10960 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10961 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10962 		    ill->ill_phyint->phyint_groupname) != 0)) {
10963 			ipif_refrele(ipif);
10964 			return (EADDRNOTAVAIL);
10965 		}
10966 
10967 		/* This is a writer */
10968 		ipif_refrele(ipif);
10969 	}
10970 
10971 	/*
10972 	 * Can not delete instance zero since it is tied to the ill.
10973 	 */
10974 	if (ipif->ipif_id == 0)
10975 		return (EBUSY);
10976 
10977 	mutex_enter(&ill->ill_lock);
10978 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10979 	mutex_exit(&ill->ill_lock);
10980 
10981 	ipif_free(ipif);
10982 
10983 	mutex_enter(&connp->conn_lock);
10984 	mutex_enter(&ill->ill_lock);
10985 
10986 
10987 	/* Are any references to this ipif active */
10988 	if (ipif_is_freeable(ipif)) {
10989 		mutex_exit(&ill->ill_lock);
10990 		mutex_exit(&connp->conn_lock);
10991 		ipif_non_duplicate(ipif);
10992 		ipif_down_tail(ipif);
10993 		ipif_free_tail(ipif); /* frees ipif */
10994 		return (0);
10995 	}
10996 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10997 	    IPIF_FREE);
10998 	mutex_exit(&ill->ill_lock);
10999 	mutex_exit(&connp->conn_lock);
11000 	if (success)
11001 		return (EINPROGRESS);
11002 	else
11003 		return (EINTR);
11004 }
11005 
11006 /*
11007  * Restart the removeif ioctl. The refcnt has gone down to 0.
11008  * The ipif is already condemned. So can't find it thru lookups.
11009  */
11010 /* ARGSUSED */
11011 int
11012 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11013     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11014 {
11015 	ill_t *ill = ipif->ipif_ill;
11016 
11017 	ASSERT(IAM_WRITER_IPIF(ipif));
11018 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11019 
11020 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11021 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11022 
11023 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11024 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
11025 		ill_delete_tail(ill);
11026 		mutex_enter(&ill->ill_lock);
11027 		ill_nic_info_dispatch(ill);
11028 		mutex_exit(&ill->ill_lock);
11029 		mi_free(ill);
11030 		return (0);
11031 	}
11032 
11033 	ipif_non_duplicate(ipif);
11034 	ipif_down_tail(ipif);
11035 	ipif_free_tail(ipif);
11036 
11037 	ILL_UNMARK_CHANGING(ill);
11038 	return (0);
11039 }
11040 
11041 /*
11042  * Set the local interface address.
11043  * Allow an address of all zero when the interface is down.
11044  */
11045 /* ARGSUSED */
11046 int
11047 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11048     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11049 {
11050 	int err = 0;
11051 	in6_addr_t v6addr;
11052 	boolean_t need_up = B_FALSE;
11053 
11054 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11055 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11056 
11057 	ASSERT(IAM_WRITER_IPIF(ipif));
11058 
11059 	if (ipif->ipif_isv6) {
11060 		sin6_t *sin6;
11061 		ill_t *ill;
11062 		phyint_t *phyi;
11063 
11064 		if (sin->sin_family != AF_INET6)
11065 			return (EAFNOSUPPORT);
11066 
11067 		sin6 = (sin6_t *)sin;
11068 		v6addr = sin6->sin6_addr;
11069 		ill = ipif->ipif_ill;
11070 		phyi = ill->ill_phyint;
11071 
11072 		/*
11073 		 * Enforce that true multicast interfaces have a link-local
11074 		 * address for logical unit 0.
11075 		 */
11076 		if (ipif->ipif_id == 0 &&
11077 		    (ill->ill_flags & ILLF_MULTICAST) &&
11078 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11079 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11080 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11081 			return (EADDRNOTAVAIL);
11082 		}
11083 
11084 		/*
11085 		 * up interfaces shouldn't have the unspecified address
11086 		 * unless they also have the IPIF_NOLOCAL flags set and
11087 		 * have a subnet assigned.
11088 		 */
11089 		if ((ipif->ipif_flags & IPIF_UP) &&
11090 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11091 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11092 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11093 			return (EADDRNOTAVAIL);
11094 		}
11095 
11096 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11097 			return (EADDRNOTAVAIL);
11098 	} else {
11099 		ipaddr_t addr;
11100 
11101 		if (sin->sin_family != AF_INET)
11102 			return (EAFNOSUPPORT);
11103 
11104 		addr = sin->sin_addr.s_addr;
11105 
11106 		/* Allow 0 as the local address. */
11107 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11108 			return (EADDRNOTAVAIL);
11109 
11110 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11111 	}
11112 
11113 
11114 	/*
11115 	 * Even if there is no change we redo things just to rerun
11116 	 * ipif_set_default.
11117 	 */
11118 	if (ipif->ipif_flags & IPIF_UP) {
11119 		/*
11120 		 * Setting a new local address, make sure
11121 		 * we have net and subnet bcast ire's for
11122 		 * the old address if we need them.
11123 		 */
11124 		if (!ipif->ipif_isv6)
11125 			ipif_check_bcast_ires(ipif);
11126 		/*
11127 		 * If the interface is already marked up,
11128 		 * we call ipif_down which will take care
11129 		 * of ditching any IREs that have been set
11130 		 * up based on the old interface address.
11131 		 */
11132 		err = ipif_logical_down(ipif, q, mp);
11133 		if (err == EINPROGRESS)
11134 			return (err);
11135 		ipif_down_tail(ipif);
11136 		need_up = 1;
11137 	}
11138 
11139 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11140 	return (err);
11141 }
11142 
11143 int
11144 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11145     boolean_t need_up)
11146 {
11147 	in6_addr_t v6addr;
11148 	in6_addr_t ov6addr;
11149 	ipaddr_t addr;
11150 	sin6_t	*sin6;
11151 	int	sinlen;
11152 	int	err = 0;
11153 	ill_t	*ill = ipif->ipif_ill;
11154 	boolean_t need_dl_down;
11155 	boolean_t need_arp_down;
11156 	struct iocblk *iocp;
11157 
11158 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11159 
11160 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11161 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11162 	ASSERT(IAM_WRITER_IPIF(ipif));
11163 
11164 	/* Must cancel any pending timer before taking the ill_lock */
11165 	if (ipif->ipif_recovery_id != 0)
11166 		(void) untimeout(ipif->ipif_recovery_id);
11167 	ipif->ipif_recovery_id = 0;
11168 
11169 	if (ipif->ipif_isv6) {
11170 		sin6 = (sin6_t *)sin;
11171 		v6addr = sin6->sin6_addr;
11172 		sinlen = sizeof (struct sockaddr_in6);
11173 	} else {
11174 		addr = sin->sin_addr.s_addr;
11175 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11176 		sinlen = sizeof (struct sockaddr_in);
11177 	}
11178 	mutex_enter(&ill->ill_lock);
11179 	ov6addr = ipif->ipif_v6lcl_addr;
11180 	ipif->ipif_v6lcl_addr = v6addr;
11181 	sctp_update_ipif_addr(ipif, ov6addr);
11182 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11183 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11184 	} else {
11185 		ipif->ipif_v6src_addr = v6addr;
11186 	}
11187 	ipif->ipif_addr_ready = 0;
11188 
11189 	/*
11190 	 * If the interface was previously marked as a duplicate, then since
11191 	 * we've now got a "new" address, it should no longer be considered a
11192 	 * duplicate -- even if the "new" address is the same as the old one.
11193 	 * Note that if all ipifs are down, we may have a pending ARP down
11194 	 * event to handle.  This is because we want to recover from duplicates
11195 	 * and thus delay tearing down ARP until the duplicates have been
11196 	 * removed or disabled.
11197 	 */
11198 	need_dl_down = need_arp_down = B_FALSE;
11199 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11200 		need_arp_down = !need_up;
11201 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11202 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11203 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11204 			need_dl_down = B_TRUE;
11205 		}
11206 	}
11207 
11208 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11209 	    !ill->ill_is_6to4tun) {
11210 		queue_t *wqp = ill->ill_wq;
11211 
11212 		/*
11213 		 * The local address of this interface is a 6to4 address,
11214 		 * check if this interface is in fact a 6to4 tunnel or just
11215 		 * an interface configured with a 6to4 address.  We are only
11216 		 * interested in the former.
11217 		 */
11218 		if (wqp != NULL) {
11219 			while ((wqp->q_next != NULL) &&
11220 			    (wqp->q_next->q_qinfo != NULL) &&
11221 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11222 
11223 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11224 				    == TUN6TO4_MODID) {
11225 					/* set for use in IP */
11226 					ill->ill_is_6to4tun = 1;
11227 					break;
11228 				}
11229 				wqp = wqp->q_next;
11230 			}
11231 		}
11232 	}
11233 
11234 	ipif_set_default(ipif);
11235 
11236 	/*
11237 	 * When publishing an interface address change event, we only notify
11238 	 * the event listeners of the new address.  It is assumed that if they
11239 	 * actively care about the addresses assigned that they will have
11240 	 * already discovered the previous address assigned (if there was one.)
11241 	 *
11242 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11243 	 */
11244 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11245 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11246 		    NE_ADDRESS_CHANGE, sin, sinlen);
11247 	}
11248 
11249 	mutex_exit(&ill->ill_lock);
11250 
11251 	if (need_up) {
11252 		/*
11253 		 * Now bring the interface back up.  If this
11254 		 * is the only IPIF for the ILL, ipif_up
11255 		 * will have to re-bind to the device, so
11256 		 * we may get back EINPROGRESS, in which
11257 		 * case, this IOCTL will get completed in
11258 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11259 		 */
11260 		err = ipif_up(ipif, q, mp);
11261 	}
11262 
11263 	if (need_dl_down)
11264 		ill_dl_down(ill);
11265 	if (need_arp_down)
11266 		ipif_arp_down(ipif);
11267 
11268 	return (err);
11269 }
11270 
11271 
11272 /*
11273  * Restart entry point to restart the address set operation after the
11274  * refcounts have dropped to zero.
11275  */
11276 /* ARGSUSED */
11277 int
11278 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11279     ip_ioctl_cmd_t *ipip, void *ifreq)
11280 {
11281 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11282 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11283 	ASSERT(IAM_WRITER_IPIF(ipif));
11284 	ipif_down_tail(ipif);
11285 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11286 }
11287 
11288 /* ARGSUSED */
11289 int
11290 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11291     ip_ioctl_cmd_t *ipip, void *if_req)
11292 {
11293 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11294 	struct lifreq *lifr = (struct lifreq *)if_req;
11295 
11296 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11297 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11298 	/*
11299 	 * The net mask and address can't change since we have a
11300 	 * reference to the ipif. So no lock is necessary.
11301 	 */
11302 	if (ipif->ipif_isv6) {
11303 		*sin6 = sin6_null;
11304 		sin6->sin6_family = AF_INET6;
11305 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11306 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11307 		lifr->lifr_addrlen =
11308 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11309 	} else {
11310 		*sin = sin_null;
11311 		sin->sin_family = AF_INET;
11312 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11313 		if (ipip->ipi_cmd_type == LIF_CMD) {
11314 			lifr->lifr_addrlen =
11315 			    ip_mask_to_plen(ipif->ipif_net_mask);
11316 		}
11317 	}
11318 	return (0);
11319 }
11320 
11321 /*
11322  * Set the destination address for a pt-pt interface.
11323  */
11324 /* ARGSUSED */
11325 int
11326 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11327     ip_ioctl_cmd_t *ipip, void *if_req)
11328 {
11329 	int err = 0;
11330 	in6_addr_t v6addr;
11331 	boolean_t need_up = B_FALSE;
11332 
11333 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11334 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11335 	ASSERT(IAM_WRITER_IPIF(ipif));
11336 
11337 	if (ipif->ipif_isv6) {
11338 		sin6_t *sin6;
11339 
11340 		if (sin->sin_family != AF_INET6)
11341 			return (EAFNOSUPPORT);
11342 
11343 		sin6 = (sin6_t *)sin;
11344 		v6addr = sin6->sin6_addr;
11345 
11346 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11347 			return (EADDRNOTAVAIL);
11348 	} else {
11349 		ipaddr_t addr;
11350 
11351 		if (sin->sin_family != AF_INET)
11352 			return (EAFNOSUPPORT);
11353 
11354 		addr = sin->sin_addr.s_addr;
11355 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11356 			return (EADDRNOTAVAIL);
11357 
11358 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11359 	}
11360 
11361 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11362 		return (0);	/* No change */
11363 
11364 	if (ipif->ipif_flags & IPIF_UP) {
11365 		/*
11366 		 * If the interface is already marked up,
11367 		 * we call ipif_down which will take care
11368 		 * of ditching any IREs that have been set
11369 		 * up based on the old pp dst address.
11370 		 */
11371 		err = ipif_logical_down(ipif, q, mp);
11372 		if (err == EINPROGRESS)
11373 			return (err);
11374 		ipif_down_tail(ipif);
11375 		need_up = B_TRUE;
11376 	}
11377 	/*
11378 	 * could return EINPROGRESS. If so ioctl will complete in
11379 	 * ip_rput_dlpi_writer
11380 	 */
11381 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11382 	return (err);
11383 }
11384 
11385 static int
11386 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11387     boolean_t need_up)
11388 {
11389 	in6_addr_t v6addr;
11390 	ill_t	*ill = ipif->ipif_ill;
11391 	int	err = 0;
11392 	boolean_t need_dl_down;
11393 	boolean_t need_arp_down;
11394 
11395 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11396 	    ipif->ipif_id, (void *)ipif));
11397 
11398 	/* Must cancel any pending timer before taking the ill_lock */
11399 	if (ipif->ipif_recovery_id != 0)
11400 		(void) untimeout(ipif->ipif_recovery_id);
11401 	ipif->ipif_recovery_id = 0;
11402 
11403 	if (ipif->ipif_isv6) {
11404 		sin6_t *sin6;
11405 
11406 		sin6 = (sin6_t *)sin;
11407 		v6addr = sin6->sin6_addr;
11408 	} else {
11409 		ipaddr_t addr;
11410 
11411 		addr = sin->sin_addr.s_addr;
11412 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11413 	}
11414 	mutex_enter(&ill->ill_lock);
11415 	/* Set point to point destination address. */
11416 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11417 		/*
11418 		 * Allow this as a means of creating logical
11419 		 * pt-pt interfaces on top of e.g. an Ethernet.
11420 		 * XXX Undocumented HACK for testing.
11421 		 * pt-pt interfaces are created with NUD disabled.
11422 		 */
11423 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11424 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11425 		if (ipif->ipif_isv6)
11426 			ill->ill_flags |= ILLF_NONUD;
11427 	}
11428 
11429 	/*
11430 	 * If the interface was previously marked as a duplicate, then since
11431 	 * we've now got a "new" address, it should no longer be considered a
11432 	 * duplicate -- even if the "new" address is the same as the old one.
11433 	 * Note that if all ipifs are down, we may have a pending ARP down
11434 	 * event to handle.
11435 	 */
11436 	need_dl_down = need_arp_down = B_FALSE;
11437 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11438 		need_arp_down = !need_up;
11439 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11440 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11441 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11442 			need_dl_down = B_TRUE;
11443 		}
11444 	}
11445 
11446 	/* Set the new address. */
11447 	ipif->ipif_v6pp_dst_addr = v6addr;
11448 	/* Make sure subnet tracks pp_dst */
11449 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11450 	mutex_exit(&ill->ill_lock);
11451 
11452 	if (need_up) {
11453 		/*
11454 		 * Now bring the interface back up.  If this
11455 		 * is the only IPIF for the ILL, ipif_up
11456 		 * will have to re-bind to the device, so
11457 		 * we may get back EINPROGRESS, in which
11458 		 * case, this IOCTL will get completed in
11459 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11460 		 */
11461 		err = ipif_up(ipif, q, mp);
11462 	}
11463 
11464 	if (need_dl_down)
11465 		ill_dl_down(ill);
11466 
11467 	if (need_arp_down)
11468 		ipif_arp_down(ipif);
11469 	return (err);
11470 }
11471 
11472 /*
11473  * Restart entry point to restart the dstaddress set operation after the
11474  * refcounts have dropped to zero.
11475  */
11476 /* ARGSUSED */
11477 int
11478 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11479     ip_ioctl_cmd_t *ipip, void *ifreq)
11480 {
11481 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11482 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11483 	ipif_down_tail(ipif);
11484 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11485 }
11486 
11487 /* ARGSUSED */
11488 int
11489 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11490     ip_ioctl_cmd_t *ipip, void *if_req)
11491 {
11492 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11493 
11494 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11495 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11496 	/*
11497 	 * Get point to point destination address. The addresses can't
11498 	 * change since we hold a reference to the ipif.
11499 	 */
11500 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11501 		return (EADDRNOTAVAIL);
11502 
11503 	if (ipif->ipif_isv6) {
11504 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11505 		*sin6 = sin6_null;
11506 		sin6->sin6_family = AF_INET6;
11507 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11508 	} else {
11509 		*sin = sin_null;
11510 		sin->sin_family = AF_INET;
11511 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11512 	}
11513 	return (0);
11514 }
11515 
11516 /*
11517  * part of ipmp, make this func return the active/inactive state and
11518  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11519  */
11520 /*
11521  * This function either sets or clears the IFF_INACTIVE flag.
11522  *
11523  * As long as there are some addresses or multicast memberships on the
11524  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11525  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11526  * will be used for outbound packets.
11527  *
11528  * Caller needs to verify the validity of setting IFF_INACTIVE.
11529  */
11530 static void
11531 phyint_inactive(phyint_t *phyi)
11532 {
11533 	ill_t *ill_v4;
11534 	ill_t *ill_v6;
11535 	ipif_t *ipif;
11536 	ilm_t *ilm;
11537 
11538 	ill_v4 = phyi->phyint_illv4;
11539 	ill_v6 = phyi->phyint_illv6;
11540 
11541 	/*
11542 	 * No need for a lock while traversing the list since iam
11543 	 * a writer
11544 	 */
11545 	if (ill_v4 != NULL) {
11546 		ASSERT(IAM_WRITER_ILL(ill_v4));
11547 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11548 		    ipif = ipif->ipif_next) {
11549 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11550 				mutex_enter(&phyi->phyint_lock);
11551 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11552 				mutex_exit(&phyi->phyint_lock);
11553 				return;
11554 			}
11555 		}
11556 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11557 		    ilm = ilm->ilm_next) {
11558 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11559 				mutex_enter(&phyi->phyint_lock);
11560 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11561 				mutex_exit(&phyi->phyint_lock);
11562 				return;
11563 			}
11564 		}
11565 	}
11566 	if (ill_v6 != NULL) {
11567 		ill_v6 = phyi->phyint_illv6;
11568 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11569 		    ipif = ipif->ipif_next) {
11570 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11571 				mutex_enter(&phyi->phyint_lock);
11572 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11573 				mutex_exit(&phyi->phyint_lock);
11574 				return;
11575 			}
11576 		}
11577 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11578 		    ilm = ilm->ilm_next) {
11579 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11580 				mutex_enter(&phyi->phyint_lock);
11581 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11582 				mutex_exit(&phyi->phyint_lock);
11583 				return;
11584 			}
11585 		}
11586 	}
11587 	mutex_enter(&phyi->phyint_lock);
11588 	phyi->phyint_flags |= PHYI_INACTIVE;
11589 	mutex_exit(&phyi->phyint_lock);
11590 }
11591 
11592 /*
11593  * This function is called only when the phyint flags change. Currently
11594  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11595  * that we can select a good ill.
11596  */
11597 static void
11598 ip_redo_nomination(phyint_t *phyi)
11599 {
11600 	ill_t *ill_v4;
11601 
11602 	ill_v4 = phyi->phyint_illv4;
11603 
11604 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11605 		ASSERT(IAM_WRITER_ILL(ill_v4));
11606 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11607 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11608 	}
11609 }
11610 
11611 /*
11612  * Heuristic to check if ill is INACTIVE.
11613  * Checks if ill has an ipif with an usable ip address.
11614  *
11615  * Return values:
11616  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11617  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11618  */
11619 static boolean_t
11620 ill_is_inactive(ill_t *ill)
11621 {
11622 	ipif_t *ipif;
11623 
11624 	/* Check whether it is in an IPMP group */
11625 	if (ill->ill_phyint->phyint_groupname == NULL)
11626 		return (B_FALSE);
11627 
11628 	if (ill->ill_ipif_up_count == 0)
11629 		return (B_TRUE);
11630 
11631 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11632 		uint64_t flags = ipif->ipif_flags;
11633 
11634 		/*
11635 		 * This ipif is usable if it is IPIF_UP and not a
11636 		 * dedicated test address.  A dedicated test address
11637 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11638 		 * (note in particular that V6 test addresses are
11639 		 * link-local data addresses and thus are marked
11640 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11641 		 */
11642 		if ((flags & IPIF_UP) &&
11643 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11644 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11645 			return (B_FALSE);
11646 	}
11647 	return (B_TRUE);
11648 }
11649 
11650 /*
11651  * Set interface flags.
11652  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11653  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11654  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11655  *
11656  * NOTE : We really don't enforce that ipif_id zero should be used
11657  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11658  *	  is because applications generally does SICGLIFFLAGS and
11659  *	  ORs in the new flags (that affects the logical) and does a
11660  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11661  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11662  *	  flags that will be turned on is correct with respect to
11663  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11664  */
11665 /* ARGSUSED */
11666 int
11667 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11668     ip_ioctl_cmd_t *ipip, void *if_req)
11669 {
11670 	uint64_t turn_on;
11671 	uint64_t turn_off;
11672 	int	err;
11673 	boolean_t need_up = B_FALSE;
11674 	phyint_t *phyi;
11675 	ill_t *ill;
11676 	uint64_t intf_flags;
11677 	boolean_t phyint_flags_modified = B_FALSE;
11678 	uint64_t flags;
11679 	struct ifreq *ifr;
11680 	struct lifreq *lifr;
11681 	boolean_t set_linklocal = B_FALSE;
11682 	boolean_t zero_source = B_FALSE;
11683 	ip_stack_t *ipst;
11684 
11685 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11686 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11687 
11688 	ASSERT(IAM_WRITER_IPIF(ipif));
11689 
11690 	ill = ipif->ipif_ill;
11691 	phyi = ill->ill_phyint;
11692 	ipst = ill->ill_ipst;
11693 
11694 	if (ipip->ipi_cmd_type == IF_CMD) {
11695 		ifr = (struct ifreq *)if_req;
11696 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11697 	} else {
11698 		lifr = (struct lifreq *)if_req;
11699 		flags = lifr->lifr_flags;
11700 	}
11701 
11702 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11703 
11704 	/*
11705 	 * Has the flags been set correctly till now ?
11706 	 */
11707 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11708 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11709 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11710 	/*
11711 	 * Compare the new flags to the old, and partition
11712 	 * into those coming on and those going off.
11713 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11714 	 */
11715 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11716 		flags |= intf_flags & ~0xFFFF;
11717 
11718 	/*
11719 	 * First check which bits will change and then which will
11720 	 * go on and off
11721 	 */
11722 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11723 	if (!turn_on)
11724 		return (0);	/* No change */
11725 
11726 	turn_off = intf_flags & turn_on;
11727 	turn_on ^= turn_off;
11728 	err = 0;
11729 
11730 	/*
11731 	 * Don't allow any bits belonging to the logical interface
11732 	 * to be set or cleared on the replacement ipif that was
11733 	 * created temporarily during a MOVE.
11734 	 */
11735 	if (ipif->ipif_replace_zero &&
11736 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11737 		return (EINVAL);
11738 	}
11739 
11740 	/*
11741 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11742 	 * IPv6 interfaces.
11743 	 */
11744 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11745 		return (EINVAL);
11746 
11747 	/*
11748 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11749 	 */
11750 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11751 		return (EINVAL);
11752 
11753 	/*
11754 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11755 	 * interfaces.  It makes no sense in that context.
11756 	 */
11757 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11758 		return (EINVAL);
11759 
11760 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11761 		zero_source = B_TRUE;
11762 
11763 	/*
11764 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11765 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11766 	 * If the link local address isn't set, and can be set, it will get
11767 	 * set later on in this function.
11768 	 */
11769 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11770 	    (flags & IFF_UP) && !zero_source &&
11771 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11772 		if (ipif_cant_setlinklocal(ipif))
11773 			return (EINVAL);
11774 		set_linklocal = B_TRUE;
11775 	}
11776 
11777 	/*
11778 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11779 	 * same time. No need to grab ill_g_usesrc_lock here, see
11780 	 * synchronization notes in ip.c
11781 	 */
11782 	if (turn_on & PHYI_STANDBY &&
11783 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11784 		return (EINVAL);
11785 	}
11786 
11787 	/*
11788 	 * If we modify physical interface flags, we'll potentially need to
11789 	 * send up two routing socket messages for the changes (one for the
11790 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11791 	 */
11792 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11793 		phyint_flags_modified = B_TRUE;
11794 
11795 	/*
11796 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11797 	 * we need to flush the IRE_CACHES belonging to this ill.
11798 	 * We handle this case here without doing the DOWN/UP dance
11799 	 * like it is done for other flags. If some other flags are
11800 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11801 	 * below will handle it by bringing it down and then
11802 	 * bringing it UP.
11803 	 */
11804 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11805 		ill_t *ill_v4, *ill_v6;
11806 
11807 		ill_v4 = phyi->phyint_illv4;
11808 		ill_v6 = phyi->phyint_illv6;
11809 
11810 		/*
11811 		 * First set the INACTIVE flag if needed. Then delete the ires.
11812 		 * ire_add will atomically prevent creating new IRE_CACHEs
11813 		 * unless hidden flag is set.
11814 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11815 		 */
11816 		if ((turn_on & PHYI_FAILED) &&
11817 		    ((intf_flags & PHYI_STANDBY) ||
11818 		    !ipst->ips_ipmp_enable_failback)) {
11819 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11820 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11821 		}
11822 		if ((turn_off & PHYI_FAILED) &&
11823 		    ((intf_flags & PHYI_STANDBY) ||
11824 		    (!ipst->ips_ipmp_enable_failback &&
11825 		    ill_is_inactive(ill)))) {
11826 			phyint_inactive(phyi);
11827 		}
11828 
11829 		if (turn_on & PHYI_STANDBY) {
11830 			/*
11831 			 * We implicitly set INACTIVE only when STANDBY is set.
11832 			 * INACTIVE is also set on non-STANDBY phyint when user
11833 			 * disables FAILBACK using configuration file.
11834 			 * Do not allow STANDBY to be set on such INACTIVE
11835 			 * phyint
11836 			 */
11837 			if (phyi->phyint_flags & PHYI_INACTIVE)
11838 				return (EINVAL);
11839 			if (!(phyi->phyint_flags & PHYI_FAILED))
11840 				phyint_inactive(phyi);
11841 		}
11842 		if (turn_off & PHYI_STANDBY) {
11843 			if (ipst->ips_ipmp_enable_failback) {
11844 				/*
11845 				 * Reset PHYI_INACTIVE.
11846 				 */
11847 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11848 			} else if (ill_is_inactive(ill) &&
11849 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11850 				/*
11851 				 * Need to set INACTIVE, when user sets
11852 				 * STANDBY on a non-STANDBY phyint and
11853 				 * later resets STANDBY
11854 				 */
11855 				phyint_inactive(phyi);
11856 			}
11857 		}
11858 		/*
11859 		 * We should always send up a message so that the
11860 		 * daemons come to know of it. Note that the zeroth
11861 		 * interface can be down and the check below for IPIF_UP
11862 		 * will not make sense as we are actually setting
11863 		 * a phyint flag here. We assume that the ipif used
11864 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11865 		 * send up any message for non-zero ipifs).
11866 		 */
11867 		phyint_flags_modified = B_TRUE;
11868 
11869 		if (ill_v4 != NULL) {
11870 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11871 			    IRE_CACHE, ill_stq_cache_delete,
11872 			    (char *)ill_v4, ill_v4);
11873 			illgrp_reset_schednext(ill_v4);
11874 		}
11875 		if (ill_v6 != NULL) {
11876 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11877 			    IRE_CACHE, ill_stq_cache_delete,
11878 			    (char *)ill_v6, ill_v6);
11879 			illgrp_reset_schednext(ill_v6);
11880 		}
11881 	}
11882 
11883 	/*
11884 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11885 	 * status of the interface and, if the interface is part of an IPMP
11886 	 * group, all other interfaces that are part of the same IPMP
11887 	 * group.
11888 	 */
11889 	if ((turn_on | turn_off) & ILLF_ROUTER)
11890 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11891 
11892 	/*
11893 	 * If the interface is not UP and we are not going to
11894 	 * bring it UP, record the flags and return. When the
11895 	 * interface comes UP later, the right actions will be
11896 	 * taken.
11897 	 */
11898 	if (!(ipif->ipif_flags & IPIF_UP) &&
11899 	    !(turn_on & IPIF_UP)) {
11900 		/* Record new flags in their respective places. */
11901 		mutex_enter(&ill->ill_lock);
11902 		mutex_enter(&ill->ill_phyint->phyint_lock);
11903 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11904 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11905 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11906 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11907 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11908 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11909 		mutex_exit(&ill->ill_lock);
11910 		mutex_exit(&ill->ill_phyint->phyint_lock);
11911 
11912 		/*
11913 		 * We do the broadcast and nomination here rather
11914 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11915 		 * the case of FAILBACK from INACTIVE standby to the
11916 		 * interface that has been repaired, PHYI_FAILED has not
11917 		 * been cleared yet. If there are only two interfaces in
11918 		 * that group, all we have is a FAILED and INACTIVE
11919 		 * interface. If we do the nomination soon after a failback,
11920 		 * the broadcast nomination code would select the
11921 		 * INACTIVE interface for receiving broadcasts as FAILED is
11922 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11923 		 * receive broadcast packets, we need to redo nomination
11924 		 * when the FAILED is cleared here. Thus, in general we
11925 		 * always do the nomination here for FAILED, STANDBY
11926 		 * and OFFLINE.
11927 		 */
11928 		if (((turn_on | turn_off) &
11929 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11930 			ip_redo_nomination(phyi);
11931 		}
11932 		if (phyint_flags_modified) {
11933 			if (phyi->phyint_illv4 != NULL) {
11934 				ip_rts_ifmsg(phyi->phyint_illv4->
11935 				    ill_ipif);
11936 			}
11937 			if (phyi->phyint_illv6 != NULL) {
11938 				ip_rts_ifmsg(phyi->phyint_illv6->
11939 				    ill_ipif);
11940 			}
11941 		}
11942 		return (0);
11943 	} else if (set_linklocal || zero_source) {
11944 		mutex_enter(&ill->ill_lock);
11945 		if (set_linklocal)
11946 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11947 		if (zero_source)
11948 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11949 		mutex_exit(&ill->ill_lock);
11950 	}
11951 
11952 	/*
11953 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11954 	 * or point-to-point interfaces with an unspecified destination. We do
11955 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11956 	 * have a subnet assigned, which is how in.ndpd currently manages its
11957 	 * onlink prefix list when no addresses are configured with those
11958 	 * prefixes.
11959 	 */
11960 	if (ipif->ipif_isv6 &&
11961 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11962 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11963 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11964 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11965 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11966 		return (EINVAL);
11967 	}
11968 
11969 	/*
11970 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11971 	 * from being brought up.
11972 	 */
11973 	if (!ipif->ipif_isv6 &&
11974 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11975 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11976 		return (EINVAL);
11977 	}
11978 
11979 	/*
11980 	 * The only flag changes that we currently take specific action on
11981 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11982 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11983 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11984 	 * the flags and bringing it back up again.
11985 	 */
11986 	if ((turn_on|turn_off) &
11987 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11988 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11989 		/*
11990 		 * Taking this ipif down, make sure we have
11991 		 * valid net and subnet bcast ire's for other
11992 		 * logical interfaces, if we need them.
11993 		 */
11994 		if (!ipif->ipif_isv6)
11995 			ipif_check_bcast_ires(ipif);
11996 
11997 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11998 		    !(turn_off & IPIF_UP)) {
11999 			need_up = B_TRUE;
12000 			if (ipif->ipif_flags & IPIF_UP)
12001 				ill->ill_logical_down = 1;
12002 			turn_on &= ~IPIF_UP;
12003 		}
12004 		err = ipif_down(ipif, q, mp);
12005 		ip1dbg(("ipif_down returns %d err ", err));
12006 		if (err == EINPROGRESS)
12007 			return (err);
12008 		ipif_down_tail(ipif);
12009 	}
12010 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12011 }
12012 
12013 static int
12014 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12015     boolean_t need_up)
12016 {
12017 	ill_t	*ill;
12018 	phyint_t *phyi;
12019 	uint64_t turn_on;
12020 	uint64_t turn_off;
12021 	uint64_t intf_flags;
12022 	boolean_t phyint_flags_modified = B_FALSE;
12023 	int	err = 0;
12024 	boolean_t set_linklocal = B_FALSE;
12025 	boolean_t zero_source = B_FALSE;
12026 
12027 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12028 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12029 
12030 	ASSERT(IAM_WRITER_IPIF(ipif));
12031 
12032 	ill = ipif->ipif_ill;
12033 	phyi = ill->ill_phyint;
12034 
12035 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12036 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12037 
12038 	turn_off = intf_flags & turn_on;
12039 	turn_on ^= turn_off;
12040 
12041 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12042 		phyint_flags_modified = B_TRUE;
12043 
12044 	/*
12045 	 * Now we change the flags. Track current value of
12046 	 * other flags in their respective places.
12047 	 */
12048 	mutex_enter(&ill->ill_lock);
12049 	mutex_enter(&phyi->phyint_lock);
12050 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12051 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12052 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12053 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12054 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12055 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12056 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12057 		set_linklocal = B_TRUE;
12058 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12059 	}
12060 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12061 		zero_source = B_TRUE;
12062 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12063 	}
12064 	mutex_exit(&ill->ill_lock);
12065 	mutex_exit(&phyi->phyint_lock);
12066 
12067 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12068 		ip_redo_nomination(phyi);
12069 
12070 	if (set_linklocal)
12071 		(void) ipif_setlinklocal(ipif);
12072 
12073 	if (zero_source)
12074 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12075 	else
12076 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12077 
12078 	if (need_up) {
12079 		/*
12080 		 * XXX ipif_up really does not know whether a phyint flags
12081 		 * was modified or not. So, it sends up information on
12082 		 * only one routing sockets message. As we don't bring up
12083 		 * the interface and also set STANDBY/FAILED simultaneously
12084 		 * it should be okay.
12085 		 */
12086 		err = ipif_up(ipif, q, mp);
12087 	} else {
12088 		/*
12089 		 * Make sure routing socket sees all changes to the flags.
12090 		 * ipif_up_done* handles this when we use ipif_up.
12091 		 */
12092 		if (phyint_flags_modified) {
12093 			if (phyi->phyint_illv4 != NULL) {
12094 				ip_rts_ifmsg(phyi->phyint_illv4->
12095 				    ill_ipif);
12096 			}
12097 			if (phyi->phyint_illv6 != NULL) {
12098 				ip_rts_ifmsg(phyi->phyint_illv6->
12099 				    ill_ipif);
12100 			}
12101 		} else {
12102 			ip_rts_ifmsg(ipif);
12103 		}
12104 		/*
12105 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12106 		 * this in need_up case.
12107 		 */
12108 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12109 	}
12110 	return (err);
12111 }
12112 
12113 /*
12114  * Restart entry point to restart the flags restart operation after the
12115  * refcounts have dropped to zero.
12116  */
12117 /* ARGSUSED */
12118 int
12119 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12120     ip_ioctl_cmd_t *ipip, void *if_req)
12121 {
12122 	int	err;
12123 	struct ifreq *ifr = (struct ifreq *)if_req;
12124 	struct lifreq *lifr = (struct lifreq *)if_req;
12125 
12126 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12127 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12128 
12129 	ipif_down_tail(ipif);
12130 	if (ipip->ipi_cmd_type == IF_CMD) {
12131 		/*
12132 		 * Since ip_sioctl_flags expects an int and ifr_flags
12133 		 * is a short we need to cast ifr_flags into an int
12134 		 * to avoid having sign extension cause bits to get
12135 		 * set that should not be.
12136 		 */
12137 		err = ip_sioctl_flags_tail(ipif,
12138 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12139 		    q, mp, B_TRUE);
12140 	} else {
12141 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12142 		    q, mp, B_TRUE);
12143 	}
12144 	return (err);
12145 }
12146 
12147 /*
12148  * Can operate on either a module or a driver queue.
12149  */
12150 /* ARGSUSED */
12151 int
12152 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12153     ip_ioctl_cmd_t *ipip, void *if_req)
12154 {
12155 	/*
12156 	 * Has the flags been set correctly till now ?
12157 	 */
12158 	ill_t *ill = ipif->ipif_ill;
12159 	phyint_t *phyi = ill->ill_phyint;
12160 
12161 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12162 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12163 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12164 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12165 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12166 
12167 	/*
12168 	 * Need a lock since some flags can be set even when there are
12169 	 * references to the ipif.
12170 	 */
12171 	mutex_enter(&ill->ill_lock);
12172 	if (ipip->ipi_cmd_type == IF_CMD) {
12173 		struct ifreq *ifr = (struct ifreq *)if_req;
12174 
12175 		/* Get interface flags (low 16 only). */
12176 		ifr->ifr_flags = ((ipif->ipif_flags |
12177 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12178 	} else {
12179 		struct lifreq *lifr = (struct lifreq *)if_req;
12180 
12181 		/* Get interface flags. */
12182 		lifr->lifr_flags = ipif->ipif_flags |
12183 		    ill->ill_flags | phyi->phyint_flags;
12184 	}
12185 	mutex_exit(&ill->ill_lock);
12186 	return (0);
12187 }
12188 
12189 /* ARGSUSED */
12190 int
12191 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12192     ip_ioctl_cmd_t *ipip, void *if_req)
12193 {
12194 	int mtu;
12195 	int ip_min_mtu;
12196 	struct ifreq	*ifr;
12197 	struct lifreq *lifr;
12198 	ire_t	*ire;
12199 	ip_stack_t *ipst;
12200 
12201 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12202 	    ipif->ipif_id, (void *)ipif));
12203 	if (ipip->ipi_cmd_type == IF_CMD) {
12204 		ifr = (struct ifreq *)if_req;
12205 		mtu = ifr->ifr_metric;
12206 	} else {
12207 		lifr = (struct lifreq *)if_req;
12208 		mtu = lifr->lifr_mtu;
12209 	}
12210 
12211 	if (ipif->ipif_isv6)
12212 		ip_min_mtu = IPV6_MIN_MTU;
12213 	else
12214 		ip_min_mtu = IP_MIN_MTU;
12215 
12216 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12217 		return (EINVAL);
12218 
12219 	/*
12220 	 * Change the MTU size in all relevant ire's.
12221 	 * Mtu change Vs. new ire creation - protocol below.
12222 	 * First change ipif_mtu and the ire_max_frag of the
12223 	 * interface ire. Then do an ire walk and change the
12224 	 * ire_max_frag of all affected ires. During ire_add
12225 	 * under the bucket lock, set the ire_max_frag of the
12226 	 * new ire being created from the ipif/ire from which
12227 	 * it is being derived. If an mtu change happens after
12228 	 * the ire is added, the new ire will be cleaned up.
12229 	 * Conversely if the mtu change happens before the ire
12230 	 * is added, ire_add will see the new value of the mtu.
12231 	 */
12232 	ipif->ipif_mtu = mtu;
12233 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12234 
12235 	if (ipif->ipif_isv6)
12236 		ire = ipif_to_ire_v6(ipif);
12237 	else
12238 		ire = ipif_to_ire(ipif);
12239 	if (ire != NULL) {
12240 		ire->ire_max_frag = ipif->ipif_mtu;
12241 		ire_refrele(ire);
12242 	}
12243 	ipst = ipif->ipif_ill->ill_ipst;
12244 	if (ipif->ipif_flags & IPIF_UP) {
12245 		if (ipif->ipif_isv6)
12246 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12247 			    ipst);
12248 		else
12249 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12250 			    ipst);
12251 	}
12252 	/* Update the MTU in SCTP's list */
12253 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12254 	return (0);
12255 }
12256 
12257 /* Get interface MTU. */
12258 /* ARGSUSED */
12259 int
12260 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12261 	ip_ioctl_cmd_t *ipip, void *if_req)
12262 {
12263 	struct ifreq	*ifr;
12264 	struct lifreq	*lifr;
12265 
12266 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12267 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12268 	if (ipip->ipi_cmd_type == IF_CMD) {
12269 		ifr = (struct ifreq *)if_req;
12270 		ifr->ifr_metric = ipif->ipif_mtu;
12271 	} else {
12272 		lifr = (struct lifreq *)if_req;
12273 		lifr->lifr_mtu = ipif->ipif_mtu;
12274 	}
12275 	return (0);
12276 }
12277 
12278 /* Set interface broadcast address. */
12279 /* ARGSUSED2 */
12280 int
12281 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12282 	ip_ioctl_cmd_t *ipip, void *if_req)
12283 {
12284 	ipaddr_t addr;
12285 	ire_t	*ire;
12286 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12287 
12288 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12289 	    ipif->ipif_id));
12290 
12291 	ASSERT(IAM_WRITER_IPIF(ipif));
12292 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12293 		return (EADDRNOTAVAIL);
12294 
12295 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12296 
12297 	if (sin->sin_family != AF_INET)
12298 		return (EAFNOSUPPORT);
12299 
12300 	addr = sin->sin_addr.s_addr;
12301 	if (ipif->ipif_flags & IPIF_UP) {
12302 		/*
12303 		 * If we are already up, make sure the new
12304 		 * broadcast address makes sense.  If it does,
12305 		 * there should be an IRE for it already.
12306 		 * Don't match on ipif, only on the ill
12307 		 * since we are sharing these now. Don't use
12308 		 * MATCH_IRE_ILL_GROUP as we are looking for
12309 		 * the broadcast ire on this ill and each ill
12310 		 * in the group has its own broadcast ire.
12311 		 */
12312 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12313 		    ipif, ALL_ZONES, NULL,
12314 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12315 		if (ire == NULL) {
12316 			return (EINVAL);
12317 		} else {
12318 			ire_refrele(ire);
12319 		}
12320 	}
12321 	/*
12322 	 * Changing the broadcast addr for this ipif.
12323 	 * Make sure we have valid net and subnet bcast
12324 	 * ire's for other logical interfaces, if needed.
12325 	 */
12326 	if (addr != ipif->ipif_brd_addr)
12327 		ipif_check_bcast_ires(ipif);
12328 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12329 	return (0);
12330 }
12331 
12332 /* Get interface broadcast address. */
12333 /* ARGSUSED */
12334 int
12335 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12336     ip_ioctl_cmd_t *ipip, void *if_req)
12337 {
12338 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12339 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12340 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12341 		return (EADDRNOTAVAIL);
12342 
12343 	/* IPIF_BROADCAST not possible with IPv6 */
12344 	ASSERT(!ipif->ipif_isv6);
12345 	*sin = sin_null;
12346 	sin->sin_family = AF_INET;
12347 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12348 	return (0);
12349 }
12350 
12351 /*
12352  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12353  */
12354 /* ARGSUSED */
12355 int
12356 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12357     ip_ioctl_cmd_t *ipip, void *if_req)
12358 {
12359 	int err = 0;
12360 	in6_addr_t v6mask;
12361 
12362 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12363 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12364 
12365 	ASSERT(IAM_WRITER_IPIF(ipif));
12366 
12367 	if (ipif->ipif_isv6) {
12368 		sin6_t *sin6;
12369 
12370 		if (sin->sin_family != AF_INET6)
12371 			return (EAFNOSUPPORT);
12372 
12373 		sin6 = (sin6_t *)sin;
12374 		v6mask = sin6->sin6_addr;
12375 	} else {
12376 		ipaddr_t mask;
12377 
12378 		if (sin->sin_family != AF_INET)
12379 			return (EAFNOSUPPORT);
12380 
12381 		mask = sin->sin_addr.s_addr;
12382 		V4MASK_TO_V6(mask, v6mask);
12383 	}
12384 
12385 	/*
12386 	 * No big deal if the interface isn't already up, or the mask
12387 	 * isn't really changing, or this is pt-pt.
12388 	 */
12389 	if (!(ipif->ipif_flags & IPIF_UP) ||
12390 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12391 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12392 		ipif->ipif_v6net_mask = v6mask;
12393 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12394 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12395 			    ipif->ipif_v6net_mask,
12396 			    ipif->ipif_v6subnet);
12397 		}
12398 		return (0);
12399 	}
12400 	/*
12401 	 * Make sure we have valid net and subnet broadcast ire's
12402 	 * for the old netmask, if needed by other logical interfaces.
12403 	 */
12404 	if (!ipif->ipif_isv6)
12405 		ipif_check_bcast_ires(ipif);
12406 
12407 	err = ipif_logical_down(ipif, q, mp);
12408 	if (err == EINPROGRESS)
12409 		return (err);
12410 	ipif_down_tail(ipif);
12411 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12412 	return (err);
12413 }
12414 
12415 static int
12416 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12417 {
12418 	in6_addr_t v6mask;
12419 	int err = 0;
12420 
12421 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12422 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12423 
12424 	if (ipif->ipif_isv6) {
12425 		sin6_t *sin6;
12426 
12427 		sin6 = (sin6_t *)sin;
12428 		v6mask = sin6->sin6_addr;
12429 	} else {
12430 		ipaddr_t mask;
12431 
12432 		mask = sin->sin_addr.s_addr;
12433 		V4MASK_TO_V6(mask, v6mask);
12434 	}
12435 
12436 	ipif->ipif_v6net_mask = v6mask;
12437 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12438 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12439 		    ipif->ipif_v6subnet);
12440 	}
12441 	err = ipif_up(ipif, q, mp);
12442 
12443 	if (err == 0 || err == EINPROGRESS) {
12444 		/*
12445 		 * The interface must be DL_BOUND if this packet has to
12446 		 * go out on the wire. Since we only go through a logical
12447 		 * down and are bound with the driver during an internal
12448 		 * down/up that is satisfied.
12449 		 */
12450 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12451 			/* Potentially broadcast an address mask reply. */
12452 			ipif_mask_reply(ipif);
12453 		}
12454 	}
12455 	return (err);
12456 }
12457 
12458 /* ARGSUSED */
12459 int
12460 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12461     ip_ioctl_cmd_t *ipip, void *if_req)
12462 {
12463 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12464 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12465 	ipif_down_tail(ipif);
12466 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12467 }
12468 
12469 /* Get interface net mask. */
12470 /* ARGSUSED */
12471 int
12472 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12473     ip_ioctl_cmd_t *ipip, void *if_req)
12474 {
12475 	struct lifreq *lifr = (struct lifreq *)if_req;
12476 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12477 
12478 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12479 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12480 
12481 	/*
12482 	 * net mask can't change since we have a reference to the ipif.
12483 	 */
12484 	if (ipif->ipif_isv6) {
12485 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12486 		*sin6 = sin6_null;
12487 		sin6->sin6_family = AF_INET6;
12488 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12489 		lifr->lifr_addrlen =
12490 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12491 	} else {
12492 		*sin = sin_null;
12493 		sin->sin_family = AF_INET;
12494 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12495 		if (ipip->ipi_cmd_type == LIF_CMD) {
12496 			lifr->lifr_addrlen =
12497 			    ip_mask_to_plen(ipif->ipif_net_mask);
12498 		}
12499 	}
12500 	return (0);
12501 }
12502 
12503 /* ARGSUSED */
12504 int
12505 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12506     ip_ioctl_cmd_t *ipip, void *if_req)
12507 {
12508 
12509 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12510 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12511 	/*
12512 	 * Set interface metric.  We don't use this for
12513 	 * anything but we keep track of it in case it is
12514 	 * important to routing applications or such.
12515 	 */
12516 	if (ipip->ipi_cmd_type == IF_CMD) {
12517 		struct ifreq    *ifr;
12518 
12519 		ifr = (struct ifreq *)if_req;
12520 		ipif->ipif_metric = ifr->ifr_metric;
12521 	} else {
12522 		struct lifreq   *lifr;
12523 
12524 		lifr = (struct lifreq *)if_req;
12525 		ipif->ipif_metric = lifr->lifr_metric;
12526 	}
12527 	return (0);
12528 }
12529 
12530 
12531 /* ARGSUSED */
12532 int
12533 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12534     ip_ioctl_cmd_t *ipip, void *if_req)
12535 {
12536 
12537 	/* Get interface metric. */
12538 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12539 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12540 	if (ipip->ipi_cmd_type == IF_CMD) {
12541 		struct ifreq    *ifr;
12542 
12543 		ifr = (struct ifreq *)if_req;
12544 		ifr->ifr_metric = ipif->ipif_metric;
12545 	} else {
12546 		struct lifreq   *lifr;
12547 
12548 		lifr = (struct lifreq *)if_req;
12549 		lifr->lifr_metric = ipif->ipif_metric;
12550 	}
12551 
12552 	return (0);
12553 }
12554 
12555 /* ARGSUSED */
12556 int
12557 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12558     ip_ioctl_cmd_t *ipip, void *if_req)
12559 {
12560 
12561 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12562 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12563 	/*
12564 	 * Set the muxid returned from I_PLINK.
12565 	 */
12566 	if (ipip->ipi_cmd_type == IF_CMD) {
12567 		struct ifreq *ifr = (struct ifreq *)if_req;
12568 
12569 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12570 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12571 	} else {
12572 		struct lifreq *lifr = (struct lifreq *)if_req;
12573 
12574 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12575 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12576 	}
12577 	return (0);
12578 }
12579 
12580 /* ARGSUSED */
12581 int
12582 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12583     ip_ioctl_cmd_t *ipip, void *if_req)
12584 {
12585 
12586 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12587 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12588 	/*
12589 	 * Get the muxid saved in ill for I_PUNLINK.
12590 	 */
12591 	if (ipip->ipi_cmd_type == IF_CMD) {
12592 		struct ifreq *ifr = (struct ifreq *)if_req;
12593 
12594 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12595 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12596 	} else {
12597 		struct lifreq *lifr = (struct lifreq *)if_req;
12598 
12599 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12600 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12601 	}
12602 	return (0);
12603 }
12604 
12605 /*
12606  * Set the subnet prefix. Does not modify the broadcast address.
12607  */
12608 /* ARGSUSED */
12609 int
12610 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12611     ip_ioctl_cmd_t *ipip, void *if_req)
12612 {
12613 	int err = 0;
12614 	in6_addr_t v6addr;
12615 	in6_addr_t v6mask;
12616 	boolean_t need_up = B_FALSE;
12617 	int addrlen;
12618 
12619 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12620 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12621 
12622 	ASSERT(IAM_WRITER_IPIF(ipif));
12623 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12624 
12625 	if (ipif->ipif_isv6) {
12626 		sin6_t *sin6;
12627 
12628 		if (sin->sin_family != AF_INET6)
12629 			return (EAFNOSUPPORT);
12630 
12631 		sin6 = (sin6_t *)sin;
12632 		v6addr = sin6->sin6_addr;
12633 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12634 			return (EADDRNOTAVAIL);
12635 	} else {
12636 		ipaddr_t addr;
12637 
12638 		if (sin->sin_family != AF_INET)
12639 			return (EAFNOSUPPORT);
12640 
12641 		addr = sin->sin_addr.s_addr;
12642 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12643 			return (EADDRNOTAVAIL);
12644 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12645 		/* Add 96 bits */
12646 		addrlen += IPV6_ABITS - IP_ABITS;
12647 	}
12648 
12649 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12650 		return (EINVAL);
12651 
12652 	/* Check if bits in the address is set past the mask */
12653 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12654 		return (EINVAL);
12655 
12656 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12657 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12658 		return (0);	/* No change */
12659 
12660 	if (ipif->ipif_flags & IPIF_UP) {
12661 		/*
12662 		 * If the interface is already marked up,
12663 		 * we call ipif_down which will take care
12664 		 * of ditching any IREs that have been set
12665 		 * up based on the old interface address.
12666 		 */
12667 		err = ipif_logical_down(ipif, q, mp);
12668 		if (err == EINPROGRESS)
12669 			return (err);
12670 		ipif_down_tail(ipif);
12671 		need_up = B_TRUE;
12672 	}
12673 
12674 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12675 	return (err);
12676 }
12677 
12678 static int
12679 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12680     queue_t *q, mblk_t *mp, boolean_t need_up)
12681 {
12682 	ill_t	*ill = ipif->ipif_ill;
12683 	int	err = 0;
12684 
12685 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12686 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12687 
12688 	/* Set the new address. */
12689 	mutex_enter(&ill->ill_lock);
12690 	ipif->ipif_v6net_mask = v6mask;
12691 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12692 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12693 		    ipif->ipif_v6subnet);
12694 	}
12695 	mutex_exit(&ill->ill_lock);
12696 
12697 	if (need_up) {
12698 		/*
12699 		 * Now bring the interface back up.  If this
12700 		 * is the only IPIF for the ILL, ipif_up
12701 		 * will have to re-bind to the device, so
12702 		 * we may get back EINPROGRESS, in which
12703 		 * case, this IOCTL will get completed in
12704 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12705 		 */
12706 		err = ipif_up(ipif, q, mp);
12707 		if (err == EINPROGRESS)
12708 			return (err);
12709 	}
12710 	return (err);
12711 }
12712 
12713 /* ARGSUSED */
12714 int
12715 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12716     ip_ioctl_cmd_t *ipip, void *if_req)
12717 {
12718 	int	addrlen;
12719 	in6_addr_t v6addr;
12720 	in6_addr_t v6mask;
12721 	struct lifreq *lifr = (struct lifreq *)if_req;
12722 
12723 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12724 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12725 	ipif_down_tail(ipif);
12726 
12727 	addrlen = lifr->lifr_addrlen;
12728 	if (ipif->ipif_isv6) {
12729 		sin6_t *sin6;
12730 
12731 		sin6 = (sin6_t *)sin;
12732 		v6addr = sin6->sin6_addr;
12733 	} else {
12734 		ipaddr_t addr;
12735 
12736 		addr = sin->sin_addr.s_addr;
12737 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12738 		addrlen += IPV6_ABITS - IP_ABITS;
12739 	}
12740 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12741 
12742 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12743 }
12744 
12745 /* ARGSUSED */
12746 int
12747 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12748     ip_ioctl_cmd_t *ipip, void *if_req)
12749 {
12750 	struct lifreq *lifr = (struct lifreq *)if_req;
12751 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12752 
12753 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12754 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12755 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12756 
12757 	if (ipif->ipif_isv6) {
12758 		*sin6 = sin6_null;
12759 		sin6->sin6_family = AF_INET6;
12760 		sin6->sin6_addr = ipif->ipif_v6subnet;
12761 		lifr->lifr_addrlen =
12762 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12763 	} else {
12764 		*sin = sin_null;
12765 		sin->sin_family = AF_INET;
12766 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12767 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12768 	}
12769 	return (0);
12770 }
12771 
12772 /*
12773  * Set the IPv6 address token.
12774  */
12775 /* ARGSUSED */
12776 int
12777 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12778     ip_ioctl_cmd_t *ipi, void *if_req)
12779 {
12780 	ill_t *ill = ipif->ipif_ill;
12781 	int err;
12782 	in6_addr_t v6addr;
12783 	in6_addr_t v6mask;
12784 	boolean_t need_up = B_FALSE;
12785 	int i;
12786 	sin6_t *sin6 = (sin6_t *)sin;
12787 	struct lifreq *lifr = (struct lifreq *)if_req;
12788 	int addrlen;
12789 
12790 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12791 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12792 	ASSERT(IAM_WRITER_IPIF(ipif));
12793 
12794 	addrlen = lifr->lifr_addrlen;
12795 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12796 	if (ipif->ipif_id != 0)
12797 		return (EINVAL);
12798 
12799 	if (!ipif->ipif_isv6)
12800 		return (EINVAL);
12801 
12802 	if (addrlen > IPV6_ABITS)
12803 		return (EINVAL);
12804 
12805 	v6addr = sin6->sin6_addr;
12806 
12807 	/*
12808 	 * The length of the token is the length from the end.  To get
12809 	 * the proper mask for this, compute the mask of the bits not
12810 	 * in the token; ie. the prefix, and then xor to get the mask.
12811 	 */
12812 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12813 		return (EINVAL);
12814 	for (i = 0; i < 4; i++) {
12815 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12816 	}
12817 
12818 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12819 	    ill->ill_token_length == addrlen)
12820 		return (0);	/* No change */
12821 
12822 	if (ipif->ipif_flags & IPIF_UP) {
12823 		err = ipif_logical_down(ipif, q, mp);
12824 		if (err == EINPROGRESS)
12825 			return (err);
12826 		ipif_down_tail(ipif);
12827 		need_up = B_TRUE;
12828 	}
12829 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12830 	return (err);
12831 }
12832 
12833 static int
12834 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12835     mblk_t *mp, boolean_t need_up)
12836 {
12837 	in6_addr_t v6addr;
12838 	in6_addr_t v6mask;
12839 	ill_t	*ill = ipif->ipif_ill;
12840 	int	i;
12841 	int	err = 0;
12842 
12843 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12844 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12845 	v6addr = sin6->sin6_addr;
12846 	/*
12847 	 * The length of the token is the length from the end.  To get
12848 	 * the proper mask for this, compute the mask of the bits not
12849 	 * in the token; ie. the prefix, and then xor to get the mask.
12850 	 */
12851 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12852 	for (i = 0; i < 4; i++)
12853 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12854 
12855 	mutex_enter(&ill->ill_lock);
12856 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12857 	ill->ill_token_length = addrlen;
12858 	mutex_exit(&ill->ill_lock);
12859 
12860 	if (need_up) {
12861 		/*
12862 		 * Now bring the interface back up.  If this
12863 		 * is the only IPIF for the ILL, ipif_up
12864 		 * will have to re-bind to the device, so
12865 		 * we may get back EINPROGRESS, in which
12866 		 * case, this IOCTL will get completed in
12867 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12868 		 */
12869 		err = ipif_up(ipif, q, mp);
12870 		if (err == EINPROGRESS)
12871 			return (err);
12872 	}
12873 	return (err);
12874 }
12875 
12876 /* ARGSUSED */
12877 int
12878 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12879     ip_ioctl_cmd_t *ipi, void *if_req)
12880 {
12881 	ill_t *ill;
12882 	sin6_t *sin6 = (sin6_t *)sin;
12883 	struct lifreq *lifr = (struct lifreq *)if_req;
12884 
12885 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12886 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12887 	if (ipif->ipif_id != 0)
12888 		return (EINVAL);
12889 
12890 	ill = ipif->ipif_ill;
12891 	if (!ill->ill_isv6)
12892 		return (ENXIO);
12893 
12894 	*sin6 = sin6_null;
12895 	sin6->sin6_family = AF_INET6;
12896 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12897 	sin6->sin6_addr = ill->ill_token;
12898 	lifr->lifr_addrlen = ill->ill_token_length;
12899 	return (0);
12900 }
12901 
12902 /*
12903  * Set (hardware) link specific information that might override
12904  * what was acquired through the DL_INFO_ACK.
12905  * The logic is as follows.
12906  *
12907  * become exclusive
12908  * set CHANGING flag
12909  * change mtu on affected IREs
12910  * clear CHANGING flag
12911  *
12912  * An ire add that occurs before the CHANGING flag is set will have its mtu
12913  * changed by the ip_sioctl_lnkinfo.
12914  *
12915  * During the time the CHANGING flag is set, no new ires will be added to the
12916  * bucket, and ire add will fail (due the CHANGING flag).
12917  *
12918  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12919  * before it is added to the bucket.
12920  *
12921  * Obviously only 1 thread can set the CHANGING flag and we need to become
12922  * exclusive to set the flag.
12923  */
12924 /* ARGSUSED */
12925 int
12926 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12927     ip_ioctl_cmd_t *ipi, void *if_req)
12928 {
12929 	ill_t		*ill = ipif->ipif_ill;
12930 	ipif_t		*nipif;
12931 	int		ip_min_mtu;
12932 	boolean_t	mtu_walk = B_FALSE;
12933 	struct lifreq	*lifr = (struct lifreq *)if_req;
12934 	lif_ifinfo_req_t *lir;
12935 	ire_t		*ire;
12936 
12937 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12938 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12939 	lir = &lifr->lifr_ifinfo;
12940 	ASSERT(IAM_WRITER_IPIF(ipif));
12941 
12942 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12943 	if (ipif->ipif_id != 0)
12944 		return (EINVAL);
12945 
12946 	/* Set interface MTU. */
12947 	if (ipif->ipif_isv6)
12948 		ip_min_mtu = IPV6_MIN_MTU;
12949 	else
12950 		ip_min_mtu = IP_MIN_MTU;
12951 
12952 	/*
12953 	 * Verify values before we set anything. Allow zero to
12954 	 * mean unspecified.
12955 	 */
12956 	if (lir->lir_maxmtu != 0 &&
12957 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12958 	    lir->lir_maxmtu < ip_min_mtu))
12959 		return (EINVAL);
12960 	if (lir->lir_reachtime != 0 &&
12961 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12962 		return (EINVAL);
12963 	if (lir->lir_reachretrans != 0 &&
12964 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12965 		return (EINVAL);
12966 
12967 	mutex_enter(&ill->ill_lock);
12968 	ill->ill_state_flags |= ILL_CHANGING;
12969 	for (nipif = ill->ill_ipif; nipif != NULL;
12970 	    nipif = nipif->ipif_next) {
12971 		nipif->ipif_state_flags |= IPIF_CHANGING;
12972 	}
12973 
12974 	mutex_exit(&ill->ill_lock);
12975 
12976 	if (lir->lir_maxmtu != 0) {
12977 		ill->ill_max_mtu = lir->lir_maxmtu;
12978 		ill->ill_mtu_userspecified = 1;
12979 		mtu_walk = B_TRUE;
12980 	}
12981 
12982 	if (lir->lir_reachtime != 0)
12983 		ill->ill_reachable_time = lir->lir_reachtime;
12984 
12985 	if (lir->lir_reachretrans != 0)
12986 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12987 
12988 	ill->ill_max_hops = lir->lir_maxhops;
12989 
12990 	ill->ill_max_buf = ND_MAX_Q;
12991 
12992 	if (mtu_walk) {
12993 		/*
12994 		 * Set the MTU on all ipifs associated with this ill except
12995 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12996 		 */
12997 		for (nipif = ill->ill_ipif; nipif != NULL;
12998 		    nipif = nipif->ipif_next) {
12999 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13000 				continue;
13001 
13002 			nipif->ipif_mtu = ill->ill_max_mtu;
13003 
13004 			if (!(nipif->ipif_flags & IPIF_UP))
13005 				continue;
13006 
13007 			if (nipif->ipif_isv6)
13008 				ire = ipif_to_ire_v6(nipif);
13009 			else
13010 				ire = ipif_to_ire(nipif);
13011 			if (ire != NULL) {
13012 				ire->ire_max_frag = ipif->ipif_mtu;
13013 				ire_refrele(ire);
13014 			}
13015 			if (ill->ill_isv6) {
13016 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13017 				    ipif_mtu_change, (char *)nipif,
13018 				    ill);
13019 			} else {
13020 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13021 				    ipif_mtu_change, (char *)nipif,
13022 				    ill);
13023 			}
13024 		}
13025 	}
13026 
13027 	mutex_enter(&ill->ill_lock);
13028 	for (nipif = ill->ill_ipif; nipif != NULL;
13029 	    nipif = nipif->ipif_next) {
13030 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13031 	}
13032 	ILL_UNMARK_CHANGING(ill);
13033 	mutex_exit(&ill->ill_lock);
13034 
13035 	return (0);
13036 }
13037 
13038 /* ARGSUSED */
13039 int
13040 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13041     ip_ioctl_cmd_t *ipi, void *if_req)
13042 {
13043 	struct lif_ifinfo_req *lir;
13044 	ill_t *ill = ipif->ipif_ill;
13045 
13046 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13047 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13048 	if (ipif->ipif_id != 0)
13049 		return (EINVAL);
13050 
13051 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13052 	lir->lir_maxhops = ill->ill_max_hops;
13053 	lir->lir_reachtime = ill->ill_reachable_time;
13054 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13055 	lir->lir_maxmtu = ill->ill_max_mtu;
13056 
13057 	return (0);
13058 }
13059 
13060 /*
13061  * Return best guess as to the subnet mask for the specified address.
13062  * Based on the subnet masks for all the configured interfaces.
13063  *
13064  * We end up returning a zero mask in the case of default, multicast or
13065  * experimental.
13066  */
13067 static ipaddr_t
13068 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13069 {
13070 	ipaddr_t net_mask;
13071 	ill_t	*ill;
13072 	ipif_t	*ipif;
13073 	ill_walk_context_t ctx;
13074 	ipif_t	*fallback_ipif = NULL;
13075 
13076 	net_mask = ip_net_mask(addr);
13077 	if (net_mask == 0) {
13078 		*ipifp = NULL;
13079 		return (0);
13080 	}
13081 
13082 	/* Let's check to see if this is maybe a local subnet route. */
13083 	/* this function only applies to IPv4 interfaces */
13084 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13085 	ill = ILL_START_WALK_V4(&ctx, ipst);
13086 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13087 		mutex_enter(&ill->ill_lock);
13088 		for (ipif = ill->ill_ipif; ipif != NULL;
13089 		    ipif = ipif->ipif_next) {
13090 			if (!IPIF_CAN_LOOKUP(ipif))
13091 				continue;
13092 			if (!(ipif->ipif_flags & IPIF_UP))
13093 				continue;
13094 			if ((ipif->ipif_subnet & net_mask) ==
13095 			    (addr & net_mask)) {
13096 				/*
13097 				 * Don't trust pt-pt interfaces if there are
13098 				 * other interfaces.
13099 				 */
13100 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13101 					if (fallback_ipif == NULL) {
13102 						ipif_refhold_locked(ipif);
13103 						fallback_ipif = ipif;
13104 					}
13105 					continue;
13106 				}
13107 
13108 				/*
13109 				 * Fine. Just assume the same net mask as the
13110 				 * directly attached subnet interface is using.
13111 				 */
13112 				ipif_refhold_locked(ipif);
13113 				mutex_exit(&ill->ill_lock);
13114 				rw_exit(&ipst->ips_ill_g_lock);
13115 				if (fallback_ipif != NULL)
13116 					ipif_refrele(fallback_ipif);
13117 				*ipifp = ipif;
13118 				return (ipif->ipif_net_mask);
13119 			}
13120 		}
13121 		mutex_exit(&ill->ill_lock);
13122 	}
13123 	rw_exit(&ipst->ips_ill_g_lock);
13124 
13125 	*ipifp = fallback_ipif;
13126 	return ((fallback_ipif != NULL) ?
13127 	    fallback_ipif->ipif_net_mask : net_mask);
13128 }
13129 
13130 /*
13131  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13132  */
13133 static void
13134 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13135 {
13136 	IOCP	iocp;
13137 	ipft_t	*ipft;
13138 	ipllc_t	*ipllc;
13139 	mblk_t	*mp1;
13140 	cred_t	*cr;
13141 	int	error = 0;
13142 	conn_t	*connp;
13143 
13144 	ip1dbg(("ip_wput_ioctl"));
13145 	iocp = (IOCP)mp->b_rptr;
13146 	mp1 = mp->b_cont;
13147 	if (mp1 == NULL) {
13148 		iocp->ioc_error = EINVAL;
13149 		mp->b_datap->db_type = M_IOCNAK;
13150 		iocp->ioc_count = 0;
13151 		qreply(q, mp);
13152 		return;
13153 	}
13154 
13155 	/*
13156 	 * These IOCTLs provide various control capabilities to
13157 	 * upstream agents such as ULPs and processes.	There
13158 	 * are currently two such IOCTLs implemented.  They
13159 	 * are used by TCP to provide update information for
13160 	 * existing IREs and to forcibly delete an IRE for a
13161 	 * host that is not responding, thereby forcing an
13162 	 * attempt at a new route.
13163 	 */
13164 	iocp->ioc_error = EINVAL;
13165 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13166 		goto done;
13167 
13168 	ipllc = (ipllc_t *)mp1->b_rptr;
13169 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13170 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13171 			break;
13172 	}
13173 	/*
13174 	 * prefer credential from mblk over ioctl;
13175 	 * see ip_sioctl_copyin_setup
13176 	 */
13177 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13178 
13179 	/*
13180 	 * Refhold the conn in case the request gets queued up in some lookup
13181 	 */
13182 	ASSERT(CONN_Q(q));
13183 	connp = Q_TO_CONN(q);
13184 	CONN_INC_REF(connp);
13185 	if (ipft->ipft_pfi &&
13186 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13187 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13188 		error = (*ipft->ipft_pfi)(q,
13189 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13190 	}
13191 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13192 		/*
13193 		 * CONN_OPER_PENDING_DONE happens in the function called
13194 		 * through ipft_pfi above.
13195 		 */
13196 		return;
13197 	}
13198 
13199 	CONN_OPER_PENDING_DONE(connp);
13200 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13201 		freemsg(mp);
13202 		return;
13203 	}
13204 	iocp->ioc_error = error;
13205 
13206 done:
13207 	mp->b_datap->db_type = M_IOCACK;
13208 	if (iocp->ioc_error)
13209 		iocp->ioc_count = 0;
13210 	qreply(q, mp);
13211 }
13212 
13213 /*
13214  * Lookup an ipif using the sequence id (ipif_seqid)
13215  */
13216 ipif_t *
13217 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13218 {
13219 	ipif_t *ipif;
13220 
13221 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13222 
13223 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13224 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13225 			return (ipif);
13226 	}
13227 	return (NULL);
13228 }
13229 
13230 /*
13231  * Assign a unique id for the ipif. This is used later when we send
13232  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13233  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13234  * IRE is added, we verify that ipif has not disappeared.
13235  */
13236 
13237 static void
13238 ipif_assign_seqid(ipif_t *ipif)
13239 {
13240 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13241 
13242 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13243 }
13244 
13245 /*
13246  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13247  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13248  * be inserted into the first space available in the list. The value of
13249  * ipif_id will then be set to the appropriate value for its position.
13250  */
13251 static int
13252 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13253 {
13254 	ill_t *ill;
13255 	ipif_t *tipif;
13256 	ipif_t **tipifp;
13257 	int id;
13258 	ip_stack_t	*ipst;
13259 
13260 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13261 	    IAM_WRITER_IPIF(ipif));
13262 
13263 	ill = ipif->ipif_ill;
13264 	ASSERT(ill != NULL);
13265 	ipst = ill->ill_ipst;
13266 
13267 	/*
13268 	 * In the case of lo0:0 we already hold the ill_g_lock.
13269 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13270 	 * ipif_insert. Another such caller is ipif_move.
13271 	 */
13272 	if (acquire_g_lock)
13273 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13274 	if (acquire_ill_lock)
13275 		mutex_enter(&ill->ill_lock);
13276 	id = ipif->ipif_id;
13277 	tipifp = &(ill->ill_ipif);
13278 	if (id == -1) {	/* need to find a real id */
13279 		id = 0;
13280 		while ((tipif = *tipifp) != NULL) {
13281 			ASSERT(tipif->ipif_id >= id);
13282 			if (tipif->ipif_id != id)
13283 				break; /* non-consecutive id */
13284 			id++;
13285 			tipifp = &(tipif->ipif_next);
13286 		}
13287 		/* limit number of logical interfaces */
13288 		if (id >= ipst->ips_ip_addrs_per_if) {
13289 			if (acquire_ill_lock)
13290 				mutex_exit(&ill->ill_lock);
13291 			if (acquire_g_lock)
13292 				rw_exit(&ipst->ips_ill_g_lock);
13293 			return (-1);
13294 		}
13295 		ipif->ipif_id = id; /* assign new id */
13296 	} else if (id < ipst->ips_ip_addrs_per_if) {
13297 		/* we have a real id; insert ipif in the right place */
13298 		while ((tipif = *tipifp) != NULL) {
13299 			ASSERT(tipif->ipif_id != id);
13300 			if (tipif->ipif_id > id)
13301 				break; /* found correct location */
13302 			tipifp = &(tipif->ipif_next);
13303 		}
13304 	} else {
13305 		if (acquire_ill_lock)
13306 			mutex_exit(&ill->ill_lock);
13307 		if (acquire_g_lock)
13308 			rw_exit(&ipst->ips_ill_g_lock);
13309 		return (-1);
13310 	}
13311 
13312 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13313 
13314 	ipif->ipif_next = tipif;
13315 	*tipifp = ipif;
13316 	if (acquire_ill_lock)
13317 		mutex_exit(&ill->ill_lock);
13318 	if (acquire_g_lock)
13319 		rw_exit(&ipst->ips_ill_g_lock);
13320 	return (0);
13321 }
13322 
13323 static void
13324 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13325 {
13326 	ipif_t	**ipifp;
13327 	ill_t	*ill = ipif->ipif_ill;
13328 
13329 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13330 	if (acquire_ill_lock)
13331 		mutex_enter(&ill->ill_lock);
13332 	else
13333 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13334 
13335 	ipifp = &ill->ill_ipif;
13336 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13337 		if (*ipifp == ipif) {
13338 			*ipifp = ipif->ipif_next;
13339 			break;
13340 		}
13341 	}
13342 
13343 	if (acquire_ill_lock)
13344 		mutex_exit(&ill->ill_lock);
13345 }
13346 
13347 /*
13348  * Allocate and initialize a new interface control structure.  (Always
13349  * called as writer.)
13350  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13351  * is not part of the global linked list of ills. ipif_seqid is unique
13352  * in the system and to preserve the uniqueness, it is assigned only
13353  * when ill becomes part of the global list. At that point ill will
13354  * have a name. If it doesn't get assigned here, it will get assigned
13355  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13356  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13357  * the interface flags or any other information from the DL_INFO_ACK for
13358  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13359  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13360  * second DL_INFO_ACK comes in from the driver.
13361  */
13362 static ipif_t *
13363 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13364 {
13365 	ipif_t	*ipif;
13366 	phyint_t *phyi;
13367 
13368 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13369 	    ill->ill_name, id, (void *)ill));
13370 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13371 
13372 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13373 		return (NULL);
13374 	*ipif = ipif_zero;	/* start clean */
13375 
13376 	ipif->ipif_ill = ill;
13377 	ipif->ipif_id = id;	/* could be -1 */
13378 	/*
13379 	 * Inherit the zoneid from the ill; for the shared stack instance
13380 	 * this is always the global zone
13381 	 */
13382 	ipif->ipif_zoneid = ill->ill_zoneid;
13383 
13384 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13385 
13386 	ipif->ipif_refcnt = 0;
13387 	ipif->ipif_saved_ire_cnt = 0;
13388 
13389 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13390 		mi_free(ipif);
13391 		return (NULL);
13392 	}
13393 	/* -1 id should have been replaced by real id */
13394 	id = ipif->ipif_id;
13395 	ASSERT(id >= 0);
13396 
13397 	if (ill->ill_name[0] != '\0')
13398 		ipif_assign_seqid(ipif);
13399 
13400 	/*
13401 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13402 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13403 	 * ioctl sets ipif_orig_ipifid to zero.
13404 	 */
13405 	ipif->ipif_orig_ipifid = id;
13406 
13407 	/*
13408 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13409 	 * The ipif is still not up and can't be looked up until the
13410 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13411 	 */
13412 	mutex_enter(&ill->ill_lock);
13413 	mutex_enter(&ill->ill_phyint->phyint_lock);
13414 	/*
13415 	 * Set the running flag when logical interface zero is created.
13416 	 * For subsequent logical interfaces, a DLPI link down
13417 	 * notification message may have cleared the running flag to
13418 	 * indicate the link is down, so we shouldn't just blindly set it.
13419 	 */
13420 	if (id == 0)
13421 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13422 	ipif->ipif_ire_type = ire_type;
13423 	phyi = ill->ill_phyint;
13424 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13425 
13426 	if (ipif->ipif_isv6) {
13427 		ill->ill_flags |= ILLF_IPV6;
13428 	} else {
13429 		ipaddr_t inaddr_any = INADDR_ANY;
13430 
13431 		ill->ill_flags |= ILLF_IPV4;
13432 
13433 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13434 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13435 		    &ipif->ipif_v6lcl_addr);
13436 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13437 		    &ipif->ipif_v6src_addr);
13438 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13439 		    &ipif->ipif_v6subnet);
13440 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13441 		    &ipif->ipif_v6net_mask);
13442 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13443 		    &ipif->ipif_v6brd_addr);
13444 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13445 		    &ipif->ipif_v6pp_dst_addr);
13446 	}
13447 
13448 	/*
13449 	 * Don't set the interface flags etc. now, will do it in
13450 	 * ip_ll_subnet_defaults.
13451 	 */
13452 	if (!initialize) {
13453 		mutex_exit(&ill->ill_lock);
13454 		mutex_exit(&ill->ill_phyint->phyint_lock);
13455 		return (ipif);
13456 	}
13457 	ipif->ipif_mtu = ill->ill_max_mtu;
13458 
13459 	if (ill->ill_bcast_addr_length != 0) {
13460 		/*
13461 		 * Later detect lack of DLPI driver multicast
13462 		 * capability by catching DL_ENABMULTI errors in
13463 		 * ip_rput_dlpi.
13464 		 */
13465 		ill->ill_flags |= ILLF_MULTICAST;
13466 		if (!ipif->ipif_isv6)
13467 			ipif->ipif_flags |= IPIF_BROADCAST;
13468 	} else {
13469 		if (ill->ill_net_type != IRE_LOOPBACK) {
13470 			if (ipif->ipif_isv6)
13471 				/*
13472 				 * Note: xresolv interfaces will eventually need
13473 				 * NOARP set here as well, but that will require
13474 				 * those external resolvers to have some
13475 				 * knowledge of that flag and act appropriately.
13476 				 * Not to be changed at present.
13477 				 */
13478 				ill->ill_flags |= ILLF_NONUD;
13479 			else
13480 				ill->ill_flags |= ILLF_NOARP;
13481 		}
13482 		if (ill->ill_phys_addr_length == 0) {
13483 			if (ill->ill_media &&
13484 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13485 				ipif->ipif_flags |= IPIF_NOXMIT;
13486 				phyi->phyint_flags |= PHYI_VIRTUAL;
13487 			} else {
13488 				/* pt-pt supports multicast. */
13489 				ill->ill_flags |= ILLF_MULTICAST;
13490 				if (ill->ill_net_type == IRE_LOOPBACK) {
13491 					phyi->phyint_flags |=
13492 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13493 				} else {
13494 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13495 				}
13496 			}
13497 		}
13498 	}
13499 	mutex_exit(&ill->ill_lock);
13500 	mutex_exit(&ill->ill_phyint->phyint_lock);
13501 	return (ipif);
13502 }
13503 
13504 /*
13505  * If appropriate, send a message up to the resolver delete the entry
13506  * for the address of this interface which is going out of business.
13507  * (Always called as writer).
13508  *
13509  * NOTE : We need to check for NULL mps as some of the fields are
13510  *	  initialized only for some interface types. See ipif_resolver_up()
13511  *	  for details.
13512  */
13513 void
13514 ipif_arp_down(ipif_t *ipif)
13515 {
13516 	mblk_t	*mp;
13517 	ill_t	*ill = ipif->ipif_ill;
13518 
13519 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13520 	ASSERT(IAM_WRITER_IPIF(ipif));
13521 
13522 	/* Delete the mapping for the local address */
13523 	mp = ipif->ipif_arp_del_mp;
13524 	if (mp != NULL) {
13525 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13526 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13527 		putnext(ill->ill_rq, mp);
13528 		ipif->ipif_arp_del_mp = NULL;
13529 	}
13530 
13531 	/*
13532 	 * If this is the last ipif that is going down and there are no
13533 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13534 	 * clean up ARP completely.
13535 	 */
13536 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13537 
13538 		/* Send up AR_INTERFACE_DOWN message */
13539 		mp = ill->ill_arp_down_mp;
13540 		if (mp != NULL) {
13541 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13542 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13543 			    ipif->ipif_id));
13544 			putnext(ill->ill_rq, mp);
13545 			ill->ill_arp_down_mp = NULL;
13546 		}
13547 
13548 		/* Tell ARP to delete the multicast mappings */
13549 		mp = ill->ill_arp_del_mapping_mp;
13550 		if (mp != NULL) {
13551 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13552 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13553 			    ipif->ipif_id));
13554 			putnext(ill->ill_rq, mp);
13555 			ill->ill_arp_del_mapping_mp = NULL;
13556 		}
13557 	}
13558 }
13559 
13560 /*
13561  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13562  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13563  * that it wants the add_mp allocated in this function to be returned
13564  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13565  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13566  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13567  * as it does a ipif_arp_down after calling this function - which will
13568  * remove what we add here.
13569  *
13570  * Returns -1 on failures and 0 on success.
13571  */
13572 int
13573 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13574 {
13575 	mblk_t	*del_mp = NULL;
13576 	mblk_t *add_mp = NULL;
13577 	mblk_t *mp;
13578 	ill_t	*ill = ipif->ipif_ill;
13579 	phyint_t *phyi = ill->ill_phyint;
13580 	ipaddr_t addr, mask, extract_mask = 0;
13581 	arma_t	*arma;
13582 	uint8_t *maddr, *bphys_addr;
13583 	uint32_t hw_start;
13584 	dl_unitdata_req_t *dlur;
13585 
13586 	ASSERT(IAM_WRITER_IPIF(ipif));
13587 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13588 		return (0);
13589 
13590 	/*
13591 	 * Delete the existing mapping from ARP. Normally ipif_down
13592 	 * -> ipif_arp_down should send this up to ARP. The only
13593 	 * reason we would find this when we are switching from
13594 	 * Multicast to Broadcast where we did not do a down.
13595 	 */
13596 	mp = ill->ill_arp_del_mapping_mp;
13597 	if (mp != NULL) {
13598 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13599 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13600 		putnext(ill->ill_rq, mp);
13601 		ill->ill_arp_del_mapping_mp = NULL;
13602 	}
13603 
13604 	if (arp_add_mapping_mp != NULL)
13605 		*arp_add_mapping_mp = NULL;
13606 
13607 	/*
13608 	 * Check that the address is not to long for the constant
13609 	 * length reserved in the template arma_t.
13610 	 */
13611 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13612 		return (-1);
13613 
13614 	/* Add mapping mblk */
13615 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13616 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13617 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13618 	    (caddr_t)&addr);
13619 	if (add_mp == NULL)
13620 		return (-1);
13621 	arma = (arma_t *)add_mp->b_rptr;
13622 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13623 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13624 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13625 
13626 	/*
13627 	 * Determine the broadcast address.
13628 	 */
13629 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13630 	if (ill->ill_sap_length < 0)
13631 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13632 	else
13633 		bphys_addr = (uchar_t *)dlur +
13634 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13635 	/*
13636 	 * Check PHYI_MULTI_BCAST and length of physical
13637 	 * address to determine if we use the mapping or the
13638 	 * broadcast address.
13639 	 */
13640 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13641 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13642 		    bphys_addr, maddr, &hw_start, &extract_mask))
13643 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13644 
13645 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13646 	    (ill->ill_flags & ILLF_MULTICAST)) {
13647 		/* Make sure this will not match the "exact" entry. */
13648 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13649 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13650 		    (caddr_t)&addr);
13651 		if (del_mp == NULL) {
13652 			freemsg(add_mp);
13653 			return (-1);
13654 		}
13655 		bcopy(&extract_mask, (char *)arma +
13656 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13657 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13658 			/* Use link-layer broadcast address for MULTI_BCAST */
13659 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13660 			ip2dbg(("ipif_arp_setup_multicast: adding"
13661 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13662 		} else {
13663 			arma->arma_hw_mapping_start = hw_start;
13664 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13665 			    " ARP setup for %s\n", ill->ill_name));
13666 		}
13667 	} else {
13668 		freemsg(add_mp);
13669 		ASSERT(del_mp == NULL);
13670 		/* It is neither MULTICAST nor MULTI_BCAST */
13671 		return (0);
13672 	}
13673 	ASSERT(add_mp != NULL && del_mp != NULL);
13674 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13675 	ill->ill_arp_del_mapping_mp = del_mp;
13676 	if (arp_add_mapping_mp != NULL) {
13677 		/* The caller just wants the mblks allocated */
13678 		*arp_add_mapping_mp = add_mp;
13679 	} else {
13680 		/* The caller wants us to send it to arp */
13681 		putnext(ill->ill_rq, add_mp);
13682 	}
13683 	return (0);
13684 }
13685 
13686 /*
13687  * Get the resolver set up for a new interface address.
13688  * (Always called as writer.)
13689  * Called both for IPv4 and IPv6 interfaces,
13690  * though it only sets up the resolver for v6
13691  * if it's an xresolv interface (one using an external resolver).
13692  * Honors ILLF_NOARP.
13693  * The enumerated value res_act is used to tune the behavior.
13694  * If set to Res_act_initial, then we set up all the resolver
13695  * structures for a new interface.  If set to Res_act_move, then
13696  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13697  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13698  * asynchronous hardware address change notification.  If set to
13699  * Res_act_defend, then we tell ARP that it needs to send a single
13700  * gratuitous message in defense of the address.
13701  * Returns error on failure.
13702  */
13703 int
13704 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13705 {
13706 	caddr_t	addr;
13707 	mblk_t	*arp_up_mp = NULL;
13708 	mblk_t	*arp_down_mp = NULL;
13709 	mblk_t	*arp_add_mp = NULL;
13710 	mblk_t	*arp_del_mp = NULL;
13711 	mblk_t	*arp_add_mapping_mp = NULL;
13712 	mblk_t	*arp_del_mapping_mp = NULL;
13713 	ill_t	*ill = ipif->ipif_ill;
13714 	uchar_t	*area_p = NULL;
13715 	uchar_t	*ared_p = NULL;
13716 	int	err = ENOMEM;
13717 	boolean_t was_dup;
13718 
13719 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13720 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13721 	ASSERT(IAM_WRITER_IPIF(ipif));
13722 
13723 	was_dup = B_FALSE;
13724 	if (res_act == Res_act_initial) {
13725 		ipif->ipif_addr_ready = 0;
13726 		/*
13727 		 * We're bringing an interface up here.  There's no way that we
13728 		 * should need to shut down ARP now.
13729 		 */
13730 		mutex_enter(&ill->ill_lock);
13731 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13732 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13733 			ill->ill_ipif_dup_count--;
13734 			was_dup = B_TRUE;
13735 		}
13736 		mutex_exit(&ill->ill_lock);
13737 	}
13738 	if (ipif->ipif_recovery_id != 0)
13739 		(void) untimeout(ipif->ipif_recovery_id);
13740 	ipif->ipif_recovery_id = 0;
13741 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13742 		ipif->ipif_addr_ready = 1;
13743 		return (0);
13744 	}
13745 	/* NDP will set the ipif_addr_ready flag when it's ready */
13746 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13747 		return (0);
13748 
13749 	if (ill->ill_isv6) {
13750 		/*
13751 		 * External resolver for IPv6
13752 		 */
13753 		ASSERT(res_act == Res_act_initial);
13754 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13755 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13756 			area_p = (uchar_t *)&ip6_area_template;
13757 			ared_p = (uchar_t *)&ip6_ared_template;
13758 		}
13759 	} else {
13760 		/*
13761 		 * IPv4 arp case. If the ARP stream has already started
13762 		 * closing, fail this request for ARP bringup. Else
13763 		 * record the fact that an ARP bringup is pending.
13764 		 */
13765 		mutex_enter(&ill->ill_lock);
13766 		if (ill->ill_arp_closing) {
13767 			mutex_exit(&ill->ill_lock);
13768 			err = EINVAL;
13769 			goto failed;
13770 		} else {
13771 			if (ill->ill_ipif_up_count == 0 &&
13772 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13773 				ill->ill_arp_bringup_pending = 1;
13774 			mutex_exit(&ill->ill_lock);
13775 		}
13776 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13777 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13778 			area_p = (uchar_t *)&ip_area_template;
13779 			ared_p = (uchar_t *)&ip_ared_template;
13780 		}
13781 	}
13782 
13783 	/*
13784 	 * Add an entry for the local address in ARP only if it
13785 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13786 	 */
13787 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13788 		area_t *area;
13789 
13790 		/* Now ask ARP to publish our address. */
13791 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13792 		if (arp_add_mp == NULL)
13793 			goto failed;
13794 		area = (area_t *)arp_add_mp->b_rptr;
13795 		if (res_act != Res_act_initial) {
13796 			/*
13797 			 * Copy the new hardware address and length into
13798 			 * arp_add_mp to be sent to ARP.
13799 			 */
13800 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13801 			bcopy(ill->ill_phys_addr,
13802 			    ((char *)area + area->area_hw_addr_offset),
13803 			    area->area_hw_addr_length);
13804 		}
13805 
13806 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13807 		    ACE_F_MYADDR;
13808 
13809 		if (res_act == Res_act_defend) {
13810 			area->area_flags |= ACE_F_DEFEND;
13811 			/*
13812 			 * If we're just defending our address now, then
13813 			 * there's no need to set up ARP multicast mappings.
13814 			 * The publish command is enough.
13815 			 */
13816 			goto done;
13817 		}
13818 
13819 		if (res_act != Res_act_initial)
13820 			goto arp_setup_multicast;
13821 
13822 		/*
13823 		 * Allocate an ARP deletion message so we know we can tell ARP
13824 		 * when the interface goes down.
13825 		 */
13826 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13827 		if (arp_del_mp == NULL)
13828 			goto failed;
13829 
13830 	} else {
13831 		if (res_act != Res_act_initial)
13832 			goto done;
13833 	}
13834 	/*
13835 	 * Need to bring up ARP or setup multicast mapping only
13836 	 * when the first interface is coming UP.
13837 	 */
13838 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13839 	    was_dup) {
13840 		goto done;
13841 	}
13842 
13843 	/*
13844 	 * Allocate an ARP down message (to be saved) and an ARP up
13845 	 * message.
13846 	 */
13847 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13848 	if (arp_down_mp == NULL)
13849 		goto failed;
13850 
13851 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13852 	if (arp_up_mp == NULL)
13853 		goto failed;
13854 
13855 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13856 		goto done;
13857 
13858 arp_setup_multicast:
13859 	/*
13860 	 * Setup the multicast mappings. This function initializes
13861 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13862 	 * IPv6.
13863 	 */
13864 	if (!ill->ill_isv6) {
13865 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13866 		if (err != 0)
13867 			goto failed;
13868 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13869 		ASSERT(arp_add_mapping_mp != NULL);
13870 	}
13871 
13872 done:
13873 	if (arp_del_mp != NULL) {
13874 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13875 		ipif->ipif_arp_del_mp = arp_del_mp;
13876 	}
13877 	if (arp_down_mp != NULL) {
13878 		ASSERT(ill->ill_arp_down_mp == NULL);
13879 		ill->ill_arp_down_mp = arp_down_mp;
13880 	}
13881 	if (arp_del_mapping_mp != NULL) {
13882 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13883 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13884 	}
13885 	if (arp_up_mp != NULL) {
13886 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13887 		    ill->ill_name, ipif->ipif_id));
13888 		putnext(ill->ill_rq, arp_up_mp);
13889 	}
13890 	if (arp_add_mp != NULL) {
13891 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13892 		    ill->ill_name, ipif->ipif_id));
13893 		/*
13894 		 * If it's an extended ARP implementation, then we'll wait to
13895 		 * hear that DAD has finished before using the interface.
13896 		 */
13897 		if (!ill->ill_arp_extend)
13898 			ipif->ipif_addr_ready = 1;
13899 		putnext(ill->ill_rq, arp_add_mp);
13900 	} else {
13901 		ipif->ipif_addr_ready = 1;
13902 	}
13903 	if (arp_add_mapping_mp != NULL) {
13904 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13905 		    ill->ill_name, ipif->ipif_id));
13906 		putnext(ill->ill_rq, arp_add_mapping_mp);
13907 	}
13908 	if (res_act != Res_act_initial)
13909 		return (0);
13910 
13911 	if (ill->ill_flags & ILLF_NOARP)
13912 		err = ill_arp_off(ill);
13913 	else
13914 		err = ill_arp_on(ill);
13915 	if (err != 0) {
13916 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13917 		freemsg(ipif->ipif_arp_del_mp);
13918 		freemsg(ill->ill_arp_down_mp);
13919 		freemsg(ill->ill_arp_del_mapping_mp);
13920 		ipif->ipif_arp_del_mp = NULL;
13921 		ill->ill_arp_down_mp = NULL;
13922 		ill->ill_arp_del_mapping_mp = NULL;
13923 		return (err);
13924 	}
13925 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13926 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13927 
13928 failed:
13929 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13930 	freemsg(arp_add_mp);
13931 	freemsg(arp_del_mp);
13932 	freemsg(arp_add_mapping_mp);
13933 	freemsg(arp_up_mp);
13934 	freemsg(arp_down_mp);
13935 	ill->ill_arp_bringup_pending = 0;
13936 	return (err);
13937 }
13938 
13939 /*
13940  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13941  * just gone back up.
13942  */
13943 static void
13944 ipif_arp_start_dad(ipif_t *ipif)
13945 {
13946 	ill_t *ill = ipif->ipif_ill;
13947 	mblk_t *arp_add_mp;
13948 	area_t *area;
13949 
13950 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13951 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13952 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13953 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13954 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13955 		/*
13956 		 * If we can't contact ARP for some reason, that's not really a
13957 		 * problem.  Just send out the routing socket notification that
13958 		 * DAD completion would have done, and continue.
13959 		 */
13960 		ipif_mask_reply(ipif);
13961 		ip_rts_ifmsg(ipif);
13962 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13963 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13964 		ipif->ipif_addr_ready = 1;
13965 		return;
13966 	}
13967 
13968 	/* Setting the 'unverified' flag restarts DAD */
13969 	area = (area_t *)arp_add_mp->b_rptr;
13970 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13971 	    ACE_F_UNVERIFIED;
13972 	putnext(ill->ill_rq, arp_add_mp);
13973 }
13974 
13975 static void
13976 ipif_ndp_start_dad(ipif_t *ipif)
13977 {
13978 	nce_t *nce;
13979 
13980 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13981 	if (nce == NULL)
13982 		return;
13983 
13984 	if (!ndp_restart_dad(nce)) {
13985 		/*
13986 		 * If we can't restart DAD for some reason, that's not really a
13987 		 * problem.  Just send out the routing socket notification that
13988 		 * DAD completion would have done, and continue.
13989 		 */
13990 		ip_rts_ifmsg(ipif);
13991 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13992 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13993 		ipif->ipif_addr_ready = 1;
13994 	}
13995 	NCE_REFRELE(nce);
13996 }
13997 
13998 /*
13999  * Restart duplicate address detection on all interfaces on the given ill.
14000  *
14001  * This is called when an interface transitions from down to up
14002  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14003  *
14004  * Note that since the underlying physical link has transitioned, we must cause
14005  * at least one routing socket message to be sent here, either via DAD
14006  * completion or just by default on the first ipif.  (If we don't do this, then
14007  * in.mpathd will see long delays when doing link-based failure recovery.)
14008  */
14009 void
14010 ill_restart_dad(ill_t *ill, boolean_t went_up)
14011 {
14012 	ipif_t *ipif;
14013 
14014 	if (ill == NULL)
14015 		return;
14016 
14017 	/*
14018 	 * If layer two doesn't support duplicate address detection, then just
14019 	 * send the routing socket message now and be done with it.
14020 	 */
14021 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14022 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14023 		ip_rts_ifmsg(ill->ill_ipif);
14024 		return;
14025 	}
14026 
14027 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14028 		if (went_up) {
14029 			if (ipif->ipif_flags & IPIF_UP) {
14030 				if (ill->ill_isv6)
14031 					ipif_ndp_start_dad(ipif);
14032 				else
14033 					ipif_arp_start_dad(ipif);
14034 			} else if (ill->ill_isv6 &&
14035 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14036 				/*
14037 				 * For IPv4, the ARP module itself will
14038 				 * automatically start the DAD process when it
14039 				 * sees DL_NOTE_LINK_UP.  We respond to the
14040 				 * AR_CN_READY at the completion of that task.
14041 				 * For IPv6, we must kick off the bring-up
14042 				 * process now.
14043 				 */
14044 				ndp_do_recovery(ipif);
14045 			} else {
14046 				/*
14047 				 * Unfortunately, the first ipif is "special"
14048 				 * and represents the underlying ill in the
14049 				 * routing socket messages.  Thus, when this
14050 				 * one ipif is down, we must still notify so
14051 				 * that the user knows the IFF_RUNNING status
14052 				 * change.  (If the first ipif is up, then
14053 				 * we'll handle eventual routing socket
14054 				 * notification via DAD completion.)
14055 				 */
14056 				if (ipif == ill->ill_ipif)
14057 					ip_rts_ifmsg(ill->ill_ipif);
14058 			}
14059 		} else {
14060 			/*
14061 			 * After link down, we'll need to send a new routing
14062 			 * message when the link comes back, so clear
14063 			 * ipif_addr_ready.
14064 			 */
14065 			ipif->ipif_addr_ready = 0;
14066 		}
14067 	}
14068 
14069 	/*
14070 	 * If we've torn down links, then notify the user right away.
14071 	 */
14072 	if (!went_up)
14073 		ip_rts_ifmsg(ill->ill_ipif);
14074 }
14075 
14076 /*
14077  * Wakeup all threads waiting to enter the ipsq, and sleeping
14078  * on any of the ills in this ipsq. The ill_lock of the ill
14079  * must be held so that waiters don't miss wakeups
14080  */
14081 static void
14082 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14083 {
14084 	phyint_t *phyint;
14085 
14086 	phyint = ipsq->ipsq_phyint_list;
14087 	while (phyint != NULL) {
14088 		if (phyint->phyint_illv4) {
14089 			if (!caller_holds_lock)
14090 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14091 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14092 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14093 			if (!caller_holds_lock)
14094 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14095 		}
14096 		if (phyint->phyint_illv6) {
14097 			if (!caller_holds_lock)
14098 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14099 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14100 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14101 			if (!caller_holds_lock)
14102 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14103 		}
14104 		phyint = phyint->phyint_ipsq_next;
14105 	}
14106 }
14107 
14108 static ipsq_t *
14109 ipsq_create(char *groupname, ip_stack_t *ipst)
14110 {
14111 	ipsq_t	*ipsq;
14112 
14113 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14114 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14115 	if (ipsq == NULL) {
14116 		return (NULL);
14117 	}
14118 
14119 	if (groupname != NULL)
14120 		(void) strcpy(ipsq->ipsq_name, groupname);
14121 	else
14122 		ipsq->ipsq_name[0] = '\0';
14123 
14124 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14125 	ipsq->ipsq_flags |= IPSQ_GROUP;
14126 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14127 	ipst->ips_ipsq_g_head = ipsq;
14128 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14129 	return (ipsq);
14130 }
14131 
14132 /*
14133  * Return an ipsq correspoding to the groupname. If 'create' is true
14134  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14135  * uniquely with an IPMP group. However during IPMP groupname operations,
14136  * multiple IPMP groups may be associated with a single ipsq. But no
14137  * IPMP group can be associated with more than 1 ipsq at any time.
14138  * For example
14139  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14140  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14141  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14142  *
14143  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14144  * status shown below during the execution of the above command.
14145  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14146  *
14147  * After the completion of the above groupname command we return to the stable
14148  * state shown below.
14149  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14150  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14151  *
14152  * Because of the above, we don't search based on the ipsq_name since that
14153  * would miss the correct ipsq during certain windows as shown above.
14154  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14155  * natural state.
14156  */
14157 static ipsq_t *
14158 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14159     ip_stack_t *ipst)
14160 {
14161 	ipsq_t	*ipsq;
14162 	int	group_len;
14163 	phyint_t *phyint;
14164 
14165 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14166 
14167 	group_len = strlen(groupname);
14168 	ASSERT(group_len != 0);
14169 	group_len++;
14170 
14171 	for (ipsq = ipst->ips_ipsq_g_head;
14172 	    ipsq != NULL;
14173 	    ipsq = ipsq->ipsq_next) {
14174 		/*
14175 		 * When an ipsq is being split, and ill_split_ipsq
14176 		 * calls this function, we exclude it from being considered.
14177 		 */
14178 		if (ipsq == exclude_ipsq)
14179 			continue;
14180 
14181 		/*
14182 		 * Compare against the ipsq_name. The groupname change happens
14183 		 * in 2 phases. The 1st phase merges the from group into
14184 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14185 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14186 		 * ipsq_name. At this point the phyint_groupname has not been
14187 		 * updated.
14188 		 */
14189 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14190 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14191 			/*
14192 			 * Verify that an ipmp groupname is exactly
14193 			 * part of 1 ipsq and is not found in any other
14194 			 * ipsq.
14195 			 */
14196 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14197 			    NULL);
14198 			return (ipsq);
14199 		}
14200 
14201 		/*
14202 		 * Comparison against ipsq_name alone is not sufficient.
14203 		 * In the case when groups are currently being
14204 		 * merged, the ipsq could hold other IPMP groups temporarily.
14205 		 * so we walk the phyint list and compare against the
14206 		 * phyint_groupname as well.
14207 		 */
14208 		phyint = ipsq->ipsq_phyint_list;
14209 		while (phyint != NULL) {
14210 			if ((group_len == phyint->phyint_groupname_len) &&
14211 			    (bcmp(phyint->phyint_groupname, groupname,
14212 			    group_len) == 0)) {
14213 				/*
14214 				 * Verify that an ipmp groupname is exactly
14215 				 * part of 1 ipsq and is not found in any other
14216 				 * ipsq.
14217 				 */
14218 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14219 				    ipst) == NULL);
14220 				return (ipsq);
14221 			}
14222 			phyint = phyint->phyint_ipsq_next;
14223 		}
14224 	}
14225 	if (create)
14226 		ipsq = ipsq_create(groupname, ipst);
14227 	return (ipsq);
14228 }
14229 
14230 static void
14231 ipsq_delete(ipsq_t *ipsq)
14232 {
14233 	ipsq_t *nipsq;
14234 	ipsq_t *pipsq = NULL;
14235 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14236 
14237 	/*
14238 	 * We don't hold the ipsq lock, but we are sure no new
14239 	 * messages can land up, since the ipsq_refs is zero.
14240 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14241 	 * is associated with this ipsq. (Lookups are based on ill_name
14242 	 * or phyint_groupname)
14243 	 */
14244 	ASSERT(ipsq->ipsq_refs == 0);
14245 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14246 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14247 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14248 		/*
14249 		 * This is not the ipsq of an IPMP group.
14250 		 */
14251 		ipsq->ipsq_ipst = NULL;
14252 		kmem_free(ipsq, sizeof (ipsq_t));
14253 		return;
14254 	}
14255 
14256 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14257 
14258 	/*
14259 	 * Locate the ipsq  before we can remove it from
14260 	 * the singly linked list of ipsq's.
14261 	 */
14262 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14263 	    nipsq = nipsq->ipsq_next) {
14264 		if (nipsq == ipsq) {
14265 			break;
14266 		}
14267 		pipsq = nipsq;
14268 	}
14269 
14270 	ASSERT(nipsq == ipsq);
14271 
14272 	/* unlink ipsq from the list */
14273 	if (pipsq != NULL)
14274 		pipsq->ipsq_next = ipsq->ipsq_next;
14275 	else
14276 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14277 	ipsq->ipsq_ipst = NULL;
14278 	kmem_free(ipsq, sizeof (ipsq_t));
14279 	rw_exit(&ipst->ips_ill_g_lock);
14280 }
14281 
14282 static void
14283 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14284     queue_t *q)
14285 {
14286 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14287 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14288 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14289 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14290 	ASSERT(current_mp != NULL);
14291 
14292 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14293 	    NEW_OP, NULL);
14294 
14295 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14296 	    new_ipsq->ipsq_xopq_mphead != NULL);
14297 
14298 	/*
14299 	 * move from old ipsq to the new ipsq.
14300 	 */
14301 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14302 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14303 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14304 
14305 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14306 }
14307 
14308 void
14309 ill_group_cleanup(ill_t *ill)
14310 {
14311 	ill_t *ill_v4;
14312 	ill_t *ill_v6;
14313 	ipif_t *ipif;
14314 
14315 	ill_v4 = ill->ill_phyint->phyint_illv4;
14316 	ill_v6 = ill->ill_phyint->phyint_illv6;
14317 
14318 	if (ill_v4 != NULL) {
14319 		mutex_enter(&ill_v4->ill_lock);
14320 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14321 		    ipif = ipif->ipif_next) {
14322 			IPIF_UNMARK_MOVING(ipif);
14323 		}
14324 		ill_v4->ill_up_ipifs = B_FALSE;
14325 		mutex_exit(&ill_v4->ill_lock);
14326 	}
14327 
14328 	if (ill_v6 != NULL) {
14329 		mutex_enter(&ill_v6->ill_lock);
14330 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14331 		    ipif = ipif->ipif_next) {
14332 			IPIF_UNMARK_MOVING(ipif);
14333 		}
14334 		ill_v6->ill_up_ipifs = B_FALSE;
14335 		mutex_exit(&ill_v6->ill_lock);
14336 	}
14337 }
14338 /*
14339  * This function is called when an ill has had a change in its group status
14340  * to bring up all the ipifs that were up before the change.
14341  */
14342 int
14343 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14344 {
14345 	ipif_t *ipif;
14346 	ill_t *ill_v4;
14347 	ill_t *ill_v6;
14348 	ill_t *from_ill;
14349 	int err = 0;
14350 
14351 
14352 	ASSERT(IAM_WRITER_ILL(ill));
14353 
14354 	/*
14355 	 * Except for ipif_state_flags and ill_state_flags the other
14356 	 * fields of the ipif/ill that are modified below are protected
14357 	 * implicitly since we are a writer. We would have tried to down
14358 	 * even an ipif that was already down, in ill_down_ipifs. So we
14359 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14360 	 */
14361 	ill_v4 = ill->ill_phyint->phyint_illv4;
14362 	ill_v6 = ill->ill_phyint->phyint_illv6;
14363 	if (ill_v4 != NULL) {
14364 		ill_v4->ill_up_ipifs = B_TRUE;
14365 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14366 		    ipif = ipif->ipif_next) {
14367 			mutex_enter(&ill_v4->ill_lock);
14368 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14369 			IPIF_UNMARK_MOVING(ipif);
14370 			mutex_exit(&ill_v4->ill_lock);
14371 			if (ipif->ipif_was_up) {
14372 				if (!(ipif->ipif_flags & IPIF_UP))
14373 					err = ipif_up(ipif, q, mp);
14374 				ipif->ipif_was_up = B_FALSE;
14375 				if (err != 0) {
14376 					/*
14377 					 * Can there be any other error ?
14378 					 */
14379 					ASSERT(err == EINPROGRESS);
14380 					return (err);
14381 				}
14382 			}
14383 		}
14384 		mutex_enter(&ill_v4->ill_lock);
14385 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14386 		mutex_exit(&ill_v4->ill_lock);
14387 		ill_v4->ill_up_ipifs = B_FALSE;
14388 		if (ill_v4->ill_move_in_progress) {
14389 			ASSERT(ill_v4->ill_move_peer != NULL);
14390 			ill_v4->ill_move_in_progress = B_FALSE;
14391 			from_ill = ill_v4->ill_move_peer;
14392 			from_ill->ill_move_in_progress = B_FALSE;
14393 			from_ill->ill_move_peer = NULL;
14394 			mutex_enter(&from_ill->ill_lock);
14395 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14396 			mutex_exit(&from_ill->ill_lock);
14397 			if (ill_v6 == NULL) {
14398 				if (from_ill->ill_phyint->phyint_flags &
14399 				    PHYI_STANDBY) {
14400 					phyint_inactive(from_ill->ill_phyint);
14401 				}
14402 				if (ill_v4->ill_phyint->phyint_flags &
14403 				    PHYI_STANDBY) {
14404 					phyint_inactive(ill_v4->ill_phyint);
14405 				}
14406 			}
14407 			ill_v4->ill_move_peer = NULL;
14408 		}
14409 	}
14410 
14411 	if (ill_v6 != NULL) {
14412 		ill_v6->ill_up_ipifs = B_TRUE;
14413 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14414 		    ipif = ipif->ipif_next) {
14415 			mutex_enter(&ill_v6->ill_lock);
14416 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14417 			IPIF_UNMARK_MOVING(ipif);
14418 			mutex_exit(&ill_v6->ill_lock);
14419 			if (ipif->ipif_was_up) {
14420 				if (!(ipif->ipif_flags & IPIF_UP))
14421 					err = ipif_up(ipif, q, mp);
14422 				ipif->ipif_was_up = B_FALSE;
14423 				if (err != 0) {
14424 					/*
14425 					 * Can there be any other error ?
14426 					 */
14427 					ASSERT(err == EINPROGRESS);
14428 					return (err);
14429 				}
14430 			}
14431 		}
14432 		mutex_enter(&ill_v6->ill_lock);
14433 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14434 		mutex_exit(&ill_v6->ill_lock);
14435 		ill_v6->ill_up_ipifs = B_FALSE;
14436 		if (ill_v6->ill_move_in_progress) {
14437 			ASSERT(ill_v6->ill_move_peer != NULL);
14438 			ill_v6->ill_move_in_progress = B_FALSE;
14439 			from_ill = ill_v6->ill_move_peer;
14440 			from_ill->ill_move_in_progress = B_FALSE;
14441 			from_ill->ill_move_peer = NULL;
14442 			mutex_enter(&from_ill->ill_lock);
14443 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14444 			mutex_exit(&from_ill->ill_lock);
14445 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14446 				phyint_inactive(from_ill->ill_phyint);
14447 			}
14448 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14449 				phyint_inactive(ill_v6->ill_phyint);
14450 			}
14451 			ill_v6->ill_move_peer = NULL;
14452 		}
14453 	}
14454 	return (0);
14455 }
14456 
14457 /*
14458  * bring down all the approriate ipifs.
14459  */
14460 /* ARGSUSED */
14461 static void
14462 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14463 {
14464 	ipif_t *ipif;
14465 
14466 	ASSERT(IAM_WRITER_ILL(ill));
14467 
14468 	/*
14469 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14470 	 * are modified below are protected implicitly since we are a writer
14471 	 */
14472 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14473 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14474 			continue;
14475 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14476 			/*
14477 			 * We go through the ipif_down logic even if the ipif
14478 			 * is already down, since routes can be added based
14479 			 * on down ipifs. Going through ipif_down once again
14480 			 * will delete any IREs created based on these routes.
14481 			 */
14482 			if (ipif->ipif_flags & IPIF_UP)
14483 				ipif->ipif_was_up = B_TRUE;
14484 			/*
14485 			 * If called with chk_nofailover true ipif is moving.
14486 			 */
14487 			mutex_enter(&ill->ill_lock);
14488 			if (chk_nofailover) {
14489 				ipif->ipif_state_flags |=
14490 				    IPIF_MOVING | IPIF_CHANGING;
14491 			} else {
14492 				ipif->ipif_state_flags |= IPIF_CHANGING;
14493 			}
14494 			mutex_exit(&ill->ill_lock);
14495 			/*
14496 			 * Need to re-create net/subnet bcast ires if
14497 			 * they are dependent on ipif.
14498 			 */
14499 			if (!ipif->ipif_isv6)
14500 				ipif_check_bcast_ires(ipif);
14501 			(void) ipif_logical_down(ipif, NULL, NULL);
14502 			ipif_non_duplicate(ipif);
14503 			ipif_down_tail(ipif);
14504 		}
14505 	}
14506 }
14507 
14508 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14509 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14510 	(ipsq)->ipsq_refs++;				\
14511 }
14512 
14513 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14514 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14515 	(ipsq)->ipsq_refs--;				\
14516 	if ((ipsq)->ipsq_refs == 0)				\
14517 		(ipsq)->ipsq_name[0] = '\0'; 		\
14518 }
14519 
14520 /*
14521  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14522  * new_ipsq.
14523  */
14524 static void
14525 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14526 {
14527 	phyint_t *phyint;
14528 	phyint_t *next_phyint;
14529 
14530 	/*
14531 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14532 	 * writer and the ill_lock of the ill in question. Also the dest
14533 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14534 	 */
14535 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14536 
14537 	phyint = cur_ipsq->ipsq_phyint_list;
14538 	cur_ipsq->ipsq_phyint_list = NULL;
14539 	while (phyint != NULL) {
14540 		next_phyint = phyint->phyint_ipsq_next;
14541 		IPSQ_DEC_REF(cur_ipsq, ipst);
14542 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14543 		new_ipsq->ipsq_phyint_list = phyint;
14544 		IPSQ_INC_REF(new_ipsq, ipst);
14545 		phyint->phyint_ipsq = new_ipsq;
14546 		phyint = next_phyint;
14547 	}
14548 }
14549 
14550 #define	SPLIT_SUCCESS		0
14551 #define	SPLIT_NOT_NEEDED	1
14552 #define	SPLIT_FAILED		2
14553 
14554 int
14555 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14556     ip_stack_t *ipst)
14557 {
14558 	ipsq_t *newipsq = NULL;
14559 
14560 	/*
14561 	 * Assertions denote pre-requisites for changing the ipsq of
14562 	 * a phyint
14563 	 */
14564 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14565 	/*
14566 	 * <ill-phyint> assocs can't change while ill_g_lock
14567 	 * is held as writer. See ill_phyint_reinit()
14568 	 */
14569 	ASSERT(phyint->phyint_illv4 == NULL ||
14570 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14571 	ASSERT(phyint->phyint_illv6 == NULL ||
14572 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14573 
14574 	if ((phyint->phyint_groupname_len !=
14575 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14576 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14577 	    phyint->phyint_groupname_len) != 0)) {
14578 		/*
14579 		 * Once we fail in creating a new ipsq due to memory shortage,
14580 		 * don't attempt to create new ipsq again, based on another
14581 		 * phyint, since we want all phyints belonging to an IPMP group
14582 		 * to be in the same ipsq even in the event of mem alloc fails.
14583 		 */
14584 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14585 		    cur_ipsq, ipst);
14586 		if (newipsq == NULL) {
14587 			/* Memory allocation failure */
14588 			return (SPLIT_FAILED);
14589 		} else {
14590 			/* ipsq_refs protected by ill_g_lock (writer) */
14591 			IPSQ_DEC_REF(cur_ipsq, ipst);
14592 			phyint->phyint_ipsq = newipsq;
14593 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14594 			newipsq->ipsq_phyint_list = phyint;
14595 			IPSQ_INC_REF(newipsq, ipst);
14596 			return (SPLIT_SUCCESS);
14597 		}
14598 	}
14599 	return (SPLIT_NOT_NEEDED);
14600 }
14601 
14602 /*
14603  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14604  * to do this split
14605  */
14606 static int
14607 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14608 {
14609 	ipsq_t *newipsq;
14610 
14611 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14612 	/*
14613 	 * <ill-phyint> assocs can't change while ill_g_lock
14614 	 * is held as writer. See ill_phyint_reinit()
14615 	 */
14616 
14617 	ASSERT(phyint->phyint_illv4 == NULL ||
14618 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14619 	ASSERT(phyint->phyint_illv6 == NULL ||
14620 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14621 
14622 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14623 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14624 		/*
14625 		 * ipsq_init failed due to no memory
14626 		 * caller will use the same ipsq
14627 		 */
14628 		return (SPLIT_FAILED);
14629 	}
14630 
14631 	/* ipsq_ref is protected by ill_g_lock (writer) */
14632 	IPSQ_DEC_REF(cur_ipsq, ipst);
14633 
14634 	/*
14635 	 * This is a new ipsq that is unknown to the world.
14636 	 * So we don't need to hold ipsq_lock,
14637 	 */
14638 	newipsq = phyint->phyint_ipsq;
14639 	newipsq->ipsq_writer = NULL;
14640 	newipsq->ipsq_reentry_cnt--;
14641 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14642 #ifdef DEBUG
14643 	newipsq->ipsq_depth = 0;
14644 #endif
14645 
14646 	return (SPLIT_SUCCESS);
14647 }
14648 
14649 /*
14650  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14651  * ipsq's representing their individual groups or themselves. Return
14652  * whether split needs to be retried again later.
14653  */
14654 static boolean_t
14655 ill_split_ipsq(ipsq_t *cur_ipsq)
14656 {
14657 	phyint_t *phyint;
14658 	phyint_t *next_phyint;
14659 	int	error;
14660 	boolean_t need_retry = B_FALSE;
14661 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14662 
14663 	phyint = cur_ipsq->ipsq_phyint_list;
14664 	cur_ipsq->ipsq_phyint_list = NULL;
14665 	while (phyint != NULL) {
14666 		next_phyint = phyint->phyint_ipsq_next;
14667 		/*
14668 		 * 'created' will tell us whether the callee actually
14669 		 * created an ipsq. Lack of memory may force the callee
14670 		 * to return without creating an ipsq.
14671 		 */
14672 		if (phyint->phyint_groupname == NULL) {
14673 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14674 		} else {
14675 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14676 			    need_retry, ipst);
14677 		}
14678 
14679 		switch (error) {
14680 		case SPLIT_FAILED:
14681 			need_retry = B_TRUE;
14682 			/* FALLTHRU */
14683 		case SPLIT_NOT_NEEDED:
14684 			/*
14685 			 * Keep it on the list.
14686 			 */
14687 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14688 			cur_ipsq->ipsq_phyint_list = phyint;
14689 			break;
14690 		case SPLIT_SUCCESS:
14691 			break;
14692 		default:
14693 			ASSERT(0);
14694 		}
14695 
14696 		phyint = next_phyint;
14697 	}
14698 	return (need_retry);
14699 }
14700 
14701 /*
14702  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14703  * and return the ills in the list. This list will be
14704  * needed to unlock all the ills later on by the caller.
14705  * The <ill-ipsq> associations could change between the
14706  * lock and unlock. Hence the unlock can't traverse the
14707  * ipsq to get the list of ills.
14708  */
14709 static int
14710 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14711 {
14712 	int	cnt = 0;
14713 	phyint_t	*phyint;
14714 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14715 
14716 	/*
14717 	 * The caller holds ill_g_lock to ensure that the ill memberships
14718 	 * of the ipsq don't change
14719 	 */
14720 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14721 
14722 	phyint = ipsq->ipsq_phyint_list;
14723 	while (phyint != NULL) {
14724 		if (phyint->phyint_illv4 != NULL) {
14725 			ASSERT(cnt < list_max);
14726 			list[cnt++] = phyint->phyint_illv4;
14727 		}
14728 		if (phyint->phyint_illv6 != NULL) {
14729 			ASSERT(cnt < list_max);
14730 			list[cnt++] = phyint->phyint_illv6;
14731 		}
14732 		phyint = phyint->phyint_ipsq_next;
14733 	}
14734 	ill_lock_ills(list, cnt);
14735 	return (cnt);
14736 }
14737 
14738 void
14739 ill_lock_ills(ill_t **list, int cnt)
14740 {
14741 	int	i;
14742 
14743 	if (cnt > 1) {
14744 		boolean_t try_again;
14745 		do {
14746 			try_again = B_FALSE;
14747 			for (i = 0; i < cnt - 1; i++) {
14748 				if (list[i] < list[i + 1]) {
14749 					ill_t	*tmp;
14750 
14751 					/* swap the elements */
14752 					tmp = list[i];
14753 					list[i] = list[i + 1];
14754 					list[i + 1] = tmp;
14755 					try_again = B_TRUE;
14756 				}
14757 			}
14758 		} while (try_again);
14759 	}
14760 
14761 	for (i = 0; i < cnt; i++) {
14762 		if (i == 0) {
14763 			if (list[i] != NULL)
14764 				mutex_enter(&list[i]->ill_lock);
14765 			else
14766 				return;
14767 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14768 			mutex_enter(&list[i]->ill_lock);
14769 		}
14770 	}
14771 }
14772 
14773 void
14774 ill_unlock_ills(ill_t **list, int cnt)
14775 {
14776 	int	i;
14777 
14778 	for (i = 0; i < cnt; i++) {
14779 		if ((i == 0) && (list[i] != NULL)) {
14780 			mutex_exit(&list[i]->ill_lock);
14781 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14782 			mutex_exit(&list[i]->ill_lock);
14783 		}
14784 	}
14785 }
14786 
14787 /*
14788  * Merge all the ills from 1 ipsq group into another ipsq group.
14789  * The source ipsq group is specified by the ipsq associated with
14790  * 'from_ill'. The destination ipsq group is specified by the ipsq
14791  * associated with 'to_ill' or 'groupname' respectively.
14792  * Note that ipsq itself does not have a reference count mechanism
14793  * and functions don't look up an ipsq and pass it around. Instead
14794  * functions pass around an ill or groupname, and the ipsq is looked
14795  * up from the ill or groupname and the required operation performed
14796  * atomically with the lookup on the ipsq.
14797  */
14798 static int
14799 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14800     queue_t *q)
14801 {
14802 	ipsq_t *old_ipsq;
14803 	ipsq_t *new_ipsq;
14804 	ill_t	**ill_list;
14805 	int	cnt;
14806 	size_t	ill_list_size;
14807 	boolean_t became_writer_on_new_sq = B_FALSE;
14808 	ip_stack_t	*ipst = from_ill->ill_ipst;
14809 
14810 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14811 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14812 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14813 
14814 	/*
14815 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14816 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14817 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14818 	 */
14819 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14820 
14821 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14822 	if (groupname != NULL)
14823 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14824 	else {
14825 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14826 	}
14827 
14828 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14829 
14830 	/*
14831 	 * both groups are on the same ipsq.
14832 	 */
14833 	if (old_ipsq == new_ipsq) {
14834 		rw_exit(&ipst->ips_ill_g_lock);
14835 		return (0);
14836 	}
14837 
14838 	cnt = old_ipsq->ipsq_refs << 1;
14839 	ill_list_size = cnt * sizeof (ill_t *);
14840 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14841 	if (ill_list == NULL) {
14842 		rw_exit(&ipst->ips_ill_g_lock);
14843 		return (ENOMEM);
14844 	}
14845 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14846 
14847 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14848 	mutex_enter(&new_ipsq->ipsq_lock);
14849 	if ((new_ipsq->ipsq_writer == NULL &&
14850 	    new_ipsq->ipsq_current_ipif == NULL) ||
14851 	    (new_ipsq->ipsq_writer == curthread)) {
14852 		new_ipsq->ipsq_writer = curthread;
14853 		new_ipsq->ipsq_reentry_cnt++;
14854 		became_writer_on_new_sq = B_TRUE;
14855 	}
14856 
14857 	/*
14858 	 * We are holding ill_g_lock as writer and all the ill locks of
14859 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14860 	 * message can land up on the old ipsq even though we don't hold the
14861 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14862 	 */
14863 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14864 
14865 	/*
14866 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14867 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14868 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14869 	 */
14870 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14871 
14872 	/*
14873 	 * Mark the new ipsq as needing a split since it is currently
14874 	 * being shared by more than 1 IPMP group. The split will
14875 	 * occur at the end of ipsq_exit
14876 	 */
14877 	new_ipsq->ipsq_split = B_TRUE;
14878 
14879 	/* Now release all the locks */
14880 	mutex_exit(&new_ipsq->ipsq_lock);
14881 	ill_unlock_ills(ill_list, cnt);
14882 	rw_exit(&ipst->ips_ill_g_lock);
14883 
14884 	kmem_free(ill_list, ill_list_size);
14885 
14886 	/*
14887 	 * If we succeeded in becoming writer on the new ipsq, then
14888 	 * drain the new ipsq and start processing  all enqueued messages
14889 	 * including the current ioctl we are processing which is either
14890 	 * a set groupname or failover/failback.
14891 	 */
14892 	if (became_writer_on_new_sq)
14893 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14894 
14895 	/*
14896 	 * syncq has been changed and all the messages have been moved.
14897 	 */
14898 	mutex_enter(&old_ipsq->ipsq_lock);
14899 	old_ipsq->ipsq_current_ipif = NULL;
14900 	old_ipsq->ipsq_current_ioctl = 0;
14901 	old_ipsq->ipsq_current_done = B_TRUE;
14902 	mutex_exit(&old_ipsq->ipsq_lock);
14903 	return (EINPROGRESS);
14904 }
14905 
14906 /*
14907  * Delete and add the loopback copy and non-loopback copy of
14908  * the BROADCAST ire corresponding to ill and addr. Used to
14909  * group broadcast ires together when ill becomes part of
14910  * a group.
14911  *
14912  * This function is also called when ill is leaving the group
14913  * so that the ires belonging to the group gets re-grouped.
14914  */
14915 static void
14916 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14917 {
14918 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14919 	ire_t **ire_ptpn = &ire_head;
14920 	ip_stack_t	*ipst = ill->ill_ipst;
14921 
14922 	/*
14923 	 * The loopback and non-loopback IREs are inserted in the order in which
14924 	 * they're found, on the basis that they are correctly ordered (loopback
14925 	 * first).
14926 	 */
14927 	for (;;) {
14928 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14929 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14930 		if (ire == NULL)
14931 			break;
14932 
14933 		/*
14934 		 * we are passing in KM_SLEEP because it is not easy to
14935 		 * go back to a sane state in case of memory failure.
14936 		 */
14937 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14938 		ASSERT(nire != NULL);
14939 		bzero(nire, sizeof (ire_t));
14940 		/*
14941 		 * Don't use ire_max_frag directly since we don't
14942 		 * hold on to 'ire' until we add the new ire 'nire' and
14943 		 * we don't want the new ire to have a dangling reference
14944 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14945 		 * be in sync with the ipif_mtu of the associate ipif.
14946 		 * For eg. this happens as a result of SIOCSLIFNAME,
14947 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14948 		 * the driver. A change in ire_max_frag triggered as
14949 		 * as a result of path mtu discovery, or due to an
14950 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14951 		 * route change -mtu command does not apply to broadcast ires.
14952 		 *
14953 		 * XXX We need a recovery strategy here if ire_init fails
14954 		 */
14955 		if (ire_init(nire,
14956 		    (uchar_t *)&ire->ire_addr,
14957 		    (uchar_t *)&ire->ire_mask,
14958 		    (uchar_t *)&ire->ire_src_addr,
14959 		    (uchar_t *)&ire->ire_gateway_addr,
14960 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14961 		    &ire->ire_ipif->ipif_mtu,
14962 		    ire->ire_nce,
14963 		    ire->ire_rfq,
14964 		    ire->ire_stq,
14965 		    ire->ire_type,
14966 		    ire->ire_ipif,
14967 		    ire->ire_cmask,
14968 		    ire->ire_phandle,
14969 		    ire->ire_ihandle,
14970 		    ire->ire_flags,
14971 		    &ire->ire_uinfo,
14972 		    NULL,
14973 		    NULL,
14974 		    ipst) == NULL) {
14975 			cmn_err(CE_PANIC, "ire_init() failed");
14976 		}
14977 		ire_delete(ire);
14978 		ire_refrele(ire);
14979 
14980 		/*
14981 		 * The newly created IREs are inserted at the tail of the list
14982 		 * starting with ire_head. As we've just allocated them no one
14983 		 * knows about them so it's safe.
14984 		 */
14985 		*ire_ptpn = nire;
14986 		ire_ptpn = &nire->ire_next;
14987 	}
14988 
14989 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14990 		int error;
14991 		ire_t *oire;
14992 		/* unlink the IRE from our list before calling ire_add() */
14993 		nire_next = nire->ire_next;
14994 		nire->ire_next = NULL;
14995 
14996 		/* ire_add adds the ire at the right place in the list */
14997 		oire = nire;
14998 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14999 		ASSERT(error == 0);
15000 		ASSERT(oire == nire);
15001 		ire_refrele(nire);	/* Held in ire_add */
15002 	}
15003 }
15004 
15005 /*
15006  * This function is usually called when an ill is inserted in
15007  * a group and all the ipifs are already UP. As all the ipifs
15008  * are already UP, the broadcast ires have already been created
15009  * and been inserted. But, ire_add_v4 would not have grouped properly.
15010  * We need to re-group for the benefit of ip_wput_ire which
15011  * expects BROADCAST ires to be grouped properly to avoid sending
15012  * more than one copy of the broadcast packet per group.
15013  *
15014  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15015  *	  because when ipif_up_done ends up calling this, ires have
15016  *        already been added before illgrp_insert i.e before ill_group
15017  *	  has been initialized.
15018  */
15019 static void
15020 ill_group_bcast_for_xmit(ill_t *ill)
15021 {
15022 	ill_group_t *illgrp;
15023 	ipif_t *ipif;
15024 	ipaddr_t addr;
15025 	ipaddr_t net_mask;
15026 	ipaddr_t subnet_netmask;
15027 
15028 	illgrp = ill->ill_group;
15029 
15030 	/*
15031 	 * This function is called even when an ill is deleted from
15032 	 * the group. Hence, illgrp could be null.
15033 	 */
15034 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15035 		return;
15036 
15037 	/*
15038 	 * Delete all the BROADCAST ires matching this ill and add
15039 	 * them back. This time, ire_add_v4 should take care of
15040 	 * grouping them with others because ill is part of the
15041 	 * group.
15042 	 */
15043 	ill_bcast_delete_and_add(ill, 0);
15044 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15045 
15046 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15047 
15048 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15049 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15050 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15051 		} else {
15052 			net_mask = htonl(IN_CLASSA_NET);
15053 		}
15054 		addr = net_mask & ipif->ipif_subnet;
15055 		ill_bcast_delete_and_add(ill, addr);
15056 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15057 
15058 		subnet_netmask = ipif->ipif_net_mask;
15059 		addr = ipif->ipif_subnet;
15060 		ill_bcast_delete_and_add(ill, addr);
15061 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15062 	}
15063 }
15064 
15065 /*
15066  * This function is called from illgrp_delete when ill is being deleted
15067  * from the group.
15068  *
15069  * As ill is not there in the group anymore, any address belonging
15070  * to this ill should be cleared of IRE_MARK_NORECV.
15071  */
15072 static void
15073 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15074 {
15075 	ire_t *ire;
15076 	irb_t *irb;
15077 	ip_stack_t	*ipst = ill->ill_ipst;
15078 
15079 	ASSERT(ill->ill_group == NULL);
15080 
15081 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15082 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15083 
15084 	if (ire != NULL) {
15085 		/*
15086 		 * IPMP and plumbing operations are serialized on the ipsq, so
15087 		 * no one will insert or delete a broadcast ire under our feet.
15088 		 */
15089 		irb = ire->ire_bucket;
15090 		rw_enter(&irb->irb_lock, RW_READER);
15091 		ire_refrele(ire);
15092 
15093 		for (; ire != NULL; ire = ire->ire_next) {
15094 			if (ire->ire_addr != addr)
15095 				break;
15096 			if (ire_to_ill(ire) != ill)
15097 				continue;
15098 
15099 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15100 			ire->ire_marks &= ~IRE_MARK_NORECV;
15101 		}
15102 		rw_exit(&irb->irb_lock);
15103 	}
15104 }
15105 
15106 /*
15107  * This function must be called only after the broadcast ires
15108  * have been grouped together. For a given address addr, nominate
15109  * only one of the ires whose interface is not FAILED or OFFLINE.
15110  *
15111  * This is also called when an ipif goes down, so that we can nominate
15112  * a different ire with the same address for receiving.
15113  */
15114 static void
15115 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15116 {
15117 	irb_t *irb;
15118 	ire_t *ire;
15119 	ire_t *ire1;
15120 	ire_t *save_ire;
15121 	ire_t **irep = NULL;
15122 	boolean_t first = B_TRUE;
15123 	ire_t *clear_ire = NULL;
15124 	ire_t *start_ire = NULL;
15125 	ire_t	*new_lb_ire;
15126 	ire_t	*new_nlb_ire;
15127 	boolean_t new_lb_ire_used = B_FALSE;
15128 	boolean_t new_nlb_ire_used = B_FALSE;
15129 	uint64_t match_flags;
15130 	uint64_t phyi_flags;
15131 	boolean_t fallback = B_FALSE;
15132 	uint_t	max_frag;
15133 
15134 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15135 	    NULL, MATCH_IRE_TYPE, ipst);
15136 	/*
15137 	 * We may not be able to find some ires if a previous
15138 	 * ire_create failed. This happens when an ipif goes
15139 	 * down and we are unable to create BROADCAST ires due
15140 	 * to memory failure. Thus, we have to check for NULL
15141 	 * below. This should handle the case for LOOPBACK,
15142 	 * POINTOPOINT and interfaces with some POINTOPOINT
15143 	 * logicals for which there are no BROADCAST ires.
15144 	 */
15145 	if (ire == NULL)
15146 		return;
15147 	/*
15148 	 * Currently IRE_BROADCASTS are deleted when an ipif
15149 	 * goes down which runs exclusively. Thus, setting
15150 	 * IRE_MARK_RCVD should not race with ire_delete marking
15151 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15152 	 * be consistent with other parts of the code that walks
15153 	 * a given bucket.
15154 	 */
15155 	save_ire = ire;
15156 	irb = ire->ire_bucket;
15157 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15158 	if (new_lb_ire == NULL) {
15159 		ire_refrele(ire);
15160 		return;
15161 	}
15162 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15163 	if (new_nlb_ire == NULL) {
15164 		ire_refrele(ire);
15165 		kmem_cache_free(ire_cache, new_lb_ire);
15166 		return;
15167 	}
15168 	IRB_REFHOLD(irb);
15169 	rw_enter(&irb->irb_lock, RW_WRITER);
15170 	/*
15171 	 * Get to the first ire matching the address and the
15172 	 * group. If the address does not match we are done
15173 	 * as we could not find the IRE. If the address matches
15174 	 * we should get to the first one matching the group.
15175 	 */
15176 	while (ire != NULL) {
15177 		if (ire->ire_addr != addr ||
15178 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15179 			break;
15180 		}
15181 		ire = ire->ire_next;
15182 	}
15183 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15184 	start_ire = ire;
15185 redo:
15186 	while (ire != NULL && ire->ire_addr == addr &&
15187 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15188 		/*
15189 		 * The first ire for any address within a group
15190 		 * should always be the one with IRE_MARK_NORECV cleared
15191 		 * so that ip_wput_ire can avoid searching for one.
15192 		 * Note down the insertion point which will be used
15193 		 * later.
15194 		 */
15195 		if (first && (irep == NULL))
15196 			irep = ire->ire_ptpn;
15197 		/*
15198 		 * PHYI_FAILED is set when the interface fails.
15199 		 * This interface might have become good, but the
15200 		 * daemon has not yet detected. We should still
15201 		 * not receive on this. PHYI_OFFLINE should never
15202 		 * be picked as this has been offlined and soon
15203 		 * be removed.
15204 		 */
15205 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15206 		if (phyi_flags & PHYI_OFFLINE) {
15207 			ire->ire_marks |= IRE_MARK_NORECV;
15208 			ire = ire->ire_next;
15209 			continue;
15210 		}
15211 		if (phyi_flags & match_flags) {
15212 			ire->ire_marks |= IRE_MARK_NORECV;
15213 			ire = ire->ire_next;
15214 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15215 			    PHYI_INACTIVE) {
15216 				fallback = B_TRUE;
15217 			}
15218 			continue;
15219 		}
15220 		if (first) {
15221 			/*
15222 			 * We will move this to the front of the list later
15223 			 * on.
15224 			 */
15225 			clear_ire = ire;
15226 			ire->ire_marks &= ~IRE_MARK_NORECV;
15227 		} else {
15228 			ire->ire_marks |= IRE_MARK_NORECV;
15229 		}
15230 		first = B_FALSE;
15231 		ire = ire->ire_next;
15232 	}
15233 	/*
15234 	 * If we never nominated anybody, try nominating at least
15235 	 * an INACTIVE, if we found one. Do it only once though.
15236 	 */
15237 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15238 	    fallback) {
15239 		match_flags = PHYI_FAILED;
15240 		ire = start_ire;
15241 		irep = NULL;
15242 		goto redo;
15243 	}
15244 	ire_refrele(save_ire);
15245 
15246 	/*
15247 	 * irep non-NULL indicates that we entered the while loop
15248 	 * above. If clear_ire is at the insertion point, we don't
15249 	 * have to do anything. clear_ire will be NULL if all the
15250 	 * interfaces are failed.
15251 	 *
15252 	 * We cannot unlink and reinsert the ire at the right place
15253 	 * in the list since there can be other walkers of this bucket.
15254 	 * Instead we delete and recreate the ire
15255 	 */
15256 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15257 		ire_t *clear_ire_stq = NULL;
15258 
15259 		bzero(new_lb_ire, sizeof (ire_t));
15260 		/* XXX We need a recovery strategy here. */
15261 		if (ire_init(new_lb_ire,
15262 		    (uchar_t *)&clear_ire->ire_addr,
15263 		    (uchar_t *)&clear_ire->ire_mask,
15264 		    (uchar_t *)&clear_ire->ire_src_addr,
15265 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15266 		    &clear_ire->ire_max_frag,
15267 		    NULL, /* let ire_nce_init derive the resolver info */
15268 		    clear_ire->ire_rfq,
15269 		    clear_ire->ire_stq,
15270 		    clear_ire->ire_type,
15271 		    clear_ire->ire_ipif,
15272 		    clear_ire->ire_cmask,
15273 		    clear_ire->ire_phandle,
15274 		    clear_ire->ire_ihandle,
15275 		    clear_ire->ire_flags,
15276 		    &clear_ire->ire_uinfo,
15277 		    NULL,
15278 		    NULL,
15279 		    ipst) == NULL)
15280 			cmn_err(CE_PANIC, "ire_init() failed");
15281 		if (clear_ire->ire_stq == NULL) {
15282 			ire_t *ire_next = clear_ire->ire_next;
15283 			if (ire_next != NULL &&
15284 			    ire_next->ire_stq != NULL &&
15285 			    ire_next->ire_addr == clear_ire->ire_addr &&
15286 			    ire_next->ire_ipif->ipif_ill ==
15287 			    clear_ire->ire_ipif->ipif_ill) {
15288 				clear_ire_stq = ire_next;
15289 
15290 				bzero(new_nlb_ire, sizeof (ire_t));
15291 				/* XXX We need a recovery strategy here. */
15292 				if (ire_init(new_nlb_ire,
15293 				    (uchar_t *)&clear_ire_stq->ire_addr,
15294 				    (uchar_t *)&clear_ire_stq->ire_mask,
15295 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15296 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15297 				    &clear_ire_stq->ire_max_frag,
15298 				    NULL,
15299 				    clear_ire_stq->ire_rfq,
15300 				    clear_ire_stq->ire_stq,
15301 				    clear_ire_stq->ire_type,
15302 				    clear_ire_stq->ire_ipif,
15303 				    clear_ire_stq->ire_cmask,
15304 				    clear_ire_stq->ire_phandle,
15305 				    clear_ire_stq->ire_ihandle,
15306 				    clear_ire_stq->ire_flags,
15307 				    &clear_ire_stq->ire_uinfo,
15308 				    NULL,
15309 				    NULL,
15310 				    ipst) == NULL)
15311 					cmn_err(CE_PANIC, "ire_init() failed");
15312 			}
15313 		}
15314 
15315 		/*
15316 		 * Delete the ire. We can't call ire_delete() since
15317 		 * we are holding the bucket lock. We can't release the
15318 		 * bucket lock since we can't allow irep to change. So just
15319 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15320 		 * ire from the list and do the refrele.
15321 		 */
15322 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15323 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15324 
15325 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15326 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15327 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15328 		}
15329 
15330 		/*
15331 		 * Also take care of otherfields like ib/ob pkt count
15332 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15333 		 */
15334 
15335 		/* Set the max_frag before adding the ire */
15336 		max_frag = *new_lb_ire->ire_max_fragp;
15337 		new_lb_ire->ire_max_fragp = NULL;
15338 		new_lb_ire->ire_max_frag = max_frag;
15339 
15340 		/* Add the new ire's. Insert at *irep */
15341 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15342 		ire1 = *irep;
15343 		if (ire1 != NULL)
15344 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15345 		new_lb_ire->ire_next = ire1;
15346 		/* Link the new one in. */
15347 		new_lb_ire->ire_ptpn = irep;
15348 		membar_producer();
15349 		*irep = new_lb_ire;
15350 		new_lb_ire_used = B_TRUE;
15351 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15352 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15353 		DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), new_lb_ire->ire_ipif,
15354 		    (char *), "ire", (void *), new_lb_ire);
15355 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15356 
15357 		if (clear_ire_stq != NULL) {
15358 			/* Set the max_frag before adding the ire */
15359 			max_frag = *new_nlb_ire->ire_max_fragp;
15360 			new_nlb_ire->ire_max_fragp = NULL;
15361 			new_nlb_ire->ire_max_frag = max_frag;
15362 
15363 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15364 			irep = &new_lb_ire->ire_next;
15365 			/* Add the new ire. Insert at *irep */
15366 			ire1 = *irep;
15367 			if (ire1 != NULL)
15368 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15369 			new_nlb_ire->ire_next = ire1;
15370 			/* Link the new one in. */
15371 			new_nlb_ire->ire_ptpn = irep;
15372 			membar_producer();
15373 			*irep = new_nlb_ire;
15374 			new_nlb_ire_used = B_TRUE;
15375 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15376 			    ire_stats_inserted);
15377 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15378 			DTRACE_PROBE3(ipif__incr__cnt,
15379 			    (ipif_t *), new_nlb_ire->ire_ipif,
15380 			    (char *), "ire", (void *), new_nlb_ire);
15381 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15382 			DTRACE_PROBE3(ill__incr__cnt,
15383 			    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15384 			    (char *), "ire", (void *), new_nlb_ire);
15385 			((ill_t *)(new_nlb_ire->ire_stq->q_ptr))->ill_ire_cnt++;
15386 		}
15387 	}
15388 	rw_exit(&irb->irb_lock);
15389 	if (!new_lb_ire_used)
15390 		kmem_cache_free(ire_cache, new_lb_ire);
15391 	if (!new_nlb_ire_used)
15392 		kmem_cache_free(ire_cache, new_nlb_ire);
15393 	IRB_REFRELE(irb);
15394 }
15395 
15396 /*
15397  * Whenever an ipif goes down we have to renominate a different
15398  * broadcast ire to receive. Whenever an ipif comes up, we need
15399  * to make sure that we have only one nominated to receive.
15400  */
15401 static void
15402 ipif_renominate_bcast(ipif_t *ipif)
15403 {
15404 	ill_t *ill = ipif->ipif_ill;
15405 	ipaddr_t subnet_addr;
15406 	ipaddr_t net_addr;
15407 	ipaddr_t net_mask = 0;
15408 	ipaddr_t subnet_netmask;
15409 	ipaddr_t addr;
15410 	ill_group_t *illgrp;
15411 	ip_stack_t	*ipst = ill->ill_ipst;
15412 
15413 	illgrp = ill->ill_group;
15414 	/*
15415 	 * If this is the last ipif going down, it might take
15416 	 * the ill out of the group. In that case ipif_down ->
15417 	 * illgrp_delete takes care of doing the nomination.
15418 	 * ipif_down does not call for this case.
15419 	 */
15420 	ASSERT(illgrp != NULL);
15421 
15422 	/* There could not have been any ires associated with this */
15423 	if (ipif->ipif_subnet == 0)
15424 		return;
15425 
15426 	ill_mark_bcast(illgrp, 0, ipst);
15427 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15428 
15429 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15430 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15431 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15432 	} else {
15433 		net_mask = htonl(IN_CLASSA_NET);
15434 	}
15435 	addr = net_mask & ipif->ipif_subnet;
15436 	ill_mark_bcast(illgrp, addr, ipst);
15437 
15438 	net_addr = ~net_mask | addr;
15439 	ill_mark_bcast(illgrp, net_addr, ipst);
15440 
15441 	subnet_netmask = ipif->ipif_net_mask;
15442 	addr = ipif->ipif_subnet;
15443 	ill_mark_bcast(illgrp, addr, ipst);
15444 
15445 	subnet_addr = ~subnet_netmask | addr;
15446 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15447 }
15448 
15449 /*
15450  * Whenever we form or delete ill groups, we need to nominate one set of
15451  * BROADCAST ires for receiving in the group.
15452  *
15453  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15454  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15455  *    for ill_ipif_up_count to be non-zero. This is the only case where
15456  *    ill_ipif_up_count is zero and we would still find the ires.
15457  *
15458  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15459  *    ipif is UP and we just have to do the nomination.
15460  *
15461  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15462  *    from the group. So, we have to do the nomination.
15463  *
15464  * Because of (3), there could be just one ill in the group. But we have
15465  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15466  * Thus, this function does not optimize when there is only one ill as
15467  * it is not correct for (3).
15468  */
15469 static void
15470 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15471 {
15472 	ill_t *ill;
15473 	ipif_t *ipif;
15474 	ipaddr_t subnet_addr;
15475 	ipaddr_t prev_subnet_addr = 0;
15476 	ipaddr_t net_addr;
15477 	ipaddr_t prev_net_addr = 0;
15478 	ipaddr_t net_mask = 0;
15479 	ipaddr_t subnet_netmask;
15480 	ipaddr_t addr;
15481 	ip_stack_t	*ipst;
15482 
15483 	/*
15484 	 * When the last memeber is leaving, there is nothing to
15485 	 * nominate.
15486 	 */
15487 	if (illgrp->illgrp_ill_count == 0) {
15488 		ASSERT(illgrp->illgrp_ill == NULL);
15489 		return;
15490 	}
15491 
15492 	ill = illgrp->illgrp_ill;
15493 	ASSERT(!ill->ill_isv6);
15494 	ipst = ill->ill_ipst;
15495 	/*
15496 	 * We assume that ires with same address and belonging to the
15497 	 * same group, has been grouped together. Nominating a *single*
15498 	 * ill in the group for sending and receiving broadcast is done
15499 	 * by making sure that the first BROADCAST ire (which will be
15500 	 * the one returned by ire_ctable_lookup for ip_rput and the
15501 	 * one that will be used in ip_wput_ire) will be the one that
15502 	 * will not have IRE_MARK_NORECV set.
15503 	 *
15504 	 * 1) ip_rput checks and discards packets received on ires marked
15505 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15506 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15507 	 *    first ire in the group for every broadcast address in the group.
15508 	 *    ip_rput will accept packets only on the first ire i.e only
15509 	 *    one copy of the ill.
15510 	 *
15511 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15512 	 *    packet for the whole group. It needs to send out on the ill
15513 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15514 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15515 	 *    the copy echoed back on other port where the ire is not marked
15516 	 *    with IRE_MARK_NORECV.
15517 	 *
15518 	 * Note that we just need to have the first IRE either loopback or
15519 	 * non-loopback (either of them may not exist if ire_create failed
15520 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15521 	 * always hit the first one and hence will always accept one copy.
15522 	 *
15523 	 * We have a broadcast ire per ill for all the unique prefixes
15524 	 * hosted on that ill. As we don't have a way of knowing the
15525 	 * unique prefixes on a given ill and hence in the whole group,
15526 	 * we just call ill_mark_bcast on all the prefixes that exist
15527 	 * in the group. For the common case of one prefix, the code
15528 	 * below optimizes by remebering the last address used for
15529 	 * markng. In the case of multiple prefixes, this will still
15530 	 * optimize depending the order of prefixes.
15531 	 *
15532 	 * The only unique address across the whole group is 0.0.0.0 and
15533 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15534 	 * the first ire in the bucket for receiving and disables the
15535 	 * others.
15536 	 */
15537 	ill_mark_bcast(illgrp, 0, ipst);
15538 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15539 	for (; ill != NULL; ill = ill->ill_group_next) {
15540 
15541 		for (ipif = ill->ill_ipif; ipif != NULL;
15542 		    ipif = ipif->ipif_next) {
15543 
15544 			if (!(ipif->ipif_flags & IPIF_UP) ||
15545 			    ipif->ipif_subnet == 0) {
15546 				continue;
15547 			}
15548 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15549 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15550 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15551 			} else {
15552 				net_mask = htonl(IN_CLASSA_NET);
15553 			}
15554 			addr = net_mask & ipif->ipif_subnet;
15555 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15556 				ill_mark_bcast(illgrp, addr, ipst);
15557 				net_addr = ~net_mask | addr;
15558 				ill_mark_bcast(illgrp, net_addr, ipst);
15559 			}
15560 			prev_net_addr = addr;
15561 
15562 			subnet_netmask = ipif->ipif_net_mask;
15563 			addr = ipif->ipif_subnet;
15564 			if (prev_subnet_addr == 0 ||
15565 			    prev_subnet_addr != addr) {
15566 				ill_mark_bcast(illgrp, addr, ipst);
15567 				subnet_addr = ~subnet_netmask | addr;
15568 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15569 			}
15570 			prev_subnet_addr = addr;
15571 		}
15572 	}
15573 }
15574 
15575 /*
15576  * This function is called while forming ill groups.
15577  *
15578  * Currently, we handle only allmulti groups. We want to join
15579  * allmulti on only one of the ills in the groups. In future,
15580  * when we have link aggregation, we may have to join normal
15581  * multicast groups on multiple ills as switch does inbound load
15582  * balancing. Following are the functions that calls this
15583  * function :
15584  *
15585  * 1) ill_recover_multicast : Interface is coming back UP.
15586  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15587  *    will call ill_recover_multicast to recover all the multicast
15588  *    groups. We need to make sure that only one member is joined
15589  *    in the ill group.
15590  *
15591  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15592  *    Somebody is joining allmulti. We need to make sure that only one
15593  *    member is joined in the group.
15594  *
15595  * 3) illgrp_insert : If allmulti has already joined, we need to make
15596  *    sure that only one member is joined in the group.
15597  *
15598  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15599  *    allmulti who we have nominated. We need to pick someother ill.
15600  *
15601  * 5) illgrp_delete : The ill we nominated is leaving the group,
15602  *    we need to pick a new ill to join the group.
15603  *
15604  * For (1), (2), (5) - we just have to check whether there is
15605  * a good ill joined in the group. If we could not find any ills
15606  * joined the group, we should join.
15607  *
15608  * For (4), the one that was nominated to receive, left the group.
15609  * There could be nobody joined in the group when this function is
15610  * called.
15611  *
15612  * For (3) - we need to explicitly check whether there are multiple
15613  * ills joined in the group.
15614  *
15615  * For simplicity, we don't differentiate any of the above cases. We
15616  * just leave the group if it is joined on any of them and join on
15617  * the first good ill.
15618  */
15619 int
15620 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15621 {
15622 	ilm_t *ilm;
15623 	ill_t *ill;
15624 	ill_t *fallback_inactive_ill = NULL;
15625 	ill_t *fallback_failed_ill = NULL;
15626 	int ret = 0;
15627 
15628 	/*
15629 	 * Leave the allmulti on all the ills and start fresh.
15630 	 */
15631 	for (ill = illgrp->illgrp_ill; ill != NULL;
15632 	    ill = ill->ill_group_next) {
15633 		if (ill->ill_join_allmulti)
15634 			(void) ip_leave_allmulti(ill->ill_ipif);
15635 	}
15636 
15637 	/*
15638 	 * Choose a good ill. Fallback to inactive or failed if
15639 	 * none available. We need to fallback to FAILED in the
15640 	 * case where we have 2 interfaces in a group - where
15641 	 * one of them is failed and another is a good one and
15642 	 * the good one (not marked inactive) is leaving the group.
15643 	 */
15644 	ret = 0;
15645 	for (ill = illgrp->illgrp_ill; ill != NULL;
15646 	    ill = ill->ill_group_next) {
15647 		/* Never pick an offline interface */
15648 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15649 			continue;
15650 
15651 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15652 			fallback_failed_ill = ill;
15653 			continue;
15654 		}
15655 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15656 			fallback_inactive_ill = ill;
15657 			continue;
15658 		}
15659 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15660 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15661 				ret = ip_join_allmulti(ill->ill_ipif);
15662 				/*
15663 				 * ip_join_allmulti can fail because of memory
15664 				 * failures. So, make sure we join at least
15665 				 * on one ill.
15666 				 */
15667 				if (ill->ill_join_allmulti)
15668 					return (0);
15669 			}
15670 		}
15671 	}
15672 	if (ret != 0) {
15673 		/*
15674 		 * If we tried nominating above and failed to do so,
15675 		 * return error. We might have tried multiple times.
15676 		 * But, return the latest error.
15677 		 */
15678 		return (ret);
15679 	}
15680 	if ((ill = fallback_inactive_ill) != NULL) {
15681 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15682 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15683 				ret = ip_join_allmulti(ill->ill_ipif);
15684 				return (ret);
15685 			}
15686 		}
15687 	} else if ((ill = fallback_failed_ill) != NULL) {
15688 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15689 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15690 				ret = ip_join_allmulti(ill->ill_ipif);
15691 				return (ret);
15692 			}
15693 		}
15694 	}
15695 	return (0);
15696 }
15697 
15698 /*
15699  * This function is called from illgrp_delete after it is
15700  * deleted from the group to reschedule responsibilities
15701  * to a different ill.
15702  */
15703 static void
15704 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15705 {
15706 	ilm_t	*ilm;
15707 	ipif_t	*ipif;
15708 	ipaddr_t subnet_addr;
15709 	ipaddr_t net_addr;
15710 	ipaddr_t net_mask = 0;
15711 	ipaddr_t subnet_netmask;
15712 	ipaddr_t addr;
15713 	ip_stack_t *ipst = ill->ill_ipst;
15714 
15715 	ASSERT(ill->ill_group == NULL);
15716 	/*
15717 	 * Broadcast Responsibility:
15718 	 *
15719 	 * 1. If this ill has been nominated for receiving broadcast
15720 	 * packets, we need to find a new one. Before we find a new
15721 	 * one, we need to re-group the ires that are part of this new
15722 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15723 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15724 	 * thing for us.
15725 	 *
15726 	 * 2. If this ill was not nominated for receiving broadcast
15727 	 * packets, we need to clear the IRE_MARK_NORECV flag
15728 	 * so that we continue to send up broadcast packets.
15729 	 */
15730 	if (!ill->ill_isv6) {
15731 		/*
15732 		 * Case 1 above : No optimization here. Just redo the
15733 		 * nomination.
15734 		 */
15735 		ill_group_bcast_for_xmit(ill);
15736 		ill_nominate_bcast_rcv(illgrp);
15737 
15738 		/*
15739 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15740 		 */
15741 		ill_clear_bcast_mark(ill, 0);
15742 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15743 
15744 		for (ipif = ill->ill_ipif; ipif != NULL;
15745 		    ipif = ipif->ipif_next) {
15746 
15747 			if (!(ipif->ipif_flags & IPIF_UP) ||
15748 			    ipif->ipif_subnet == 0) {
15749 				continue;
15750 			}
15751 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15752 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15753 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15754 			} else {
15755 				net_mask = htonl(IN_CLASSA_NET);
15756 			}
15757 			addr = net_mask & ipif->ipif_subnet;
15758 			ill_clear_bcast_mark(ill, addr);
15759 
15760 			net_addr = ~net_mask | addr;
15761 			ill_clear_bcast_mark(ill, net_addr);
15762 
15763 			subnet_netmask = ipif->ipif_net_mask;
15764 			addr = ipif->ipif_subnet;
15765 			ill_clear_bcast_mark(ill, addr);
15766 
15767 			subnet_addr = ~subnet_netmask | addr;
15768 			ill_clear_bcast_mark(ill, subnet_addr);
15769 		}
15770 	}
15771 
15772 	/*
15773 	 * Multicast Responsibility.
15774 	 *
15775 	 * If we have joined allmulti on this one, find a new member
15776 	 * in the group to join allmulti. As this ill is already part
15777 	 * of allmulti, we don't have to join on this one.
15778 	 *
15779 	 * If we have not joined allmulti on this one, there is no
15780 	 * responsibility to handoff. But we need to take new
15781 	 * responsibility i.e, join allmulti on this one if we need
15782 	 * to.
15783 	 */
15784 	if (ill->ill_join_allmulti) {
15785 		(void) ill_nominate_mcast_rcv(illgrp);
15786 	} else {
15787 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15788 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15789 				(void) ip_join_allmulti(ill->ill_ipif);
15790 				break;
15791 			}
15792 		}
15793 	}
15794 
15795 	/*
15796 	 * We intentionally do the flushing of IRE_CACHES only matching
15797 	 * on the ill and not on groups. Note that we are already deleted
15798 	 * from the group.
15799 	 *
15800 	 * This will make sure that all IRE_CACHES whose stq is pointing
15801 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15802 	 * deleted and IRE_CACHES that are not pointing at this ill will
15803 	 * be left alone.
15804 	 */
15805 	if (ill->ill_isv6) {
15806 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15807 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15808 	} else {
15809 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15810 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15811 	}
15812 
15813 	/*
15814 	 * Some conn may have cached one of the IREs deleted above. By removing
15815 	 * the ire reference, we clean up the extra reference to the ill held in
15816 	 * ire->ire_stq.
15817 	 */
15818 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15819 
15820 	/*
15821 	 * Re-do source address selection for all the members in the
15822 	 * group, if they borrowed source address from one of the ipifs
15823 	 * in this ill.
15824 	 */
15825 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15826 		if (ill->ill_isv6) {
15827 			ipif_update_other_ipifs_v6(ipif, illgrp);
15828 		} else {
15829 			ipif_update_other_ipifs(ipif, illgrp);
15830 		}
15831 	}
15832 }
15833 
15834 /*
15835  * Delete the ill from the group. The caller makes sure that it is
15836  * in a group and it okay to delete from the group. So, we always
15837  * delete here.
15838  */
15839 static void
15840 illgrp_delete(ill_t *ill)
15841 {
15842 	ill_group_t *illgrp;
15843 	ill_group_t *tmpg;
15844 	ill_t *tmp_ill;
15845 	ip_stack_t	*ipst = ill->ill_ipst;
15846 
15847 	/*
15848 	 * Reset illgrp_ill_schednext if it was pointing at us.
15849 	 * We need to do this before we set ill_group to NULL.
15850 	 */
15851 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15852 	mutex_enter(&ill->ill_lock);
15853 
15854 	illgrp_reset_schednext(ill);
15855 
15856 	illgrp = ill->ill_group;
15857 
15858 	/* Delete the ill from illgrp. */
15859 	if (illgrp->illgrp_ill == ill) {
15860 		illgrp->illgrp_ill = ill->ill_group_next;
15861 	} else {
15862 		tmp_ill = illgrp->illgrp_ill;
15863 		while (tmp_ill->ill_group_next != ill) {
15864 			tmp_ill = tmp_ill->ill_group_next;
15865 			ASSERT(tmp_ill != NULL);
15866 		}
15867 		tmp_ill->ill_group_next = ill->ill_group_next;
15868 	}
15869 	ill->ill_group = NULL;
15870 	ill->ill_group_next = NULL;
15871 
15872 	illgrp->illgrp_ill_count--;
15873 	mutex_exit(&ill->ill_lock);
15874 	rw_exit(&ipst->ips_ill_g_lock);
15875 
15876 	/*
15877 	 * As this ill is leaving the group, we need to hand off
15878 	 * the responsibilities to the other ills in the group, if
15879 	 * this ill had some responsibilities.
15880 	 */
15881 
15882 	ill_handoff_responsibility(ill, illgrp);
15883 
15884 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15885 
15886 	if (illgrp->illgrp_ill_count == 0) {
15887 
15888 		ASSERT(illgrp->illgrp_ill == NULL);
15889 		if (ill->ill_isv6) {
15890 			if (illgrp == ipst->ips_illgrp_head_v6) {
15891 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15892 			} else {
15893 				tmpg = ipst->ips_illgrp_head_v6;
15894 				while (tmpg->illgrp_next != illgrp) {
15895 					tmpg = tmpg->illgrp_next;
15896 					ASSERT(tmpg != NULL);
15897 				}
15898 				tmpg->illgrp_next = illgrp->illgrp_next;
15899 			}
15900 		} else {
15901 			if (illgrp == ipst->ips_illgrp_head_v4) {
15902 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15903 			} else {
15904 				tmpg = ipst->ips_illgrp_head_v4;
15905 				while (tmpg->illgrp_next != illgrp) {
15906 					tmpg = tmpg->illgrp_next;
15907 					ASSERT(tmpg != NULL);
15908 				}
15909 				tmpg->illgrp_next = illgrp->illgrp_next;
15910 			}
15911 		}
15912 		mutex_destroy(&illgrp->illgrp_lock);
15913 		mi_free(illgrp);
15914 	}
15915 	rw_exit(&ipst->ips_ill_g_lock);
15916 
15917 	/*
15918 	 * Even though the ill is out of the group its not necessary
15919 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15920 	 * We will split the ipsq when phyint_groupname is set to NULL.
15921 	 */
15922 
15923 	/*
15924 	 * Send a routing sockets message if we are deleting from
15925 	 * groups with names.
15926 	 */
15927 	if (ill->ill_phyint->phyint_groupname_len != 0)
15928 		ip_rts_ifmsg(ill->ill_ipif);
15929 }
15930 
15931 /*
15932  * Re-do source address selection. This is normally called when
15933  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15934  * ipif comes up.
15935  */
15936 void
15937 ill_update_source_selection(ill_t *ill)
15938 {
15939 	ipif_t *ipif;
15940 
15941 	ASSERT(IAM_WRITER_ILL(ill));
15942 
15943 	if (ill->ill_group != NULL)
15944 		ill = ill->ill_group->illgrp_ill;
15945 
15946 	for (; ill != NULL; ill = ill->ill_group_next) {
15947 		for (ipif = ill->ill_ipif; ipif != NULL;
15948 		    ipif = ipif->ipif_next) {
15949 			if (ill->ill_isv6)
15950 				ipif_recreate_interface_routes_v6(NULL, ipif);
15951 			else
15952 				ipif_recreate_interface_routes(NULL, ipif);
15953 		}
15954 	}
15955 }
15956 
15957 /*
15958  * Insert ill in a group headed by illgrp_head. The caller can either
15959  * pass a groupname in which case we search for a group with the
15960  * same name to insert in or pass a group to insert in. This function
15961  * would only search groups with names.
15962  *
15963  * NOTE : The caller should make sure that there is at least one ipif
15964  *	  UP on this ill so that illgrp_scheduler can pick this ill
15965  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15966  *	  already sent a DL_UNBIND to the driver and we don't want to
15967  *	  send anymore packets. We don't assert for ipif_up_count
15968  *	  to be greater than zero, because ipif_up_done wants to call
15969  *	  this function before bumping up the ipif_up_count. See
15970  *	  ipif_up_done() for details.
15971  */
15972 int
15973 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15974     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15975 {
15976 	ill_group_t *illgrp;
15977 	ill_t *prev_ill;
15978 	phyint_t *phyi;
15979 	ip_stack_t	*ipst = ill->ill_ipst;
15980 
15981 	ASSERT(ill->ill_group == NULL);
15982 
15983 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15984 	mutex_enter(&ill->ill_lock);
15985 
15986 	if (groupname != NULL) {
15987 		/*
15988 		 * Look for a group with a matching groupname to insert.
15989 		 */
15990 		for (illgrp = *illgrp_head; illgrp != NULL;
15991 		    illgrp = illgrp->illgrp_next) {
15992 
15993 			ill_t *tmp_ill;
15994 
15995 			/*
15996 			 * If we have an ill_group_t in the list which has
15997 			 * no ill_t assigned then we must be in the process of
15998 			 * removing this group. We skip this as illgrp_delete()
15999 			 * will remove it from the list.
16000 			 */
16001 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16002 				ASSERT(illgrp->illgrp_ill_count == 0);
16003 				continue;
16004 			}
16005 
16006 			ASSERT(tmp_ill->ill_phyint != NULL);
16007 			phyi = tmp_ill->ill_phyint;
16008 			/*
16009 			 * Look at groups which has names only.
16010 			 */
16011 			if (phyi->phyint_groupname_len == 0)
16012 				continue;
16013 			/*
16014 			 * Names are stored in the phyint common to both
16015 			 * IPv4 and IPv6.
16016 			 */
16017 			if (mi_strcmp(phyi->phyint_groupname,
16018 			    groupname) == 0) {
16019 				break;
16020 			}
16021 		}
16022 	} else {
16023 		/*
16024 		 * If the caller passes in a NULL "grp_to_insert", we
16025 		 * allocate one below and insert this singleton.
16026 		 */
16027 		illgrp = grp_to_insert;
16028 	}
16029 
16030 	ill->ill_group_next = NULL;
16031 
16032 	if (illgrp == NULL) {
16033 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16034 		if (illgrp == NULL) {
16035 			return (ENOMEM);
16036 		}
16037 		illgrp->illgrp_next = *illgrp_head;
16038 		*illgrp_head = illgrp;
16039 		illgrp->illgrp_ill = ill;
16040 		illgrp->illgrp_ill_count = 1;
16041 		ill->ill_group = illgrp;
16042 		/*
16043 		 * Used in illgrp_scheduler to protect multiple threads
16044 		 * from traversing the list.
16045 		 */
16046 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16047 	} else {
16048 		ASSERT(ill->ill_net_type ==
16049 		    illgrp->illgrp_ill->ill_net_type);
16050 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16051 
16052 		/* Insert ill at tail of this group */
16053 		prev_ill = illgrp->illgrp_ill;
16054 		while (prev_ill->ill_group_next != NULL)
16055 			prev_ill = prev_ill->ill_group_next;
16056 		prev_ill->ill_group_next = ill;
16057 		ill->ill_group = illgrp;
16058 		illgrp->illgrp_ill_count++;
16059 		/*
16060 		 * Inherit group properties. Currently only forwarding
16061 		 * is the property we try to keep the same with all the
16062 		 * ills. When there are more, we will abstract this into
16063 		 * a function.
16064 		 */
16065 		ill->ill_flags &= ~ILLF_ROUTER;
16066 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16067 	}
16068 	mutex_exit(&ill->ill_lock);
16069 	rw_exit(&ipst->ips_ill_g_lock);
16070 
16071 	/*
16072 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16073 	 *    may be zero as it has not yet been bumped. But the ires
16074 	 *    have already been added. So, we do the nomination here
16075 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16076 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16077 	 *    ill_ipif_up_count here while nominating broadcast ires for
16078 	 *    receive.
16079 	 *
16080 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16081 	 *    to group them properly as ire_add() has already happened
16082 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16083 	 *    case, we need to do it here anyway.
16084 	 */
16085 	if (!ill->ill_isv6) {
16086 		ill_group_bcast_for_xmit(ill);
16087 		ill_nominate_bcast_rcv(illgrp);
16088 	}
16089 
16090 	if (!ipif_is_coming_up) {
16091 		/*
16092 		 * When ipif_up_done() calls this function, the multicast
16093 		 * groups have not been joined yet. So, there is no point in
16094 		 * nomination. ip_join_allmulti will handle groups when
16095 		 * ill_recover_multicast is called from ipif_up_done() later.
16096 		 */
16097 		(void) ill_nominate_mcast_rcv(illgrp);
16098 		/*
16099 		 * ipif_up_done calls ill_update_source_selection
16100 		 * anyway. Moreover, we don't want to re-create
16101 		 * interface routes while ipif_up_done() still has reference
16102 		 * to them. Refer to ipif_up_done() for more details.
16103 		 */
16104 		ill_update_source_selection(ill);
16105 	}
16106 
16107 	/*
16108 	 * Send a routing sockets message if we are inserting into
16109 	 * groups with names.
16110 	 */
16111 	if (groupname != NULL)
16112 		ip_rts_ifmsg(ill->ill_ipif);
16113 	return (0);
16114 }
16115 
16116 /*
16117  * Return the first phyint matching the groupname. There could
16118  * be more than one when there are ill groups.
16119  *
16120  * If 'usable' is set, then we exclude ones that are marked with any of
16121  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16122  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16123  * emulation of ipmp.
16124  */
16125 phyint_t *
16126 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16127 {
16128 	phyint_t *phyi;
16129 
16130 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16131 	/*
16132 	 * Group names are stored in the phyint - a common structure
16133 	 * to both IPv4 and IPv6.
16134 	 */
16135 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16136 	for (; phyi != NULL;
16137 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16138 	    phyi, AVL_AFTER)) {
16139 		if (phyi->phyint_groupname_len == 0)
16140 			continue;
16141 		/*
16142 		 * Skip the ones that should not be used since the callers
16143 		 * sometime use this for sending packets.
16144 		 */
16145 		if (usable && (phyi->phyint_flags &
16146 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16147 			continue;
16148 
16149 		ASSERT(phyi->phyint_groupname != NULL);
16150 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16151 			return (phyi);
16152 	}
16153 	return (NULL);
16154 }
16155 
16156 
16157 /*
16158  * Return the first usable phyint matching the group index. By 'usable'
16159  * we exclude ones that are marked ununsable with any of
16160  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16161  *
16162  * Used only for the ipmp/netinfo emulation of ipmp.
16163  */
16164 phyint_t *
16165 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16166 {
16167 	phyint_t *phyi;
16168 
16169 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16170 
16171 	if (!ipst->ips_ipmp_hook_emulation)
16172 		return (NULL);
16173 
16174 	/*
16175 	 * Group indicies are stored in the phyint - a common structure
16176 	 * to both IPv4 and IPv6.
16177 	 */
16178 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16179 	for (; phyi != NULL;
16180 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16181 	    phyi, AVL_AFTER)) {
16182 		/* Ignore the ones that do not have a group */
16183 		if (phyi->phyint_groupname_len == 0)
16184 			continue;
16185 
16186 		ASSERT(phyi->phyint_group_ifindex != 0);
16187 		/*
16188 		 * Skip the ones that should not be used since the callers
16189 		 * sometime use this for sending packets.
16190 		 */
16191 		if (phyi->phyint_flags &
16192 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16193 			continue;
16194 		if (phyi->phyint_group_ifindex == group_ifindex)
16195 			return (phyi);
16196 	}
16197 	return (NULL);
16198 }
16199 
16200 
16201 /*
16202  * MT notes on creation and deletion of IPMP groups
16203  *
16204  * Creation and deletion of IPMP groups introduce the need to merge or
16205  * split the associated serialization objects i.e the ipsq's. Normally all
16206  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16207  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16208  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16209  * is a need to change the <ill-ipsq> association and we have to operate on both
16210  * the source and destination IPMP groups. For eg. attempting to set the
16211  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16212  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16213  * source or destination IPMP group are mapped to a single ipsq for executing
16214  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16215  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16216  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16217  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16218  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16219  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16220  *
16221  * In the above example the ioctl handling code locates the current ipsq of hme0
16222  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16223  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16224  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16225  * the destination ipsq. If the destination ipsq is not busy, it also enters
16226  * the destination ipsq exclusively. Now the actual groupname setting operation
16227  * can proceed. If the destination ipsq is busy, the operation is enqueued
16228  * on the destination (merged) ipsq and will be handled in the unwind from
16229  * ipsq_exit.
16230  *
16231  * To prevent other threads accessing the ill while the group name change is
16232  * in progres, we bring down the ipifs which also removes the ill from the
16233  * group. The group is changed in phyint and when the first ipif on the ill
16234  * is brought up, the ill is inserted into the right IPMP group by
16235  * illgrp_insert.
16236  */
16237 /* ARGSUSED */
16238 int
16239 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16240     ip_ioctl_cmd_t *ipip, void *ifreq)
16241 {
16242 	int i;
16243 	char *tmp;
16244 	int namelen;
16245 	ill_t *ill = ipif->ipif_ill;
16246 	ill_t *ill_v4, *ill_v6;
16247 	int err = 0;
16248 	phyint_t *phyi;
16249 	phyint_t *phyi_tmp;
16250 	struct lifreq *lifr;
16251 	mblk_t	*mp1;
16252 	char *groupname;
16253 	ipsq_t *ipsq;
16254 	ip_stack_t	*ipst = ill->ill_ipst;
16255 
16256 	ASSERT(IAM_WRITER_IPIF(ipif));
16257 
16258 	/* Existance verified in ip_wput_nondata */
16259 	mp1 = mp->b_cont->b_cont;
16260 	lifr = (struct lifreq *)mp1->b_rptr;
16261 	groupname = lifr->lifr_groupname;
16262 
16263 	if (ipif->ipif_id != 0)
16264 		return (EINVAL);
16265 
16266 	phyi = ill->ill_phyint;
16267 	ASSERT(phyi != NULL);
16268 
16269 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16270 		return (EINVAL);
16271 
16272 	tmp = groupname;
16273 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16274 		;
16275 
16276 	if (i == LIFNAMSIZ) {
16277 		/* no null termination */
16278 		return (EINVAL);
16279 	}
16280 
16281 	/*
16282 	 * Calculate the namelen exclusive of the null
16283 	 * termination character.
16284 	 */
16285 	namelen = tmp - groupname;
16286 
16287 	ill_v4 = phyi->phyint_illv4;
16288 	ill_v6 = phyi->phyint_illv6;
16289 
16290 	/*
16291 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16292 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16293 	 * synchronization notes in ip.c
16294 	 */
16295 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16296 		return (EINVAL);
16297 	}
16298 
16299 	/*
16300 	 * mark the ill as changing.
16301 	 * this should queue all new requests on the syncq.
16302 	 */
16303 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16304 
16305 	if (ill_v4 != NULL)
16306 		ill_v4->ill_state_flags |= ILL_CHANGING;
16307 	if (ill_v6 != NULL)
16308 		ill_v6->ill_state_flags |= ILL_CHANGING;
16309 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16310 
16311 	if (namelen == 0) {
16312 		/*
16313 		 * Null string means remove this interface from the
16314 		 * existing group.
16315 		 */
16316 		if (phyi->phyint_groupname_len == 0) {
16317 			/*
16318 			 * Never was in a group.
16319 			 */
16320 			err = 0;
16321 			goto done;
16322 		}
16323 
16324 		/*
16325 		 * IPv4 or IPv6 may be temporarily out of the group when all
16326 		 * the ipifs are down. Thus, we need to check for ill_group to
16327 		 * be non-NULL.
16328 		 */
16329 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16330 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16331 			mutex_enter(&ill_v4->ill_lock);
16332 			if (!ill_is_quiescent(ill_v4)) {
16333 				/*
16334 				 * ipsq_pending_mp_add will not fail since
16335 				 * connp is NULL
16336 				 */
16337 				(void) ipsq_pending_mp_add(NULL,
16338 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16339 				mutex_exit(&ill_v4->ill_lock);
16340 				err = EINPROGRESS;
16341 				goto done;
16342 			}
16343 			mutex_exit(&ill_v4->ill_lock);
16344 		}
16345 
16346 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16347 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16348 			mutex_enter(&ill_v6->ill_lock);
16349 			if (!ill_is_quiescent(ill_v6)) {
16350 				(void) ipsq_pending_mp_add(NULL,
16351 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16352 				mutex_exit(&ill_v6->ill_lock);
16353 				err = EINPROGRESS;
16354 				goto done;
16355 			}
16356 			mutex_exit(&ill_v6->ill_lock);
16357 		}
16358 
16359 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16360 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16361 		mutex_enter(&phyi->phyint_lock);
16362 		ASSERT(phyi->phyint_groupname != NULL);
16363 		mi_free(phyi->phyint_groupname);
16364 		phyi->phyint_groupname = NULL;
16365 		phyi->phyint_groupname_len = 0;
16366 
16367 		/* Restore the ifindex used to be the per interface one */
16368 		phyi->phyint_group_ifindex = 0;
16369 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16370 		mutex_exit(&phyi->phyint_lock);
16371 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16372 		rw_exit(&ipst->ips_ill_g_lock);
16373 		err = ill_up_ipifs(ill, q, mp);
16374 
16375 		/*
16376 		 * set the split flag so that the ipsq can be split
16377 		 */
16378 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16379 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16380 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16381 
16382 	} else {
16383 		if (phyi->phyint_groupname_len != 0) {
16384 			ASSERT(phyi->phyint_groupname != NULL);
16385 			/* Are we inserting in the same group ? */
16386 			if (mi_strcmp(groupname,
16387 			    phyi->phyint_groupname) == 0) {
16388 				err = 0;
16389 				goto done;
16390 			}
16391 		}
16392 
16393 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16394 		/*
16395 		 * Merge ipsq for the group's.
16396 		 * This check is here as multiple groups/ills might be
16397 		 * sharing the same ipsq.
16398 		 * If we have to merege than the operation is restarted
16399 		 * on the new ipsq.
16400 		 */
16401 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16402 		if (phyi->phyint_ipsq != ipsq) {
16403 			rw_exit(&ipst->ips_ill_g_lock);
16404 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16405 			goto done;
16406 		}
16407 		/*
16408 		 * Running exclusive on new ipsq.
16409 		 */
16410 
16411 		ASSERT(ipsq != NULL);
16412 		ASSERT(ipsq->ipsq_writer == curthread);
16413 
16414 		/*
16415 		 * Check whether the ill_type and ill_net_type matches before
16416 		 * we allocate any memory so that the cleanup is easier.
16417 		 *
16418 		 * We can't group dissimilar ones as we can't load spread
16419 		 * packets across the group because of potential link-level
16420 		 * header differences.
16421 		 */
16422 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16423 		if (phyi_tmp != NULL) {
16424 			if ((ill_v4 != NULL &&
16425 			    phyi_tmp->phyint_illv4 != NULL) &&
16426 			    ((ill_v4->ill_net_type !=
16427 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16428 			    (ill_v4->ill_type !=
16429 			    phyi_tmp->phyint_illv4->ill_type))) {
16430 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16431 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16432 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16433 				rw_exit(&ipst->ips_ill_g_lock);
16434 				return (EINVAL);
16435 			}
16436 			if ((ill_v6 != NULL &&
16437 			    phyi_tmp->phyint_illv6 != NULL) &&
16438 			    ((ill_v6->ill_net_type !=
16439 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16440 			    (ill_v6->ill_type !=
16441 			    phyi_tmp->phyint_illv6->ill_type))) {
16442 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16443 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16444 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16445 				rw_exit(&ipst->ips_ill_g_lock);
16446 				return (EINVAL);
16447 			}
16448 		}
16449 
16450 		rw_exit(&ipst->ips_ill_g_lock);
16451 
16452 		/*
16453 		 * bring down all v4 ipifs.
16454 		 */
16455 		if (ill_v4 != NULL) {
16456 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16457 		}
16458 
16459 		/*
16460 		 * bring down all v6 ipifs.
16461 		 */
16462 		if (ill_v6 != NULL) {
16463 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16464 		}
16465 
16466 		/*
16467 		 * make sure all ipifs are down and there are no active
16468 		 * references. Call to ipsq_pending_mp_add will not fail
16469 		 * since connp is NULL.
16470 		 */
16471 		if (ill_v4 != NULL) {
16472 			mutex_enter(&ill_v4->ill_lock);
16473 			if (!ill_is_quiescent(ill_v4)) {
16474 				(void) ipsq_pending_mp_add(NULL,
16475 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16476 				mutex_exit(&ill_v4->ill_lock);
16477 				err = EINPROGRESS;
16478 				goto done;
16479 			}
16480 			mutex_exit(&ill_v4->ill_lock);
16481 		}
16482 
16483 		if (ill_v6 != NULL) {
16484 			mutex_enter(&ill_v6->ill_lock);
16485 			if (!ill_is_quiescent(ill_v6)) {
16486 				(void) ipsq_pending_mp_add(NULL,
16487 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16488 				mutex_exit(&ill_v6->ill_lock);
16489 				err = EINPROGRESS;
16490 				goto done;
16491 			}
16492 			mutex_exit(&ill_v6->ill_lock);
16493 		}
16494 
16495 		/*
16496 		 * allocate including space for null terminator
16497 		 * before we insert.
16498 		 */
16499 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16500 		if (tmp == NULL)
16501 			return (ENOMEM);
16502 
16503 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16504 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16505 		mutex_enter(&phyi->phyint_lock);
16506 		if (phyi->phyint_groupname_len != 0) {
16507 			ASSERT(phyi->phyint_groupname != NULL);
16508 			mi_free(phyi->phyint_groupname);
16509 		}
16510 
16511 		/*
16512 		 * setup the new group name.
16513 		 */
16514 		phyi->phyint_groupname = tmp;
16515 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16516 		phyi->phyint_groupname_len = namelen + 1;
16517 
16518 		if (ipst->ips_ipmp_hook_emulation) {
16519 			/*
16520 			 * If the group already exists we use the existing
16521 			 * group_ifindex, otherwise we pick a new index here.
16522 			 */
16523 			if (phyi_tmp != NULL) {
16524 				phyi->phyint_group_ifindex =
16525 				    phyi_tmp->phyint_group_ifindex;
16526 			} else {
16527 				/* XXX We need a recovery strategy here. */
16528 				if (!ip_assign_ifindex(
16529 				    &phyi->phyint_group_ifindex, ipst))
16530 					cmn_err(CE_PANIC,
16531 					    "ip_assign_ifindex() failed");
16532 			}
16533 		}
16534 		/*
16535 		 * Select whether the netinfo and hook use the per-interface
16536 		 * or per-group ifindex.
16537 		 */
16538 		if (ipst->ips_ipmp_hook_emulation)
16539 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16540 		else
16541 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16542 
16543 		if (ipst->ips_ipmp_hook_emulation &&
16544 		    phyi_tmp != NULL) {
16545 			/* First phyint in group - group PLUMB event */
16546 			ill_nic_info_plumb(ill, B_TRUE);
16547 		}
16548 		mutex_exit(&phyi->phyint_lock);
16549 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16550 		rw_exit(&ipst->ips_ill_g_lock);
16551 
16552 		err = ill_up_ipifs(ill, q, mp);
16553 	}
16554 
16555 done:
16556 	/*
16557 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16558 	 */
16559 	if (err != EINPROGRESS) {
16560 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16561 		if (ill_v4 != NULL)
16562 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16563 		if (ill_v6 != NULL)
16564 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16565 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16566 	}
16567 	return (err);
16568 }
16569 
16570 /* ARGSUSED */
16571 int
16572 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16573     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16574 {
16575 	ill_t *ill;
16576 	phyint_t *phyi;
16577 	struct lifreq *lifr;
16578 	mblk_t	*mp1;
16579 
16580 	/* Existence verified in ip_wput_nondata */
16581 	mp1 = mp->b_cont->b_cont;
16582 	lifr = (struct lifreq *)mp1->b_rptr;
16583 	ill = ipif->ipif_ill;
16584 	phyi = ill->ill_phyint;
16585 
16586 	lifr->lifr_groupname[0] = '\0';
16587 	/*
16588 	 * ill_group may be null if all the interfaces
16589 	 * are down. But still, the phyint should always
16590 	 * hold the name.
16591 	 */
16592 	if (phyi->phyint_groupname_len != 0) {
16593 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16594 		    phyi->phyint_groupname_len);
16595 	}
16596 
16597 	return (0);
16598 }
16599 
16600 
16601 typedef struct conn_move_s {
16602 	ill_t	*cm_from_ill;
16603 	ill_t	*cm_to_ill;
16604 	int	cm_ifindex;
16605 } conn_move_t;
16606 
16607 /*
16608  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16609  */
16610 static void
16611 conn_move(conn_t *connp, caddr_t arg)
16612 {
16613 	conn_move_t *connm;
16614 	int ifindex;
16615 	int i;
16616 	ill_t *from_ill;
16617 	ill_t *to_ill;
16618 	ilg_t *ilg;
16619 	ilm_t *ret_ilm;
16620 
16621 	connm = (conn_move_t *)arg;
16622 	ifindex = connm->cm_ifindex;
16623 	from_ill = connm->cm_from_ill;
16624 	to_ill = connm->cm_to_ill;
16625 
16626 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16627 
16628 	/* All multicast fields protected by conn_lock */
16629 	mutex_enter(&connp->conn_lock);
16630 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16631 	if ((connp->conn_outgoing_ill == from_ill) &&
16632 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16633 		connp->conn_outgoing_ill = to_ill;
16634 		connp->conn_incoming_ill = to_ill;
16635 	}
16636 
16637 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16638 
16639 	if ((connp->conn_multicast_ill == from_ill) &&
16640 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16641 		connp->conn_multicast_ill = connm->cm_to_ill;
16642 	}
16643 
16644 	/*
16645 	 * Change the ilg_ill to point to the new one. This assumes
16646 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16647 	 * has been told to receive packets on this interface.
16648 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16649 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16650 	 * some ilms may not have moved. We check to see whether
16651 	 * the ilms have moved to to_ill. We can't check on from_ill
16652 	 * as in the process of moving, we could have split an ilm
16653 	 * in to two - which has the same orig_ifindex and v6group.
16654 	 *
16655 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16656 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16657 	 */
16658 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16659 		ilg = &connp->conn_ilg[i];
16660 		if ((ilg->ilg_ill == from_ill) &&
16661 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16662 			/* ifindex != 0 indicates failback */
16663 			if (ifindex != 0) {
16664 				connp->conn_ilg[i].ilg_ill = to_ill;
16665 				continue;
16666 			}
16667 
16668 			mutex_enter(&to_ill->ill_lock);
16669 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16670 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16671 			    connp->conn_zoneid);
16672 			mutex_exit(&to_ill->ill_lock);
16673 
16674 			if (ret_ilm != NULL)
16675 				connp->conn_ilg[i].ilg_ill = to_ill;
16676 		}
16677 	}
16678 	mutex_exit(&connp->conn_lock);
16679 }
16680 
16681 static void
16682 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16683 {
16684 	conn_move_t connm;
16685 	ip_stack_t	*ipst = from_ill->ill_ipst;
16686 
16687 	connm.cm_from_ill = from_ill;
16688 	connm.cm_to_ill = to_ill;
16689 	connm.cm_ifindex = ifindex;
16690 
16691 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16692 }
16693 
16694 /*
16695  * ilm has been moved from from_ill to to_ill.
16696  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16697  * appropriately.
16698  *
16699  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16700  *	  the code there de-references ipif_ill to get the ill to
16701  *	  send multicast requests. It does not work as ipif is on its
16702  *	  move and already moved when this function is called.
16703  *	  Thus, we need to use from_ill and to_ill send down multicast
16704  *	  requests.
16705  */
16706 static void
16707 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16708 {
16709 	ipif_t *ipif;
16710 	ilm_t *ilm;
16711 
16712 	/*
16713 	 * See whether we need to send down DL_ENABMULTI_REQ on
16714 	 * to_ill as ilm has just been added.
16715 	 */
16716 	ASSERT(IAM_WRITER_ILL(to_ill));
16717 	ASSERT(IAM_WRITER_ILL(from_ill));
16718 
16719 	ILM_WALKER_HOLD(to_ill);
16720 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16721 
16722 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16723 			continue;
16724 		/*
16725 		 * no locks held, ill/ipif cannot dissappear as long
16726 		 * as we are writer.
16727 		 */
16728 		ipif = to_ill->ill_ipif;
16729 		/*
16730 		 * No need to hold any lock as we are the writer and this
16731 		 * can only be changed by a writer.
16732 		 */
16733 		ilm->ilm_is_new = B_FALSE;
16734 
16735 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16736 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16737 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16738 			    "resolver\n"));
16739 			continue;		/* Must be IRE_IF_NORESOLVER */
16740 		}
16741 
16742 
16743 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16744 			ip1dbg(("ilm_send_multicast_reqs: "
16745 			    "to_ill MULTI_BCAST\n"));
16746 			goto from;
16747 		}
16748 
16749 		if (to_ill->ill_isv6)
16750 			mld_joingroup(ilm);
16751 		else
16752 			igmp_joingroup(ilm);
16753 
16754 		if (to_ill->ill_ipif_up_count == 0) {
16755 			/*
16756 			 * Nobody there. All multicast addresses will be
16757 			 * re-joined when we get the DL_BIND_ACK bringing the
16758 			 * interface up.
16759 			 */
16760 			ilm->ilm_notify_driver = B_FALSE;
16761 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16762 			goto from;
16763 		}
16764 
16765 		/*
16766 		 * For allmulti address, we want to join on only one interface.
16767 		 * Checking for ilm_numentries_v6 is not correct as you may
16768 		 * find an ilm with zero address on to_ill, but we may not
16769 		 * have nominated to_ill for receiving. Thus, if we have
16770 		 * nominated from_ill (ill_join_allmulti is set), nominate
16771 		 * only if to_ill is not already nominated (to_ill normally
16772 		 * should not have been nominated if "from_ill" has already
16773 		 * been nominated. As we don't prevent failovers from happening
16774 		 * across groups, we don't assert).
16775 		 */
16776 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16777 			/*
16778 			 * There is no need to hold ill locks as we are
16779 			 * writer on both ills and when ill_join_allmulti
16780 			 * is changed the thread is always a writer.
16781 			 */
16782 			if (from_ill->ill_join_allmulti &&
16783 			    !to_ill->ill_join_allmulti) {
16784 				(void) ip_join_allmulti(to_ill->ill_ipif);
16785 			}
16786 		} else if (ilm->ilm_notify_driver) {
16787 
16788 			/*
16789 			 * This is a newly moved ilm so we need to tell the
16790 			 * driver about the new group. There can be more than
16791 			 * one ilm's for the same group in the list each with a
16792 			 * different orig_ifindex. We have to inform the driver
16793 			 * once. In ilm_move_v[4,6] we only set the flag
16794 			 * ilm_notify_driver for the first ilm.
16795 			 */
16796 
16797 			(void) ip_ll_send_enabmulti_req(to_ill,
16798 			    &ilm->ilm_v6addr);
16799 		}
16800 
16801 		ilm->ilm_notify_driver = B_FALSE;
16802 
16803 		/*
16804 		 * See whether we need to send down DL_DISABMULTI_REQ on
16805 		 * from_ill as ilm has just been removed.
16806 		 */
16807 from:
16808 		ipif = from_ill->ill_ipif;
16809 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16810 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16811 			ip1dbg(("ilm_send_multicast_reqs: "
16812 			    "from_ill not resolver\n"));
16813 			continue;		/* Must be IRE_IF_NORESOLVER */
16814 		}
16815 
16816 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16817 			ip1dbg(("ilm_send_multicast_reqs: "
16818 			    "from_ill MULTI_BCAST\n"));
16819 			continue;
16820 		}
16821 
16822 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16823 			if (from_ill->ill_join_allmulti)
16824 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16825 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16826 			(void) ip_ll_send_disabmulti_req(from_ill,
16827 			    &ilm->ilm_v6addr);
16828 		}
16829 	}
16830 	ILM_WALKER_RELE(to_ill);
16831 }
16832 
16833 /*
16834  * This function is called when all multicast memberships needs
16835  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16836  * called only once unlike the IPv4 counterpart where it is called after
16837  * every logical interface is moved. The reason is due to multicast
16838  * memberships are joined using an interface address in IPv4 while in
16839  * IPv6, interface index is used.
16840  */
16841 static void
16842 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16843 {
16844 	ilm_t	*ilm;
16845 	ilm_t	*ilm_next;
16846 	ilm_t	*new_ilm;
16847 	ilm_t	**ilmp;
16848 	int	count;
16849 	char buf[INET6_ADDRSTRLEN];
16850 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16851 	ip_stack_t	*ipst = from_ill->ill_ipst;
16852 
16853 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16854 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16855 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16856 
16857 	if (ifindex == 0) {
16858 		/*
16859 		 * Form the solicited node mcast address which is used later.
16860 		 */
16861 		ipif_t *ipif;
16862 
16863 		ipif = from_ill->ill_ipif;
16864 		ASSERT(ipif->ipif_id == 0);
16865 
16866 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16867 	}
16868 
16869 	ilmp = &from_ill->ill_ilm;
16870 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16871 		ilm_next = ilm->ilm_next;
16872 
16873 		if (ilm->ilm_flags & ILM_DELETED) {
16874 			ilmp = &ilm->ilm_next;
16875 			continue;
16876 		}
16877 
16878 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16879 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16880 		ASSERT(ilm->ilm_orig_ifindex != 0);
16881 		if (ilm->ilm_orig_ifindex == ifindex) {
16882 			/*
16883 			 * We are failing back multicast memberships.
16884 			 * If the same ilm exists in to_ill, it means somebody
16885 			 * has joined the same group there e.g. ff02::1
16886 			 * is joined within the kernel when the interfaces
16887 			 * came UP.
16888 			 */
16889 			ASSERT(ilm->ilm_ipif == NULL);
16890 			if (new_ilm != NULL) {
16891 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16892 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16893 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16894 					new_ilm->ilm_is_new = B_TRUE;
16895 				}
16896 			} else {
16897 				/*
16898 				 * check if we can just move the ilm
16899 				 */
16900 				if (from_ill->ill_ilm_walker_cnt != 0) {
16901 					/*
16902 					 * We have walkers we cannot move
16903 					 * the ilm, so allocate a new ilm,
16904 					 * this (old) ilm will be marked
16905 					 * ILM_DELETED at the end of the loop
16906 					 * and will be freed when the
16907 					 * last walker exits.
16908 					 */
16909 					new_ilm = (ilm_t *)mi_zalloc
16910 					    (sizeof (ilm_t));
16911 					if (new_ilm == NULL) {
16912 						ip0dbg(("ilm_move_v6: "
16913 						    "FAILBACK of IPv6"
16914 						    " multicast address %s : "
16915 						    "from %s to"
16916 						    " %s failed : ENOMEM \n",
16917 						    inet_ntop(AF_INET6,
16918 						    &ilm->ilm_v6addr, buf,
16919 						    sizeof (buf)),
16920 						    from_ill->ill_name,
16921 						    to_ill->ill_name));
16922 
16923 							ilmp = &ilm->ilm_next;
16924 							continue;
16925 					}
16926 					*new_ilm = *ilm;
16927 					/*
16928 					 * we don't want new_ilm linked to
16929 					 * ilm's filter list.
16930 					 */
16931 					new_ilm->ilm_filter = NULL;
16932 				} else {
16933 					/*
16934 					 * No walkers we can move the ilm.
16935 					 * lets take it out of the list.
16936 					 */
16937 					*ilmp = ilm->ilm_next;
16938 					ilm->ilm_next = NULL;
16939 					DTRACE_PROBE3(ill__decr__cnt,
16940 					    (ill_t *), from_ill,
16941 					    (char *), "ilm", (void *), ilm);
16942 					ASSERT(from_ill->ill_ilm_cnt > 0);
16943 					from_ill->ill_ilm_cnt--;
16944 
16945 					new_ilm = ilm;
16946 				}
16947 
16948 				/*
16949 				 * if this is the first ilm for the group
16950 				 * set ilm_notify_driver so that we notify the
16951 				 * driver in ilm_send_multicast_reqs.
16952 				 */
16953 				if (ilm_lookup_ill_v6(to_ill,
16954 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16955 					new_ilm->ilm_notify_driver = B_TRUE;
16956 
16957 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16958 				    (char *), "ilm", (void *), new_ilm);
16959 				new_ilm->ilm_ill = to_ill;
16960 				to_ill->ill_ilm_cnt++;
16961 
16962 				/* Add to the to_ill's list */
16963 				new_ilm->ilm_next = to_ill->ill_ilm;
16964 				to_ill->ill_ilm = new_ilm;
16965 				/*
16966 				 * set the flag so that mld_joingroup is
16967 				 * called in ilm_send_multicast_reqs().
16968 				 */
16969 				new_ilm->ilm_is_new = B_TRUE;
16970 			}
16971 			goto bottom;
16972 		} else if (ifindex != 0) {
16973 			/*
16974 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16975 			 * has not matched above, look at the next ilm.
16976 			 */
16977 			ilmp = &ilm->ilm_next;
16978 			continue;
16979 		}
16980 		/*
16981 		 * If we are here, it means ifindex is 0. Failover
16982 		 * everything.
16983 		 *
16984 		 * We need to handle solicited node mcast address
16985 		 * and all_nodes mcast address differently as they
16986 		 * are joined witin the kenrel (ipif_multicast_up)
16987 		 * and potentially from the userland. We are called
16988 		 * after the ipifs of from_ill has been moved.
16989 		 * If we still find ilms on ill with solicited node
16990 		 * mcast address or all_nodes mcast address, it must
16991 		 * belong to the UP interface that has not moved e.g.
16992 		 * ipif_id 0 with the link local prefix does not move.
16993 		 * We join this on the new ill accounting for all the
16994 		 * userland memberships so that applications don't
16995 		 * see any failure.
16996 		 *
16997 		 * We need to make sure that we account only for the
16998 		 * solicited node and all node multicast addresses
16999 		 * that was brought UP on these. In the case of
17000 		 * a failover from A to B, we might have ilms belonging
17001 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17002 		 * for the membership from the userland. If we are failing
17003 		 * over from B to C now, we will find the ones belonging
17004 		 * to A on B. These don't account for the ill_ipif_up_count.
17005 		 * They just move from B to C. The check below on
17006 		 * ilm_orig_ifindex ensures that.
17007 		 */
17008 		if ((ilm->ilm_orig_ifindex ==
17009 		    from_ill->ill_phyint->phyint_ifindex) &&
17010 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17011 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17012 		    &ilm->ilm_v6addr))) {
17013 			ASSERT(ilm->ilm_refcnt > 0);
17014 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17015 			/*
17016 			 * For indentation reasons, we are not using a
17017 			 * "else" here.
17018 			 */
17019 			if (count == 0) {
17020 				ilmp = &ilm->ilm_next;
17021 				continue;
17022 			}
17023 			ilm->ilm_refcnt -= count;
17024 			if (new_ilm != NULL) {
17025 				/*
17026 				 * Can find one with the same
17027 				 * ilm_orig_ifindex, if we are failing
17028 				 * over to a STANDBY. This happens
17029 				 * when somebody wants to join a group
17030 				 * on a STANDBY interface and we
17031 				 * internally join on a different one.
17032 				 * If we had joined on from_ill then, a
17033 				 * failover now will find a new ilm
17034 				 * with this index.
17035 				 */
17036 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17037 				    " new ilm on %s, group address %s\n",
17038 				    to_ill->ill_name,
17039 				    inet_ntop(AF_INET6,
17040 				    &ilm->ilm_v6addr, buf,
17041 				    sizeof (buf))));
17042 				new_ilm->ilm_refcnt += count;
17043 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17044 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17045 					new_ilm->ilm_is_new = B_TRUE;
17046 				}
17047 			} else {
17048 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17049 				if (new_ilm == NULL) {
17050 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17051 					    " multicast address %s : from %s to"
17052 					    " %s failed : ENOMEM \n",
17053 					    inet_ntop(AF_INET6,
17054 					    &ilm->ilm_v6addr, buf,
17055 					    sizeof (buf)), from_ill->ill_name,
17056 					    to_ill->ill_name));
17057 					ilmp = &ilm->ilm_next;
17058 					continue;
17059 				}
17060 				*new_ilm = *ilm;
17061 				new_ilm->ilm_filter = NULL;
17062 				new_ilm->ilm_refcnt = count;
17063 				new_ilm->ilm_timer = INFINITY;
17064 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17065 				new_ilm->ilm_is_new = B_TRUE;
17066 				/*
17067 				 * If the to_ill has not joined this
17068 				 * group we need to tell the driver in
17069 				 * ill_send_multicast_reqs.
17070 				 */
17071 				if (ilm_lookup_ill_v6(to_ill,
17072 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17073 					new_ilm->ilm_notify_driver = B_TRUE;
17074 
17075 				new_ilm->ilm_ill = to_ill;
17076 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17077 				    (char *), "ilm", (void *), new_ilm);
17078 				to_ill->ill_ilm_cnt++;
17079 
17080 				/* Add to the to_ill's list */
17081 				new_ilm->ilm_next = to_ill->ill_ilm;
17082 				to_ill->ill_ilm = new_ilm;
17083 				ASSERT(new_ilm->ilm_ipif == NULL);
17084 			}
17085 			if (ilm->ilm_refcnt == 0) {
17086 				goto bottom;
17087 			} else {
17088 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17089 				CLEAR_SLIST(new_ilm->ilm_filter);
17090 				ilmp = &ilm->ilm_next;
17091 			}
17092 			continue;
17093 		} else {
17094 			/*
17095 			 * ifindex = 0 means, move everything pointing at
17096 			 * from_ill. We are doing this becuase ill has
17097 			 * either FAILED or became INACTIVE.
17098 			 *
17099 			 * As we would like to move things later back to
17100 			 * from_ill, we want to retain the identity of this
17101 			 * ilm. Thus, we don't blindly increment the reference
17102 			 * count on the ilms matching the address alone. We
17103 			 * need to match on the ilm_orig_index also. new_ilm
17104 			 * was obtained by matching ilm_orig_index also.
17105 			 */
17106 			if (new_ilm != NULL) {
17107 				/*
17108 				 * This is possible only if a previous restore
17109 				 * was incomplete i.e restore to
17110 				 * ilm_orig_ifindex left some ilms because
17111 				 * of some failures. Thus when we are failing
17112 				 * again, we might find our old friends there.
17113 				 */
17114 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17115 				    " on %s, group address %s\n",
17116 				    to_ill->ill_name,
17117 				    inet_ntop(AF_INET6,
17118 				    &ilm->ilm_v6addr, buf,
17119 				    sizeof (buf))));
17120 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17121 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17122 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17123 					new_ilm->ilm_is_new = B_TRUE;
17124 				}
17125 			} else {
17126 				if (from_ill->ill_ilm_walker_cnt != 0) {
17127 					new_ilm = (ilm_t *)
17128 					    mi_zalloc(sizeof (ilm_t));
17129 					if (new_ilm == NULL) {
17130 						ip0dbg(("ilm_move_v6: "
17131 						    "FAILOVER of IPv6"
17132 						    " multicast address %s : "
17133 						    "from %s to"
17134 						    " %s failed : ENOMEM \n",
17135 						    inet_ntop(AF_INET6,
17136 						    &ilm->ilm_v6addr, buf,
17137 						    sizeof (buf)),
17138 						    from_ill->ill_name,
17139 						    to_ill->ill_name));
17140 
17141 							ilmp = &ilm->ilm_next;
17142 							continue;
17143 					}
17144 					*new_ilm = *ilm;
17145 					new_ilm->ilm_filter = NULL;
17146 				} else {
17147 					*ilmp = ilm->ilm_next;
17148 					DTRACE_PROBE3(ill__decr__cnt,
17149 					    (ill_t *), from_ill,
17150 					    (char *), "ilm", (void *), ilm);
17151 					ASSERT(from_ill->ill_ilm_cnt > 0);
17152 					from_ill->ill_ilm_cnt--;
17153 
17154 					new_ilm = ilm;
17155 				}
17156 				/*
17157 				 * If the to_ill has not joined this
17158 				 * group we need to tell the driver in
17159 				 * ill_send_multicast_reqs.
17160 				 */
17161 				if (ilm_lookup_ill_v6(to_ill,
17162 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17163 					new_ilm->ilm_notify_driver = B_TRUE;
17164 
17165 				/* Add to the to_ill's list */
17166 				new_ilm->ilm_next = to_ill->ill_ilm;
17167 				to_ill->ill_ilm = new_ilm;
17168 				ASSERT(ilm->ilm_ipif == NULL);
17169 				new_ilm->ilm_ill = to_ill;
17170 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17171 				    (char *), "ilm", (void *), new_ilm);
17172 				to_ill->ill_ilm_cnt++;
17173 				new_ilm->ilm_is_new = B_TRUE;
17174 			}
17175 
17176 		}
17177 
17178 bottom:
17179 		/*
17180 		 * Revert multicast filter state to (EXCLUDE, NULL).
17181 		 * new_ilm->ilm_is_new should already be set if needed.
17182 		 */
17183 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17184 		CLEAR_SLIST(new_ilm->ilm_filter);
17185 		/*
17186 		 * We allocated/got a new ilm, free the old one.
17187 		 */
17188 		if (new_ilm != ilm) {
17189 			if (from_ill->ill_ilm_walker_cnt == 0) {
17190 				*ilmp = ilm->ilm_next;
17191 
17192 				ASSERT(ilm->ilm_ipif == NULL); /* ipv6 */
17193 				DTRACE_PROBE3(ill__decr__cnt, (ill_t *),
17194 				    from_ill, (char *), "ilm", (void *), ilm);
17195 				ASSERT(from_ill->ill_ilm_cnt > 0);
17196 				from_ill->ill_ilm_cnt--;
17197 
17198 				ilm_inactive(ilm); /* frees this ilm */
17199 
17200 			} else {
17201 				ilm->ilm_flags |= ILM_DELETED;
17202 				from_ill->ill_ilm_cleanup_reqd = 1;
17203 				ilmp = &ilm->ilm_next;
17204 			}
17205 		}
17206 	}
17207 }
17208 
17209 /*
17210  * Move all the multicast memberships to to_ill. Called when
17211  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17212  * different from IPv6 counterpart as multicast memberships are associated
17213  * with ills in IPv6. This function is called after every ipif is moved
17214  * unlike IPv6, where it is moved only once.
17215  */
17216 static void
17217 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17218 {
17219 	ilm_t	*ilm;
17220 	ilm_t	*ilm_next;
17221 	ilm_t	*new_ilm;
17222 	ilm_t	**ilmp;
17223 	ip_stack_t	*ipst = from_ill->ill_ipst;
17224 
17225 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17226 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17227 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17228 
17229 	ilmp = &from_ill->ill_ilm;
17230 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17231 		ilm_next = ilm->ilm_next;
17232 
17233 		if (ilm->ilm_flags & ILM_DELETED) {
17234 			ilmp = &ilm->ilm_next;
17235 			continue;
17236 		}
17237 
17238 		ASSERT(ilm->ilm_ipif != NULL);
17239 
17240 		if (ilm->ilm_ipif != ipif) {
17241 			ilmp = &ilm->ilm_next;
17242 			continue;
17243 		}
17244 
17245 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17246 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17247 			new_ilm = ilm_lookup_ipif(ipif,
17248 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17249 			if (new_ilm != NULL) {
17250 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17251 				/*
17252 				 * We still need to deal with the from_ill.
17253 				 */
17254 				new_ilm->ilm_is_new = B_TRUE;
17255 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17256 				CLEAR_SLIST(new_ilm->ilm_filter);
17257 				ASSERT(ilm->ilm_ipif == ipif);
17258 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17259 				if (from_ill->ill_ilm_walker_cnt == 0) {
17260 					DTRACE_PROBE3(ill__decr__cnt,
17261 					    (ill_t *), from_ill,
17262 					    (char *), "ilm", (void *), ilm);
17263 					ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17264 				}
17265 				goto delete_ilm;
17266 			}
17267 			/*
17268 			 * If we could not find one e.g. ipif is
17269 			 * still down on to_ill, we add this ilm
17270 			 * on ill_new to preserve the reference
17271 			 * count.
17272 			 */
17273 		}
17274 		/*
17275 		 * When ipifs move, ilms always move with it
17276 		 * to the NEW ill. Thus we should never be
17277 		 * able to find ilm till we really move it here.
17278 		 */
17279 		ASSERT(ilm_lookup_ipif(ipif,
17280 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17281 
17282 		if (from_ill->ill_ilm_walker_cnt != 0) {
17283 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17284 			if (new_ilm == NULL) {
17285 				char buf[INET6_ADDRSTRLEN];
17286 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17287 				    " multicast address %s : "
17288 				    "from %s to"
17289 				    " %s failed : ENOMEM \n",
17290 				    inet_ntop(AF_INET,
17291 				    &ilm->ilm_v6addr, buf,
17292 				    sizeof (buf)),
17293 				    from_ill->ill_name,
17294 				    to_ill->ill_name));
17295 
17296 				ilmp = &ilm->ilm_next;
17297 				continue;
17298 			}
17299 			*new_ilm = *ilm;
17300 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
17301 			    (char *), "ilm", (void *), ilm);
17302 			new_ilm->ilm_ipif->ipif_ilm_cnt++;
17303 			/* We don't want new_ilm linked to ilm's filter list */
17304 			new_ilm->ilm_filter = NULL;
17305 		} else {
17306 			/* Remove from the list */
17307 			*ilmp = ilm->ilm_next;
17308 			new_ilm = ilm;
17309 		}
17310 
17311 		/*
17312 		 * If we have never joined this group on the to_ill
17313 		 * make sure we tell the driver.
17314 		 */
17315 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17316 		    ALL_ZONES) == NULL)
17317 			new_ilm->ilm_notify_driver = B_TRUE;
17318 
17319 		/* Add to the to_ill's list */
17320 		new_ilm->ilm_next = to_ill->ill_ilm;
17321 		to_ill->ill_ilm = new_ilm;
17322 		new_ilm->ilm_is_new = B_TRUE;
17323 
17324 		/*
17325 		 * Revert multicast filter state to (EXCLUDE, NULL)
17326 		 */
17327 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17328 		CLEAR_SLIST(new_ilm->ilm_filter);
17329 
17330 		/*
17331 		 * Delete only if we have allocated a new ilm.
17332 		 */
17333 		if (new_ilm != ilm) {
17334 delete_ilm:
17335 			if (from_ill->ill_ilm_walker_cnt == 0) {
17336 				/* Remove from the list */
17337 				*ilmp = ilm->ilm_next;
17338 				ilm->ilm_next = NULL;
17339 				DTRACE_PROBE3(ipif__decr__cnt,
17340 				    (ipif_t *), ilm->ilm_ipif,
17341 				    (char *), "ilm", (void *), ilm);
17342 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17343 				ilm->ilm_ipif->ipif_ilm_cnt--;
17344 				ilm_inactive(ilm);
17345 			} else {
17346 				ilm->ilm_flags |= ILM_DELETED;
17347 				from_ill->ill_ilm_cleanup_reqd = 1;
17348 				ilmp = &ilm->ilm_next;
17349 			}
17350 		}
17351 	}
17352 }
17353 
17354 static uint_t
17355 ipif_get_id(ill_t *ill, uint_t id)
17356 {
17357 	uint_t	unit;
17358 	ipif_t	*tipif;
17359 	boolean_t found = B_FALSE;
17360 	ip_stack_t	*ipst = ill->ill_ipst;
17361 
17362 	/*
17363 	 * During failback, we want to go back to the same id
17364 	 * instead of the smallest id so that the original
17365 	 * configuration is maintained. id is non-zero in that
17366 	 * case.
17367 	 */
17368 	if (id != 0) {
17369 		/*
17370 		 * While failing back, if we still have an ipif with
17371 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17372 		 * as soon as we return from this function. It was
17373 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17374 		 * we can choose the smallest id. Thus we return zero
17375 		 * in that case ignoring the hint.
17376 		 */
17377 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17378 			return (0);
17379 		for (tipif = ill->ill_ipif; tipif != NULL;
17380 		    tipif = tipif->ipif_next) {
17381 			if (tipif->ipif_id == id) {
17382 				found = B_TRUE;
17383 				break;
17384 			}
17385 		}
17386 		/*
17387 		 * If somebody already plumbed another logical
17388 		 * with the same id, we won't be able to find it.
17389 		 */
17390 		if (!found)
17391 			return (id);
17392 	}
17393 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17394 		found = B_FALSE;
17395 		for (tipif = ill->ill_ipif; tipif != NULL;
17396 		    tipif = tipif->ipif_next) {
17397 			if (tipif->ipif_id == unit) {
17398 				found = B_TRUE;
17399 				break;
17400 			}
17401 		}
17402 		if (!found)
17403 			break;
17404 	}
17405 	return (unit);
17406 }
17407 
17408 /* ARGSUSED */
17409 static int
17410 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17411     ipif_t **rep_ipif_ptr)
17412 {
17413 	ill_t	*from_ill;
17414 	ipif_t	*rep_ipif;
17415 	uint_t	unit;
17416 	int err = 0;
17417 	ipif_t	*to_ipif;
17418 	struct iocblk	*iocp;
17419 	boolean_t failback_cmd;
17420 	boolean_t remove_ipif;
17421 	int	rc;
17422 	ip_stack_t	*ipst;
17423 
17424 	ASSERT(IAM_WRITER_ILL(to_ill));
17425 	ASSERT(IAM_WRITER_IPIF(ipif));
17426 
17427 	iocp = (struct iocblk *)mp->b_rptr;
17428 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17429 	remove_ipif = B_FALSE;
17430 
17431 	from_ill = ipif->ipif_ill;
17432 	ipst = from_ill->ill_ipst;
17433 
17434 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17435 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17436 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17437 
17438 	/*
17439 	 * Don't move LINK LOCAL addresses as they are tied to
17440 	 * physical interface.
17441 	 */
17442 	if (from_ill->ill_isv6 &&
17443 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17444 		ipif->ipif_was_up = B_FALSE;
17445 		IPIF_UNMARK_MOVING(ipif);
17446 		return (0);
17447 	}
17448 
17449 	/*
17450 	 * We set the ipif_id to maximum so that the search for
17451 	 * ipif_id will pick the lowest number i.e 0 in the
17452 	 * following 2 cases :
17453 	 *
17454 	 * 1) We have a replacement ipif at the head of to_ill.
17455 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17456 	 *    on to_ill and hence the MOVE might fail. We want to
17457 	 *    remove it only if we could move the ipif. Thus, by
17458 	 *    setting it to the MAX value, we make the search in
17459 	 *    ipif_get_id return the zeroth id.
17460 	 *
17461 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17462 	 *    we might just have a zero address plumbed on the ipif
17463 	 *    with zero id in the case of IPv4. We remove that while
17464 	 *    doing the failback. We want to remove it only if we
17465 	 *    could move the ipif. Thus, by setting it to the MAX
17466 	 *    value, we make the search in ipif_get_id return the
17467 	 *    zeroth id.
17468 	 *
17469 	 * Both (1) and (2) are done only when when we are moving
17470 	 * an ipif (either due to failover/failback) which originally
17471 	 * belonged to this interface i.e the ipif_orig_ifindex is
17472 	 * the same as to_ill's ifindex. This is needed so that
17473 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17474 	 * from B -> A (B is being removed from the group) and
17475 	 * FAILBACK from A -> B restores the original configuration.
17476 	 * Without the check for orig_ifindex, the second FAILOVER
17477 	 * could make the ipif belonging to B replace the A's zeroth
17478 	 * ipif and the subsequent failback re-creating the replacement
17479 	 * ipif again.
17480 	 *
17481 	 * NOTE : We created the replacement ipif when we did a
17482 	 * FAILOVER (See below). We could check for FAILBACK and
17483 	 * then look for replacement ipif to be removed. But we don't
17484 	 * want to do that because we wan't to allow the possibility
17485 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17486 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17487 	 * from B -> A.
17488 	 */
17489 	to_ipif = to_ill->ill_ipif;
17490 	if ((to_ill->ill_phyint->phyint_ifindex ==
17491 	    ipif->ipif_orig_ifindex) &&
17492 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17493 		ASSERT(to_ipif->ipif_id == 0);
17494 		remove_ipif = B_TRUE;
17495 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17496 	}
17497 	/*
17498 	 * Find the lowest logical unit number on the to_ill.
17499 	 * If we are failing back, try to get the original id
17500 	 * rather than the lowest one so that the original
17501 	 * configuration is maintained.
17502 	 *
17503 	 * XXX need a better scheme for this.
17504 	 */
17505 	if (failback_cmd) {
17506 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17507 	} else {
17508 		unit = ipif_get_id(to_ill, 0);
17509 	}
17510 
17511 	/* Reset back to zero in case we fail below */
17512 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17513 		to_ipif->ipif_id = 0;
17514 
17515 	if (unit == ipst->ips_ip_addrs_per_if) {
17516 		ipif->ipif_was_up = B_FALSE;
17517 		IPIF_UNMARK_MOVING(ipif);
17518 		return (EINVAL);
17519 	}
17520 
17521 	/*
17522 	 * ipif is ready to move from "from_ill" to "to_ill".
17523 	 *
17524 	 * 1) If we are moving ipif with id zero, create a
17525 	 *    replacement ipif for this ipif on from_ill. If this fails
17526 	 *    fail the MOVE operation.
17527 	 *
17528 	 * 2) Remove the replacement ipif on to_ill if any.
17529 	 *    We could remove the replacement ipif when we are moving
17530 	 *    the ipif with id zero. But what if somebody already
17531 	 *    unplumbed it ? Thus we always remove it if it is present.
17532 	 *    We want to do it only if we are sure we are going to
17533 	 *    move the ipif to to_ill which is why there are no
17534 	 *    returns due to error till ipif is linked to to_ill.
17535 	 *    Note that the first ipif that we failback will always
17536 	 *    be zero if it is present.
17537 	 */
17538 	if (ipif->ipif_id == 0) {
17539 		ipaddr_t inaddr_any = INADDR_ANY;
17540 
17541 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17542 		if (rep_ipif == NULL) {
17543 			ipif->ipif_was_up = B_FALSE;
17544 			IPIF_UNMARK_MOVING(ipif);
17545 			return (ENOMEM);
17546 		}
17547 		*rep_ipif = ipif_zero;
17548 		/*
17549 		 * Before we put the ipif on the list, store the addresses
17550 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17551 		 * assumes so. This logic is not any different from what
17552 		 * ipif_allocate does.
17553 		 */
17554 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17555 		    &rep_ipif->ipif_v6lcl_addr);
17556 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17557 		    &rep_ipif->ipif_v6src_addr);
17558 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17559 		    &rep_ipif->ipif_v6subnet);
17560 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17561 		    &rep_ipif->ipif_v6net_mask);
17562 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17563 		    &rep_ipif->ipif_v6brd_addr);
17564 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17565 		    &rep_ipif->ipif_v6pp_dst_addr);
17566 		/*
17567 		 * We mark IPIF_NOFAILOVER so that this can never
17568 		 * move.
17569 		 */
17570 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17571 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17572 		rep_ipif->ipif_replace_zero = B_TRUE;
17573 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17574 		    MUTEX_DEFAULT, NULL);
17575 		rep_ipif->ipif_id = 0;
17576 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17577 		rep_ipif->ipif_ill = from_ill;
17578 		rep_ipif->ipif_orig_ifindex =
17579 		    from_ill->ill_phyint->phyint_ifindex;
17580 		/* Insert at head */
17581 		rep_ipif->ipif_next = from_ill->ill_ipif;
17582 		from_ill->ill_ipif = rep_ipif;
17583 		/*
17584 		 * We don't really care to let apps know about
17585 		 * this interface.
17586 		 */
17587 	}
17588 
17589 	if (remove_ipif) {
17590 		/*
17591 		 * We set to a max value above for this case to get
17592 		 * id zero. ASSERT that we did get one.
17593 		 */
17594 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17595 		rep_ipif = to_ipif;
17596 		to_ill->ill_ipif = rep_ipif->ipif_next;
17597 		rep_ipif->ipif_next = NULL;
17598 		/*
17599 		 * If some apps scanned and find this interface,
17600 		 * it is time to let them know, so that they can
17601 		 * delete it.
17602 		 */
17603 
17604 		*rep_ipif_ptr = rep_ipif;
17605 	}
17606 
17607 	/* Get it out of the ILL interface list. */
17608 	ipif_remove(ipif, B_FALSE);
17609 
17610 	/* Assign the new ill */
17611 	ipif->ipif_ill = to_ill;
17612 	ipif->ipif_id = unit;
17613 	/* id has already been checked */
17614 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17615 	ASSERT(rc == 0);
17616 	/* Let SCTP update its list */
17617 	sctp_move_ipif(ipif, from_ill, to_ill);
17618 	/*
17619 	 * Handle the failover and failback of ipif_t between
17620 	 * ill_t that have differing maximum mtu values.
17621 	 */
17622 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17623 		if (ipif->ipif_saved_mtu == 0) {
17624 			/*
17625 			 * As this ipif_t is moving to an ill_t
17626 			 * that has a lower ill_max_mtu, its
17627 			 * ipif_mtu needs to be saved so it can
17628 			 * be restored during failback or during
17629 			 * failover to an ill_t which has a
17630 			 * higher ill_max_mtu.
17631 			 */
17632 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17633 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17634 		} else {
17635 			/*
17636 			 * The ipif_t is, once again, moving to
17637 			 * an ill_t that has a lower maximum mtu
17638 			 * value.
17639 			 */
17640 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17641 		}
17642 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17643 	    ipif->ipif_saved_mtu != 0) {
17644 		/*
17645 		 * The mtu of this ipif_t had to be reduced
17646 		 * during an earlier failover; this is an
17647 		 * opportunity for it to be increased (either as
17648 		 * part of another failover or a failback).
17649 		 */
17650 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17651 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17652 			ipif->ipif_saved_mtu = 0;
17653 		} else {
17654 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17655 		}
17656 	}
17657 
17658 	/*
17659 	 * We preserve all the other fields of the ipif including
17660 	 * ipif_saved_ire_mp. The routes that are saved here will
17661 	 * be recreated on the new interface and back on the old
17662 	 * interface when we move back.
17663 	 */
17664 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17665 
17666 	return (err);
17667 }
17668 
17669 static int
17670 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17671     int ifindex, ipif_t **rep_ipif_ptr)
17672 {
17673 	ipif_t *mipif;
17674 	ipif_t *ipif_next;
17675 	int err;
17676 
17677 	/*
17678 	 * We don't really try to MOVE back things if some of the
17679 	 * operations fail. The daemon will take care of moving again
17680 	 * later on.
17681 	 */
17682 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17683 		ipif_next = mipif->ipif_next;
17684 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17685 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17686 
17687 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17688 
17689 			/*
17690 			 * When the MOVE fails, it is the job of the
17691 			 * application to take care of this properly
17692 			 * i.e try again if it is ENOMEM.
17693 			 */
17694 			if (mipif->ipif_ill != from_ill) {
17695 				/*
17696 				 * ipif has moved.
17697 				 *
17698 				 * Move the multicast memberships associated
17699 				 * with this ipif to the new ill. For IPv6, we
17700 				 * do it once after all the ipifs are moved
17701 				 * (in ill_move) as they are not associated
17702 				 * with ipifs.
17703 				 *
17704 				 * We need to move the ilms as the ipif has
17705 				 * already been moved to a new ill even
17706 				 * in the case of errors. Neither
17707 				 * ilm_free(ipif) will find the ilm
17708 				 * when somebody unplumbs this ipif nor
17709 				 * ilm_delete(ilm) will be able to find the
17710 				 * ilm, if we don't move now.
17711 				 */
17712 				if (!from_ill->ill_isv6)
17713 					ilm_move_v4(from_ill, to_ill, mipif);
17714 			}
17715 
17716 			if (err != 0)
17717 				return (err);
17718 		}
17719 	}
17720 	return (0);
17721 }
17722 
17723 static int
17724 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17725 {
17726 	int ifindex;
17727 	int err;
17728 	struct iocblk	*iocp;
17729 	ipif_t	*ipif;
17730 	ipif_t *rep_ipif_ptr = NULL;
17731 	ipif_t	*from_ipif = NULL;
17732 	boolean_t check_rep_if = B_FALSE;
17733 	ip_stack_t	*ipst = from_ill->ill_ipst;
17734 
17735 	iocp = (struct iocblk *)mp->b_rptr;
17736 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17737 		/*
17738 		 * Move everything pointing at from_ill to to_ill.
17739 		 * We acheive this by passing in 0 as ifindex.
17740 		 */
17741 		ifindex = 0;
17742 	} else {
17743 		/*
17744 		 * Move everything pointing at from_ill whose original
17745 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17746 		 * We acheive this by passing in ifindex rather than 0.
17747 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17748 		 */
17749 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17750 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17751 	}
17752 
17753 	/*
17754 	 * Determine if there is at least one ipif that would move from
17755 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17756 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17757 	 * the move, in which case we need to quiesce the replacement ipif also.
17758 	 */
17759 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17760 	    from_ipif = from_ipif->ipif_next) {
17761 		if (((ifindex == 0) ||
17762 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17763 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17764 			check_rep_if = B_TRUE;
17765 			break;
17766 		}
17767 	}
17768 
17769 
17770 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17771 
17772 	GRAB_ILL_LOCKS(from_ill, to_ill);
17773 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17774 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17775 		    mp, ILL_MOVE_OK);
17776 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17777 		return (EINPROGRESS);
17778 	}
17779 
17780 	/* Check if the replacement ipif is quiescent to delete */
17781 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17782 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17783 		to_ill->ill_ipif->ipif_state_flags |=
17784 		    IPIF_MOVING | IPIF_CHANGING;
17785 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17786 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17787 			    mp, ILL_MOVE_OK);
17788 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17789 			return (EINPROGRESS);
17790 		}
17791 	}
17792 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17793 
17794 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17795 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17796 	GRAB_ILL_LOCKS(from_ill, to_ill);
17797 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17798 
17799 	/* ilm_move is done inside ipif_move for IPv4 */
17800 	if (err == 0 && from_ill->ill_isv6)
17801 		ilm_move_v6(from_ill, to_ill, ifindex);
17802 
17803 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17804 	rw_exit(&ipst->ips_ill_g_lock);
17805 
17806 	/*
17807 	 * send rts messages and multicast messages.
17808 	 */
17809 	if (rep_ipif_ptr != NULL) {
17810 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17811 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17812 			rep_ipif_ptr->ipif_recovery_id = 0;
17813 		}
17814 		ip_rts_ifmsg(rep_ipif_ptr);
17815 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17816 #ifdef DEBUG
17817 		ipif_trace_cleanup(rep_ipif_ptr);
17818 #endif
17819 		mi_free(rep_ipif_ptr);
17820 	}
17821 
17822 	conn_move_ill(from_ill, to_ill, ifindex);
17823 
17824 	return (err);
17825 }
17826 
17827 /*
17828  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17829  * Also checks for the validity of the arguments.
17830  * Note: We are already exclusive inside the from group.
17831  * It is upto the caller to release refcnt on the to_ill's.
17832  */
17833 static int
17834 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17835     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17836 {
17837 	int dst_index;
17838 	ipif_t *ipif_v4, *ipif_v6;
17839 	struct lifreq *lifr;
17840 	mblk_t *mp1;
17841 	boolean_t exists;
17842 	sin_t	*sin;
17843 	int	err = 0;
17844 	ip_stack_t	*ipst;
17845 
17846 	if (CONN_Q(q))
17847 		ipst = CONNQ_TO_IPST(q);
17848 	else
17849 		ipst = ILLQ_TO_IPST(q);
17850 
17851 
17852 	if ((mp1 = mp->b_cont) == NULL)
17853 		return (EPROTO);
17854 
17855 	if ((mp1 = mp1->b_cont) == NULL)
17856 		return (EPROTO);
17857 
17858 	lifr = (struct lifreq *)mp1->b_rptr;
17859 	sin = (sin_t *)&lifr->lifr_addr;
17860 
17861 	/*
17862 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17863 	 * specific operations.
17864 	 */
17865 	if (sin->sin_family != AF_UNSPEC)
17866 		return (EINVAL);
17867 
17868 	/*
17869 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17870 	 * NULLs for the last 4 args and we know the lookup won't fail
17871 	 * with EINPROGRESS.
17872 	 */
17873 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17874 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17875 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17876 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17877 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17878 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17879 
17880 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17881 		return (ENXIO);
17882 
17883 	if (ipif_v4 != NULL) {
17884 		ASSERT(ipif_v4->ipif_refcnt != 0);
17885 		if (ipif_v4->ipif_id != 0) {
17886 			err = EINVAL;
17887 			goto done;
17888 		}
17889 
17890 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17891 		*ill_from_v4 = ipif_v4->ipif_ill;
17892 	}
17893 
17894 	if (ipif_v6 != NULL) {
17895 		ASSERT(ipif_v6->ipif_refcnt != 0);
17896 		if (ipif_v6->ipif_id != 0) {
17897 			err = EINVAL;
17898 			goto done;
17899 		}
17900 
17901 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17902 		*ill_from_v6 = ipif_v6->ipif_ill;
17903 	}
17904 
17905 	err = 0;
17906 	dst_index = lifr->lifr_movetoindex;
17907 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17908 	    q, mp, ip_process_ioctl, &err, ipst);
17909 	if (err != 0) {
17910 		/*
17911 		 * There could be only v6.
17912 		 */
17913 		if (err != ENXIO)
17914 			goto done;
17915 		err = 0;
17916 	}
17917 
17918 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17919 	    q, mp, ip_process_ioctl, &err, ipst);
17920 	if (err != 0) {
17921 		if (err != ENXIO)
17922 			goto done;
17923 		if (*ill_to_v4 == NULL) {
17924 			err = ENXIO;
17925 			goto done;
17926 		}
17927 		err = 0;
17928 	}
17929 
17930 	/*
17931 	 * If we have something to MOVE i.e "from" not NULL,
17932 	 * "to" should be non-NULL.
17933 	 */
17934 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17935 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17936 		err = EINVAL;
17937 	}
17938 
17939 done:
17940 	if (ipif_v4 != NULL)
17941 		ipif_refrele(ipif_v4);
17942 	if (ipif_v6 != NULL)
17943 		ipif_refrele(ipif_v6);
17944 	return (err);
17945 }
17946 
17947 /*
17948  * FAILOVER and FAILBACK are modelled as MOVE operations.
17949  *
17950  * We don't check whether the MOVE is within the same group or
17951  * not, because this ioctl can be used as a generic mechanism
17952  * to failover from interface A to B, though things will function
17953  * only if they are really part of the same group. Moreover,
17954  * all ipifs may be down and hence temporarily out of the group.
17955  *
17956  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17957  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17958  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17959  * have been deleted and there are no active references. Once quiescent the
17960  * ipif's are moved and brought up on the new ill.
17961  *
17962  * Normally the source ill and destination ill belong to the same IPMP group
17963  * and hence the same ipsq_t. In the event they don't belong to the same
17964  * same group the two ipsq's are first merged into one ipsq - that of the
17965  * to_ill. The multicast memberships on the source and destination ill cannot
17966  * change during the move operation since multicast joins/leaves also have to
17967  * execute on the same ipsq and are hence serialized.
17968  */
17969 /* ARGSUSED */
17970 int
17971 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17972     ip_ioctl_cmd_t *ipip, void *ifreq)
17973 {
17974 	ill_t *ill_to_v4 = NULL;
17975 	ill_t *ill_to_v6 = NULL;
17976 	ill_t *ill_from_v4 = NULL;
17977 	ill_t *ill_from_v6 = NULL;
17978 	int err = 0;
17979 
17980 	/*
17981 	 * setup from and to ill's, we can get EINPROGRESS only for
17982 	 * to_ill's.
17983 	 */
17984 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17985 	    &ill_to_v4, &ill_to_v6);
17986 
17987 	if (err != 0) {
17988 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17989 		goto done;
17990 	}
17991 
17992 	/*
17993 	 * nothing to do.
17994 	 */
17995 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17996 		goto done;
17997 	}
17998 
17999 	/*
18000 	 * nothing to do.
18001 	 */
18002 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18003 		goto done;
18004 	}
18005 
18006 	/*
18007 	 * Mark the ill as changing.
18008 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18009 	 * in ill_up_ipifs in case of error they are cleared below.
18010 	 */
18011 
18012 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18013 	if (ill_from_v4 != NULL)
18014 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18015 	if (ill_from_v6 != NULL)
18016 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18017 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18018 
18019 	/*
18020 	 * Make sure that both src and dst are
18021 	 * in the same syncq group. If not make it happen.
18022 	 * We are not holding any locks because we are the writer
18023 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18024 	 * to protect to_ipsq against changing.
18025 	 */
18026 	if (ill_from_v4 != NULL) {
18027 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18028 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18029 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18030 			    NULL, mp, q);
18031 			goto err_ret;
18032 
18033 		}
18034 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18035 	} else {
18036 
18037 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18038 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18039 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18040 			    NULL, mp, q);
18041 			goto err_ret;
18042 
18043 		}
18044 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18045 	}
18046 
18047 	/*
18048 	 * Now that the ipsq's have been merged and we are the writer
18049 	 * lets mark to_ill as changing as well.
18050 	 */
18051 
18052 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18053 	if (ill_to_v4 != NULL)
18054 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18055 	if (ill_to_v6 != NULL)
18056 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18057 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18058 
18059 	/*
18060 	 * Its ok for us to proceed with the move even if
18061 	 * ill_pending_mp is non null on one of the from ill's as the reply
18062 	 * should not be looking at the ipif, it should only care about the
18063 	 * ill itself.
18064 	 */
18065 
18066 	/*
18067 	 * lets move ipv4 first.
18068 	 */
18069 	if (ill_from_v4 != NULL) {
18070 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18071 		ill_from_v4->ill_move_in_progress = B_TRUE;
18072 		ill_to_v4->ill_move_in_progress = B_TRUE;
18073 		ill_to_v4->ill_move_peer = ill_from_v4;
18074 		ill_from_v4->ill_move_peer = ill_to_v4;
18075 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18076 	}
18077 
18078 	/*
18079 	 * Now lets move ipv6.
18080 	 */
18081 	if (err == 0 && ill_from_v6 != NULL) {
18082 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18083 		ill_from_v6->ill_move_in_progress = B_TRUE;
18084 		ill_to_v6->ill_move_in_progress = B_TRUE;
18085 		ill_to_v6->ill_move_peer = ill_from_v6;
18086 		ill_from_v6->ill_move_peer = ill_to_v6;
18087 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18088 	}
18089 
18090 err_ret:
18091 	/*
18092 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18093 	 * moved to become quiescent.
18094 	 */
18095 	if (err == EINPROGRESS) {
18096 		goto done;
18097 	}
18098 
18099 	/*
18100 	 * if err is set ill_up_ipifs will not be called
18101 	 * lets clear the flags.
18102 	 */
18103 
18104 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18105 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18106 	/*
18107 	 * Some of the clearing may be redundant. But it is simple
18108 	 * not making any extra checks.
18109 	 */
18110 	if (ill_from_v6 != NULL) {
18111 		ill_from_v6->ill_move_in_progress = B_FALSE;
18112 		ill_from_v6->ill_move_peer = NULL;
18113 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18114 	}
18115 	if (ill_from_v4 != NULL) {
18116 		ill_from_v4->ill_move_in_progress = B_FALSE;
18117 		ill_from_v4->ill_move_peer = NULL;
18118 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18119 	}
18120 	if (ill_to_v6 != NULL) {
18121 		ill_to_v6->ill_move_in_progress = B_FALSE;
18122 		ill_to_v6->ill_move_peer = NULL;
18123 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18124 	}
18125 	if (ill_to_v4 != NULL) {
18126 		ill_to_v4->ill_move_in_progress = B_FALSE;
18127 		ill_to_v4->ill_move_peer = NULL;
18128 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18129 	}
18130 
18131 	/*
18132 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18133 	 * Do this always to maintain proper state i.e even in case of errors.
18134 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18135 	 * we need not call on both v4 and v6 interfaces.
18136 	 */
18137 	if (ill_from_v4 != NULL) {
18138 		if ((ill_from_v4->ill_phyint->phyint_flags &
18139 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18140 			phyint_inactive(ill_from_v4->ill_phyint);
18141 		}
18142 	} else if (ill_from_v6 != NULL) {
18143 		if ((ill_from_v6->ill_phyint->phyint_flags &
18144 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18145 			phyint_inactive(ill_from_v6->ill_phyint);
18146 		}
18147 	}
18148 
18149 	if (ill_to_v4 != NULL) {
18150 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18151 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18152 		}
18153 	} else if (ill_to_v6 != NULL) {
18154 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18155 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18156 		}
18157 	}
18158 
18159 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18160 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18161 
18162 no_err:
18163 	/*
18164 	 * lets bring the interfaces up on the to_ill.
18165 	 */
18166 	if (err == 0) {
18167 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18168 		    q, mp);
18169 	}
18170 
18171 	if (err == 0) {
18172 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18173 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18174 
18175 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18176 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18177 	}
18178 done:
18179 
18180 	if (ill_to_v4 != NULL) {
18181 		ill_refrele(ill_to_v4);
18182 	}
18183 	if (ill_to_v6 != NULL) {
18184 		ill_refrele(ill_to_v6);
18185 	}
18186 
18187 	return (err);
18188 }
18189 
18190 static void
18191 ill_dl_down(ill_t *ill)
18192 {
18193 	/*
18194 	 * The ill is down; unbind but stay attached since we're still
18195 	 * associated with a PPA. If we have negotiated DLPI capabilites
18196 	 * with the data link service provider (IDS_OK) then reset them.
18197 	 * The interval between unbinding and rebinding is potentially
18198 	 * unbounded hence we cannot assume things will be the same.
18199 	 * The DLPI capabilities will be probed again when the data link
18200 	 * is brought up.
18201 	 */
18202 	mblk_t	*mp = ill->ill_unbind_mp;
18203 
18204 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18205 
18206 	ill->ill_unbind_mp = NULL;
18207 	if (mp != NULL) {
18208 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18209 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18210 		    ill->ill_name));
18211 		mutex_enter(&ill->ill_lock);
18212 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18213 		mutex_exit(&ill->ill_lock);
18214 		/*
18215 		 * Reset the capabilities if the negotiation is done or is
18216 		 * still in progress. Note that ill_capability_reset() will
18217 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18218 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18219 		 *
18220 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18221 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18222 		 * the capabilities renegotiation from happening.
18223 		 */
18224 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18225 			ill_capability_reset(ill);
18226 		ill->ill_capab_reneg = B_FALSE;
18227 
18228 		ill_dlpi_send(ill, mp);
18229 	}
18230 
18231 	/*
18232 	 * Toss all of our multicast memberships.  We could keep them, but
18233 	 * then we'd have to do bookkeeping of any joins and leaves performed
18234 	 * by the application while the the interface is down (we can't just
18235 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18236 	 * on a downed interface).
18237 	 */
18238 	ill_leave_multicast(ill);
18239 
18240 	mutex_enter(&ill->ill_lock);
18241 	ill->ill_dl_up = 0;
18242 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18243 	mutex_exit(&ill->ill_lock);
18244 }
18245 
18246 static void
18247 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18248 {
18249 	union DL_primitives *dlp;
18250 	t_uscalar_t prim;
18251 
18252 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18253 
18254 	dlp = (union DL_primitives *)mp->b_rptr;
18255 	prim = dlp->dl_primitive;
18256 
18257 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18258 	    dl_primstr(prim), prim, ill->ill_name));
18259 
18260 	switch (prim) {
18261 	case DL_PHYS_ADDR_REQ:
18262 	{
18263 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18264 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18265 		break;
18266 	}
18267 	case DL_BIND_REQ:
18268 		mutex_enter(&ill->ill_lock);
18269 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18270 		mutex_exit(&ill->ill_lock);
18271 		break;
18272 	}
18273 
18274 	/*
18275 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18276 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18277 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18278 	 */
18279 	mutex_enter(&ill->ill_lock);
18280 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18281 	    (prim == DL_UNBIND_REQ)) {
18282 		ill->ill_dlpi_pending = prim;
18283 	}
18284 	mutex_exit(&ill->ill_lock);
18285 
18286 	putnext(ill->ill_wq, mp);
18287 }
18288 
18289 /*
18290  * Helper function for ill_dlpi_send().
18291  */
18292 /* ARGSUSED */
18293 static void
18294 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18295 {
18296 	ill_dlpi_send(q->q_ptr, mp);
18297 }
18298 
18299 /*
18300  * Send a DLPI control message to the driver but make sure there
18301  * is only one outstanding message. Uses ill_dlpi_pending to tell
18302  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18303  * when an ACK or a NAK is received to process the next queued message.
18304  */
18305 void
18306 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18307 {
18308 	mblk_t **mpp;
18309 
18310 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18311 
18312 	/*
18313 	 * To ensure that any DLPI requests for current exclusive operation
18314 	 * are always completely sent before any DLPI messages for other
18315 	 * operations, require writer access before enqueuing.
18316 	 */
18317 	if (!IAM_WRITER_ILL(ill)) {
18318 		ill_refhold(ill);
18319 		/* qwriter_ip() does the ill_refrele() */
18320 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18321 		    NEW_OP, B_TRUE);
18322 		return;
18323 	}
18324 
18325 	mutex_enter(&ill->ill_lock);
18326 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18327 		/* Must queue message. Tail insertion */
18328 		mpp = &ill->ill_dlpi_deferred;
18329 		while (*mpp != NULL)
18330 			mpp = &((*mpp)->b_next);
18331 
18332 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18333 		    ill->ill_name));
18334 
18335 		*mpp = mp;
18336 		mutex_exit(&ill->ill_lock);
18337 		return;
18338 	}
18339 	mutex_exit(&ill->ill_lock);
18340 	ill_dlpi_dispatch(ill, mp);
18341 }
18342 
18343 /*
18344  * Send all deferred DLPI messages without waiting for their ACKs.
18345  */
18346 void
18347 ill_dlpi_send_deferred(ill_t *ill)
18348 {
18349 	mblk_t *mp, *nextmp;
18350 
18351 	/*
18352 	 * Clear ill_dlpi_pending so that the message is not queued in
18353 	 * ill_dlpi_send().
18354 	 */
18355 	mutex_enter(&ill->ill_lock);
18356 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18357 	mp = ill->ill_dlpi_deferred;
18358 	ill->ill_dlpi_deferred = NULL;
18359 	mutex_exit(&ill->ill_lock);
18360 
18361 	for (; mp != NULL; mp = nextmp) {
18362 		nextmp = mp->b_next;
18363 		mp->b_next = NULL;
18364 		ill_dlpi_send(ill, mp);
18365 	}
18366 }
18367 
18368 /*
18369  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18370  */
18371 boolean_t
18372 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18373 {
18374 	t_uscalar_t pending;
18375 
18376 	mutex_enter(&ill->ill_lock);
18377 	if (ill->ill_dlpi_pending == prim) {
18378 		mutex_exit(&ill->ill_lock);
18379 		return (B_TRUE);
18380 	}
18381 
18382 	/*
18383 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18384 	 * without waiting, so don't print any warnings in that case.
18385 	 */
18386 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18387 		mutex_exit(&ill->ill_lock);
18388 		return (B_FALSE);
18389 	}
18390 	pending = ill->ill_dlpi_pending;
18391 	mutex_exit(&ill->ill_lock);
18392 
18393 	if (pending == DL_PRIM_INVAL) {
18394 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18395 		    "received unsolicited ack for %s on %s\n",
18396 		    dl_primstr(prim), ill->ill_name);
18397 	} else {
18398 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18399 		    "received unexpected ack for %s on %s (expecting %s)\n",
18400 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18401 	}
18402 	return (B_FALSE);
18403 }
18404 
18405 /*
18406  * Complete the current DLPI operation associated with `prim' on `ill' and
18407  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18408  * operations and the ill's current exclusive IPSQ operation has finished
18409  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18410  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18411  * the comments above ipsq_current_finish() for details.
18412  */
18413 void
18414 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18415 {
18416 	mblk_t *mp;
18417 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18418 
18419 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18420 	mutex_enter(&ill->ill_lock);
18421 
18422 	ASSERT(prim != DL_PRIM_INVAL);
18423 	ASSERT(ill->ill_dlpi_pending == prim);
18424 
18425 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18426 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18427 
18428 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18429 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18430 
18431 		mutex_enter(&ipsq->ipsq_lock);
18432 		if (ipsq->ipsq_current_done)
18433 			ipsq->ipsq_current_ipif = NULL;
18434 		mutex_exit(&ipsq->ipsq_lock);
18435 
18436 		cv_signal(&ill->ill_cv);
18437 		mutex_exit(&ill->ill_lock);
18438 		return;
18439 	}
18440 
18441 	ill->ill_dlpi_deferred = mp->b_next;
18442 	mp->b_next = NULL;
18443 	mutex_exit(&ill->ill_lock);
18444 
18445 	ill_dlpi_dispatch(ill, mp);
18446 }
18447 
18448 void
18449 conn_delete_ire(conn_t *connp, caddr_t arg)
18450 {
18451 	ipif_t	*ipif = (ipif_t *)arg;
18452 	ire_t	*ire;
18453 
18454 	/*
18455 	 * Look at the cached ires on conns which has pointers to ipifs.
18456 	 * We just call ire_refrele which clears up the reference
18457 	 * to ire. Called when a conn closes. Also called from ipif_free
18458 	 * to cleanup indirect references to the stale ipif via the cached ire.
18459 	 */
18460 	mutex_enter(&connp->conn_lock);
18461 	ire = connp->conn_ire_cache;
18462 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18463 		connp->conn_ire_cache = NULL;
18464 		mutex_exit(&connp->conn_lock);
18465 		IRE_REFRELE_NOTR(ire);
18466 		return;
18467 	}
18468 	mutex_exit(&connp->conn_lock);
18469 
18470 }
18471 
18472 /*
18473  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18474  * of IREs. Those IREs may have been previously cached in the conn structure.
18475  * This ipcl_walk() walker function releases all references to such IREs based
18476  * on the condemned flag.
18477  */
18478 /* ARGSUSED */
18479 void
18480 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18481 {
18482 	ire_t	*ire;
18483 
18484 	mutex_enter(&connp->conn_lock);
18485 	ire = connp->conn_ire_cache;
18486 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18487 		connp->conn_ire_cache = NULL;
18488 		mutex_exit(&connp->conn_lock);
18489 		IRE_REFRELE_NOTR(ire);
18490 		return;
18491 	}
18492 	mutex_exit(&connp->conn_lock);
18493 }
18494 
18495 /*
18496  * Take down a specific interface, but don't lose any information about it.
18497  * Also delete interface from its interface group (ifgrp).
18498  * (Always called as writer.)
18499  * This function goes through the down sequence even if the interface is
18500  * already down. There are 2 reasons.
18501  * a. Currently we permit interface routes that depend on down interfaces
18502  *    to be added. This behaviour itself is questionable. However it appears
18503  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18504  *    time. We go thru the cleanup in order to remove these routes.
18505  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18506  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18507  *    down, but we need to cleanup i.e. do ill_dl_down and
18508  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18509  *
18510  * IP-MT notes:
18511  *
18512  * Model of reference to interfaces.
18513  *
18514  * The following members in ipif_t track references to the ipif.
18515  *	int     ipif_refcnt;    Active reference count
18516  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18517  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18518  *
18519  * The following members in ill_t track references to the ill.
18520  *	int             ill_refcnt;     active refcnt
18521  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18522  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18523  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18524  *
18525  * Reference to an ipif or ill can be obtained in any of the following ways.
18526  *
18527  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18528  * Pointers to ipif / ill from other data structures viz ire and conn.
18529  * Implicit reference to the ipif / ill by holding a reference to the ire.
18530  *
18531  * The ipif/ill lookup functions return a reference held ipif / ill.
18532  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18533  * This is a purely dynamic reference count associated with threads holding
18534  * references to the ipif / ill. Pointers from other structures do not
18535  * count towards this reference count.
18536  *
18537  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18538  * associated with the ipif/ill. This is incremented whenever a new
18539  * ire is created referencing the ipif/ill. This is done atomically inside
18540  * ire_add_v[46] where the ire is actually added to the ire hash table.
18541  * The count is decremented in ire_inactive where the ire is destroyed.
18542  *
18543  * nce's reference ill's thru nce_ill and the count of nce's associated with
18544  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18545  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18546  * table. Similarly it is decremented in ndp_inactive() where the nce
18547  * is destroyed.
18548  *
18549  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18550  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18551  * in ilm_walker_cleanup() or ilm_delete().
18552  *
18553  * Flow of ioctls involving interface down/up
18554  *
18555  * The following is the sequence of an attempt to set some critical flags on an
18556  * up interface.
18557  * ip_sioctl_flags
18558  * ipif_down
18559  * wait for ipif to be quiescent
18560  * ipif_down_tail
18561  * ip_sioctl_flags_tail
18562  *
18563  * All set ioctls that involve down/up sequence would have a skeleton similar
18564  * to the above. All the *tail functions are called after the refcounts have
18565  * dropped to the appropriate values.
18566  *
18567  * The mechanism to quiesce an ipif is as follows.
18568  *
18569  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18570  * on the ipif. Callers either pass a flag requesting wait or the lookup
18571  *  functions will return NULL.
18572  *
18573  * Delete all ires referencing this ipif
18574  *
18575  * Any thread attempting to do an ipif_refhold on an ipif that has been
18576  * obtained thru a cached pointer will first make sure that
18577  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18578  * increment the refcount.
18579  *
18580  * The above guarantees that the ipif refcount will eventually come down to
18581  * zero and the ipif will quiesce, once all threads that currently hold a
18582  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18583  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18584  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18585  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18586  * in ip.h
18587  *
18588  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18589  *
18590  * Threads trying to lookup an ipif or ill can pass a flag requesting
18591  * wait and restart if the ipif / ill cannot be looked up currently.
18592  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18593  * failure if the ipif is currently undergoing an exclusive operation, and
18594  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18595  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18596  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18597  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18598  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18599  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18600  * until we release the ipsq_lock, even though the the ill/ipif state flags
18601  * can change after we drop the ill_lock.
18602  *
18603  * An attempt to send out a packet using an ipif that is currently
18604  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18605  * operation and restart it later when the exclusive condition on the ipif ends.
18606  * This is an example of not passing the wait flag to the lookup functions. For
18607  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18608  * out a multicast packet on that ipif will fail while the ipif is
18609  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18610  * currently IPIF_CHANGING will also fail.
18611  */
18612 int
18613 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18614 {
18615 	ill_t		*ill = ipif->ipif_ill;
18616 	phyint_t	*phyi;
18617 	conn_t		*connp;
18618 	boolean_t	success;
18619 	boolean_t	ipif_was_up = B_FALSE;
18620 	ip_stack_t	*ipst = ill->ill_ipst;
18621 
18622 	ASSERT(IAM_WRITER_IPIF(ipif));
18623 
18624 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18625 
18626 	if (ipif->ipif_flags & IPIF_UP) {
18627 		mutex_enter(&ill->ill_lock);
18628 		ipif->ipif_flags &= ~IPIF_UP;
18629 		ASSERT(ill->ill_ipif_up_count > 0);
18630 		--ill->ill_ipif_up_count;
18631 		mutex_exit(&ill->ill_lock);
18632 		ipif_was_up = B_TRUE;
18633 		/* Update status in SCTP's list */
18634 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18635 	}
18636 
18637 	/*
18638 	 * Blow away memberships we established in ipif_multicast_up().
18639 	 */
18640 	ipif_multicast_down(ipif);
18641 
18642 	/*
18643 	 * Remove from the mapping for __sin6_src_id. We insert only
18644 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18645 	 * stored as mapped addresses, we need to check for mapped
18646 	 * INADDR_ANY also.
18647 	 */
18648 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18649 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18650 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18651 		int err;
18652 
18653 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18654 		    ipif->ipif_zoneid, ipst);
18655 		if (err != 0) {
18656 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18657 		}
18658 	}
18659 
18660 	/*
18661 	 * Before we delete the ill from the group (if any), we need
18662 	 * to make sure that we delete all the routes dependent on
18663 	 * this and also any ipifs dependent on this ipif for
18664 	 * source address. We need to do before we delete from
18665 	 * the group because
18666 	 *
18667 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18668 	 *
18669 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18670 	 *    for re-doing source address selection. Note that
18671 	 *    ipif_select_source[_v6] called from
18672 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18673 	 *    because we have already marked down here i.e cleared
18674 	 *    IPIF_UP.
18675 	 */
18676 	if (ipif->ipif_isv6) {
18677 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18678 		    ipst);
18679 	} else {
18680 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18681 		    ipst);
18682 	}
18683 
18684 	/*
18685 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18686 	 * ires have been deleted above. Otherwise a thread could end up
18687 	 * caching an ire in a conn after we have finished the cleanup of the
18688 	 * conn. The caching is done after making sure that the ire is not yet
18689 	 * condemned. Also documented in the block comment above ip_output
18690 	 */
18691 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18692 	/* Also, delete the ires cached in SCTP */
18693 	sctp_ire_cache_flush(ipif);
18694 
18695 	/*
18696 	 * Update any other ipifs which have used "our" local address as
18697 	 * a source address. This entails removing and recreating IRE_INTERFACE
18698 	 * entries for such ipifs.
18699 	 */
18700 	if (ipif->ipif_isv6)
18701 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18702 	else
18703 		ipif_update_other_ipifs(ipif, ill->ill_group);
18704 
18705 	if (ipif_was_up) {
18706 		/*
18707 		 * Check whether it is last ipif to leave this group.
18708 		 * If this is the last ipif to leave, we should remove
18709 		 * this ill from the group as ipif_select_source will not
18710 		 * be able to find any useful ipifs if this ill is selected
18711 		 * for load balancing.
18712 		 *
18713 		 * For nameless groups, we should call ifgrp_delete if this
18714 		 * belongs to some group. As this ipif is going down, we may
18715 		 * need to reconstruct groups.
18716 		 */
18717 		phyi = ill->ill_phyint;
18718 		/*
18719 		 * If the phyint_groupname_len is 0, it may or may not
18720 		 * be in the nameless group. If the phyint_groupname_len is
18721 		 * not 0, then this ill should be part of some group.
18722 		 * As we always insert this ill in the group if
18723 		 * phyint_groupname_len is not zero when the first ipif
18724 		 * comes up (in ipif_up_done), it should be in a group
18725 		 * when the namelen is not 0.
18726 		 *
18727 		 * NOTE : When we delete the ill from the group,it will
18728 		 * blow away all the IRE_CACHES pointing either at this ipif or
18729 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18730 		 * should be pointing at this ill.
18731 		 */
18732 		ASSERT(phyi->phyint_groupname_len == 0 ||
18733 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18734 
18735 		if (phyi->phyint_groupname_len != 0) {
18736 			if (ill->ill_ipif_up_count == 0)
18737 				illgrp_delete(ill);
18738 		}
18739 
18740 		/*
18741 		 * If we have deleted some of the broadcast ires associated
18742 		 * with this ipif, we need to re-nominate somebody else if
18743 		 * the ires that we deleted were the nominated ones.
18744 		 */
18745 		if (ill->ill_group != NULL && !ill->ill_isv6)
18746 			ipif_renominate_bcast(ipif);
18747 	}
18748 
18749 	/*
18750 	 * neighbor-discovery or arp entries for this interface.
18751 	 */
18752 	ipif_ndp_down(ipif);
18753 
18754 	/*
18755 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18756 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18757 	 * and ill_delete -> ipif_free -> ipif_down
18758 	 */
18759 	if (mp == NULL) {
18760 		ASSERT(q == NULL);
18761 		return (0);
18762 	}
18763 
18764 	if (CONN_Q(q)) {
18765 		connp = Q_TO_CONN(q);
18766 		mutex_enter(&connp->conn_lock);
18767 	} else {
18768 		connp = NULL;
18769 	}
18770 	mutex_enter(&ill->ill_lock);
18771 	/*
18772 	 * Are there any ire's pointing to this ipif that are still active ?
18773 	 * If this is the last ipif going down, are there any ire's pointing
18774 	 * to this ill that are still active ?
18775 	 */
18776 	if (ipif_is_quiescent(ipif)) {
18777 		mutex_exit(&ill->ill_lock);
18778 		if (connp != NULL)
18779 			mutex_exit(&connp->conn_lock);
18780 		return (0);
18781 	}
18782 
18783 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18784 	    ill->ill_name, (void *)ill));
18785 	/*
18786 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18787 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18788 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18789 	 */
18790 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18791 	if (!success) {
18792 		/* The conn is closing. So just return */
18793 		ASSERT(connp != NULL);
18794 		mutex_exit(&ill->ill_lock);
18795 		mutex_exit(&connp->conn_lock);
18796 		return (EINTR);
18797 	}
18798 
18799 	mutex_exit(&ill->ill_lock);
18800 	if (connp != NULL)
18801 		mutex_exit(&connp->conn_lock);
18802 	return (EINPROGRESS);
18803 }
18804 
18805 void
18806 ipif_down_tail(ipif_t *ipif)
18807 {
18808 	ill_t	*ill = ipif->ipif_ill;
18809 
18810 	/*
18811 	 * Skip any loopback interface (null wq).
18812 	 * If this is the last logical interface on the ill
18813 	 * have ill_dl_down tell the driver we are gone (unbind)
18814 	 * Note that lun 0 can ipif_down even though
18815 	 * there are other logical units that are up.
18816 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18817 	 */
18818 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18819 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18820 	    ill->ill_dl_up) {
18821 		ill_dl_down(ill);
18822 	}
18823 	ill->ill_logical_down = 0;
18824 
18825 	/*
18826 	 * Have to be after removing the routes in ipif_down_delete_ire.
18827 	 */
18828 	if (ipif->ipif_isv6) {
18829 		if (ill->ill_flags & ILLF_XRESOLV)
18830 			ipif_arp_down(ipif);
18831 	} else {
18832 		ipif_arp_down(ipif);
18833 	}
18834 
18835 	ip_rts_ifmsg(ipif);
18836 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18837 }
18838 
18839 /*
18840  * Bring interface logically down without bringing the physical interface
18841  * down e.g. when the netmask is changed. This avoids long lasting link
18842  * negotiations between an ethernet interface and a certain switches.
18843  */
18844 static int
18845 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18846 {
18847 	/*
18848 	 * The ill_logical_down flag is a transient flag. It is set here
18849 	 * and is cleared once the down has completed in ipif_down_tail.
18850 	 * This flag does not indicate whether the ill stream is in the
18851 	 * DL_BOUND state with the driver. Instead this flag is used by
18852 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18853 	 * the driver. The state of the ill stream i.e. whether it is
18854 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18855 	 */
18856 	ipif->ipif_ill->ill_logical_down = 1;
18857 	return (ipif_down(ipif, q, mp));
18858 }
18859 
18860 /*
18861  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18862  * If the usesrc client ILL is already part of a usesrc group or not,
18863  * in either case a ire_stq with the matching usesrc client ILL will
18864  * locate the IRE's that need to be deleted. We want IREs to be created
18865  * with the new source address.
18866  */
18867 static void
18868 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18869 {
18870 	ill_t	*ucill = (ill_t *)ill_arg;
18871 
18872 	ASSERT(IAM_WRITER_ILL(ucill));
18873 
18874 	if (ire->ire_stq == NULL)
18875 		return;
18876 
18877 	if ((ire->ire_type == IRE_CACHE) &&
18878 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18879 		ire_delete(ire);
18880 }
18881 
18882 /*
18883  * ire_walk routine to delete every IRE dependent on the interface
18884  * address that is going down.	(Always called as writer.)
18885  * Works for both v4 and v6.
18886  * In addition for checking for ire_ipif matches it also checks for
18887  * IRE_CACHE entries which have the same source address as the
18888  * disappearing ipif since ipif_select_source might have picked
18889  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18890  * care of any IRE_INTERFACE with the disappearing source address.
18891  */
18892 static void
18893 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18894 {
18895 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18896 	ill_t *ire_ill;
18897 	ill_t *ipif_ill;
18898 
18899 	ASSERT(IAM_WRITER_IPIF(ipif));
18900 	if (ire->ire_ipif == NULL)
18901 		return;
18902 
18903 	/*
18904 	 * For IPv4, we derive source addresses for an IRE from ipif's
18905 	 * belonging to the same IPMP group as the IRE's outgoing
18906 	 * interface.  If an IRE's outgoing interface isn't in the
18907 	 * same IPMP group as a particular ipif, then that ipif
18908 	 * couldn't have been used as a source address for this IRE.
18909 	 *
18910 	 * For IPv6, source addresses are only restricted to the IPMP group
18911 	 * if the IRE is for a link-local address or a multicast address.
18912 	 * Otherwise, source addresses for an IRE can be chosen from
18913 	 * interfaces other than the the outgoing interface for that IRE.
18914 	 *
18915 	 * For source address selection details, see ipif_select_source()
18916 	 * and ipif_select_source_v6().
18917 	 */
18918 	if (ire->ire_ipversion == IPV4_VERSION ||
18919 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18920 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18921 		ire_ill = ire->ire_ipif->ipif_ill;
18922 		ipif_ill = ipif->ipif_ill;
18923 
18924 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18925 			return;
18926 		}
18927 	}
18928 
18929 
18930 	if (ire->ire_ipif != ipif) {
18931 		/*
18932 		 * Look for a matching source address.
18933 		 */
18934 		if (ire->ire_type != IRE_CACHE)
18935 			return;
18936 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18937 			return;
18938 
18939 		if (ire->ire_ipversion == IPV4_VERSION) {
18940 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18941 				return;
18942 		} else {
18943 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18944 			    &ipif->ipif_v6lcl_addr))
18945 				return;
18946 		}
18947 		ire_delete(ire);
18948 		return;
18949 	}
18950 	/*
18951 	 * ire_delete() will do an ire_flush_cache which will delete
18952 	 * all ire_ipif matches
18953 	 */
18954 	ire_delete(ire);
18955 }
18956 
18957 /*
18958  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18959  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18960  * 2) when an interface is brought up or down (on that ill).
18961  * This ensures that the IRE_CACHE entries don't retain stale source
18962  * address selection results.
18963  */
18964 void
18965 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18966 {
18967 	ill_t	*ill = (ill_t *)ill_arg;
18968 	ill_t	*ipif_ill;
18969 
18970 	ASSERT(IAM_WRITER_ILL(ill));
18971 	/*
18972 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18973 	 * Hence this should be IRE_CACHE.
18974 	 */
18975 	ASSERT(ire->ire_type == IRE_CACHE);
18976 
18977 	/*
18978 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18979 	 * We are only interested in IRE_CACHES that has borrowed
18980 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18981 	 * for which we need to look at ire_ipif->ipif_ill match
18982 	 * with ill.
18983 	 */
18984 	ASSERT(ire->ire_ipif != NULL);
18985 	ipif_ill = ire->ire_ipif->ipif_ill;
18986 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18987 	    ipif_ill->ill_group == ill->ill_group)) {
18988 		ire_delete(ire);
18989 	}
18990 }
18991 
18992 /*
18993  * Delete all the ire whose stq references ill_arg.
18994  */
18995 static void
18996 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18997 {
18998 	ill_t	*ill = (ill_t *)ill_arg;
18999 	ill_t	*ire_ill;
19000 
19001 	ASSERT(IAM_WRITER_ILL(ill));
19002 	/*
19003 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19004 	 * Hence this should be IRE_CACHE.
19005 	 */
19006 	ASSERT(ire->ire_type == IRE_CACHE);
19007 
19008 	/*
19009 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19010 	 * matches ill. We are only interested in IRE_CACHES that
19011 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19012 	 * filtering here.
19013 	 */
19014 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19015 
19016 	if (ire_ill == ill)
19017 		ire_delete(ire);
19018 }
19019 
19020 /*
19021  * This is called when an ill leaves the group. We want to delete
19022  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19023  * pointing at ill.
19024  */
19025 static void
19026 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19027 {
19028 	ill_t	*ill = (ill_t *)ill_arg;
19029 
19030 	ASSERT(IAM_WRITER_ILL(ill));
19031 	ASSERT(ill->ill_group == NULL);
19032 	/*
19033 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19034 	 * Hence this should be IRE_CACHE.
19035 	 */
19036 	ASSERT(ire->ire_type == IRE_CACHE);
19037 	/*
19038 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19039 	 * matches ill. We are interested in both.
19040 	 */
19041 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19042 	    (ire->ire_ipif->ipif_ill == ill));
19043 
19044 	ire_delete(ire);
19045 }
19046 
19047 /*
19048  * Initiate deallocate of an IPIF. Always called as writer. Called by
19049  * ill_delete or ip_sioctl_removeif.
19050  */
19051 static void
19052 ipif_free(ipif_t *ipif)
19053 {
19054 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19055 
19056 	ASSERT(IAM_WRITER_IPIF(ipif));
19057 
19058 	if (ipif->ipif_recovery_id != 0)
19059 		(void) untimeout(ipif->ipif_recovery_id);
19060 	ipif->ipif_recovery_id = 0;
19061 
19062 	/* Remove conn references */
19063 	reset_conn_ipif(ipif);
19064 
19065 	/*
19066 	 * Make sure we have valid net and subnet broadcast ire's for the
19067 	 * other ipif's which share them with this ipif.
19068 	 */
19069 	if (!ipif->ipif_isv6)
19070 		ipif_check_bcast_ires(ipif);
19071 
19072 	/*
19073 	 * Take down the interface. We can be called either from ill_delete
19074 	 * or from ip_sioctl_removeif.
19075 	 */
19076 	(void) ipif_down(ipif, NULL, NULL);
19077 
19078 	/*
19079 	 * Now that the interface is down, there's no chance it can still
19080 	 * become a duplicate.  Cancel any timer that may have been set while
19081 	 * tearing down.
19082 	 */
19083 	if (ipif->ipif_recovery_id != 0)
19084 		(void) untimeout(ipif->ipif_recovery_id);
19085 	ipif->ipif_recovery_id = 0;
19086 
19087 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19088 	/* Remove pointers to this ill in the multicast routing tables */
19089 	reset_mrt_vif_ipif(ipif);
19090 	rw_exit(&ipst->ips_ill_g_lock);
19091 }
19092 
19093 /*
19094  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19095  * also ill_move().
19096  */
19097 static void
19098 ipif_free_tail(ipif_t *ipif)
19099 {
19100 	mblk_t	*mp;
19101 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19102 
19103 	/*
19104 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19105 	 */
19106 	mutex_enter(&ipif->ipif_saved_ire_lock);
19107 	mp = ipif->ipif_saved_ire_mp;
19108 	ipif->ipif_saved_ire_mp = NULL;
19109 	mutex_exit(&ipif->ipif_saved_ire_lock);
19110 	freemsg(mp);
19111 
19112 	/*
19113 	 * Need to hold both ill_g_lock and ill_lock while
19114 	 * inserting or removing an ipif from the linked list
19115 	 * of ipifs hanging off the ill.
19116 	 */
19117 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19118 
19119 	ASSERT(ilm_walk_ipif(ipif) == 0);
19120 
19121 #ifdef DEBUG
19122 	ipif_trace_cleanup(ipif);
19123 #endif
19124 
19125 	/* Ask SCTP to take it out of it list */
19126 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19127 
19128 	/* Get it out of the ILL interface list. */
19129 	ipif_remove(ipif, B_TRUE);
19130 	rw_exit(&ipst->ips_ill_g_lock);
19131 
19132 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19133 
19134 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19135 	ASSERT(ipif->ipif_recovery_id == 0);
19136 
19137 	/* Free the memory. */
19138 	mi_free(ipif);
19139 }
19140 
19141 /*
19142  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19143  * is zero.
19144  */
19145 void
19146 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19147 {
19148 	char	lbuf[LIFNAMSIZ];
19149 	char	*name;
19150 	size_t	name_len;
19151 
19152 	buf[0] = '\0';
19153 	name = ipif->ipif_ill->ill_name;
19154 	name_len = ipif->ipif_ill->ill_name_length;
19155 	if (ipif->ipif_id != 0) {
19156 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19157 		    ipif->ipif_id);
19158 		name = lbuf;
19159 		name_len = mi_strlen(name) + 1;
19160 	}
19161 	len -= 1;
19162 	buf[len] = '\0';
19163 	len = MIN(len, name_len);
19164 	bcopy(name, buf, len);
19165 }
19166 
19167 /*
19168  * Find an IPIF based on the name passed in.  Names can be of the
19169  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19170  * The <phys> string can have forms like <dev><#> (e.g., le0),
19171  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19172  * When there is no colon, the implied unit id is zero. <phys> must
19173  * correspond to the name of an ILL.  (May be called as writer.)
19174  */
19175 static ipif_t *
19176 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19177     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19178     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19179 {
19180 	char	*cp;
19181 	char	*endp;
19182 	long	id;
19183 	ill_t	*ill;
19184 	ipif_t	*ipif;
19185 	uint_t	ire_type;
19186 	boolean_t did_alloc = B_FALSE;
19187 	ipsq_t	*ipsq;
19188 
19189 	if (error != NULL)
19190 		*error = 0;
19191 
19192 	/*
19193 	 * If the caller wants to us to create the ipif, make sure we have a
19194 	 * valid zoneid
19195 	 */
19196 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19197 
19198 	if (namelen == 0) {
19199 		if (error != NULL)
19200 			*error = ENXIO;
19201 		return (NULL);
19202 	}
19203 
19204 	*exists = B_FALSE;
19205 	/* Look for a colon in the name. */
19206 	endp = &name[namelen];
19207 	for (cp = endp; --cp > name; ) {
19208 		if (*cp == IPIF_SEPARATOR_CHAR)
19209 			break;
19210 	}
19211 
19212 	if (*cp == IPIF_SEPARATOR_CHAR) {
19213 		/*
19214 		 * Reject any non-decimal aliases for logical
19215 		 * interfaces. Aliases with leading zeroes
19216 		 * are also rejected as they introduce ambiguity
19217 		 * in the naming of the interfaces.
19218 		 * In order to confirm with existing semantics,
19219 		 * and to not break any programs/script relying
19220 		 * on that behaviour, if<0>:0 is considered to be
19221 		 * a valid interface.
19222 		 *
19223 		 * If alias has two or more digits and the first
19224 		 * is zero, fail.
19225 		 */
19226 		if (&cp[2] < endp && cp[1] == '0') {
19227 			if (error != NULL)
19228 				*error = EINVAL;
19229 			return (NULL);
19230 		}
19231 	}
19232 
19233 	if (cp <= name) {
19234 		cp = endp;
19235 	} else {
19236 		*cp = '\0';
19237 	}
19238 
19239 	/*
19240 	 * Look up the ILL, based on the portion of the name
19241 	 * before the slash. ill_lookup_on_name returns a held ill.
19242 	 * Temporary to check whether ill exists already. If so
19243 	 * ill_lookup_on_name will clear it.
19244 	 */
19245 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19246 	    q, mp, func, error, &did_alloc, ipst);
19247 	if (cp != endp)
19248 		*cp = IPIF_SEPARATOR_CHAR;
19249 	if (ill == NULL)
19250 		return (NULL);
19251 
19252 	/* Establish the unit number in the name. */
19253 	id = 0;
19254 	if (cp < endp && *endp == '\0') {
19255 		/* If there was a colon, the unit number follows. */
19256 		cp++;
19257 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19258 			ill_refrele(ill);
19259 			if (error != NULL)
19260 				*error = ENXIO;
19261 			return (NULL);
19262 		}
19263 	}
19264 
19265 	GRAB_CONN_LOCK(q);
19266 	mutex_enter(&ill->ill_lock);
19267 	/* Now see if there is an IPIF with this unit number. */
19268 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19269 		if (ipif->ipif_id == id) {
19270 			if (zoneid != ALL_ZONES &&
19271 			    zoneid != ipif->ipif_zoneid &&
19272 			    ipif->ipif_zoneid != ALL_ZONES) {
19273 				mutex_exit(&ill->ill_lock);
19274 				RELEASE_CONN_LOCK(q);
19275 				ill_refrele(ill);
19276 				if (error != NULL)
19277 					*error = ENXIO;
19278 				return (NULL);
19279 			}
19280 			/*
19281 			 * The block comment at the start of ipif_down
19282 			 * explains the use of the macros used below
19283 			 */
19284 			if (IPIF_CAN_LOOKUP(ipif)) {
19285 				ipif_refhold_locked(ipif);
19286 				mutex_exit(&ill->ill_lock);
19287 				if (!did_alloc)
19288 					*exists = B_TRUE;
19289 				/*
19290 				 * Drop locks before calling ill_refrele
19291 				 * since it can potentially call into
19292 				 * ipif_ill_refrele_tail which can end up
19293 				 * in trying to acquire any lock.
19294 				 */
19295 				RELEASE_CONN_LOCK(q);
19296 				ill_refrele(ill);
19297 				return (ipif);
19298 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19299 				ipsq = ill->ill_phyint->phyint_ipsq;
19300 				mutex_enter(&ipsq->ipsq_lock);
19301 				mutex_exit(&ill->ill_lock);
19302 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19303 				mutex_exit(&ipsq->ipsq_lock);
19304 				RELEASE_CONN_LOCK(q);
19305 				ill_refrele(ill);
19306 				if (error != NULL)
19307 					*error = EINPROGRESS;
19308 				return (NULL);
19309 			}
19310 		}
19311 	}
19312 	RELEASE_CONN_LOCK(q);
19313 
19314 	if (!do_alloc) {
19315 		mutex_exit(&ill->ill_lock);
19316 		ill_refrele(ill);
19317 		if (error != NULL)
19318 			*error = ENXIO;
19319 		return (NULL);
19320 	}
19321 
19322 	/*
19323 	 * If none found, atomically allocate and return a new one.
19324 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19325 	 * to support "receive only" use of lo0:1 etc. as is still done
19326 	 * below as an initial guess.
19327 	 * However, this is now likely to be overriden later in ipif_up_done()
19328 	 * when we know for sure what address has been configured on the
19329 	 * interface, since we might have more than one loopback interface
19330 	 * with a loopback address, e.g. in the case of zones, and all the
19331 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19332 	 */
19333 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19334 		ire_type = IRE_LOOPBACK;
19335 	else
19336 		ire_type = IRE_LOCAL;
19337 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19338 	if (ipif != NULL)
19339 		ipif_refhold_locked(ipif);
19340 	else if (error != NULL)
19341 		*error = ENOMEM;
19342 	mutex_exit(&ill->ill_lock);
19343 	ill_refrele(ill);
19344 	return (ipif);
19345 }
19346 
19347 /*
19348  * This routine is called whenever a new address comes up on an ipif.  If
19349  * we are configured to respond to address mask requests, then we are supposed
19350  * to broadcast an address mask reply at this time.  This routine is also
19351  * called if we are already up, but a netmask change is made.  This is legal
19352  * but might not make the system manager very popular.	(May be called
19353  * as writer.)
19354  */
19355 void
19356 ipif_mask_reply(ipif_t *ipif)
19357 {
19358 	icmph_t	*icmph;
19359 	ipha_t	*ipha;
19360 	mblk_t	*mp;
19361 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19362 
19363 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19364 
19365 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19366 		return;
19367 
19368 	/* ICMP mask reply is IPv4 only */
19369 	ASSERT(!ipif->ipif_isv6);
19370 	/* ICMP mask reply is not for a loopback interface */
19371 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19372 
19373 	mp = allocb(REPLY_LEN, BPRI_HI);
19374 	if (mp == NULL)
19375 		return;
19376 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19377 
19378 	ipha = (ipha_t *)mp->b_rptr;
19379 	bzero(ipha, REPLY_LEN);
19380 	*ipha = icmp_ipha;
19381 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19382 	ipha->ipha_src = ipif->ipif_src_addr;
19383 	ipha->ipha_dst = ipif->ipif_brd_addr;
19384 	ipha->ipha_length = htons(REPLY_LEN);
19385 	ipha->ipha_ident = 0;
19386 
19387 	icmph = (icmph_t *)&ipha[1];
19388 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19389 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19390 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19391 
19392 	put(ipif->ipif_wq, mp);
19393 
19394 #undef	REPLY_LEN
19395 }
19396 
19397 /*
19398  * When the mtu in the ipif changes, we call this routine through ire_walk
19399  * to update all the relevant IREs.
19400  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19401  */
19402 static void
19403 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19404 {
19405 	ipif_t *ipif = (ipif_t *)ipif_arg;
19406 
19407 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19408 		return;
19409 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19410 }
19411 
19412 /*
19413  * When the mtu in the ill changes, we call this routine through ire_walk
19414  * to update all the relevant IREs.
19415  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19416  */
19417 void
19418 ill_mtu_change(ire_t *ire, char *ill_arg)
19419 {
19420 	ill_t	*ill = (ill_t *)ill_arg;
19421 
19422 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19423 		return;
19424 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19425 }
19426 
19427 /*
19428  * Join the ipif specific multicast groups.
19429  * Must be called after a mapping has been set up in the resolver.  (Always
19430  * called as writer.)
19431  */
19432 void
19433 ipif_multicast_up(ipif_t *ipif)
19434 {
19435 	int err, index;
19436 	ill_t *ill;
19437 
19438 	ASSERT(IAM_WRITER_IPIF(ipif));
19439 
19440 	ill = ipif->ipif_ill;
19441 	index = ill->ill_phyint->phyint_ifindex;
19442 
19443 	ip1dbg(("ipif_multicast_up\n"));
19444 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19445 		return;
19446 
19447 	if (ipif->ipif_isv6) {
19448 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19449 			return;
19450 
19451 		/* Join the all hosts multicast address */
19452 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19453 		/*
19454 		 * Passing B_TRUE means we have to join the multicast
19455 		 * membership on this interface even though this is
19456 		 * FAILED. If we join on a different one in the group,
19457 		 * we will not be able to delete the membership later
19458 		 * as we currently don't track where we join when we
19459 		 * join within the kernel unlike applications where
19460 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19461 		 * for more on this.
19462 		 */
19463 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19464 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19465 		if (err != 0) {
19466 			ip0dbg(("ipif_multicast_up: "
19467 			    "all_hosts_mcast failed %d\n",
19468 			    err));
19469 			return;
19470 		}
19471 		/*
19472 		 * Enable multicast for the solicited node multicast address
19473 		 */
19474 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19475 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19476 
19477 			ipv6_multi.s6_addr32[3] |=
19478 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19479 
19480 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19481 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19482 			    NULL);
19483 			if (err != 0) {
19484 				ip0dbg(("ipif_multicast_up: solicited MC"
19485 				    " failed %d\n", err));
19486 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19487 				    ill, ill->ill_phyint->phyint_ifindex,
19488 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19489 				return;
19490 			}
19491 		}
19492 	} else {
19493 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19494 			return;
19495 
19496 		/* Join the all hosts multicast address */
19497 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19498 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19499 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19500 		if (err) {
19501 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19502 			return;
19503 		}
19504 	}
19505 	ipif->ipif_multicast_up = 1;
19506 }
19507 
19508 /*
19509  * Blow away any multicast groups that we joined in ipif_multicast_up().
19510  * (Explicit memberships are blown away in ill_leave_multicast() when the
19511  * ill is brought down.)
19512  */
19513 static void
19514 ipif_multicast_down(ipif_t *ipif)
19515 {
19516 	int err;
19517 
19518 	ASSERT(IAM_WRITER_IPIF(ipif));
19519 
19520 	ip1dbg(("ipif_multicast_down\n"));
19521 	if (!ipif->ipif_multicast_up)
19522 		return;
19523 
19524 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19525 
19526 	if (!ipif->ipif_isv6) {
19527 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19528 		    B_TRUE);
19529 		if (err != 0)
19530 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19531 
19532 		ipif->ipif_multicast_up = 0;
19533 		return;
19534 	}
19535 
19536 	/*
19537 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19538 	 * we should look for ilms on this ill rather than the ones that have
19539 	 * been failed over here.  They are here temporarily. As
19540 	 * ipif_multicast_up has joined on this ill, we should delete only
19541 	 * from this ill.
19542 	 */
19543 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19544 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19545 	    B_TRUE, B_TRUE);
19546 	if (err != 0) {
19547 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19548 		    err));
19549 	}
19550 	/*
19551 	 * Disable multicast for the solicited node multicast address
19552 	 */
19553 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19554 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19555 
19556 		ipv6_multi.s6_addr32[3] |=
19557 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19558 
19559 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19560 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19561 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19562 
19563 		if (err != 0) {
19564 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19565 			    err));
19566 		}
19567 	}
19568 
19569 	ipif->ipif_multicast_up = 0;
19570 }
19571 
19572 /*
19573  * Used when an interface comes up to recreate any extra routes on this
19574  * interface.
19575  */
19576 static ire_t **
19577 ipif_recover_ire(ipif_t *ipif)
19578 {
19579 	mblk_t	*mp;
19580 	ire_t	**ipif_saved_irep;
19581 	ire_t	**irep;
19582 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19583 
19584 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19585 	    ipif->ipif_id));
19586 
19587 	mutex_enter(&ipif->ipif_saved_ire_lock);
19588 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19589 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19590 	if (ipif_saved_irep == NULL) {
19591 		mutex_exit(&ipif->ipif_saved_ire_lock);
19592 		return (NULL);
19593 	}
19594 
19595 	irep = ipif_saved_irep;
19596 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19597 		ire_t		*ire;
19598 		queue_t		*rfq;
19599 		queue_t		*stq;
19600 		ifrt_t		*ifrt;
19601 		uchar_t		*src_addr;
19602 		uchar_t		*gateway_addr;
19603 		ushort_t	type;
19604 
19605 		/*
19606 		 * When the ire was initially created and then added in
19607 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19608 		 * in the case of a traditional interface route, or as one of
19609 		 * the IRE_OFFSUBNET types (with the exception of
19610 		 * IRE_HOST types ire which is created by icmp_redirect() and
19611 		 * which we don't need to save or recover).  In the case where
19612 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19613 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19614 		 * to satisfy software like GateD and Sun Cluster which creates
19615 		 * routes using the the loopback interface's address as a
19616 		 * gateway.
19617 		 *
19618 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19619 		 * ire_create() will be called in the same way here as
19620 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19621 		 * the route looks like a traditional interface route (where
19622 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19623 		 * the saved ifrt->ifrt_type.  This means that in the case where
19624 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19625 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19626 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19627 		 */
19628 		ifrt = (ifrt_t *)mp->b_rptr;
19629 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19630 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19631 			rfq = NULL;
19632 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19633 			    ? ipif->ipif_rq : ipif->ipif_wq;
19634 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19635 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19636 			    : (uint8_t *)&ipif->ipif_src_addr;
19637 			gateway_addr = NULL;
19638 			type = ipif->ipif_net_type;
19639 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19640 			/* Recover multiroute broadcast IRE. */
19641 			rfq = ipif->ipif_rq;
19642 			stq = ipif->ipif_wq;
19643 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19644 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19645 			    : (uint8_t *)&ipif->ipif_src_addr;
19646 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19647 			type = ifrt->ifrt_type;
19648 		} else {
19649 			rfq = NULL;
19650 			stq = NULL;
19651 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19652 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19653 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19654 			type = ifrt->ifrt_type;
19655 		}
19656 
19657 		/*
19658 		 * Create a copy of the IRE with the saved address and netmask.
19659 		 */
19660 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19661 		    "0x%x/0x%x\n",
19662 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19663 		    ntohl(ifrt->ifrt_addr),
19664 		    ntohl(ifrt->ifrt_mask)));
19665 		ire = ire_create(
19666 		    (uint8_t *)&ifrt->ifrt_addr,
19667 		    (uint8_t *)&ifrt->ifrt_mask,
19668 		    src_addr,
19669 		    gateway_addr,
19670 		    &ifrt->ifrt_max_frag,
19671 		    NULL,
19672 		    rfq,
19673 		    stq,
19674 		    type,
19675 		    ipif,
19676 		    0,
19677 		    0,
19678 		    0,
19679 		    ifrt->ifrt_flags,
19680 		    &ifrt->ifrt_iulp_info,
19681 		    NULL,
19682 		    NULL,
19683 		    ipst);
19684 
19685 		if (ire == NULL) {
19686 			mutex_exit(&ipif->ipif_saved_ire_lock);
19687 			kmem_free(ipif_saved_irep,
19688 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19689 			return (NULL);
19690 		}
19691 
19692 		/*
19693 		 * Some software (for example, GateD and Sun Cluster) attempts
19694 		 * to create (what amount to) IRE_PREFIX routes with the
19695 		 * loopback address as the gateway.  This is primarily done to
19696 		 * set up prefixes with the RTF_REJECT flag set (for example,
19697 		 * when generating aggregate routes.)
19698 		 *
19699 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19700 		 * IRE_LOOPBACK, then we map the request into a
19701 		 * IRE_IF_NORESOLVER.
19702 		 */
19703 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19704 			ire->ire_type = IRE_IF_NORESOLVER;
19705 		/*
19706 		 * ire held by ire_add, will be refreled' towards the
19707 		 * the end of ipif_up_done
19708 		 */
19709 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19710 		*irep = ire;
19711 		irep++;
19712 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19713 	}
19714 	mutex_exit(&ipif->ipif_saved_ire_lock);
19715 	return (ipif_saved_irep);
19716 }
19717 
19718 /*
19719  * Used to set the netmask and broadcast address to default values when the
19720  * interface is brought up.  (Always called as writer.)
19721  */
19722 static void
19723 ipif_set_default(ipif_t *ipif)
19724 {
19725 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19726 
19727 	if (!ipif->ipif_isv6) {
19728 		/*
19729 		 * Interface holds an IPv4 address. Default
19730 		 * mask is the natural netmask.
19731 		 */
19732 		if (!ipif->ipif_net_mask) {
19733 			ipaddr_t	v4mask;
19734 
19735 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19736 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19737 		}
19738 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19739 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19740 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19741 		} else {
19742 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19743 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19744 		}
19745 		/*
19746 		 * NOTE: SunOS 4.X does this even if the broadcast address
19747 		 * has been already set thus we do the same here.
19748 		 */
19749 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19750 			ipaddr_t	v4addr;
19751 
19752 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19753 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19754 		}
19755 	} else {
19756 		/*
19757 		 * Interface holds an IPv6-only address.  Default
19758 		 * mask is all-ones.
19759 		 */
19760 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19761 			ipif->ipif_v6net_mask = ipv6_all_ones;
19762 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19763 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19764 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19765 		} else {
19766 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19767 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19768 		}
19769 	}
19770 }
19771 
19772 /*
19773  * Return 0 if this address can be used as local address without causing
19774  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19775  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19776  * Special checks are needed to allow the same IPv6 link-local address
19777  * on different ills.
19778  * TODO: allowing the same site-local address on different ill's.
19779  */
19780 int
19781 ip_addr_availability_check(ipif_t *new_ipif)
19782 {
19783 	in6_addr_t our_v6addr;
19784 	ill_t *ill;
19785 	ipif_t *ipif;
19786 	ill_walk_context_t ctx;
19787 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19788 
19789 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19790 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19791 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19792 
19793 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19794 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19795 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19796 		return (0);
19797 
19798 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19799 
19800 	if (new_ipif->ipif_isv6)
19801 		ill = ILL_START_WALK_V6(&ctx, ipst);
19802 	else
19803 		ill = ILL_START_WALK_V4(&ctx, ipst);
19804 
19805 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19806 		for (ipif = ill->ill_ipif; ipif != NULL;
19807 		    ipif = ipif->ipif_next) {
19808 			if ((ipif == new_ipif) ||
19809 			    !(ipif->ipif_flags & IPIF_UP) ||
19810 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19811 				continue;
19812 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19813 			    &our_v6addr)) {
19814 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19815 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19816 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19817 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19818 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19819 				    new_ipif->ipif_ill != ill)
19820 					continue;
19821 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19822 				    new_ipif->ipif_ill != ill)
19823 					continue;
19824 				else if (new_ipif->ipif_zoneid !=
19825 				    ipif->ipif_zoneid &&
19826 				    ipif->ipif_zoneid != ALL_ZONES &&
19827 				    IS_LOOPBACK(ill))
19828 					continue;
19829 				else if (new_ipif->ipif_ill == ill)
19830 					return (EADDRINUSE);
19831 				else
19832 					return (EADDRNOTAVAIL);
19833 			}
19834 		}
19835 	}
19836 
19837 	return (0);
19838 }
19839 
19840 /*
19841  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19842  * IREs for the ipif.
19843  * When the routine returns EINPROGRESS then mp has been consumed and
19844  * the ioctl will be acked from ip_rput_dlpi.
19845  */
19846 static int
19847 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19848 {
19849 	ill_t	*ill = ipif->ipif_ill;
19850 	boolean_t isv6 = ipif->ipif_isv6;
19851 	int	err = 0;
19852 	boolean_t success;
19853 
19854 	ASSERT(IAM_WRITER_IPIF(ipif));
19855 
19856 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19857 
19858 	/* Shouldn't get here if it is already up. */
19859 	if (ipif->ipif_flags & IPIF_UP)
19860 		return (EALREADY);
19861 
19862 	/* Skip arp/ndp for any loopback interface. */
19863 	if (ill->ill_wq != NULL) {
19864 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19865 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19866 
19867 		if (!ill->ill_dl_up) {
19868 			/*
19869 			 * ill_dl_up is not yet set. i.e. we are yet to
19870 			 * DL_BIND with the driver and this is the first
19871 			 * logical interface on the ill to become "up".
19872 			 * Tell the driver to get going (via DL_BIND_REQ).
19873 			 * Note that changing "significant" IFF_ flags
19874 			 * address/netmask etc cause a down/up dance, but
19875 			 * does not cause an unbind (DL_UNBIND) with the driver
19876 			 */
19877 			return (ill_dl_up(ill, ipif, mp, q));
19878 		}
19879 
19880 		/*
19881 		 * ipif_resolver_up may end up sending an
19882 		 * AR_INTERFACE_UP message to ARP, which would, in
19883 		 * turn send a DLPI message to the driver. ioctls are
19884 		 * serialized and so we cannot send more than one
19885 		 * interface up message at a time. If ipif_resolver_up
19886 		 * does send an interface up message to ARP, we get
19887 		 * EINPROGRESS and we will complete in ip_arp_done.
19888 		 */
19889 
19890 		ASSERT(connp != NULL || !CONN_Q(q));
19891 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19892 		if (connp != NULL)
19893 			mutex_enter(&connp->conn_lock);
19894 		mutex_enter(&ill->ill_lock);
19895 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19896 		mutex_exit(&ill->ill_lock);
19897 		if (connp != NULL)
19898 			mutex_exit(&connp->conn_lock);
19899 		if (!success)
19900 			return (EINTR);
19901 
19902 		/*
19903 		 * Crank up IPv6 neighbor discovery
19904 		 * Unlike ARP, this should complete when
19905 		 * ipif_ndp_up returns. However, for
19906 		 * ILLF_XRESOLV interfaces we also send a
19907 		 * AR_INTERFACE_UP to the external resolver.
19908 		 * That ioctl will complete in ip_rput.
19909 		 */
19910 		if (isv6) {
19911 			err = ipif_ndp_up(ipif);
19912 			if (err != 0) {
19913 				if (err != EINPROGRESS)
19914 					mp = ipsq_pending_mp_get(ipsq, &connp);
19915 				return (err);
19916 			}
19917 		}
19918 		/* Now, ARP */
19919 		err = ipif_resolver_up(ipif, Res_act_initial);
19920 		if (err == EINPROGRESS) {
19921 			/* We will complete it in ip_arp_done */
19922 			return (err);
19923 		}
19924 		mp = ipsq_pending_mp_get(ipsq, &connp);
19925 		ASSERT(mp != NULL);
19926 		if (err != 0)
19927 			return (err);
19928 	} else {
19929 		/*
19930 		 * Interfaces without underlying hardware don't do duplicate
19931 		 * address detection.
19932 		 */
19933 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19934 		ipif->ipif_addr_ready = 1;
19935 	}
19936 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19937 }
19938 
19939 /*
19940  * Perform a bind for the physical device.
19941  * When the routine returns EINPROGRESS then mp has been consumed and
19942  * the ioctl will be acked from ip_rput_dlpi.
19943  * Allocate an unbind message and save it until ipif_down.
19944  */
19945 static int
19946 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19947 {
19948 	areq_t	*areq;
19949 	mblk_t	*areq_mp = NULL;
19950 	mblk_t	*bind_mp = NULL;
19951 	mblk_t	*unbind_mp = NULL;
19952 	conn_t	*connp;
19953 	boolean_t success;
19954 	uint16_t sap_addr;
19955 
19956 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19957 	ASSERT(IAM_WRITER_ILL(ill));
19958 	ASSERT(mp != NULL);
19959 
19960 	/* Create a resolver cookie for ARP */
19961 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19962 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19963 		if (areq_mp == NULL)
19964 			return (ENOMEM);
19965 
19966 		freemsg(ill->ill_resolver_mp);
19967 		ill->ill_resolver_mp = areq_mp;
19968 		areq = (areq_t *)areq_mp->b_rptr;
19969 		sap_addr = ill->ill_sap;
19970 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19971 	}
19972 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19973 	    DL_BIND_REQ);
19974 	if (bind_mp == NULL)
19975 		goto bad;
19976 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19977 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19978 
19979 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19980 	if (unbind_mp == NULL)
19981 		goto bad;
19982 
19983 	/*
19984 	 * Record state needed to complete this operation when the
19985 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19986 	 */
19987 	ASSERT(WR(q)->q_next == NULL);
19988 	connp = Q_TO_CONN(q);
19989 
19990 	mutex_enter(&connp->conn_lock);
19991 	mutex_enter(&ipif->ipif_ill->ill_lock);
19992 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19993 	mutex_exit(&ipif->ipif_ill->ill_lock);
19994 	mutex_exit(&connp->conn_lock);
19995 	if (!success)
19996 		goto bad;
19997 
19998 	/*
19999 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20000 	 * the interface goes down.
20001 	 */
20002 	ASSERT(ill->ill_unbind_mp == NULL);
20003 	ill->ill_unbind_mp = unbind_mp;
20004 
20005 	ill_dlpi_send(ill, bind_mp);
20006 	/* Send down link-layer capabilities probe if not already done. */
20007 	ill_capability_probe(ill);
20008 
20009 	/*
20010 	 * Sysid used to rely on the fact that netboots set domainname
20011 	 * and the like. Now that miniroot boots aren't strictly netboots
20012 	 * and miniroot network configuration is driven from userland
20013 	 * these things still need to be set. This situation can be detected
20014 	 * by comparing the interface being configured here to the one
20015 	 * dhcifname was set to reference by the boot loader. Once sysid is
20016 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20017 	 */
20018 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
20019 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
20020 	    (strlen(srpc_domain) == 0)) {
20021 		if (dhcpinit() != 0)
20022 			cmn_err(CE_WARN, "no cached dhcp response");
20023 	}
20024 
20025 	/*
20026 	 * This operation will complete in ip_rput_dlpi with either
20027 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20028 	 */
20029 	return (EINPROGRESS);
20030 bad:
20031 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20032 	/*
20033 	 * We don't have to check for possible removal from illgrp
20034 	 * as we have not yet inserted in illgrp. For groups
20035 	 * without names, this ipif is still not UP and hence
20036 	 * this could not have possibly had any influence in forming
20037 	 * groups.
20038 	 */
20039 
20040 	freemsg(bind_mp);
20041 	freemsg(unbind_mp);
20042 	return (ENOMEM);
20043 }
20044 
20045 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20046 
20047 /*
20048  * DLPI and ARP is up.
20049  * Create all the IREs associated with an interface bring up multicast.
20050  * Set the interface flag and finish other initialization
20051  * that potentially had to be differed to after DL_BIND_ACK.
20052  */
20053 int
20054 ipif_up_done(ipif_t *ipif)
20055 {
20056 	ire_t	*ire_array[20];
20057 	ire_t	**irep = ire_array;
20058 	ire_t	**irep1;
20059 	ipaddr_t net_mask = 0;
20060 	ipaddr_t subnet_mask, route_mask;
20061 	ill_t	*ill = ipif->ipif_ill;
20062 	queue_t	*stq;
20063 	ipif_t	 *src_ipif;
20064 	ipif_t   *tmp_ipif;
20065 	boolean_t	flush_ire_cache = B_TRUE;
20066 	int	err = 0;
20067 	phyint_t *phyi;
20068 	ire_t	**ipif_saved_irep = NULL;
20069 	int ipif_saved_ire_cnt;
20070 	int	cnt;
20071 	boolean_t	src_ipif_held = B_FALSE;
20072 	boolean_t	ire_added = B_FALSE;
20073 	boolean_t	loopback = B_FALSE;
20074 	ip_stack_t	*ipst = ill->ill_ipst;
20075 
20076 	ip1dbg(("ipif_up_done(%s:%u)\n",
20077 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20078 	/* Check if this is a loopback interface */
20079 	if (ipif->ipif_ill->ill_wq == NULL)
20080 		loopback = B_TRUE;
20081 
20082 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20083 	/*
20084 	 * If all other interfaces for this ill are down or DEPRECATED,
20085 	 * or otherwise unsuitable for source address selection, remove
20086 	 * any IRE_CACHE entries for this ill to make sure source
20087 	 * address selection gets to take this new ipif into account.
20088 	 * No need to hold ill_lock while traversing the ipif list since
20089 	 * we are writer
20090 	 */
20091 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20092 	    tmp_ipif = tmp_ipif->ipif_next) {
20093 		if (((tmp_ipif->ipif_flags &
20094 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20095 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20096 		    (tmp_ipif == ipif))
20097 			continue;
20098 		/* first useable pre-existing interface */
20099 		flush_ire_cache = B_FALSE;
20100 		break;
20101 	}
20102 	if (flush_ire_cache)
20103 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20104 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20105 
20106 	/*
20107 	 * Figure out which way the send-to queue should go.  Only
20108 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20109 	 * should show up here.
20110 	 */
20111 	switch (ill->ill_net_type) {
20112 	case IRE_IF_RESOLVER:
20113 		stq = ill->ill_rq;
20114 		break;
20115 	case IRE_IF_NORESOLVER:
20116 	case IRE_LOOPBACK:
20117 		stq = ill->ill_wq;
20118 		break;
20119 	default:
20120 		return (EINVAL);
20121 	}
20122 
20123 	if (IS_LOOPBACK(ill)) {
20124 		/*
20125 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20126 		 * ipif_lookup_on_name(), but in the case of zones we can have
20127 		 * several loopback addresses on lo0. So all the interfaces with
20128 		 * loopback addresses need to be marked IRE_LOOPBACK.
20129 		 */
20130 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20131 		    htonl(INADDR_LOOPBACK))
20132 			ipif->ipif_ire_type = IRE_LOOPBACK;
20133 		else
20134 			ipif->ipif_ire_type = IRE_LOCAL;
20135 	}
20136 
20137 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20138 		/*
20139 		 * Can't use our source address. Select a different
20140 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20141 		 */
20142 		src_ipif = ipif_select_source(ipif->ipif_ill,
20143 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20144 		if (src_ipif == NULL)
20145 			src_ipif = ipif;	/* Last resort */
20146 		else
20147 			src_ipif_held = B_TRUE;
20148 	} else {
20149 		src_ipif = ipif;
20150 	}
20151 
20152 	/* Create all the IREs associated with this interface */
20153 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20154 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20155 
20156 		/*
20157 		 * If we're on a labeled system then make sure that zone-
20158 		 * private addresses have proper remote host database entries.
20159 		 */
20160 		if (is_system_labeled() &&
20161 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20162 		    !tsol_check_interface_address(ipif))
20163 			return (EINVAL);
20164 
20165 		/* Register the source address for __sin6_src_id */
20166 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20167 		    ipif->ipif_zoneid, ipst);
20168 		if (err != 0) {
20169 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20170 			return (err);
20171 		}
20172 
20173 		/* If the interface address is set, create the local IRE. */
20174 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20175 		    (void *)ipif,
20176 		    ipif->ipif_ire_type,
20177 		    ntohl(ipif->ipif_lcl_addr)));
20178 		*irep++ = ire_create(
20179 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20180 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20181 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20182 		    NULL,				/* no gateway */
20183 		    &ip_loopback_mtuplus,		/* max frag size */
20184 		    NULL,
20185 		    ipif->ipif_rq,			/* recv-from queue */
20186 		    NULL,				/* no send-to queue */
20187 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20188 		    ipif,
20189 		    0,
20190 		    0,
20191 		    0,
20192 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20193 		    RTF_PRIVATE : 0,
20194 		    &ire_uinfo_null,
20195 		    NULL,
20196 		    NULL,
20197 		    ipst);
20198 	} else {
20199 		ip1dbg((
20200 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20201 		    ipif->ipif_ire_type,
20202 		    ntohl(ipif->ipif_lcl_addr),
20203 		    (uint_t)ipif->ipif_flags));
20204 	}
20205 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20206 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20207 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20208 	} else {
20209 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20210 	}
20211 
20212 	subnet_mask = ipif->ipif_net_mask;
20213 
20214 	/*
20215 	 * If mask was not specified, use natural netmask of
20216 	 * interface address. Also, store this mask back into the
20217 	 * ipif struct.
20218 	 */
20219 	if (subnet_mask == 0) {
20220 		subnet_mask = net_mask;
20221 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20222 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20223 		    ipif->ipif_v6subnet);
20224 	}
20225 
20226 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20227 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20228 	    ipif->ipif_subnet != INADDR_ANY) {
20229 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20230 
20231 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20232 			route_mask = IP_HOST_MASK;
20233 		} else {
20234 			route_mask = subnet_mask;
20235 		}
20236 
20237 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20238 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20239 		    (void *)ipif, (void *)ill,
20240 		    ill->ill_net_type,
20241 		    ntohl(ipif->ipif_subnet)));
20242 		*irep++ = ire_create(
20243 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20244 		    (uchar_t *)&route_mask,		/* mask */
20245 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20246 		    NULL,				/* no gateway */
20247 		    &ipif->ipif_mtu,			/* max frag */
20248 		    NULL,
20249 		    NULL,				/* no recv queue */
20250 		    stq,				/* send-to queue */
20251 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20252 		    ipif,
20253 		    0,
20254 		    0,
20255 		    0,
20256 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20257 		    &ire_uinfo_null,
20258 		    NULL,
20259 		    NULL,
20260 		    ipst);
20261 	}
20262 
20263 	/*
20264 	 * Create any necessary broadcast IREs.
20265 	 */
20266 	if (ipif->ipif_flags & IPIF_BROADCAST)
20267 		irep = ipif_create_bcast_ires(ipif, irep);
20268 
20269 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20270 
20271 	/* If an earlier ire_create failed, get out now */
20272 	for (irep1 = irep; irep1 > ire_array; ) {
20273 		irep1--;
20274 		if (*irep1 == NULL) {
20275 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20276 			err = ENOMEM;
20277 			goto bad;
20278 		}
20279 	}
20280 
20281 	/*
20282 	 * Need to atomically check for ip_addr_availablity_check
20283 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20284 	 * from group also.The ill_g_lock is grabbed as reader
20285 	 * just to make sure no new ills or new ipifs are being added
20286 	 * to the system while we are checking the uniqueness of addresses.
20287 	 */
20288 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20289 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20290 	/* Mark it up, and increment counters. */
20291 	ipif->ipif_flags |= IPIF_UP;
20292 	ill->ill_ipif_up_count++;
20293 	err = ip_addr_availability_check(ipif);
20294 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20295 	rw_exit(&ipst->ips_ill_g_lock);
20296 
20297 	if (err != 0) {
20298 		/*
20299 		 * Our address may already be up on the same ill. In this case,
20300 		 * the ARP entry for our ipif replaced the one for the other
20301 		 * ipif. So we don't want to delete it (otherwise the other ipif
20302 		 * would be unable to send packets).
20303 		 * ip_addr_availability_check() identifies this case for us and
20304 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20305 		 * which is the expected error code.
20306 		 */
20307 		if (err == EADDRINUSE) {
20308 			freemsg(ipif->ipif_arp_del_mp);
20309 			ipif->ipif_arp_del_mp = NULL;
20310 			err = EADDRNOTAVAIL;
20311 		}
20312 		ill->ill_ipif_up_count--;
20313 		ipif->ipif_flags &= ~IPIF_UP;
20314 		goto bad;
20315 	}
20316 
20317 	/*
20318 	 * Add in all newly created IREs.  ire_create_bcast() has
20319 	 * already checked for duplicates of the IRE_BROADCAST type.
20320 	 * We want to add before we call ifgrp_insert which wants
20321 	 * to know whether IRE_IF_RESOLVER exists or not.
20322 	 *
20323 	 * NOTE : We refrele the ire though we may branch to "bad"
20324 	 *	  later on where we do ire_delete. This is okay
20325 	 *	  because nobody can delete it as we are running
20326 	 *	  exclusively.
20327 	 */
20328 	for (irep1 = irep; irep1 > ire_array; ) {
20329 		irep1--;
20330 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20331 		/*
20332 		 * refheld by ire_add. refele towards the end of the func
20333 		 */
20334 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20335 	}
20336 	ire_added = B_TRUE;
20337 	/*
20338 	 * Form groups if possible.
20339 	 *
20340 	 * If we are supposed to be in a ill_group with a name, insert it
20341 	 * now as we know that at least one ipif is UP. Otherwise form
20342 	 * nameless groups.
20343 	 *
20344 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20345 	 * this ipif into the appropriate interface group, or create a
20346 	 * new one. If this is already in a nameless group, we try to form
20347 	 * a bigger group looking at other ills potentially sharing this
20348 	 * ipif's prefix.
20349 	 */
20350 	phyi = ill->ill_phyint;
20351 	if (phyi->phyint_groupname_len != 0) {
20352 		ASSERT(phyi->phyint_groupname != NULL);
20353 		if (ill->ill_ipif_up_count == 1) {
20354 			ASSERT(ill->ill_group == NULL);
20355 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20356 			    phyi->phyint_groupname, NULL, B_TRUE);
20357 			if (err != 0) {
20358 				ip1dbg(("ipif_up_done: illgrp allocation "
20359 				    "failed, error %d\n", err));
20360 				goto bad;
20361 			}
20362 		}
20363 		ASSERT(ill->ill_group != NULL);
20364 	}
20365 
20366 	/*
20367 	 * When this is part of group, we need to make sure that
20368 	 * any broadcast ires created because of this ipif coming
20369 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20370 	 * so that we don't receive duplicate broadcast packets.
20371 	 */
20372 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20373 		ipif_renominate_bcast(ipif);
20374 
20375 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20376 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20377 	ipif_saved_irep = ipif_recover_ire(ipif);
20378 
20379 	if (!loopback) {
20380 		/*
20381 		 * If the broadcast address has been set, make sure it makes
20382 		 * sense based on the interface address.
20383 		 * Only match on ill since we are sharing broadcast addresses.
20384 		 */
20385 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20386 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20387 			ire_t	*ire;
20388 
20389 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20390 			    IRE_BROADCAST, ipif, ALL_ZONES,
20391 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20392 
20393 			if (ire == NULL) {
20394 				/*
20395 				 * If there isn't a matching broadcast IRE,
20396 				 * revert to the default for this netmask.
20397 				 */
20398 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20399 				mutex_enter(&ipif->ipif_ill->ill_lock);
20400 				ipif_set_default(ipif);
20401 				mutex_exit(&ipif->ipif_ill->ill_lock);
20402 			} else {
20403 				ire_refrele(ire);
20404 			}
20405 		}
20406 
20407 	}
20408 
20409 	/* This is the first interface on this ill */
20410 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20411 		/*
20412 		 * Need to recover all multicast memberships in the driver.
20413 		 * This had to be deferred until we had attached.
20414 		 */
20415 		ill_recover_multicast(ill);
20416 	}
20417 	/* Join the allhosts multicast address */
20418 	ipif_multicast_up(ipif);
20419 
20420 	if (!loopback) {
20421 		/*
20422 		 * See whether anybody else would benefit from the
20423 		 * new ipif that we added. We call this always rather
20424 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20425 		 * ipif is for the benefit of illgrp_insert (done above)
20426 		 * which does not do source address selection as it does
20427 		 * not want to re-create interface routes that we are
20428 		 * having reference to it here.
20429 		 */
20430 		ill_update_source_selection(ill);
20431 	}
20432 
20433 	for (irep1 = irep; irep1 > ire_array; ) {
20434 		irep1--;
20435 		if (*irep1 != NULL) {
20436 			/* was held in ire_add */
20437 			ire_refrele(*irep1);
20438 		}
20439 	}
20440 
20441 	cnt = ipif_saved_ire_cnt;
20442 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20443 		if (*irep1 != NULL) {
20444 			/* was held in ire_add */
20445 			ire_refrele(*irep1);
20446 		}
20447 	}
20448 
20449 	if (!loopback && ipif->ipif_addr_ready) {
20450 		/* Broadcast an address mask reply. */
20451 		ipif_mask_reply(ipif);
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 	/*
20461 	 * This had to be deferred until we had bound.  Tell routing sockets and
20462 	 * others that this interface is up if it looks like the address has
20463 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20464 	 * duplicate address detection to do its thing.
20465 	 */
20466 	if (ipif->ipif_addr_ready) {
20467 		ip_rts_ifmsg(ipif);
20468 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20469 		/* Let SCTP update the status for this ipif */
20470 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20471 	}
20472 	return (0);
20473 
20474 bad:
20475 	ip1dbg(("ipif_up_done: FAILED \n"));
20476 	/*
20477 	 * We don't have to bother removing from ill groups because
20478 	 *
20479 	 * 1) For groups with names, we insert only when the first ipif
20480 	 *    comes up. In that case if it fails, it will not be in any
20481 	 *    group. So, we need not try to remove for that case.
20482 	 *
20483 	 * 2) For groups without names, either we tried to insert ipif_ill
20484 	 *    in a group as singleton or found some other group to become
20485 	 *    a bigger group. For the former, if it fails we don't have
20486 	 *    anything to do as ipif_ill is not in the group and for the
20487 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20488 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20489 	 */
20490 	while (irep > ire_array) {
20491 		irep--;
20492 		if (*irep != NULL) {
20493 			ire_delete(*irep);
20494 			if (ire_added)
20495 				ire_refrele(*irep);
20496 		}
20497 	}
20498 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20499 
20500 	if (ipif_saved_irep != NULL) {
20501 		kmem_free(ipif_saved_irep,
20502 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20503 	}
20504 	if (src_ipif_held)
20505 		ipif_refrele(src_ipif);
20506 
20507 	ipif_arp_down(ipif);
20508 	return (err);
20509 }
20510 
20511 /*
20512  * Turn off the ARP with the ILLF_NOARP flag.
20513  */
20514 static int
20515 ill_arp_off(ill_t *ill)
20516 {
20517 	mblk_t	*arp_off_mp = NULL;
20518 	mblk_t	*arp_on_mp = NULL;
20519 
20520 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20521 
20522 	ASSERT(IAM_WRITER_ILL(ill));
20523 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20524 
20525 	/*
20526 	 * If the on message is still around we've already done
20527 	 * an arp_off without doing an arp_on thus there is no
20528 	 * work needed.
20529 	 */
20530 	if (ill->ill_arp_on_mp != NULL)
20531 		return (0);
20532 
20533 	/*
20534 	 * Allocate an ARP on message (to be saved) and an ARP off message
20535 	 */
20536 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20537 	if (!arp_off_mp)
20538 		return (ENOMEM);
20539 
20540 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20541 	if (!arp_on_mp)
20542 		goto failed;
20543 
20544 	ASSERT(ill->ill_arp_on_mp == NULL);
20545 	ill->ill_arp_on_mp = arp_on_mp;
20546 
20547 	/* Send an AR_INTERFACE_OFF request */
20548 	putnext(ill->ill_rq, arp_off_mp);
20549 	return (0);
20550 failed:
20551 
20552 	if (arp_off_mp)
20553 		freemsg(arp_off_mp);
20554 	return (ENOMEM);
20555 }
20556 
20557 /*
20558  * Turn on ARP by turning off the ILLF_NOARP flag.
20559  */
20560 static int
20561 ill_arp_on(ill_t *ill)
20562 {
20563 	mblk_t	*mp;
20564 
20565 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20566 
20567 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20568 
20569 	ASSERT(IAM_WRITER_ILL(ill));
20570 	/*
20571 	 * Send an AR_INTERFACE_ON request if we have already done
20572 	 * an arp_off (which allocated the message).
20573 	 */
20574 	if (ill->ill_arp_on_mp != NULL) {
20575 		mp = ill->ill_arp_on_mp;
20576 		ill->ill_arp_on_mp = NULL;
20577 		putnext(ill->ill_rq, mp);
20578 	}
20579 	return (0);
20580 }
20581 
20582 /*
20583  * Called after either deleting ill from the group or when setting
20584  * FAILED or STANDBY on the interface.
20585  */
20586 static void
20587 illgrp_reset_schednext(ill_t *ill)
20588 {
20589 	ill_group_t *illgrp;
20590 	ill_t *save_ill;
20591 
20592 	ASSERT(IAM_WRITER_ILL(ill));
20593 	/*
20594 	 * When called from illgrp_delete, ill_group will be non-NULL.
20595 	 * But when called from ip_sioctl_flags, it could be NULL if
20596 	 * somebody is setting FAILED/INACTIVE on some interface which
20597 	 * is not part of a group.
20598 	 */
20599 	illgrp = ill->ill_group;
20600 	if (illgrp == NULL)
20601 		return;
20602 	if (illgrp->illgrp_ill_schednext != ill)
20603 		return;
20604 
20605 	illgrp->illgrp_ill_schednext = NULL;
20606 	save_ill = ill;
20607 	/*
20608 	 * Choose a good ill to be the next one for
20609 	 * outbound traffic. As the flags FAILED/STANDBY is
20610 	 * not yet marked when called from ip_sioctl_flags,
20611 	 * we check for ill separately.
20612 	 */
20613 	for (ill = illgrp->illgrp_ill; ill != NULL;
20614 	    ill = ill->ill_group_next) {
20615 		if ((ill != save_ill) &&
20616 		    !(ill->ill_phyint->phyint_flags &
20617 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20618 			illgrp->illgrp_ill_schednext = ill;
20619 			return;
20620 		}
20621 	}
20622 }
20623 
20624 /*
20625  * Given an ill, find the next ill in the group to be scheduled.
20626  * (This should be called by ip_newroute() before ire_create().)
20627  * The passed in ill may be pulled out of the group, after we have picked
20628  * up a different outgoing ill from the same group. However ire add will
20629  * atomically check this.
20630  */
20631 ill_t *
20632 illgrp_scheduler(ill_t *ill)
20633 {
20634 	ill_t *retill;
20635 	ill_group_t *illgrp;
20636 	int illcnt;
20637 	int i;
20638 	uint64_t flags;
20639 	ip_stack_t	*ipst = ill->ill_ipst;
20640 
20641 	/*
20642 	 * We don't use a lock to check for the ill_group. If this ill
20643 	 * is currently being inserted we may end up just returning this
20644 	 * ill itself. That is ok.
20645 	 */
20646 	if (ill->ill_group == NULL) {
20647 		ill_refhold(ill);
20648 		return (ill);
20649 	}
20650 
20651 	/*
20652 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20653 	 * a set of stable ills. No ill can be added or deleted or change
20654 	 * group while we hold the reader lock.
20655 	 */
20656 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20657 	if ((illgrp = ill->ill_group) == NULL) {
20658 		rw_exit(&ipst->ips_ill_g_lock);
20659 		ill_refhold(ill);
20660 		return (ill);
20661 	}
20662 
20663 	illcnt = illgrp->illgrp_ill_count;
20664 	mutex_enter(&illgrp->illgrp_lock);
20665 	retill = illgrp->illgrp_ill_schednext;
20666 
20667 	if (retill == NULL)
20668 		retill = illgrp->illgrp_ill;
20669 
20670 	/*
20671 	 * We do a circular search beginning at illgrp_ill_schednext
20672 	 * or illgrp_ill. We don't check the flags against the ill lock
20673 	 * since it can change anytime. The ire creation will be atomic
20674 	 * and will fail if the ill is FAILED or OFFLINE.
20675 	 */
20676 	for (i = 0; i < illcnt; i++) {
20677 		flags = retill->ill_phyint->phyint_flags;
20678 
20679 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20680 		    ILL_CAN_LOOKUP(retill)) {
20681 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20682 			ill_refhold(retill);
20683 			break;
20684 		}
20685 		retill = retill->ill_group_next;
20686 		if (retill == NULL)
20687 			retill = illgrp->illgrp_ill;
20688 	}
20689 	mutex_exit(&illgrp->illgrp_lock);
20690 	rw_exit(&ipst->ips_ill_g_lock);
20691 
20692 	return (i == illcnt ? NULL : retill);
20693 }
20694 
20695 /*
20696  * Checks for availbility of a usable source address (if there is one) when the
20697  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20698  * this selection is done regardless of the destination.
20699  */
20700 boolean_t
20701 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20702 {
20703 	uint_t	ifindex;
20704 	ipif_t	*ipif = NULL;
20705 	ill_t	*uill;
20706 	boolean_t isv6;
20707 	ip_stack_t	*ipst = ill->ill_ipst;
20708 
20709 	ASSERT(ill != NULL);
20710 
20711 	isv6 = ill->ill_isv6;
20712 	ifindex = ill->ill_usesrc_ifindex;
20713 	if (ifindex != 0) {
20714 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20715 		    NULL, ipst);
20716 		if (uill == NULL)
20717 			return (NULL);
20718 		mutex_enter(&uill->ill_lock);
20719 		for (ipif = uill->ill_ipif; ipif != NULL;
20720 		    ipif = ipif->ipif_next) {
20721 			if (!IPIF_CAN_LOOKUP(ipif))
20722 				continue;
20723 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20724 				continue;
20725 			if (!(ipif->ipif_flags & IPIF_UP))
20726 				continue;
20727 			if (ipif->ipif_zoneid != zoneid)
20728 				continue;
20729 			if ((isv6 &&
20730 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20731 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20732 				continue;
20733 			mutex_exit(&uill->ill_lock);
20734 			ill_refrele(uill);
20735 			return (B_TRUE);
20736 		}
20737 		mutex_exit(&uill->ill_lock);
20738 		ill_refrele(uill);
20739 	}
20740 	return (B_FALSE);
20741 }
20742 
20743 /*
20744  * Determine the best source address given a destination address and an ill.
20745  * Prefers non-deprecated over deprecated but will return a deprecated
20746  * address if there is no other choice. If there is a usable source address
20747  * on the interface pointed to by ill_usesrc_ifindex then that is given
20748  * first preference.
20749  *
20750  * Returns NULL if there is no suitable source address for the ill.
20751  * This only occurs when there is no valid source address for the ill.
20752  */
20753 ipif_t *
20754 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20755 {
20756 	ipif_t *ipif;
20757 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20758 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20759 	int index = 0;
20760 	boolean_t wrapped = B_FALSE;
20761 	boolean_t same_subnet_only = B_FALSE;
20762 	boolean_t ipif_same_found, ipif_other_found;
20763 	boolean_t specific_found;
20764 	ill_t	*till, *usill = NULL;
20765 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20766 	ip_stack_t	*ipst = ill->ill_ipst;
20767 
20768 	if (ill->ill_usesrc_ifindex != 0) {
20769 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20770 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20771 		if (usill != NULL)
20772 			ill = usill;	/* Select source from usesrc ILL */
20773 		else
20774 			return (NULL);
20775 	}
20776 
20777 	/*
20778 	 * If we're dealing with an unlabeled destination on a labeled system,
20779 	 * make sure that we ignore source addresses that are incompatible with
20780 	 * the destination's default label.  That destination's default label
20781 	 * must dominate the minimum label on the source address.
20782 	 */
20783 	dst_rhtp = NULL;
20784 	if (is_system_labeled()) {
20785 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20786 		if (dst_rhtp == NULL)
20787 			return (NULL);
20788 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20789 			TPC_RELE(dst_rhtp);
20790 			dst_rhtp = NULL;
20791 		}
20792 	}
20793 
20794 	/*
20795 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20796 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20797 	 * After selecting the right ipif, under ill_lock make sure ipif is
20798 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20799 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20800 	 * but not under a lock.
20801 	 */
20802 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20803 
20804 retry:
20805 	till = ill;
20806 	ipif_arr[0] = NULL;
20807 
20808 	if (till->ill_group != NULL)
20809 		till = till->ill_group->illgrp_ill;
20810 
20811 	/*
20812 	 * Choose one good source address from each ill across the group.
20813 	 * If possible choose a source address in the same subnet as
20814 	 * the destination address.
20815 	 *
20816 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20817 	 * This is okay because of the following.
20818 	 *
20819 	 *    If PHYI_FAILED is set and we still have non-deprecated
20820 	 *    addresses, it means the addresses have not yet been
20821 	 *    failed over to a different interface. We potentially
20822 	 *    select them to create IRE_CACHES, which will be later
20823 	 *    flushed when the addresses move over.
20824 	 *
20825 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20826 	 *    addresses, it means either the user has configured them
20827 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20828 	 *    been moved over. For the former, in.mpathd does a failover
20829 	 *    when the interface becomes INACTIVE and hence we should
20830 	 *    not find them. Once INACTIVE is set, we don't allow them
20831 	 *    to create logical interfaces anymore. For the latter, a
20832 	 *    flush will happen when INACTIVE is cleared which will
20833 	 *    flush the IRE_CACHES.
20834 	 *
20835 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20836 	 *    over soon. We potentially select them to create IRE_CACHEs,
20837 	 *    which will be later flushed when the addresses move over.
20838 	 *
20839 	 * NOTE : As ipif_select_source is called to borrow source address
20840 	 * for an ipif that is part of a group, source address selection
20841 	 * will be re-done whenever the group changes i.e either an
20842 	 * insertion/deletion in the group.
20843 	 *
20844 	 * Fill ipif_arr[] with source addresses, using these rules:
20845 	 *
20846 	 *	1. At most one source address from a given ill ends up
20847 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20848 	 *	   associated with a given ill ends up in ipif_arr[].
20849 	 *
20850 	 *	2. If there is at least one non-deprecated ipif in the
20851 	 *	   IPMP group with a source address on the same subnet as
20852 	 *	   our destination, then fill ipif_arr[] only with
20853 	 *	   source addresses on the same subnet as our destination.
20854 	 *	   Note that because of (1), only the first
20855 	 *	   non-deprecated ipif found with a source address
20856 	 *	   matching the destination ends up in ipif_arr[].
20857 	 *
20858 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20859 	 *	   addresses not in the same subnet as our destination.
20860 	 *	   Again, because of (1), only the first off-subnet source
20861 	 *	   address will be chosen.
20862 	 *
20863 	 *	4. If there are no non-deprecated ipifs, then just use
20864 	 *	   the source address associated with the last deprecated
20865 	 *	   one we find that happens to be on the same subnet,
20866 	 *	   otherwise the first one not in the same subnet.
20867 	 */
20868 	specific_found = B_FALSE;
20869 	for (; till != NULL; till = till->ill_group_next) {
20870 		ipif_same_found = B_FALSE;
20871 		ipif_other_found = B_FALSE;
20872 		for (ipif = till->ill_ipif; ipif != NULL;
20873 		    ipif = ipif->ipif_next) {
20874 			if (!IPIF_CAN_LOOKUP(ipif))
20875 				continue;
20876 			/* Always skip NOLOCAL and ANYCAST interfaces */
20877 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20878 				continue;
20879 			if (!(ipif->ipif_flags & IPIF_UP) ||
20880 			    !ipif->ipif_addr_ready)
20881 				continue;
20882 			if (ipif->ipif_zoneid != zoneid &&
20883 			    ipif->ipif_zoneid != ALL_ZONES)
20884 				continue;
20885 			/*
20886 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20887 			 * but are not valid as source addresses.
20888 			 */
20889 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20890 				continue;
20891 
20892 			/*
20893 			 * Check compatibility of local address for
20894 			 * destination's default label if we're on a labeled
20895 			 * system.  Incompatible addresses can't be used at
20896 			 * all.
20897 			 */
20898 			if (dst_rhtp != NULL) {
20899 				boolean_t incompat;
20900 
20901 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20902 				    IPV4_VERSION, B_FALSE);
20903 				if (src_rhtp == NULL)
20904 					continue;
20905 				incompat =
20906 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20907 				    src_rhtp->tpc_tp.tp_doi !=
20908 				    dst_rhtp->tpc_tp.tp_doi ||
20909 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20910 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20911 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20912 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20913 				TPC_RELE(src_rhtp);
20914 				if (incompat)
20915 					continue;
20916 			}
20917 
20918 			/*
20919 			 * We prefer not to use all all-zones addresses, if we
20920 			 * can avoid it, as they pose problems with unlabeled
20921 			 * destinations.
20922 			 */
20923 			if (ipif->ipif_zoneid != ALL_ZONES) {
20924 				if (!specific_found &&
20925 				    (!same_subnet_only ||
20926 				    (ipif->ipif_net_mask & dst) ==
20927 				    ipif->ipif_subnet)) {
20928 					index = 0;
20929 					specific_found = B_TRUE;
20930 					ipif_other_found = B_FALSE;
20931 				}
20932 			} else {
20933 				if (specific_found)
20934 					continue;
20935 			}
20936 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20937 				if (ipif_dep == NULL ||
20938 				    (ipif->ipif_net_mask & dst) ==
20939 				    ipif->ipif_subnet)
20940 					ipif_dep = ipif;
20941 				continue;
20942 			}
20943 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20944 				/* found a source address in the same subnet */
20945 				if (!same_subnet_only) {
20946 					same_subnet_only = B_TRUE;
20947 					index = 0;
20948 				}
20949 				ipif_same_found = B_TRUE;
20950 			} else {
20951 				if (same_subnet_only || ipif_other_found)
20952 					continue;
20953 				ipif_other_found = B_TRUE;
20954 			}
20955 			ipif_arr[index++] = ipif;
20956 			if (index == MAX_IPIF_SELECT_SOURCE) {
20957 				wrapped = B_TRUE;
20958 				index = 0;
20959 			}
20960 			if (ipif_same_found)
20961 				break;
20962 		}
20963 	}
20964 
20965 	if (ipif_arr[0] == NULL) {
20966 		ipif = ipif_dep;
20967 	} else {
20968 		if (wrapped)
20969 			index = MAX_IPIF_SELECT_SOURCE;
20970 		ipif = ipif_arr[ipif_rand(ipst) % index];
20971 		ASSERT(ipif != NULL);
20972 	}
20973 
20974 	if (ipif != NULL) {
20975 		mutex_enter(&ipif->ipif_ill->ill_lock);
20976 		if (!IPIF_CAN_LOOKUP(ipif)) {
20977 			mutex_exit(&ipif->ipif_ill->ill_lock);
20978 			goto retry;
20979 		}
20980 		ipif_refhold_locked(ipif);
20981 		mutex_exit(&ipif->ipif_ill->ill_lock);
20982 	}
20983 
20984 	rw_exit(&ipst->ips_ill_g_lock);
20985 	if (usill != NULL)
20986 		ill_refrele(usill);
20987 	if (dst_rhtp != NULL)
20988 		TPC_RELE(dst_rhtp);
20989 
20990 #ifdef DEBUG
20991 	if (ipif == NULL) {
20992 		char buf1[INET6_ADDRSTRLEN];
20993 
20994 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20995 		    ill->ill_name,
20996 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20997 	} else {
20998 		char buf1[INET6_ADDRSTRLEN];
20999 		char buf2[INET6_ADDRSTRLEN];
21000 
21001 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21002 		    ipif->ipif_ill->ill_name,
21003 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21004 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21005 		    buf2, sizeof (buf2))));
21006 	}
21007 #endif /* DEBUG */
21008 	return (ipif);
21009 }
21010 
21011 
21012 /*
21013  * If old_ipif is not NULL, see if ipif was derived from old
21014  * ipif and if so, recreate the interface route by re-doing
21015  * source address selection. This happens when ipif_down ->
21016  * ipif_update_other_ipifs calls us.
21017  *
21018  * If old_ipif is NULL, just redo the source address selection
21019  * if needed. This happens when illgrp_insert or ipif_up_done
21020  * calls us.
21021  */
21022 static void
21023 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21024 {
21025 	ire_t *ire;
21026 	ire_t *ipif_ire;
21027 	queue_t *stq;
21028 	ipif_t *nipif;
21029 	ill_t *ill;
21030 	boolean_t need_rele = B_FALSE;
21031 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21032 
21033 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21034 	ASSERT(IAM_WRITER_IPIF(ipif));
21035 
21036 	ill = ipif->ipif_ill;
21037 	if (!(ipif->ipif_flags &
21038 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21039 		/*
21040 		 * Can't possibly have borrowed the source
21041 		 * from old_ipif.
21042 		 */
21043 		return;
21044 	}
21045 
21046 	/*
21047 	 * Is there any work to be done? No work if the address
21048 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21049 	 * ipif_select_source() does not borrow addresses from
21050 	 * NOLOCAL and ANYCAST interfaces).
21051 	 */
21052 	if ((old_ipif != NULL) &&
21053 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21054 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21055 	    (old_ipif->ipif_flags &
21056 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21057 		return;
21058 	}
21059 
21060 	/*
21061 	 * Perform the same checks as when creating the
21062 	 * IRE_INTERFACE in ipif_up_done.
21063 	 */
21064 	if (!(ipif->ipif_flags & IPIF_UP))
21065 		return;
21066 
21067 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21068 	    (ipif->ipif_subnet == INADDR_ANY))
21069 		return;
21070 
21071 	ipif_ire = ipif_to_ire(ipif);
21072 	if (ipif_ire == NULL)
21073 		return;
21074 
21075 	/*
21076 	 * We know that ipif uses some other source for its
21077 	 * IRE_INTERFACE. Is it using the source of this
21078 	 * old_ipif?
21079 	 */
21080 	if (old_ipif != NULL &&
21081 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21082 		ire_refrele(ipif_ire);
21083 		return;
21084 	}
21085 	if (ip_debug > 2) {
21086 		/* ip1dbg */
21087 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21088 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21089 	}
21090 
21091 	stq = ipif_ire->ire_stq;
21092 
21093 	/*
21094 	 * Can't use our source address. Select a different
21095 	 * source address for the IRE_INTERFACE.
21096 	 */
21097 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21098 	if (nipif == NULL) {
21099 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21100 		nipif = ipif;
21101 	} else {
21102 		need_rele = B_TRUE;
21103 	}
21104 
21105 	ire = ire_create(
21106 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21107 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21108 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21109 	    NULL,				/* no gateway */
21110 	    &ipif->ipif_mtu,			/* max frag */
21111 	    NULL,				/* no src nce */
21112 	    NULL,				/* no recv from queue */
21113 	    stq,				/* send-to queue */
21114 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21115 	    ipif,
21116 	    0,
21117 	    0,
21118 	    0,
21119 	    0,
21120 	    &ire_uinfo_null,
21121 	    NULL,
21122 	    NULL,
21123 	    ipst);
21124 
21125 	if (ire != NULL) {
21126 		ire_t *ret_ire;
21127 		int error;
21128 
21129 		/*
21130 		 * We don't need ipif_ire anymore. We need to delete
21131 		 * before we add so that ire_add does not detect
21132 		 * duplicates.
21133 		 */
21134 		ire_delete(ipif_ire);
21135 		ret_ire = ire;
21136 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21137 		ASSERT(error == 0);
21138 		ASSERT(ire == ret_ire);
21139 		/* Held in ire_add */
21140 		ire_refrele(ret_ire);
21141 	}
21142 	/*
21143 	 * Either we are falling through from above or could not
21144 	 * allocate a replacement.
21145 	 */
21146 	ire_refrele(ipif_ire);
21147 	if (need_rele)
21148 		ipif_refrele(nipif);
21149 }
21150 
21151 /*
21152  * This old_ipif is going away.
21153  *
21154  * Determine if any other ipif's is using our address as
21155  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21156  * IPIF_DEPRECATED).
21157  * Find the IRE_INTERFACE for such ipifs and recreate them
21158  * to use an different source address following the rules in
21159  * ipif_up_done.
21160  *
21161  * This function takes an illgrp as an argument so that illgrp_delete
21162  * can call this to update source address even after deleting the
21163  * old_ipif->ipif_ill from the ill group.
21164  */
21165 static void
21166 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21167 {
21168 	ipif_t *ipif;
21169 	ill_t *ill;
21170 	char	buf[INET6_ADDRSTRLEN];
21171 
21172 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21173 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21174 
21175 	ill = old_ipif->ipif_ill;
21176 
21177 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21178 	    ill->ill_name,
21179 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21180 	    buf, sizeof (buf))));
21181 	/*
21182 	 * If this part of a group, look at all ills as ipif_select_source
21183 	 * borrows source address across all the ills in the group.
21184 	 */
21185 	if (illgrp != NULL)
21186 		ill = illgrp->illgrp_ill;
21187 
21188 	for (; ill != NULL; ill = ill->ill_group_next) {
21189 		for (ipif = ill->ill_ipif; ipif != NULL;
21190 		    ipif = ipif->ipif_next) {
21191 
21192 			if (ipif == old_ipif)
21193 				continue;
21194 
21195 			ipif_recreate_interface_routes(old_ipif, ipif);
21196 		}
21197 	}
21198 }
21199 
21200 /* ARGSUSED */
21201 int
21202 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21203 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21204 {
21205 	/*
21206 	 * ill_phyint_reinit merged the v4 and v6 into a single
21207 	 * ipsq. Could also have become part of a ipmp group in the
21208 	 * process, and we might not have been able to complete the
21209 	 * operation in ipif_set_values, if we could not become
21210 	 * exclusive.  If so restart it here.
21211 	 */
21212 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21213 }
21214 
21215 
21216 /*
21217  * Can operate on either a module or a driver queue.
21218  * Returns an error if not a module queue.
21219  */
21220 /* ARGSUSED */
21221 int
21222 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21223     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21224 {
21225 	queue_t		*q1 = q;
21226 	char 		*cp;
21227 	char		interf_name[LIFNAMSIZ];
21228 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21229 
21230 	if (q->q_next == NULL) {
21231 		ip1dbg((
21232 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21233 		return (EINVAL);
21234 	}
21235 
21236 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21237 		return (EALREADY);
21238 
21239 	do {
21240 		q1 = q1->q_next;
21241 	} while (q1->q_next);
21242 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21243 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21244 
21245 	/*
21246 	 * Here we are not going to delay the ioack until after
21247 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21248 	 * original ioctl message before sending the requests.
21249 	 */
21250 	return (ipif_set_values(q, mp, interf_name, &ppa));
21251 }
21252 
21253 /* ARGSUSED */
21254 int
21255 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21256     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21257 {
21258 	return (ENXIO);
21259 }
21260 
21261 /*
21262  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21263  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21264  * ire_check_and_create_bcast()).
21265  */
21266 static ire_t **
21267 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21268 {
21269 	ipaddr_t addr;
21270 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21271 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21272 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21273 
21274 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21275 
21276 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21277 
21278 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21279 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21280 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21281 
21282 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21283 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21284 
21285 	/*
21286 	 * For backward compatibility, we create net broadcast IREs based on
21287 	 * the old "IP address class system", since some old machines only
21288 	 * respond to these class derived net broadcast.  However, we must not
21289 	 * create these net broadcast IREs if the subnetmask is shorter than
21290 	 * the IP address class based derived netmask.  Otherwise, we may
21291 	 * create a net broadcast address which is the same as an IP address
21292 	 * on the subnet -- and then TCP will refuse to talk to that address.
21293 	 */
21294 	if (netmask < subnetmask) {
21295 		addr = netmask & ipif->ipif_subnet;
21296 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21297 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21298 		    flags);
21299 	}
21300 
21301 	/*
21302 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21303 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21304 	 * created.  Creating these broadcast IREs will only create confusion
21305 	 * as `addr' will be the same as the IP address.
21306 	 */
21307 	if (subnetmask != 0xFFFFFFFF) {
21308 		addr = ipif->ipif_subnet;
21309 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21310 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21311 		    irep, flags);
21312 	}
21313 
21314 	return (irep);
21315 }
21316 
21317 /*
21318  * Broadcast IRE info structure used in the functions below.  Since we
21319  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21320  */
21321 typedef struct bcast_ireinfo {
21322 	uchar_t		bi_type;	/* BCAST_* value from below */
21323 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21324 			bi_needrep:1,	/* do we need to replace it? */
21325 			bi_haverep:1,	/* have we replaced it? */
21326 			bi_pad:5;
21327 	ipaddr_t	bi_addr;	/* IRE address */
21328 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21329 } bcast_ireinfo_t;
21330 
21331 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21332 
21333 /*
21334  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21335  * return B_TRUE if it should immediately be used to recreate the IRE.
21336  */
21337 static boolean_t
21338 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21339 {
21340 	ipaddr_t addr;
21341 
21342 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21343 
21344 	switch (bireinfop->bi_type) {
21345 	case BCAST_NET:
21346 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21347 		if (addr != bireinfop->bi_addr)
21348 			return (B_FALSE);
21349 		break;
21350 	case BCAST_SUBNET:
21351 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21352 			return (B_FALSE);
21353 		break;
21354 	}
21355 
21356 	bireinfop->bi_needrep = 1;
21357 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21358 		if (bireinfop->bi_backup == NULL)
21359 			bireinfop->bi_backup = ipif;
21360 		return (B_FALSE);
21361 	}
21362 	return (B_TRUE);
21363 }
21364 
21365 /*
21366  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21367  * them ala ire_check_and_create_bcast().
21368  */
21369 static ire_t **
21370 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21371 {
21372 	ipaddr_t mask, addr;
21373 
21374 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21375 
21376 	addr = bireinfop->bi_addr;
21377 	irep = ire_create_bcast(ipif, addr, irep);
21378 
21379 	switch (bireinfop->bi_type) {
21380 	case BCAST_NET:
21381 		mask = ip_net_mask(ipif->ipif_subnet);
21382 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21383 		break;
21384 	case BCAST_SUBNET:
21385 		mask = ipif->ipif_net_mask;
21386 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21387 		break;
21388 	}
21389 
21390 	bireinfop->bi_haverep = 1;
21391 	return (irep);
21392 }
21393 
21394 /*
21395  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21396  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21397  * that are going away are still needed.  If so, have ipif_create_bcast()
21398  * recreate them (except for the deprecated case, as explained below).
21399  */
21400 static ire_t **
21401 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21402     ire_t **irep)
21403 {
21404 	int i;
21405 	ipif_t *ipif;
21406 
21407 	ASSERT(!ill->ill_isv6);
21408 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21409 		/*
21410 		 * Skip this ipif if it's (a) the one being taken down, (b)
21411 		 * not in the same zone, or (c) has no valid local address.
21412 		 */
21413 		if (ipif == test_ipif ||
21414 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21415 		    ipif->ipif_subnet == 0 ||
21416 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21417 		    (IPIF_UP|IPIF_BROADCAST))
21418 			continue;
21419 
21420 		/*
21421 		 * For each dying IRE that hasn't yet been replaced, see if
21422 		 * `ipif' needs it and whether the IRE should be recreated on
21423 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21424 		 * will return B_FALSE even if `ipif' needs the IRE on the
21425 		 * hopes that we'll later find a needy non-deprecated ipif.
21426 		 * However, the ipif is recorded in bi_backup for possible
21427 		 * subsequent use by ipif_check_bcast_ires().
21428 		 */
21429 		for (i = 0; i < BCAST_COUNT; i++) {
21430 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21431 				continue;
21432 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21433 				continue;
21434 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21435 		}
21436 
21437 		/*
21438 		 * If we've replaced all of the broadcast IREs that are going
21439 		 * to be taken down, we know we're done.
21440 		 */
21441 		for (i = 0; i < BCAST_COUNT; i++) {
21442 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21443 				break;
21444 		}
21445 		if (i == BCAST_COUNT)
21446 			break;
21447 	}
21448 	return (irep);
21449 }
21450 
21451 /*
21452  * Check if `test_ipif' (which is going away) is associated with any existing
21453  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21454  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21455  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21456  *
21457  * This is necessary because broadcast IREs are shared.  In particular, a
21458  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21459  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21460  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21461  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21462  * same zone, they will share the same set of broadcast IREs.
21463  *
21464  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21465  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21466  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21467  */
21468 static void
21469 ipif_check_bcast_ires(ipif_t *test_ipif)
21470 {
21471 	ill_t		*ill = test_ipif->ipif_ill;
21472 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21473 	ire_t		**irep1, **irep = &ire_array[0];
21474 	uint_t 		i, willdie;
21475 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21476 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21477 
21478 	ASSERT(!test_ipif->ipif_isv6);
21479 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21480 
21481 	/*
21482 	 * No broadcast IREs for the LOOPBACK interface
21483 	 * or others such as point to point and IPIF_NOXMIT.
21484 	 */
21485 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21486 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21487 		return;
21488 
21489 	bzero(bireinfo, sizeof (bireinfo));
21490 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21491 	bireinfo[0].bi_addr = 0;
21492 
21493 	bireinfo[1].bi_type = BCAST_ALLONES;
21494 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21495 
21496 	bireinfo[2].bi_type = BCAST_NET;
21497 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21498 
21499 	if (test_ipif->ipif_net_mask != 0)
21500 		mask = test_ipif->ipif_net_mask;
21501 	bireinfo[3].bi_type = BCAST_SUBNET;
21502 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21503 
21504 	/*
21505 	 * Figure out what (if any) broadcast IREs will die as a result of
21506 	 * `test_ipif' going away.  If none will die, we're done.
21507 	 */
21508 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21509 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21510 		    test_ipif, ALL_ZONES, NULL,
21511 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21512 		if (ire != NULL) {
21513 			willdie++;
21514 			bireinfo[i].bi_willdie = 1;
21515 			ire_refrele(ire);
21516 		}
21517 	}
21518 
21519 	if (willdie == 0)
21520 		return;
21521 
21522 	/*
21523 	 * Walk through all the ipifs that will be affected by the dying IREs,
21524 	 * and recreate the IREs as necessary.
21525 	 */
21526 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21527 
21528 	/*
21529 	 * Scan through the set of broadcast IREs and see if there are any
21530 	 * that we need to replace that have not yet been replaced.  If so,
21531 	 * replace them using the appropriate backup ipif.
21532 	 */
21533 	for (i = 0; i < BCAST_COUNT; i++) {
21534 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21535 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21536 			    &bireinfo[i], irep);
21537 	}
21538 
21539 	/*
21540 	 * If we can't create all of them, don't add any of them.  (Code in
21541 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21542 	 * non-loopback copy and loopback copy for a given address.)
21543 	 */
21544 	for (irep1 = irep; irep1 > ire_array; ) {
21545 		irep1--;
21546 		if (*irep1 == NULL) {
21547 			ip0dbg(("ipif_check_bcast_ires: can't create "
21548 			    "IRE_BROADCAST, memory allocation failure\n"));
21549 			while (irep > ire_array) {
21550 				irep--;
21551 				if (*irep != NULL)
21552 					ire_delete(*irep);
21553 			}
21554 			return;
21555 		}
21556 	}
21557 
21558 	for (irep1 = irep; irep1 > ire_array; ) {
21559 		irep1--;
21560 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21561 			ire_refrele(*irep1);		/* Held in ire_add */
21562 	}
21563 }
21564 
21565 /*
21566  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21567  * from lifr_flags and the name from lifr_name.
21568  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21569  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21570  * Returns EINPROGRESS when mp has been consumed by queueing it on
21571  * ill_pending_mp and the ioctl will complete in ip_rput.
21572  *
21573  * Can operate on either a module or a driver queue.
21574  * Returns an error if not a module queue.
21575  */
21576 /* ARGSUSED */
21577 int
21578 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21579     ip_ioctl_cmd_t *ipip, void *if_req)
21580 {
21581 	ill_t	*ill = q->q_ptr;
21582 	phyint_t *phyi;
21583 	ip_stack_t *ipst;
21584 	struct lifreq *lifr = if_req;
21585 
21586 	ASSERT(ipif != NULL);
21587 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21588 
21589 	if (q->q_next == NULL) {
21590 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21591 		return (EINVAL);
21592 	}
21593 
21594 	/*
21595 	 * If we are not writer on 'q' then this interface exists already
21596 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21597 	 * so return EALREADY.
21598 	 */
21599 	if (ill != ipif->ipif_ill)
21600 		return (EALREADY);
21601 
21602 	if (ill->ill_name[0] != '\0')
21603 		return (EALREADY);
21604 
21605 	/*
21606 	 * Set all the flags. Allows all kinds of override. Provide some
21607 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21608 	 * unless there is either multicast/broadcast support in the driver
21609 	 * or it is a pt-pt link.
21610 	 */
21611 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21612 		/* Meaningless to IP thus don't allow them to be set. */
21613 		ip1dbg(("ip_setname: EINVAL 1\n"));
21614 		return (EINVAL);
21615 	}
21616 
21617 	/*
21618 	 * If there's another ill already with the requested name, ensure
21619 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21620 	 * fuse together two unrelated ills, which will cause chaos.
21621 	 */
21622 	ipst = ill->ill_ipst;
21623 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21624 	    lifr->lifr_name, NULL);
21625 	if (phyi != NULL) {
21626 		ill_t *ill_mate = phyi->phyint_illv4;
21627 
21628 		if (ill_mate == NULL)
21629 			ill_mate = phyi->phyint_illv6;
21630 		ASSERT(ill_mate != NULL);
21631 
21632 		if (ill_mate->ill_media->ip_m_mac_type !=
21633 		    ill->ill_media->ip_m_mac_type) {
21634 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21635 			    "use the same ill name on differing media\n"));
21636 			return (EINVAL);
21637 		}
21638 	}
21639 
21640 	/*
21641 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21642 	 * ill_bcast_addr_length info.
21643 	 */
21644 	if (!ill->ill_needs_attach &&
21645 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21646 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21647 	    ill->ill_bcast_addr_length == 0)) {
21648 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21649 		ip1dbg(("ip_setname: EINVAL 2\n"));
21650 		return (EINVAL);
21651 	}
21652 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21653 	    ((lifr->lifr_flags & IFF_IPV6) ||
21654 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21655 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21656 		ip1dbg(("ip_setname: EINVAL 3\n"));
21657 		return (EINVAL);
21658 	}
21659 	if (lifr->lifr_flags & IFF_UP) {
21660 		/* Can only be set with SIOCSLIFFLAGS */
21661 		ip1dbg(("ip_setname: EINVAL 4\n"));
21662 		return (EINVAL);
21663 	}
21664 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21665 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21666 		ip1dbg(("ip_setname: EINVAL 5\n"));
21667 		return (EINVAL);
21668 	}
21669 	/*
21670 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21671 	 */
21672 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21673 	    !(lifr->lifr_flags & IFF_IPV6) &&
21674 	    !(ipif->ipif_isv6)) {
21675 		ip1dbg(("ip_setname: EINVAL 6\n"));
21676 		return (EINVAL);
21677 	}
21678 
21679 	/*
21680 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21681 	 * we have all the flags here. So, we assign rather than we OR.
21682 	 * We can't OR the flags here because we don't want to set
21683 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21684 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21685 	 * on lifr_flags value here.
21686 	 */
21687 	/*
21688 	 * This ill has not been inserted into the global list.
21689 	 * So we are still single threaded and don't need any lock
21690 	 */
21691 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21692 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21693 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21694 
21695 	/* We started off as V4. */
21696 	if (ill->ill_flags & ILLF_IPV6) {
21697 		ill->ill_phyint->phyint_illv6 = ill;
21698 		ill->ill_phyint->phyint_illv4 = NULL;
21699 	}
21700 
21701 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21702 }
21703 
21704 /* ARGSUSED */
21705 int
21706 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21707     ip_ioctl_cmd_t *ipip, void *if_req)
21708 {
21709 	/*
21710 	 * ill_phyint_reinit merged the v4 and v6 into a single
21711 	 * ipsq. Could also have become part of a ipmp group in the
21712 	 * process, and we might not have been able to complete the
21713 	 * slifname in ipif_set_values, if we could not become
21714 	 * exclusive.  If so restart it here
21715 	 */
21716 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21717 }
21718 
21719 /*
21720  * Return a pointer to the ipif which matches the index, IP version type and
21721  * zoneid.
21722  */
21723 ipif_t *
21724 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21725     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21726 {
21727 	ill_t	*ill;
21728 	ipif_t	*ipif = NULL;
21729 
21730 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21731 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21732 
21733 	if (err != NULL)
21734 		*err = 0;
21735 
21736 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21737 	if (ill != NULL) {
21738 		mutex_enter(&ill->ill_lock);
21739 		for (ipif = ill->ill_ipif; ipif != NULL;
21740 		    ipif = ipif->ipif_next) {
21741 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21742 			    zoneid == ipif->ipif_zoneid ||
21743 			    ipif->ipif_zoneid == ALL_ZONES)) {
21744 				ipif_refhold_locked(ipif);
21745 				break;
21746 			}
21747 		}
21748 		mutex_exit(&ill->ill_lock);
21749 		ill_refrele(ill);
21750 		if (ipif == NULL && err != NULL)
21751 			*err = ENXIO;
21752 	}
21753 	return (ipif);
21754 }
21755 
21756 typedef struct conn_change_s {
21757 	uint_t cc_old_ifindex;
21758 	uint_t cc_new_ifindex;
21759 } conn_change_t;
21760 
21761 /*
21762  * ipcl_walk function for changing interface index.
21763  */
21764 static void
21765 conn_change_ifindex(conn_t *connp, caddr_t arg)
21766 {
21767 	conn_change_t *connc;
21768 	uint_t old_ifindex;
21769 	uint_t new_ifindex;
21770 	int i;
21771 	ilg_t *ilg;
21772 
21773 	connc = (conn_change_t *)arg;
21774 	old_ifindex = connc->cc_old_ifindex;
21775 	new_ifindex = connc->cc_new_ifindex;
21776 
21777 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21778 		connp->conn_orig_bound_ifindex = new_ifindex;
21779 
21780 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21781 		connp->conn_orig_multicast_ifindex = new_ifindex;
21782 
21783 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21784 		ilg = &connp->conn_ilg[i];
21785 		if (ilg->ilg_orig_ifindex == old_ifindex)
21786 			ilg->ilg_orig_ifindex = new_ifindex;
21787 	}
21788 }
21789 
21790 /*
21791  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21792  * to new_index if it matches the old_index.
21793  *
21794  * Failovers typically happen within a group of ills. But somebody
21795  * can remove an ill from the group after a failover happened. If
21796  * we are setting the ifindex after this, we potentially need to
21797  * look at all the ills rather than just the ones in the group.
21798  * We cut down the work by looking at matching ill_net_types
21799  * and ill_types as we could not possibly grouped them together.
21800  */
21801 static void
21802 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21803 {
21804 	ill_t *ill;
21805 	ipif_t *ipif;
21806 	uint_t old_ifindex;
21807 	uint_t new_ifindex;
21808 	ilm_t *ilm;
21809 	ill_walk_context_t ctx;
21810 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21811 
21812 	old_ifindex = connc->cc_old_ifindex;
21813 	new_ifindex = connc->cc_new_ifindex;
21814 
21815 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21816 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21817 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21818 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21819 		    (ill_orig->ill_type != ill->ill_type)) {
21820 			continue;
21821 		}
21822 		for (ipif = ill->ill_ipif; ipif != NULL;
21823 		    ipif = ipif->ipif_next) {
21824 			if (ipif->ipif_orig_ifindex == old_ifindex)
21825 				ipif->ipif_orig_ifindex = new_ifindex;
21826 		}
21827 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21828 			if (ilm->ilm_orig_ifindex == old_ifindex)
21829 				ilm->ilm_orig_ifindex = new_ifindex;
21830 		}
21831 	}
21832 	rw_exit(&ipst->ips_ill_g_lock);
21833 }
21834 
21835 /*
21836  * We first need to ensure that the new index is unique, and
21837  * then carry the change across both v4 and v6 ill representation
21838  * of the physical interface.
21839  */
21840 /* ARGSUSED */
21841 int
21842 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21843     ip_ioctl_cmd_t *ipip, void *ifreq)
21844 {
21845 	ill_t		*ill;
21846 	ill_t		*ill_other;
21847 	phyint_t	*phyi;
21848 	int		old_index;
21849 	conn_change_t	connc;
21850 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21851 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21852 	uint_t	index;
21853 	ill_t	*ill_v4;
21854 	ill_t	*ill_v6;
21855 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21856 
21857 	if (ipip->ipi_cmd_type == IF_CMD)
21858 		index = ifr->ifr_index;
21859 	else
21860 		index = lifr->lifr_index;
21861 
21862 	/*
21863 	 * Only allow on physical interface. Also, index zero is illegal.
21864 	 *
21865 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21866 	 *
21867 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21868 	 *    implies a possible failback might have to happen. As failback
21869 	 *    depends on the old index, we should fail setting the index.
21870 	 *
21871 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21872 	 *    any addresses or multicast memberships are failed over to
21873 	 *    a non-STANDBY interface. As failback depends on the old
21874 	 *    index, we should fail setting the index for this case also.
21875 	 *
21876 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21877 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21878 	 */
21879 	ill = ipif->ipif_ill;
21880 	phyi = ill->ill_phyint;
21881 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21882 	    ipif->ipif_id != 0 || index == 0) {
21883 		return (EINVAL);
21884 	}
21885 	old_index = phyi->phyint_ifindex;
21886 
21887 	/* If the index is not changing, no work to do */
21888 	if (old_index == index)
21889 		return (0);
21890 
21891 	/*
21892 	 * Use ill_lookup_on_ifindex to determine if the
21893 	 * new index is unused and if so allow the change.
21894 	 */
21895 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21896 	    ipst);
21897 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21898 	    ipst);
21899 	if (ill_v6 != NULL || ill_v4 != NULL) {
21900 		if (ill_v4 != NULL)
21901 			ill_refrele(ill_v4);
21902 		if (ill_v6 != NULL)
21903 			ill_refrele(ill_v6);
21904 		return (EBUSY);
21905 	}
21906 
21907 	/*
21908 	 * The new index is unused. Set it in the phyint.
21909 	 * Locate the other ill so that we can send a routing
21910 	 * sockets message.
21911 	 */
21912 	if (ill->ill_isv6) {
21913 		ill_other = phyi->phyint_illv4;
21914 	} else {
21915 		ill_other = phyi->phyint_illv6;
21916 	}
21917 
21918 	phyi->phyint_ifindex = index;
21919 
21920 	/* Update SCTP's ILL list */
21921 	sctp_ill_reindex(ill, old_index);
21922 
21923 	connc.cc_old_ifindex = old_index;
21924 	connc.cc_new_ifindex = index;
21925 	ip_change_ifindex(ill, &connc);
21926 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21927 
21928 	/* Send the routing sockets message */
21929 	ip_rts_ifmsg(ipif);
21930 	if (ill_other != NULL)
21931 		ip_rts_ifmsg(ill_other->ill_ipif);
21932 
21933 	return (0);
21934 }
21935 
21936 /* ARGSUSED */
21937 int
21938 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21939     ip_ioctl_cmd_t *ipip, void *ifreq)
21940 {
21941 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21942 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21943 
21944 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21945 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21946 	/* Get the interface index */
21947 	if (ipip->ipi_cmd_type == IF_CMD) {
21948 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21949 	} else {
21950 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21951 	}
21952 	return (0);
21953 }
21954 
21955 /* ARGSUSED */
21956 int
21957 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21958     ip_ioctl_cmd_t *ipip, void *ifreq)
21959 {
21960 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21961 
21962 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21963 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21964 	/* Get the interface zone */
21965 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21966 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21967 	return (0);
21968 }
21969 
21970 /*
21971  * Set the zoneid of an interface.
21972  */
21973 /* ARGSUSED */
21974 int
21975 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21976     ip_ioctl_cmd_t *ipip, void *ifreq)
21977 {
21978 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21979 	int err = 0;
21980 	boolean_t need_up = B_FALSE;
21981 	zone_t *zptr;
21982 	zone_status_t status;
21983 	zoneid_t zoneid;
21984 
21985 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21986 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21987 		if (!is_system_labeled())
21988 			return (ENOTSUP);
21989 		zoneid = GLOBAL_ZONEID;
21990 	}
21991 
21992 	/* cannot assign instance zero to a non-global zone */
21993 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21994 		return (ENOTSUP);
21995 
21996 	/*
21997 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21998 	 * the event of a race with the zone shutdown processing, since IP
21999 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22000 	 * interface will be cleaned up even if the zone is shut down
22001 	 * immediately after the status check. If the interface can't be brought
22002 	 * down right away, and the zone is shut down before the restart
22003 	 * function is called, we resolve the possible races by rechecking the
22004 	 * zone status in the restart function.
22005 	 */
22006 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22007 		return (EINVAL);
22008 	status = zone_status_get(zptr);
22009 	zone_rele(zptr);
22010 
22011 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22012 		return (EINVAL);
22013 
22014 	if (ipif->ipif_flags & IPIF_UP) {
22015 		/*
22016 		 * If the interface is already marked up,
22017 		 * we call ipif_down which will take care
22018 		 * of ditching any IREs that have been set
22019 		 * up based on the old interface address.
22020 		 */
22021 		err = ipif_logical_down(ipif, q, mp);
22022 		if (err == EINPROGRESS)
22023 			return (err);
22024 		ipif_down_tail(ipif);
22025 		need_up = B_TRUE;
22026 	}
22027 
22028 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22029 	return (err);
22030 }
22031 
22032 static int
22033 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22034     queue_t *q, mblk_t *mp, boolean_t need_up)
22035 {
22036 	int	err = 0;
22037 	ip_stack_t	*ipst;
22038 
22039 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22040 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22041 
22042 	if (CONN_Q(q))
22043 		ipst = CONNQ_TO_IPST(q);
22044 	else
22045 		ipst = ILLQ_TO_IPST(q);
22046 
22047 	/*
22048 	 * For exclusive stacks we don't allow a different zoneid than
22049 	 * global.
22050 	 */
22051 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22052 	    zoneid != GLOBAL_ZONEID)
22053 		return (EINVAL);
22054 
22055 	/* Set the new zone id. */
22056 	ipif->ipif_zoneid = zoneid;
22057 
22058 	/* Update sctp list */
22059 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22060 
22061 	if (need_up) {
22062 		/*
22063 		 * Now bring the interface back up.  If this
22064 		 * is the only IPIF for the ILL, ipif_up
22065 		 * will have to re-bind to the device, so
22066 		 * we may get back EINPROGRESS, in which
22067 		 * case, this IOCTL will get completed in
22068 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22069 		 */
22070 		err = ipif_up(ipif, q, mp);
22071 	}
22072 	return (err);
22073 }
22074 
22075 /* ARGSUSED */
22076 int
22077 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22078     ip_ioctl_cmd_t *ipip, void *if_req)
22079 {
22080 	struct lifreq *lifr = (struct lifreq *)if_req;
22081 	zoneid_t zoneid;
22082 	zone_t *zptr;
22083 	zone_status_t status;
22084 
22085 	ASSERT(ipif->ipif_id != 0);
22086 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22087 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22088 		zoneid = GLOBAL_ZONEID;
22089 
22090 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22091 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22092 
22093 	/*
22094 	 * We recheck the zone status to resolve the following race condition:
22095 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22096 	 * 2) hme0:1 is up and can't be brought down right away;
22097 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22098 	 * 3) zone "myzone" is halted; the zone status switches to
22099 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22100 	 * the interfaces to remove - hme0:1 is not returned because it's not
22101 	 * yet in "myzone", so it won't be removed;
22102 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22103 	 * status check here, we would have hme0:1 in "myzone" after it's been
22104 	 * destroyed.
22105 	 * Note that if the status check fails, we need to bring the interface
22106 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22107 	 * ipif_up_done[_v6]().
22108 	 */
22109 	status = ZONE_IS_UNINITIALIZED;
22110 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22111 		status = zone_status_get(zptr);
22112 		zone_rele(zptr);
22113 	}
22114 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22115 		if (ipif->ipif_isv6) {
22116 			(void) ipif_up_done_v6(ipif);
22117 		} else {
22118 			(void) ipif_up_done(ipif);
22119 		}
22120 		return (EINVAL);
22121 	}
22122 
22123 	ipif_down_tail(ipif);
22124 
22125 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22126 	    B_TRUE));
22127 }
22128 
22129 /* ARGSUSED */
22130 int
22131 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22132 	ip_ioctl_cmd_t *ipip, void *ifreq)
22133 {
22134 	struct lifreq	*lifr = ifreq;
22135 
22136 	ASSERT(q->q_next == NULL);
22137 	ASSERT(CONN_Q(q));
22138 
22139 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22140 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22141 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22142 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22143 
22144 	return (0);
22145 }
22146 
22147 
22148 /* Find the previous ILL in this usesrc group */
22149 static ill_t *
22150 ill_prev_usesrc(ill_t *uill)
22151 {
22152 	ill_t *ill;
22153 
22154 	for (ill = uill->ill_usesrc_grp_next;
22155 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22156 	    ill = ill->ill_usesrc_grp_next)
22157 		/* do nothing */;
22158 	return (ill);
22159 }
22160 
22161 /*
22162  * Release all members of the usesrc group. This routine is called
22163  * from ill_delete when the interface being unplumbed is the
22164  * group head.
22165  */
22166 static void
22167 ill_disband_usesrc_group(ill_t *uill)
22168 {
22169 	ill_t *next_ill, *tmp_ill;
22170 	ip_stack_t	*ipst = uill->ill_ipst;
22171 
22172 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22173 	next_ill = uill->ill_usesrc_grp_next;
22174 
22175 	do {
22176 		ASSERT(next_ill != NULL);
22177 		tmp_ill = next_ill->ill_usesrc_grp_next;
22178 		ASSERT(tmp_ill != NULL);
22179 		next_ill->ill_usesrc_grp_next = NULL;
22180 		next_ill->ill_usesrc_ifindex = 0;
22181 		next_ill = tmp_ill;
22182 	} while (next_ill->ill_usesrc_ifindex != 0);
22183 	uill->ill_usesrc_grp_next = NULL;
22184 }
22185 
22186 /*
22187  * Remove the client usesrc ILL from the list and relink to a new list
22188  */
22189 int
22190 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22191 {
22192 	ill_t *ill, *tmp_ill;
22193 	ip_stack_t	*ipst = ucill->ill_ipst;
22194 
22195 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22196 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22197 
22198 	/*
22199 	 * Check if the usesrc client ILL passed in is not already
22200 	 * in use as a usesrc ILL i.e one whose source address is
22201 	 * in use OR a usesrc ILL is not already in use as a usesrc
22202 	 * client ILL
22203 	 */
22204 	if ((ucill->ill_usesrc_ifindex == 0) ||
22205 	    (uill->ill_usesrc_ifindex != 0)) {
22206 		return (-1);
22207 	}
22208 
22209 	ill = ill_prev_usesrc(ucill);
22210 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22211 
22212 	/* Remove from the current list */
22213 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22214 		/* Only two elements in the list */
22215 		ASSERT(ill->ill_usesrc_ifindex == 0);
22216 		ill->ill_usesrc_grp_next = NULL;
22217 	} else {
22218 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22219 	}
22220 
22221 	if (ifindex == 0) {
22222 		ucill->ill_usesrc_ifindex = 0;
22223 		ucill->ill_usesrc_grp_next = NULL;
22224 		return (0);
22225 	}
22226 
22227 	ucill->ill_usesrc_ifindex = ifindex;
22228 	tmp_ill = uill->ill_usesrc_grp_next;
22229 	uill->ill_usesrc_grp_next = ucill;
22230 	ucill->ill_usesrc_grp_next =
22231 	    (tmp_ill != NULL) ? tmp_ill : uill;
22232 	return (0);
22233 }
22234 
22235 /*
22236  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22237  * ip.c for locking details.
22238  */
22239 /* ARGSUSED */
22240 int
22241 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22242     ip_ioctl_cmd_t *ipip, void *ifreq)
22243 {
22244 	struct lifreq *lifr = (struct lifreq *)ifreq;
22245 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22246 	    ill_flag_changed = B_FALSE;
22247 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22248 	int err = 0, ret;
22249 	uint_t ifindex;
22250 	phyint_t *us_phyint, *us_cli_phyint;
22251 	ipsq_t *ipsq = NULL;
22252 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22253 
22254 	ASSERT(IAM_WRITER_IPIF(ipif));
22255 	ASSERT(q->q_next == NULL);
22256 	ASSERT(CONN_Q(q));
22257 
22258 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22259 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22260 
22261 	ASSERT(us_cli_phyint != NULL);
22262 
22263 	/*
22264 	 * If the client ILL is being used for IPMP, abort.
22265 	 * Note, this can be done before ipsq_try_enter since we are already
22266 	 * exclusive on this ILL
22267 	 */
22268 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22269 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22270 		return (EINVAL);
22271 	}
22272 
22273 	ifindex = lifr->lifr_index;
22274 	if (ifindex == 0) {
22275 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22276 			/* non usesrc group interface, nothing to reset */
22277 			return (0);
22278 		}
22279 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22280 		/* valid reset request */
22281 		reset_flg = B_TRUE;
22282 	}
22283 
22284 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22285 	    ip_process_ioctl, &err, ipst);
22286 
22287 	if (usesrc_ill == NULL) {
22288 		return (err);
22289 	}
22290 
22291 	/*
22292 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22293 	 * group nor can either of the interfaces be used for standy. So
22294 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22295 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22296 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22297 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22298 	 * the usesrc_cli_ill
22299 	 */
22300 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22301 	    NEW_OP, B_TRUE);
22302 	if (ipsq == NULL) {
22303 		err = EINPROGRESS;
22304 		/* Operation enqueued on the ipsq of the usesrc ILL */
22305 		goto done;
22306 	}
22307 
22308 	/* Check if the usesrc_ill is used for IPMP */
22309 	us_phyint = usesrc_ill->ill_phyint;
22310 	if ((us_phyint->phyint_groupname != NULL) ||
22311 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22312 		err = EINVAL;
22313 		goto done;
22314 	}
22315 
22316 	/*
22317 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22318 	 * already a client then return EINVAL
22319 	 */
22320 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22321 		err = EINVAL;
22322 		goto done;
22323 	}
22324 
22325 	/*
22326 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22327 	 * be then this is a duplicate operation.
22328 	 */
22329 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22330 		err = 0;
22331 		goto done;
22332 	}
22333 
22334 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22335 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22336 	    usesrc_ill->ill_isv6));
22337 
22338 	/*
22339 	 * The next step ensures that no new ires will be created referencing
22340 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22341 	 * we go through an ire walk deleting all ire caches that reference
22342 	 * the client ill. New ires referencing the client ill that are added
22343 	 * to the ire table before the ILL_CHANGING flag is set, will be
22344 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22345 	 * the client ill while the ILL_CHANGING flag is set will be failed
22346 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22347 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22348 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22349 	 * belong to the same usesrc group.
22350 	 */
22351 	mutex_enter(&usesrc_cli_ill->ill_lock);
22352 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22353 	mutex_exit(&usesrc_cli_ill->ill_lock);
22354 	ill_flag_changed = B_TRUE;
22355 
22356 	if (ipif->ipif_isv6)
22357 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22358 		    ALL_ZONES, ipst);
22359 	else
22360 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22361 		    ALL_ZONES, ipst);
22362 
22363 	/*
22364 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22365 	 * and the ill_usesrc_ifindex fields
22366 	 */
22367 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22368 
22369 	if (reset_flg) {
22370 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22371 		if (ret != 0) {
22372 			err = EINVAL;
22373 		}
22374 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22375 		goto done;
22376 	}
22377 
22378 	/*
22379 	 * Four possibilities to consider:
22380 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22381 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22382 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22383 	 * 4. Both are part of their respective usesrc groups
22384 	 */
22385 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22386 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22387 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22388 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22389 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22390 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22391 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22392 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22393 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22394 		/* Insert at head of list */
22395 		usesrc_cli_ill->ill_usesrc_grp_next =
22396 		    usesrc_ill->ill_usesrc_grp_next;
22397 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22398 	} else {
22399 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22400 		    ifindex);
22401 		if (ret != 0)
22402 			err = EINVAL;
22403 	}
22404 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22405 
22406 done:
22407 	if (ill_flag_changed) {
22408 		mutex_enter(&usesrc_cli_ill->ill_lock);
22409 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22410 		mutex_exit(&usesrc_cli_ill->ill_lock);
22411 	}
22412 	if (ipsq != NULL)
22413 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22414 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22415 	ill_refrele(usesrc_ill);
22416 	return (err);
22417 }
22418 
22419 /*
22420  * comparison function used by avl.
22421  */
22422 static int
22423 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22424 {
22425 
22426 	uint_t index;
22427 
22428 	ASSERT(phyip != NULL && index_ptr != NULL);
22429 
22430 	index = *((uint_t *)index_ptr);
22431 	/*
22432 	 * let the phyint with the lowest index be on top.
22433 	 */
22434 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22435 		return (1);
22436 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22437 		return (-1);
22438 	return (0);
22439 }
22440 
22441 /*
22442  * comparison function used by avl.
22443  */
22444 static int
22445 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22446 {
22447 	ill_t *ill;
22448 	int res = 0;
22449 
22450 	ASSERT(phyip != NULL && name_ptr != NULL);
22451 
22452 	if (((phyint_t *)phyip)->phyint_illv4)
22453 		ill = ((phyint_t *)phyip)->phyint_illv4;
22454 	else
22455 		ill = ((phyint_t *)phyip)->phyint_illv6;
22456 	ASSERT(ill != NULL);
22457 
22458 	res = strcmp(ill->ill_name, (char *)name_ptr);
22459 	if (res > 0)
22460 		return (1);
22461 	else if (res < 0)
22462 		return (-1);
22463 	return (0);
22464 }
22465 /*
22466  * This function is called from ill_delete when the ill is being
22467  * unplumbed. We remove the reference from the phyint and we also
22468  * free the phyint when there are no more references to it.
22469  */
22470 static void
22471 ill_phyint_free(ill_t *ill)
22472 {
22473 	phyint_t *phyi;
22474 	phyint_t *next_phyint;
22475 	ipsq_t *cur_ipsq;
22476 	ip_stack_t	*ipst = ill->ill_ipst;
22477 
22478 	ASSERT(ill->ill_phyint != NULL);
22479 
22480 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22481 	phyi = ill->ill_phyint;
22482 	ill->ill_phyint = NULL;
22483 	/*
22484 	 * ill_init allocates a phyint always to store the copy
22485 	 * of flags relevant to phyint. At that point in time, we could
22486 	 * not assign the name and hence phyint_illv4/v6 could not be
22487 	 * initialized. Later in ipif_set_values, we assign the name to
22488 	 * the ill, at which point in time we assign phyint_illv4/v6.
22489 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22490 	 */
22491 	if (ill->ill_flags & ILLF_IPV6) {
22492 		phyi->phyint_illv6 = NULL;
22493 	} else {
22494 		phyi->phyint_illv4 = NULL;
22495 	}
22496 	/*
22497 	 * ipif_down removes it from the group when the last ipif goes
22498 	 * down.
22499 	 */
22500 	ASSERT(ill->ill_group == NULL);
22501 
22502 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22503 		return;
22504 
22505 	/*
22506 	 * Make sure this phyint was put in the list.
22507 	 */
22508 	if (phyi->phyint_ifindex > 0) {
22509 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22510 		    phyi);
22511 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22512 		    phyi);
22513 	}
22514 	/*
22515 	 * remove phyint from the ipsq list.
22516 	 */
22517 	cur_ipsq = phyi->phyint_ipsq;
22518 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22519 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22520 	} else {
22521 		next_phyint = cur_ipsq->ipsq_phyint_list;
22522 		while (next_phyint != NULL) {
22523 			if (next_phyint->phyint_ipsq_next == phyi) {
22524 				next_phyint->phyint_ipsq_next =
22525 				    phyi->phyint_ipsq_next;
22526 				break;
22527 			}
22528 			next_phyint = next_phyint->phyint_ipsq_next;
22529 		}
22530 		ASSERT(next_phyint != NULL);
22531 	}
22532 	IPSQ_DEC_REF(cur_ipsq, ipst);
22533 
22534 	if (phyi->phyint_groupname_len != 0) {
22535 		ASSERT(phyi->phyint_groupname != NULL);
22536 		mi_free(phyi->phyint_groupname);
22537 	}
22538 	mi_free(phyi);
22539 }
22540 
22541 /*
22542  * Attach the ill to the phyint structure which can be shared by both
22543  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22544  * function is called from ipif_set_values and ill_lookup_on_name (for
22545  * loopback) where we know the name of the ill. We lookup the ill and if
22546  * there is one present already with the name use that phyint. Otherwise
22547  * reuse the one allocated by ill_init.
22548  */
22549 static void
22550 ill_phyint_reinit(ill_t *ill)
22551 {
22552 	boolean_t isv6 = ill->ill_isv6;
22553 	phyint_t *phyi_old;
22554 	phyint_t *phyi;
22555 	avl_index_t where = 0;
22556 	ill_t	*ill_other = NULL;
22557 	ipsq_t	*ipsq;
22558 	ip_stack_t	*ipst = ill->ill_ipst;
22559 
22560 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22561 
22562 	phyi_old = ill->ill_phyint;
22563 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22564 	    phyi_old->phyint_illv6 == NULL));
22565 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22566 	    phyi_old->phyint_illv4 == NULL));
22567 	ASSERT(phyi_old->phyint_ifindex == 0);
22568 
22569 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22570 	    ill->ill_name, &where);
22571 
22572 	/*
22573 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22574 	 *    the global list of ills. So no other thread could have located
22575 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22576 	 * 2. Now locate the other protocol instance of this ill.
22577 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22578 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22579 	 *    of neither ill can change.
22580 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22581 	 *    other ill.
22582 	 * 5. Release all locks.
22583 	 */
22584 
22585 	/*
22586 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22587 	 * we are initializing IPv4.
22588 	 */
22589 	if (phyi != NULL) {
22590 		ill_other = (isv6) ? phyi->phyint_illv4 :
22591 		    phyi->phyint_illv6;
22592 		ASSERT(ill_other->ill_phyint != NULL);
22593 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22594 		    (!isv6 && ill_other->ill_isv6));
22595 		GRAB_ILL_LOCKS(ill, ill_other);
22596 		/*
22597 		 * We are potentially throwing away phyint_flags which
22598 		 * could be different from the one that we obtain from
22599 		 * ill_other->ill_phyint. But it is okay as we are assuming
22600 		 * that the state maintained within IP is correct.
22601 		 */
22602 		mutex_enter(&phyi->phyint_lock);
22603 		if (isv6) {
22604 			ASSERT(phyi->phyint_illv6 == NULL);
22605 			phyi->phyint_illv6 = ill;
22606 		} else {
22607 			ASSERT(phyi->phyint_illv4 == NULL);
22608 			phyi->phyint_illv4 = ill;
22609 		}
22610 		/*
22611 		 * This is a new ill, currently undergoing SLIFNAME
22612 		 * So we could not have joined an IPMP group until now.
22613 		 */
22614 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22615 		    phyi_old->phyint_groupname == NULL);
22616 
22617 		/*
22618 		 * This phyi_old is going away. Decref ipsq_refs and
22619 		 * assert it is zero. The ipsq itself will be freed in
22620 		 * ipsq_exit
22621 		 */
22622 		ipsq = phyi_old->phyint_ipsq;
22623 		IPSQ_DEC_REF(ipsq, ipst);
22624 		ASSERT(ipsq->ipsq_refs == 0);
22625 		/* Get the singleton phyint out of the ipsq list */
22626 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22627 		ipsq->ipsq_phyint_list = NULL;
22628 		phyi_old->phyint_illv4 = NULL;
22629 		phyi_old->phyint_illv6 = NULL;
22630 		mi_free(phyi_old);
22631 	} else {
22632 		mutex_enter(&ill->ill_lock);
22633 		/*
22634 		 * We don't need to acquire any lock, since
22635 		 * the ill is not yet visible globally  and we
22636 		 * have not yet released the ill_g_lock.
22637 		 */
22638 		phyi = phyi_old;
22639 		mutex_enter(&phyi->phyint_lock);
22640 		/* XXX We need a recovery strategy here. */
22641 		if (!phyint_assign_ifindex(phyi, ipst))
22642 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22643 
22644 		/* No IPMP group yet, thus the hook uses the ifindex */
22645 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22646 
22647 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22648 		    (void *)phyi, where);
22649 
22650 		(void) avl_find(&ipst->ips_phyint_g_list->
22651 		    phyint_list_avl_by_index,
22652 		    &phyi->phyint_ifindex, &where);
22653 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22654 		    (void *)phyi, where);
22655 	}
22656 
22657 	/*
22658 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22659 	 * pending mp is not affected because that is per ill basis.
22660 	 */
22661 	ill->ill_phyint = phyi;
22662 
22663 	/*
22664 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22665 	 * We do this here as when the first ipif was allocated,
22666 	 * ipif_allocate does not know the right interface index.
22667 	 */
22668 
22669 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22670 	/*
22671 	 * Now that the phyint's ifindex has been assigned, complete the
22672 	 * remaining
22673 	 */
22674 
22675 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22676 	if (ill->ill_isv6) {
22677 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22678 		    ill->ill_phyint->phyint_ifindex;
22679 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22680 	} else {
22681 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22682 	}
22683 
22684 	/*
22685 	 * Generate an event within the hooks framework to indicate that
22686 	 * a new interface has just been added to IP.  For this event to
22687 	 * be generated, the network interface must, at least, have an
22688 	 * ifindex assigned to it.
22689 	 *
22690 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22691 	 * that the ordering of delivered events to listeners matches the
22692 	 * order of them in the kernel.
22693 	 *
22694 	 * This function could be called from ill_lookup_on_name. In that case
22695 	 * the interface is loopback "lo", which will not generate a NIC event.
22696 	 */
22697 	if (ill->ill_name_length <= 2 ||
22698 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22699 		/*
22700 		 * Generate nic plumb event for ill_name even if
22701 		 * ipmp_hook_emulation is set. That avoids generating events
22702 		 * for the ill_names should ipmp_hook_emulation be turned on
22703 		 * later.
22704 		 */
22705 		ill_nic_info_plumb(ill, B_FALSE);
22706 	}
22707 	RELEASE_ILL_LOCKS(ill, ill_other);
22708 	mutex_exit(&phyi->phyint_lock);
22709 }
22710 
22711 /*
22712  * Allocate a NE_PLUMB nic info event and store in the ill.
22713  * If 'group' is set we do it for the group name, otherwise the ill name.
22714  * It will be sent when we leave the ipsq.
22715  */
22716 void
22717 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22718 {
22719 	phyint_t	*phyi = ill->ill_phyint;
22720 	char		*name;
22721 	int		namelen;
22722 
22723 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22724 
22725 	if (group) {
22726 		ASSERT(phyi->phyint_groupname_len != 0);
22727 		namelen = phyi->phyint_groupname_len;
22728 		name = phyi->phyint_groupname;
22729 	} else {
22730 		namelen = ill->ill_name_length;
22731 		name = ill->ill_name;
22732 	}
22733 
22734 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22735 }
22736 
22737 /*
22738  * Unhook the nic event message from the ill and enqueue it
22739  * into the nic event taskq.
22740  */
22741 void
22742 ill_nic_info_dispatch(ill_t *ill)
22743 {
22744 	hook_nic_event_t *info;
22745 
22746 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22747 
22748 	if ((info = ill->ill_nic_event_info) != NULL) {
22749 		if (ddi_taskq_dispatch(eventq_queue_nic,
22750 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22751 			ip2dbg(("ill_nic_info_dispatch: "
22752 			    "ddi_taskq_dispatch failed\n"));
22753 			if (info->hne_data != NULL)
22754 				kmem_free(info->hne_data, info->hne_datalen);
22755 			kmem_free(info, sizeof (hook_nic_event_t));
22756 		}
22757 		ill->ill_nic_event_info = NULL;
22758 	}
22759 }
22760 
22761 /*
22762  * Notify any downstream modules of the name of this interface.
22763  * An M_IOCTL is used even though we don't expect a successful reply.
22764  * Any reply message from the driver (presumably an M_IOCNAK) will
22765  * eventually get discarded somewhere upstream.  The message format is
22766  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22767  * to IP.
22768  */
22769 static void
22770 ip_ifname_notify(ill_t *ill, queue_t *q)
22771 {
22772 	mblk_t *mp1, *mp2;
22773 	struct iocblk *iocp;
22774 	struct lifreq *lifr;
22775 
22776 	mp1 = mkiocb(SIOCSLIFNAME);
22777 	if (mp1 == NULL)
22778 		return;
22779 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22780 	if (mp2 == NULL) {
22781 		freeb(mp1);
22782 		return;
22783 	}
22784 
22785 	mp1->b_cont = mp2;
22786 	iocp = (struct iocblk *)mp1->b_rptr;
22787 	iocp->ioc_count = sizeof (struct lifreq);
22788 
22789 	lifr = (struct lifreq *)mp2->b_rptr;
22790 	mp2->b_wptr += sizeof (struct lifreq);
22791 	bzero(lifr, sizeof (struct lifreq));
22792 
22793 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22794 	lifr->lifr_ppa = ill->ill_ppa;
22795 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22796 
22797 	putnext(q, mp1);
22798 }
22799 
22800 static int
22801 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22802 {
22803 	int err;
22804 	ip_stack_t	*ipst = ill->ill_ipst;
22805 
22806 	/* Set the obsolete NDD per-interface forwarding name. */
22807 	err = ill_set_ndd_name(ill);
22808 	if (err != 0) {
22809 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22810 		    err);
22811 	}
22812 
22813 	/* Tell downstream modules where they are. */
22814 	ip_ifname_notify(ill, q);
22815 
22816 	/*
22817 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22818 	 * Error cases are ENOMEM ...
22819 	 */
22820 	err = ill_dl_phys(ill, ipif, mp, q);
22821 
22822 	/*
22823 	 * If there is no IRE expiration timer running, get one started.
22824 	 * igmp and mld timers will be triggered by the first multicast
22825 	 */
22826 	if (ipst->ips_ip_ire_expire_id == 0) {
22827 		/*
22828 		 * acquire the lock and check again.
22829 		 */
22830 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22831 		if (ipst->ips_ip_ire_expire_id == 0) {
22832 			ipst->ips_ip_ire_expire_id = timeout(
22833 			    ip_trash_timer_expire, ipst,
22834 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22835 		}
22836 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22837 	}
22838 
22839 	if (ill->ill_isv6) {
22840 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22841 		if (ipst->ips_mld_slowtimeout_id == 0) {
22842 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22843 			    (void *)ipst,
22844 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22845 		}
22846 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22847 	} else {
22848 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22849 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22850 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22851 			    (void *)ipst,
22852 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22853 		}
22854 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22855 	}
22856 
22857 	return (err);
22858 }
22859 
22860 /*
22861  * Common routine for ppa and ifname setting. Should be called exclusive.
22862  *
22863  * Returns EINPROGRESS when mp has been consumed by queueing it on
22864  * ill_pending_mp and the ioctl will complete in ip_rput.
22865  *
22866  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22867  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22868  * For SLIFNAME, we pass these values back to the userland.
22869  */
22870 static int
22871 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22872 {
22873 	ill_t	*ill;
22874 	ipif_t	*ipif;
22875 	ipsq_t	*ipsq;
22876 	char	*ppa_ptr;
22877 	char	*old_ptr;
22878 	char	old_char;
22879 	int	error;
22880 	ip_stack_t	*ipst;
22881 
22882 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22883 	ASSERT(q->q_next != NULL);
22884 	ASSERT(interf_name != NULL);
22885 
22886 	ill = (ill_t *)q->q_ptr;
22887 	ipst = ill->ill_ipst;
22888 
22889 	ASSERT(ill->ill_ipst != NULL);
22890 	ASSERT(ill->ill_name[0] == '\0');
22891 	ASSERT(IAM_WRITER_ILL(ill));
22892 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22893 	ASSERT(ill->ill_ppa == UINT_MAX);
22894 
22895 	/* The ppa is sent down by ifconfig or is chosen */
22896 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22897 		return (EINVAL);
22898 	}
22899 
22900 	/*
22901 	 * make sure ppa passed in is same as ppa in the name.
22902 	 * This check is not made when ppa == UINT_MAX in that case ppa
22903 	 * in the name could be anything. System will choose a ppa and
22904 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22905 	 */
22906 	if (*new_ppa_ptr != UINT_MAX) {
22907 		/* stoi changes the pointer */
22908 		old_ptr = ppa_ptr;
22909 		/*
22910 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22911 		 * (they don't have an externally visible ppa).  We assign one
22912 		 * here so that we can manage the interface.  Note that in
22913 		 * the past this value was always 0 for DLPI 1 drivers.
22914 		 */
22915 		if (*new_ppa_ptr == 0)
22916 			*new_ppa_ptr = stoi(&old_ptr);
22917 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22918 			return (EINVAL);
22919 	}
22920 	/*
22921 	 * terminate string before ppa
22922 	 * save char at that location.
22923 	 */
22924 	old_char = ppa_ptr[0];
22925 	ppa_ptr[0] = '\0';
22926 
22927 	ill->ill_ppa = *new_ppa_ptr;
22928 	/*
22929 	 * Finish as much work now as possible before calling ill_glist_insert
22930 	 * which makes the ill globally visible and also merges it with the
22931 	 * other protocol instance of this phyint. The remaining work is
22932 	 * done after entering the ipsq which may happen sometime later.
22933 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22934 	 */
22935 	ipif = ill->ill_ipif;
22936 
22937 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22938 	ipif_assign_seqid(ipif);
22939 
22940 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22941 		ill->ill_flags |= ILLF_IPV4;
22942 
22943 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22944 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22945 
22946 	if (ill->ill_flags & ILLF_IPV6) {
22947 
22948 		ill->ill_isv6 = B_TRUE;
22949 		if (ill->ill_rq != NULL) {
22950 			ill->ill_rq->q_qinfo = &iprinitv6;
22951 			ill->ill_wq->q_qinfo = &ipwinitv6;
22952 		}
22953 
22954 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22955 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22956 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22957 		ipif->ipif_v6subnet = ipv6_all_zeros;
22958 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22959 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22960 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22961 		/*
22962 		 * point-to-point or Non-mulicast capable
22963 		 * interfaces won't do NUD unless explicitly
22964 		 * configured to do so.
22965 		 */
22966 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22967 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22968 			ill->ill_flags |= ILLF_NONUD;
22969 		}
22970 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22971 		if (ill->ill_flags & ILLF_NOARP) {
22972 			/*
22973 			 * Note: xresolv interfaces will eventually need
22974 			 * NOARP set here as well, but that will require
22975 			 * those external resolvers to have some
22976 			 * knowledge of that flag and act appropriately.
22977 			 * Not to be changed at present.
22978 			 */
22979 			ill->ill_flags &= ~ILLF_NOARP;
22980 		}
22981 		/*
22982 		 * Set the ILLF_ROUTER flag according to the global
22983 		 * IPv6 forwarding policy.
22984 		 */
22985 		if (ipst->ips_ipv6_forward != 0)
22986 			ill->ill_flags |= ILLF_ROUTER;
22987 	} else if (ill->ill_flags & ILLF_IPV4) {
22988 		ill->ill_isv6 = B_FALSE;
22989 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22990 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22991 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22992 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22993 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22994 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22995 		/*
22996 		 * Set the ILLF_ROUTER flag according to the global
22997 		 * IPv4 forwarding policy.
22998 		 */
22999 		if (ipst->ips_ip_g_forward != 0)
23000 			ill->ill_flags |= ILLF_ROUTER;
23001 	}
23002 
23003 	ASSERT(ill->ill_phyint != NULL);
23004 
23005 	/*
23006 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23007 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23008 	 */
23009 	if (!ill_allocate_mibs(ill))
23010 		return (ENOMEM);
23011 
23012 	/*
23013 	 * Pick a default sap until we get the DL_INFO_ACK back from
23014 	 * the driver.
23015 	 */
23016 	if (ill->ill_sap == 0) {
23017 		if (ill->ill_isv6)
23018 			ill->ill_sap  = IP6_DL_SAP;
23019 		else
23020 			ill->ill_sap  = IP_DL_SAP;
23021 	}
23022 
23023 	ill->ill_ifname_pending = 1;
23024 	ill->ill_ifname_pending_err = 0;
23025 
23026 	ill_refhold(ill);
23027 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23028 	if ((error = ill_glist_insert(ill, interf_name,
23029 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23030 		ill->ill_ppa = UINT_MAX;
23031 		ill->ill_name[0] = '\0';
23032 		/*
23033 		 * undo null termination done above.
23034 		 */
23035 		ppa_ptr[0] = old_char;
23036 		rw_exit(&ipst->ips_ill_g_lock);
23037 		ill_refrele(ill);
23038 		return (error);
23039 	}
23040 
23041 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23042 
23043 	/*
23044 	 * When we return the buffer pointed to by interf_name should contain
23045 	 * the same name as in ill_name.
23046 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23047 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23048 	 * so copy full name and update the ppa ptr.
23049 	 * When ppa passed in != UINT_MAX all values are correct just undo
23050 	 * null termination, this saves a bcopy.
23051 	 */
23052 	if (*new_ppa_ptr == UINT_MAX) {
23053 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23054 		*new_ppa_ptr = ill->ill_ppa;
23055 	} else {
23056 		/*
23057 		 * undo null termination done above.
23058 		 */
23059 		ppa_ptr[0] = old_char;
23060 	}
23061 
23062 	/* Let SCTP know about this ILL */
23063 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23064 
23065 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23066 	    B_TRUE);
23067 
23068 	rw_exit(&ipst->ips_ill_g_lock);
23069 	ill_refrele(ill);
23070 	if (ipsq == NULL)
23071 		return (EINPROGRESS);
23072 
23073 	/*
23074 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23075 	 */
23076 	if (ipsq->ipsq_current_ipif == NULL)
23077 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23078 	else
23079 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23080 
23081 	error = ipif_set_values_tail(ill, ipif, mp, q);
23082 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23083 	if (error != 0 && error != EINPROGRESS) {
23084 		/*
23085 		 * restore previous values
23086 		 */
23087 		ill->ill_isv6 = B_FALSE;
23088 	}
23089 	return (error);
23090 }
23091 
23092 
23093 void
23094 ipif_init(ip_stack_t *ipst)
23095 {
23096 	hrtime_t hrt;
23097 	int i;
23098 
23099 	/*
23100 	 * Can't call drv_getparm here as it is too early in the boot.
23101 	 * As we use ipif_src_random just for picking a different
23102 	 * source address everytime, this need not be really random.
23103 	 */
23104 	hrt = gethrtime();
23105 	ipst->ips_ipif_src_random =
23106 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23107 
23108 	for (i = 0; i < MAX_G_HEADS; i++) {
23109 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23110 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23111 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23112 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23113 	}
23114 
23115 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23116 	    ill_phyint_compare_index,
23117 	    sizeof (phyint_t),
23118 	    offsetof(struct phyint, phyint_avl_by_index));
23119 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23120 	    ill_phyint_compare_name,
23121 	    sizeof (phyint_t),
23122 	    offsetof(struct phyint, phyint_avl_by_name));
23123 }
23124 
23125 /*
23126  * Lookup the ipif corresponding to the onlink destination address. For
23127  * point-to-point interfaces, it matches with remote endpoint destination
23128  * address. For point-to-multipoint interfaces it only tries to match the
23129  * destination with the interface's subnet address. The longest, most specific
23130  * match is found to take care of such rare network configurations like -
23131  * le0: 129.146.1.1/16
23132  * le1: 129.146.2.2/24
23133  * It is used only by SO_DONTROUTE at the moment.
23134  */
23135 ipif_t *
23136 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23137 {
23138 	ipif_t	*ipif, *best_ipif;
23139 	ill_t	*ill;
23140 	ill_walk_context_t ctx;
23141 
23142 	ASSERT(zoneid != ALL_ZONES);
23143 	best_ipif = NULL;
23144 
23145 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23146 	ill = ILL_START_WALK_V4(&ctx, ipst);
23147 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23148 		mutex_enter(&ill->ill_lock);
23149 		for (ipif = ill->ill_ipif; ipif != NULL;
23150 		    ipif = ipif->ipif_next) {
23151 			if (!IPIF_CAN_LOOKUP(ipif))
23152 				continue;
23153 			if (ipif->ipif_zoneid != zoneid &&
23154 			    ipif->ipif_zoneid != ALL_ZONES)
23155 				continue;
23156 			/*
23157 			 * Point-to-point case. Look for exact match with
23158 			 * destination address.
23159 			 */
23160 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23161 				if (ipif->ipif_pp_dst_addr == addr) {
23162 					ipif_refhold_locked(ipif);
23163 					mutex_exit(&ill->ill_lock);
23164 					rw_exit(&ipst->ips_ill_g_lock);
23165 					if (best_ipif != NULL)
23166 						ipif_refrele(best_ipif);
23167 					return (ipif);
23168 				}
23169 			} else if (ipif->ipif_subnet == (addr &
23170 			    ipif->ipif_net_mask)) {
23171 				/*
23172 				 * Point-to-multipoint case. Looping through to
23173 				 * find the most specific match. If there are
23174 				 * multiple best match ipif's then prefer ipif's
23175 				 * that are UP. If there is only one best match
23176 				 * ipif and it is DOWN we must still return it.
23177 				 */
23178 				if ((best_ipif == NULL) ||
23179 				    (ipif->ipif_net_mask >
23180 				    best_ipif->ipif_net_mask) ||
23181 				    ((ipif->ipif_net_mask ==
23182 				    best_ipif->ipif_net_mask) &&
23183 				    ((ipif->ipif_flags & IPIF_UP) &&
23184 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23185 					ipif_refhold_locked(ipif);
23186 					mutex_exit(&ill->ill_lock);
23187 					rw_exit(&ipst->ips_ill_g_lock);
23188 					if (best_ipif != NULL)
23189 						ipif_refrele(best_ipif);
23190 					best_ipif = ipif;
23191 					rw_enter(&ipst->ips_ill_g_lock,
23192 					    RW_READER);
23193 					mutex_enter(&ill->ill_lock);
23194 				}
23195 			}
23196 		}
23197 		mutex_exit(&ill->ill_lock);
23198 	}
23199 	rw_exit(&ipst->ips_ill_g_lock);
23200 	return (best_ipif);
23201 }
23202 
23203 
23204 /*
23205  * Save enough information so that we can recreate the IRE if
23206  * the interface goes down and then up.
23207  */
23208 static void
23209 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23210 {
23211 	mblk_t	*save_mp;
23212 
23213 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23214 	if (save_mp != NULL) {
23215 		ifrt_t	*ifrt;
23216 
23217 		save_mp->b_wptr += sizeof (ifrt_t);
23218 		ifrt = (ifrt_t *)save_mp->b_rptr;
23219 		bzero(ifrt, sizeof (ifrt_t));
23220 		ifrt->ifrt_type = ire->ire_type;
23221 		ifrt->ifrt_addr = ire->ire_addr;
23222 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23223 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23224 		ifrt->ifrt_mask = ire->ire_mask;
23225 		ifrt->ifrt_flags = ire->ire_flags;
23226 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23227 		mutex_enter(&ipif->ipif_saved_ire_lock);
23228 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23229 		ipif->ipif_saved_ire_mp = save_mp;
23230 		ipif->ipif_saved_ire_cnt++;
23231 		mutex_exit(&ipif->ipif_saved_ire_lock);
23232 	}
23233 }
23234 
23235 
23236 static void
23237 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23238 {
23239 	mblk_t	**mpp;
23240 	mblk_t	*mp;
23241 	ifrt_t	*ifrt;
23242 
23243 	/* Remove from ipif_saved_ire_mp list if it is there */
23244 	mutex_enter(&ipif->ipif_saved_ire_lock);
23245 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23246 	    mpp = &(*mpp)->b_cont) {
23247 		/*
23248 		 * On a given ipif, the triple of address, gateway and
23249 		 * mask is unique for each saved IRE (in the case of
23250 		 * ordinary interface routes, the gateway address is
23251 		 * all-zeroes).
23252 		 */
23253 		mp = *mpp;
23254 		ifrt = (ifrt_t *)mp->b_rptr;
23255 		if (ifrt->ifrt_addr == ire->ire_addr &&
23256 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23257 		    ifrt->ifrt_mask == ire->ire_mask) {
23258 			*mpp = mp->b_cont;
23259 			ipif->ipif_saved_ire_cnt--;
23260 			freeb(mp);
23261 			break;
23262 		}
23263 	}
23264 	mutex_exit(&ipif->ipif_saved_ire_lock);
23265 }
23266 
23267 
23268 /*
23269  * IP multirouting broadcast routes handling
23270  * Append CGTP broadcast IREs to regular ones created
23271  * at ifconfig time.
23272  */
23273 static void
23274 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23275 {
23276 	ire_t *ire_prim;
23277 
23278 	ASSERT(ire != NULL);
23279 	ASSERT(ire_dst != NULL);
23280 
23281 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23282 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23283 	if (ire_prim != NULL) {
23284 		/*
23285 		 * We are in the special case of broadcasts for
23286 		 * CGTP. We add an IRE_BROADCAST that holds
23287 		 * the RTF_MULTIRT flag, the destination
23288 		 * address of ire_dst and the low level
23289 		 * info of ire_prim. In other words, CGTP
23290 		 * broadcast is added to the redundant ipif.
23291 		 */
23292 		ipif_t *ipif_prim;
23293 		ire_t  *bcast_ire;
23294 
23295 		ipif_prim = ire_prim->ire_ipif;
23296 
23297 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23298 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23299 		    (void *)ire_dst, (void *)ire_prim,
23300 		    (void *)ipif_prim));
23301 
23302 		bcast_ire = ire_create(
23303 		    (uchar_t *)&ire->ire_addr,
23304 		    (uchar_t *)&ip_g_all_ones,
23305 		    (uchar_t *)&ire_dst->ire_src_addr,
23306 		    (uchar_t *)&ire->ire_gateway_addr,
23307 		    &ipif_prim->ipif_mtu,
23308 		    NULL,
23309 		    ipif_prim->ipif_rq,
23310 		    ipif_prim->ipif_wq,
23311 		    IRE_BROADCAST,
23312 		    ipif_prim,
23313 		    0,
23314 		    0,
23315 		    0,
23316 		    ire->ire_flags,
23317 		    &ire_uinfo_null,
23318 		    NULL,
23319 		    NULL,
23320 		    ipst);
23321 
23322 		if (bcast_ire != NULL) {
23323 
23324 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23325 			    B_FALSE) == 0) {
23326 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23327 				    "added bcast_ire %p\n",
23328 				    (void *)bcast_ire));
23329 
23330 				ipif_save_ire(bcast_ire->ire_ipif,
23331 				    bcast_ire);
23332 				ire_refrele(bcast_ire);
23333 			}
23334 		}
23335 		ire_refrele(ire_prim);
23336 	}
23337 }
23338 
23339 
23340 /*
23341  * IP multirouting broadcast routes handling
23342  * Remove the broadcast ire
23343  */
23344 static void
23345 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23346 {
23347 	ire_t *ire_dst;
23348 
23349 	ASSERT(ire != NULL);
23350 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23351 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23352 	if (ire_dst != NULL) {
23353 		ire_t *ire_prim;
23354 
23355 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23356 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23357 		if (ire_prim != NULL) {
23358 			ipif_t *ipif_prim;
23359 			ire_t  *bcast_ire;
23360 
23361 			ipif_prim = ire_prim->ire_ipif;
23362 
23363 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23364 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23365 			    (void *)ire_dst, (void *)ire_prim,
23366 			    (void *)ipif_prim));
23367 
23368 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23369 			    ire->ire_gateway_addr,
23370 			    IRE_BROADCAST,
23371 			    ipif_prim, ALL_ZONES,
23372 			    NULL,
23373 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23374 			    MATCH_IRE_MASK, ipst);
23375 
23376 			if (bcast_ire != NULL) {
23377 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23378 				    "looked up bcast_ire %p\n",
23379 				    (void *)bcast_ire));
23380 				ipif_remove_ire(bcast_ire->ire_ipif,
23381 				    bcast_ire);
23382 				ire_delete(bcast_ire);
23383 				ire_refrele(bcast_ire);
23384 			}
23385 			ire_refrele(ire_prim);
23386 		}
23387 		ire_refrele(ire_dst);
23388 	}
23389 }
23390 
23391 /*
23392  * IPsec hardware acceleration capabilities related functions.
23393  */
23394 
23395 /*
23396  * Free a per-ill IPsec capabilities structure.
23397  */
23398 static void
23399 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23400 {
23401 	if (capab->auth_hw_algs != NULL)
23402 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23403 	if (capab->encr_hw_algs != NULL)
23404 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23405 	if (capab->encr_algparm != NULL)
23406 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23407 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23408 }
23409 
23410 /*
23411  * Allocate a new per-ill IPsec capabilities structure. This structure
23412  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23413  * an array which specifies, for each algorithm, whether this algorithm
23414  * is supported by the ill or not.
23415  */
23416 static ill_ipsec_capab_t *
23417 ill_ipsec_capab_alloc(void)
23418 {
23419 	ill_ipsec_capab_t *capab;
23420 	uint_t nelems;
23421 
23422 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23423 	if (capab == NULL)
23424 		return (NULL);
23425 
23426 	/* we need one bit per algorithm */
23427 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23428 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23429 
23430 	/* allocate memory to store algorithm flags */
23431 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23432 	if (capab->encr_hw_algs == NULL)
23433 		goto nomem;
23434 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23435 	if (capab->auth_hw_algs == NULL)
23436 		goto nomem;
23437 	/*
23438 	 * Leave encr_algparm NULL for now since we won't need it half
23439 	 * the time
23440 	 */
23441 	return (capab);
23442 
23443 nomem:
23444 	ill_ipsec_capab_free(capab);
23445 	return (NULL);
23446 }
23447 
23448 /*
23449  * Resize capability array.  Since we're exclusive, this is OK.
23450  */
23451 static boolean_t
23452 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23453 {
23454 	ipsec_capab_algparm_t *nalp, *oalp;
23455 	uint32_t olen, nlen;
23456 
23457 	oalp = capab->encr_algparm;
23458 	olen = capab->encr_algparm_size;
23459 
23460 	if (oalp != NULL) {
23461 		if (algid < capab->encr_algparm_end)
23462 			return (B_TRUE);
23463 	}
23464 
23465 	nlen = (algid + 1) * sizeof (*nalp);
23466 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23467 	if (nalp == NULL)
23468 		return (B_FALSE);
23469 
23470 	if (oalp != NULL) {
23471 		bcopy(oalp, nalp, olen);
23472 		kmem_free(oalp, olen);
23473 	}
23474 	capab->encr_algparm = nalp;
23475 	capab->encr_algparm_size = nlen;
23476 	capab->encr_algparm_end = algid + 1;
23477 
23478 	return (B_TRUE);
23479 }
23480 
23481 /*
23482  * Compare the capabilities of the specified ill with the protocol
23483  * and algorithms specified by the SA passed as argument.
23484  * If they match, returns B_TRUE, B_FALSE if they do not match.
23485  *
23486  * The ill can be passed as a pointer to it, or by specifying its index
23487  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23488  *
23489  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23490  * packet is eligible for hardware acceleration, and by
23491  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23492  * to a particular ill.
23493  */
23494 boolean_t
23495 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23496     ipsa_t *sa, netstack_t *ns)
23497 {
23498 	boolean_t sa_isv6;
23499 	uint_t algid;
23500 	struct ill_ipsec_capab_s *cpp;
23501 	boolean_t need_refrele = B_FALSE;
23502 	ip_stack_t	*ipst = ns->netstack_ip;
23503 
23504 	if (ill == NULL) {
23505 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23506 		    NULL, NULL, NULL, ipst);
23507 		if (ill == NULL) {
23508 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23509 			return (B_FALSE);
23510 		}
23511 		need_refrele = B_TRUE;
23512 	}
23513 
23514 	/*
23515 	 * Use the address length specified by the SA to determine
23516 	 * if it corresponds to a IPv6 address, and fail the matching
23517 	 * if the isv6 flag passed as argument does not match.
23518 	 * Note: this check is used for SADB capability checking before
23519 	 * sending SA information to an ill.
23520 	 */
23521 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23522 	if (sa_isv6 != ill_isv6)
23523 		/* protocol mismatch */
23524 		goto done;
23525 
23526 	/*
23527 	 * Check if the ill supports the protocol, algorithm(s) and
23528 	 * key size(s) specified by the SA, and get the pointers to
23529 	 * the algorithms supported by the ill.
23530 	 */
23531 	switch (sa->ipsa_type) {
23532 
23533 	case SADB_SATYPE_ESP:
23534 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23535 			/* ill does not support ESP acceleration */
23536 			goto done;
23537 		cpp = ill->ill_ipsec_capab_esp;
23538 		algid = sa->ipsa_auth_alg;
23539 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23540 			goto done;
23541 		algid = sa->ipsa_encr_alg;
23542 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23543 			goto done;
23544 		if (algid < cpp->encr_algparm_end) {
23545 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23546 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23547 				goto done;
23548 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23549 				goto done;
23550 		}
23551 		break;
23552 
23553 	case SADB_SATYPE_AH:
23554 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23555 			/* ill does not support AH acceleration */
23556 			goto done;
23557 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23558 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23559 			goto done;
23560 		break;
23561 	}
23562 
23563 	if (need_refrele)
23564 		ill_refrele(ill);
23565 	return (B_TRUE);
23566 done:
23567 	if (need_refrele)
23568 		ill_refrele(ill);
23569 	return (B_FALSE);
23570 }
23571 
23572 
23573 /*
23574  * Add a new ill to the list of IPsec capable ills.
23575  * Called from ill_capability_ipsec_ack() when an ACK was received
23576  * indicating that IPsec hardware processing was enabled for an ill.
23577  *
23578  * ill must point to the ill for which acceleration was enabled.
23579  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23580  */
23581 static void
23582 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23583 {
23584 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23585 	uint_t sa_type;
23586 	uint_t ipproto;
23587 	ip_stack_t	*ipst = ill->ill_ipst;
23588 
23589 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23590 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23591 
23592 	switch (dl_cap) {
23593 	case DL_CAPAB_IPSEC_AH:
23594 		sa_type = SADB_SATYPE_AH;
23595 		ills = &ipst->ips_ipsec_capab_ills_ah;
23596 		ipproto = IPPROTO_AH;
23597 		break;
23598 	case DL_CAPAB_IPSEC_ESP:
23599 		sa_type = SADB_SATYPE_ESP;
23600 		ills = &ipst->ips_ipsec_capab_ills_esp;
23601 		ipproto = IPPROTO_ESP;
23602 		break;
23603 	}
23604 
23605 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23606 
23607 	/*
23608 	 * Add ill index to list of hardware accelerators. If
23609 	 * already in list, do nothing.
23610 	 */
23611 	for (cur_ill = *ills; cur_ill != NULL &&
23612 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23613 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23614 		;
23615 
23616 	if (cur_ill == NULL) {
23617 		/* if this is a new entry for this ill */
23618 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23619 		if (new_ill == NULL) {
23620 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23621 			return;
23622 		}
23623 
23624 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23625 		new_ill->ill_isv6 = ill->ill_isv6;
23626 		new_ill->next = *ills;
23627 		*ills = new_ill;
23628 	} else if (!sadb_resync) {
23629 		/* not resync'ing SADB and an entry exists for this ill */
23630 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23631 		return;
23632 	}
23633 
23634 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23635 
23636 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23637 		/*
23638 		 * IPsec module for protocol loaded, initiate dump
23639 		 * of the SADB to this ill.
23640 		 */
23641 		sadb_ill_download(ill, sa_type);
23642 }
23643 
23644 /*
23645  * Remove an ill from the list of IPsec capable ills.
23646  */
23647 static void
23648 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23649 {
23650 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23651 	ip_stack_t	*ipst = ill->ill_ipst;
23652 
23653 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23654 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23655 
23656 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23657 	    &ipst->ips_ipsec_capab_ills_esp;
23658 
23659 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23660 
23661 	prev_ill = NULL;
23662 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23663 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23664 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23665 		;
23666 	if (cur_ill == NULL) {
23667 		/* entry not found */
23668 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23669 		return;
23670 	}
23671 	if (prev_ill == NULL) {
23672 		/* entry at front of list */
23673 		*ills = NULL;
23674 	} else {
23675 		prev_ill->next = cur_ill->next;
23676 	}
23677 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23678 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23679 }
23680 
23681 /*
23682  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23683  * supporting the specified IPsec protocol acceleration.
23684  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23685  * We free the mblk and, if sa is non-null, release the held referece.
23686  */
23687 void
23688 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23689     netstack_t *ns)
23690 {
23691 	ipsec_capab_ill_t *ici, *cur_ici;
23692 	ill_t *ill;
23693 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23694 	ip_stack_t	*ipst = ns->netstack_ip;
23695 
23696 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23697 	    ipst->ips_ipsec_capab_ills_esp;
23698 
23699 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23700 
23701 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23702 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23703 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23704 
23705 		/*
23706 		 * Handle the case where the ill goes away while the SADB is
23707 		 * attempting to send messages.  If it's going away, it's
23708 		 * nuking its shadow SADB, so we don't care..
23709 		 */
23710 
23711 		if (ill == NULL)
23712 			continue;
23713 
23714 		if (sa != NULL) {
23715 			/*
23716 			 * Make sure capabilities match before
23717 			 * sending SA to ill.
23718 			 */
23719 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23720 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23721 				ill_refrele(ill);
23722 				continue;
23723 			}
23724 
23725 			mutex_enter(&sa->ipsa_lock);
23726 			sa->ipsa_flags |= IPSA_F_HW;
23727 			mutex_exit(&sa->ipsa_lock);
23728 		}
23729 
23730 		/*
23731 		 * Copy template message, and add it to the front
23732 		 * of the mblk ship list. We want to avoid holding
23733 		 * the ipsec_capab_ills_lock while sending the
23734 		 * message to the ills.
23735 		 *
23736 		 * The b_next and b_prev are temporarily used
23737 		 * to build a list of mblks to be sent down, and to
23738 		 * save the ill to which they must be sent.
23739 		 */
23740 		nmp = copymsg(mp);
23741 		if (nmp == NULL) {
23742 			ill_refrele(ill);
23743 			continue;
23744 		}
23745 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23746 		nmp->b_next = mp_ship_list;
23747 		mp_ship_list = nmp;
23748 		nmp->b_prev = (mblk_t *)ill;
23749 	}
23750 
23751 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23752 
23753 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23754 		/* restore the mblk to a sane state */
23755 		next_mp = nmp->b_next;
23756 		nmp->b_next = NULL;
23757 		ill = (ill_t *)nmp->b_prev;
23758 		nmp->b_prev = NULL;
23759 
23760 		ill_dlpi_send(ill, nmp);
23761 		ill_refrele(ill);
23762 	}
23763 
23764 	if (sa != NULL)
23765 		IPSA_REFRELE(sa);
23766 	freemsg(mp);
23767 }
23768 
23769 /*
23770  * Derive an interface id from the link layer address.
23771  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23772  */
23773 static boolean_t
23774 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23775 {
23776 	char		*addr;
23777 
23778 	if (phys_length != ETHERADDRL)
23779 		return (B_FALSE);
23780 
23781 	/* Form EUI-64 like address */
23782 	addr = (char *)&v6addr->s6_addr32[2];
23783 	bcopy((char *)phys_addr, addr, 3);
23784 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23785 	addr[3] = (char)0xff;
23786 	addr[4] = (char)0xfe;
23787 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23788 	return (B_TRUE);
23789 }
23790 
23791 /* ARGSUSED */
23792 static boolean_t
23793 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23794 {
23795 	return (B_FALSE);
23796 }
23797 
23798 /* ARGSUSED */
23799 static boolean_t
23800 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23801     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23802 {
23803 	/*
23804 	 * Multicast address mappings used over Ethernet/802.X.
23805 	 * This address is used as a base for mappings.
23806 	 */
23807 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23808 	    0x00, 0x00, 0x00};
23809 
23810 	/*
23811 	 * Extract low order 32 bits from IPv6 multicast address.
23812 	 * Or that into the link layer address, starting from the
23813 	 * second byte.
23814 	 */
23815 	*hw_start = 2;
23816 	v6_extract_mask->s6_addr32[0] = 0;
23817 	v6_extract_mask->s6_addr32[1] = 0;
23818 	v6_extract_mask->s6_addr32[2] = 0;
23819 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23820 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23821 	return (B_TRUE);
23822 }
23823 
23824 /*
23825  * Indicate by return value whether multicast is supported. If not,
23826  * this code should not touch/change any parameters.
23827  */
23828 /* ARGSUSED */
23829 static boolean_t
23830 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23831     uint32_t *hw_start, ipaddr_t *extract_mask)
23832 {
23833 	/*
23834 	 * Multicast address mappings used over Ethernet/802.X.
23835 	 * This address is used as a base for mappings.
23836 	 */
23837 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23838 	    0x00, 0x00, 0x00 };
23839 
23840 	if (phys_length != ETHERADDRL)
23841 		return (B_FALSE);
23842 
23843 	*extract_mask = htonl(0x007fffff);
23844 	*hw_start = 2;
23845 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23846 	return (B_TRUE);
23847 }
23848 
23849 /*
23850  * Derive IPoIB interface id from the link layer address.
23851  */
23852 static boolean_t
23853 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23854 {
23855 	char		*addr;
23856 
23857 	if (phys_length != 20)
23858 		return (B_FALSE);
23859 	addr = (char *)&v6addr->s6_addr32[2];
23860 	bcopy(phys_addr + 12, addr, 8);
23861 	/*
23862 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23863 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23864 	 * rules. In these cases, the IBA considers these GUIDs to be in
23865 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23866 	 * required; vendors are required not to assign global EUI-64's
23867 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23868 	 * of the interface identifier. Whether the GUID is in modified
23869 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23870 	 * bit set to 1.
23871 	 */
23872 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23873 	return (B_TRUE);
23874 }
23875 
23876 /*
23877  * Note on mapping from multicast IP addresses to IPoIB multicast link
23878  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23879  * The format of an IPoIB multicast address is:
23880  *
23881  *  4 byte QPN      Scope Sign.  Pkey
23882  * +--------------------------------------------+
23883  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23884  * +--------------------------------------------+
23885  *
23886  * The Scope and Pkey components are properties of the IBA port and
23887  * network interface. They can be ascertained from the broadcast address.
23888  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23889  */
23890 
23891 static boolean_t
23892 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23893     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23894 {
23895 	/*
23896 	 * Base IPoIB IPv6 multicast address used for mappings.
23897 	 * Does not contain the IBA scope/Pkey values.
23898 	 */
23899 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23900 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23901 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23902 
23903 	/*
23904 	 * Extract low order 80 bits from IPv6 multicast address.
23905 	 * Or that into the link layer address, starting from the
23906 	 * sixth byte.
23907 	 */
23908 	*hw_start = 6;
23909 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23910 
23911 	/*
23912 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23913 	 */
23914 	*(maddr + 5) = *(bphys_addr + 5);
23915 	*(maddr + 8) = *(bphys_addr + 8);
23916 	*(maddr + 9) = *(bphys_addr + 9);
23917 
23918 	v6_extract_mask->s6_addr32[0] = 0;
23919 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23920 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23921 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23922 	return (B_TRUE);
23923 }
23924 
23925 static boolean_t
23926 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23927     uint32_t *hw_start, ipaddr_t *extract_mask)
23928 {
23929 	/*
23930 	 * Base IPoIB IPv4 multicast address used for mappings.
23931 	 * Does not contain the IBA scope/Pkey values.
23932 	 */
23933 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23934 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23935 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23936 
23937 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23938 		return (B_FALSE);
23939 
23940 	/*
23941 	 * Extract low order 28 bits from IPv4 multicast address.
23942 	 * Or that into the link layer address, starting from the
23943 	 * sixteenth byte.
23944 	 */
23945 	*extract_mask = htonl(0x0fffffff);
23946 	*hw_start = 16;
23947 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23948 
23949 	/*
23950 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23951 	 */
23952 	*(maddr + 5) = *(bphys_addr + 5);
23953 	*(maddr + 8) = *(bphys_addr + 8);
23954 	*(maddr + 9) = *(bphys_addr + 9);
23955 	return (B_TRUE);
23956 }
23957 
23958 /*
23959  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23960  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23961  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23962  * the link-local address is preferred.
23963  */
23964 boolean_t
23965 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23966 {
23967 	ipif_t	*ipif;
23968 	ipif_t	*maybe_ipif = NULL;
23969 
23970 	mutex_enter(&ill->ill_lock);
23971 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23972 		mutex_exit(&ill->ill_lock);
23973 		if (ipifp != NULL)
23974 			*ipifp = NULL;
23975 		return (B_FALSE);
23976 	}
23977 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23978 		if (!IPIF_CAN_LOOKUP(ipif))
23979 			continue;
23980 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23981 		    ipif->ipif_zoneid != ALL_ZONES)
23982 			continue;
23983 		if ((ipif->ipif_flags & flags) != flags)
23984 			continue;
23985 
23986 		if (ipifp == NULL) {
23987 			mutex_exit(&ill->ill_lock);
23988 			ASSERT(maybe_ipif == NULL);
23989 			return (B_TRUE);
23990 		}
23991 		if (!ill->ill_isv6 ||
23992 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23993 			ipif_refhold_locked(ipif);
23994 			mutex_exit(&ill->ill_lock);
23995 			*ipifp = ipif;
23996 			return (B_TRUE);
23997 		}
23998 		if (maybe_ipif == NULL)
23999 			maybe_ipif = ipif;
24000 	}
24001 	if (ipifp != NULL) {
24002 		if (maybe_ipif != NULL)
24003 			ipif_refhold_locked(maybe_ipif);
24004 		*ipifp = maybe_ipif;
24005 	}
24006 	mutex_exit(&ill->ill_lock);
24007 	return (maybe_ipif != NULL);
24008 }
24009 
24010 /*
24011  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24012  */
24013 boolean_t
24014 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24015 {
24016 	ill_t *illg;
24017 	ip_stack_t	*ipst = ill->ill_ipst;
24018 
24019 	/*
24020 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24021 	 */
24022 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24023 		return (B_TRUE);
24024 	}
24025 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24026 	if (ill->ill_group == NULL) {
24027 		/* ill not in a group */
24028 		rw_exit(&ipst->ips_ill_g_lock);
24029 		return (B_FALSE);
24030 	}
24031 
24032 	/*
24033 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24034 	 * group. We need to look for an ipif in the zone on all the ills in the
24035 	 * group.
24036 	 */
24037 	illg = ill->ill_group->illgrp_ill;
24038 	do {
24039 		/*
24040 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24041 		 * that it's not there.
24042 		 */
24043 		if (illg != ill &&
24044 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24045 			break;
24046 		}
24047 	} while ((illg = illg->ill_group_next) != NULL);
24048 	rw_exit(&ipst->ips_ill_g_lock);
24049 	return (illg != NULL);
24050 }
24051 
24052 /*
24053  * Check if this ill is only being used to send ICMP probes for IPMP
24054  */
24055 boolean_t
24056 ill_is_probeonly(ill_t *ill)
24057 {
24058 	/*
24059 	 * Check if the interface is FAILED, or INACTIVE
24060 	 */
24061 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24062 		return (B_TRUE);
24063 
24064 	return (B_FALSE);
24065 }
24066 
24067 /*
24068  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24069  * If a pointer to an ipif_t is returned then the caller will need to do
24070  * an ill_refrele().
24071  *
24072  * If there is no real interface which matches the ifindex, then it looks
24073  * for a group that has a matching index. In the case of a group match the
24074  * lifidx must be zero. We don't need emulate the logical interfaces
24075  * since IP Filter's use of netinfo doesn't use that.
24076  */
24077 ipif_t *
24078 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24079     ip_stack_t *ipst)
24080 {
24081 	ipif_t *ipif;
24082 	ill_t *ill;
24083 
24084 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24085 	    ipst);
24086 
24087 	if (ill == NULL) {
24088 		/* Fallback to group names only if hook_emulation set */
24089 		if (!ipst->ips_ipmp_hook_emulation)
24090 			return (NULL);
24091 
24092 		if (lifidx != 0)
24093 			return (NULL);
24094 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24095 		if (ill == NULL)
24096 			return (NULL);
24097 	}
24098 
24099 	mutex_enter(&ill->ill_lock);
24100 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24101 		mutex_exit(&ill->ill_lock);
24102 		ill_refrele(ill);
24103 		return (NULL);
24104 	}
24105 
24106 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24107 		if (!IPIF_CAN_LOOKUP(ipif))
24108 			continue;
24109 		if (lifidx == ipif->ipif_id) {
24110 			ipif_refhold_locked(ipif);
24111 			break;
24112 		}
24113 	}
24114 
24115 	mutex_exit(&ill->ill_lock);
24116 	ill_refrele(ill);
24117 	return (ipif);
24118 }
24119 
24120 /*
24121  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24122  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24123  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24124  * for details.
24125  */
24126 void
24127 ill_fastpath_flush(ill_t *ill)
24128 {
24129 	ip_stack_t *ipst = ill->ill_ipst;
24130 
24131 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24132 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24133 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24134 }
24135 
24136 /*
24137  * Set the physical address information for `ill' to the contents of the
24138  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24139  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24140  * EINPROGRESS will be returned.
24141  */
24142 int
24143 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24144 {
24145 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24146 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24147 
24148 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24149 
24150 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24151 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24152 		/* Changing DL_IPV6_TOKEN is not yet supported */
24153 		return (0);
24154 	}
24155 
24156 	/*
24157 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24158 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24159 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24160 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24161 	 */
24162 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24163 		freemsg(mp);
24164 		return (ENOMEM);
24165 	}
24166 
24167 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24168 
24169 	/*
24170 	 * If we can quiesce the ill, then set the address.  If not, then
24171 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24172 	 */
24173 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24174 	mutex_enter(&ill->ill_lock);
24175 	if (!ill_is_quiescent(ill)) {
24176 		/* call cannot fail since `conn_t *' argument is NULL */
24177 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24178 		    mp, ILL_DOWN);
24179 		mutex_exit(&ill->ill_lock);
24180 		return (EINPROGRESS);
24181 	}
24182 	mutex_exit(&ill->ill_lock);
24183 
24184 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24185 	return (0);
24186 }
24187 
24188 /*
24189  * Once the ill associated with `q' has quiesced, set its physical address
24190  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24191  * are passed (linked by b_cont), since we sometimes need to save two distinct
24192  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24193  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24194  * is quiesced, we know any stale IREs with the old address information have
24195  * already been removed, so we don't need to call ill_fastpath_flush().
24196  */
24197 /* ARGSUSED */
24198 static void
24199 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24200 {
24201 	ill_t		*ill = q->q_ptr;
24202 	mblk_t		*addrmp2 = unlinkb(addrmp);
24203 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24204 	uint_t		addrlen, addroff;
24205 
24206 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24207 
24208 	addroff	= dlindp->dl_addr_offset;
24209 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24210 
24211 	switch (dlindp->dl_data) {
24212 	case DL_IPV6_LINK_LAYER_ADDR:
24213 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24214 		freemsg(addrmp2);
24215 		break;
24216 
24217 	case DL_CURR_PHYS_ADDR:
24218 		freemsg(ill->ill_phys_addr_mp);
24219 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24220 		ill->ill_phys_addr_mp = addrmp;
24221 		ill->ill_phys_addr_length = addrlen;
24222 
24223 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24224 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24225 		else
24226 			freemsg(addrmp2);
24227 		break;
24228 	default:
24229 		ASSERT(0);
24230 	}
24231 
24232 	/*
24233 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24234 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24235 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24236 	 * brought up.
24237 	 */
24238 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24239 		ipsq_current_finish(ipsq);
24240 }
24241 
24242 /*
24243  * Helper routine for setting the ill_nd_lla fields.
24244  */
24245 void
24246 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24247 {
24248 	freemsg(ill->ill_nd_lla_mp);
24249 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24250 	ill->ill_nd_lla_mp = ndmp;
24251 	ill->ill_nd_lla_len = addrlen;
24252 }
24253 
24254 major_t IP_MAJ;
24255 #define	IP	"ip"
24256 
24257 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24258 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24259 
24260 /*
24261  * Issue REMOVEIF ioctls to have the loopback interfaces
24262  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24263  * the former going away when the user-level processes in the zone
24264  * are killed  * and the latter are cleaned up by the stream head
24265  * str_stack_shutdown callback that undoes all I_PLINKs.
24266  */
24267 void
24268 ip_loopback_cleanup(ip_stack_t *ipst)
24269 {
24270 	int error;
24271 	ldi_handle_t	lh = NULL;
24272 	ldi_ident_t	li = NULL;
24273 	int		rval;
24274 	cred_t		*cr;
24275 	struct strioctl iocb;
24276 	struct lifreq	lifreq;
24277 
24278 	IP_MAJ = ddi_name_to_major(IP);
24279 
24280 #ifdef NS_DEBUG
24281 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24282 	    ipst->ips_netstack->netstack_stackid);
24283 #endif
24284 
24285 	bzero(&lifreq, sizeof (lifreq));
24286 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24287 
24288 	error = ldi_ident_from_major(IP_MAJ, &li);
24289 	if (error) {
24290 #ifdef DEBUG
24291 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24292 		    error);
24293 #endif
24294 		return;
24295 	}
24296 
24297 	cr = zone_get_kcred(netstackid_to_zoneid(
24298 	    ipst->ips_netstack->netstack_stackid));
24299 	ASSERT(cr != NULL);
24300 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24301 	if (error) {
24302 #ifdef DEBUG
24303 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24304 		    error);
24305 #endif
24306 		goto out;
24307 	}
24308 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24309 	iocb.ic_timout = 15;
24310 	iocb.ic_len = sizeof (lifreq);
24311 	iocb.ic_dp = (char *)&lifreq;
24312 
24313 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24314 	/* LINTED - statement has no consequent */
24315 	if (error) {
24316 #ifdef NS_DEBUG
24317 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24318 		    "UDP6 error %d\n", error);
24319 #endif
24320 	}
24321 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24322 	lh = NULL;
24323 
24324 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24325 	if (error) {
24326 #ifdef NS_DEBUG
24327 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24328 		    error);
24329 #endif
24330 		goto out;
24331 	}
24332 
24333 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24334 	iocb.ic_timout = 15;
24335 	iocb.ic_len = sizeof (lifreq);
24336 	iocb.ic_dp = (char *)&lifreq;
24337 
24338 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24339 	/* LINTED - statement has no consequent */
24340 	if (error) {
24341 #ifdef NS_DEBUG
24342 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24343 		    "UDP error %d\n", error);
24344 #endif
24345 	}
24346 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24347 	lh = NULL;
24348 
24349 out:
24350 	/* Close layered handles */
24351 	if (lh)
24352 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24353 	if (li)
24354 		ldi_ident_release(li);
24355 
24356 	crfree(cr);
24357 }
24358 
24359 /*
24360  * This needs to be in-sync with nic_event_t definition
24361  */
24362 static const char *
24363 ill_hook_event2str(nic_event_t event)
24364 {
24365 	switch (event) {
24366 	case NE_PLUMB:
24367 		return ("PLUMB");
24368 	case NE_UNPLUMB:
24369 		return ("UNPLUMB");
24370 	case NE_UP:
24371 		return ("UP");
24372 	case NE_DOWN:
24373 		return ("DOWN");
24374 	case NE_ADDRESS_CHANGE:
24375 		return ("ADDRESS_CHANGE");
24376 	default:
24377 		return ("UNKNOWN");
24378 	}
24379 }
24380 
24381 static void
24382 ill_hook_event_destroy(ill_t *ill)
24383 {
24384 	hook_nic_event_t	*info;
24385 
24386 	if ((info = ill->ill_nic_event_info) != NULL) {
24387 		if (info->hne_data != NULL)
24388 			kmem_free(info->hne_data, info->hne_datalen);
24389 		kmem_free(info, sizeof (hook_nic_event_t));
24390 
24391 		ill->ill_nic_event_info = NULL;
24392 	}
24393 
24394 }
24395 
24396 boolean_t
24397 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24398     nic_event_data_t data, size_t datalen)
24399 {
24400 	ip_stack_t		*ipst = ill->ill_ipst;
24401 	hook_nic_event_t	*info;
24402 	const char		*str = NULL;
24403 
24404 	/* destroy nic event info if it exists */
24405 	if ((info = ill->ill_nic_event_info) != NULL) {
24406 		str = ill_hook_event2str(info->hne_event);
24407 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24408 		    "attached for %s\n", str, ill->ill_name));
24409 		ill_hook_event_destroy(ill);
24410 	}
24411 
24412 	/* create a new nic event info */
24413 	if ((info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP)) == NULL)
24414 		goto fail;
24415 
24416 	ill->ill_nic_event_info = info;
24417 
24418 	if (event == NE_UNPLUMB)
24419 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
24420 	else
24421 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24422 	info->hne_lif = lif;
24423 	info->hne_event = event;
24424 	info->hne_family = ill->ill_isv6 ?
24425 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24426 	info->hne_data = NULL;
24427 	info->hne_datalen = 0;
24428 
24429 	if (data != NULL && datalen != 0) {
24430 		info->hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24431 		if (info->hne_data != NULL) {
24432 			bcopy(data, info->hne_data, datalen);
24433 			info->hne_datalen = datalen;
24434 		} else {
24435 			ill_hook_event_destroy(ill);
24436 			goto fail;
24437 		}
24438 	}
24439 
24440 	return (B_TRUE);
24441 fail:
24442 	str = ill_hook_event2str(event);
24443 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24444 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24445 	return (B_FALSE);
24446 }
24447