xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision 8ad7418892268f9f0ba29518ab332f6a26f69fc5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 
48 #include <sys/kmem.h>
49 #include <sys/systm.h>
50 #include <sys/param.h>
51 #include <sys/socket.h>
52 #include <sys/isa_defs.h>
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/if_types.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
58 #include <sys/sockio.h>
59 #include <netinet/in.h>
60 #include <netinet/ip6.h>
61 #include <netinet/icmp6.h>
62 #include <netinet/igmp_var.h>
63 #include <sys/strsun.h>
64 #include <sys/policy.h>
65 #include <sys/ethernet.h>
66 
67 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
68 #include <inet/mi.h>
69 #include <inet/nd.h>
70 #include <inet/arp.h>
71 #include <inet/mib2.h>
72 #include <inet/ip.h>
73 #include <inet/ip6.h>
74 #include <inet/ip6_asp.h>
75 #include <inet/tcp.h>
76 #include <inet/ip_multi.h>
77 #include <inet/ip_ire.h>
78 #include <inet/ip_ftable.h>
79 #include <inet/ip_rts.h>
80 #include <inet/ip_ndp.h>
81 #include <inet/ip_if.h>
82 #include <inet/ip_impl.h>
83 #include <inet/tun.h>
84 #include <inet/sctp_ip.h>
85 #include <inet/ip_netinfo.h>
86 #include <inet/mib2.h>
87 
88 #include <net/pfkeyv2.h>
89 #include <inet/ipsec_info.h>
90 #include <inet/sadb.h>
91 #include <inet/ipsec_impl.h>
92 #include <sys/iphada.h>
93 
94 
95 #include <netinet/igmp.h>
96 #include <inet/ip_listutils.h>
97 #include <inet/ipclassifier.h>
98 #include <sys/mac.h>
99 
100 #include <sys/systeminfo.h>
101 #include <sys/bootconf.h>
102 
103 #include <sys/tsol/tndb.h>
104 #include <sys/tsol/tnet.h>
105 
106 /* The character which tells where the ill_name ends */
107 #define	IPIF_SEPARATOR_CHAR	':'
108 
109 /* IP ioctl function table entry */
110 typedef struct ipft_s {
111 	int	ipft_cmd;
112 	pfi_t	ipft_pfi;
113 	int	ipft_min_size;
114 	int	ipft_flags;
115 } ipft_t;
116 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
117 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
118 
119 typedef struct ip_sock_ar_s {
120 	union {
121 		area_t	ip_sock_area;
122 		ared_t	ip_sock_ared;
123 		areq_t	ip_sock_areq;
124 	} ip_sock_ar_u;
125 	queue_t	*ip_sock_ar_q;
126 } ip_sock_ar_t;
127 
128 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
129 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
130 		    char *value, caddr_t cp, cred_t *ioc_cr);
131 
132 static boolean_t ill_is_quiescent(ill_t *);
133 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
134 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
135 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
140     queue_t *q, mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
142     mblk_t *mp);
143 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
148     int ioccmd, struct linkblk *li, boolean_t doconsist);
149 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
150 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
151 static void	ipsq_flush(ill_t *ill);
152 
153 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
154     queue_t *q, mblk_t *mp, boolean_t need_up);
155 static void	ipsq_delete(ipsq_t *);
156 
157 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
158 		    boolean_t initialize);
159 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
160 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
161 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
162 		    boolean_t isv6);
163 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
164 static void	ipif_delete_cache_ire(ire_t *, char *);
165 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
166 static void	ipif_free(ipif_t *ipif);
167 static void	ipif_free_tail(ipif_t *ipif);
168 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
169 static void	ipif_multicast_down(ipif_t *ipif);
170 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
171 static void	ipif_set_default(ipif_t *ipif);
172 static int	ipif_set_values(queue_t *q, mblk_t *mp,
173     char *interf_name, uint_t *ppa);
174 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
175     queue_t *q);
176 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
177     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
178     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
179 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
180 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
181 
182 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
183 static int	ill_arp_off(ill_t *ill);
184 static int	ill_arp_on(ill_t *ill);
185 static void	ill_delete_interface_type(ill_if_t *);
186 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
187 static void	ill_dl_down(ill_t *ill);
188 static void	ill_down(ill_t *ill);
189 static void	ill_downi(ire_t *ire, char *ill_arg);
190 static void	ill_free_mib(ill_t *ill);
191 static void	ill_glist_delete(ill_t *);
192 static boolean_t ill_has_usable_ipif(ill_t *);
193 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
194 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
195 static void	ill_phyint_free(ill_t *ill);
196 static void	ill_phyint_reinit(ill_t *ill);
197 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
198 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
199 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
200 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
201 static void	ill_stq_cache_delete(ire_t *, char *);
202 
203 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
204 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
206     in6_addr_t *);
207 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     ipaddr_t *);
209 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
210 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
211     in6_addr_t *);
212 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     ipaddr_t *);
214 
215 static void	ipif_save_ire(ipif_t *, ire_t *);
216 static void	ipif_remove_ire(ipif_t *, ire_t *);
217 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
218 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
219 
220 /*
221  * Per-ill IPsec capabilities management.
222  */
223 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
224 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
225 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
226 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
227 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
228 static void ill_capability_proto(ill_t *, int, mblk_t *);
229 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
230     boolean_t);
231 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
234 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
236 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
238 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
239     dl_capability_sub_t *);
240 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
241 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
242 static void ill_capability_lso_reset(ill_t *, mblk_t **);
243 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
245 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
246 static void	ill_capability_dls_disable(ill_t *);
247 
248 static void	illgrp_cache_delete(ire_t *, char *);
249 static void	illgrp_delete(ill_t *ill);
250 static void	illgrp_reset_schednext(ill_t *ill);
251 
252 static ill_t	*ill_prev_usesrc(ill_t *);
253 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
254 static void	ill_disband_usesrc_group(ill_t *);
255 
256 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
257 
258 #ifdef DEBUG
259 static	void	ill_trace_cleanup(const ill_t *);
260 static	void	ipif_trace_cleanup(const ipif_t *);
261 #endif
262 
263 /*
264  * if we go over the memory footprint limit more than once in this msec
265  * interval, we'll start pruning aggressively.
266  */
267 int ip_min_frag_prune_time = 0;
268 
269 /*
270  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
271  * and the IPsec DOI
272  */
273 #define	MAX_IPSEC_ALGS	256
274 
275 #define	BITSPERBYTE	8
276 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
277 
278 #define	IPSEC_ALG_ENABLE(algs, algid) \
279 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
280 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
281 
282 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
283 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
284 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
285 
286 typedef uint8_t ipsec_capab_elem_t;
287 
288 /*
289  * Per-algorithm parameters.  Note that at present, only encryption
290  * algorithms have variable keysize (IKE does not provide a way to negotiate
291  * auth algorithm keysize).
292  *
293  * All sizes here are in bits.
294  */
295 typedef struct
296 {
297 	uint16_t	minkeylen;
298 	uint16_t	maxkeylen;
299 } ipsec_capab_algparm_t;
300 
301 /*
302  * Per-ill capabilities.
303  */
304 struct ill_ipsec_capab_s {
305 	ipsec_capab_elem_t *encr_hw_algs;
306 	ipsec_capab_elem_t *auth_hw_algs;
307 	uint32_t algs_size;	/* size of _hw_algs in bytes */
308 	/* algorithm key lengths */
309 	ipsec_capab_algparm_t *encr_algparm;
310 	uint32_t encr_algparm_size;
311 	uint32_t encr_algparm_end;
312 };
313 
314 /*
315  * The field values are larger than strictly necessary for simple
316  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
317  */
318 static area_t	ip_area_template = {
319 	AR_ENTRY_ADD,			/* area_cmd */
320 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
321 					/* area_name_offset */
322 	/* area_name_length temporarily holds this structure length */
323 	sizeof (area_t),			/* area_name_length */
324 	IP_ARP_PROTO_TYPE,		/* area_proto */
325 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
326 	IP_ADDR_LEN,			/* area_proto_addr_length */
327 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
328 					/* area_proto_mask_offset */
329 	0,				/* area_flags */
330 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
331 					/* area_hw_addr_offset */
332 	/* Zero length hw_addr_length means 'use your idea of the address' */
333 	0				/* area_hw_addr_length */
334 };
335 
336 /*
337  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
338  * support
339  */
340 static area_t	ip6_area_template = {
341 	AR_ENTRY_ADD,			/* area_cmd */
342 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
343 					/* area_name_offset */
344 	/* area_name_length temporarily holds this structure length */
345 	sizeof (area_t),			/* area_name_length */
346 	IP_ARP_PROTO_TYPE,		/* area_proto */
347 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
348 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
349 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
350 					/* area_proto_mask_offset */
351 	0,				/* area_flags */
352 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
353 					/* area_hw_addr_offset */
354 	/* Zero length hw_addr_length means 'use your idea of the address' */
355 	0				/* area_hw_addr_length */
356 };
357 
358 static ared_t	ip_ared_template = {
359 	AR_ENTRY_DELETE,
360 	sizeof (ared_t) + IP_ADDR_LEN,
361 	sizeof (ared_t),
362 	IP_ARP_PROTO_TYPE,
363 	sizeof (ared_t),
364 	IP_ADDR_LEN,
365 	0
366 };
367 
368 static ared_t	ip6_ared_template = {
369 	AR_ENTRY_DELETE,
370 	sizeof (ared_t) + IPV6_ADDR_LEN,
371 	sizeof (ared_t),
372 	IP_ARP_PROTO_TYPE,
373 	sizeof (ared_t),
374 	IPV6_ADDR_LEN,
375 	0
376 };
377 
378 /*
379  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
380  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
381  * areq is used).
382  */
383 static areq_t	ip_areq_template = {
384 	AR_ENTRY_QUERY,			/* cmd */
385 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
386 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
387 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
388 	sizeof (areq_t),			/* target addr offset */
389 	IP_ADDR_LEN,			/* target addr_length */
390 	0,				/* flags */
391 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
392 	IP_ADDR_LEN,			/* sender addr length */
393 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
394 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
395 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
396 	/* anything else filled in by the code */
397 };
398 
399 static arc_t	ip_aru_template = {
400 	AR_INTERFACE_UP,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_ard_template = {
406 	AR_INTERFACE_DOWN,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aron_template = {
412 	AR_INTERFACE_ON,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 static arc_t	ip_aroff_template = {
418 	AR_INTERFACE_OFF,
419 	sizeof (arc_t),		/* Name offset */
420 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
421 };
422 
423 static arma_t	ip_arma_multi_template = {
424 	AR_MAPPING_ADD,
425 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
426 				/* Name offset */
427 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
428 	IP_ARP_PROTO_TYPE,
429 	sizeof (arma_t),			/* proto_addr_offset */
430 	IP_ADDR_LEN,				/* proto_addr_length */
431 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
432 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
433 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
434 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
435 	IP_MAX_HW_LEN,				/* hw_addr_length */
436 	0,					/* hw_mapping_start */
437 };
438 
439 static ipft_t	ip_ioctl_ftbl[] = {
440 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
441 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
444 		IPFT_F_NO_REPLY },
445 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
446 	{ 0 }
447 };
448 
449 /* Simple ICMP IP Header Template */
450 static ipha_t icmp_ipha = {
451 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
452 };
453 
454 /* Flag descriptors for ip_ipif_report */
455 static nv_t	ipif_nv_tbl[] = {
456 	{ IPIF_UP,		"UP" },
457 	{ IPIF_BROADCAST,	"BROADCAST" },
458 	{ ILLF_DEBUG,		"DEBUG" },
459 	{ PHYI_LOOPBACK,	"LOOPBACK" },
460 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
461 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
462 	{ PHYI_RUNNING,		"RUNNING" },
463 	{ ILLF_NOARP,		"NOARP" },
464 	{ PHYI_PROMISC,		"PROMISC" },
465 	{ PHYI_ALLMULTI,	"ALLMULTI" },
466 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
467 	{ ILLF_MULTICAST,	"MULTICAST" },
468 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
469 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
470 	{ IPIF_DHCPRUNNING,	"DHCP" },
471 	{ IPIF_PRIVATE,		"PRIVATE" },
472 	{ IPIF_NOXMIT,		"NOXMIT" },
473 	{ IPIF_NOLOCAL,		"NOLOCAL" },
474 	{ IPIF_DEPRECATED,	"DEPRECATED" },
475 	{ IPIF_PREFERRED,	"PREFERRED" },
476 	{ IPIF_TEMPORARY,	"TEMPORARY" },
477 	{ IPIF_ADDRCONF,	"ADDRCONF" },
478 	{ PHYI_VIRTUAL,		"VIRTUAL" },
479 	{ ILLF_ROUTER,		"ROUTER" },
480 	{ ILLF_NONUD,		"NONUD" },
481 	{ IPIF_ANYCAST,		"ANYCAST" },
482 	{ ILLF_NORTEXCH,	"NORTEXCH" },
483 	{ ILLF_IPV4,		"IPV4" },
484 	{ ILLF_IPV6,		"IPV6" },
485 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
486 	{ PHYI_FAILED,		"FAILED" },
487 	{ PHYI_STANDBY,		"STANDBY" },
488 	{ PHYI_INACTIVE,	"INACTIVE" },
489 	{ PHYI_OFFLINE,		"OFFLINE" },
490 };
491 
492 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
493 
494 static ip_m_t	ip_m_tbl[] = {
495 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_ether_v6intfid },
497 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_ether_v6intfid },
505 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
506 	    ip_ib_v6intfid },
507 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
508 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
509 	    ip_nodef_v6intfid }
510 };
511 
512 static ill_t	ill_null;		/* Empty ILL for init. */
513 char	ipif_loopback_name[] = "lo0";
514 static char *ipv4_forward_suffix = ":ip_forwarding";
515 static char *ipv6_forward_suffix = ":ip6_forwarding";
516 static	sin6_t	sin6_null;	/* Zero address for quick clears */
517 static	sin_t	sin_null;	/* Zero address for quick clears */
518 
519 /* When set search for unused ipif_seqid */
520 static ipif_t	ipif_zero;
521 
522 /*
523  * ppa arena is created after these many
524  * interfaces have been plumbed.
525  */
526 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
527 
528 /*
529  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
530  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
531  * set through platform specific code (Niagara/Ontario).
532  */
533 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
534 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
535 
536 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
537 
538 static uint_t
539 ipif_rand(ip_stack_t *ipst)
540 {
541 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
542 	    12345;
543 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
544 }
545 
546 /*
547  * Allocate per-interface mibs.
548  * Returns true if ok. False otherwise.
549  *  ipsq  may not yet be allocated (loopback case ).
550  */
551 static boolean_t
552 ill_allocate_mibs(ill_t *ill)
553 {
554 	/* Already allocated? */
555 	if (ill->ill_ip_mib != NULL) {
556 		if (ill->ill_isv6)
557 			ASSERT(ill->ill_icmp6_mib != NULL);
558 		return (B_TRUE);
559 	}
560 
561 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
562 	    KM_NOSLEEP);
563 	if (ill->ill_ip_mib == NULL) {
564 		return (B_FALSE);
565 	}
566 
567 	/* Setup static information */
568 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
569 	    sizeof (mib2_ipIfStatsEntry_t));
570 	if (ill->ill_isv6) {
571 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
573 		    sizeof (mib2_ipv6AddrEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
575 		    sizeof (mib2_ipv6RouteEntry_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
577 		    sizeof (mib2_ipv6NetToMediaEntry_t));
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
579 		    sizeof (ipv6_member_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
581 		    sizeof (ipv6_grpsrc_t));
582 	} else {
583 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
585 		    sizeof (mib2_ipAddrEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
587 		    sizeof (mib2_ipRouteEntry_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
589 		    sizeof (mib2_ipNetToMediaEntry_t));
590 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
591 		    sizeof (ip_member_t));
592 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
593 		    sizeof (ip_grpsrc_t));
594 
595 		/*
596 		 * For a v4 ill, we are done at this point, because per ill
597 		 * icmp mibs are only used for v6.
598 		 */
599 		return (B_TRUE);
600 	}
601 
602 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
603 	    KM_NOSLEEP);
604 	if (ill->ill_icmp6_mib == NULL) {
605 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
606 		ill->ill_ip_mib = NULL;
607 		return (B_FALSE);
608 	}
609 	/* static icmp info */
610 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
611 	    sizeof (mib2_ipv6IfIcmpEntry_t);
612 	/*
613 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
614 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
615 	 * -> ill_phyint_reinit
616 	 */
617 	return (B_TRUE);
618 }
619 
620 /*
621  * Common code for preparation of ARP commands.  Two points to remember:
622  * 	1) The ill_name is tacked on at the end of the allocated space so
623  *	   the templates name_offset field must contain the total space
624  *	   to allocate less the name length.
625  *
626  *	2) The templates name_length field should contain the *template*
627  *	   length.  We use it as a parameter to bcopy() and then write
628  *	   the real ill_name_length into the name_length field of the copy.
629  * (Always called as writer.)
630  */
631 mblk_t *
632 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
633 {
634 	arc_t	*arc = (arc_t *)template;
635 	char	*cp;
636 	int	len;
637 	mblk_t	*mp;
638 	uint_t	name_length = ill->ill_name_length;
639 	uint_t	template_len = arc->arc_name_length;
640 
641 	len = arc->arc_name_offset + name_length;
642 	mp = allocb(len, BPRI_HI);
643 	if (mp == NULL)
644 		return (NULL);
645 	cp = (char *)mp->b_rptr;
646 	mp->b_wptr = (uchar_t *)&cp[len];
647 	if (template_len)
648 		bcopy(template, cp, template_len);
649 	if (len > template_len)
650 		bzero(&cp[template_len], len - template_len);
651 	mp->b_datap->db_type = M_PROTO;
652 
653 	arc = (arc_t *)cp;
654 	arc->arc_name_length = name_length;
655 	cp = (char *)arc + arc->arc_name_offset;
656 	bcopy(ill->ill_name, cp, name_length);
657 
658 	if (addr) {
659 		area_t	*area = (area_t *)mp->b_rptr;
660 
661 		cp = (char *)area + area->area_proto_addr_offset;
662 		bcopy(addr, cp, area->area_proto_addr_length);
663 		if (area->area_cmd == AR_ENTRY_ADD) {
664 			cp = (char *)area;
665 			len = area->area_proto_addr_length;
666 			if (area->area_proto_mask_offset)
667 				cp += area->area_proto_mask_offset;
668 			else
669 				cp += area->area_proto_addr_offset + len;
670 			while (len-- > 0)
671 				*cp++ = (char)~0;
672 		}
673 	}
674 	return (mp);
675 }
676 
677 mblk_t *
678 ipif_area_alloc(ipif_t *ipif)
679 {
680 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
681 	    (char *)&ipif->ipif_lcl_addr));
682 }
683 
684 mblk_t *
685 ipif_ared_alloc(ipif_t *ipif)
686 {
687 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
688 	    (char *)&ipif->ipif_lcl_addr));
689 }
690 
691 mblk_t *
692 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
693 {
694 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
695 	    (char *)&addr));
696 }
697 
698 /*
699  * Completely vaporize a lower level tap and all associated interfaces.
700  * ill_delete is called only out of ip_close when the device control
701  * stream is being closed.
702  */
703 void
704 ill_delete(ill_t *ill)
705 {
706 	ipif_t	*ipif;
707 	ill_t	*prev_ill;
708 	ip_stack_t	*ipst = ill->ill_ipst;
709 
710 	/*
711 	 * ill_delete may be forcibly entering the ipsq. The previous
712 	 * ioctl may not have completed and may need to be aborted.
713 	 * ipsq_flush takes care of it. If we don't need to enter the
714 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
715 	 * ill_delete_tail is sufficient.
716 	 */
717 	ipsq_flush(ill);
718 
719 	/*
720 	 * Nuke all interfaces.  ipif_free will take down the interface,
721 	 * remove it from the list, and free the data structure.
722 	 * Walk down the ipif list and remove the logical interfaces
723 	 * first before removing the main ipif. We can't unplumb
724 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
725 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
726 	 * POINTOPOINT.
727 	 *
728 	 * If ill_ipif was not properly initialized (i.e low on memory),
729 	 * then no interfaces to clean up. In this case just clean up the
730 	 * ill.
731 	 */
732 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
733 		ipif_free(ipif);
734 
735 	/*
736 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
737 	 * So nobody can be using this mp now. Free the mp allocated for
738 	 * honoring ILLF_NOARP
739 	 */
740 	freemsg(ill->ill_arp_on_mp);
741 	ill->ill_arp_on_mp = NULL;
742 
743 	/* Clean up msgs on pending upcalls for mrouted */
744 	reset_mrt_ill(ill);
745 
746 	/*
747 	 * ipif_free -> reset_conn_ipif will remove all multicast
748 	 * references for IPv4. For IPv6, we need to do it here as
749 	 * it points only at ills.
750 	 */
751 	reset_conn_ill(ill);
752 
753 	/*
754 	 * ill_down will arrange to blow off any IRE's dependent on this
755 	 * ILL, and shut down fragmentation reassembly.
756 	 */
757 	ill_down(ill);
758 
759 	/* Let SCTP know, so that it can remove this from its list. */
760 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
761 
762 	/*
763 	 * If an address on this ILL is being used as a source address then
764 	 * clear out the pointers in other ILLs that point to this ILL.
765 	 */
766 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
767 	if (ill->ill_usesrc_grp_next != NULL) {
768 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
769 			ill_disband_usesrc_group(ill);
770 		} else {	/* consumer of the usesrc ILL */
771 			prev_ill = ill_prev_usesrc(ill);
772 			prev_ill->ill_usesrc_grp_next =
773 			    ill->ill_usesrc_grp_next;
774 		}
775 	}
776 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
777 }
778 
779 static void
780 ipif_non_duplicate(ipif_t *ipif)
781 {
782 	ill_t *ill = ipif->ipif_ill;
783 	mutex_enter(&ill->ill_lock);
784 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
785 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
786 		ASSERT(ill->ill_ipif_dup_count > 0);
787 		ill->ill_ipif_dup_count--;
788 	}
789 	mutex_exit(&ill->ill_lock);
790 }
791 
792 /*
793  * ill_delete_tail is called from ip_modclose after all references
794  * to the closing ill are gone. The wait is done in ip_modclose
795  */
796 void
797 ill_delete_tail(ill_t *ill)
798 {
799 	mblk_t	**mpp;
800 	ipif_t	*ipif;
801 	ip_stack_t	*ipst = ill->ill_ipst;
802 
803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
804 		ipif_non_duplicate(ipif);
805 		ipif_down_tail(ipif);
806 	}
807 
808 	ASSERT(ill->ill_ipif_dup_count == 0 &&
809 	    ill->ill_arp_down_mp == NULL &&
810 	    ill->ill_arp_del_mapping_mp == NULL);
811 
812 	/*
813 	 * If polling capability is enabled (which signifies direct
814 	 * upcall into IP and driver has ill saved as a handle),
815 	 * we need to make sure that unbind has completed before we
816 	 * let the ill disappear and driver no longer has any reference
817 	 * to this ill.
818 	 */
819 	mutex_enter(&ill->ill_lock);
820 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
821 		cv_wait(&ill->ill_cv, &ill->ill_lock);
822 	mutex_exit(&ill->ill_lock);
823 
824 	/*
825 	 * Clean up polling and soft ring capabilities
826 	 */
827 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
828 		ill_capability_dls_disable(ill);
829 
830 	if (ill->ill_net_type != IRE_LOOPBACK)
831 		qprocsoff(ill->ill_rq);
832 
833 	/*
834 	 * We do an ipsq_flush once again now. New messages could have
835 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
836 	 * could also have landed up if an ioctl thread had looked up
837 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
838 	 * enqueued the ioctl when we did the ipsq_flush last time.
839 	 */
840 	ipsq_flush(ill);
841 
842 	/*
843 	 * Free capabilities.
844 	 */
845 	if (ill->ill_ipsec_capab_ah != NULL) {
846 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
847 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
848 		ill->ill_ipsec_capab_ah = NULL;
849 	}
850 
851 	if (ill->ill_ipsec_capab_esp != NULL) {
852 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
853 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
854 		ill->ill_ipsec_capab_esp = NULL;
855 	}
856 
857 	if (ill->ill_mdt_capab != NULL) {
858 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
859 		ill->ill_mdt_capab = NULL;
860 	}
861 
862 	if (ill->ill_hcksum_capab != NULL) {
863 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
864 		ill->ill_hcksum_capab = NULL;
865 	}
866 
867 	if (ill->ill_zerocopy_capab != NULL) {
868 		kmem_free(ill->ill_zerocopy_capab,
869 		    sizeof (ill_zerocopy_capab_t));
870 		ill->ill_zerocopy_capab = NULL;
871 	}
872 
873 	if (ill->ill_lso_capab != NULL) {
874 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
875 		ill->ill_lso_capab = NULL;
876 	}
877 
878 	if (ill->ill_dls_capab != NULL) {
879 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
880 		ill->ill_dls_capab->ill_unbind_conn = NULL;
881 		kmem_free(ill->ill_dls_capab,
882 		    sizeof (ill_dls_capab_t) +
883 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
884 		ill->ill_dls_capab = NULL;
885 	}
886 
887 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
888 
889 	while (ill->ill_ipif != NULL)
890 		ipif_free_tail(ill->ill_ipif);
891 
892 	/*
893 	 * We have removed all references to ilm from conn and the ones joined
894 	 * within the kernel.
895 	 *
896 	 * We don't walk conns, mrts and ires because
897 	 *
898 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
899 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
900 	 *    ill references.
901 	 */
902 	ASSERT(ilm_walk_ill(ill) == 0);
903 	/*
904 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
905 	 * could free the phyint. No more reference to the phyint after this
906 	 * point.
907 	 */
908 	(void) ill_glist_delete(ill);
909 
910 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
911 	if (ill->ill_ndd_name != NULL)
912 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
913 	rw_exit(&ipst->ips_ip_g_nd_lock);
914 
915 	if (ill->ill_frag_ptr != NULL) {
916 		uint_t count;
917 
918 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
919 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
920 		}
921 		mi_free(ill->ill_frag_ptr);
922 		ill->ill_frag_ptr = NULL;
923 		ill->ill_frag_hash_tbl = NULL;
924 	}
925 
926 	freemsg(ill->ill_nd_lla_mp);
927 	/* Free all retained control messages. */
928 	mpp = &ill->ill_first_mp_to_free;
929 	do {
930 		while (mpp[0]) {
931 			mblk_t  *mp;
932 			mblk_t  *mp1;
933 
934 			mp = mpp[0];
935 			mpp[0] = mp->b_next;
936 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
937 				mp1->b_next = NULL;
938 				mp1->b_prev = NULL;
939 			}
940 			freemsg(mp);
941 		}
942 	} while (mpp++ != &ill->ill_last_mp_to_free);
943 
944 	ill_free_mib(ill);
945 
946 #ifdef DEBUG
947 	ill_trace_cleanup(ill);
948 #endif
949 
950 	/* Drop refcnt here */
951 	netstack_rele(ill->ill_ipst->ips_netstack);
952 	ill->ill_ipst = NULL;
953 }
954 
955 static void
956 ill_free_mib(ill_t *ill)
957 {
958 	ip_stack_t *ipst = ill->ill_ipst;
959 
960 	/*
961 	 * MIB statistics must not be lost, so when an interface
962 	 * goes away the counter values will be added to the global
963 	 * MIBs.
964 	 */
965 	if (ill->ill_ip_mib != NULL) {
966 		if (ill->ill_isv6) {
967 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
968 			    ill->ill_ip_mib);
969 		} else {
970 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
971 			    ill->ill_ip_mib);
972 		}
973 
974 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
975 		ill->ill_ip_mib = NULL;
976 	}
977 	if (ill->ill_icmp6_mib != NULL) {
978 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
979 		    ill->ill_icmp6_mib);
980 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
981 		ill->ill_icmp6_mib = NULL;
982 	}
983 }
984 
985 /*
986  * Concatenate together a physical address and a sap.
987  *
988  * Sap_lengths are interpreted as follows:
989  *   sap_length == 0	==>	no sap
990  *   sap_length > 0	==>	sap is at the head of the dlpi address
991  *   sap_length < 0	==>	sap is at the tail of the dlpi address
992  */
993 static void
994 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
995     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
996 {
997 	uint16_t sap_addr = (uint16_t)sap_src;
998 
999 	if (sap_length == 0) {
1000 		if (phys_src == NULL)
1001 			bzero(dst, phys_length);
1002 		else
1003 			bcopy(phys_src, dst, phys_length);
1004 	} else if (sap_length < 0) {
1005 		if (phys_src == NULL)
1006 			bzero(dst, phys_length);
1007 		else
1008 			bcopy(phys_src, dst, phys_length);
1009 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1010 	} else {
1011 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1012 		if (phys_src == NULL)
1013 			bzero((char *)dst + sap_length, phys_length);
1014 		else
1015 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1016 	}
1017 }
1018 
1019 /*
1020  * Generate a dl_unitdata_req mblk for the device and address given.
1021  * addr_length is the length of the physical portion of the address.
1022  * If addr is NULL include an all zero address of the specified length.
1023  * TRUE? In any case, addr_length is taken to be the entire length of the
1024  * dlpi address, including the absolute value of sap_length.
1025  */
1026 mblk_t *
1027 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1028 		t_scalar_t sap_length)
1029 {
1030 	dl_unitdata_req_t *dlur;
1031 	mblk_t	*mp;
1032 	t_scalar_t	abs_sap_length;		/* absolute value */
1033 
1034 	abs_sap_length = ABS(sap_length);
1035 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1036 	    DL_UNITDATA_REQ);
1037 	if (mp == NULL)
1038 		return (NULL);
1039 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1040 	/* HACK: accomodate incompatible DLPI drivers */
1041 	if (addr_length == 8)
1042 		addr_length = 6;
1043 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1044 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1045 	dlur->dl_priority.dl_min = 0;
1046 	dlur->dl_priority.dl_max = 0;
1047 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1048 	    (uchar_t *)&dlur[1]);
1049 	return (mp);
1050 }
1051 
1052 /*
1053  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1054  * Return an error if we already have 1 or more ioctls in progress.
1055  * This is used only for non-exclusive ioctls. Currently this is used
1056  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1057  * and thus need to use ipsq_pending_mp_add.
1058  */
1059 boolean_t
1060 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1061 {
1062 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1063 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1064 	/*
1065 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1066 	 */
1067 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1068 	    (add_mp->b_datap->db_type == M_IOCTL));
1069 
1070 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1071 	/*
1072 	 * Return error if the conn has started closing. The conn
1073 	 * could have finished cleaning up the pending mp list,
1074 	 * If so we should not add another mp to the list negating
1075 	 * the cleanup.
1076 	 */
1077 	if (connp->conn_state_flags & CONN_CLOSING)
1078 		return (B_FALSE);
1079 	/*
1080 	 * Add the pending mp to the head of the list, chained by b_next.
1081 	 * Note down the conn on which the ioctl request came, in b_prev.
1082 	 * This will be used to later get the conn, when we get a response
1083 	 * on the ill queue, from some other module (typically arp)
1084 	 */
1085 	add_mp->b_next = (void *)ill->ill_pending_mp;
1086 	add_mp->b_queue = CONNP_TO_WQ(connp);
1087 	ill->ill_pending_mp = add_mp;
1088 	if (connp != NULL)
1089 		connp->conn_oper_pending_ill = ill;
1090 	return (B_TRUE);
1091 }
1092 
1093 /*
1094  * Retrieve the ill_pending_mp and return it. We have to walk the list
1095  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1096  */
1097 mblk_t *
1098 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1099 {
1100 	mblk_t	*prev = NULL;
1101 	mblk_t	*curr = NULL;
1102 	uint_t	id;
1103 	conn_t	*connp;
1104 
1105 	/*
1106 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1107 	 * up the pending mp, but it does not know the ioc_id and
1108 	 * passes in a zero for it.
1109 	 */
1110 	mutex_enter(&ill->ill_lock);
1111 	if (ioc_id != 0)
1112 		*connpp = NULL;
1113 
1114 	/* Search the list for the appropriate ioctl based on ioc_id */
1115 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1116 	    prev = curr, curr = curr->b_next) {
1117 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1118 		connp = Q_TO_CONN(curr->b_queue);
1119 		/* Match based on the ioc_id or based on the conn */
1120 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1121 			break;
1122 	}
1123 
1124 	if (curr != NULL) {
1125 		/* Unlink the mblk from the pending mp list */
1126 		if (prev != NULL) {
1127 			prev->b_next = curr->b_next;
1128 		} else {
1129 			ASSERT(ill->ill_pending_mp == curr);
1130 			ill->ill_pending_mp = curr->b_next;
1131 		}
1132 
1133 		/*
1134 		 * conn refcnt must have been bumped up at the start of
1135 		 * the ioctl. So we can safely access the conn.
1136 		 */
1137 		ASSERT(CONN_Q(curr->b_queue));
1138 		*connpp = Q_TO_CONN(curr->b_queue);
1139 		curr->b_next = NULL;
1140 		curr->b_queue = NULL;
1141 	}
1142 
1143 	mutex_exit(&ill->ill_lock);
1144 
1145 	return (curr);
1146 }
1147 
1148 /*
1149  * Add the pending mp to the list. There can be only 1 pending mp
1150  * in the list. Any exclusive ioctl that needs to wait for a response
1151  * from another module or driver needs to use this function to set
1152  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1153  * the other module/driver. This is also used while waiting for the
1154  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1155  */
1156 boolean_t
1157 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1158     int waitfor)
1159 {
1160 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1161 
1162 	ASSERT(IAM_WRITER_IPIF(ipif));
1163 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1164 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1165 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1166 	/*
1167 	 * The caller may be using a different ipif than the one passed into
1168 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1169 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1170 	 * that `ipsq_current_ipif == ipif'.
1171 	 */
1172 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1173 
1174 	/*
1175 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1176 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1177 	 */
1178 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1179 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1180 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1181 
1182 	if (connp != NULL) {
1183 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1184 		/*
1185 		 * Return error if the conn has started closing. The conn
1186 		 * could have finished cleaning up the pending mp list,
1187 		 * If so we should not add another mp to the list negating
1188 		 * the cleanup.
1189 		 */
1190 		if (connp->conn_state_flags & CONN_CLOSING)
1191 			return (B_FALSE);
1192 	}
1193 	mutex_enter(&ipsq->ipsq_lock);
1194 	ipsq->ipsq_pending_ipif = ipif;
1195 	/*
1196 	 * Note down the queue in b_queue. This will be returned by
1197 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1198 	 * the processing
1199 	 */
1200 	add_mp->b_next = NULL;
1201 	add_mp->b_queue = q;
1202 	ipsq->ipsq_pending_mp = add_mp;
1203 	ipsq->ipsq_waitfor = waitfor;
1204 
1205 	if (connp != NULL)
1206 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1207 	mutex_exit(&ipsq->ipsq_lock);
1208 	return (B_TRUE);
1209 }
1210 
1211 /*
1212  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1213  * queued in the list.
1214  */
1215 mblk_t *
1216 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1217 {
1218 	mblk_t	*curr = NULL;
1219 
1220 	mutex_enter(&ipsq->ipsq_lock);
1221 	*connpp = NULL;
1222 	if (ipsq->ipsq_pending_mp == NULL) {
1223 		mutex_exit(&ipsq->ipsq_lock);
1224 		return (NULL);
1225 	}
1226 
1227 	/* There can be only 1 such excl message */
1228 	curr = ipsq->ipsq_pending_mp;
1229 	ASSERT(curr != NULL && curr->b_next == NULL);
1230 	ipsq->ipsq_pending_ipif = NULL;
1231 	ipsq->ipsq_pending_mp = NULL;
1232 	ipsq->ipsq_waitfor = 0;
1233 	mutex_exit(&ipsq->ipsq_lock);
1234 
1235 	if (CONN_Q(curr->b_queue)) {
1236 		/*
1237 		 * This mp did a refhold on the conn, at the start of the ioctl.
1238 		 * So we can safely return a pointer to the conn to the caller.
1239 		 */
1240 		*connpp = Q_TO_CONN(curr->b_queue);
1241 	} else {
1242 		*connpp = NULL;
1243 	}
1244 	curr->b_next = NULL;
1245 	curr->b_prev = NULL;
1246 	return (curr);
1247 }
1248 
1249 /*
1250  * Cleanup the ioctl mp queued in ipsq_pending_mp
1251  * - Called in the ill_delete path
1252  * - Called in the M_ERROR or M_HANGUP path on the ill.
1253  * - Called in the conn close path.
1254  */
1255 boolean_t
1256 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1257 {
1258 	mblk_t	*mp;
1259 	ipsq_t	*ipsq;
1260 	queue_t	*q;
1261 	ipif_t	*ipif;
1262 
1263 	ASSERT(IAM_WRITER_ILL(ill));
1264 	ipsq = ill->ill_phyint->phyint_ipsq;
1265 	mutex_enter(&ipsq->ipsq_lock);
1266 	/*
1267 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1268 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1269 	 * even if it is meant for another ill, since we have to enqueue
1270 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1271 	 * If connp is non-null we are called from the conn close path.
1272 	 */
1273 	mp = ipsq->ipsq_pending_mp;
1274 	if (mp == NULL || (connp != NULL &&
1275 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1276 		mutex_exit(&ipsq->ipsq_lock);
1277 		return (B_FALSE);
1278 	}
1279 	/* Now remove from the ipsq_pending_mp */
1280 	ipsq->ipsq_pending_mp = NULL;
1281 	q = mp->b_queue;
1282 	mp->b_next = NULL;
1283 	mp->b_prev = NULL;
1284 	mp->b_queue = NULL;
1285 
1286 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1287 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1288 	if (ill->ill_move_in_progress) {
1289 		ILL_CLEAR_MOVE(ill);
1290 	} else if (ill->ill_up_ipifs) {
1291 		ill_group_cleanup(ill);
1292 	}
1293 
1294 	ipif = ipsq->ipsq_pending_ipif;
1295 	ipsq->ipsq_pending_ipif = NULL;
1296 	ipsq->ipsq_waitfor = 0;
1297 	ipsq->ipsq_current_ipif = NULL;
1298 	ipsq->ipsq_current_ioctl = 0;
1299 	ipsq->ipsq_current_done = B_TRUE;
1300 	mutex_exit(&ipsq->ipsq_lock);
1301 
1302 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1303 		if (connp == NULL) {
1304 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1305 		} else {
1306 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1307 			mutex_enter(&ipif->ipif_ill->ill_lock);
1308 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1309 			mutex_exit(&ipif->ipif_ill->ill_lock);
1310 		}
1311 	} else {
1312 		/*
1313 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1314 		 * be just inet_freemsg. we have to restart it
1315 		 * otherwise the thread will be stuck.
1316 		 */
1317 		inet_freemsg(mp);
1318 	}
1319 	return (B_TRUE);
1320 }
1321 
1322 /*
1323  * The ill is closing. Cleanup all the pending mps. Called exclusively
1324  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1325  * knows this ill, and hence nobody can add an mp to this list
1326  */
1327 static void
1328 ill_pending_mp_cleanup(ill_t *ill)
1329 {
1330 	mblk_t	*mp;
1331 	queue_t	*q;
1332 
1333 	ASSERT(IAM_WRITER_ILL(ill));
1334 
1335 	mutex_enter(&ill->ill_lock);
1336 	/*
1337 	 * Every mp on the pending mp list originating from an ioctl
1338 	 * added 1 to the conn refcnt, at the start of the ioctl.
1339 	 * So bump it down now.  See comments in ip_wput_nondata()
1340 	 */
1341 	while (ill->ill_pending_mp != NULL) {
1342 		mp = ill->ill_pending_mp;
1343 		ill->ill_pending_mp = mp->b_next;
1344 		mutex_exit(&ill->ill_lock);
1345 
1346 		q = mp->b_queue;
1347 		ASSERT(CONN_Q(q));
1348 		mp->b_next = NULL;
1349 		mp->b_prev = NULL;
1350 		mp->b_queue = NULL;
1351 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1352 		mutex_enter(&ill->ill_lock);
1353 	}
1354 	ill->ill_pending_ipif = NULL;
1355 
1356 	mutex_exit(&ill->ill_lock);
1357 }
1358 
1359 /*
1360  * Called in the conn close path and ill delete path
1361  */
1362 static void
1363 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1364 {
1365 	ipsq_t	*ipsq;
1366 	mblk_t	*prev;
1367 	mblk_t	*curr;
1368 	mblk_t	*next;
1369 	queue_t	*q;
1370 	mblk_t	*tmp_list = NULL;
1371 
1372 	ASSERT(IAM_WRITER_ILL(ill));
1373 	if (connp != NULL)
1374 		q = CONNP_TO_WQ(connp);
1375 	else
1376 		q = ill->ill_wq;
1377 
1378 	ipsq = ill->ill_phyint->phyint_ipsq;
1379 	/*
1380 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1381 	 * In the case of ioctl from a conn, there can be only 1 mp
1382 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1383 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1384 	 * ioctls meant for this ill form conn's are not flushed. They will
1385 	 * be processed during ipsq_exit and will not find the ill and will
1386 	 * return error.
1387 	 */
1388 	mutex_enter(&ipsq->ipsq_lock);
1389 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1390 	    curr = next) {
1391 		next = curr->b_next;
1392 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1393 			/* Unlink the mblk from the pending mp list */
1394 			if (prev != NULL) {
1395 				prev->b_next = curr->b_next;
1396 			} else {
1397 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1398 				ipsq->ipsq_xopq_mphead = curr->b_next;
1399 			}
1400 			if (ipsq->ipsq_xopq_mptail == curr)
1401 				ipsq->ipsq_xopq_mptail = prev;
1402 			/*
1403 			 * Create a temporary list and release the ipsq lock
1404 			 * New elements are added to the head of the tmp_list
1405 			 */
1406 			curr->b_next = tmp_list;
1407 			tmp_list = curr;
1408 		} else {
1409 			prev = curr;
1410 		}
1411 	}
1412 	mutex_exit(&ipsq->ipsq_lock);
1413 
1414 	while (tmp_list != NULL) {
1415 		curr = tmp_list;
1416 		tmp_list = curr->b_next;
1417 		curr->b_next = NULL;
1418 		curr->b_prev = NULL;
1419 		curr->b_queue = NULL;
1420 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1421 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1422 			    CONN_CLOSE : NO_COPYOUT, NULL);
1423 		} else {
1424 			/*
1425 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1426 			 * this can't be just inet_freemsg. we have to
1427 			 * restart it otherwise the thread will be stuck.
1428 			 */
1429 			inet_freemsg(curr);
1430 		}
1431 	}
1432 }
1433 
1434 /*
1435  * This conn has started closing. Cleanup any pending ioctl from this conn.
1436  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1437  */
1438 void
1439 conn_ioctl_cleanup(conn_t *connp)
1440 {
1441 	mblk_t *curr;
1442 	ipsq_t	*ipsq;
1443 	ill_t	*ill;
1444 	boolean_t refheld;
1445 
1446 	/*
1447 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1448 	 * ioctl has not yet started, the mp is pending in the list headed by
1449 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1450 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1451 	 * is currently executing now the mp is not queued anywhere but
1452 	 * conn_oper_pending_ill is null. The conn close will wait
1453 	 * till the conn_ref drops to zero.
1454 	 */
1455 	mutex_enter(&connp->conn_lock);
1456 	ill = connp->conn_oper_pending_ill;
1457 	if (ill == NULL) {
1458 		mutex_exit(&connp->conn_lock);
1459 		return;
1460 	}
1461 
1462 	curr = ill_pending_mp_get(ill, &connp, 0);
1463 	if (curr != NULL) {
1464 		mutex_exit(&connp->conn_lock);
1465 		CONN_DEC_REF(connp);
1466 		inet_freemsg(curr);
1467 		return;
1468 	}
1469 	/*
1470 	 * We may not be able to refhold the ill if the ill/ipif
1471 	 * is changing. But we need to make sure that the ill will
1472 	 * not vanish. So we just bump up the ill_waiter count.
1473 	 */
1474 	refheld = ill_waiter_inc(ill);
1475 	mutex_exit(&connp->conn_lock);
1476 	if (refheld) {
1477 		if (ipsq_enter(ill, B_TRUE)) {
1478 			ill_waiter_dcr(ill);
1479 			/*
1480 			 * Check whether this ioctl has started and is
1481 			 * pending now in ipsq_pending_mp. If it is not
1482 			 * found there then check whether this ioctl has
1483 			 * not even started and is in the ipsq_xopq list.
1484 			 */
1485 			if (!ipsq_pending_mp_cleanup(ill, connp))
1486 				ipsq_xopq_mp_cleanup(ill, connp);
1487 			ipsq = ill->ill_phyint->phyint_ipsq;
1488 			ipsq_exit(ipsq);
1489 			return;
1490 		}
1491 	}
1492 
1493 	/*
1494 	 * The ill is also closing and we could not bump up the
1495 	 * ill_waiter_count or we could not enter the ipsq. Leave
1496 	 * the cleanup to ill_delete
1497 	 */
1498 	mutex_enter(&connp->conn_lock);
1499 	while (connp->conn_oper_pending_ill != NULL)
1500 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1501 	mutex_exit(&connp->conn_lock);
1502 	if (refheld)
1503 		ill_waiter_dcr(ill);
1504 }
1505 
1506 /*
1507  * ipcl_walk function for cleaning up conn_*_ill fields.
1508  */
1509 static void
1510 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1511 {
1512 	ill_t	*ill = (ill_t *)arg;
1513 	ire_t	*ire;
1514 
1515 	mutex_enter(&connp->conn_lock);
1516 	if (connp->conn_multicast_ill == ill) {
1517 		/* Revert to late binding */
1518 		connp->conn_multicast_ill = NULL;
1519 		connp->conn_orig_multicast_ifindex = 0;
1520 	}
1521 	if (connp->conn_incoming_ill == ill)
1522 		connp->conn_incoming_ill = NULL;
1523 	if (connp->conn_outgoing_ill == ill)
1524 		connp->conn_outgoing_ill = NULL;
1525 	if (connp->conn_outgoing_pill == ill)
1526 		connp->conn_outgoing_pill = NULL;
1527 	if (connp->conn_nofailover_ill == ill)
1528 		connp->conn_nofailover_ill = NULL;
1529 	if (connp->conn_dhcpinit_ill == ill) {
1530 		connp->conn_dhcpinit_ill = NULL;
1531 		ASSERT(ill->ill_dhcpinit != 0);
1532 		atomic_dec_32(&ill->ill_dhcpinit);
1533 	}
1534 	if (connp->conn_ire_cache != NULL) {
1535 		ire = connp->conn_ire_cache;
1536 		/*
1537 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1538 		 * interface X and ipif coming from interface Y, if interface
1539 		 * X and Y are part of the same IPMPgroup. Thus whenever
1540 		 * interface X goes down, remove all references to it by
1541 		 * checking both on ire_ipif and ire_stq.
1542 		 */
1543 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1544 		    (ire->ire_type == IRE_CACHE &&
1545 		    ire->ire_stq == ill->ill_wq)) {
1546 			connp->conn_ire_cache = NULL;
1547 			mutex_exit(&connp->conn_lock);
1548 			ire_refrele_notr(ire);
1549 			return;
1550 		}
1551 	}
1552 	mutex_exit(&connp->conn_lock);
1553 }
1554 
1555 /* ARGSUSED */
1556 void
1557 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1558 {
1559 	ill_t	*ill = q->q_ptr;
1560 	ipif_t	*ipif;
1561 
1562 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1563 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1564 		ipif_non_duplicate(ipif);
1565 		ipif_down_tail(ipif);
1566 	}
1567 	freemsg(mp);
1568 	ipsq_current_finish(ipsq);
1569 }
1570 
1571 /*
1572  * ill_down_start is called when we want to down this ill and bring it up again
1573  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1574  * all interfaces, but don't tear down any plumbing.
1575  */
1576 boolean_t
1577 ill_down_start(queue_t *q, mblk_t *mp)
1578 {
1579 	ill_t	*ill = q->q_ptr;
1580 	ipif_t	*ipif;
1581 
1582 	ASSERT(IAM_WRITER_ILL(ill));
1583 
1584 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1585 		(void) ipif_down(ipif, NULL, NULL);
1586 
1587 	ill_down(ill);
1588 
1589 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1590 
1591 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1592 
1593 	/*
1594 	 * Atomically test and add the pending mp if references are active.
1595 	 */
1596 	mutex_enter(&ill->ill_lock);
1597 	if (!ill_is_quiescent(ill)) {
1598 		/* call cannot fail since `conn_t *' argument is NULL */
1599 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1600 		    mp, ILL_DOWN);
1601 		mutex_exit(&ill->ill_lock);
1602 		return (B_FALSE);
1603 	}
1604 	mutex_exit(&ill->ill_lock);
1605 	return (B_TRUE);
1606 }
1607 
1608 static void
1609 ill_down(ill_t *ill)
1610 {
1611 	ip_stack_t	*ipst = ill->ill_ipst;
1612 
1613 	/* Blow off any IREs dependent on this ILL. */
1614 	ire_walk(ill_downi, (char *)ill, ipst);
1615 
1616 	/* Remove any conn_*_ill depending on this ill */
1617 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1618 
1619 	if (ill->ill_group != NULL) {
1620 		illgrp_delete(ill);
1621 	}
1622 }
1623 
1624 /*
1625  * ire_walk routine used to delete every IRE that depends on queues
1626  * associated with 'ill'.  (Always called as writer.)
1627  */
1628 static void
1629 ill_downi(ire_t *ire, char *ill_arg)
1630 {
1631 	ill_t	*ill = (ill_t *)ill_arg;
1632 
1633 	/*
1634 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1635 	 * interface X and ipif coming from interface Y, if interface
1636 	 * X and Y are part of the same IPMP group. Thus whenever interface
1637 	 * X goes down, remove all references to it by checking both
1638 	 * on ire_ipif and ire_stq.
1639 	 */
1640 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1641 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1642 		ire_delete(ire);
1643 	}
1644 }
1645 
1646 /*
1647  * Remove ire/nce from the fastpath list.
1648  */
1649 void
1650 ill_fastpath_nack(ill_t *ill)
1651 {
1652 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1653 }
1654 
1655 /* Consume an M_IOCACK of the fastpath probe. */
1656 void
1657 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1658 {
1659 	mblk_t	*mp1 = mp;
1660 
1661 	/*
1662 	 * If this was the first attempt turn on the fastpath probing.
1663 	 */
1664 	mutex_enter(&ill->ill_lock);
1665 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1666 		ill->ill_dlpi_fastpath_state = IDS_OK;
1667 	mutex_exit(&ill->ill_lock);
1668 
1669 	/* Free the M_IOCACK mblk, hold on to the data */
1670 	mp = mp->b_cont;
1671 	freeb(mp1);
1672 	if (mp == NULL)
1673 		return;
1674 	if (mp->b_cont != NULL) {
1675 		/*
1676 		 * Update all IRE's or NCE's that are waiting for
1677 		 * fastpath update.
1678 		 */
1679 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1680 		mp1 = mp->b_cont;
1681 		freeb(mp);
1682 		mp = mp1;
1683 	} else {
1684 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1685 	}
1686 
1687 	freeb(mp);
1688 }
1689 
1690 /*
1691  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1692  * The data portion of the request is a dl_unitdata_req_t template for
1693  * what we would send downstream in the absence of a fastpath confirmation.
1694  */
1695 int
1696 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1697 {
1698 	struct iocblk	*ioc;
1699 	mblk_t	*mp;
1700 
1701 	if (dlur_mp == NULL)
1702 		return (EINVAL);
1703 
1704 	mutex_enter(&ill->ill_lock);
1705 	switch (ill->ill_dlpi_fastpath_state) {
1706 	case IDS_FAILED:
1707 		/*
1708 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1709 		 * support it.
1710 		 */
1711 		mutex_exit(&ill->ill_lock);
1712 		return (ENOTSUP);
1713 	case IDS_UNKNOWN:
1714 		/* This is the first probe */
1715 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1716 		break;
1717 	default:
1718 		break;
1719 	}
1720 	mutex_exit(&ill->ill_lock);
1721 
1722 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1723 		return (EAGAIN);
1724 
1725 	mp->b_cont = copyb(dlur_mp);
1726 	if (mp->b_cont == NULL) {
1727 		freeb(mp);
1728 		return (EAGAIN);
1729 	}
1730 
1731 	ioc = (struct iocblk *)mp->b_rptr;
1732 	ioc->ioc_count = msgdsize(mp->b_cont);
1733 
1734 	putnext(ill->ill_wq, mp);
1735 	return (0);
1736 }
1737 
1738 void
1739 ill_capability_probe(ill_t *ill)
1740 {
1741 	/*
1742 	 * Do so only if capabilities are still unknown.
1743 	 */
1744 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1745 		return;
1746 
1747 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1748 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1749 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1750 }
1751 
1752 void
1753 ill_capability_reset(ill_t *ill)
1754 {
1755 	mblk_t *sc_mp = NULL;
1756 	mblk_t *tmp;
1757 
1758 	/*
1759 	 * Note here that we reset the state to UNKNOWN, and later send
1760 	 * down the DL_CAPABILITY_REQ without first setting the state to
1761 	 * INPROGRESS.  We do this in order to distinguish the
1762 	 * DL_CAPABILITY_ACK response which may come back in response to
1763 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1764 	 * also handle the case where the driver doesn't send us back
1765 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1766 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1767 	 * features are turned off until the state reaches IDS_OK.
1768 	 */
1769 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1770 	ill->ill_capab_reneg = B_FALSE;
1771 
1772 	/*
1773 	 * Disable sub-capabilities and request a list of sub-capability
1774 	 * messages which will be sent down to the driver.  Each handler
1775 	 * allocates the corresponding dl_capability_sub_t inside an
1776 	 * mblk, and links it to the existing sc_mp mblk, or return it
1777 	 * as sc_mp if it's the first sub-capability (the passed in
1778 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1779 	 * sc_mp will be pulled-up, before passing it downstream.
1780 	 */
1781 	ill_capability_mdt_reset(ill, &sc_mp);
1782 	ill_capability_hcksum_reset(ill, &sc_mp);
1783 	ill_capability_zerocopy_reset(ill, &sc_mp);
1784 	ill_capability_ipsec_reset(ill, &sc_mp);
1785 	ill_capability_dls_reset(ill, &sc_mp);
1786 	ill_capability_lso_reset(ill, &sc_mp);
1787 
1788 	/* Nothing to send down in order to disable the capabilities? */
1789 	if (sc_mp == NULL)
1790 		return;
1791 
1792 	tmp = msgpullup(sc_mp, -1);
1793 	freemsg(sc_mp);
1794 	if ((sc_mp = tmp) == NULL) {
1795 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1796 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1797 		return;
1798 	}
1799 
1800 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1801 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1802 }
1803 
1804 /*
1805  * Request or set new-style hardware capabilities supported by DLS provider.
1806  */
1807 static void
1808 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1809 {
1810 	mblk_t *mp;
1811 	dl_capability_req_t *capb;
1812 	size_t size = 0;
1813 	uint8_t *ptr;
1814 
1815 	if (reqp != NULL)
1816 		size = MBLKL(reqp);
1817 
1818 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1819 	if (mp == NULL) {
1820 		freemsg(reqp);
1821 		return;
1822 	}
1823 	ptr = mp->b_rptr;
1824 
1825 	capb = (dl_capability_req_t *)ptr;
1826 	ptr += sizeof (dl_capability_req_t);
1827 
1828 	if (reqp != NULL) {
1829 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1830 		capb->dl_sub_length = size;
1831 		bcopy(reqp->b_rptr, ptr, size);
1832 		ptr += size;
1833 		mp->b_cont = reqp->b_cont;
1834 		freeb(reqp);
1835 	}
1836 	ASSERT(ptr == mp->b_wptr);
1837 
1838 	ill_dlpi_send(ill, mp);
1839 }
1840 
1841 static void
1842 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1843 {
1844 	dl_capab_id_t *id_ic;
1845 	uint_t sub_dl_cap = outers->dl_cap;
1846 	dl_capability_sub_t *inners;
1847 	uint8_t *capend;
1848 
1849 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1850 
1851 	/*
1852 	 * Note: range checks here are not absolutely sufficient to
1853 	 * make us robust against malformed messages sent by drivers;
1854 	 * this is in keeping with the rest of IP's dlpi handling.
1855 	 * (Remember, it's coming from something else in the kernel
1856 	 * address space)
1857 	 */
1858 
1859 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1860 	if (capend > mp->b_wptr) {
1861 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1862 		    "malformed sub-capability too long for mblk");
1863 		return;
1864 	}
1865 
1866 	id_ic = (dl_capab_id_t *)(outers + 1);
1867 
1868 	if (outers->dl_length < sizeof (*id_ic) ||
1869 	    (inners = &id_ic->id_subcap,
1870 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1871 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1872 		    "encapsulated capab type %d too long for mblk",
1873 		    inners->dl_cap);
1874 		return;
1875 	}
1876 
1877 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1878 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1879 		    "isn't as expected; pass-thru module(s) detected, "
1880 		    "discarding capability\n", inners->dl_cap));
1881 		return;
1882 	}
1883 
1884 	/* Process the encapsulated sub-capability */
1885 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1886 }
1887 
1888 /*
1889  * Process Multidata Transmit capability negotiation ack received from a
1890  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1891  * DL_CAPABILITY_ACK message.
1892  */
1893 static void
1894 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1895 {
1896 	mblk_t *nmp = NULL;
1897 	dl_capability_req_t *oc;
1898 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1899 	ill_mdt_capab_t **ill_mdt_capab;
1900 	uint_t sub_dl_cap = isub->dl_cap;
1901 	uint8_t *capend;
1902 
1903 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1904 
1905 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1906 
1907 	/*
1908 	 * Note: range checks here are not absolutely sufficient to
1909 	 * make us robust against malformed messages sent by drivers;
1910 	 * this is in keeping with the rest of IP's dlpi handling.
1911 	 * (Remember, it's coming from something else in the kernel
1912 	 * address space)
1913 	 */
1914 
1915 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1916 	if (capend > mp->b_wptr) {
1917 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1918 		    "malformed sub-capability too long for mblk");
1919 		return;
1920 	}
1921 
1922 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1923 
1924 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1925 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1926 		    "unsupported MDT sub-capability (version %d, expected %d)",
1927 		    mdt_ic->mdt_version, MDT_VERSION_2);
1928 		return;
1929 	}
1930 
1931 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1932 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1933 		    "capability isn't as expected; pass-thru module(s) "
1934 		    "detected, discarding capability\n"));
1935 		return;
1936 	}
1937 
1938 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1939 
1940 		if (*ill_mdt_capab == NULL) {
1941 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1942 			    KM_NOSLEEP);
1943 
1944 			if (*ill_mdt_capab == NULL) {
1945 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1946 				    "could not enable MDT version %d "
1947 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1948 				    ill->ill_name);
1949 				return;
1950 			}
1951 		}
1952 
1953 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1954 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1955 		    "header spaces, %d max pld bufs, %d span limit)\n",
1956 		    ill->ill_name, MDT_VERSION_2,
1957 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1958 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1959 
1960 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1961 		(*ill_mdt_capab)->ill_mdt_on = 1;
1962 		/*
1963 		 * Round the following values to the nearest 32-bit; ULP
1964 		 * may further adjust them to accomodate for additional
1965 		 * protocol headers.  We pass these values to ULP during
1966 		 * bind time.
1967 		 */
1968 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1969 		    roundup(mdt_ic->mdt_hdr_head, 4);
1970 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1971 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1972 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1973 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1974 
1975 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1976 	} else {
1977 		uint_t size;
1978 		uchar_t *rptr;
1979 
1980 		size = sizeof (dl_capability_req_t) +
1981 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1982 
1983 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1984 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1985 			    "could not enable MDT for %s (ENOMEM)\n",
1986 			    ill->ill_name);
1987 			return;
1988 		}
1989 
1990 		rptr = nmp->b_rptr;
1991 		/* initialize dl_capability_req_t */
1992 		oc = (dl_capability_req_t *)nmp->b_rptr;
1993 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1994 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1995 		    sizeof (dl_capab_mdt_t);
1996 		nmp->b_rptr += sizeof (dl_capability_req_t);
1997 
1998 		/* initialize dl_capability_sub_t */
1999 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2000 		nmp->b_rptr += sizeof (*isub);
2001 
2002 		/* initialize dl_capab_mdt_t */
2003 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2004 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2005 
2006 		nmp->b_rptr = rptr;
2007 
2008 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2009 		    "to enable MDT version %d\n", ill->ill_name,
2010 		    MDT_VERSION_2));
2011 
2012 		/* set ENABLE flag */
2013 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2014 
2015 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2016 		ill_dlpi_send(ill, nmp);
2017 	}
2018 }
2019 
2020 static void
2021 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2022 {
2023 	mblk_t *mp;
2024 	dl_capab_mdt_t *mdt_subcap;
2025 	dl_capability_sub_t *dl_subcap;
2026 	int size;
2027 
2028 	if (!ILL_MDT_CAPABLE(ill))
2029 		return;
2030 
2031 	ASSERT(ill->ill_mdt_capab != NULL);
2032 	/*
2033 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2034 	 * structure since it's possible that another thread is still
2035 	 * referring to it.  The structure only gets deallocated when
2036 	 * we destroy the ill.
2037 	 */
2038 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2039 
2040 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2041 
2042 	mp = allocb(size, BPRI_HI);
2043 	if (mp == NULL) {
2044 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2045 		    "request to disable MDT\n"));
2046 		return;
2047 	}
2048 
2049 	mp->b_wptr = mp->b_rptr + size;
2050 
2051 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2052 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2053 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2054 
2055 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2056 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2057 	mdt_subcap->mdt_flags = 0;
2058 	mdt_subcap->mdt_hdr_head = 0;
2059 	mdt_subcap->mdt_hdr_tail = 0;
2060 
2061 	if (*sc_mp != NULL)
2062 		linkb(*sc_mp, mp);
2063 	else
2064 		*sc_mp = mp;
2065 }
2066 
2067 /*
2068  * Send a DL_NOTIFY_REQ to the specified ill to enable
2069  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2070  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2071  * acceleration.
2072  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2073  */
2074 static boolean_t
2075 ill_enable_promisc_notify(ill_t *ill)
2076 {
2077 	mblk_t *mp;
2078 	dl_notify_req_t *req;
2079 
2080 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2081 
2082 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2083 	if (mp == NULL)
2084 		return (B_FALSE);
2085 
2086 	req = (dl_notify_req_t *)mp->b_rptr;
2087 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2088 	    DL_NOTE_PROMISC_OFF_PHYS;
2089 
2090 	ill_dlpi_send(ill, mp);
2091 
2092 	return (B_TRUE);
2093 }
2094 
2095 /*
2096  * Allocate an IPsec capability request which will be filled by our
2097  * caller to turn on support for one or more algorithms.
2098  */
2099 static mblk_t *
2100 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2101 {
2102 	mblk_t *nmp;
2103 	dl_capability_req_t	*ocap;
2104 	dl_capab_ipsec_t	*ocip;
2105 	dl_capab_ipsec_t	*icip;
2106 	uint8_t			*ptr;
2107 	icip = (dl_capab_ipsec_t *)(isub + 1);
2108 
2109 	/*
2110 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2111 	 * PROMISC_ON/OFF notification from the provider. We need to
2112 	 * do this before enabling the algorithms to avoid leakage of
2113 	 * cleartext packets.
2114 	 */
2115 
2116 	if (!ill_enable_promisc_notify(ill))
2117 		return (NULL);
2118 
2119 	/*
2120 	 * Allocate new mblk which will contain a new capability
2121 	 * request to enable the capabilities.
2122 	 */
2123 
2124 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2125 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2126 	if (nmp == NULL)
2127 		return (NULL);
2128 
2129 	ptr = nmp->b_rptr;
2130 
2131 	/* initialize dl_capability_req_t */
2132 	ocap = (dl_capability_req_t *)ptr;
2133 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2134 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2135 	ptr += sizeof (dl_capability_req_t);
2136 
2137 	/* initialize dl_capability_sub_t */
2138 	bcopy(isub, ptr, sizeof (*isub));
2139 	ptr += sizeof (*isub);
2140 
2141 	/* initialize dl_capab_ipsec_t */
2142 	ocip = (dl_capab_ipsec_t *)ptr;
2143 	bcopy(icip, ocip, sizeof (*icip));
2144 
2145 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2146 	return (nmp);
2147 }
2148 
2149 /*
2150  * Process an IPsec capability negotiation ack received from a DLS Provider.
2151  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2152  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2153  */
2154 static void
2155 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2156 {
2157 	dl_capab_ipsec_t	*icip;
2158 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2159 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2160 	uint_t cipher, nciphers;
2161 	mblk_t *nmp;
2162 	uint_t alg_len;
2163 	boolean_t need_sadb_dump;
2164 	uint_t sub_dl_cap = isub->dl_cap;
2165 	ill_ipsec_capab_t **ill_capab;
2166 	uint64_t ill_capab_flag;
2167 	uint8_t *capend, *ciphend;
2168 	boolean_t sadb_resync;
2169 
2170 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2171 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2172 
2173 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2174 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2175 		ill_capab_flag = ILL_CAPAB_AH;
2176 	} else {
2177 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2178 		ill_capab_flag = ILL_CAPAB_ESP;
2179 	}
2180 
2181 	/*
2182 	 * If the ill capability structure exists, then this incoming
2183 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2184 	 * If this is so, then we'd need to resynchronize the SADB
2185 	 * after re-enabling the offloaded ciphers.
2186 	 */
2187 	sadb_resync = (*ill_capab != NULL);
2188 
2189 	/*
2190 	 * Note: range checks here are not absolutely sufficient to
2191 	 * make us robust against malformed messages sent by drivers;
2192 	 * this is in keeping with the rest of IP's dlpi handling.
2193 	 * (Remember, it's coming from something else in the kernel
2194 	 * address space)
2195 	 */
2196 
2197 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2198 	if (capend > mp->b_wptr) {
2199 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2200 		    "malformed sub-capability too long for mblk");
2201 		return;
2202 	}
2203 
2204 	/*
2205 	 * There are two types of acks we process here:
2206 	 * 1. acks in reply to a (first form) generic capability req
2207 	 *    (no ENABLE flag set)
2208 	 * 2. acks in reply to a ENABLE capability req.
2209 	 *    (ENABLE flag set)
2210 	 *
2211 	 * We process the subcapability passed as argument as follows:
2212 	 * 1 do initializations
2213 	 *   1.1 initialize nmp = NULL
2214 	 *   1.2 set need_sadb_dump to B_FALSE
2215 	 * 2 for each cipher in subcapability:
2216 	 *   2.1 if ENABLE flag is set:
2217 	 *	2.1.1 update per-ill ipsec capabilities info
2218 	 *	2.1.2 set need_sadb_dump to B_TRUE
2219 	 *   2.2 if ENABLE flag is not set:
2220 	 *	2.2.1 if nmp is NULL:
2221 	 *		2.2.1.1 allocate and initialize nmp
2222 	 *		2.2.1.2 init current pos in nmp
2223 	 *	2.2.2 copy current cipher to current pos in nmp
2224 	 *	2.2.3 set ENABLE flag in nmp
2225 	 *	2.2.4 update current pos
2226 	 * 3 if nmp is not equal to NULL, send enable request
2227 	 *   3.1 send capability request
2228 	 * 4 if need_sadb_dump is B_TRUE
2229 	 *   4.1 enable promiscuous on/off notifications
2230 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2231 	 *	AH or ESP SA's to interface.
2232 	 */
2233 
2234 	nmp = NULL;
2235 	oalg = NULL;
2236 	need_sadb_dump = B_FALSE;
2237 	icip = (dl_capab_ipsec_t *)(isub + 1);
2238 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2239 
2240 	nciphers = icip->cip_nciphers;
2241 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2242 
2243 	if (ciphend > capend) {
2244 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2245 		    "too many ciphers for sub-capability len");
2246 		return;
2247 	}
2248 
2249 	for (cipher = 0; cipher < nciphers; cipher++) {
2250 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2251 
2252 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2253 			/*
2254 			 * TBD: when we provide a way to disable capabilities
2255 			 * from above, need to manage the request-pending state
2256 			 * and fail if we were not expecting this ACK.
2257 			 */
2258 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2259 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2260 
2261 			/*
2262 			 * Update IPsec capabilities for this ill
2263 			 */
2264 
2265 			if (*ill_capab == NULL) {
2266 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2267 				    ("ill_capability_ipsec_ack: "
2268 				    "allocating ipsec_capab for ill\n"));
2269 				*ill_capab = ill_ipsec_capab_alloc();
2270 
2271 				if (*ill_capab == NULL) {
2272 					cmn_err(CE_WARN,
2273 					    "ill_capability_ipsec_ack: "
2274 					    "could not enable IPsec Hardware "
2275 					    "acceleration for %s (ENOMEM)\n",
2276 					    ill->ill_name);
2277 					return;
2278 				}
2279 			}
2280 
2281 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2282 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2283 
2284 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2285 				cmn_err(CE_WARN,
2286 				    "ill_capability_ipsec_ack: "
2287 				    "malformed IPsec algorithm id %d",
2288 				    ialg->alg_prim);
2289 				continue;
2290 			}
2291 
2292 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2293 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2294 				    ialg->alg_prim);
2295 			} else {
2296 				ipsec_capab_algparm_t *alp;
2297 
2298 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2299 				    ialg->alg_prim);
2300 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2301 				    ialg->alg_prim)) {
2302 					cmn_err(CE_WARN,
2303 					    "ill_capability_ipsec_ack: "
2304 					    "no space for IPsec alg id %d",
2305 					    ialg->alg_prim);
2306 					continue;
2307 				}
2308 				alp = &((*ill_capab)->encr_algparm[
2309 				    ialg->alg_prim]);
2310 				alp->minkeylen = ialg->alg_minbits;
2311 				alp->maxkeylen = ialg->alg_maxbits;
2312 			}
2313 			ill->ill_capabilities |= ill_capab_flag;
2314 			/*
2315 			 * indicate that a capability was enabled, which
2316 			 * will be used below to kick off a SADB dump
2317 			 * to the ill.
2318 			 */
2319 			need_sadb_dump = B_TRUE;
2320 		} else {
2321 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2322 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2323 			    ialg->alg_prim));
2324 
2325 			if (nmp == NULL) {
2326 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2327 				if (nmp == NULL) {
2328 					/*
2329 					 * Sending the PROMISC_ON/OFF
2330 					 * notification request failed.
2331 					 * We cannot enable the algorithms
2332 					 * since the Provider will not
2333 					 * notify IP of promiscous mode
2334 					 * changes, which could lead
2335 					 * to leakage of packets.
2336 					 */
2337 					cmn_err(CE_WARN,
2338 					    "ill_capability_ipsec_ack: "
2339 					    "could not enable IPsec Hardware "
2340 					    "acceleration for %s (ENOMEM)\n",
2341 					    ill->ill_name);
2342 					return;
2343 				}
2344 				/* ptr to current output alg specifier */
2345 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2346 			}
2347 
2348 			/*
2349 			 * Copy current alg specifier, set ENABLE
2350 			 * flag, and advance to next output alg.
2351 			 * For now we enable all IPsec capabilities.
2352 			 */
2353 			ASSERT(oalg != NULL);
2354 			bcopy(ialg, oalg, alg_len);
2355 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2356 			nmp->b_wptr += alg_len;
2357 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2358 		}
2359 
2360 		/* move to next input algorithm specifier */
2361 		ialg = (dl_capab_ipsec_alg_t *)
2362 		    ((char *)ialg + alg_len);
2363 	}
2364 
2365 	if (nmp != NULL)
2366 		/*
2367 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2368 		 * IPsec hardware acceleration.
2369 		 */
2370 		ill_dlpi_send(ill, nmp);
2371 
2372 	if (need_sadb_dump)
2373 		/*
2374 		 * An acknowledgement corresponding to a request to
2375 		 * enable acceleration was received, notify SADB.
2376 		 */
2377 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2378 }
2379 
2380 /*
2381  * Given an mblk with enough space in it, create sub-capability entries for
2382  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2383  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2384  * in preparation for the reset the DL_CAPABILITY_REQ message.
2385  */
2386 static void
2387 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2388     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2389 {
2390 	dl_capab_ipsec_t *oipsec;
2391 	dl_capab_ipsec_alg_t *oalg;
2392 	dl_capability_sub_t *dl_subcap;
2393 	int i, k;
2394 
2395 	ASSERT(nciphers > 0);
2396 	ASSERT(ill_cap != NULL);
2397 	ASSERT(mp != NULL);
2398 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2399 
2400 	/* dl_capability_sub_t for "stype" */
2401 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2402 	dl_subcap->dl_cap = stype;
2403 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2404 	mp->b_wptr += sizeof (dl_capability_sub_t);
2405 
2406 	/* dl_capab_ipsec_t for "stype" */
2407 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2408 	oipsec->cip_version = 1;
2409 	oipsec->cip_nciphers = nciphers;
2410 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2411 
2412 	/* create entries for "stype" AUTH ciphers */
2413 	for (i = 0; i < ill_cap->algs_size; i++) {
2414 		for (k = 0; k < BITSPERBYTE; k++) {
2415 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2416 				continue;
2417 
2418 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2419 			bzero((void *)oalg, sizeof (*oalg));
2420 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2421 			oalg->alg_prim = k + (BITSPERBYTE * i);
2422 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2423 		}
2424 	}
2425 	/* create entries for "stype" ENCR ciphers */
2426 	for (i = 0; i < ill_cap->algs_size; i++) {
2427 		for (k = 0; k < BITSPERBYTE; k++) {
2428 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2429 				continue;
2430 
2431 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2432 			bzero((void *)oalg, sizeof (*oalg));
2433 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2434 			oalg->alg_prim = k + (BITSPERBYTE * i);
2435 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2436 		}
2437 	}
2438 }
2439 
2440 /*
2441  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2442  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2443  * POPC instruction, but our macro is more flexible for an arbitrary length
2444  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2445  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2446  * stays that way, we can reduce the number of iterations required.
2447  */
2448 #define	COUNT_1S(val, sum) {					\
2449 	uint8_t x = val & 0xff;					\
2450 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2451 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2452 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2453 }
2454 
2455 /* ARGSUSED */
2456 static void
2457 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2458 {
2459 	mblk_t *mp;
2460 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2461 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2462 	uint64_t ill_capabilities = ill->ill_capabilities;
2463 	int ah_cnt = 0, esp_cnt = 0;
2464 	int ah_len = 0, esp_len = 0;
2465 	int i, size = 0;
2466 
2467 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2468 		return;
2469 
2470 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2471 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2472 
2473 	/* Find out the number of ciphers for AH */
2474 	if (cap_ah != NULL) {
2475 		for (i = 0; i < cap_ah->algs_size; i++) {
2476 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2477 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2478 		}
2479 		if (ah_cnt > 0) {
2480 			size += sizeof (dl_capability_sub_t) +
2481 			    sizeof (dl_capab_ipsec_t);
2482 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2483 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2484 			size += ah_len;
2485 		}
2486 	}
2487 
2488 	/* Find out the number of ciphers for ESP */
2489 	if (cap_esp != NULL) {
2490 		for (i = 0; i < cap_esp->algs_size; i++) {
2491 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2492 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2493 		}
2494 		if (esp_cnt > 0) {
2495 			size += sizeof (dl_capability_sub_t) +
2496 			    sizeof (dl_capab_ipsec_t);
2497 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2498 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2499 			size += esp_len;
2500 		}
2501 	}
2502 
2503 	if (size == 0) {
2504 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2505 		    "there's nothing to reset\n"));
2506 		return;
2507 	}
2508 
2509 	mp = allocb(size, BPRI_HI);
2510 	if (mp == NULL) {
2511 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2512 		    "request to disable IPSEC Hardware Acceleration\n"));
2513 		return;
2514 	}
2515 
2516 	/*
2517 	 * Clear the capability flags for IPsec HA but retain the ill
2518 	 * capability structures since it's possible that another thread
2519 	 * is still referring to them.  The structures only get deallocated
2520 	 * when we destroy the ill.
2521 	 *
2522 	 * Various places check the flags to see if the ill is capable of
2523 	 * hardware acceleration, and by clearing them we ensure that new
2524 	 * outbound IPsec packets are sent down encrypted.
2525 	 */
2526 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2527 
2528 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2529 	if (ah_cnt > 0) {
2530 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2531 		    cap_ah, mp);
2532 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2533 	}
2534 
2535 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2536 	if (esp_cnt > 0) {
2537 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2538 		    cap_esp, mp);
2539 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2540 	}
2541 
2542 	/*
2543 	 * At this point we've composed a bunch of sub-capabilities to be
2544 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2545 	 * by the caller.  Upon receiving this reset message, the driver
2546 	 * must stop inbound decryption (by destroying all inbound SAs)
2547 	 * and let the corresponding packets come in encrypted.
2548 	 */
2549 
2550 	if (*sc_mp != NULL)
2551 		linkb(*sc_mp, mp);
2552 	else
2553 		*sc_mp = mp;
2554 }
2555 
2556 static void
2557 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2558     boolean_t encapsulated)
2559 {
2560 	boolean_t legacy = B_FALSE;
2561 
2562 	/*
2563 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2564 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2565 	 * instructed the driver to disable its advertised capabilities,
2566 	 * so there's no point in accepting any response at this moment.
2567 	 */
2568 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2569 		return;
2570 
2571 	/*
2572 	 * Note that only the following two sub-capabilities may be
2573 	 * considered as "legacy", since their original definitions
2574 	 * do not incorporate the dl_mid_t module ID token, and hence
2575 	 * may require the use of the wrapper sub-capability.
2576 	 */
2577 	switch (subp->dl_cap) {
2578 	case DL_CAPAB_IPSEC_AH:
2579 	case DL_CAPAB_IPSEC_ESP:
2580 		legacy = B_TRUE;
2581 		break;
2582 	}
2583 
2584 	/*
2585 	 * For legacy sub-capabilities which don't incorporate a queue_t
2586 	 * pointer in their structures, discard them if we detect that
2587 	 * there are intermediate modules in between IP and the driver.
2588 	 */
2589 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2590 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2591 		    "%d discarded; %d module(s) present below IP\n",
2592 		    subp->dl_cap, ill->ill_lmod_cnt));
2593 		return;
2594 	}
2595 
2596 	switch (subp->dl_cap) {
2597 	case DL_CAPAB_IPSEC_AH:
2598 	case DL_CAPAB_IPSEC_ESP:
2599 		ill_capability_ipsec_ack(ill, mp, subp);
2600 		break;
2601 	case DL_CAPAB_MDT:
2602 		ill_capability_mdt_ack(ill, mp, subp);
2603 		break;
2604 	case DL_CAPAB_HCKSUM:
2605 		ill_capability_hcksum_ack(ill, mp, subp);
2606 		break;
2607 	case DL_CAPAB_ZEROCOPY:
2608 		ill_capability_zerocopy_ack(ill, mp, subp);
2609 		break;
2610 	case DL_CAPAB_POLL:
2611 		if (!SOFT_RINGS_ENABLED())
2612 			ill_capability_dls_ack(ill, mp, subp);
2613 		break;
2614 	case DL_CAPAB_SOFT_RING:
2615 		if (SOFT_RINGS_ENABLED())
2616 			ill_capability_dls_ack(ill, mp, subp);
2617 		break;
2618 	case DL_CAPAB_LSO:
2619 		ill_capability_lso_ack(ill, mp, subp);
2620 		break;
2621 	default:
2622 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2623 		    subp->dl_cap));
2624 	}
2625 }
2626 
2627 /*
2628  * As part of negotiating polling capability, the driver tells us
2629  * the default (or normal) blanking interval and packet threshold
2630  * (the receive timer fires if blanking interval is reached or
2631  * the packet threshold is reached).
2632  *
2633  * As part of manipulating the polling interval, we always use our
2634  * estimated interval (avg service time * number of packets queued
2635  * on the squeue) but we try to blank for a minimum of
2636  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2637  * packet threshold during this time. When we are not in polling mode
2638  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2639  * rr_min_blank_ratio but up the packet cnt by a ratio of
2640  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2641  * possible although for a shorter interval.
2642  */
2643 #define	RR_MAX_BLANK_RATIO	20
2644 #define	RR_MIN_BLANK_RATIO	10
2645 #define	RR_MAX_PKT_CNT_RATIO	3
2646 #define	RR_MIN_PKT_CNT_RATIO	3
2647 
2648 /*
2649  * These can be tuned via /etc/system.
2650  */
2651 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2652 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2653 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2654 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2655 
2656 static mac_resource_handle_t
2657 ill_ring_add(void *arg, mac_resource_t *mrp)
2658 {
2659 	ill_t			*ill = (ill_t *)arg;
2660 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2661 	ill_rx_ring_t		*rx_ring;
2662 	int			ip_rx_index;
2663 
2664 	ASSERT(mrp != NULL);
2665 	if (mrp->mr_type != MAC_RX_FIFO) {
2666 		return (NULL);
2667 	}
2668 	ASSERT(ill != NULL);
2669 	ASSERT(ill->ill_dls_capab != NULL);
2670 
2671 	mutex_enter(&ill->ill_lock);
2672 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2673 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2674 		ASSERT(rx_ring != NULL);
2675 
2676 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2677 			time_t normal_blank_time =
2678 			    mrfp->mrf_normal_blank_time;
2679 			uint_t normal_pkt_cnt =
2680 			    mrfp->mrf_normal_pkt_count;
2681 
2682 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2683 
2684 	rx_ring->rr_blank = mrfp->mrf_blank;
2685 	rx_ring->rr_handle = mrfp->mrf_arg;
2686 	rx_ring->rr_ill = ill;
2687 	rx_ring->rr_normal_blank_time = normal_blank_time;
2688 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2689 
2690 			rx_ring->rr_max_blank_time =
2691 			    normal_blank_time * rr_max_blank_ratio;
2692 			rx_ring->rr_min_blank_time =
2693 			    normal_blank_time * rr_min_blank_ratio;
2694 			rx_ring->rr_max_pkt_cnt =
2695 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2696 			rx_ring->rr_min_pkt_cnt =
2697 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2698 
2699 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2700 			mutex_exit(&ill->ill_lock);
2701 
2702 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2703 			    (int), ip_rx_index);
2704 			return ((mac_resource_handle_t)rx_ring);
2705 		}
2706 	}
2707 
2708 	/*
2709 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2710 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2711 	 * should be made configurable. Meanwhile it cause no panic because
2712 	 * driver will pass ip_input a NULL handle which will make
2713 	 * IP allocate the default squeue and Polling mode will not
2714 	 * be used for this ring.
2715 	 */
2716 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2717 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2718 
2719 	mutex_exit(&ill->ill_lock);
2720 	return (NULL);
2721 }
2722 
2723 static boolean_t
2724 ill_capability_dls_init(ill_t *ill)
2725 {
2726 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2727 	conn_t 			*connp;
2728 	size_t			sz;
2729 	ip_stack_t *ipst = ill->ill_ipst;
2730 
2731 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2732 		if (ill_dls == NULL) {
2733 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2734 			    "soft_ring enabled for ill=%s (%p) but data "
2735 			    "structs uninitialized\n", ill->ill_name,
2736 			    (void *)ill);
2737 		}
2738 		return (B_TRUE);
2739 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2740 		if (ill_dls == NULL) {
2741 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2742 			    "polling enabled for ill=%s (%p) but data "
2743 			    "structs uninitialized\n", ill->ill_name,
2744 			    (void *)ill);
2745 		}
2746 		return (B_TRUE);
2747 	}
2748 
2749 	if (ill_dls != NULL) {
2750 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2751 		/* Soft_Ring or polling is being re-enabled */
2752 
2753 		connp = ill_dls->ill_unbind_conn;
2754 		ASSERT(rx_ring != NULL);
2755 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2756 		bzero((void *)rx_ring,
2757 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2758 		ill_dls->ill_ring_tbl = rx_ring;
2759 		ill_dls->ill_unbind_conn = connp;
2760 		return (B_TRUE);
2761 	}
2762 
2763 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2764 	    ipst->ips_netstack)) == NULL)
2765 		return (B_FALSE);
2766 
2767 	sz = sizeof (ill_dls_capab_t);
2768 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2769 
2770 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2771 	if (ill_dls == NULL) {
2772 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2773 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2774 		    (void *)ill);
2775 		CONN_DEC_REF(connp);
2776 		return (B_FALSE);
2777 	}
2778 
2779 	/* Allocate space to hold ring table */
2780 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2781 	ill->ill_dls_capab = ill_dls;
2782 	ill_dls->ill_unbind_conn = connp;
2783 	return (B_TRUE);
2784 }
2785 
2786 /*
2787  * ill_capability_dls_disable: disable soft_ring and/or polling
2788  * capability. Since any of the rings might already be in use, need
2789  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2790  * direct calls if necessary.
2791  */
2792 static void
2793 ill_capability_dls_disable(ill_t *ill)
2794 {
2795 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2796 
2797 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2798 		ip_squeue_clean_all(ill);
2799 		ill_dls->ill_tx = NULL;
2800 		ill_dls->ill_tx_handle = NULL;
2801 		ill_dls->ill_dls_change_status = NULL;
2802 		ill_dls->ill_dls_bind = NULL;
2803 		ill_dls->ill_dls_unbind = NULL;
2804 	}
2805 
2806 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2807 }
2808 
2809 static void
2810 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2811     dl_capability_sub_t *isub)
2812 {
2813 	uint_t			size;
2814 	uchar_t			*rptr;
2815 	dl_capab_dls_t	dls, *odls;
2816 	ill_dls_capab_t	*ill_dls;
2817 	mblk_t			*nmp = NULL;
2818 	dl_capability_req_t	*ocap;
2819 	uint_t			sub_dl_cap = isub->dl_cap;
2820 
2821 	if (!ill_capability_dls_init(ill))
2822 		return;
2823 	ill_dls = ill->ill_dls_capab;
2824 
2825 	/* Copy locally to get the members aligned */
2826 	bcopy((void *)idls, (void *)&dls,
2827 	    sizeof (dl_capab_dls_t));
2828 
2829 	/* Get the tx function and handle from dld */
2830 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2831 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2832 
2833 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2834 		ill_dls->ill_dls_change_status =
2835 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2836 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2837 		ill_dls->ill_dls_unbind =
2838 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2839 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2840 	}
2841 
2842 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2843 	    isub->dl_length;
2844 
2845 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2846 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2847 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2848 		    ill->ill_name, (void *)ill);
2849 		return;
2850 	}
2851 
2852 	/* initialize dl_capability_req_t */
2853 	rptr = nmp->b_rptr;
2854 	ocap = (dl_capability_req_t *)rptr;
2855 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2856 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2857 	rptr += sizeof (dl_capability_req_t);
2858 
2859 	/* initialize dl_capability_sub_t */
2860 	bcopy(isub, rptr, sizeof (*isub));
2861 	rptr += sizeof (*isub);
2862 
2863 	odls = (dl_capab_dls_t *)rptr;
2864 	rptr += sizeof (dl_capab_dls_t);
2865 
2866 	/* initialize dl_capab_dls_t to be sent down */
2867 	dls.dls_rx_handle = (uintptr_t)ill;
2868 	dls.dls_rx = (uintptr_t)ip_input;
2869 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2870 
2871 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2872 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2873 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2874 		dls.dls_flags = SOFT_RING_ENABLE;
2875 	} else {
2876 		dls.dls_flags = POLL_ENABLE;
2877 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2878 		    "to enable polling\n", ill->ill_name));
2879 	}
2880 	bcopy((void *)&dls, (void *)odls,
2881 	    sizeof (dl_capab_dls_t));
2882 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2883 	/*
2884 	 * nmp points to a DL_CAPABILITY_REQ message to
2885 	 * enable either soft_ring or polling
2886 	 */
2887 	ill_dlpi_send(ill, nmp);
2888 }
2889 
2890 static void
2891 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2892 {
2893 	mblk_t *mp;
2894 	dl_capab_dls_t *idls;
2895 	dl_capability_sub_t *dl_subcap;
2896 	int size;
2897 
2898 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2899 		return;
2900 
2901 	ASSERT(ill->ill_dls_capab != NULL);
2902 
2903 	size = sizeof (*dl_subcap) + sizeof (*idls);
2904 
2905 	mp = allocb(size, BPRI_HI);
2906 	if (mp == NULL) {
2907 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2908 		    "request to disable soft_ring\n"));
2909 		return;
2910 	}
2911 
2912 	mp->b_wptr = mp->b_rptr + size;
2913 
2914 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2915 	dl_subcap->dl_length = sizeof (*idls);
2916 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2917 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2918 	else
2919 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2920 
2921 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2922 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2923 		idls->dls_flags = SOFT_RING_DISABLE;
2924 	else
2925 		idls->dls_flags = POLL_DISABLE;
2926 
2927 	if (*sc_mp != NULL)
2928 		linkb(*sc_mp, mp);
2929 	else
2930 		*sc_mp = mp;
2931 }
2932 
2933 /*
2934  * Process a soft_ring/poll capability negotiation ack received
2935  * from a DLS Provider.isub must point to the sub-capability
2936  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2937  */
2938 static void
2939 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2940 {
2941 	dl_capab_dls_t		*idls;
2942 	uint_t			sub_dl_cap = isub->dl_cap;
2943 	uint8_t			*capend;
2944 
2945 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2946 	    sub_dl_cap == DL_CAPAB_POLL);
2947 
2948 	if (ill->ill_isv6)
2949 		return;
2950 
2951 	/*
2952 	 * Note: range checks here are not absolutely sufficient to
2953 	 * make us robust against malformed messages sent by drivers;
2954 	 * this is in keeping with the rest of IP's dlpi handling.
2955 	 * (Remember, it's coming from something else in the kernel
2956 	 * address space)
2957 	 */
2958 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2959 	if (capend > mp->b_wptr) {
2960 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2961 		    "malformed sub-capability too long for mblk");
2962 		return;
2963 	}
2964 
2965 	/*
2966 	 * There are two types of acks we process here:
2967 	 * 1. acks in reply to a (first form) generic capability req
2968 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2969 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2970 	 *    capability req.
2971 	 */
2972 	idls = (dl_capab_dls_t *)(isub + 1);
2973 
2974 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2975 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2976 		    "capability isn't as expected; pass-thru "
2977 		    "module(s) detected, discarding capability\n"));
2978 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2979 			/*
2980 			 * This is a capability renegotitation case.
2981 			 * The interface better be unusable at this
2982 			 * point other wise bad things will happen
2983 			 * if we disable direct calls on a running
2984 			 * and up interface.
2985 			 */
2986 			ill_capability_dls_disable(ill);
2987 		}
2988 		return;
2989 	}
2990 
2991 	switch (idls->dls_flags) {
2992 	default:
2993 		/* Disable if unknown flag */
2994 	case SOFT_RING_DISABLE:
2995 	case POLL_DISABLE:
2996 		ill_capability_dls_disable(ill);
2997 		break;
2998 	case SOFT_RING_CAPABLE:
2999 	case POLL_CAPABLE:
3000 		/*
3001 		 * If the capability was already enabled, its safe
3002 		 * to disable it first to get rid of stale information
3003 		 * and then start enabling it again.
3004 		 */
3005 		ill_capability_dls_disable(ill);
3006 		ill_capability_dls_capable(ill, idls, isub);
3007 		break;
3008 	case SOFT_RING_ENABLE:
3009 	case POLL_ENABLE:
3010 		mutex_enter(&ill->ill_lock);
3011 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3012 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3013 			ASSERT(ill->ill_dls_capab != NULL);
3014 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3015 		}
3016 		if (sub_dl_cap == DL_CAPAB_POLL &&
3017 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3018 			ASSERT(ill->ill_dls_capab != NULL);
3019 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3020 			ip1dbg(("ill_capability_dls_ack: interface %s "
3021 			    "has enabled polling\n", ill->ill_name));
3022 		}
3023 		mutex_exit(&ill->ill_lock);
3024 		break;
3025 	}
3026 }
3027 
3028 /*
3029  * Process a hardware checksum offload capability negotiation ack received
3030  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3031  * of a DL_CAPABILITY_ACK message.
3032  */
3033 static void
3034 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3035 {
3036 	dl_capability_req_t	*ocap;
3037 	dl_capab_hcksum_t	*ihck, *ohck;
3038 	ill_hcksum_capab_t	**ill_hcksum;
3039 	mblk_t			*nmp = NULL;
3040 	uint_t			sub_dl_cap = isub->dl_cap;
3041 	uint8_t			*capend;
3042 
3043 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3044 
3045 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3046 
3047 	/*
3048 	 * Note: range checks here are not absolutely sufficient to
3049 	 * make us robust against malformed messages sent by drivers;
3050 	 * this is in keeping with the rest of IP's dlpi handling.
3051 	 * (Remember, it's coming from something else in the kernel
3052 	 * address space)
3053 	 */
3054 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3055 	if (capend > mp->b_wptr) {
3056 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3057 		    "malformed sub-capability too long for mblk");
3058 		return;
3059 	}
3060 
3061 	/*
3062 	 * There are two types of acks we process here:
3063 	 * 1. acks in reply to a (first form) generic capability req
3064 	 *    (no ENABLE flag set)
3065 	 * 2. acks in reply to a ENABLE capability req.
3066 	 *    (ENABLE flag set)
3067 	 */
3068 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3069 
3070 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3071 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3072 		    "unsupported hardware checksum "
3073 		    "sub-capability (version %d, expected %d)",
3074 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3075 		return;
3076 	}
3077 
3078 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3079 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3080 		    "checksum capability isn't as expected; pass-thru "
3081 		    "module(s) detected, discarding capability\n"));
3082 		return;
3083 	}
3084 
3085 #define	CURR_HCKSUM_CAPAB				\
3086 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3087 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3088 
3089 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3090 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3091 		/* do ENABLE processing */
3092 		if (*ill_hcksum == NULL) {
3093 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3094 			    KM_NOSLEEP);
3095 
3096 			if (*ill_hcksum == NULL) {
3097 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3098 				    "could not enable hcksum version %d "
3099 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3100 				    ill->ill_name);
3101 				return;
3102 			}
3103 		}
3104 
3105 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3106 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3107 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3108 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3109 		    "has enabled hardware checksumming\n ",
3110 		    ill->ill_name));
3111 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3112 		/*
3113 		 * Enabling hardware checksum offload
3114 		 * Currently IP supports {TCP,UDP}/IPv4
3115 		 * partial and full cksum offload and
3116 		 * IPv4 header checksum offload.
3117 		 * Allocate new mblk which will
3118 		 * contain a new capability request
3119 		 * to enable hardware checksum offload.
3120 		 */
3121 		uint_t	size;
3122 		uchar_t	*rptr;
3123 
3124 		size = sizeof (dl_capability_req_t) +
3125 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3126 
3127 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3128 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3129 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3130 			    ill->ill_name);
3131 			return;
3132 		}
3133 
3134 		rptr = nmp->b_rptr;
3135 		/* initialize dl_capability_req_t */
3136 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3137 		ocap->dl_sub_offset =
3138 		    sizeof (dl_capability_req_t);
3139 		ocap->dl_sub_length =
3140 		    sizeof (dl_capability_sub_t) +
3141 		    isub->dl_length;
3142 		nmp->b_rptr += sizeof (dl_capability_req_t);
3143 
3144 		/* initialize dl_capability_sub_t */
3145 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3146 		nmp->b_rptr += sizeof (*isub);
3147 
3148 		/* initialize dl_capab_hcksum_t */
3149 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3150 		bcopy(ihck, ohck, sizeof (*ihck));
3151 
3152 		nmp->b_rptr = rptr;
3153 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3154 
3155 		/* Set ENABLE flag */
3156 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3157 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3158 
3159 		/*
3160 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3161 		 * hardware checksum acceleration.
3162 		 */
3163 		ill_dlpi_send(ill, nmp);
3164 	} else {
3165 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3166 		    "advertised %x hardware checksum capability flags\n",
3167 		    ill->ill_name, ihck->hcksum_txflags));
3168 	}
3169 }
3170 
3171 static void
3172 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3173 {
3174 	mblk_t *mp;
3175 	dl_capab_hcksum_t *hck_subcap;
3176 	dl_capability_sub_t *dl_subcap;
3177 	int size;
3178 
3179 	if (!ILL_HCKSUM_CAPABLE(ill))
3180 		return;
3181 
3182 	ASSERT(ill->ill_hcksum_capab != NULL);
3183 	/*
3184 	 * Clear the capability flag for hardware checksum offload but
3185 	 * retain the ill_hcksum_capab structure since it's possible that
3186 	 * another thread is still referring to it.  The structure only
3187 	 * gets deallocated when we destroy the ill.
3188 	 */
3189 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3190 
3191 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3192 
3193 	mp = allocb(size, BPRI_HI);
3194 	if (mp == NULL) {
3195 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3196 		    "request to disable hardware checksum offload\n"));
3197 		return;
3198 	}
3199 
3200 	mp->b_wptr = mp->b_rptr + size;
3201 
3202 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3203 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3204 	dl_subcap->dl_length = sizeof (*hck_subcap);
3205 
3206 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3207 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3208 	hck_subcap->hcksum_txflags = 0;
3209 
3210 	if (*sc_mp != NULL)
3211 		linkb(*sc_mp, mp);
3212 	else
3213 		*sc_mp = mp;
3214 }
3215 
3216 static void
3217 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3218 {
3219 	mblk_t *nmp = NULL;
3220 	dl_capability_req_t *oc;
3221 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3222 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3223 	uint_t sub_dl_cap = isub->dl_cap;
3224 	uint8_t *capend;
3225 
3226 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3227 
3228 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3229 
3230 	/*
3231 	 * Note: range checks here are not absolutely sufficient to
3232 	 * make us robust against malformed messages sent by drivers;
3233 	 * this is in keeping with the rest of IP's dlpi handling.
3234 	 * (Remember, it's coming from something else in the kernel
3235 	 * address space)
3236 	 */
3237 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3238 	if (capend > mp->b_wptr) {
3239 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3240 		    "malformed sub-capability too long for mblk");
3241 		return;
3242 	}
3243 
3244 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3245 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3246 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3247 		    "unsupported ZEROCOPY sub-capability (version %d, "
3248 		    "expected %d)", zc_ic->zerocopy_version,
3249 		    ZEROCOPY_VERSION_1);
3250 		return;
3251 	}
3252 
3253 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3254 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3255 		    "capability isn't as expected; pass-thru module(s) "
3256 		    "detected, discarding capability\n"));
3257 		return;
3258 	}
3259 
3260 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3261 		if (*ill_zerocopy_capab == NULL) {
3262 			*ill_zerocopy_capab =
3263 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3264 			    KM_NOSLEEP);
3265 
3266 			if (*ill_zerocopy_capab == NULL) {
3267 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3268 				    "could not enable Zero-copy version %d "
3269 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3270 				    ill->ill_name);
3271 				return;
3272 			}
3273 		}
3274 
3275 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3276 		    "supports Zero-copy version %d\n", ill->ill_name,
3277 		    ZEROCOPY_VERSION_1));
3278 
3279 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3280 		    zc_ic->zerocopy_version;
3281 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3282 		    zc_ic->zerocopy_flags;
3283 
3284 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3285 	} else {
3286 		uint_t size;
3287 		uchar_t *rptr;
3288 
3289 		size = sizeof (dl_capability_req_t) +
3290 		    sizeof (dl_capability_sub_t) +
3291 		    sizeof (dl_capab_zerocopy_t);
3292 
3293 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3294 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3295 			    "could not enable zerocopy for %s (ENOMEM)\n",
3296 			    ill->ill_name);
3297 			return;
3298 		}
3299 
3300 		rptr = nmp->b_rptr;
3301 		/* initialize dl_capability_req_t */
3302 		oc = (dl_capability_req_t *)rptr;
3303 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3304 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3305 		    sizeof (dl_capab_zerocopy_t);
3306 		rptr += sizeof (dl_capability_req_t);
3307 
3308 		/* initialize dl_capability_sub_t */
3309 		bcopy(isub, rptr, sizeof (*isub));
3310 		rptr += sizeof (*isub);
3311 
3312 		/* initialize dl_capab_zerocopy_t */
3313 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3314 		*zc_oc = *zc_ic;
3315 
3316 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3317 		    "to enable zero-copy version %d\n", ill->ill_name,
3318 		    ZEROCOPY_VERSION_1));
3319 
3320 		/* set VMSAFE_MEM flag */
3321 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3322 
3323 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3324 		ill_dlpi_send(ill, nmp);
3325 	}
3326 }
3327 
3328 static void
3329 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3330 {
3331 	mblk_t *mp;
3332 	dl_capab_zerocopy_t *zerocopy_subcap;
3333 	dl_capability_sub_t *dl_subcap;
3334 	int size;
3335 
3336 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3337 		return;
3338 
3339 	ASSERT(ill->ill_zerocopy_capab != NULL);
3340 	/*
3341 	 * Clear the capability flag for Zero-copy but retain the
3342 	 * ill_zerocopy_capab structure since it's possible that another
3343 	 * thread is still referring to it.  The structure only gets
3344 	 * deallocated when we destroy the ill.
3345 	 */
3346 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3347 
3348 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3349 
3350 	mp = allocb(size, BPRI_HI);
3351 	if (mp == NULL) {
3352 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3353 		    "request to disable Zero-copy\n"));
3354 		return;
3355 	}
3356 
3357 	mp->b_wptr = mp->b_rptr + size;
3358 
3359 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3360 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3361 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3362 
3363 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3364 	zerocopy_subcap->zerocopy_version =
3365 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3366 	zerocopy_subcap->zerocopy_flags = 0;
3367 
3368 	if (*sc_mp != NULL)
3369 		linkb(*sc_mp, mp);
3370 	else
3371 		*sc_mp = mp;
3372 }
3373 
3374 /*
3375  * Process Large Segment Offload capability negotiation ack received from a
3376  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3377  * DL_CAPABILITY_ACK message.
3378  */
3379 static void
3380 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3381 {
3382 	mblk_t *nmp = NULL;
3383 	dl_capability_req_t *oc;
3384 	dl_capab_lso_t *lso_ic, *lso_oc;
3385 	ill_lso_capab_t **ill_lso_capab;
3386 	uint_t sub_dl_cap = isub->dl_cap;
3387 	uint8_t *capend;
3388 
3389 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3390 
3391 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3392 
3393 	/*
3394 	 * Note: range checks here are not absolutely sufficient to
3395 	 * make us robust against malformed messages sent by drivers;
3396 	 * this is in keeping with the rest of IP's dlpi handling.
3397 	 * (Remember, it's coming from something else in the kernel
3398 	 * address space)
3399 	 */
3400 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3401 	if (capend > mp->b_wptr) {
3402 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3403 		    "malformed sub-capability too long for mblk");
3404 		return;
3405 	}
3406 
3407 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3408 
3409 	if (lso_ic->lso_version != LSO_VERSION_1) {
3410 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3411 		    "unsupported LSO sub-capability (version %d, expected %d)",
3412 		    lso_ic->lso_version, LSO_VERSION_1);
3413 		return;
3414 	}
3415 
3416 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3417 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3418 		    "capability isn't as expected; pass-thru module(s) "
3419 		    "detected, discarding capability\n"));
3420 		return;
3421 	}
3422 
3423 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3424 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3425 		if (*ill_lso_capab == NULL) {
3426 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3427 			    KM_NOSLEEP);
3428 
3429 			if (*ill_lso_capab == NULL) {
3430 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3431 				    "could not enable LSO version %d "
3432 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3433 				    ill->ill_name);
3434 				return;
3435 			}
3436 		}
3437 
3438 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3439 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3440 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3441 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3442 
3443 		ip1dbg(("ill_capability_lso_ack: interface %s "
3444 		    "has enabled LSO\n ", ill->ill_name));
3445 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3446 		uint_t size;
3447 		uchar_t *rptr;
3448 
3449 		size = sizeof (dl_capability_req_t) +
3450 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3451 
3452 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3453 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3454 			    "could not enable LSO for %s (ENOMEM)\n",
3455 			    ill->ill_name);
3456 			return;
3457 		}
3458 
3459 		rptr = nmp->b_rptr;
3460 		/* initialize dl_capability_req_t */
3461 		oc = (dl_capability_req_t *)nmp->b_rptr;
3462 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3463 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3464 		    sizeof (dl_capab_lso_t);
3465 		nmp->b_rptr += sizeof (dl_capability_req_t);
3466 
3467 		/* initialize dl_capability_sub_t */
3468 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3469 		nmp->b_rptr += sizeof (*isub);
3470 
3471 		/* initialize dl_capab_lso_t */
3472 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3473 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3474 
3475 		nmp->b_rptr = rptr;
3476 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3477 
3478 		/* set ENABLE flag */
3479 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3480 
3481 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3482 		ill_dlpi_send(ill, nmp);
3483 	} else {
3484 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3485 		    "advertised %x LSO capability flags\n",
3486 		    ill->ill_name, lso_ic->lso_flags));
3487 	}
3488 }
3489 
3490 static void
3491 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3492 {
3493 	mblk_t *mp;
3494 	dl_capab_lso_t *lso_subcap;
3495 	dl_capability_sub_t *dl_subcap;
3496 	int size;
3497 
3498 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3499 		return;
3500 
3501 	ASSERT(ill->ill_lso_capab != NULL);
3502 	/*
3503 	 * Clear the capability flag for LSO but retain the
3504 	 * ill_lso_capab structure since it's possible that another
3505 	 * thread is still referring to it.  The structure only gets
3506 	 * deallocated when we destroy the ill.
3507 	 */
3508 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3509 
3510 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3511 
3512 	mp = allocb(size, BPRI_HI);
3513 	if (mp == NULL) {
3514 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3515 		    "request to disable LSO\n"));
3516 		return;
3517 	}
3518 
3519 	mp->b_wptr = mp->b_rptr + size;
3520 
3521 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3522 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3523 	dl_subcap->dl_length = sizeof (*lso_subcap);
3524 
3525 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3526 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3527 	lso_subcap->lso_flags = 0;
3528 
3529 	if (*sc_mp != NULL)
3530 		linkb(*sc_mp, mp);
3531 	else
3532 		*sc_mp = mp;
3533 }
3534 
3535 /*
3536  * Consume a new-style hardware capabilities negotiation ack.
3537  * Called from ip_rput_dlpi_writer().
3538  */
3539 void
3540 ill_capability_ack(ill_t *ill, mblk_t *mp)
3541 {
3542 	dl_capability_ack_t *capp;
3543 	dl_capability_sub_t *subp, *endp;
3544 
3545 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3546 		ill->ill_dlpi_capab_state = IDS_OK;
3547 
3548 	capp = (dl_capability_ack_t *)mp->b_rptr;
3549 
3550 	if (capp->dl_sub_length == 0)
3551 		/* no new-style capabilities */
3552 		return;
3553 
3554 	/* make sure the driver supplied correct dl_sub_length */
3555 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3556 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3557 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3558 		return;
3559 	}
3560 
3561 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3562 	/*
3563 	 * There are sub-capabilities. Process the ones we know about.
3564 	 * Loop until we don't have room for another sub-cap header..
3565 	 */
3566 	for (subp = SC(capp, capp->dl_sub_offset),
3567 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3568 	    subp <= endp;
3569 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3570 
3571 		switch (subp->dl_cap) {
3572 		case DL_CAPAB_ID_WRAPPER:
3573 			ill_capability_id_ack(ill, mp, subp);
3574 			break;
3575 		default:
3576 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3577 			break;
3578 		}
3579 	}
3580 #undef SC
3581 }
3582 
3583 /*
3584  * This routine is called to scan the fragmentation reassembly table for
3585  * the specified ILL for any packets that are starting to smell.
3586  * dead_interval is the maximum time in seconds that will be tolerated.  It
3587  * will either be the value specified in ip_g_frag_timeout, or zero if the
3588  * ILL is shutting down and it is time to blow everything off.
3589  *
3590  * It returns the number of seconds (as a time_t) that the next frag timer
3591  * should be scheduled for, 0 meaning that the timer doesn't need to be
3592  * re-started.  Note that the method of calculating next_timeout isn't
3593  * entirely accurate since time will flow between the time we grab
3594  * current_time and the time we schedule the next timeout.  This isn't a
3595  * big problem since this is the timer for sending an ICMP reassembly time
3596  * exceeded messages, and it doesn't have to be exactly accurate.
3597  *
3598  * This function is
3599  * sometimes called as writer, although this is not required.
3600  */
3601 time_t
3602 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3603 {
3604 	ipfb_t	*ipfb;
3605 	ipfb_t	*endp;
3606 	ipf_t	*ipf;
3607 	ipf_t	*ipfnext;
3608 	mblk_t	*mp;
3609 	time_t	current_time = gethrestime_sec();
3610 	time_t	next_timeout = 0;
3611 	uint32_t	hdr_length;
3612 	mblk_t	*send_icmp_head;
3613 	mblk_t	*send_icmp_head_v6;
3614 	zoneid_t zoneid;
3615 	ip_stack_t *ipst = ill->ill_ipst;
3616 
3617 	ipfb = ill->ill_frag_hash_tbl;
3618 	if (ipfb == NULL)
3619 		return (B_FALSE);
3620 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3621 	/* Walk the frag hash table. */
3622 	for (; ipfb < endp; ipfb++) {
3623 		send_icmp_head = NULL;
3624 		send_icmp_head_v6 = NULL;
3625 		mutex_enter(&ipfb->ipfb_lock);
3626 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3627 			time_t frag_time = current_time - ipf->ipf_timestamp;
3628 			time_t frag_timeout;
3629 
3630 			if (frag_time < dead_interval) {
3631 				/*
3632 				 * There are some outstanding fragments
3633 				 * that will timeout later.  Make note of
3634 				 * the time so that we can reschedule the
3635 				 * next timeout appropriately.
3636 				 */
3637 				frag_timeout = dead_interval - frag_time;
3638 				if (next_timeout == 0 ||
3639 				    frag_timeout < next_timeout) {
3640 					next_timeout = frag_timeout;
3641 				}
3642 				break;
3643 			}
3644 			/* Time's up.  Get it out of here. */
3645 			hdr_length = ipf->ipf_nf_hdr_len;
3646 			ipfnext = ipf->ipf_hash_next;
3647 			if (ipfnext)
3648 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3649 			*ipf->ipf_ptphn = ipfnext;
3650 			mp = ipf->ipf_mp->b_cont;
3651 			for (; mp; mp = mp->b_cont) {
3652 				/* Extra points for neatness. */
3653 				IP_REASS_SET_START(mp, 0);
3654 				IP_REASS_SET_END(mp, 0);
3655 			}
3656 			mp = ipf->ipf_mp->b_cont;
3657 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3658 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3659 			ipfb->ipfb_count -= ipf->ipf_count;
3660 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3661 			ipfb->ipfb_frag_pkts--;
3662 			/*
3663 			 * We do not send any icmp message from here because
3664 			 * we currently are holding the ipfb_lock for this
3665 			 * hash chain. If we try and send any icmp messages
3666 			 * from here we may end up via a put back into ip
3667 			 * trying to get the same lock, causing a recursive
3668 			 * mutex panic. Instead we build a list and send all
3669 			 * the icmp messages after we have dropped the lock.
3670 			 */
3671 			if (ill->ill_isv6) {
3672 				if (hdr_length != 0) {
3673 					mp->b_next = send_icmp_head_v6;
3674 					send_icmp_head_v6 = mp;
3675 				} else {
3676 					freemsg(mp);
3677 				}
3678 			} else {
3679 				if (hdr_length != 0) {
3680 					mp->b_next = send_icmp_head;
3681 					send_icmp_head = mp;
3682 				} else {
3683 					freemsg(mp);
3684 				}
3685 			}
3686 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3687 			freeb(ipf->ipf_mp);
3688 		}
3689 		mutex_exit(&ipfb->ipfb_lock);
3690 		/*
3691 		 * Now need to send any icmp messages that we delayed from
3692 		 * above.
3693 		 */
3694 		while (send_icmp_head_v6 != NULL) {
3695 			ip6_t *ip6h;
3696 
3697 			mp = send_icmp_head_v6;
3698 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3699 			mp->b_next = NULL;
3700 			if (mp->b_datap->db_type == M_CTL)
3701 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3702 			else
3703 				ip6h = (ip6_t *)mp->b_rptr;
3704 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3705 			    ill, ipst);
3706 			if (zoneid == ALL_ZONES) {
3707 				freemsg(mp);
3708 			} else {
3709 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3710 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3711 				    B_FALSE, zoneid, ipst);
3712 			}
3713 		}
3714 		while (send_icmp_head != NULL) {
3715 			ipaddr_t dst;
3716 
3717 			mp = send_icmp_head;
3718 			send_icmp_head = send_icmp_head->b_next;
3719 			mp->b_next = NULL;
3720 
3721 			if (mp->b_datap->db_type == M_CTL)
3722 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3723 			else
3724 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3725 
3726 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3727 			if (zoneid == ALL_ZONES) {
3728 				freemsg(mp);
3729 			} else {
3730 				icmp_time_exceeded(ill->ill_wq, mp,
3731 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3732 				    ipst);
3733 			}
3734 		}
3735 	}
3736 	/*
3737 	 * A non-dying ILL will use the return value to decide whether to
3738 	 * restart the frag timer, and for how long.
3739 	 */
3740 	return (next_timeout);
3741 }
3742 
3743 /*
3744  * This routine is called when the approximate count of mblk memory used
3745  * for the specified ILL has exceeded max_count.
3746  */
3747 void
3748 ill_frag_prune(ill_t *ill, uint_t max_count)
3749 {
3750 	ipfb_t	*ipfb;
3751 	ipf_t	*ipf;
3752 	size_t	count;
3753 
3754 	/*
3755 	 * If we are here within ip_min_frag_prune_time msecs remove
3756 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3757 	 * ill_frag_free_num_pkts.
3758 	 */
3759 	mutex_enter(&ill->ill_lock);
3760 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3761 	    (ip_min_frag_prune_time != 0 ?
3762 	    ip_min_frag_prune_time : msec_per_tick)) {
3763 
3764 		ill->ill_frag_free_num_pkts++;
3765 
3766 	} else {
3767 		ill->ill_frag_free_num_pkts = 0;
3768 	}
3769 	ill->ill_last_frag_clean_time = lbolt;
3770 	mutex_exit(&ill->ill_lock);
3771 
3772 	/*
3773 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3774 	 */
3775 	if (ill->ill_frag_free_num_pkts != 0) {
3776 		int ix;
3777 
3778 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3779 			ipfb = &ill->ill_frag_hash_tbl[ix];
3780 			mutex_enter(&ipfb->ipfb_lock);
3781 			if (ipfb->ipfb_ipf != NULL) {
3782 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3783 				    ill->ill_frag_free_num_pkts);
3784 			}
3785 			mutex_exit(&ipfb->ipfb_lock);
3786 		}
3787 	}
3788 	/*
3789 	 * While the reassembly list for this ILL is too big, prune a fragment
3790 	 * queue by age, oldest first.
3791 	 */
3792 	while (ill->ill_frag_count > max_count) {
3793 		int	ix;
3794 		ipfb_t	*oipfb = NULL;
3795 		uint_t	oldest = UINT_MAX;
3796 
3797 		count = 0;
3798 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3799 			ipfb = &ill->ill_frag_hash_tbl[ix];
3800 			mutex_enter(&ipfb->ipfb_lock);
3801 			ipf = ipfb->ipfb_ipf;
3802 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3803 				oldest = ipf->ipf_gen;
3804 				oipfb = ipfb;
3805 			}
3806 			count += ipfb->ipfb_count;
3807 			mutex_exit(&ipfb->ipfb_lock);
3808 		}
3809 		if (oipfb == NULL)
3810 			break;
3811 
3812 		if (count <= max_count)
3813 			return;	/* Somebody beat us to it, nothing to do */
3814 		mutex_enter(&oipfb->ipfb_lock);
3815 		ipf = oipfb->ipfb_ipf;
3816 		if (ipf != NULL) {
3817 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3818 		}
3819 		mutex_exit(&oipfb->ipfb_lock);
3820 	}
3821 }
3822 
3823 /*
3824  * free 'free_cnt' fragmented packets starting at ipf.
3825  */
3826 void
3827 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3828 {
3829 	size_t	count;
3830 	mblk_t	*mp;
3831 	mblk_t	*tmp;
3832 	ipf_t **ipfp = ipf->ipf_ptphn;
3833 
3834 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3835 	ASSERT(ipfp != NULL);
3836 	ASSERT(ipf != NULL);
3837 
3838 	while (ipf != NULL && free_cnt-- > 0) {
3839 		count = ipf->ipf_count;
3840 		mp = ipf->ipf_mp;
3841 		ipf = ipf->ipf_hash_next;
3842 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3843 			IP_REASS_SET_START(tmp, 0);
3844 			IP_REASS_SET_END(tmp, 0);
3845 		}
3846 		atomic_add_32(&ill->ill_frag_count, -count);
3847 		ASSERT(ipfb->ipfb_count >= count);
3848 		ipfb->ipfb_count -= count;
3849 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3850 		ipfb->ipfb_frag_pkts--;
3851 		freemsg(mp);
3852 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3853 	}
3854 
3855 	if (ipf)
3856 		ipf->ipf_ptphn = ipfp;
3857 	ipfp[0] = ipf;
3858 }
3859 
3860 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3861 	"obsolete and may be removed in a future release of Solaris.  Use " \
3862 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3863 
3864 /*
3865  * For obsolete per-interface forwarding configuration;
3866  * called in response to ND_GET.
3867  */
3868 /* ARGSUSED */
3869 static int
3870 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3871 {
3872 	ill_t *ill = (ill_t *)cp;
3873 
3874 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3875 
3876 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3877 	return (0);
3878 }
3879 
3880 /*
3881  * For obsolete per-interface forwarding configuration;
3882  * called in response to ND_SET.
3883  */
3884 /* ARGSUSED */
3885 static int
3886 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3887     cred_t *ioc_cr)
3888 {
3889 	long value;
3890 	int retval;
3891 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3892 
3893 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3894 
3895 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3896 	    value < 0 || value > 1) {
3897 		return (EINVAL);
3898 	}
3899 
3900 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3901 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3902 	rw_exit(&ipst->ips_ill_g_lock);
3903 	return (retval);
3904 }
3905 
3906 /*
3907  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3908  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3909  * up RTS_IFINFO routing socket messages for each interface whose flags we
3910  * change.
3911  */
3912 int
3913 ill_forward_set(ill_t *ill, boolean_t enable)
3914 {
3915 	ill_group_t *illgrp;
3916 	ip_stack_t	*ipst = ill->ill_ipst;
3917 
3918 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3919 
3920 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3921 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3922 		return (0);
3923 
3924 	if (IS_LOOPBACK(ill))
3925 		return (EINVAL);
3926 
3927 	/*
3928 	 * If the ill is in an IPMP group, set the forwarding policy on all
3929 	 * members of the group to the same value.
3930 	 */
3931 	illgrp = ill->ill_group;
3932 	if (illgrp != NULL) {
3933 		ill_t *tmp_ill;
3934 
3935 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3936 		    tmp_ill = tmp_ill->ill_group_next) {
3937 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3938 			    (enable ? "Enabling" : "Disabling"),
3939 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3940 			    tmp_ill->ill_name));
3941 			mutex_enter(&tmp_ill->ill_lock);
3942 			if (enable)
3943 				tmp_ill->ill_flags |= ILLF_ROUTER;
3944 			else
3945 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3946 			mutex_exit(&tmp_ill->ill_lock);
3947 			if (tmp_ill->ill_isv6)
3948 				ill_set_nce_router_flags(tmp_ill, enable);
3949 			/* Notify routing socket listeners of this change. */
3950 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3951 		}
3952 	} else {
3953 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3954 		    (enable ? "Enabling" : "Disabling"),
3955 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3956 		mutex_enter(&ill->ill_lock);
3957 		if (enable)
3958 			ill->ill_flags |= ILLF_ROUTER;
3959 		else
3960 			ill->ill_flags &= ~ILLF_ROUTER;
3961 		mutex_exit(&ill->ill_lock);
3962 		if (ill->ill_isv6)
3963 			ill_set_nce_router_flags(ill, enable);
3964 		/* Notify routing socket listeners of this change. */
3965 		ip_rts_ifmsg(ill->ill_ipif);
3966 	}
3967 
3968 	return (0);
3969 }
3970 
3971 /*
3972  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3973  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3974  * set or clear.
3975  */
3976 static void
3977 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3978 {
3979 	ipif_t *ipif;
3980 	nce_t *nce;
3981 
3982 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3983 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3984 		if (nce != NULL) {
3985 			mutex_enter(&nce->nce_lock);
3986 			if (enable)
3987 				nce->nce_flags |= NCE_F_ISROUTER;
3988 			else
3989 				nce->nce_flags &= ~NCE_F_ISROUTER;
3990 			mutex_exit(&nce->nce_lock);
3991 			NCE_REFRELE(nce);
3992 		}
3993 	}
3994 }
3995 
3996 /*
3997  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3998  * for this ill.  Make sure the v6/v4 question has been answered about this
3999  * ill.  The creation of this ndd variable is only for backwards compatibility.
4000  * The preferred way to control per-interface IP forwarding is through the
4001  * ILLF_ROUTER interface flag.
4002  */
4003 static int
4004 ill_set_ndd_name(ill_t *ill)
4005 {
4006 	char *suffix;
4007 	ip_stack_t	*ipst = ill->ill_ipst;
4008 
4009 	ASSERT(IAM_WRITER_ILL(ill));
4010 
4011 	if (ill->ill_isv6)
4012 		suffix = ipv6_forward_suffix;
4013 	else
4014 		suffix = ipv4_forward_suffix;
4015 
4016 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4017 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4018 	/*
4019 	 * Copies over the '\0'.
4020 	 * Note that strlen(suffix) is always bounded.
4021 	 */
4022 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4023 	    strlen(suffix) + 1);
4024 
4025 	/*
4026 	 * Use of the nd table requires holding the reader lock.
4027 	 * Modifying the nd table thru nd_load/nd_unload requires
4028 	 * the writer lock.
4029 	 */
4030 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4031 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4032 	    nd_ill_forward_set, (caddr_t)ill)) {
4033 		/*
4034 		 * If the nd_load failed, it only meant that it could not
4035 		 * allocate a new bunch of room for further NDD expansion.
4036 		 * Because of that, the ill_ndd_name will be set to 0, and
4037 		 * this interface is at the mercy of the global ip_forwarding
4038 		 * variable.
4039 		 */
4040 		rw_exit(&ipst->ips_ip_g_nd_lock);
4041 		ill->ill_ndd_name = NULL;
4042 		return (ENOMEM);
4043 	}
4044 	rw_exit(&ipst->ips_ip_g_nd_lock);
4045 	return (0);
4046 }
4047 
4048 /*
4049  * Intializes the context structure and returns the first ill in the list
4050  * cuurently start_list and end_list can have values:
4051  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4052  * IP_V4_G_HEAD		Traverse IPV4 list only.
4053  * IP_V6_G_HEAD		Traverse IPV6 list only.
4054  */
4055 
4056 /*
4057  * We don't check for CONDEMNED ills here. Caller must do that if
4058  * necessary under the ill lock.
4059  */
4060 ill_t *
4061 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4062     ip_stack_t *ipst)
4063 {
4064 	ill_if_t *ifp;
4065 	ill_t *ill;
4066 	avl_tree_t *avl_tree;
4067 
4068 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4069 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4070 
4071 	/*
4072 	 * setup the lists to search
4073 	 */
4074 	if (end_list != MAX_G_HEADS) {
4075 		ctx->ctx_current_list = start_list;
4076 		ctx->ctx_last_list = end_list;
4077 	} else {
4078 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4079 		ctx->ctx_current_list = 0;
4080 	}
4081 
4082 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4083 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4084 		if (ifp != (ill_if_t *)
4085 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4086 			avl_tree = &ifp->illif_avl_by_ppa;
4087 			ill = avl_first(avl_tree);
4088 			/*
4089 			 * ill is guaranteed to be non NULL or ifp should have
4090 			 * not existed.
4091 			 */
4092 			ASSERT(ill != NULL);
4093 			return (ill);
4094 		}
4095 		ctx->ctx_current_list++;
4096 	}
4097 
4098 	return (NULL);
4099 }
4100 
4101 /*
4102  * returns the next ill in the list. ill_first() must have been called
4103  * before calling ill_next() or bad things will happen.
4104  */
4105 
4106 /*
4107  * We don't check for CONDEMNED ills here. Caller must do that if
4108  * necessary under the ill lock.
4109  */
4110 ill_t *
4111 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4112 {
4113 	ill_if_t *ifp;
4114 	ill_t *ill;
4115 	ip_stack_t	*ipst = lastill->ill_ipst;
4116 
4117 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4118 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4119 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4120 	    AVL_AFTER)) != NULL) {
4121 		return (ill);
4122 	}
4123 
4124 	/* goto next ill_ifp in the list. */
4125 	ifp = lastill->ill_ifptr->illif_next;
4126 
4127 	/* make sure not at end of circular list */
4128 	while (ifp ==
4129 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4130 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4131 			return (NULL);
4132 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4133 	}
4134 
4135 	return (avl_first(&ifp->illif_avl_by_ppa));
4136 }
4137 
4138 /*
4139  * Check interface name for correct format which is name+ppa.
4140  * name can contain characters and digits, the right most digits
4141  * make up the ppa number. use of octal is not allowed, name must contain
4142  * a ppa, return pointer to the start of ppa.
4143  * In case of error return NULL.
4144  */
4145 static char *
4146 ill_get_ppa_ptr(char *name)
4147 {
4148 	int namelen = mi_strlen(name);
4149 
4150 	int len = namelen;
4151 
4152 	name += len;
4153 	while (len > 0) {
4154 		name--;
4155 		if (*name < '0' || *name > '9')
4156 			break;
4157 		len--;
4158 	}
4159 
4160 	/* empty string, all digits, or no trailing digits */
4161 	if (len == 0 || len == (int)namelen)
4162 		return (NULL);
4163 
4164 	name++;
4165 	/* check for attempted use of octal */
4166 	if (*name == '0' && len != (int)namelen - 1)
4167 		return (NULL);
4168 	return (name);
4169 }
4170 
4171 /*
4172  * use avl tree to locate the ill.
4173  */
4174 static ill_t *
4175 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4176     ipsq_func_t func, int *error, ip_stack_t *ipst)
4177 {
4178 	char *ppa_ptr = NULL;
4179 	int len;
4180 	uint_t ppa;
4181 	ill_t *ill = NULL;
4182 	ill_if_t *ifp;
4183 	int list;
4184 	ipsq_t *ipsq;
4185 
4186 	if (error != NULL)
4187 		*error = 0;
4188 
4189 	/*
4190 	 * get ppa ptr
4191 	 */
4192 	if (isv6)
4193 		list = IP_V6_G_HEAD;
4194 	else
4195 		list = IP_V4_G_HEAD;
4196 
4197 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4198 		if (error != NULL)
4199 			*error = ENXIO;
4200 		return (NULL);
4201 	}
4202 
4203 	len = ppa_ptr - name + 1;
4204 
4205 	ppa = stoi(&ppa_ptr);
4206 
4207 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4208 
4209 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4210 		/*
4211 		 * match is done on len - 1 as the name is not null
4212 		 * terminated it contains ppa in addition to the interface
4213 		 * name.
4214 		 */
4215 		if ((ifp->illif_name_len == len) &&
4216 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4217 			break;
4218 		} else {
4219 			ifp = ifp->illif_next;
4220 		}
4221 	}
4222 
4223 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4224 		/*
4225 		 * Even the interface type does not exist.
4226 		 */
4227 		if (error != NULL)
4228 			*error = ENXIO;
4229 		return (NULL);
4230 	}
4231 
4232 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4233 	if (ill != NULL) {
4234 		/*
4235 		 * The block comment at the start of ipif_down
4236 		 * explains the use of the macros used below
4237 		 */
4238 		GRAB_CONN_LOCK(q);
4239 		mutex_enter(&ill->ill_lock);
4240 		if (ILL_CAN_LOOKUP(ill)) {
4241 			ill_refhold_locked(ill);
4242 			mutex_exit(&ill->ill_lock);
4243 			RELEASE_CONN_LOCK(q);
4244 			return (ill);
4245 		} else if (ILL_CAN_WAIT(ill, q)) {
4246 			ipsq = ill->ill_phyint->phyint_ipsq;
4247 			mutex_enter(&ipsq->ipsq_lock);
4248 			mutex_exit(&ill->ill_lock);
4249 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4250 			mutex_exit(&ipsq->ipsq_lock);
4251 			RELEASE_CONN_LOCK(q);
4252 			if (error != NULL)
4253 				*error = EINPROGRESS;
4254 			return (NULL);
4255 		}
4256 		mutex_exit(&ill->ill_lock);
4257 		RELEASE_CONN_LOCK(q);
4258 	}
4259 	if (error != NULL)
4260 		*error = ENXIO;
4261 	return (NULL);
4262 }
4263 
4264 /*
4265  * comparison function for use with avl.
4266  */
4267 static int
4268 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4269 {
4270 	uint_t ppa;
4271 	uint_t ill_ppa;
4272 
4273 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4274 
4275 	ppa = *((uint_t *)ppa_ptr);
4276 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4277 	/*
4278 	 * We want the ill with the lowest ppa to be on the
4279 	 * top.
4280 	 */
4281 	if (ill_ppa < ppa)
4282 		return (1);
4283 	if (ill_ppa > ppa)
4284 		return (-1);
4285 	return (0);
4286 }
4287 
4288 /*
4289  * remove an interface type from the global list.
4290  */
4291 static void
4292 ill_delete_interface_type(ill_if_t *interface)
4293 {
4294 	ASSERT(interface != NULL);
4295 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4296 
4297 	avl_destroy(&interface->illif_avl_by_ppa);
4298 	if (interface->illif_ppa_arena != NULL)
4299 		vmem_destroy(interface->illif_ppa_arena);
4300 
4301 	remque(interface);
4302 
4303 	mi_free(interface);
4304 }
4305 
4306 /*
4307  * remove ill from the global list.
4308  */
4309 static void
4310 ill_glist_delete(ill_t *ill)
4311 {
4312 	ip_stack_t	*ipst;
4313 
4314 	if (ill == NULL)
4315 		return;
4316 	ipst = ill->ill_ipst;
4317 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4318 
4319 	/*
4320 	 * If the ill was never inserted into the AVL tree
4321 	 * we skip the if branch.
4322 	 */
4323 	if (ill->ill_ifptr != NULL) {
4324 		/*
4325 		 * remove from AVL tree and free ppa number
4326 		 */
4327 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4328 
4329 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4330 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4331 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4332 		}
4333 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4334 			ill_delete_interface_type(ill->ill_ifptr);
4335 		}
4336 
4337 		/*
4338 		 * Indicate ill is no longer in the list.
4339 		 */
4340 		ill->ill_ifptr = NULL;
4341 		ill->ill_name_length = 0;
4342 		ill->ill_name[0] = '\0';
4343 		ill->ill_ppa = UINT_MAX;
4344 	}
4345 
4346 	/*
4347 	 * Run the unplumb hook after the NIC has disappeared from being
4348 	 * visible so that attempts to revalidate its existance will fail.
4349 	 *
4350 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4351 	 * that the ordering of delivered events to listeners matches the
4352 	 * order of them in the kernel.
4353 	 */
4354 	mutex_enter(&ill->ill_lock);
4355 	ill_nic_info_dispatch(ill);
4356 	mutex_exit(&ill->ill_lock);
4357 
4358 	/* Generate NE_UNPLUMB event for ill_name. */
4359 	(void) ill_hook_event_create(ill, 0, NE_UNPLUMB, ill->ill_name,
4360 	    ill->ill_name_length);
4361 
4362 	ill_phyint_free(ill);
4363 
4364 	mutex_enter(&ill->ill_lock);
4365 	ill_nic_info_dispatch(ill);
4366 	mutex_exit(&ill->ill_lock);
4367 
4368 	rw_exit(&ipst->ips_ill_g_lock);
4369 }
4370 
4371 /*
4372  * allocate a ppa, if the number of plumbed interfaces of this type are
4373  * less than ill_no_arena do a linear search to find a unused ppa.
4374  * When the number goes beyond ill_no_arena switch to using an arena.
4375  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4376  * is the return value for an error condition, so allocation starts at one
4377  * and is decremented by one.
4378  */
4379 static int
4380 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4381 {
4382 	ill_t *tmp_ill;
4383 	uint_t start, end;
4384 	int ppa;
4385 
4386 	if (ifp->illif_ppa_arena == NULL &&
4387 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4388 		/*
4389 		 * Create an arena.
4390 		 */
4391 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4392 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4393 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4394 			/* allocate what has already been assigned */
4395 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4396 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4397 		    tmp_ill, AVL_AFTER)) {
4398 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4399 			    1,		/* size */
4400 			    1,		/* align/quantum */
4401 			    0,		/* phase */
4402 			    0,		/* nocross */
4403 			    /* minaddr */
4404 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4405 			    /* maxaddr */
4406 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4407 			    VM_NOSLEEP|VM_FIRSTFIT);
4408 			if (ppa == 0) {
4409 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4410 				    " failed while switching"));
4411 				vmem_destroy(ifp->illif_ppa_arena);
4412 				ifp->illif_ppa_arena = NULL;
4413 				break;
4414 			}
4415 		}
4416 	}
4417 
4418 	if (ifp->illif_ppa_arena != NULL) {
4419 		if (ill->ill_ppa == UINT_MAX) {
4420 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4421 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4422 			if (ppa == 0)
4423 				return (EAGAIN);
4424 			ill->ill_ppa = --ppa;
4425 		} else {
4426 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4427 			    1, 		/* size */
4428 			    1, 		/* align/quantum */
4429 			    0, 		/* phase */
4430 			    0, 		/* nocross */
4431 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4432 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4433 			    VM_NOSLEEP|VM_FIRSTFIT);
4434 			/*
4435 			 * Most likely the allocation failed because
4436 			 * the requested ppa was in use.
4437 			 */
4438 			if (ppa == 0)
4439 				return (EEXIST);
4440 		}
4441 		return (0);
4442 	}
4443 
4444 	/*
4445 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4446 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4447 	 */
4448 	if (ill->ill_ppa == UINT_MAX) {
4449 		end = UINT_MAX - 1;
4450 		start = 0;
4451 	} else {
4452 		end = start = ill->ill_ppa;
4453 	}
4454 
4455 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4456 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4457 		if (start++ >= end) {
4458 			if (ill->ill_ppa == UINT_MAX)
4459 				return (EAGAIN);
4460 			else
4461 				return (EEXIST);
4462 		}
4463 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4464 	}
4465 	ill->ill_ppa = start;
4466 	return (0);
4467 }
4468 
4469 /*
4470  * Insert ill into the list of configured ill's. Once this function completes,
4471  * the ill is globally visible and is available through lookups. More precisely
4472  * this happens after the caller drops the ill_g_lock.
4473  */
4474 static int
4475 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4476 {
4477 	ill_if_t *ill_interface;
4478 	avl_index_t where = 0;
4479 	int error;
4480 	int name_length;
4481 	int index;
4482 	boolean_t check_length = B_FALSE;
4483 	ip_stack_t	*ipst = ill->ill_ipst;
4484 
4485 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4486 
4487 	name_length = mi_strlen(name) + 1;
4488 
4489 	if (isv6)
4490 		index = IP_V6_G_HEAD;
4491 	else
4492 		index = IP_V4_G_HEAD;
4493 
4494 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4495 	/*
4496 	 * Search for interface type based on name
4497 	 */
4498 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4499 		if ((ill_interface->illif_name_len == name_length) &&
4500 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4501 			break;
4502 		}
4503 		ill_interface = ill_interface->illif_next;
4504 	}
4505 
4506 	/*
4507 	 * Interface type not found, create one.
4508 	 */
4509 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4510 
4511 		ill_g_head_t ghead;
4512 
4513 		/*
4514 		 * allocate ill_if_t structure
4515 		 */
4516 
4517 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4518 		if (ill_interface == NULL) {
4519 			return (ENOMEM);
4520 		}
4521 
4522 
4523 
4524 		(void) strcpy(ill_interface->illif_name, name);
4525 		ill_interface->illif_name_len = name_length;
4526 
4527 		avl_create(&ill_interface->illif_avl_by_ppa,
4528 		    ill_compare_ppa, sizeof (ill_t),
4529 		    offsetof(struct ill_s, ill_avl_byppa));
4530 
4531 		/*
4532 		 * link the structure in the back to maintain order
4533 		 * of configuration for ifconfig output.
4534 		 */
4535 		ghead = ipst->ips_ill_g_heads[index];
4536 		insque(ill_interface, ghead.ill_g_list_tail);
4537 
4538 	}
4539 
4540 	if (ill->ill_ppa == UINT_MAX)
4541 		check_length = B_TRUE;
4542 
4543 	error = ill_alloc_ppa(ill_interface, ill);
4544 	if (error != 0) {
4545 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4546 			ill_delete_interface_type(ill->ill_ifptr);
4547 		return (error);
4548 	}
4549 
4550 	/*
4551 	 * When the ppa is choosen by the system, check that there is
4552 	 * enough space to insert ppa. if a specific ppa was passed in this
4553 	 * check is not required as the interface name passed in will have
4554 	 * the right ppa in it.
4555 	 */
4556 	if (check_length) {
4557 		/*
4558 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4559 		 */
4560 		char buf[sizeof (uint_t) * 3];
4561 
4562 		/*
4563 		 * convert ppa to string to calculate the amount of space
4564 		 * required for it in the name.
4565 		 */
4566 		numtos(ill->ill_ppa, buf);
4567 
4568 		/* Do we have enough space to insert ppa ? */
4569 
4570 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4571 			/* Free ppa and interface type struct */
4572 			if (ill_interface->illif_ppa_arena != NULL) {
4573 				vmem_free(ill_interface->illif_ppa_arena,
4574 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4575 			}
4576 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4577 				ill_delete_interface_type(ill->ill_ifptr);
4578 
4579 			return (EINVAL);
4580 		}
4581 	}
4582 
4583 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4584 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4585 
4586 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4587 	    &where);
4588 	ill->ill_ifptr = ill_interface;
4589 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4590 
4591 	ill_phyint_reinit(ill);
4592 	return (0);
4593 }
4594 
4595 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4596 static boolean_t
4597 ipsq_init(ill_t *ill)
4598 {
4599 	ipsq_t  *ipsq;
4600 
4601 	/* Init the ipsq and impicitly enter as writer */
4602 	ill->ill_phyint->phyint_ipsq =
4603 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4604 	if (ill->ill_phyint->phyint_ipsq == NULL)
4605 		return (B_FALSE);
4606 	ipsq = ill->ill_phyint->phyint_ipsq;
4607 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4608 	ill->ill_phyint->phyint_ipsq_next = NULL;
4609 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4610 	ipsq->ipsq_refs = 1;
4611 	ipsq->ipsq_writer = curthread;
4612 	ipsq->ipsq_reentry_cnt = 1;
4613 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4614 #ifdef DEBUG
4615 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4616 	    IPSQ_STACK_DEPTH);
4617 #endif
4618 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4619 	return (B_TRUE);
4620 }
4621 
4622 /*
4623  * ill_init is called by ip_open when a device control stream is opened.
4624  * It does a few initializations, and shoots a DL_INFO_REQ message down
4625  * to the driver.  The response is later picked up in ip_rput_dlpi and
4626  * used to set up default mechanisms for talking to the driver.  (Always
4627  * called as writer.)
4628  *
4629  * If this function returns error, ip_open will call ip_close which in
4630  * turn will call ill_delete to clean up any memory allocated here that
4631  * is not yet freed.
4632  */
4633 int
4634 ill_init(queue_t *q, ill_t *ill)
4635 {
4636 	int	count;
4637 	dl_info_req_t	*dlir;
4638 	mblk_t	*info_mp;
4639 	uchar_t *frag_ptr;
4640 
4641 	/*
4642 	 * The ill is initialized to zero by mi_alloc*(). In addition
4643 	 * some fields already contain valid values, initialized in
4644 	 * ip_open(), before we reach here.
4645 	 */
4646 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4647 
4648 	ill->ill_rq = q;
4649 	ill->ill_wq = WR(q);
4650 
4651 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4652 	    BPRI_HI);
4653 	if (info_mp == NULL)
4654 		return (ENOMEM);
4655 
4656 	/*
4657 	 * Allocate sufficient space to contain our fragment hash table and
4658 	 * the device name.
4659 	 */
4660 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4661 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4662 	if (frag_ptr == NULL) {
4663 		freemsg(info_mp);
4664 		return (ENOMEM);
4665 	}
4666 	ill->ill_frag_ptr = frag_ptr;
4667 	ill->ill_frag_free_num_pkts = 0;
4668 	ill->ill_last_frag_clean_time = 0;
4669 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4670 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4671 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4672 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4673 		    NULL, MUTEX_DEFAULT, NULL);
4674 	}
4675 
4676 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4677 	if (ill->ill_phyint == NULL) {
4678 		freemsg(info_mp);
4679 		mi_free(frag_ptr);
4680 		return (ENOMEM);
4681 	}
4682 
4683 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4684 	/*
4685 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4686 	 * at this point because of the following reason. If we can't
4687 	 * enter the ipsq at some point and cv_wait, the writer that
4688 	 * wakes us up tries to locate us using the list of all phyints
4689 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4690 	 * If we don't set it now, we risk a missed wakeup.
4691 	 */
4692 	ill->ill_phyint->phyint_illv4 = ill;
4693 	ill->ill_ppa = UINT_MAX;
4694 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4695 
4696 	if (!ipsq_init(ill)) {
4697 		freemsg(info_mp);
4698 		mi_free(frag_ptr);
4699 		mi_free(ill->ill_phyint);
4700 		return (ENOMEM);
4701 	}
4702 
4703 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4704 
4705 	/* Frag queue limit stuff */
4706 	ill->ill_frag_count = 0;
4707 	ill->ill_ipf_gen = 0;
4708 
4709 	ill->ill_global_timer = INFINITY;
4710 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4711 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4712 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4713 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4714 
4715 	/*
4716 	 * Initialize IPv6 configuration variables.  The IP module is always
4717 	 * opened as an IPv4 module.  Instead tracking down the cases where
4718 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4719 	 * here for convenience, this has no effect until the ill is set to do
4720 	 * IPv6.
4721 	 */
4722 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4723 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4724 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4725 	ill->ill_max_buf = ND_MAX_Q;
4726 	ill->ill_refcnt = 0;
4727 
4728 	/* Send down the Info Request to the driver. */
4729 	info_mp->b_datap->db_type = M_PCPROTO;
4730 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4731 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4732 	dlir->dl_primitive = DL_INFO_REQ;
4733 
4734 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4735 
4736 	qprocson(q);
4737 	ill_dlpi_send(ill, info_mp);
4738 
4739 	return (0);
4740 }
4741 
4742 /*
4743  * ill_dls_info
4744  * creates datalink socket info from the device.
4745  */
4746 int
4747 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4748 {
4749 	size_t	len;
4750 	ill_t	*ill = ipif->ipif_ill;
4751 
4752 	sdl->sdl_family = AF_LINK;
4753 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4754 	sdl->sdl_type = ill->ill_type;
4755 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4756 	len = strlen(sdl->sdl_data);
4757 	ASSERT(len < 256);
4758 	sdl->sdl_nlen = (uchar_t)len;
4759 	sdl->sdl_alen = ill->ill_phys_addr_length;
4760 	sdl->sdl_slen = 0;
4761 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4762 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4763 
4764 	return (sizeof (struct sockaddr_dl));
4765 }
4766 
4767 /*
4768  * ill_xarp_info
4769  * creates xarp info from the device.
4770  */
4771 static int
4772 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4773 {
4774 	sdl->sdl_family = AF_LINK;
4775 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4776 	sdl->sdl_type = ill->ill_type;
4777 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4778 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4779 	sdl->sdl_alen = ill->ill_phys_addr_length;
4780 	sdl->sdl_slen = 0;
4781 	return (sdl->sdl_nlen);
4782 }
4783 
4784 static int
4785 loopback_kstat_update(kstat_t *ksp, int rw)
4786 {
4787 	kstat_named_t *kn;
4788 	netstackid_t	stackid;
4789 	netstack_t	*ns;
4790 	ip_stack_t	*ipst;
4791 
4792 	if (ksp == NULL || ksp->ks_data == NULL)
4793 		return (EIO);
4794 
4795 	if (rw == KSTAT_WRITE)
4796 		return (EACCES);
4797 
4798 	kn = KSTAT_NAMED_PTR(ksp);
4799 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4800 
4801 	ns = netstack_find_by_stackid(stackid);
4802 	if (ns == NULL)
4803 		return (-1);
4804 
4805 	ipst = ns->netstack_ip;
4806 	if (ipst == NULL) {
4807 		netstack_rele(ns);
4808 		return (-1);
4809 	}
4810 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4811 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4812 	netstack_rele(ns);
4813 	return (0);
4814 }
4815 
4816 /*
4817  * Has ifindex been plumbed already.
4818  * Compares both phyint_ifindex and phyint_group_ifindex.
4819  */
4820 static boolean_t
4821 phyint_exists(uint_t index, ip_stack_t *ipst)
4822 {
4823 	phyint_t *phyi;
4824 
4825 	ASSERT(index != 0);
4826 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4827 	/*
4828 	 * Indexes are stored in the phyint - a common structure
4829 	 * to both IPv4 and IPv6.
4830 	 */
4831 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4832 	for (; phyi != NULL;
4833 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4834 	    phyi, AVL_AFTER)) {
4835 		if (phyi->phyint_ifindex == index ||
4836 		    phyi->phyint_group_ifindex == index)
4837 			return (B_TRUE);
4838 	}
4839 	return (B_FALSE);
4840 }
4841 
4842 /* Pick a unique ifindex */
4843 boolean_t
4844 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4845 {
4846 	uint_t starting_index;
4847 
4848 	if (!ipst->ips_ill_index_wrap) {
4849 		*indexp = ipst->ips_ill_index++;
4850 		if (ipst->ips_ill_index == 0) {
4851 			/* Reached the uint_t limit Next time wrap  */
4852 			ipst->ips_ill_index_wrap = B_TRUE;
4853 		}
4854 		return (B_TRUE);
4855 	}
4856 
4857 	/*
4858 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4859 	 * at this point and don't want to call any function that attempts
4860 	 * to get the lock again.
4861 	 */
4862 	starting_index = ipst->ips_ill_index++;
4863 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4864 		if (ipst->ips_ill_index != 0 &&
4865 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4866 			/* found unused index - use it */
4867 			*indexp = ipst->ips_ill_index;
4868 			return (B_TRUE);
4869 		}
4870 	}
4871 
4872 	/*
4873 	 * all interface indicies are inuse.
4874 	 */
4875 	return (B_FALSE);
4876 }
4877 
4878 /*
4879  * Assign a unique interface index for the phyint.
4880  */
4881 static boolean_t
4882 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4883 {
4884 	ASSERT(phyi->phyint_ifindex == 0);
4885 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4886 }
4887 
4888 /*
4889  * Return a pointer to the ill which matches the supplied name.  Note that
4890  * the ill name length includes the null termination character.  (May be
4891  * called as writer.)
4892  * If do_alloc and the interface is "lo0" it will be automatically created.
4893  * Cannot bump up reference on condemned ills. So dup detect can't be done
4894  * using this func.
4895  */
4896 ill_t *
4897 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4898     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4899     ip_stack_t *ipst)
4900 {
4901 	ill_t	*ill;
4902 	ipif_t	*ipif;
4903 	kstat_named_t	*kn;
4904 	boolean_t isloopback;
4905 	ipsq_t *old_ipsq;
4906 	in6_addr_t ov6addr;
4907 
4908 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4909 
4910 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4911 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4912 	rw_exit(&ipst->ips_ill_g_lock);
4913 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4914 		return (ill);
4915 
4916 	/*
4917 	 * Couldn't find it.  Does this happen to be a lookup for the
4918 	 * loopback device and are we allowed to allocate it?
4919 	 */
4920 	if (!isloopback || !do_alloc)
4921 		return (NULL);
4922 
4923 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4924 
4925 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4926 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4927 		rw_exit(&ipst->ips_ill_g_lock);
4928 		return (ill);
4929 	}
4930 
4931 	/* Create the loopback device on demand */
4932 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4933 	    sizeof (ipif_loopback_name), BPRI_MED));
4934 	if (ill == NULL)
4935 		goto done;
4936 
4937 	*ill = ill_null;
4938 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4939 	ill->ill_ipst = ipst;
4940 	netstack_hold(ipst->ips_netstack);
4941 	/*
4942 	 * For exclusive stacks we set the zoneid to zero
4943 	 * to make IP operate as if in the global zone.
4944 	 */
4945 	ill->ill_zoneid = GLOBAL_ZONEID;
4946 
4947 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4948 	if (ill->ill_phyint == NULL)
4949 		goto done;
4950 
4951 	if (isv6)
4952 		ill->ill_phyint->phyint_illv6 = ill;
4953 	else
4954 		ill->ill_phyint->phyint_illv4 = ill;
4955 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4956 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4957 	/* Add room for tcp+ip headers */
4958 	if (isv6) {
4959 		ill->ill_isv6 = B_TRUE;
4960 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4961 	} else {
4962 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4963 	}
4964 	if (!ill_allocate_mibs(ill))
4965 		goto done;
4966 	ill->ill_max_mtu = ill->ill_max_frag;
4967 	/*
4968 	 * ipif_loopback_name can't be pointed at directly because its used
4969 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4970 	 * from the glist, ill_glist_delete() sets the first character of
4971 	 * ill_name to '\0'.
4972 	 */
4973 	ill->ill_name = (char *)ill + sizeof (*ill);
4974 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4975 	ill->ill_name_length = sizeof (ipif_loopback_name);
4976 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4977 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4978 
4979 	ill->ill_global_timer = INFINITY;
4980 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4981 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4982 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4983 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4984 
4985 	/* No resolver here. */
4986 	ill->ill_net_type = IRE_LOOPBACK;
4987 
4988 	/* Initialize the ipsq */
4989 	if (!ipsq_init(ill))
4990 		goto done;
4991 
4992 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4993 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4994 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4995 #ifdef DEBUG
4996 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4997 #endif
4998 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4999 	if (ipif == NULL)
5000 		goto done;
5001 
5002 	ill->ill_flags = ILLF_MULTICAST;
5003 
5004 	ov6addr = ipif->ipif_v6lcl_addr;
5005 	/* Set up default loopback address and mask. */
5006 	if (!isv6) {
5007 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5008 
5009 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5010 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5011 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5012 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5013 		    ipif->ipif_v6subnet);
5014 		ill->ill_flags |= ILLF_IPV4;
5015 	} else {
5016 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5017 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5018 		ipif->ipif_v6net_mask = ipv6_all_ones;
5019 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5020 		    ipif->ipif_v6subnet);
5021 		ill->ill_flags |= ILLF_IPV6;
5022 	}
5023 
5024 	/*
5025 	 * Chain us in at the end of the ill list. hold the ill
5026 	 * before we make it globally visible. 1 for the lookup.
5027 	 */
5028 	ill->ill_refcnt = 0;
5029 	ill_refhold(ill);
5030 
5031 	ill->ill_frag_count = 0;
5032 	ill->ill_frag_free_num_pkts = 0;
5033 	ill->ill_last_frag_clean_time = 0;
5034 
5035 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5036 
5037 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5038 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5039 
5040 	/* Let SCTP know so that it can add this to its list */
5041 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5042 
5043 	/*
5044 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5045 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5046 	 * requires to be after ill_glist_insert() since we need the
5047 	 * ill_index set. Pass on ipv6_loopback as the old address.
5048 	 */
5049 	sctp_update_ipif_addr(ipif, ov6addr);
5050 
5051 	/*
5052 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5053 	 */
5054 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5055 		/* Loopback ills aren't in any IPMP group */
5056 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5057 		ipsq_delete(old_ipsq);
5058 	}
5059 
5060 	/*
5061 	 * Delay this till the ipif is allocated as ipif_allocate
5062 	 * de-references ill_phyint for getting the ifindex. We
5063 	 * can't do this before ipif_allocate because ill_phyint_reinit
5064 	 * -> phyint_assign_ifindex expects ipif to be present.
5065 	 */
5066 	mutex_enter(&ill->ill_phyint->phyint_lock);
5067 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5068 	mutex_exit(&ill->ill_phyint->phyint_lock);
5069 
5070 	if (ipst->ips_loopback_ksp == NULL) {
5071 		/* Export loopback interface statistics */
5072 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5073 		    ipif_loopback_name, "net",
5074 		    KSTAT_TYPE_NAMED, 2, 0,
5075 		    ipst->ips_netstack->netstack_stackid);
5076 		if (ipst->ips_loopback_ksp != NULL) {
5077 			ipst->ips_loopback_ksp->ks_update =
5078 			    loopback_kstat_update;
5079 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5080 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5081 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5082 			ipst->ips_loopback_ksp->ks_private =
5083 			    (void *)(uintptr_t)ipst->ips_netstack->
5084 			    netstack_stackid;
5085 			kstat_install(ipst->ips_loopback_ksp);
5086 		}
5087 	}
5088 
5089 	if (error != NULL)
5090 		*error = 0;
5091 	*did_alloc = B_TRUE;
5092 	rw_exit(&ipst->ips_ill_g_lock);
5093 	return (ill);
5094 done:
5095 	if (ill != NULL) {
5096 		if (ill->ill_phyint != NULL) {
5097 			ipsq_t	*ipsq;
5098 
5099 			ipsq = ill->ill_phyint->phyint_ipsq;
5100 			if (ipsq != NULL) {
5101 				ipsq->ipsq_ipst = NULL;
5102 				kmem_free(ipsq, sizeof (ipsq_t));
5103 			}
5104 			mi_free(ill->ill_phyint);
5105 		}
5106 		ill_free_mib(ill);
5107 		if (ill->ill_ipst != NULL)
5108 			netstack_rele(ill->ill_ipst->ips_netstack);
5109 		mi_free(ill);
5110 	}
5111 	rw_exit(&ipst->ips_ill_g_lock);
5112 	if (error != NULL)
5113 		*error = ENOMEM;
5114 	return (NULL);
5115 }
5116 
5117 /*
5118  * For IPP calls - use the ip_stack_t for global stack.
5119  */
5120 ill_t *
5121 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5122     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5123 {
5124 	ip_stack_t	*ipst;
5125 	ill_t		*ill;
5126 
5127 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5128 	if (ipst == NULL) {
5129 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5130 		return (NULL);
5131 	}
5132 
5133 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5134 	netstack_rele(ipst->ips_netstack);
5135 	return (ill);
5136 }
5137 
5138 /*
5139  * Return a pointer to the ill which matches the index and IP version type.
5140  */
5141 ill_t *
5142 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5143     ipsq_func_t func, int *err, ip_stack_t *ipst)
5144 {
5145 	ill_t	*ill;
5146 	ipsq_t  *ipsq;
5147 	phyint_t *phyi;
5148 
5149 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5150 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5151 
5152 	if (err != NULL)
5153 		*err = 0;
5154 
5155 	/*
5156 	 * Indexes are stored in the phyint - a common structure
5157 	 * to both IPv4 and IPv6.
5158 	 */
5159 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5160 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5161 	    (void *) &index, NULL);
5162 	if (phyi != NULL) {
5163 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5164 		if (ill != NULL) {
5165 			/*
5166 			 * The block comment at the start of ipif_down
5167 			 * explains the use of the macros used below
5168 			 */
5169 			GRAB_CONN_LOCK(q);
5170 			mutex_enter(&ill->ill_lock);
5171 			if (ILL_CAN_LOOKUP(ill)) {
5172 				ill_refhold_locked(ill);
5173 				mutex_exit(&ill->ill_lock);
5174 				RELEASE_CONN_LOCK(q);
5175 				rw_exit(&ipst->ips_ill_g_lock);
5176 				return (ill);
5177 			} else if (ILL_CAN_WAIT(ill, q)) {
5178 				ipsq = ill->ill_phyint->phyint_ipsq;
5179 				mutex_enter(&ipsq->ipsq_lock);
5180 				rw_exit(&ipst->ips_ill_g_lock);
5181 				mutex_exit(&ill->ill_lock);
5182 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5183 				mutex_exit(&ipsq->ipsq_lock);
5184 				RELEASE_CONN_LOCK(q);
5185 				if (err != NULL)
5186 					*err = EINPROGRESS;
5187 				return (NULL);
5188 			}
5189 			RELEASE_CONN_LOCK(q);
5190 			mutex_exit(&ill->ill_lock);
5191 		}
5192 	}
5193 	rw_exit(&ipst->ips_ill_g_lock);
5194 	if (err != NULL)
5195 		*err = ENXIO;
5196 	return (NULL);
5197 }
5198 
5199 /*
5200  * Return the ifindex next in sequence after the passed in ifindex.
5201  * If there is no next ifindex for the given protocol, return 0.
5202  */
5203 uint_t
5204 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5205 {
5206 	phyint_t *phyi;
5207 	phyint_t *phyi_initial;
5208 	uint_t   ifindex;
5209 
5210 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5211 
5212 	if (index == 0) {
5213 		phyi = avl_first(
5214 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5215 	} else {
5216 		phyi = phyi_initial = avl_find(
5217 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5218 		    (void *) &index, NULL);
5219 	}
5220 
5221 	for (; phyi != NULL;
5222 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5223 	    phyi, AVL_AFTER)) {
5224 		/*
5225 		 * If we're not returning the first interface in the tree
5226 		 * and we still haven't moved past the phyint_t that
5227 		 * corresponds to index, avl_walk needs to be called again
5228 		 */
5229 		if (!((index != 0) && (phyi == phyi_initial))) {
5230 			if (isv6) {
5231 				if ((phyi->phyint_illv6) &&
5232 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5233 				    (phyi->phyint_illv6->ill_isv6 == 1))
5234 					break;
5235 			} else {
5236 				if ((phyi->phyint_illv4) &&
5237 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5238 				    (phyi->phyint_illv4->ill_isv6 == 0))
5239 					break;
5240 			}
5241 		}
5242 	}
5243 
5244 	rw_exit(&ipst->ips_ill_g_lock);
5245 
5246 	if (phyi != NULL)
5247 		ifindex = phyi->phyint_ifindex;
5248 	else
5249 		ifindex = 0;
5250 
5251 	return (ifindex);
5252 }
5253 
5254 /*
5255  * Return the ifindex for the named interface.
5256  * If there is no next ifindex for the interface, return 0.
5257  */
5258 uint_t
5259 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5260 {
5261 	phyint_t	*phyi;
5262 	avl_index_t	where = 0;
5263 	uint_t		ifindex;
5264 
5265 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5266 
5267 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5268 	    name, &where)) == NULL) {
5269 		rw_exit(&ipst->ips_ill_g_lock);
5270 		return (0);
5271 	}
5272 
5273 	ifindex = phyi->phyint_ifindex;
5274 
5275 	rw_exit(&ipst->ips_ill_g_lock);
5276 
5277 	return (ifindex);
5278 }
5279 
5280 /*
5281  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5282  * that gives a running thread a reference to the ill. This reference must be
5283  * released by the thread when it is done accessing the ill and related
5284  * objects. ill_refcnt can not be used to account for static references
5285  * such as other structures pointing to an ill. Callers must generally
5286  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5287  * or be sure that the ill is not being deleted or changing state before
5288  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5289  * ill won't change any of its critical state such as address, netmask etc.
5290  */
5291 void
5292 ill_refhold(ill_t *ill)
5293 {
5294 	mutex_enter(&ill->ill_lock);
5295 	ill->ill_refcnt++;
5296 	ILL_TRACE_REF(ill);
5297 	mutex_exit(&ill->ill_lock);
5298 }
5299 
5300 void
5301 ill_refhold_locked(ill_t *ill)
5302 {
5303 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5304 	ill->ill_refcnt++;
5305 	ILL_TRACE_REF(ill);
5306 }
5307 
5308 int
5309 ill_check_and_refhold(ill_t *ill)
5310 {
5311 	mutex_enter(&ill->ill_lock);
5312 	if (ILL_CAN_LOOKUP(ill)) {
5313 		ill_refhold_locked(ill);
5314 		mutex_exit(&ill->ill_lock);
5315 		return (0);
5316 	}
5317 	mutex_exit(&ill->ill_lock);
5318 	return (ILL_LOOKUP_FAILED);
5319 }
5320 
5321 /*
5322  * Must not be called while holding any locks. Otherwise if this is
5323  * the last reference to be released, there is a chance of recursive mutex
5324  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5325  * to restart an ioctl.
5326  */
5327 void
5328 ill_refrele(ill_t *ill)
5329 {
5330 	mutex_enter(&ill->ill_lock);
5331 	ASSERT(ill->ill_refcnt != 0);
5332 	ill->ill_refcnt--;
5333 	ILL_UNTRACE_REF(ill);
5334 	if (ill->ill_refcnt != 0) {
5335 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5336 		mutex_exit(&ill->ill_lock);
5337 		return;
5338 	}
5339 
5340 	/* Drops the ill_lock */
5341 	ipif_ill_refrele_tail(ill);
5342 }
5343 
5344 /*
5345  * Obtain a weak reference count on the ill. This reference ensures the
5346  * ill won't be freed, but the ill may change any of its critical state
5347  * such as netmask, address etc. Returns an error if the ill has started
5348  * closing.
5349  */
5350 boolean_t
5351 ill_waiter_inc(ill_t *ill)
5352 {
5353 	mutex_enter(&ill->ill_lock);
5354 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5355 		mutex_exit(&ill->ill_lock);
5356 		return (B_FALSE);
5357 	}
5358 	ill->ill_waiters++;
5359 	mutex_exit(&ill->ill_lock);
5360 	return (B_TRUE);
5361 }
5362 
5363 void
5364 ill_waiter_dcr(ill_t *ill)
5365 {
5366 	mutex_enter(&ill->ill_lock);
5367 	ill->ill_waiters--;
5368 	if (ill->ill_waiters == 0)
5369 		cv_broadcast(&ill->ill_cv);
5370 	mutex_exit(&ill->ill_lock);
5371 }
5372 
5373 /*
5374  * Named Dispatch routine to produce a formatted report on all ILLs.
5375  * This report is accessed by using the ndd utility to "get" ND variable
5376  * "ip_ill_status".
5377  */
5378 /* ARGSUSED */
5379 int
5380 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5381 {
5382 	ill_t		*ill;
5383 	ill_walk_context_t ctx;
5384 	ip_stack_t	*ipst;
5385 
5386 	ipst = CONNQ_TO_IPST(q);
5387 
5388 	(void) mi_mpprintf(mp,
5389 	    "ILL      " MI_COL_HDRPAD_STR
5390 	/*   01234567[89ABCDEF] */
5391 	    "rq       " MI_COL_HDRPAD_STR
5392 	/*   01234567[89ABCDEF] */
5393 	    "wq       " MI_COL_HDRPAD_STR
5394 	/*   01234567[89ABCDEF] */
5395 	    "upcnt mxfrg err name");
5396 	/*   12345 12345 123 xxxxxxxx  */
5397 
5398 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5399 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5400 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5401 		(void) mi_mpprintf(mp,
5402 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5403 		    "%05u %05u %03d %s",
5404 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5405 		    ill->ill_ipif_up_count,
5406 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5407 	}
5408 	rw_exit(&ipst->ips_ill_g_lock);
5409 
5410 	return (0);
5411 }
5412 
5413 /*
5414  * Named Dispatch routine to produce a formatted report on all IPIFs.
5415  * This report is accessed by using the ndd utility to "get" ND variable
5416  * "ip_ipif_status".
5417  */
5418 /* ARGSUSED */
5419 int
5420 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5421 {
5422 	char	buf1[INET6_ADDRSTRLEN];
5423 	char	buf2[INET6_ADDRSTRLEN];
5424 	char	buf3[INET6_ADDRSTRLEN];
5425 	char	buf4[INET6_ADDRSTRLEN];
5426 	char	buf5[INET6_ADDRSTRLEN];
5427 	char	buf6[INET6_ADDRSTRLEN];
5428 	char	buf[LIFNAMSIZ];
5429 	ill_t	*ill;
5430 	ipif_t	*ipif;
5431 	nv_t	*nvp;
5432 	uint64_t flags;
5433 	zoneid_t zoneid;
5434 	ill_walk_context_t ctx;
5435 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5436 
5437 	(void) mi_mpprintf(mp,
5438 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5439 	    "\tlocal address\n"
5440 	    "\tsrc address\n"
5441 	    "\tsubnet\n"
5442 	    "\tmask\n"
5443 	    "\tbroadcast\n"
5444 	    "\tp-p-dst");
5445 
5446 	ASSERT(q->q_next == NULL);
5447 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5448 
5449 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5450 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5451 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5452 		for (ipif = ill->ill_ipif; ipif != NULL;
5453 		    ipif = ipif->ipif_next) {
5454 			if (zoneid != GLOBAL_ZONEID &&
5455 			    zoneid != ipif->ipif_zoneid &&
5456 			    ipif->ipif_zoneid != ALL_ZONES)
5457 				continue;
5458 
5459 			ipif_get_name(ipif, buf, sizeof (buf));
5460 			(void) mi_mpprintf(mp,
5461 			    MI_COL_PTRFMT_STR
5462 			    "%04u %05u %u/%u/%u %s %d",
5463 			    (void *)ipif,
5464 			    ipif->ipif_metric, ipif->ipif_mtu,
5465 			    ipif->ipif_ib_pkt_count,
5466 			    ipif->ipif_ob_pkt_count,
5467 			    ipif->ipif_fo_pkt_count,
5468 			    buf,
5469 			    ipif->ipif_zoneid);
5470 
5471 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5472 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5473 
5474 		/* Tack on text strings for any flags. */
5475 		nvp = ipif_nv_tbl;
5476 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5477 			if (nvp->nv_value & flags)
5478 				(void) mi_mpprintf_nr(mp, " %s",
5479 				    nvp->nv_name);
5480 		}
5481 		(void) mi_mpprintf(mp,
5482 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5483 		    inet_ntop(AF_INET6,
5484 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5485 		    inet_ntop(AF_INET6,
5486 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5487 		    inet_ntop(AF_INET6,
5488 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5489 		    inet_ntop(AF_INET6,
5490 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5491 		    inet_ntop(AF_INET6,
5492 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5493 		    inet_ntop(AF_INET6,
5494 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5495 		}
5496 	}
5497 	rw_exit(&ipst->ips_ill_g_lock);
5498 	return (0);
5499 }
5500 
5501 /*
5502  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5503  * driver.  We construct best guess defaults for lower level information that
5504  * we need.  If an interface is brought up without injection of any overriding
5505  * information from outside, we have to be ready to go with these defaults.
5506  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5507  * we primarely want the dl_provider_style.
5508  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5509  * at which point we assume the other part of the information is valid.
5510  */
5511 void
5512 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5513 {
5514 	uchar_t		*brdcst_addr;
5515 	uint_t		brdcst_addr_length, phys_addr_length;
5516 	t_scalar_t	sap_length;
5517 	dl_info_ack_t	*dlia;
5518 	ip_m_t		*ipm;
5519 	dl_qos_cl_sel1_t *sel1;
5520 
5521 	ASSERT(IAM_WRITER_ILL(ill));
5522 
5523 	/*
5524 	 * Till the ill is fully up ILL_CHANGING will be set and
5525 	 * the ill is not globally visible. So no need for a lock.
5526 	 */
5527 	dlia = (dl_info_ack_t *)mp->b_rptr;
5528 	ill->ill_mactype = dlia->dl_mac_type;
5529 
5530 	ipm = ip_m_lookup(dlia->dl_mac_type);
5531 	if (ipm == NULL) {
5532 		ipm = ip_m_lookup(DL_OTHER);
5533 		ASSERT(ipm != NULL);
5534 	}
5535 	ill->ill_media = ipm;
5536 
5537 	/*
5538 	 * When the new DLPI stuff is ready we'll pull lengths
5539 	 * from dlia.
5540 	 */
5541 	if (dlia->dl_version == DL_VERSION_2) {
5542 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5543 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5544 		    brdcst_addr_length);
5545 		if (brdcst_addr == NULL) {
5546 			brdcst_addr_length = 0;
5547 		}
5548 		sap_length = dlia->dl_sap_length;
5549 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5550 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5551 		    brdcst_addr_length, sap_length, phys_addr_length));
5552 	} else {
5553 		brdcst_addr_length = 6;
5554 		brdcst_addr = ip_six_byte_all_ones;
5555 		sap_length = -2;
5556 		phys_addr_length = brdcst_addr_length;
5557 	}
5558 
5559 	ill->ill_bcast_addr_length = brdcst_addr_length;
5560 	ill->ill_phys_addr_length = phys_addr_length;
5561 	ill->ill_sap_length = sap_length;
5562 	ill->ill_max_frag = dlia->dl_max_sdu;
5563 	ill->ill_max_mtu = ill->ill_max_frag;
5564 
5565 	ill->ill_type = ipm->ip_m_type;
5566 
5567 	if (!ill->ill_dlpi_style_set) {
5568 		if (dlia->dl_provider_style == DL_STYLE2)
5569 			ill->ill_needs_attach = 1;
5570 
5571 		/*
5572 		 * Allocate the first ipif on this ill. We don't delay it
5573 		 * further as ioctl handling assumes atleast one ipif to
5574 		 * be present.
5575 		 *
5576 		 * At this point we don't know whether the ill is v4 or v6.
5577 		 * We will know this whan the SIOCSLIFNAME happens and
5578 		 * the correct value for ill_isv6 will be assigned in
5579 		 * ipif_set_values(). We need to hold the ill lock and
5580 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5581 		 * the wakeup.
5582 		 */
5583 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5584 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5585 		mutex_enter(&ill->ill_lock);
5586 		ASSERT(ill->ill_dlpi_style_set == 0);
5587 		ill->ill_dlpi_style_set = 1;
5588 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5589 		cv_broadcast(&ill->ill_cv);
5590 		mutex_exit(&ill->ill_lock);
5591 		freemsg(mp);
5592 		return;
5593 	}
5594 	ASSERT(ill->ill_ipif != NULL);
5595 	/*
5596 	 * We know whether it is IPv4 or IPv6 now, as this is the
5597 	 * second DL_INFO_ACK we are recieving in response to the
5598 	 * DL_INFO_REQ sent in ipif_set_values.
5599 	 */
5600 	if (ill->ill_isv6)
5601 		ill->ill_sap = IP6_DL_SAP;
5602 	else
5603 		ill->ill_sap = IP_DL_SAP;
5604 	/*
5605 	 * Set ipif_mtu which is used to set the IRE's
5606 	 * ire_max_frag value. The driver could have sent
5607 	 * a different mtu from what it sent last time. No
5608 	 * need to call ipif_mtu_change because IREs have
5609 	 * not yet been created.
5610 	 */
5611 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5612 	/*
5613 	 * Clear all the flags that were set based on ill_bcast_addr_length
5614 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5615 	 * changed now and we need to re-evaluate.
5616 	 */
5617 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5618 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5619 
5620 	/*
5621 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5622 	 * changed now.
5623 	 */
5624 	if (ill->ill_bcast_addr_length == 0) {
5625 		if (ill->ill_resolver_mp != NULL)
5626 			freemsg(ill->ill_resolver_mp);
5627 		if (ill->ill_bcast_mp != NULL)
5628 			freemsg(ill->ill_bcast_mp);
5629 		if (ill->ill_flags & ILLF_XRESOLV)
5630 			ill->ill_net_type = IRE_IF_RESOLVER;
5631 		else
5632 			ill->ill_net_type = IRE_IF_NORESOLVER;
5633 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5634 		    ill->ill_phys_addr_length,
5635 		    ill->ill_sap,
5636 		    ill->ill_sap_length);
5637 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5638 
5639 		if (ill->ill_isv6)
5640 			/*
5641 			 * Note: xresolv interfaces will eventually need NOARP
5642 			 * set here as well, but that will require those
5643 			 * external resolvers to have some knowledge of
5644 			 * that flag and act appropriately. Not to be changed
5645 			 * at present.
5646 			 */
5647 			ill->ill_flags |= ILLF_NONUD;
5648 		else
5649 			ill->ill_flags |= ILLF_NOARP;
5650 
5651 		if (ill->ill_phys_addr_length == 0) {
5652 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5653 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5654 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5655 			} else {
5656 				/* pt-pt supports multicast. */
5657 				ill->ill_flags |= ILLF_MULTICAST;
5658 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5659 			}
5660 		}
5661 	} else {
5662 		ill->ill_net_type = IRE_IF_RESOLVER;
5663 		if (ill->ill_bcast_mp != NULL)
5664 			freemsg(ill->ill_bcast_mp);
5665 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5666 		    ill->ill_bcast_addr_length, ill->ill_sap,
5667 		    ill->ill_sap_length);
5668 		/*
5669 		 * Later detect lack of DLPI driver multicast
5670 		 * capability by catching DL_ENABMULTI errors in
5671 		 * ip_rput_dlpi.
5672 		 */
5673 		ill->ill_flags |= ILLF_MULTICAST;
5674 		if (!ill->ill_isv6)
5675 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5676 	}
5677 	/* By default an interface does not support any CoS marking */
5678 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5679 
5680 	/*
5681 	 * If we get QoS information in DL_INFO_ACK, the device supports
5682 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5683 	 */
5684 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5685 	    dlia->dl_qos_length);
5686 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5687 		ill->ill_flags |= ILLF_COS_ENABLED;
5688 	}
5689 
5690 	/* Clear any previous error indication. */
5691 	ill->ill_error = 0;
5692 	freemsg(mp);
5693 }
5694 
5695 /*
5696  * Perform various checks to verify that an address would make sense as a
5697  * local, remote, or subnet interface address.
5698  */
5699 static boolean_t
5700 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5701 {
5702 	ipaddr_t	net_mask;
5703 
5704 	/*
5705 	 * Don't allow all zeroes, or all ones, but allow
5706 	 * all ones netmask.
5707 	 */
5708 	if ((net_mask = ip_net_mask(addr)) == 0)
5709 		return (B_FALSE);
5710 	/* A given netmask overrides the "guess" netmask */
5711 	if (subnet_mask != 0)
5712 		net_mask = subnet_mask;
5713 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5714 	    (addr == (addr | ~net_mask)))) {
5715 		return (B_FALSE);
5716 	}
5717 
5718 	/*
5719 	 * Even if the netmask is all ones, we do not allow address to be
5720 	 * 255.255.255.255
5721 	 */
5722 	if (addr == INADDR_BROADCAST)
5723 		return (B_FALSE);
5724 
5725 	if (CLASSD(addr))
5726 		return (B_FALSE);
5727 
5728 	return (B_TRUE);
5729 }
5730 
5731 #define	V6_IPIF_LINKLOCAL(p)	\
5732 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5733 
5734 /*
5735  * Compare two given ipifs and check if the second one is better than
5736  * the first one using the order of preference (not taking deprecated
5737  * into acount) specified in ipif_lookup_multicast().
5738  */
5739 static boolean_t
5740 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5741 {
5742 	/* Check the least preferred first. */
5743 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5744 		/* If both ipifs are the same, use the first one. */
5745 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5746 			return (B_FALSE);
5747 		else
5748 			return (B_TRUE);
5749 	}
5750 
5751 	/* For IPv6, check for link local address. */
5752 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5753 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5754 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5755 			/* The second one is equal or less preferred. */
5756 			return (B_FALSE);
5757 		} else {
5758 			return (B_TRUE);
5759 		}
5760 	}
5761 
5762 	/* Then check for point to point interface. */
5763 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5764 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5765 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5766 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5767 			return (B_FALSE);
5768 		} else {
5769 			return (B_TRUE);
5770 		}
5771 	}
5772 
5773 	/* old_ipif is a normal interface, so no need to use the new one. */
5774 	return (B_FALSE);
5775 }
5776 
5777 /*
5778  * Find any non-virtual, not condemned, and up multicast capable interface
5779  * given an IP instance and zoneid.  Order of preference is:
5780  *
5781  * 1. normal
5782  * 1.1 normal, but deprecated
5783  * 2. point to point
5784  * 2.1 point to point, but deprecated
5785  * 3. link local
5786  * 3.1 link local, but deprecated
5787  * 4. loopback.
5788  */
5789 ipif_t *
5790 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5791 {
5792 	ill_t			*ill;
5793 	ill_walk_context_t	ctx;
5794 	ipif_t			*ipif;
5795 	ipif_t			*saved_ipif = NULL;
5796 	ipif_t			*dep_ipif = NULL;
5797 
5798 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5799 	if (isv6)
5800 		ill = ILL_START_WALK_V6(&ctx, ipst);
5801 	else
5802 		ill = ILL_START_WALK_V4(&ctx, ipst);
5803 
5804 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5805 		mutex_enter(&ill->ill_lock);
5806 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5807 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5808 			mutex_exit(&ill->ill_lock);
5809 			continue;
5810 		}
5811 		for (ipif = ill->ill_ipif; ipif != NULL;
5812 		    ipif = ipif->ipif_next) {
5813 			if (zoneid != ipif->ipif_zoneid &&
5814 			    zoneid != ALL_ZONES &&
5815 			    ipif->ipif_zoneid != ALL_ZONES) {
5816 				continue;
5817 			}
5818 			if (!(ipif->ipif_flags & IPIF_UP) ||
5819 			    !IPIF_CAN_LOOKUP(ipif)) {
5820 				continue;
5821 			}
5822 
5823 			/*
5824 			 * Found one candidate.  If it is deprecated,
5825 			 * remember it in dep_ipif.  If it is not deprecated,
5826 			 * remember it in saved_ipif.
5827 			 */
5828 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5829 				if (dep_ipif == NULL) {
5830 					dep_ipif = ipif;
5831 				} else if (ipif_comp_multi(dep_ipif, ipif,
5832 				    isv6)) {
5833 					/*
5834 					 * If the previous dep_ipif does not
5835 					 * belong to the same ill, we've done
5836 					 * a ipif_refhold() on it.  So we need
5837 					 * to release it.
5838 					 */
5839 					if (dep_ipif->ipif_ill != ill)
5840 						ipif_refrele(dep_ipif);
5841 					dep_ipif = ipif;
5842 				}
5843 				continue;
5844 			}
5845 			if (saved_ipif == NULL) {
5846 				saved_ipif = ipif;
5847 			} else {
5848 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5849 					if (saved_ipif->ipif_ill != ill)
5850 						ipif_refrele(saved_ipif);
5851 					saved_ipif = ipif;
5852 				}
5853 			}
5854 		}
5855 		/*
5856 		 * Before going to the next ill, do a ipif_refhold() on the
5857 		 * saved ones.
5858 		 */
5859 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5860 			ipif_refhold_locked(saved_ipif);
5861 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5862 			ipif_refhold_locked(dep_ipif);
5863 		mutex_exit(&ill->ill_lock);
5864 	}
5865 	rw_exit(&ipst->ips_ill_g_lock);
5866 
5867 	/*
5868 	 * If we have only the saved_ipif, return it.  But if we have both
5869 	 * saved_ipif and dep_ipif, check to see which one is better.
5870 	 */
5871 	if (saved_ipif != NULL) {
5872 		if (dep_ipif != NULL) {
5873 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5874 				ipif_refrele(saved_ipif);
5875 				return (dep_ipif);
5876 			} else {
5877 				ipif_refrele(dep_ipif);
5878 				return (saved_ipif);
5879 			}
5880 		}
5881 		return (saved_ipif);
5882 	} else {
5883 		return (dep_ipif);
5884 	}
5885 }
5886 
5887 /*
5888  * This function is called when an application does not specify an interface
5889  * to be used for multicast traffic (joining a group/sending data).  It
5890  * calls ire_lookup_multi() to look for an interface route for the
5891  * specified multicast group.  Doing this allows the administrator to add
5892  * prefix routes for multicast to indicate which interface to be used for
5893  * multicast traffic in the above scenario.  The route could be for all
5894  * multicast (224.0/4), for a single multicast group (a /32 route) or
5895  * anything in between.  If there is no such multicast route, we just find
5896  * any multicast capable interface and return it.  The returned ipif
5897  * is refhold'ed.
5898  */
5899 ipif_t *
5900 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5901 {
5902 	ire_t			*ire;
5903 	ipif_t			*ipif;
5904 
5905 	ire = ire_lookup_multi(group, zoneid, ipst);
5906 	if (ire != NULL) {
5907 		ipif = ire->ire_ipif;
5908 		ipif_refhold(ipif);
5909 		ire_refrele(ire);
5910 		return (ipif);
5911 	}
5912 
5913 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5914 }
5915 
5916 /*
5917  * Look for an ipif with the specified interface address and destination.
5918  * The destination address is used only for matching point-to-point interfaces.
5919  */
5920 ipif_t *
5921 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5922     ipsq_func_t func, int *error, ip_stack_t *ipst)
5923 {
5924 	ipif_t	*ipif;
5925 	ill_t	*ill;
5926 	ill_walk_context_t ctx;
5927 	ipsq_t	*ipsq;
5928 
5929 	if (error != NULL)
5930 		*error = 0;
5931 
5932 	/*
5933 	 * First match all the point-to-point interfaces
5934 	 * before looking at non-point-to-point interfaces.
5935 	 * This is done to avoid returning non-point-to-point
5936 	 * ipif instead of unnumbered point-to-point ipif.
5937 	 */
5938 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5939 	ill = ILL_START_WALK_V4(&ctx, ipst);
5940 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5941 		GRAB_CONN_LOCK(q);
5942 		mutex_enter(&ill->ill_lock);
5943 		for (ipif = ill->ill_ipif; ipif != NULL;
5944 		    ipif = ipif->ipif_next) {
5945 			/* Allow the ipif to be down */
5946 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5947 			    (ipif->ipif_lcl_addr == if_addr) &&
5948 			    (ipif->ipif_pp_dst_addr == dst)) {
5949 				/*
5950 				 * The block comment at the start of ipif_down
5951 				 * explains the use of the macros used below
5952 				 */
5953 				if (IPIF_CAN_LOOKUP(ipif)) {
5954 					ipif_refhold_locked(ipif);
5955 					mutex_exit(&ill->ill_lock);
5956 					RELEASE_CONN_LOCK(q);
5957 					rw_exit(&ipst->ips_ill_g_lock);
5958 					return (ipif);
5959 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5960 					ipsq = ill->ill_phyint->phyint_ipsq;
5961 					mutex_enter(&ipsq->ipsq_lock);
5962 					mutex_exit(&ill->ill_lock);
5963 					rw_exit(&ipst->ips_ill_g_lock);
5964 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5965 					    ill);
5966 					mutex_exit(&ipsq->ipsq_lock);
5967 					RELEASE_CONN_LOCK(q);
5968 					if (error != NULL)
5969 						*error = EINPROGRESS;
5970 					return (NULL);
5971 				}
5972 			}
5973 		}
5974 		mutex_exit(&ill->ill_lock);
5975 		RELEASE_CONN_LOCK(q);
5976 	}
5977 	rw_exit(&ipst->ips_ill_g_lock);
5978 
5979 	/* lookup the ipif based on interface address */
5980 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5981 	    ipst);
5982 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5983 	return (ipif);
5984 }
5985 
5986 /*
5987  * Look for an ipif with the specified address. For point-point links
5988  * we look for matches on either the destination address and the local
5989  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5990  * is set.
5991  * Matches on a specific ill if match_ill is set.
5992  */
5993 ipif_t *
5994 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5995     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5996 {
5997 	ipif_t  *ipif;
5998 	ill_t   *ill;
5999 	boolean_t ptp = B_FALSE;
6000 	ipsq_t	*ipsq;
6001 	ill_walk_context_t	ctx;
6002 
6003 	if (error != NULL)
6004 		*error = 0;
6005 
6006 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6007 	/*
6008 	 * Repeat twice, first based on local addresses and
6009 	 * next time for pointopoint.
6010 	 */
6011 repeat:
6012 	ill = ILL_START_WALK_V4(&ctx, ipst);
6013 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6014 		if (match_ill != NULL && ill != match_ill) {
6015 			continue;
6016 		}
6017 		GRAB_CONN_LOCK(q);
6018 		mutex_enter(&ill->ill_lock);
6019 		for (ipif = ill->ill_ipif; ipif != NULL;
6020 		    ipif = ipif->ipif_next) {
6021 			if (zoneid != ALL_ZONES &&
6022 			    zoneid != ipif->ipif_zoneid &&
6023 			    ipif->ipif_zoneid != ALL_ZONES)
6024 				continue;
6025 			/* Allow the ipif to be down */
6026 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6027 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6028 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6029 			    (ipif->ipif_pp_dst_addr == addr))) {
6030 				/*
6031 				 * The block comment at the start of ipif_down
6032 				 * explains the use of the macros used below
6033 				 */
6034 				if (IPIF_CAN_LOOKUP(ipif)) {
6035 					ipif_refhold_locked(ipif);
6036 					mutex_exit(&ill->ill_lock);
6037 					RELEASE_CONN_LOCK(q);
6038 					rw_exit(&ipst->ips_ill_g_lock);
6039 					return (ipif);
6040 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6041 					ipsq = ill->ill_phyint->phyint_ipsq;
6042 					mutex_enter(&ipsq->ipsq_lock);
6043 					mutex_exit(&ill->ill_lock);
6044 					rw_exit(&ipst->ips_ill_g_lock);
6045 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6046 					    ill);
6047 					mutex_exit(&ipsq->ipsq_lock);
6048 					RELEASE_CONN_LOCK(q);
6049 					if (error != NULL)
6050 						*error = EINPROGRESS;
6051 					return (NULL);
6052 				}
6053 			}
6054 		}
6055 		mutex_exit(&ill->ill_lock);
6056 		RELEASE_CONN_LOCK(q);
6057 	}
6058 
6059 	/* If we already did the ptp case, then we are done */
6060 	if (ptp) {
6061 		rw_exit(&ipst->ips_ill_g_lock);
6062 		if (error != NULL)
6063 			*error = ENXIO;
6064 		return (NULL);
6065 	}
6066 	ptp = B_TRUE;
6067 	goto repeat;
6068 }
6069 
6070 /*
6071  * Look for an ipif with the specified address. For point-point links
6072  * we look for matches on either the destination address and the local
6073  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6074  * is set.
6075  * Matches on a specific ill if match_ill is set.
6076  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6077  */
6078 zoneid_t
6079 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6080 {
6081 	zoneid_t zoneid;
6082 	ipif_t  *ipif;
6083 	ill_t   *ill;
6084 	boolean_t ptp = B_FALSE;
6085 	ill_walk_context_t	ctx;
6086 
6087 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6088 	/*
6089 	 * Repeat twice, first based on local addresses and
6090 	 * next time for pointopoint.
6091 	 */
6092 repeat:
6093 	ill = ILL_START_WALK_V4(&ctx, ipst);
6094 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6095 		if (match_ill != NULL && ill != match_ill) {
6096 			continue;
6097 		}
6098 		mutex_enter(&ill->ill_lock);
6099 		for (ipif = ill->ill_ipif; ipif != NULL;
6100 		    ipif = ipif->ipif_next) {
6101 			/* Allow the ipif to be down */
6102 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6103 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6104 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6105 			    (ipif->ipif_pp_dst_addr == addr)) &&
6106 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6107 				zoneid = ipif->ipif_zoneid;
6108 				mutex_exit(&ill->ill_lock);
6109 				rw_exit(&ipst->ips_ill_g_lock);
6110 				/*
6111 				 * If ipif_zoneid was ALL_ZONES then we have
6112 				 * a trusted extensions shared IP address.
6113 				 * In that case GLOBAL_ZONEID works to send.
6114 				 */
6115 				if (zoneid == ALL_ZONES)
6116 					zoneid = GLOBAL_ZONEID;
6117 				return (zoneid);
6118 			}
6119 		}
6120 		mutex_exit(&ill->ill_lock);
6121 	}
6122 
6123 	/* If we already did the ptp case, then we are done */
6124 	if (ptp) {
6125 		rw_exit(&ipst->ips_ill_g_lock);
6126 		return (ALL_ZONES);
6127 	}
6128 	ptp = B_TRUE;
6129 	goto repeat;
6130 }
6131 
6132 /*
6133  * Look for an ipif that matches the specified remote address i.e. the
6134  * ipif that would receive the specified packet.
6135  * First look for directly connected interfaces and then do a recursive
6136  * IRE lookup and pick the first ipif corresponding to the source address in the
6137  * ire.
6138  * Returns: held ipif
6139  */
6140 ipif_t *
6141 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6142 {
6143 	ipif_t	*ipif;
6144 	ire_t	*ire;
6145 	ip_stack_t	*ipst = ill->ill_ipst;
6146 
6147 	ASSERT(!ill->ill_isv6);
6148 
6149 	/*
6150 	 * Someone could be changing this ipif currently or change it
6151 	 * after we return this. Thus  a few packets could use the old
6152 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6153 	 * will atomically be updated or cleaned up with the new value
6154 	 * Thus we don't need a lock to check the flags or other attrs below.
6155 	 */
6156 	mutex_enter(&ill->ill_lock);
6157 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6158 		if (!IPIF_CAN_LOOKUP(ipif))
6159 			continue;
6160 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6161 		    ipif->ipif_zoneid != ALL_ZONES)
6162 			continue;
6163 		/* Allow the ipif to be down */
6164 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6165 			if ((ipif->ipif_pp_dst_addr == addr) ||
6166 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6167 			    ipif->ipif_lcl_addr == addr)) {
6168 				ipif_refhold_locked(ipif);
6169 				mutex_exit(&ill->ill_lock);
6170 				return (ipif);
6171 			}
6172 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6173 			ipif_refhold_locked(ipif);
6174 			mutex_exit(&ill->ill_lock);
6175 			return (ipif);
6176 		}
6177 	}
6178 	mutex_exit(&ill->ill_lock);
6179 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6180 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6181 	if (ire != NULL) {
6182 		/*
6183 		 * The callers of this function wants to know the
6184 		 * interface on which they have to send the replies
6185 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6186 		 * derived from different ills, we really don't care
6187 		 * what we return here.
6188 		 */
6189 		ipif = ire->ire_ipif;
6190 		if (ipif != NULL) {
6191 			ipif_refhold(ipif);
6192 			ire_refrele(ire);
6193 			return (ipif);
6194 		}
6195 		ire_refrele(ire);
6196 	}
6197 	/* Pick the first interface */
6198 	ipif = ipif_get_next_ipif(NULL, ill);
6199 	return (ipif);
6200 }
6201 
6202 /*
6203  * This func does not prevent refcnt from increasing. But if
6204  * the caller has taken steps to that effect, then this func
6205  * can be used to determine whether the ill has become quiescent
6206  */
6207 static boolean_t
6208 ill_is_quiescent(ill_t *ill)
6209 {
6210 	ipif_t	*ipif;
6211 
6212 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6213 
6214 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6215 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6216 			return (B_FALSE);
6217 		}
6218 	}
6219 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6220 		return (B_FALSE);
6221 	}
6222 	return (B_TRUE);
6223 }
6224 
6225 boolean_t
6226 ill_is_freeable(ill_t *ill)
6227 {
6228 	ipif_t	*ipif;
6229 
6230 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6231 
6232 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6233 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6234 			return (B_FALSE);
6235 		}
6236 	}
6237 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6238 		return (B_FALSE);
6239 	}
6240 	return (B_TRUE);
6241 }
6242 
6243 /*
6244  * This func does not prevent refcnt from increasing. But if
6245  * the caller has taken steps to that effect, then this func
6246  * can be used to determine whether the ipif has become quiescent
6247  */
6248 static boolean_t
6249 ipif_is_quiescent(ipif_t *ipif)
6250 {
6251 	ill_t *ill;
6252 
6253 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6254 
6255 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6256 		return (B_FALSE);
6257 	}
6258 
6259 	ill = ipif->ipif_ill;
6260 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6261 	    ill->ill_logical_down) {
6262 		return (B_TRUE);
6263 	}
6264 
6265 	/* This is the last ipif going down or being deleted on this ill */
6266 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6267 		return (B_FALSE);
6268 	}
6269 
6270 	return (B_TRUE);
6271 }
6272 
6273 /*
6274  * return true if the ipif can be destroyed: the ipif has to be quiescent
6275  * with zero references from ire/nce/ilm to it.
6276  */
6277 static boolean_t
6278 ipif_is_freeable(ipif_t *ipif)
6279 {
6280 
6281 	ill_t *ill;
6282 
6283 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6284 
6285 	if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6286 		return (B_FALSE);
6287 	}
6288 
6289 	ill = ipif->ipif_ill;
6290 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6291 	    ill->ill_logical_down) {
6292 		return (B_TRUE);
6293 	}
6294 
6295 	/* This is the last ipif going down or being deleted on this ill */
6296 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6297 		return (B_FALSE);
6298 	}
6299 
6300 	return (B_TRUE);
6301 }
6302 
6303 /*
6304  * This func does not prevent refcnt from increasing. But if
6305  * the caller has taken steps to that effect, then this func
6306  * can be used to determine whether the ipifs marked with IPIF_MOVING
6307  * have become quiescent and can be moved in a failover/failback.
6308  */
6309 static ipif_t *
6310 ill_quiescent_to_move(ill_t *ill)
6311 {
6312 	ipif_t  *ipif;
6313 
6314 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6315 
6316 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6317 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6318 			if (ipif->ipif_refcnt != 0 ||
6319 			    !IPIF_DOWN_OK(ipif)) {
6320 				return (ipif);
6321 			}
6322 		}
6323 	}
6324 	return (NULL);
6325 }
6326 
6327 /*
6328  * The ipif/ill/ire has been refreled. Do the tail processing.
6329  * Determine if the ipif or ill in question has become quiescent and if so
6330  * wakeup close and/or restart any queued pending ioctl that is waiting
6331  * for the ipif_down (or ill_down)
6332  */
6333 void
6334 ipif_ill_refrele_tail(ill_t *ill)
6335 {
6336 	mblk_t	*mp;
6337 	conn_t	*connp;
6338 	ipsq_t	*ipsq;
6339 	ipif_t	*ipif;
6340 	dl_notify_ind_t *dlindp;
6341 
6342 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6343 
6344 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6345 	    ill_is_freeable(ill)) {
6346 		/* ill_close may be waiting */
6347 		cv_broadcast(&ill->ill_cv);
6348 	}
6349 
6350 	/* ipsq can't change because ill_lock  is held */
6351 	ipsq = ill->ill_phyint->phyint_ipsq;
6352 	if (ipsq->ipsq_waitfor == 0) {
6353 		/* Not waiting for anything, just return. */
6354 		mutex_exit(&ill->ill_lock);
6355 		return;
6356 	}
6357 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6358 	    ipsq->ipsq_pending_ipif != NULL);
6359 	/*
6360 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6361 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6362 	 * be zero for restarting an ioctl that ends up downing the ill.
6363 	 */
6364 	ipif = ipsq->ipsq_pending_ipif;
6365 	if (ipif->ipif_ill != ill) {
6366 		/* The ioctl is pending on some other ill. */
6367 		mutex_exit(&ill->ill_lock);
6368 		return;
6369 	}
6370 
6371 	switch (ipsq->ipsq_waitfor) {
6372 	case IPIF_DOWN:
6373 		if (!ipif_is_quiescent(ipif)) {
6374 			mutex_exit(&ill->ill_lock);
6375 			return;
6376 		}
6377 		break;
6378 	case IPIF_FREE:
6379 		if (!ipif_is_freeable(ipif)) {
6380 			mutex_exit(&ill->ill_lock);
6381 			return;
6382 		}
6383 		break;
6384 
6385 	case ILL_DOWN:
6386 		if (!ill_is_quiescent(ill)) {
6387 			mutex_exit(&ill->ill_lock);
6388 			return;
6389 		}
6390 		break;
6391 	case ILL_FREE:
6392 		/*
6393 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6394 		 * waits synchronously in ip_close, and no message is queued in
6395 		 * ipsq_pending_mp at all in this case
6396 		 */
6397 		if (!ill_is_freeable(ill)) {
6398 			mutex_exit(&ill->ill_lock);
6399 			return;
6400 		}
6401 		break;
6402 
6403 	case ILL_MOVE_OK:
6404 		if (ill_quiescent_to_move(ill) != NULL) {
6405 			mutex_exit(&ill->ill_lock);
6406 			return;
6407 		}
6408 		break;
6409 	default:
6410 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6411 		    (void *)ipsq, ipsq->ipsq_waitfor);
6412 	}
6413 
6414 	/*
6415 	 * Incr refcnt for the qwriter_ip call below which
6416 	 * does a refrele
6417 	 */
6418 	ill_refhold_locked(ill);
6419 	mp = ipsq_pending_mp_get(ipsq, &connp);
6420 	mutex_exit(&ill->ill_lock);
6421 
6422 	ASSERT(mp != NULL);
6423 	/*
6424 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6425 	 * we can only get here when the current operation decides it
6426 	 * it needs to quiesce via ipsq_pending_mp_add().
6427 	 */
6428 	switch (mp->b_datap->db_type) {
6429 	case M_PCPROTO:
6430 	case M_PROTO:
6431 		/*
6432 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6433 		 */
6434 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6435 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6436 
6437 		switch (dlindp->dl_notification) {
6438 		case DL_NOTE_PHYS_ADDR:
6439 			qwriter_ip(ill, ill->ill_rq, mp,
6440 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6441 			return;
6442 		default:
6443 			ASSERT(0);
6444 		}
6445 		break;
6446 
6447 	case M_ERROR:
6448 	case M_HANGUP:
6449 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6450 		    B_TRUE);
6451 		return;
6452 
6453 	case M_IOCTL:
6454 	case M_IOCDATA:
6455 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6456 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6457 		return;
6458 
6459 	default:
6460 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6461 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6462 	}
6463 }
6464 
6465 #ifdef DEBUG
6466 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6467 static void
6468 th_trace_rrecord(th_trace_t *th_trace)
6469 {
6470 	tr_buf_t *tr_buf;
6471 	uint_t lastref;
6472 
6473 	lastref = th_trace->th_trace_lastref;
6474 	lastref++;
6475 	if (lastref == TR_BUF_MAX)
6476 		lastref = 0;
6477 	th_trace->th_trace_lastref = lastref;
6478 	tr_buf = &th_trace->th_trbuf[lastref];
6479 	tr_buf->tr_time = lbolt;
6480 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6481 }
6482 
6483 static void
6484 th_trace_free(void *value)
6485 {
6486 	th_trace_t *th_trace = value;
6487 
6488 	ASSERT(th_trace->th_refcnt == 0);
6489 	kmem_free(th_trace, sizeof (*th_trace));
6490 }
6491 
6492 /*
6493  * Find or create the per-thread hash table used to track object references.
6494  * The ipst argument is NULL if we shouldn't allocate.
6495  *
6496  * Accesses per-thread data, so there's no need to lock here.
6497  */
6498 static mod_hash_t *
6499 th_trace_gethash(ip_stack_t *ipst)
6500 {
6501 	th_hash_t *thh;
6502 
6503 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6504 		mod_hash_t *mh;
6505 		char name[256];
6506 		size_t objsize, rshift;
6507 		int retv;
6508 
6509 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6510 			return (NULL);
6511 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6512 		    (void *)curthread);
6513 
6514 		/*
6515 		 * We use mod_hash_create_extended here rather than the more
6516 		 * obvious mod_hash_create_ptrhash because the latter has a
6517 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6518 		 * block.
6519 		 */
6520 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6521 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6522 		rshift = highbit(objsize);
6523 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6524 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6525 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6526 		if (mh == NULL) {
6527 			kmem_free(thh, sizeof (*thh));
6528 			return (NULL);
6529 		}
6530 		thh->thh_hash = mh;
6531 		thh->thh_ipst = ipst;
6532 		/*
6533 		 * We trace ills, ipifs, ires, and nces.  All of these are
6534 		 * per-IP-stack, so the lock on the thread list is as well.
6535 		 */
6536 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6537 		list_insert_tail(&ip_thread_list, thh);
6538 		rw_exit(&ip_thread_rwlock);
6539 		retv = tsd_set(ip_thread_data, thh);
6540 		ASSERT(retv == 0);
6541 	}
6542 	return (thh != NULL ? thh->thh_hash : NULL);
6543 }
6544 
6545 boolean_t
6546 th_trace_ref(const void *obj, ip_stack_t *ipst)
6547 {
6548 	th_trace_t *th_trace;
6549 	mod_hash_t *mh;
6550 	mod_hash_val_t val;
6551 
6552 	if ((mh = th_trace_gethash(ipst)) == NULL)
6553 		return (B_FALSE);
6554 
6555 	/*
6556 	 * Attempt to locate the trace buffer for this obj and thread.
6557 	 * If it does not exist, then allocate a new trace buffer and
6558 	 * insert into the hash.
6559 	 */
6560 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6561 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6562 		if (th_trace == NULL)
6563 			return (B_FALSE);
6564 
6565 		th_trace->th_id = curthread;
6566 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6567 		    (mod_hash_val_t)th_trace) != 0) {
6568 			kmem_free(th_trace, sizeof (th_trace_t));
6569 			return (B_FALSE);
6570 		}
6571 	} else {
6572 		th_trace = (th_trace_t *)val;
6573 	}
6574 
6575 	ASSERT(th_trace->th_refcnt >= 0 &&
6576 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6577 
6578 	th_trace->th_refcnt++;
6579 	th_trace_rrecord(th_trace);
6580 	return (B_TRUE);
6581 }
6582 
6583 /*
6584  * For the purpose of tracing a reference release, we assume that global
6585  * tracing is always on and that the same thread initiated the reference hold
6586  * is releasing.
6587  */
6588 void
6589 th_trace_unref(const void *obj)
6590 {
6591 	int retv;
6592 	mod_hash_t *mh;
6593 	th_trace_t *th_trace;
6594 	mod_hash_val_t val;
6595 
6596 	mh = th_trace_gethash(NULL);
6597 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6598 	ASSERT(retv == 0);
6599 	th_trace = (th_trace_t *)val;
6600 
6601 	ASSERT(th_trace->th_refcnt > 0);
6602 	th_trace->th_refcnt--;
6603 	th_trace_rrecord(th_trace);
6604 }
6605 
6606 /*
6607  * If tracing has been disabled, then we assume that the reference counts are
6608  * now useless, and we clear them out before destroying the entries.
6609  */
6610 void
6611 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6612 {
6613 	th_hash_t	*thh;
6614 	mod_hash_t	*mh;
6615 	mod_hash_val_t	val;
6616 	th_trace_t	*th_trace;
6617 	int		retv;
6618 
6619 	rw_enter(&ip_thread_rwlock, RW_READER);
6620 	for (thh = list_head(&ip_thread_list); thh != NULL;
6621 	    thh = list_next(&ip_thread_list, thh)) {
6622 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6623 		    &val) == 0) {
6624 			th_trace = (th_trace_t *)val;
6625 			if (trace_disable)
6626 				th_trace->th_refcnt = 0;
6627 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6628 			ASSERT(retv == 0);
6629 		}
6630 	}
6631 	rw_exit(&ip_thread_rwlock);
6632 }
6633 
6634 void
6635 ipif_trace_ref(ipif_t *ipif)
6636 {
6637 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6638 
6639 	if (ipif->ipif_trace_disable)
6640 		return;
6641 
6642 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6643 		ipif->ipif_trace_disable = B_TRUE;
6644 		ipif_trace_cleanup(ipif);
6645 	}
6646 }
6647 
6648 void
6649 ipif_untrace_ref(ipif_t *ipif)
6650 {
6651 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6652 
6653 	if (!ipif->ipif_trace_disable)
6654 		th_trace_unref(ipif);
6655 }
6656 
6657 void
6658 ill_trace_ref(ill_t *ill)
6659 {
6660 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6661 
6662 	if (ill->ill_trace_disable)
6663 		return;
6664 
6665 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6666 		ill->ill_trace_disable = B_TRUE;
6667 		ill_trace_cleanup(ill);
6668 	}
6669 }
6670 
6671 void
6672 ill_untrace_ref(ill_t *ill)
6673 {
6674 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6675 
6676 	if (!ill->ill_trace_disable)
6677 		th_trace_unref(ill);
6678 }
6679 
6680 /*
6681  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6682  * failure, ipif_trace_disable is set.
6683  */
6684 static void
6685 ipif_trace_cleanup(const ipif_t *ipif)
6686 {
6687 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6688 }
6689 
6690 /*
6691  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6692  * failure, ill_trace_disable is set.
6693  */
6694 static void
6695 ill_trace_cleanup(const ill_t *ill)
6696 {
6697 	th_trace_cleanup(ill, ill->ill_trace_disable);
6698 }
6699 #endif /* DEBUG */
6700 
6701 void
6702 ipif_refhold_locked(ipif_t *ipif)
6703 {
6704 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6705 	ipif->ipif_refcnt++;
6706 	IPIF_TRACE_REF(ipif);
6707 }
6708 
6709 void
6710 ipif_refhold(ipif_t *ipif)
6711 {
6712 	ill_t	*ill;
6713 
6714 	ill = ipif->ipif_ill;
6715 	mutex_enter(&ill->ill_lock);
6716 	ipif->ipif_refcnt++;
6717 	IPIF_TRACE_REF(ipif);
6718 	mutex_exit(&ill->ill_lock);
6719 }
6720 
6721 /*
6722  * Must not be called while holding any locks. Otherwise if this is
6723  * the last reference to be released there is a chance of recursive mutex
6724  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6725  * to restart an ioctl.
6726  */
6727 void
6728 ipif_refrele(ipif_t *ipif)
6729 {
6730 	ill_t	*ill;
6731 
6732 	ill = ipif->ipif_ill;
6733 
6734 	mutex_enter(&ill->ill_lock);
6735 	ASSERT(ipif->ipif_refcnt != 0);
6736 	ipif->ipif_refcnt--;
6737 	IPIF_UNTRACE_REF(ipif);
6738 	if (ipif->ipif_refcnt != 0) {
6739 		mutex_exit(&ill->ill_lock);
6740 		return;
6741 	}
6742 
6743 	/* Drops the ill_lock */
6744 	ipif_ill_refrele_tail(ill);
6745 }
6746 
6747 ipif_t *
6748 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6749 {
6750 	ipif_t	*ipif;
6751 
6752 	mutex_enter(&ill->ill_lock);
6753 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6754 	    ipif != NULL; ipif = ipif->ipif_next) {
6755 		if (!IPIF_CAN_LOOKUP(ipif))
6756 			continue;
6757 		ipif_refhold_locked(ipif);
6758 		mutex_exit(&ill->ill_lock);
6759 		return (ipif);
6760 	}
6761 	mutex_exit(&ill->ill_lock);
6762 	return (NULL);
6763 }
6764 
6765 /*
6766  * TODO: make this table extendible at run time
6767  * Return a pointer to the mac type info for 'mac_type'
6768  */
6769 static ip_m_t *
6770 ip_m_lookup(t_uscalar_t mac_type)
6771 {
6772 	ip_m_t	*ipm;
6773 
6774 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6775 		if (ipm->ip_m_mac_type == mac_type)
6776 			return (ipm);
6777 	return (NULL);
6778 }
6779 
6780 /*
6781  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6782  * ipif_arg is passed in to associate it with the correct interface.
6783  * We may need to restart this operation if the ipif cannot be looked up
6784  * due to an exclusive operation that is currently in progress. The restart
6785  * entry point is specified by 'func'
6786  */
6787 int
6788 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6789     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6790     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6791     struct rtsa_s *sp, ip_stack_t *ipst)
6792 {
6793 	ire_t	*ire;
6794 	ire_t	*gw_ire = NULL;
6795 	ipif_t	*ipif = NULL;
6796 	boolean_t ipif_refheld = B_FALSE;
6797 	uint_t	type;
6798 	int	match_flags = MATCH_IRE_TYPE;
6799 	int	error;
6800 	tsol_gc_t *gc = NULL;
6801 	tsol_gcgrp_t *gcgrp = NULL;
6802 	boolean_t gcgrp_xtraref = B_FALSE;
6803 
6804 	ip1dbg(("ip_rt_add:"));
6805 
6806 	if (ire_arg != NULL)
6807 		*ire_arg = NULL;
6808 
6809 	/*
6810 	 * If this is the case of RTF_HOST being set, then we set the netmask
6811 	 * to all ones (regardless if one was supplied).
6812 	 */
6813 	if (flags & RTF_HOST)
6814 		mask = IP_HOST_MASK;
6815 
6816 	/*
6817 	 * Prevent routes with a zero gateway from being created (since
6818 	 * interfaces can currently be plumbed and brought up no assigned
6819 	 * address).
6820 	 */
6821 	if (gw_addr == 0)
6822 		return (ENETUNREACH);
6823 	/*
6824 	 * Get the ipif, if any, corresponding to the gw_addr
6825 	 */
6826 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6827 	    ipst);
6828 	if (ipif != NULL) {
6829 		if (IS_VNI(ipif->ipif_ill)) {
6830 			ipif_refrele(ipif);
6831 			return (EINVAL);
6832 		}
6833 		ipif_refheld = B_TRUE;
6834 	} else if (error == EINPROGRESS) {
6835 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6836 		return (EINPROGRESS);
6837 	} else {
6838 		error = 0;
6839 	}
6840 
6841 	if (ipif != NULL) {
6842 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6843 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6844 	} else {
6845 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6846 	}
6847 
6848 	/*
6849 	 * GateD will attempt to create routes with a loopback interface
6850 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6851 	 * these routes to be added, but create them as interface routes
6852 	 * since the gateway is an interface address.
6853 	 */
6854 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6855 		flags &= ~RTF_GATEWAY;
6856 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6857 		    mask == IP_HOST_MASK) {
6858 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6859 			    ALL_ZONES, NULL, match_flags, ipst);
6860 			if (ire != NULL) {
6861 				ire_refrele(ire);
6862 				if (ipif_refheld)
6863 					ipif_refrele(ipif);
6864 				return (EEXIST);
6865 			}
6866 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6867 			    "for 0x%x\n", (void *)ipif,
6868 			    ipif->ipif_ire_type,
6869 			    ntohl(ipif->ipif_lcl_addr)));
6870 			ire = ire_create(
6871 			    (uchar_t *)&dst_addr,	/* dest address */
6872 			    (uchar_t *)&mask,		/* mask */
6873 			    (uchar_t *)&ipif->ipif_src_addr,
6874 			    NULL,			/* no gateway */
6875 			    &ipif->ipif_mtu,
6876 			    NULL,
6877 			    ipif->ipif_rq,		/* recv-from queue */
6878 			    NULL,			/* no send-to queue */
6879 			    ipif->ipif_ire_type,	/* LOOPBACK */
6880 			    ipif,
6881 			    0,
6882 			    0,
6883 			    0,
6884 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6885 			    RTF_PRIVATE : 0,
6886 			    &ire_uinfo_null,
6887 			    NULL,
6888 			    NULL,
6889 			    ipst);
6890 
6891 			if (ire == NULL) {
6892 				if (ipif_refheld)
6893 					ipif_refrele(ipif);
6894 				return (ENOMEM);
6895 			}
6896 			error = ire_add(&ire, q, mp, func, B_FALSE);
6897 			if (error == 0)
6898 				goto save_ire;
6899 			if (ipif_refheld)
6900 				ipif_refrele(ipif);
6901 			return (error);
6902 
6903 		}
6904 	}
6905 
6906 	/*
6907 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6908 	 * and the gateway address provided is one of the system's interface
6909 	 * addresses.  By using the routing socket interface and supplying an
6910 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6911 	 * specifying an interface route to be created is available which uses
6912 	 * the interface index that specifies the outgoing interface rather than
6913 	 * the address of an outgoing interface (which may not be able to
6914 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6915 	 * flag, routes can be specified which not only specify the next-hop to
6916 	 * be used when routing to a certain prefix, but also which outgoing
6917 	 * interface should be used.
6918 	 *
6919 	 * Previously, interfaces would have unique addresses assigned to them
6920 	 * and so the address assigned to a particular interface could be used
6921 	 * to identify a particular interface.  One exception to this was the
6922 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6923 	 *
6924 	 * With the advent of IPv6 and its link-local addresses, this
6925 	 * restriction was relaxed and interfaces could share addresses between
6926 	 * themselves.  In fact, typically all of the link-local interfaces on
6927 	 * an IPv6 node or router will have the same link-local address.  In
6928 	 * order to differentiate between these interfaces, the use of an
6929 	 * interface index is necessary and this index can be carried inside a
6930 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6931 	 * of using the interface index, however, is that all of the ipif's that
6932 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6933 	 * cannot be used to differentiate between ipif's (or logical
6934 	 * interfaces) that belong to the same ill (physical interface).
6935 	 *
6936 	 * For example, in the following case involving IPv4 interfaces and
6937 	 * logical interfaces
6938 	 *
6939 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6940 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6941 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6942 	 *
6943 	 * the ipif's corresponding to each of these interface routes can be
6944 	 * uniquely identified by the "gateway" (actually interface address).
6945 	 *
6946 	 * In this case involving multiple IPv6 default routes to a particular
6947 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6948 	 * default route is of interest:
6949 	 *
6950 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6951 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6952 	 */
6953 
6954 	/* RTF_GATEWAY not set */
6955 	if (!(flags & RTF_GATEWAY)) {
6956 		queue_t	*stq;
6957 
6958 		if (sp != NULL) {
6959 			ip2dbg(("ip_rt_add: gateway security attributes "
6960 			    "cannot be set with interface route\n"));
6961 			if (ipif_refheld)
6962 				ipif_refrele(ipif);
6963 			return (EINVAL);
6964 		}
6965 
6966 		/*
6967 		 * As the interface index specified with the RTA_IFP sockaddr is
6968 		 * the same for all ipif's off of an ill, the matching logic
6969 		 * below uses MATCH_IRE_ILL if such an index was specified.
6970 		 * This means that routes sharing the same prefix when added
6971 		 * using a RTA_IFP sockaddr must have distinct interface
6972 		 * indices (namely, they must be on distinct ill's).
6973 		 *
6974 		 * On the other hand, since the gateway address will usually be
6975 		 * different for each ipif on the system, the matching logic
6976 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6977 		 * route.  This means that interface routes for the same prefix
6978 		 * can be created if they belong to distinct ipif's and if a
6979 		 * RTA_IFP sockaddr is not present.
6980 		 */
6981 		if (ipif_arg != NULL) {
6982 			if (ipif_refheld)  {
6983 				ipif_refrele(ipif);
6984 				ipif_refheld = B_FALSE;
6985 			}
6986 			ipif = ipif_arg;
6987 			match_flags |= MATCH_IRE_ILL;
6988 		} else {
6989 			/*
6990 			 * Check the ipif corresponding to the gw_addr
6991 			 */
6992 			if (ipif == NULL)
6993 				return (ENETUNREACH);
6994 			match_flags |= MATCH_IRE_IPIF;
6995 		}
6996 		ASSERT(ipif != NULL);
6997 
6998 		/*
6999 		 * We check for an existing entry at this point.
7000 		 *
7001 		 * Since a netmask isn't passed in via the ioctl interface
7002 		 * (SIOCADDRT), we don't check for a matching netmask in that
7003 		 * case.
7004 		 */
7005 		if (!ioctl_msg)
7006 			match_flags |= MATCH_IRE_MASK;
7007 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7008 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7009 		if (ire != NULL) {
7010 			ire_refrele(ire);
7011 			if (ipif_refheld)
7012 				ipif_refrele(ipif);
7013 			return (EEXIST);
7014 		}
7015 
7016 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7017 		    ? ipif->ipif_rq : ipif->ipif_wq;
7018 
7019 		/*
7020 		 * Create a copy of the IRE_LOOPBACK,
7021 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7022 		 * the modified address and netmask.
7023 		 */
7024 		ire = ire_create(
7025 		    (uchar_t *)&dst_addr,
7026 		    (uint8_t *)&mask,
7027 		    (uint8_t *)&ipif->ipif_src_addr,
7028 		    NULL,
7029 		    &ipif->ipif_mtu,
7030 		    NULL,
7031 		    NULL,
7032 		    stq,
7033 		    ipif->ipif_net_type,
7034 		    ipif,
7035 		    0,
7036 		    0,
7037 		    0,
7038 		    flags,
7039 		    &ire_uinfo_null,
7040 		    NULL,
7041 		    NULL,
7042 		    ipst);
7043 		if (ire == NULL) {
7044 			if (ipif_refheld)
7045 				ipif_refrele(ipif);
7046 			return (ENOMEM);
7047 		}
7048 
7049 		/*
7050 		 * Some software (for example, GateD and Sun Cluster) attempts
7051 		 * to create (what amount to) IRE_PREFIX routes with the
7052 		 * loopback address as the gateway.  This is primarily done to
7053 		 * set up prefixes with the RTF_REJECT flag set (for example,
7054 		 * when generating aggregate routes.)
7055 		 *
7056 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7057 		 * IRE_LOOPBACK, then we map the request into a
7058 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7059 		 * these interface routes, by definition, can only be that.
7060 		 *
7061 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7062 		 * routine, but rather using ire_create() directly.
7063 		 *
7064 		 */
7065 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7066 			ire->ire_type = IRE_IF_NORESOLVER;
7067 			ire->ire_flags |= RTF_BLACKHOLE;
7068 		}
7069 
7070 		error = ire_add(&ire, q, mp, func, B_FALSE);
7071 		if (error == 0)
7072 			goto save_ire;
7073 
7074 		/*
7075 		 * In the result of failure, ire_add() will have already
7076 		 * deleted the ire in question, so there is no need to
7077 		 * do that here.
7078 		 */
7079 		if (ipif_refheld)
7080 			ipif_refrele(ipif);
7081 		return (error);
7082 	}
7083 	if (ipif_refheld) {
7084 		ipif_refrele(ipif);
7085 		ipif_refheld = B_FALSE;
7086 	}
7087 
7088 	/*
7089 	 * Get an interface IRE for the specified gateway.
7090 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7091 	 * gateway, it is currently unreachable and we fail the request
7092 	 * accordingly.
7093 	 */
7094 	ipif = ipif_arg;
7095 	if (ipif_arg != NULL)
7096 		match_flags |= MATCH_IRE_ILL;
7097 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7098 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7099 	if (gw_ire == NULL)
7100 		return (ENETUNREACH);
7101 
7102 	/*
7103 	 * We create one of three types of IREs as a result of this request
7104 	 * based on the netmask.  A netmask of all ones (which is automatically
7105 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7106 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7107 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7108 	 * destination prefix.
7109 	 */
7110 	if (mask == IP_HOST_MASK)
7111 		type = IRE_HOST;
7112 	else if (mask == 0)
7113 		type = IRE_DEFAULT;
7114 	else
7115 		type = IRE_PREFIX;
7116 
7117 	/* check for a duplicate entry */
7118 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7119 	    NULL, ALL_ZONES, 0, NULL,
7120 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7121 	if (ire != NULL) {
7122 		ire_refrele(gw_ire);
7123 		ire_refrele(ire);
7124 		return (EEXIST);
7125 	}
7126 
7127 	/* Security attribute exists */
7128 	if (sp != NULL) {
7129 		tsol_gcgrp_addr_t ga;
7130 
7131 		/* find or create the gateway credentials group */
7132 		ga.ga_af = AF_INET;
7133 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7134 
7135 		/* we hold reference to it upon success */
7136 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7137 		if (gcgrp == NULL) {
7138 			ire_refrele(gw_ire);
7139 			return (ENOMEM);
7140 		}
7141 
7142 		/*
7143 		 * Create and add the security attribute to the group; a
7144 		 * reference to the group is made upon allocating a new
7145 		 * entry successfully.  If it finds an already-existing
7146 		 * entry for the security attribute in the group, it simply
7147 		 * returns it and no new reference is made to the group.
7148 		 */
7149 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7150 		if (gc == NULL) {
7151 			/* release reference held by gcgrp_lookup */
7152 			GCGRP_REFRELE(gcgrp);
7153 			ire_refrele(gw_ire);
7154 			return (ENOMEM);
7155 		}
7156 	}
7157 
7158 	/* Create the IRE. */
7159 	ire = ire_create(
7160 	    (uchar_t *)&dst_addr,		/* dest address */
7161 	    (uchar_t *)&mask,			/* mask */
7162 	    /* src address assigned by the caller? */
7163 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7164 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7165 	    (uchar_t *)&gw_addr,		/* gateway address */
7166 	    &gw_ire->ire_max_frag,
7167 	    NULL,				/* no src nce */
7168 	    NULL,				/* no recv-from queue */
7169 	    NULL,				/* no send-to queue */
7170 	    (ushort_t)type,			/* IRE type */
7171 	    ipif_arg,
7172 	    0,
7173 	    0,
7174 	    0,
7175 	    flags,
7176 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7177 	    gc,					/* security attribute */
7178 	    NULL,
7179 	    ipst);
7180 
7181 	/*
7182 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7183 	 * reference to the 'gcgrp'. We can now release the extra reference
7184 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7185 	 */
7186 	if (gcgrp_xtraref)
7187 		GCGRP_REFRELE(gcgrp);
7188 	if (ire == NULL) {
7189 		if (gc != NULL)
7190 			GC_REFRELE(gc);
7191 		ire_refrele(gw_ire);
7192 		return (ENOMEM);
7193 	}
7194 
7195 	/*
7196 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7197 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7198 	 */
7199 
7200 	/* Add the new IRE. */
7201 	error = ire_add(&ire, q, mp, func, B_FALSE);
7202 	if (error != 0) {
7203 		/*
7204 		 * In the result of failure, ire_add() will have already
7205 		 * deleted the ire in question, so there is no need to
7206 		 * do that here.
7207 		 */
7208 		ire_refrele(gw_ire);
7209 		return (error);
7210 	}
7211 
7212 	if (flags & RTF_MULTIRT) {
7213 		/*
7214 		 * Invoke the CGTP (multirouting) filtering module
7215 		 * to add the dst address in the filtering database.
7216 		 * Replicated inbound packets coming from that address
7217 		 * will be filtered to discard the duplicates.
7218 		 * It is not necessary to call the CGTP filter hook
7219 		 * when the dst address is a broadcast or multicast,
7220 		 * because an IP source address cannot be a broadcast
7221 		 * or a multicast.
7222 		 */
7223 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7224 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7225 		if (ire_dst != NULL) {
7226 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7227 			ire_refrele(ire_dst);
7228 			goto save_ire;
7229 		}
7230 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7231 		    !CLASSD(ire->ire_addr)) {
7232 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7233 			    ipst->ips_netstack->netstack_stackid,
7234 			    ire->ire_addr,
7235 			    ire->ire_gateway_addr,
7236 			    ire->ire_src_addr,
7237 			    gw_ire->ire_src_addr);
7238 			if (res != 0) {
7239 				ire_refrele(gw_ire);
7240 				ire_delete(ire);
7241 				return (res);
7242 			}
7243 		}
7244 	}
7245 
7246 	/*
7247 	 * Now that the prefix IRE entry has been created, delete any
7248 	 * existing gateway IRE cache entries as well as any IRE caches
7249 	 * using the gateway, and force them to be created through
7250 	 * ip_newroute.
7251 	 */
7252 	if (gc != NULL) {
7253 		ASSERT(gcgrp != NULL);
7254 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7255 	}
7256 
7257 save_ire:
7258 	if (gw_ire != NULL) {
7259 		ire_refrele(gw_ire);
7260 	}
7261 	if (ipif != NULL) {
7262 		/*
7263 		 * Save enough information so that we can recreate the IRE if
7264 		 * the interface goes down and then up.  The metrics associated
7265 		 * with the route will be saved as well when rts_setmetrics() is
7266 		 * called after the IRE has been created.  In the case where
7267 		 * memory cannot be allocated, none of this information will be
7268 		 * saved.
7269 		 */
7270 		ipif_save_ire(ipif, ire);
7271 	}
7272 	if (ioctl_msg)
7273 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7274 	if (ire_arg != NULL) {
7275 		/*
7276 		 * Store the ire that was successfully added into where ire_arg
7277 		 * points to so that callers don't have to look it up
7278 		 * themselves (but they are responsible for ire_refrele()ing
7279 		 * the ire when they are finished with it).
7280 		 */
7281 		*ire_arg = ire;
7282 	} else {
7283 		ire_refrele(ire);		/* Held in ire_add */
7284 	}
7285 	if (ipif_refheld)
7286 		ipif_refrele(ipif);
7287 	return (0);
7288 }
7289 
7290 /*
7291  * ip_rt_delete is called to delete an IPv4 route.
7292  * ipif_arg is passed in to associate it with the correct interface.
7293  * We may need to restart this operation if the ipif cannot be looked up
7294  * due to an exclusive operation that is currently in progress. The restart
7295  * entry point is specified by 'func'
7296  */
7297 /* ARGSUSED4 */
7298 int
7299 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7300     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7301     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7302 {
7303 	ire_t	*ire = NULL;
7304 	ipif_t	*ipif;
7305 	boolean_t ipif_refheld = B_FALSE;
7306 	uint_t	type;
7307 	uint_t	match_flags = MATCH_IRE_TYPE;
7308 	int	err = 0;
7309 
7310 	ip1dbg(("ip_rt_delete:"));
7311 	/*
7312 	 * If this is the case of RTF_HOST being set, then we set the netmask
7313 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7314 	 */
7315 	if (flags & RTF_HOST) {
7316 		mask = IP_HOST_MASK;
7317 		match_flags |= MATCH_IRE_MASK;
7318 	} else if (rtm_addrs & RTA_NETMASK) {
7319 		match_flags |= MATCH_IRE_MASK;
7320 	}
7321 
7322 	/*
7323 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7324 	 * we check if the gateway address is one of our interfaces first,
7325 	 * and fall back on RTF_GATEWAY routes.
7326 	 *
7327 	 * This makes it possible to delete an original
7328 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7329 	 *
7330 	 * As the interface index specified with the RTA_IFP sockaddr is the
7331 	 * same for all ipif's off of an ill, the matching logic below uses
7332 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7333 	 * sharing the same prefix and interface index as the the route
7334 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7335 	 * is specified in the request.
7336 	 *
7337 	 * On the other hand, since the gateway address will usually be
7338 	 * different for each ipif on the system, the matching logic
7339 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7340 	 * route.  This means that interface routes for the same prefix can be
7341 	 * uniquely identified if they belong to distinct ipif's and if a
7342 	 * RTA_IFP sockaddr is not present.
7343 	 *
7344 	 * For more detail on specifying routes by gateway address and by
7345 	 * interface index, see the comments in ip_rt_add().
7346 	 */
7347 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7348 	    ipst);
7349 	if (ipif != NULL)
7350 		ipif_refheld = B_TRUE;
7351 	else if (err == EINPROGRESS)
7352 		return (err);
7353 	else
7354 		err = 0;
7355 	if (ipif != NULL) {
7356 		if (ipif_arg != NULL) {
7357 			if (ipif_refheld) {
7358 				ipif_refrele(ipif);
7359 				ipif_refheld = B_FALSE;
7360 			}
7361 			ipif = ipif_arg;
7362 			match_flags |= MATCH_IRE_ILL;
7363 		} else {
7364 			match_flags |= MATCH_IRE_IPIF;
7365 		}
7366 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7367 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7368 			    ALL_ZONES, NULL, match_flags, ipst);
7369 		}
7370 		if (ire == NULL) {
7371 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7372 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7373 			    match_flags, ipst);
7374 		}
7375 	}
7376 
7377 	if (ire == NULL) {
7378 		/*
7379 		 * At this point, the gateway address is not one of our own
7380 		 * addresses or a matching interface route was not found.  We
7381 		 * set the IRE type to lookup based on whether
7382 		 * this is a host route, a default route or just a prefix.
7383 		 *
7384 		 * If an ipif_arg was passed in, then the lookup is based on an
7385 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7386 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7387 		 * set as the route being looked up is not a traditional
7388 		 * interface route.
7389 		 */
7390 		match_flags &= ~MATCH_IRE_IPIF;
7391 		match_flags |= MATCH_IRE_GW;
7392 		if (ipif_arg != NULL)
7393 			match_flags |= MATCH_IRE_ILL;
7394 		if (mask == IP_HOST_MASK)
7395 			type = IRE_HOST;
7396 		else if (mask == 0)
7397 			type = IRE_DEFAULT;
7398 		else
7399 			type = IRE_PREFIX;
7400 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7401 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7402 	}
7403 
7404 	if (ipif_refheld)
7405 		ipif_refrele(ipif);
7406 
7407 	/* ipif is not refheld anymore */
7408 	if (ire == NULL)
7409 		return (ESRCH);
7410 
7411 	if (ire->ire_flags & RTF_MULTIRT) {
7412 		/*
7413 		 * Invoke the CGTP (multirouting) filtering module
7414 		 * to remove the dst address from the filtering database.
7415 		 * Packets coming from that address will no longer be
7416 		 * filtered to remove duplicates.
7417 		 */
7418 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7419 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7420 			    ipst->ips_netstack->netstack_stackid,
7421 			    ire->ire_addr, ire->ire_gateway_addr);
7422 		}
7423 		ip_cgtp_bcast_delete(ire, ipst);
7424 	}
7425 
7426 	ipif = ire->ire_ipif;
7427 	if (ipif != NULL)
7428 		ipif_remove_ire(ipif, ire);
7429 	if (ioctl_msg)
7430 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7431 	ire_delete(ire);
7432 	ire_refrele(ire);
7433 	return (err);
7434 }
7435 
7436 /*
7437  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7438  */
7439 /* ARGSUSED */
7440 int
7441 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7442     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7443 {
7444 	ipaddr_t dst_addr;
7445 	ipaddr_t gw_addr;
7446 	ipaddr_t mask;
7447 	int error = 0;
7448 	mblk_t *mp1;
7449 	struct rtentry *rt;
7450 	ipif_t *ipif = NULL;
7451 	ip_stack_t	*ipst;
7452 
7453 	ASSERT(q->q_next == NULL);
7454 	ipst = CONNQ_TO_IPST(q);
7455 
7456 	ip1dbg(("ip_siocaddrt:"));
7457 	/* Existence of mp1 verified in ip_wput_nondata */
7458 	mp1 = mp->b_cont->b_cont;
7459 	rt = (struct rtentry *)mp1->b_rptr;
7460 
7461 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7462 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7463 
7464 	/*
7465 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7466 	 * to a particular host address.  In this case, we set the netmask to
7467 	 * all ones for the particular destination address.  Otherwise,
7468 	 * determine the netmask to be used based on dst_addr and the interfaces
7469 	 * in use.
7470 	 */
7471 	if (rt->rt_flags & RTF_HOST) {
7472 		mask = IP_HOST_MASK;
7473 	} else {
7474 		/*
7475 		 * Note that ip_subnet_mask returns a zero mask in the case of
7476 		 * default (an all-zeroes address).
7477 		 */
7478 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7479 	}
7480 
7481 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7482 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7483 	if (ipif != NULL)
7484 		ipif_refrele(ipif);
7485 	return (error);
7486 }
7487 
7488 /*
7489  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7490  */
7491 /* ARGSUSED */
7492 int
7493 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7494     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7495 {
7496 	ipaddr_t dst_addr;
7497 	ipaddr_t gw_addr;
7498 	ipaddr_t mask;
7499 	int error;
7500 	mblk_t *mp1;
7501 	struct rtentry *rt;
7502 	ipif_t *ipif = NULL;
7503 	ip_stack_t	*ipst;
7504 
7505 	ASSERT(q->q_next == NULL);
7506 	ipst = CONNQ_TO_IPST(q);
7507 
7508 	ip1dbg(("ip_siocdelrt:"));
7509 	/* Existence of mp1 verified in ip_wput_nondata */
7510 	mp1 = mp->b_cont->b_cont;
7511 	rt = (struct rtentry *)mp1->b_rptr;
7512 
7513 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7514 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7515 
7516 	/*
7517 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7518 	 * to a particular host address.  In this case, we set the netmask to
7519 	 * all ones for the particular destination address.  Otherwise,
7520 	 * determine the netmask to be used based on dst_addr and the interfaces
7521 	 * in use.
7522 	 */
7523 	if (rt->rt_flags & RTF_HOST) {
7524 		mask = IP_HOST_MASK;
7525 	} else {
7526 		/*
7527 		 * Note that ip_subnet_mask returns a zero mask in the case of
7528 		 * default (an all-zeroes address).
7529 		 */
7530 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7531 	}
7532 
7533 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7534 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7535 	    mp, ip_process_ioctl, ipst);
7536 	if (ipif != NULL)
7537 		ipif_refrele(ipif);
7538 	return (error);
7539 }
7540 
7541 /*
7542  * Enqueue the mp onto the ipsq, chained by b_next.
7543  * b_prev stores the function to be executed later, and b_queue the queue
7544  * where this mp originated.
7545  */
7546 void
7547 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7548     ill_t *pending_ill)
7549 {
7550 	conn_t	*connp = NULL;
7551 
7552 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7553 	ASSERT(func != NULL);
7554 
7555 	mp->b_queue = q;
7556 	mp->b_prev = (void *)func;
7557 	mp->b_next = NULL;
7558 
7559 	switch (type) {
7560 	case CUR_OP:
7561 		if (ipsq->ipsq_mptail != NULL) {
7562 			ASSERT(ipsq->ipsq_mphead != NULL);
7563 			ipsq->ipsq_mptail->b_next = mp;
7564 		} else {
7565 			ASSERT(ipsq->ipsq_mphead == NULL);
7566 			ipsq->ipsq_mphead = mp;
7567 		}
7568 		ipsq->ipsq_mptail = mp;
7569 		break;
7570 
7571 	case NEW_OP:
7572 		if (ipsq->ipsq_xopq_mptail != NULL) {
7573 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7574 			ipsq->ipsq_xopq_mptail->b_next = mp;
7575 		} else {
7576 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7577 			ipsq->ipsq_xopq_mphead = mp;
7578 		}
7579 		ipsq->ipsq_xopq_mptail = mp;
7580 		break;
7581 	default:
7582 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7583 	}
7584 
7585 	if (CONN_Q(q) && pending_ill != NULL) {
7586 		connp = Q_TO_CONN(q);
7587 
7588 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7589 		connp->conn_oper_pending_ill = pending_ill;
7590 	}
7591 }
7592 
7593 /*
7594  * Return the mp at the head of the ipsq. After emptying the ipsq
7595  * look at the next ioctl, if this ioctl is complete. Otherwise
7596  * return, we will resume when we complete the current ioctl.
7597  * The current ioctl will wait till it gets a response from the
7598  * driver below.
7599  */
7600 static mblk_t *
7601 ipsq_dq(ipsq_t *ipsq)
7602 {
7603 	mblk_t	*mp;
7604 
7605 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7606 
7607 	mp = ipsq->ipsq_mphead;
7608 	if (mp != NULL) {
7609 		ipsq->ipsq_mphead = mp->b_next;
7610 		if (ipsq->ipsq_mphead == NULL)
7611 			ipsq->ipsq_mptail = NULL;
7612 		mp->b_next = NULL;
7613 		return (mp);
7614 	}
7615 	if (ipsq->ipsq_current_ipif != NULL)
7616 		return (NULL);
7617 	mp = ipsq->ipsq_xopq_mphead;
7618 	if (mp != NULL) {
7619 		ipsq->ipsq_xopq_mphead = mp->b_next;
7620 		if (ipsq->ipsq_xopq_mphead == NULL)
7621 			ipsq->ipsq_xopq_mptail = NULL;
7622 		mp->b_next = NULL;
7623 		return (mp);
7624 	}
7625 	return (NULL);
7626 }
7627 
7628 /*
7629  * Enter the ipsq corresponding to ill, by waiting synchronously till
7630  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7631  * will have to drain completely before ipsq_enter returns success.
7632  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7633  * and the ipsq_exit logic will start the next enqueued ioctl after
7634  * completion of the current ioctl. If 'force' is used, we don't wait
7635  * for the enqueued ioctls. This is needed when a conn_close wants to
7636  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7637  * of an ill can also use this option. But we dont' use it currently.
7638  */
7639 #define	ENTER_SQ_WAIT_TICKS 100
7640 boolean_t
7641 ipsq_enter(ill_t *ill, boolean_t force)
7642 {
7643 	ipsq_t	*ipsq;
7644 	boolean_t waited_enough = B_FALSE;
7645 
7646 	/*
7647 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7648 	 * Since the <ill-ipsq> assocs could change while we wait for the
7649 	 * writer, it is easier to wait on a fixed global rather than try to
7650 	 * cv_wait on a changing ipsq.
7651 	 */
7652 	mutex_enter(&ill->ill_lock);
7653 	for (;;) {
7654 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7655 			mutex_exit(&ill->ill_lock);
7656 			return (B_FALSE);
7657 		}
7658 
7659 		ipsq = ill->ill_phyint->phyint_ipsq;
7660 		mutex_enter(&ipsq->ipsq_lock);
7661 		if (ipsq->ipsq_writer == NULL &&
7662 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7663 			break;
7664 		} else if (ipsq->ipsq_writer != NULL) {
7665 			mutex_exit(&ipsq->ipsq_lock);
7666 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7667 		} else {
7668 			mutex_exit(&ipsq->ipsq_lock);
7669 			if (force) {
7670 				(void) cv_timedwait(&ill->ill_cv,
7671 				    &ill->ill_lock,
7672 				    lbolt + ENTER_SQ_WAIT_TICKS);
7673 				waited_enough = B_TRUE;
7674 				continue;
7675 			} else {
7676 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7677 			}
7678 		}
7679 	}
7680 
7681 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7682 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7683 	ipsq->ipsq_writer = curthread;
7684 	ipsq->ipsq_reentry_cnt++;
7685 #ifdef DEBUG
7686 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7687 #endif
7688 	mutex_exit(&ipsq->ipsq_lock);
7689 	mutex_exit(&ill->ill_lock);
7690 	return (B_TRUE);
7691 }
7692 
7693 /*
7694  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7695  * certain critical operations like plumbing (i.e. most set ioctls),
7696  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7697  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7698  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7699  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7700  * threads executing in the ipsq. Responses from the driver pertain to the
7701  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7702  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7703  *
7704  * If a thread does not want to reenter the ipsq when it is already writer,
7705  * it must make sure that the specified reentry point to be called later
7706  * when the ipsq is empty, nor any code path starting from the specified reentry
7707  * point must never ever try to enter the ipsq again. Otherwise it can lead
7708  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7709  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7710  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7711  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7712  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7713  * ioctl if the current ioctl has completed. If the current ioctl is still
7714  * in progress it simply returns. The current ioctl could be waiting for
7715  * a response from another module (arp_ or the driver or could be waiting for
7716  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7717  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7718  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7719  * ipsq_current_ipif is clear which happens only on ioctl completion.
7720  */
7721 
7722 /*
7723  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7724  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7725  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7726  * completion.
7727  */
7728 ipsq_t *
7729 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7730     ipsq_func_t func, int type, boolean_t reentry_ok)
7731 {
7732 	ipsq_t	*ipsq;
7733 
7734 	/* Only 1 of ipif or ill can be specified */
7735 	ASSERT((ipif != NULL) ^ (ill != NULL));
7736 	if (ipif != NULL)
7737 		ill = ipif->ipif_ill;
7738 
7739 	/*
7740 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7741 	 * ipsq of an ill can't change when ill_lock is held.
7742 	 */
7743 	GRAB_CONN_LOCK(q);
7744 	mutex_enter(&ill->ill_lock);
7745 	ipsq = ill->ill_phyint->phyint_ipsq;
7746 	mutex_enter(&ipsq->ipsq_lock);
7747 
7748 	/*
7749 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7750 	 *    (Note: If the caller does not specify reentry_ok then neither
7751 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7752 	 *    again. Otherwise it can lead to an infinite loop
7753 	 * 2. Enter the ipsq if there is no current writer and this attempted
7754 	 *    entry is part of the current ioctl or operation
7755 	 * 3. Enter the ipsq if there is no current writer and this is a new
7756 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7757 	 *    empty and there is no ioctl (or operation) currently in progress
7758 	 */
7759 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7760 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7761 	    ipsq->ipsq_current_ipif == NULL))) ||
7762 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7763 		/* Success. */
7764 		ipsq->ipsq_reentry_cnt++;
7765 		ipsq->ipsq_writer = curthread;
7766 		mutex_exit(&ipsq->ipsq_lock);
7767 		mutex_exit(&ill->ill_lock);
7768 		RELEASE_CONN_LOCK(q);
7769 #ifdef DEBUG
7770 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7771 		    IPSQ_STACK_DEPTH);
7772 #endif
7773 		return (ipsq);
7774 	}
7775 
7776 	ipsq_enq(ipsq, q, mp, func, type, ill);
7777 
7778 	mutex_exit(&ipsq->ipsq_lock);
7779 	mutex_exit(&ill->ill_lock);
7780 	RELEASE_CONN_LOCK(q);
7781 	return (NULL);
7782 }
7783 
7784 /*
7785  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7786  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7787  * cannot be entered, the mp is queued for completion.
7788  */
7789 void
7790 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7791     boolean_t reentry_ok)
7792 {
7793 	ipsq_t	*ipsq;
7794 
7795 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7796 
7797 	/*
7798 	 * Drop the caller's refhold on the ill.  This is safe since we either
7799 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7800 	 * IPSQ, in which case we return without accessing ill anymore.  This
7801 	 * is needed because func needs to see the correct refcount.
7802 	 * e.g. removeif can work only then.
7803 	 */
7804 	ill_refrele(ill);
7805 	if (ipsq != NULL) {
7806 		(*func)(ipsq, q, mp, NULL);
7807 		ipsq_exit(ipsq);
7808 	}
7809 }
7810 
7811 /*
7812  * If there are more than ILL_GRP_CNT ills in a group,
7813  * we use kmem alloc'd buffers, else use the stack
7814  */
7815 #define	ILL_GRP_CNT	14
7816 /*
7817  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7818  * Called by a thread that is currently exclusive on this ipsq.
7819  */
7820 void
7821 ipsq_exit(ipsq_t *ipsq)
7822 {
7823 	queue_t	*q;
7824 	mblk_t	*mp;
7825 	ipsq_func_t	func;
7826 	int	next;
7827 	ill_t	**ill_list = NULL;
7828 	size_t	ill_list_size = 0;
7829 	int	cnt = 0;
7830 	boolean_t need_ipsq_free = B_FALSE;
7831 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7832 
7833 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7834 	mutex_enter(&ipsq->ipsq_lock);
7835 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7836 	if (ipsq->ipsq_reentry_cnt != 1) {
7837 		ipsq->ipsq_reentry_cnt--;
7838 		mutex_exit(&ipsq->ipsq_lock);
7839 		return;
7840 	}
7841 
7842 	mp = ipsq_dq(ipsq);
7843 	while (mp != NULL) {
7844 again:
7845 		mutex_exit(&ipsq->ipsq_lock);
7846 		func = (ipsq_func_t)mp->b_prev;
7847 		q = (queue_t *)mp->b_queue;
7848 		mp->b_prev = NULL;
7849 		mp->b_queue = NULL;
7850 
7851 		/*
7852 		 * If 'q' is an conn queue, it is valid, since we did a
7853 		 * a refhold on the connp, at the start of the ioctl.
7854 		 * If 'q' is an ill queue, it is valid, since close of an
7855 		 * ill will clean up the 'ipsq'.
7856 		 */
7857 		(*func)(ipsq, q, mp, NULL);
7858 
7859 		mutex_enter(&ipsq->ipsq_lock);
7860 		mp = ipsq_dq(ipsq);
7861 	}
7862 
7863 	mutex_exit(&ipsq->ipsq_lock);
7864 
7865 	/*
7866 	 * Need to grab the locks in the right order. Need to
7867 	 * atomically check (under ipsq_lock) that there are no
7868 	 * messages before relinquishing the ipsq. Also need to
7869 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7870 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7871 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7872 	 * to grab ill_g_lock as writer.
7873 	 */
7874 	rw_enter(&ipst->ips_ill_g_lock,
7875 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7876 
7877 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7878 	if (ipsq->ipsq_refs != 0) {
7879 		/* At most 2 ills v4/v6 per phyint */
7880 		cnt = ipsq->ipsq_refs << 1;
7881 		ill_list_size = cnt * sizeof (ill_t *);
7882 		/*
7883 		 * If memory allocation fails, we will do the split
7884 		 * the next time ipsq_exit is called for whatever reason.
7885 		 * As long as the ipsq_split flag is set the need to
7886 		 * split is remembered.
7887 		 */
7888 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7889 		if (ill_list != NULL)
7890 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7891 	}
7892 	mutex_enter(&ipsq->ipsq_lock);
7893 	mp = ipsq_dq(ipsq);
7894 	if (mp != NULL) {
7895 		/* oops, some message has landed up, we can't get out */
7896 		if (ill_list != NULL)
7897 			ill_unlock_ills(ill_list, cnt);
7898 		rw_exit(&ipst->ips_ill_g_lock);
7899 		if (ill_list != NULL)
7900 			kmem_free(ill_list, ill_list_size);
7901 		ill_list = NULL;
7902 		ill_list_size = 0;
7903 		cnt = 0;
7904 		goto again;
7905 	}
7906 
7907 	/*
7908 	 * Split only if no ioctl is pending and if memory alloc succeeded
7909 	 * above.
7910 	 */
7911 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7912 	    ill_list != NULL) {
7913 		/*
7914 		 * No new ill can join this ipsq since we are holding the
7915 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7916 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7917 		 * If so we will retry on the next ipsq_exit.
7918 		 */
7919 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7920 	}
7921 
7922 	/*
7923 	 * We are holding the ipsq lock, hence no new messages can
7924 	 * land up on the ipsq, and there are no messages currently.
7925 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7926 	 * atomically while holding ill locks.
7927 	 */
7928 	ipsq->ipsq_writer = NULL;
7929 	ipsq->ipsq_reentry_cnt--;
7930 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7931 #ifdef DEBUG
7932 	ipsq->ipsq_depth = 0;
7933 #endif
7934 	mutex_exit(&ipsq->ipsq_lock);
7935 	/*
7936 	 * For IPMP this should wake up all ills in this ipsq.
7937 	 * We need to hold the ill_lock while waking up waiters to
7938 	 * avoid missed wakeups. But there is no need to acquire all
7939 	 * the ill locks and then wakeup. If we have not acquired all
7940 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7941 	 * wakes up ills one at a time after getting the right ill_lock
7942 	 */
7943 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7944 	if (ill_list != NULL)
7945 		ill_unlock_ills(ill_list, cnt);
7946 	if (ipsq->ipsq_refs == 0)
7947 		need_ipsq_free = B_TRUE;
7948 	rw_exit(&ipst->ips_ill_g_lock);
7949 	if (ill_list != 0)
7950 		kmem_free(ill_list, ill_list_size);
7951 
7952 	if (need_ipsq_free) {
7953 		/*
7954 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7955 		 * looked up. ipsq can be looked up only thru ill or phyint
7956 		 * and there are no ills/phyint on this ipsq.
7957 		 */
7958 		ipsq_delete(ipsq);
7959 	}
7960 
7961 	/*
7962 	 * Now that we're outside the IPSQ, start any IGMP/MLD timers.  We
7963 	 * can't start these inside the IPSQ since e.g. igmp_start_timers() ->
7964 	 * untimeout() (inside the IPSQ, waiting for an executing timeout to
7965 	 * finish) could deadlock with igmp_timeout_handler() -> ipsq_enter()
7966 	 * (executing the timeout, waiting to get inside the IPSQ).
7967 	 *
7968 	 * However, there is one exception to the above: if this thread *is*
7969 	 * the IGMP/MLD timeout handler thread, then we must not start its
7970 	 * timer until the current handler is done.
7971 	 */
7972 	mutex_enter(&ipst->ips_igmp_timer_lock);
7973 	if (curthread != ipst->ips_igmp_timer_thread) {
7974 		next = ipst->ips_igmp_deferred_next;
7975 		ipst->ips_igmp_deferred_next = INFINITY;
7976 		mutex_exit(&ipst->ips_igmp_timer_lock);
7977 
7978 		if (next != INFINITY)
7979 			igmp_start_timers(next, ipst);
7980 	} else {
7981 		mutex_exit(&ipst->ips_igmp_timer_lock);
7982 	}
7983 
7984 	mutex_enter(&ipst->ips_mld_timer_lock);
7985 	if (curthread != ipst->ips_mld_timer_thread) {
7986 		next = ipst->ips_mld_deferred_next;
7987 		ipst->ips_mld_deferred_next = INFINITY;
7988 		mutex_exit(&ipst->ips_mld_timer_lock);
7989 
7990 		if (next != INFINITY)
7991 			mld_start_timers(next, ipst);
7992 	} else {
7993 		mutex_exit(&ipst->ips_mld_timer_lock);
7994 	}
7995 }
7996 
7997 /*
7998  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7999  * and `ioccmd'.
8000  */
8001 void
8002 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8003 {
8004 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8005 
8006 	mutex_enter(&ipsq->ipsq_lock);
8007 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8008 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8009 	ipsq->ipsq_current_done = B_FALSE;
8010 	ipsq->ipsq_current_ipif = ipif;
8011 	ipsq->ipsq_current_ioctl = ioccmd;
8012 	mutex_exit(&ipsq->ipsq_lock);
8013 }
8014 
8015 /*
8016  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8017  * the next exclusive operation to begin once we ipsq_exit().  However, if
8018  * pending DLPI operations remain, then we will wait for the queue to drain
8019  * before allowing the next exclusive operation to begin.  This ensures that
8020  * DLPI operations from one exclusive operation are never improperly processed
8021  * as part of a subsequent exclusive operation.
8022  */
8023 void
8024 ipsq_current_finish(ipsq_t *ipsq)
8025 {
8026 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8027 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8028 
8029 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8030 
8031 	/*
8032 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8033 	 * (but in that case, IPIF_CHANGING will already be clear and no
8034 	 * pending DLPI messages can remain).
8035 	 */
8036 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8037 		ill_t *ill = ipif->ipif_ill;
8038 
8039 		mutex_enter(&ill->ill_lock);
8040 		dlpi_pending = ill->ill_dlpi_pending;
8041 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8042 		/* Send any queued event */
8043 		ill_nic_info_dispatch(ill);
8044 		mutex_exit(&ill->ill_lock);
8045 	}
8046 
8047 	mutex_enter(&ipsq->ipsq_lock);
8048 	ipsq->ipsq_current_ioctl = 0;
8049 	ipsq->ipsq_current_done = B_TRUE;
8050 	if (dlpi_pending == DL_PRIM_INVAL)
8051 		ipsq->ipsq_current_ipif = NULL;
8052 	mutex_exit(&ipsq->ipsq_lock);
8053 }
8054 
8055 /*
8056  * The ill is closing. Flush all messages on the ipsq that originated
8057  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8058  * for this ill since ipsq_enter could not have entered until then.
8059  * New messages can't be queued since the CONDEMNED flag is set.
8060  */
8061 static void
8062 ipsq_flush(ill_t *ill)
8063 {
8064 	queue_t	*q;
8065 	mblk_t	*prev;
8066 	mblk_t	*mp;
8067 	mblk_t	*mp_next;
8068 	ipsq_t	*ipsq;
8069 
8070 	ASSERT(IAM_WRITER_ILL(ill));
8071 	ipsq = ill->ill_phyint->phyint_ipsq;
8072 	/*
8073 	 * Flush any messages sent up by the driver.
8074 	 */
8075 	mutex_enter(&ipsq->ipsq_lock);
8076 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8077 		mp_next = mp->b_next;
8078 		q = mp->b_queue;
8079 		if (q == ill->ill_rq || q == ill->ill_wq) {
8080 			/* Remove the mp from the ipsq */
8081 			if (prev == NULL)
8082 				ipsq->ipsq_mphead = mp->b_next;
8083 			else
8084 				prev->b_next = mp->b_next;
8085 			if (ipsq->ipsq_mptail == mp) {
8086 				ASSERT(mp_next == NULL);
8087 				ipsq->ipsq_mptail = prev;
8088 			}
8089 			inet_freemsg(mp);
8090 		} else {
8091 			prev = mp;
8092 		}
8093 	}
8094 	mutex_exit(&ipsq->ipsq_lock);
8095 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8096 	ipsq_xopq_mp_cleanup(ill, NULL);
8097 	ill_pending_mp_cleanup(ill);
8098 }
8099 
8100 /* ARGSUSED */
8101 int
8102 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8103     ip_ioctl_cmd_t *ipip, void *ifreq)
8104 {
8105 	ill_t	*ill;
8106 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8107 	boolean_t isv6;
8108 	conn_t	*connp;
8109 	ip_stack_t	*ipst;
8110 
8111 	connp = Q_TO_CONN(q);
8112 	ipst = connp->conn_netstack->netstack_ip;
8113 	isv6 = connp->conn_af_isv6;
8114 	/*
8115 	 * Set original index.
8116 	 * Failover and failback move logical interfaces
8117 	 * from one physical interface to another.  The
8118 	 * original index indicates the parent of a logical
8119 	 * interface, in other words, the physical interface
8120 	 * the logical interface will be moved back to on
8121 	 * failback.
8122 	 */
8123 
8124 	/*
8125 	 * Don't allow the original index to be changed
8126 	 * for non-failover addresses, autoconfigured
8127 	 * addresses, or IPv6 link local addresses.
8128 	 */
8129 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8130 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8131 		return (EINVAL);
8132 	}
8133 	/*
8134 	 * The new original index must be in use by some
8135 	 * physical interface.
8136 	 */
8137 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8138 	    NULL, NULL, ipst);
8139 	if (ill == NULL)
8140 		return (ENXIO);
8141 	ill_refrele(ill);
8142 
8143 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8144 	/*
8145 	 * When this ipif gets failed back, don't
8146 	 * preserve the original id, as it is no
8147 	 * longer applicable.
8148 	 */
8149 	ipif->ipif_orig_ipifid = 0;
8150 	/*
8151 	 * For IPv4, change the original index of any
8152 	 * multicast addresses associated with the
8153 	 * ipif to the new value.
8154 	 */
8155 	if (!isv6) {
8156 		ilm_t *ilm;
8157 
8158 		mutex_enter(&ipif->ipif_ill->ill_lock);
8159 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8160 		    ilm = ilm->ilm_next) {
8161 			if (ilm->ilm_ipif == ipif) {
8162 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8163 			}
8164 		}
8165 		mutex_exit(&ipif->ipif_ill->ill_lock);
8166 	}
8167 	return (0);
8168 }
8169 
8170 /* ARGSUSED */
8171 int
8172 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8173     ip_ioctl_cmd_t *ipip, void *ifreq)
8174 {
8175 	struct lifreq *lifr = (struct lifreq *)ifreq;
8176 
8177 	/*
8178 	 * Get the original interface index i.e the one
8179 	 * before FAILOVER if it ever happened.
8180 	 */
8181 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8182 	return (0);
8183 }
8184 
8185 /*
8186  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8187  * refhold and return the associated ipif
8188  */
8189 /* ARGSUSED */
8190 int
8191 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8192     cmd_info_t *ci, ipsq_func_t func)
8193 {
8194 	boolean_t exists;
8195 	struct iftun_req *ta;
8196 	ipif_t	*ipif;
8197 	ill_t	*ill;
8198 	boolean_t isv6;
8199 	mblk_t	*mp1;
8200 	int	error;
8201 	conn_t	*connp;
8202 	ip_stack_t	*ipst;
8203 
8204 	/* Existence verified in ip_wput_nondata */
8205 	mp1 = mp->b_cont->b_cont;
8206 	ta = (struct iftun_req *)mp1->b_rptr;
8207 	/*
8208 	 * Null terminate the string to protect against buffer
8209 	 * overrun. String was generated by user code and may not
8210 	 * be trusted.
8211 	 */
8212 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8213 
8214 	connp = Q_TO_CONN(q);
8215 	isv6 = connp->conn_af_isv6;
8216 	ipst = connp->conn_netstack->netstack_ip;
8217 
8218 	/* Disallows implicit create */
8219 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8220 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8221 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8222 	if (ipif == NULL)
8223 		return (error);
8224 
8225 	if (ipif->ipif_id != 0) {
8226 		/*
8227 		 * We really don't want to set/get tunnel parameters
8228 		 * on virtual tunnel interfaces.  Only allow the
8229 		 * base tunnel to do these.
8230 		 */
8231 		ipif_refrele(ipif);
8232 		return (EINVAL);
8233 	}
8234 
8235 	/*
8236 	 * Send down to tunnel mod for ioctl processing.
8237 	 * Will finish ioctl in ip_rput_other().
8238 	 */
8239 	ill = ipif->ipif_ill;
8240 	if (ill->ill_net_type == IRE_LOOPBACK) {
8241 		ipif_refrele(ipif);
8242 		return (EOPNOTSUPP);
8243 	}
8244 
8245 	if (ill->ill_wq == NULL) {
8246 		ipif_refrele(ipif);
8247 		return (ENXIO);
8248 	}
8249 	/*
8250 	 * Mark the ioctl as coming from an IPv6 interface for
8251 	 * tun's convenience.
8252 	 */
8253 	if (ill->ill_isv6)
8254 		ta->ifta_flags |= 0x80000000;
8255 	ci->ci_ipif = ipif;
8256 	return (0);
8257 }
8258 
8259 /*
8260  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8261  * and return the associated ipif.
8262  * Return value:
8263  *	Non zero: An error has occurred. ci may not be filled out.
8264  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8265  *	a held ipif in ci.ci_ipif.
8266  */
8267 int
8268 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8269     cmd_info_t *ci, ipsq_func_t func)
8270 {
8271 	sin_t		*sin;
8272 	sin6_t		*sin6;
8273 	char		*name;
8274 	struct ifreq    *ifr;
8275 	struct lifreq    *lifr;
8276 	ipif_t		*ipif = NULL;
8277 	ill_t		*ill;
8278 	conn_t		*connp;
8279 	boolean_t	isv6;
8280 	boolean_t	exists;
8281 	int		err;
8282 	mblk_t		*mp1;
8283 	zoneid_t	zoneid;
8284 	ip_stack_t	*ipst;
8285 
8286 	if (q->q_next != NULL) {
8287 		ill = (ill_t *)q->q_ptr;
8288 		isv6 = ill->ill_isv6;
8289 		connp = NULL;
8290 		zoneid = ALL_ZONES;
8291 		ipst = ill->ill_ipst;
8292 	} else {
8293 		ill = NULL;
8294 		connp = Q_TO_CONN(q);
8295 		isv6 = connp->conn_af_isv6;
8296 		zoneid = connp->conn_zoneid;
8297 		if (zoneid == GLOBAL_ZONEID) {
8298 			/* global zone can access ipifs in all zones */
8299 			zoneid = ALL_ZONES;
8300 		}
8301 		ipst = connp->conn_netstack->netstack_ip;
8302 	}
8303 
8304 	/* Has been checked in ip_wput_nondata */
8305 	mp1 = mp->b_cont->b_cont;
8306 
8307 	if (ipip->ipi_cmd_type == IF_CMD) {
8308 		/* This a old style SIOC[GS]IF* command */
8309 		ifr = (struct ifreq *)mp1->b_rptr;
8310 		/*
8311 		 * Null terminate the string to protect against buffer
8312 		 * overrun. String was generated by user code and may not
8313 		 * be trusted.
8314 		 */
8315 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8316 		sin = (sin_t *)&ifr->ifr_addr;
8317 		name = ifr->ifr_name;
8318 		ci->ci_sin = sin;
8319 		ci->ci_sin6 = NULL;
8320 		ci->ci_lifr = (struct lifreq *)ifr;
8321 	} else {
8322 		/* This a new style SIOC[GS]LIF* command */
8323 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8324 		lifr = (struct lifreq *)mp1->b_rptr;
8325 		/*
8326 		 * Null terminate the string to protect against buffer
8327 		 * overrun. String was generated by user code and may not
8328 		 * be trusted.
8329 		 */
8330 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8331 		name = lifr->lifr_name;
8332 		sin = (sin_t *)&lifr->lifr_addr;
8333 		sin6 = (sin6_t *)&lifr->lifr_addr;
8334 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8335 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8336 			    LIFNAMSIZ);
8337 		}
8338 		ci->ci_sin = sin;
8339 		ci->ci_sin6 = sin6;
8340 		ci->ci_lifr = lifr;
8341 	}
8342 
8343 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8344 		/*
8345 		 * The ioctl will be failed if the ioctl comes down
8346 		 * an conn stream
8347 		 */
8348 		if (ill == NULL) {
8349 			/*
8350 			 * Not an ill queue, return EINVAL same as the
8351 			 * old error code.
8352 			 */
8353 			return (ENXIO);
8354 		}
8355 		ipif = ill->ill_ipif;
8356 		ipif_refhold(ipif);
8357 	} else {
8358 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8359 		    &exists, isv6, zoneid,
8360 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8361 		    ipst);
8362 		if (ipif == NULL) {
8363 			if (err == EINPROGRESS)
8364 				return (err);
8365 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8366 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8367 				/*
8368 				 * Need to try both v4 and v6 since this
8369 				 * ioctl can come down either v4 or v6
8370 				 * socket. The lifreq.lifr_family passed
8371 				 * down by this ioctl is AF_UNSPEC.
8372 				 */
8373 				ipif = ipif_lookup_on_name(name,
8374 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8375 				    zoneid, (connp == NULL) ? q :
8376 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8377 				if (err == EINPROGRESS)
8378 					return (err);
8379 			}
8380 			err = 0;	/* Ensure we don't use it below */
8381 		}
8382 	}
8383 
8384 	/*
8385 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8386 	 */
8387 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8388 		ipif_refrele(ipif);
8389 		return (ENXIO);
8390 	}
8391 
8392 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8393 	    name[0] == '\0') {
8394 		/*
8395 		 * Handle a or a SIOC?IF* with a null name
8396 		 * during plumb (on the ill queue before the I_PLINK).
8397 		 */
8398 		ipif = ill->ill_ipif;
8399 		ipif_refhold(ipif);
8400 	}
8401 
8402 	if (ipif == NULL)
8403 		return (ENXIO);
8404 
8405 	/*
8406 	 * Allow only GET operations if this ipif has been created
8407 	 * temporarily due to a MOVE operation.
8408 	 */
8409 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8410 		ipif_refrele(ipif);
8411 		return (EINVAL);
8412 	}
8413 
8414 	ci->ci_ipif = ipif;
8415 	return (0);
8416 }
8417 
8418 /*
8419  * Return the total number of ipifs.
8420  */
8421 static uint_t
8422 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8423 {
8424 	uint_t numifs = 0;
8425 	ill_t	*ill;
8426 	ill_walk_context_t	ctx;
8427 	ipif_t	*ipif;
8428 
8429 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8430 	ill = ILL_START_WALK_V4(&ctx, ipst);
8431 
8432 	while (ill != NULL) {
8433 		for (ipif = ill->ill_ipif; ipif != NULL;
8434 		    ipif = ipif->ipif_next) {
8435 			if (ipif->ipif_zoneid == zoneid ||
8436 			    ipif->ipif_zoneid == ALL_ZONES)
8437 				numifs++;
8438 		}
8439 		ill = ill_next(&ctx, ill);
8440 	}
8441 	rw_exit(&ipst->ips_ill_g_lock);
8442 	return (numifs);
8443 }
8444 
8445 /*
8446  * Return the total number of ipifs.
8447  */
8448 static uint_t
8449 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8450 {
8451 	uint_t numifs = 0;
8452 	ill_t	*ill;
8453 	ipif_t	*ipif;
8454 	ill_walk_context_t	ctx;
8455 
8456 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8457 
8458 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8459 	if (family == AF_INET)
8460 		ill = ILL_START_WALK_V4(&ctx, ipst);
8461 	else if (family == AF_INET6)
8462 		ill = ILL_START_WALK_V6(&ctx, ipst);
8463 	else
8464 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8465 
8466 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8467 		for (ipif = ill->ill_ipif; ipif != NULL;
8468 		    ipif = ipif->ipif_next) {
8469 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8470 			    !(lifn_flags & LIFC_NOXMIT))
8471 				continue;
8472 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8473 			    !(lifn_flags & LIFC_TEMPORARY))
8474 				continue;
8475 			if (((ipif->ipif_flags &
8476 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8477 			    IPIF_DEPRECATED)) ||
8478 			    IS_LOOPBACK(ill) ||
8479 			    !(ipif->ipif_flags & IPIF_UP)) &&
8480 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8481 				continue;
8482 
8483 			if (zoneid != ipif->ipif_zoneid &&
8484 			    ipif->ipif_zoneid != ALL_ZONES &&
8485 			    (zoneid != GLOBAL_ZONEID ||
8486 			    !(lifn_flags & LIFC_ALLZONES)))
8487 				continue;
8488 
8489 			numifs++;
8490 		}
8491 	}
8492 	rw_exit(&ipst->ips_ill_g_lock);
8493 	return (numifs);
8494 }
8495 
8496 uint_t
8497 ip_get_lifsrcofnum(ill_t *ill)
8498 {
8499 	uint_t numifs = 0;
8500 	ill_t	*ill_head = ill;
8501 	ip_stack_t	*ipst = ill->ill_ipst;
8502 
8503 	/*
8504 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8505 	 * other thread may be trying to relink the ILLs in this usesrc group
8506 	 * and adjusting the ill_usesrc_grp_next pointers
8507 	 */
8508 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8509 	if ((ill->ill_usesrc_ifindex == 0) &&
8510 	    (ill->ill_usesrc_grp_next != NULL)) {
8511 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8512 		    ill = ill->ill_usesrc_grp_next)
8513 			numifs++;
8514 	}
8515 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8516 
8517 	return (numifs);
8518 }
8519 
8520 /* Null values are passed in for ipif, sin, and ifreq */
8521 /* ARGSUSED */
8522 int
8523 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8524     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8525 {
8526 	int *nump;
8527 	conn_t *connp = Q_TO_CONN(q);
8528 
8529 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8530 
8531 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8532 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8533 
8534 	*nump = ip_get_numifs(connp->conn_zoneid,
8535 	    connp->conn_netstack->netstack_ip);
8536 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8537 	return (0);
8538 }
8539 
8540 /* Null values are passed in for ipif, sin, and ifreq */
8541 /* ARGSUSED */
8542 int
8543 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8544     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8545 {
8546 	struct lifnum *lifn;
8547 	mblk_t	*mp1;
8548 	conn_t *connp = Q_TO_CONN(q);
8549 
8550 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8551 
8552 	/* Existence checked in ip_wput_nondata */
8553 	mp1 = mp->b_cont->b_cont;
8554 
8555 	lifn = (struct lifnum *)mp1->b_rptr;
8556 	switch (lifn->lifn_family) {
8557 	case AF_UNSPEC:
8558 	case AF_INET:
8559 	case AF_INET6:
8560 		break;
8561 	default:
8562 		return (EAFNOSUPPORT);
8563 	}
8564 
8565 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8566 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8567 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8568 	return (0);
8569 }
8570 
8571 /* ARGSUSED */
8572 int
8573 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8574     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8575 {
8576 	STRUCT_HANDLE(ifconf, ifc);
8577 	mblk_t *mp1;
8578 	struct iocblk *iocp;
8579 	struct ifreq *ifr;
8580 	ill_walk_context_t	ctx;
8581 	ill_t	*ill;
8582 	ipif_t	*ipif;
8583 	struct sockaddr_in *sin;
8584 	int32_t	ifclen;
8585 	zoneid_t zoneid;
8586 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8587 
8588 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8589 
8590 	ip1dbg(("ip_sioctl_get_ifconf"));
8591 	/* Existence verified in ip_wput_nondata */
8592 	mp1 = mp->b_cont->b_cont;
8593 	iocp = (struct iocblk *)mp->b_rptr;
8594 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8595 
8596 	/*
8597 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8598 	 * the user buffer address and length into which the list of struct
8599 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8600 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8601 	 * the SIOCGIFCONF operation was redefined to simply provide
8602 	 * a large output buffer into which we are supposed to jam the ifreq
8603 	 * array.  The same ioctl command code was used, despite the fact that
8604 	 * both the applications and the kernel code had to change, thus making
8605 	 * it impossible to support both interfaces.
8606 	 *
8607 	 * For reasons not good enough to try to explain, the following
8608 	 * algorithm is used for deciding what to do with one of these:
8609 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8610 	 * form with the output buffer coming down as the continuation message.
8611 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8612 	 * and we have to copy in the ifconf structure to find out how big the
8613 	 * output buffer is and where to copy out to.  Sure no problem...
8614 	 *
8615 	 */
8616 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8617 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8618 		int numifs = 0;
8619 		size_t ifc_bufsize;
8620 
8621 		/*
8622 		 * Must be (better be!) continuation of a TRANSPARENT
8623 		 * IOCTL.  We just copied in the ifconf structure.
8624 		 */
8625 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8626 		    (struct ifconf *)mp1->b_rptr);
8627 
8628 		/*
8629 		 * Allocate a buffer to hold requested information.
8630 		 *
8631 		 * If ifc_len is larger than what is needed, we only
8632 		 * allocate what we will use.
8633 		 *
8634 		 * If ifc_len is smaller than what is needed, return
8635 		 * EINVAL.
8636 		 *
8637 		 * XXX: the ill_t structure can hava 2 counters, for
8638 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8639 		 * number of interfaces for a device, so we don't need
8640 		 * to count them here...
8641 		 */
8642 		numifs = ip_get_numifs(zoneid, ipst);
8643 
8644 		ifclen = STRUCT_FGET(ifc, ifc_len);
8645 		ifc_bufsize = numifs * sizeof (struct ifreq);
8646 		if (ifc_bufsize > ifclen) {
8647 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8648 				/* old behaviour */
8649 				return (EINVAL);
8650 			} else {
8651 				ifc_bufsize = ifclen;
8652 			}
8653 		}
8654 
8655 		mp1 = mi_copyout_alloc(q, mp,
8656 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8657 		if (mp1 == NULL)
8658 			return (ENOMEM);
8659 
8660 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8661 	}
8662 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8663 	/*
8664 	 * the SIOCGIFCONF ioctl only knows about
8665 	 * IPv4 addresses, so don't try to tell
8666 	 * it about interfaces with IPv6-only
8667 	 * addresses. (Last parm 'isv6' is B_FALSE)
8668 	 */
8669 
8670 	ifr = (struct ifreq *)mp1->b_rptr;
8671 
8672 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8673 	ill = ILL_START_WALK_V4(&ctx, ipst);
8674 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8675 		for (ipif = ill->ill_ipif; ipif != NULL;
8676 		    ipif = ipif->ipif_next) {
8677 			if (zoneid != ipif->ipif_zoneid &&
8678 			    ipif->ipif_zoneid != ALL_ZONES)
8679 				continue;
8680 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8681 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8682 					/* old behaviour */
8683 					rw_exit(&ipst->ips_ill_g_lock);
8684 					return (EINVAL);
8685 				} else {
8686 					goto if_copydone;
8687 				}
8688 			}
8689 			ipif_get_name(ipif, ifr->ifr_name,
8690 			    sizeof (ifr->ifr_name));
8691 			sin = (sin_t *)&ifr->ifr_addr;
8692 			*sin = sin_null;
8693 			sin->sin_family = AF_INET;
8694 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8695 			ifr++;
8696 		}
8697 	}
8698 if_copydone:
8699 	rw_exit(&ipst->ips_ill_g_lock);
8700 	mp1->b_wptr = (uchar_t *)ifr;
8701 
8702 	if (STRUCT_BUF(ifc) != NULL) {
8703 		STRUCT_FSET(ifc, ifc_len,
8704 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8705 	}
8706 	return (0);
8707 }
8708 
8709 /*
8710  * Get the interfaces using the address hosted on the interface passed in,
8711  * as a source adddress
8712  */
8713 /* ARGSUSED */
8714 int
8715 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8716     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8717 {
8718 	mblk_t *mp1;
8719 	ill_t	*ill, *ill_head;
8720 	ipif_t	*ipif, *orig_ipif;
8721 	int	numlifs = 0;
8722 	size_t	lifs_bufsize, lifsmaxlen;
8723 	struct	lifreq *lifr;
8724 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8725 	uint_t	ifindex;
8726 	zoneid_t zoneid;
8727 	int err = 0;
8728 	boolean_t isv6 = B_FALSE;
8729 	struct	sockaddr_in	*sin;
8730 	struct	sockaddr_in6	*sin6;
8731 	STRUCT_HANDLE(lifsrcof, lifs);
8732 	ip_stack_t		*ipst;
8733 
8734 	ipst = CONNQ_TO_IPST(q);
8735 
8736 	ASSERT(q->q_next == NULL);
8737 
8738 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8739 
8740 	/* Existence verified in ip_wput_nondata */
8741 	mp1 = mp->b_cont->b_cont;
8742 
8743 	/*
8744 	 * Must be (better be!) continuation of a TRANSPARENT
8745 	 * IOCTL.  We just copied in the lifsrcof structure.
8746 	 */
8747 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8748 	    (struct lifsrcof *)mp1->b_rptr);
8749 
8750 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8751 		return (EINVAL);
8752 
8753 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8754 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8755 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8756 	    ip_process_ioctl, &err, ipst);
8757 	if (ipif == NULL) {
8758 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8759 		    ifindex));
8760 		return (err);
8761 	}
8762 
8763 	/* Allocate a buffer to hold requested information */
8764 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8765 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8766 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8767 	/* The actual size needed is always returned in lifs_len */
8768 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8769 
8770 	/* If the amount we need is more than what is passed in, abort */
8771 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8772 		ipif_refrele(ipif);
8773 		return (0);
8774 	}
8775 
8776 	mp1 = mi_copyout_alloc(q, mp,
8777 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8778 	if (mp1 == NULL) {
8779 		ipif_refrele(ipif);
8780 		return (ENOMEM);
8781 	}
8782 
8783 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8784 	bzero(mp1->b_rptr, lifs_bufsize);
8785 
8786 	lifr = (struct lifreq *)mp1->b_rptr;
8787 
8788 	ill = ill_head = ipif->ipif_ill;
8789 	orig_ipif = ipif;
8790 
8791 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8792 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8793 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8794 
8795 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8796 	for (; (ill != NULL) && (ill != ill_head);
8797 	    ill = ill->ill_usesrc_grp_next) {
8798 
8799 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8800 			break;
8801 
8802 		ipif = ill->ill_ipif;
8803 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8804 		if (ipif->ipif_isv6) {
8805 			sin6 = (sin6_t *)&lifr->lifr_addr;
8806 			*sin6 = sin6_null;
8807 			sin6->sin6_family = AF_INET6;
8808 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8809 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8810 			    &ipif->ipif_v6net_mask);
8811 		} else {
8812 			sin = (sin_t *)&lifr->lifr_addr;
8813 			*sin = sin_null;
8814 			sin->sin_family = AF_INET;
8815 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8816 			lifr->lifr_addrlen = ip_mask_to_plen(
8817 			    ipif->ipif_net_mask);
8818 		}
8819 		lifr++;
8820 	}
8821 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8822 	rw_exit(&ipst->ips_ill_g_lock);
8823 	ipif_refrele(orig_ipif);
8824 	mp1->b_wptr = (uchar_t *)lifr;
8825 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8826 
8827 	return (0);
8828 }
8829 
8830 /* ARGSUSED */
8831 int
8832 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8833     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8834 {
8835 	mblk_t *mp1;
8836 	int	list;
8837 	ill_t	*ill;
8838 	ipif_t	*ipif;
8839 	int	flags;
8840 	int	numlifs = 0;
8841 	size_t	lifc_bufsize;
8842 	struct	lifreq *lifr;
8843 	sa_family_t	family;
8844 	struct	sockaddr_in	*sin;
8845 	struct	sockaddr_in6	*sin6;
8846 	ill_walk_context_t	ctx;
8847 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8848 	int32_t	lifclen;
8849 	zoneid_t zoneid;
8850 	STRUCT_HANDLE(lifconf, lifc);
8851 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8852 
8853 	ip1dbg(("ip_sioctl_get_lifconf"));
8854 
8855 	ASSERT(q->q_next == NULL);
8856 
8857 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8858 
8859 	/* Existence verified in ip_wput_nondata */
8860 	mp1 = mp->b_cont->b_cont;
8861 
8862 	/*
8863 	 * An extended version of SIOCGIFCONF that takes an
8864 	 * additional address family and flags field.
8865 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8866 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8867 	 * interfaces are omitted.
8868 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8869 	 * unless LIFC_TEMPORARY is specified.
8870 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8871 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8872 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8873 	 * has priority over LIFC_NOXMIT.
8874 	 */
8875 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8876 
8877 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8878 		return (EINVAL);
8879 
8880 	/*
8881 	 * Must be (better be!) continuation of a TRANSPARENT
8882 	 * IOCTL.  We just copied in the lifconf structure.
8883 	 */
8884 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8885 
8886 	family = STRUCT_FGET(lifc, lifc_family);
8887 	flags = STRUCT_FGET(lifc, lifc_flags);
8888 
8889 	switch (family) {
8890 	case AF_UNSPEC:
8891 		/*
8892 		 * walk all ILL's.
8893 		 */
8894 		list = MAX_G_HEADS;
8895 		break;
8896 	case AF_INET:
8897 		/*
8898 		 * walk only IPV4 ILL's.
8899 		 */
8900 		list = IP_V4_G_HEAD;
8901 		break;
8902 	case AF_INET6:
8903 		/*
8904 		 * walk only IPV6 ILL's.
8905 		 */
8906 		list = IP_V6_G_HEAD;
8907 		break;
8908 	default:
8909 		return (EAFNOSUPPORT);
8910 	}
8911 
8912 	/*
8913 	 * Allocate a buffer to hold requested information.
8914 	 *
8915 	 * If lifc_len is larger than what is needed, we only
8916 	 * allocate what we will use.
8917 	 *
8918 	 * If lifc_len is smaller than what is needed, return
8919 	 * EINVAL.
8920 	 */
8921 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8922 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8923 	lifclen = STRUCT_FGET(lifc, lifc_len);
8924 	if (lifc_bufsize > lifclen) {
8925 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8926 			return (EINVAL);
8927 		else
8928 			lifc_bufsize = lifclen;
8929 	}
8930 
8931 	mp1 = mi_copyout_alloc(q, mp,
8932 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8933 	if (mp1 == NULL)
8934 		return (ENOMEM);
8935 
8936 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8937 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8938 
8939 	lifr = (struct lifreq *)mp1->b_rptr;
8940 
8941 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8942 	ill = ill_first(list, list, &ctx, ipst);
8943 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8944 		for (ipif = ill->ill_ipif; ipif != NULL;
8945 		    ipif = ipif->ipif_next) {
8946 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8947 			    !(flags & LIFC_NOXMIT))
8948 				continue;
8949 
8950 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8951 			    !(flags & LIFC_TEMPORARY))
8952 				continue;
8953 
8954 			if (((ipif->ipif_flags &
8955 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8956 			    IPIF_DEPRECATED)) ||
8957 			    IS_LOOPBACK(ill) ||
8958 			    !(ipif->ipif_flags & IPIF_UP)) &&
8959 			    (flags & LIFC_EXTERNAL_SOURCE))
8960 				continue;
8961 
8962 			if (zoneid != ipif->ipif_zoneid &&
8963 			    ipif->ipif_zoneid != ALL_ZONES &&
8964 			    (zoneid != GLOBAL_ZONEID ||
8965 			    !(flags & LIFC_ALLZONES)))
8966 				continue;
8967 
8968 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8969 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8970 					rw_exit(&ipst->ips_ill_g_lock);
8971 					return (EINVAL);
8972 				} else {
8973 					goto lif_copydone;
8974 				}
8975 			}
8976 
8977 			ipif_get_name(ipif, lifr->lifr_name,
8978 			    sizeof (lifr->lifr_name));
8979 			if (ipif->ipif_isv6) {
8980 				sin6 = (sin6_t *)&lifr->lifr_addr;
8981 				*sin6 = sin6_null;
8982 				sin6->sin6_family = AF_INET6;
8983 				sin6->sin6_addr =
8984 				    ipif->ipif_v6lcl_addr;
8985 				lifr->lifr_addrlen =
8986 				    ip_mask_to_plen_v6(
8987 				    &ipif->ipif_v6net_mask);
8988 			} else {
8989 				sin = (sin_t *)&lifr->lifr_addr;
8990 				*sin = sin_null;
8991 				sin->sin_family = AF_INET;
8992 				sin->sin_addr.s_addr =
8993 				    ipif->ipif_lcl_addr;
8994 				lifr->lifr_addrlen =
8995 				    ip_mask_to_plen(
8996 				    ipif->ipif_net_mask);
8997 			}
8998 			lifr++;
8999 		}
9000 	}
9001 lif_copydone:
9002 	rw_exit(&ipst->ips_ill_g_lock);
9003 
9004 	mp1->b_wptr = (uchar_t *)lifr;
9005 	if (STRUCT_BUF(lifc) != NULL) {
9006 		STRUCT_FSET(lifc, lifc_len,
9007 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9008 	}
9009 	return (0);
9010 }
9011 
9012 /* ARGSUSED */
9013 int
9014 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9015     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9016 {
9017 	ip_stack_t	*ipst;
9018 
9019 	if (q->q_next == NULL)
9020 		ipst = CONNQ_TO_IPST(q);
9021 	else
9022 		ipst = ILLQ_TO_IPST(q);
9023 
9024 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9025 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9026 	return (0);
9027 }
9028 
9029 static void
9030 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9031 {
9032 	ip6_asp_t *table;
9033 	size_t table_size;
9034 	mblk_t *data_mp;
9035 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9036 	ip_stack_t	*ipst;
9037 
9038 	if (q->q_next == NULL)
9039 		ipst = CONNQ_TO_IPST(q);
9040 	else
9041 		ipst = ILLQ_TO_IPST(q);
9042 
9043 	/* These two ioctls are I_STR only */
9044 	if (iocp->ioc_count == TRANSPARENT) {
9045 		miocnak(q, mp, 0, EINVAL);
9046 		return;
9047 	}
9048 
9049 	data_mp = mp->b_cont;
9050 	if (data_mp == NULL) {
9051 		/* The user passed us a NULL argument */
9052 		table = NULL;
9053 		table_size = iocp->ioc_count;
9054 	} else {
9055 		/*
9056 		 * The user provided a table.  The stream head
9057 		 * may have copied in the user data in chunks,
9058 		 * so make sure everything is pulled up
9059 		 * properly.
9060 		 */
9061 		if (MBLKL(data_mp) < iocp->ioc_count) {
9062 			mblk_t *new_data_mp;
9063 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9064 			    NULL) {
9065 				miocnak(q, mp, 0, ENOMEM);
9066 				return;
9067 			}
9068 			freemsg(data_mp);
9069 			data_mp = new_data_mp;
9070 			mp->b_cont = data_mp;
9071 		}
9072 		table = (ip6_asp_t *)data_mp->b_rptr;
9073 		table_size = iocp->ioc_count;
9074 	}
9075 
9076 	switch (iocp->ioc_cmd) {
9077 	case SIOCGIP6ADDRPOLICY:
9078 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9079 		if (iocp->ioc_rval == -1)
9080 			iocp->ioc_error = EINVAL;
9081 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9082 		else if (table != NULL &&
9083 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9084 			ip6_asp_t *src = table;
9085 			ip6_asp32_t *dst = (void *)table;
9086 			int count = table_size / sizeof (ip6_asp_t);
9087 			int i;
9088 
9089 			/*
9090 			 * We need to do an in-place shrink of the array
9091 			 * to match the alignment attributes of the
9092 			 * 32-bit ABI looking at it.
9093 			 */
9094 			/* LINTED: logical expression always true: op "||" */
9095 			ASSERT(sizeof (*src) > sizeof (*dst));
9096 			for (i = 1; i < count; i++)
9097 				bcopy(src + i, dst + i, sizeof (*dst));
9098 		}
9099 #endif
9100 		break;
9101 
9102 	case SIOCSIP6ADDRPOLICY:
9103 		ASSERT(mp->b_prev == NULL);
9104 		mp->b_prev = (void *)q;
9105 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9106 		/*
9107 		 * We pass in the datamodel here so that the ip6_asp_replace()
9108 		 * routine can handle converting from 32-bit to native formats
9109 		 * where necessary.
9110 		 *
9111 		 * A better way to handle this might be to convert the inbound
9112 		 * data structure here, and hang it off a new 'mp'; thus the
9113 		 * ip6_asp_replace() logic would always be dealing with native
9114 		 * format data structures..
9115 		 *
9116 		 * (An even simpler way to handle these ioctls is to just
9117 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9118 		 * and just recompile everything that depends on it.)
9119 		 */
9120 #endif
9121 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9122 		    iocp->ioc_flag & IOC_MODELS);
9123 		return;
9124 	}
9125 
9126 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9127 	qreply(q, mp);
9128 }
9129 
9130 static void
9131 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9132 {
9133 	mblk_t 		*data_mp;
9134 	struct dstinforeq	*dir;
9135 	uint8_t		*end, *cur;
9136 	in6_addr_t	*daddr, *saddr;
9137 	ipaddr_t	v4daddr;
9138 	ire_t		*ire;
9139 	char		*slabel, *dlabel;
9140 	boolean_t	isipv4;
9141 	int		match_ire;
9142 	ill_t		*dst_ill;
9143 	ipif_t		*src_ipif, *ire_ipif;
9144 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9145 	zoneid_t	zoneid;
9146 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9147 
9148 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9149 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9150 
9151 	/*
9152 	 * This ioctl is I_STR only, and must have a
9153 	 * data mblk following the M_IOCTL mblk.
9154 	 */
9155 	data_mp = mp->b_cont;
9156 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9157 		miocnak(q, mp, 0, EINVAL);
9158 		return;
9159 	}
9160 
9161 	if (MBLKL(data_mp) < iocp->ioc_count) {
9162 		mblk_t *new_data_mp;
9163 
9164 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9165 			miocnak(q, mp, 0, ENOMEM);
9166 			return;
9167 		}
9168 		freemsg(data_mp);
9169 		data_mp = new_data_mp;
9170 		mp->b_cont = data_mp;
9171 	}
9172 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9173 
9174 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9175 	    end - cur >= sizeof (struct dstinforeq);
9176 	    cur += sizeof (struct dstinforeq)) {
9177 		dir = (struct dstinforeq *)cur;
9178 		daddr = &dir->dir_daddr;
9179 		saddr = &dir->dir_saddr;
9180 
9181 		/*
9182 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9183 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9184 		 * and ipif_select_source[_v6]() do not.
9185 		 */
9186 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9187 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9188 
9189 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9190 		if (isipv4) {
9191 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9192 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9193 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9194 		} else {
9195 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9196 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9197 		}
9198 		if (ire == NULL) {
9199 			dir->dir_dreachable = 0;
9200 
9201 			/* move on to next dst addr */
9202 			continue;
9203 		}
9204 		dir->dir_dreachable = 1;
9205 
9206 		ire_ipif = ire->ire_ipif;
9207 		if (ire_ipif == NULL)
9208 			goto next_dst;
9209 
9210 		/*
9211 		 * We expect to get back an interface ire or a
9212 		 * gateway ire cache entry.  For both types, the
9213 		 * output interface is ire_ipif->ipif_ill.
9214 		 */
9215 		dst_ill = ire_ipif->ipif_ill;
9216 		dir->dir_dmactype = dst_ill->ill_mactype;
9217 
9218 		if (isipv4) {
9219 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9220 		} else {
9221 			src_ipif = ipif_select_source_v6(dst_ill,
9222 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9223 			    zoneid);
9224 		}
9225 		if (src_ipif == NULL)
9226 			goto next_dst;
9227 
9228 		*saddr = src_ipif->ipif_v6lcl_addr;
9229 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9230 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9231 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9232 		dir->dir_sdeprecated =
9233 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9234 		ipif_refrele(src_ipif);
9235 next_dst:
9236 		ire_refrele(ire);
9237 	}
9238 	miocack(q, mp, iocp->ioc_count, 0);
9239 }
9240 
9241 /*
9242  * Check if this is an address assigned to this machine.
9243  * Skips interfaces that are down by using ire checks.
9244  * Translates mapped addresses to v4 addresses and then
9245  * treats them as such, returning true if the v4 address
9246  * associated with this mapped address is configured.
9247  * Note: Applications will have to be careful what they do
9248  * with the response; use of mapped addresses limits
9249  * what can be done with the socket, especially with
9250  * respect to socket options and ioctls - neither IPv4
9251  * options nor IPv6 sticky options/ancillary data options
9252  * may be used.
9253  */
9254 /* ARGSUSED */
9255 int
9256 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9257     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9258 {
9259 	struct sioc_addrreq *sia;
9260 	sin_t *sin;
9261 	ire_t *ire;
9262 	mblk_t *mp1;
9263 	zoneid_t zoneid;
9264 	ip_stack_t	*ipst;
9265 
9266 	ip1dbg(("ip_sioctl_tmyaddr"));
9267 
9268 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9269 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9270 	ipst = CONNQ_TO_IPST(q);
9271 
9272 	/* Existence verified in ip_wput_nondata */
9273 	mp1 = mp->b_cont->b_cont;
9274 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9275 	sin = (sin_t *)&sia->sa_addr;
9276 	switch (sin->sin_family) {
9277 	case AF_INET6: {
9278 		sin6_t *sin6 = (sin6_t *)sin;
9279 
9280 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9281 			ipaddr_t v4_addr;
9282 
9283 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9284 			    v4_addr);
9285 			ire = ire_ctable_lookup(v4_addr, 0,
9286 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9287 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9288 		} else {
9289 			in6_addr_t v6addr;
9290 
9291 			v6addr = sin6->sin6_addr;
9292 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9293 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9294 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9295 		}
9296 		break;
9297 	}
9298 	case AF_INET: {
9299 		ipaddr_t v4addr;
9300 
9301 		v4addr = sin->sin_addr.s_addr;
9302 		ire = ire_ctable_lookup(v4addr, 0,
9303 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9304 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9305 		break;
9306 	}
9307 	default:
9308 		return (EAFNOSUPPORT);
9309 	}
9310 	if (ire != NULL) {
9311 		sia->sa_res = 1;
9312 		ire_refrele(ire);
9313 	} else {
9314 		sia->sa_res = 0;
9315 	}
9316 	return (0);
9317 }
9318 
9319 /*
9320  * Check if this is an address assigned on-link i.e. neighbor,
9321  * and makes sure it's reachable from the current zone.
9322  * Returns true for my addresses as well.
9323  * Translates mapped addresses to v4 addresses and then
9324  * treats them as such, returning true if the v4 address
9325  * associated with this mapped address is configured.
9326  * Note: Applications will have to be careful what they do
9327  * with the response; use of mapped addresses limits
9328  * what can be done with the socket, especially with
9329  * respect to socket options and ioctls - neither IPv4
9330  * options nor IPv6 sticky options/ancillary data options
9331  * may be used.
9332  */
9333 /* ARGSUSED */
9334 int
9335 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9336     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9337 {
9338 	struct sioc_addrreq *sia;
9339 	sin_t *sin;
9340 	mblk_t	*mp1;
9341 	ire_t *ire = NULL;
9342 	zoneid_t zoneid;
9343 	ip_stack_t	*ipst;
9344 
9345 	ip1dbg(("ip_sioctl_tonlink"));
9346 
9347 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9348 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9349 	ipst = CONNQ_TO_IPST(q);
9350 
9351 	/* Existence verified in ip_wput_nondata */
9352 	mp1 = mp->b_cont->b_cont;
9353 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9354 	sin = (sin_t *)&sia->sa_addr;
9355 
9356 	/*
9357 	 * Match addresses with a zero gateway field to avoid
9358 	 * routes going through a router.
9359 	 * Exclude broadcast and multicast addresses.
9360 	 */
9361 	switch (sin->sin_family) {
9362 	case AF_INET6: {
9363 		sin6_t *sin6 = (sin6_t *)sin;
9364 
9365 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9366 			ipaddr_t v4_addr;
9367 
9368 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9369 			    v4_addr);
9370 			if (!CLASSD(v4_addr)) {
9371 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9372 				    NULL, NULL, zoneid, NULL,
9373 				    MATCH_IRE_GW, ipst);
9374 			}
9375 		} else {
9376 			in6_addr_t v6addr;
9377 			in6_addr_t v6gw;
9378 
9379 			v6addr = sin6->sin6_addr;
9380 			v6gw = ipv6_all_zeros;
9381 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9382 				ire = ire_route_lookup_v6(&v6addr, 0,
9383 				    &v6gw, 0, NULL, NULL, zoneid,
9384 				    NULL, MATCH_IRE_GW, ipst);
9385 			}
9386 		}
9387 		break;
9388 	}
9389 	case AF_INET: {
9390 		ipaddr_t v4addr;
9391 
9392 		v4addr = sin->sin_addr.s_addr;
9393 		if (!CLASSD(v4addr)) {
9394 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9395 			    NULL, NULL, zoneid, NULL,
9396 			    MATCH_IRE_GW, ipst);
9397 		}
9398 		break;
9399 	}
9400 	default:
9401 		return (EAFNOSUPPORT);
9402 	}
9403 	sia->sa_res = 0;
9404 	if (ire != NULL) {
9405 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9406 		    IRE_LOCAL|IRE_LOOPBACK)) {
9407 			sia->sa_res = 1;
9408 		}
9409 		ire_refrele(ire);
9410 	}
9411 	return (0);
9412 }
9413 
9414 /*
9415  * TBD: implement when kernel maintaines a list of site prefixes.
9416  */
9417 /* ARGSUSED */
9418 int
9419 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9420     ip_ioctl_cmd_t *ipip, void *ifreq)
9421 {
9422 	return (ENXIO);
9423 }
9424 
9425 /* ARGSUSED */
9426 int
9427 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9428     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9429 {
9430 	ill_t  		*ill;
9431 	mblk_t		*mp1;
9432 	conn_t		*connp;
9433 	boolean_t	success;
9434 
9435 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9436 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9437 	/* ioctl comes down on an conn */
9438 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9439 	connp = Q_TO_CONN(q);
9440 
9441 	mp->b_datap->db_type = M_IOCTL;
9442 
9443 	/*
9444 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9445 	 * The original mp contains contaminated b_next values due to 'mi',
9446 	 * which is needed to do the mi_copy_done. Unfortunately if we
9447 	 * send down the original mblk itself and if we are popped due to an
9448 	 * an unplumb before the response comes back from tunnel,
9449 	 * the streamhead (which does a freemsg) will see this contaminated
9450 	 * message and the assertion in freemsg about non-null b_next/b_prev
9451 	 * will panic a DEBUG kernel.
9452 	 */
9453 	mp1 = copymsg(mp);
9454 	if (mp1 == NULL)
9455 		return (ENOMEM);
9456 
9457 	ill = ipif->ipif_ill;
9458 	mutex_enter(&connp->conn_lock);
9459 	mutex_enter(&ill->ill_lock);
9460 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9461 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9462 		    mp, 0);
9463 	} else {
9464 		success = ill_pending_mp_add(ill, connp, mp);
9465 	}
9466 	mutex_exit(&ill->ill_lock);
9467 	mutex_exit(&connp->conn_lock);
9468 
9469 	if (success) {
9470 		ip1dbg(("sending down tunparam request "));
9471 		putnext(ill->ill_wq, mp1);
9472 		return (EINPROGRESS);
9473 	} else {
9474 		/* The conn has started closing */
9475 		freemsg(mp1);
9476 		return (EINTR);
9477 	}
9478 }
9479 
9480 /*
9481  * ARP IOCTLs.
9482  * How does IP get in the business of fronting ARP configuration/queries?
9483  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9484  * are by tradition passed in through a datagram socket.  That lands in IP.
9485  * As it happens, this is just as well since the interface is quite crude in
9486  * that it passes in no information about protocol or hardware types, or
9487  * interface association.  After making the protocol assumption, IP is in
9488  * the position to look up the name of the ILL, which ARP will need, and
9489  * format a request that can be handled by ARP.  The request is passed up
9490  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9491  * back a response.  ARP supports its own set of more general IOCTLs, in
9492  * case anyone is interested.
9493  */
9494 /* ARGSUSED */
9495 int
9496 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9497     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9498 {
9499 	mblk_t *mp1;
9500 	mblk_t *mp2;
9501 	mblk_t *pending_mp;
9502 	ipaddr_t ipaddr;
9503 	area_t *area;
9504 	struct iocblk *iocp;
9505 	conn_t *connp;
9506 	struct arpreq *ar;
9507 	struct xarpreq *xar;
9508 	int flags, alength;
9509 	char *lladdr;
9510 	ip_stack_t	*ipst;
9511 	ill_t *ill = ipif->ipif_ill;
9512 	boolean_t if_arp_ioctl = B_FALSE;
9513 
9514 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9515 	connp = Q_TO_CONN(q);
9516 	ipst = connp->conn_netstack->netstack_ip;
9517 
9518 	if (ipip->ipi_cmd_type == XARP_CMD) {
9519 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9520 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9521 		ar = NULL;
9522 
9523 		flags = xar->xarp_flags;
9524 		lladdr = LLADDR(&xar->xarp_ha);
9525 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9526 		/*
9527 		 * Validate against user's link layer address length
9528 		 * input and name and addr length limits.
9529 		 */
9530 		alength = ill->ill_phys_addr_length;
9531 		if (ipip->ipi_cmd == SIOCSXARP) {
9532 			if (alength != xar->xarp_ha.sdl_alen ||
9533 			    (alength + xar->xarp_ha.sdl_nlen >
9534 			    sizeof (xar->xarp_ha.sdl_data)))
9535 				return (EINVAL);
9536 		}
9537 	} else {
9538 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9539 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9540 		xar = NULL;
9541 
9542 		flags = ar->arp_flags;
9543 		lladdr = ar->arp_ha.sa_data;
9544 		/*
9545 		 * Theoretically, the sa_family could tell us what link
9546 		 * layer type this operation is trying to deal with. By
9547 		 * common usage AF_UNSPEC means ethernet. We'll assume
9548 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9549 		 * for now. Our new SIOC*XARP ioctls can be used more
9550 		 * generally.
9551 		 *
9552 		 * If the underlying media happens to have a non 6 byte
9553 		 * address, arp module will fail set/get, but the del
9554 		 * operation will succeed.
9555 		 */
9556 		alength = 6;
9557 		if ((ipip->ipi_cmd != SIOCDARP) &&
9558 		    (alength != ill->ill_phys_addr_length)) {
9559 			return (EINVAL);
9560 		}
9561 	}
9562 
9563 	/*
9564 	 * We are going to pass up to ARP a packet chain that looks
9565 	 * like:
9566 	 *
9567 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9568 	 *
9569 	 * Get a copy of the original IOCTL mblk to head the chain,
9570 	 * to be sent up (in mp1). Also get another copy to store
9571 	 * in the ill_pending_mp list, for matching the response
9572 	 * when it comes back from ARP.
9573 	 */
9574 	mp1 = copyb(mp);
9575 	pending_mp = copymsg(mp);
9576 	if (mp1 == NULL || pending_mp == NULL) {
9577 		if (mp1 != NULL)
9578 			freeb(mp1);
9579 		if (pending_mp != NULL)
9580 			inet_freemsg(pending_mp);
9581 		return (ENOMEM);
9582 	}
9583 
9584 	ipaddr = sin->sin_addr.s_addr;
9585 
9586 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9587 	    (caddr_t)&ipaddr);
9588 	if (mp2 == NULL) {
9589 		freeb(mp1);
9590 		inet_freemsg(pending_mp);
9591 		return (ENOMEM);
9592 	}
9593 	/* Put together the chain. */
9594 	mp1->b_cont = mp2;
9595 	mp1->b_datap->db_type = M_IOCTL;
9596 	mp2->b_cont = mp;
9597 	mp2->b_datap->db_type = M_DATA;
9598 
9599 	iocp = (struct iocblk *)mp1->b_rptr;
9600 
9601 	/*
9602 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9603 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9604 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9605 	 * ioc_count field; set ioc_count to be correct.
9606 	 */
9607 	iocp->ioc_count = MBLKL(mp1->b_cont);
9608 
9609 	/*
9610 	 * Set the proper command in the ARP message.
9611 	 * Convert the SIOC{G|S|D}ARP calls into our
9612 	 * AR_ENTRY_xxx calls.
9613 	 */
9614 	area = (area_t *)mp2->b_rptr;
9615 	switch (iocp->ioc_cmd) {
9616 	case SIOCDARP:
9617 	case SIOCDXARP:
9618 		/*
9619 		 * We defer deleting the corresponding IRE until
9620 		 * we return from arp.
9621 		 */
9622 		area->area_cmd = AR_ENTRY_DELETE;
9623 		area->area_proto_mask_offset = 0;
9624 		break;
9625 	case SIOCGARP:
9626 	case SIOCGXARP:
9627 		area->area_cmd = AR_ENTRY_SQUERY;
9628 		area->area_proto_mask_offset = 0;
9629 		break;
9630 	case SIOCSARP:
9631 	case SIOCSXARP:
9632 		/*
9633 		 * Delete the corresponding ire to make sure IP will
9634 		 * pick up any change from arp.
9635 		 */
9636 		if (!if_arp_ioctl) {
9637 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9638 		} else {
9639 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9640 			if (ipif != NULL) {
9641 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9642 				    ipst);
9643 				ipif_refrele(ipif);
9644 			}
9645 		}
9646 		break;
9647 	}
9648 	iocp->ioc_cmd = area->area_cmd;
9649 
9650 	/*
9651 	 * Fill in the rest of the ARP operation fields.
9652 	 */
9653 	area->area_hw_addr_length = alength;
9654 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9655 
9656 	/* Translate the flags. */
9657 	if (flags & ATF_PERM)
9658 		area->area_flags |= ACE_F_PERMANENT;
9659 	if (flags & ATF_PUBL)
9660 		area->area_flags |= ACE_F_PUBLISH;
9661 	if (flags & ATF_AUTHORITY)
9662 		area->area_flags |= ACE_F_AUTHORITY;
9663 
9664 	/*
9665 	 * Before sending 'mp' to ARP, we have to clear the b_next
9666 	 * and b_prev. Otherwise if STREAMS encounters such a message
9667 	 * in freemsg(), (because ARP can close any time) it can cause
9668 	 * a panic. But mi code needs the b_next and b_prev values of
9669 	 * mp->b_cont, to complete the ioctl. So we store it here
9670 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9671 	 * when the response comes down from ARP.
9672 	 */
9673 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9674 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9675 	mp->b_cont->b_next = NULL;
9676 	mp->b_cont->b_prev = NULL;
9677 
9678 	mutex_enter(&connp->conn_lock);
9679 	mutex_enter(&ill->ill_lock);
9680 	/* conn has not yet started closing, hence this can't fail */
9681 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9682 	mutex_exit(&ill->ill_lock);
9683 	mutex_exit(&connp->conn_lock);
9684 
9685 	/*
9686 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9687 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9688 	 */
9689 	putnext(ill->ill_rq, mp1);
9690 	return (EINPROGRESS);
9691 }
9692 
9693 /*
9694  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9695  * the associated sin and refhold and return the associated ipif via `ci'.
9696  */
9697 int
9698 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9699     cmd_info_t *ci, ipsq_func_t func)
9700 {
9701 	mblk_t	*mp1;
9702 	int	err;
9703 	sin_t	*sin;
9704 	conn_t	*connp;
9705 	ipif_t	*ipif;
9706 	ire_t	*ire = NULL;
9707 	ill_t	*ill = NULL;
9708 	boolean_t exists;
9709 	ip_stack_t *ipst;
9710 	struct arpreq *ar;
9711 	struct xarpreq *xar;
9712 	struct sockaddr_dl *sdl;
9713 
9714 	/* ioctl comes down on a conn */
9715 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9716 	connp = Q_TO_CONN(q);
9717 	if (connp->conn_af_isv6)
9718 		return (ENXIO);
9719 
9720 	ipst = connp->conn_netstack->netstack_ip;
9721 
9722 	/* Verified in ip_wput_nondata */
9723 	mp1 = mp->b_cont->b_cont;
9724 
9725 	if (ipip->ipi_cmd_type == XARP_CMD) {
9726 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9727 		xar = (struct xarpreq *)mp1->b_rptr;
9728 		sin = (sin_t *)&xar->xarp_pa;
9729 		sdl = &xar->xarp_ha;
9730 
9731 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9732 			return (ENXIO);
9733 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9734 			return (EINVAL);
9735 	} else {
9736 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9737 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9738 		ar = (struct arpreq *)mp1->b_rptr;
9739 		sin = (sin_t *)&ar->arp_pa;
9740 	}
9741 
9742 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9743 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9744 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9745 		    mp, func, &err, ipst);
9746 		if (ipif == NULL)
9747 			return (err);
9748 		if (ipif->ipif_id != 0 ||
9749 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9750 			ipif_refrele(ipif);
9751 			return (ENXIO);
9752 		}
9753 	} else {
9754 		/*
9755 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9756 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9757 		 * case, a simple forwarding table lookup will return the
9758 		 * IRE_IF_RESOLVER for the first interface in the group, which
9759 		 * might not be the interface on which the requested IP
9760 		 * address was resolved due to the ill selection algorithm
9761 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9762 		 * lookup first: if the IRE cache entry for the IP address is
9763 		 * still there, it will contain the ill pointer for the right
9764 		 * interface, so we use that. If the cache entry has been
9765 		 * flushed, we fall back to the forwarding table lookup. This
9766 		 * should be rare enough since IRE cache entries have a longer
9767 		 * life expectancy than ARP cache entries.
9768 		 */
9769 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9770 		    ipst);
9771 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9772 		    ((ill = ire_to_ill(ire)) == NULL) ||
9773 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9774 			if (ire != NULL)
9775 				ire_refrele(ire);
9776 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9777 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9778 			    NULL, MATCH_IRE_TYPE, ipst);
9779 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9780 
9781 				if (ire != NULL)
9782 					ire_refrele(ire);
9783 				return (ENXIO);
9784 			}
9785 		}
9786 		ASSERT(ire != NULL && ill != NULL);
9787 		ipif = ill->ill_ipif;
9788 		ipif_refhold(ipif);
9789 		ire_refrele(ire);
9790 	}
9791 	ci->ci_sin = sin;
9792 	ci->ci_ipif = ipif;
9793 	return (0);
9794 }
9795 
9796 /*
9797  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9798  * atomically set/clear the muxids. Also complete the ioctl by acking or
9799  * naking it.  Note that the code is structured such that the link type,
9800  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9801  * its clones use the persistent link, while pppd(1M) and perhaps many
9802  * other daemons may use non-persistent link.  When combined with some
9803  * ill_t states, linking and unlinking lower streams may be used as
9804  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9805  */
9806 /* ARGSUSED */
9807 void
9808 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9809 {
9810 	mblk_t		*mp1, *mp2;
9811 	struct linkblk	*li;
9812 	struct ipmx_s	*ipmxp;
9813 	ill_t		*ill;
9814 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9815 	int		err = 0;
9816 	boolean_t	entered_ipsq = B_FALSE;
9817 	boolean_t	islink;
9818 	ip_stack_t	*ipst;
9819 
9820 	if (CONN_Q(q))
9821 		ipst = CONNQ_TO_IPST(q);
9822 	else
9823 		ipst = ILLQ_TO_IPST(q);
9824 
9825 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9826 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9827 
9828 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9829 
9830 	mp1 = mp->b_cont;	/* This is the linkblk info */
9831 	li = (struct linkblk *)mp1->b_rptr;
9832 
9833 	/*
9834 	 * ARP has added this special mblk, and the utility is asking us
9835 	 * to perform consistency checks, and also atomically set the
9836 	 * muxid. Ifconfig is an example.  It achieves this by using
9837 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9838 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9839 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9840 	 * and other comments in this routine for more details.
9841 	 */
9842 	mp2 = mp1->b_cont;	/* This is added by ARP */
9843 
9844 	/*
9845 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9846 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9847 	 * get the special mblk above.  For backward compatibility, we
9848 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9849 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9850 	 * not atomic, and can leave the streams unplumbable if the utility
9851 	 * is interrupted before it does the SIOCSLIFMUXID.
9852 	 */
9853 	if (mp2 == NULL) {
9854 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9855 		if (err == EINPROGRESS)
9856 			return;
9857 		goto done;
9858 	}
9859 
9860 	/*
9861 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9862 	 * ARP has appended this last mblk to tell us whether the lower stream
9863 	 * is an arp-dev stream or an IP module stream.
9864 	 */
9865 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9866 	if (ipmxp->ipmx_arpdev_stream) {
9867 		/*
9868 		 * The lower stream is the arp-dev stream.
9869 		 */
9870 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9871 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9872 		if (ill == NULL) {
9873 			if (err == EINPROGRESS)
9874 				return;
9875 			err = EINVAL;
9876 			goto done;
9877 		}
9878 
9879 		if (ipsq == NULL) {
9880 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9881 			    NEW_OP, B_TRUE);
9882 			if (ipsq == NULL) {
9883 				ill_refrele(ill);
9884 				return;
9885 			}
9886 			entered_ipsq = B_TRUE;
9887 		}
9888 		ASSERT(IAM_WRITER_ILL(ill));
9889 		ill_refrele(ill);
9890 
9891 		/*
9892 		 * To ensure consistency between IP and ARP, the following
9893 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9894 		 * This is because the muxid's are stored in the IP stream on
9895 		 * the ill.
9896 		 *
9897 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9898 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9899 		 * not yet plinked, and it also checks that the corresponding
9900 		 * IP stream is already plinked.
9901 		 *
9902 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9903 		 * punlinking the IP stream. IP does not allow punlink of the
9904 		 * IP stream unless the arp stream has been punlinked.
9905 		 */
9906 		if ((islink &&
9907 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9908 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9909 			err = EINVAL;
9910 			goto done;
9911 		}
9912 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9913 	} else {
9914 		/*
9915 		 * The lower stream is probably an IP module stream.  Do
9916 		 * consistency checking.
9917 		 */
9918 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9919 		if (err == EINPROGRESS)
9920 			return;
9921 	}
9922 done:
9923 	if (err == 0)
9924 		miocack(q, mp, 0, 0);
9925 	else
9926 		miocnak(q, mp, 0, err);
9927 
9928 	/* Conn was refheld in ip_sioctl_copyin_setup */
9929 	if (CONN_Q(q))
9930 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9931 	if (entered_ipsq)
9932 		ipsq_exit(ipsq);
9933 }
9934 
9935 /*
9936  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9937  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9938  * module stream).  If `doconsist' is set, then do the extended consistency
9939  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9940  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9941  * an error code on failure.
9942  */
9943 static int
9944 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9945     struct linkblk *li, boolean_t doconsist)
9946 {
9947 	ill_t  		*ill;
9948 	queue_t		*ipwq, *dwq;
9949 	const char	*name;
9950 	struct qinit	*qinfo;
9951 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9952 	boolean_t	entered_ipsq = B_FALSE;
9953 
9954 	/*
9955 	 * Walk the lower stream to verify it's the IP module stream.
9956 	 * The IP module is identified by its name, wput function,
9957 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9958 	 * (li->l_qbot) will not vanish until this ioctl completes.
9959 	 */
9960 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9961 		qinfo = ipwq->q_qinfo;
9962 		name = qinfo->qi_minfo->mi_idname;
9963 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9964 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9965 			break;
9966 		}
9967 	}
9968 
9969 	/*
9970 	 * If this isn't an IP module stream, bail.
9971 	 */
9972 	if (ipwq == NULL)
9973 		return (0);
9974 
9975 	ill = ipwq->q_ptr;
9976 	ASSERT(ill != NULL);
9977 
9978 	if (ipsq == NULL) {
9979 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9980 		    NEW_OP, B_TRUE);
9981 		if (ipsq == NULL)
9982 			return (EINPROGRESS);
9983 		entered_ipsq = B_TRUE;
9984 	}
9985 	ASSERT(IAM_WRITER_ILL(ill));
9986 
9987 	if (doconsist) {
9988 		/*
9989 		 * Consistency checking requires that I_{P}LINK occurs
9990 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9991 		 * occurs prior to clearing ill_arp_muxid.
9992 		 */
9993 		if ((islink && ill->ill_ip_muxid != 0) ||
9994 		    (!islink && ill->ill_arp_muxid != 0)) {
9995 			if (entered_ipsq)
9996 				ipsq_exit(ipsq);
9997 			return (EINVAL);
9998 		}
9999 	}
10000 
10001 	/*
10002 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10003 	 * the read queue of the module immediately below IP in the ill.
10004 	 * These are used during the capability negotiation below.
10005 	 */
10006 	ill->ill_lmod_rq = NULL;
10007 	ill->ill_lmod_cnt = 0;
10008 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10009 		ill->ill_lmod_rq = RD(dwq);
10010 		for (; dwq != NULL; dwq = dwq->q_next)
10011 			ill->ill_lmod_cnt++;
10012 	}
10013 
10014 	if (doconsist)
10015 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10016 
10017 	/*
10018 	 * If there's at least one up ipif on this ill, then we're bound to
10019 	 * the underlying driver via DLPI.  In that case, renegotiate
10020 	 * capabilities to account for any possible change in modules
10021 	 * interposed between IP and the driver.
10022 	 */
10023 	if (ill->ill_ipif_up_count > 0) {
10024 		if (islink)
10025 			ill_capability_probe(ill);
10026 		else
10027 			ill_capability_reset(ill);
10028 	}
10029 
10030 	if (entered_ipsq)
10031 		ipsq_exit(ipsq);
10032 
10033 	return (0);
10034 }
10035 
10036 /*
10037  * Search the ioctl command in the ioctl tables and return a pointer
10038  * to the ioctl command information. The ioctl command tables are
10039  * static and fully populated at compile time.
10040  */
10041 ip_ioctl_cmd_t *
10042 ip_sioctl_lookup(int ioc_cmd)
10043 {
10044 	int index;
10045 	ip_ioctl_cmd_t *ipip;
10046 	ip_ioctl_cmd_t *ipip_end;
10047 
10048 	if (ioc_cmd == IPI_DONTCARE)
10049 		return (NULL);
10050 
10051 	/*
10052 	 * Do a 2 step search. First search the indexed table
10053 	 * based on the least significant byte of the ioctl cmd.
10054 	 * If we don't find a match, then search the misc table
10055 	 * serially.
10056 	 */
10057 	index = ioc_cmd & 0xFF;
10058 	if (index < ip_ndx_ioctl_count) {
10059 		ipip = &ip_ndx_ioctl_table[index];
10060 		if (ipip->ipi_cmd == ioc_cmd) {
10061 			/* Found a match in the ndx table */
10062 			return (ipip);
10063 		}
10064 	}
10065 
10066 	/* Search the misc table */
10067 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10068 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10069 		if (ipip->ipi_cmd == ioc_cmd)
10070 			/* Found a match in the misc table */
10071 			return (ipip);
10072 	}
10073 
10074 	return (NULL);
10075 }
10076 
10077 /*
10078  * Wrapper function for resuming deferred ioctl processing
10079  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10080  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10081  */
10082 /* ARGSUSED */
10083 void
10084 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10085     void *dummy_arg)
10086 {
10087 	ip_sioctl_copyin_setup(q, mp);
10088 }
10089 
10090 /*
10091  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10092  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10093  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10094  * We establish here the size of the block to be copied in.  mi_copyin
10095  * arranges for this to happen, an processing continues in ip_wput with
10096  * an M_IOCDATA message.
10097  */
10098 void
10099 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10100 {
10101 	int	copyin_size;
10102 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10103 	ip_ioctl_cmd_t *ipip;
10104 	cred_t *cr;
10105 	ip_stack_t	*ipst;
10106 
10107 	if (CONN_Q(q))
10108 		ipst = CONNQ_TO_IPST(q);
10109 	else
10110 		ipst = ILLQ_TO_IPST(q);
10111 
10112 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10113 	if (ipip == NULL) {
10114 		/*
10115 		 * The ioctl is not one we understand or own.
10116 		 * Pass it along to be processed down stream,
10117 		 * if this is a module instance of IP, else nak
10118 		 * the ioctl.
10119 		 */
10120 		if (q->q_next == NULL) {
10121 			goto nak;
10122 		} else {
10123 			putnext(q, mp);
10124 			return;
10125 		}
10126 	}
10127 
10128 	/*
10129 	 * If this is deferred, then we will do all the checks when we
10130 	 * come back.
10131 	 */
10132 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10133 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10134 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10135 		return;
10136 	}
10137 
10138 	/*
10139 	 * Only allow a very small subset of IP ioctls on this stream if
10140 	 * IP is a module and not a driver. Allowing ioctls to be processed
10141 	 * in this case may cause assert failures or data corruption.
10142 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10143 	 * ioctls allowed on an IP module stream, after which this stream
10144 	 * normally becomes a multiplexor (at which time the stream head
10145 	 * will fail all ioctls).
10146 	 */
10147 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10148 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10149 			/*
10150 			 * Pass common Streams ioctls which the IP
10151 			 * module does not own or consume along to
10152 			 * be processed down stream.
10153 			 */
10154 			putnext(q, mp);
10155 			return;
10156 		} else {
10157 			goto nak;
10158 		}
10159 	}
10160 
10161 	/* Make sure we have ioctl data to process. */
10162 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10163 		goto nak;
10164 
10165 	/*
10166 	 * Prefer dblk credential over ioctl credential; some synthesized
10167 	 * ioctls have kcred set because there's no way to crhold()
10168 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10169 	 * the framework; the caller of ioctl needs to hold the reference
10170 	 * for the duration of the call).
10171 	 */
10172 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10173 
10174 	/* Make sure normal users don't send down privileged ioctls */
10175 	if ((ipip->ipi_flags & IPI_PRIV) &&
10176 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10177 		/* We checked the privilege earlier but log it here */
10178 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10179 		return;
10180 	}
10181 
10182 	/*
10183 	 * The ioctl command tables can only encode fixed length
10184 	 * ioctl data. If the length is variable, the table will
10185 	 * encode the length as zero. Such special cases are handled
10186 	 * below in the switch.
10187 	 */
10188 	if (ipip->ipi_copyin_size != 0) {
10189 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10190 		return;
10191 	}
10192 
10193 	switch (iocp->ioc_cmd) {
10194 	case O_SIOCGIFCONF:
10195 	case SIOCGIFCONF:
10196 		/*
10197 		 * This IOCTL is hilarious.  See comments in
10198 		 * ip_sioctl_get_ifconf for the story.
10199 		 */
10200 		if (iocp->ioc_count == TRANSPARENT)
10201 			copyin_size = SIZEOF_STRUCT(ifconf,
10202 			    iocp->ioc_flag);
10203 		else
10204 			copyin_size = iocp->ioc_count;
10205 		mi_copyin(q, mp, NULL, copyin_size);
10206 		return;
10207 
10208 	case O_SIOCGLIFCONF:
10209 	case SIOCGLIFCONF:
10210 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10211 		mi_copyin(q, mp, NULL, copyin_size);
10212 		return;
10213 
10214 	case SIOCGLIFSRCOF:
10215 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10216 		mi_copyin(q, mp, NULL, copyin_size);
10217 		return;
10218 	case SIOCGIP6ADDRPOLICY:
10219 		ip_sioctl_ip6addrpolicy(q, mp);
10220 		ip6_asp_table_refrele(ipst);
10221 		return;
10222 
10223 	case SIOCSIP6ADDRPOLICY:
10224 		ip_sioctl_ip6addrpolicy(q, mp);
10225 		return;
10226 
10227 	case SIOCGDSTINFO:
10228 		ip_sioctl_dstinfo(q, mp);
10229 		ip6_asp_table_refrele(ipst);
10230 		return;
10231 
10232 	case I_PLINK:
10233 	case I_PUNLINK:
10234 	case I_LINK:
10235 	case I_UNLINK:
10236 		/*
10237 		 * We treat non-persistent link similarly as the persistent
10238 		 * link case, in terms of plumbing/unplumbing, as well as
10239 		 * dynamic re-plumbing events indicator.  See comments
10240 		 * in ip_sioctl_plink() for more.
10241 		 *
10242 		 * Request can be enqueued in the 'ipsq' while waiting
10243 		 * to become exclusive. So bump up the conn ref.
10244 		 */
10245 		if (CONN_Q(q))
10246 			CONN_INC_REF(Q_TO_CONN(q));
10247 		ip_sioctl_plink(NULL, q, mp, NULL);
10248 		return;
10249 
10250 	case ND_GET:
10251 	case ND_SET:
10252 		/*
10253 		 * Use of the nd table requires holding the reader lock.
10254 		 * Modifying the nd table thru nd_load/nd_unload requires
10255 		 * the writer lock.
10256 		 */
10257 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10258 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10259 			rw_exit(&ipst->ips_ip_g_nd_lock);
10260 
10261 			if (iocp->ioc_error)
10262 				iocp->ioc_count = 0;
10263 			mp->b_datap->db_type = M_IOCACK;
10264 			qreply(q, mp);
10265 			return;
10266 		}
10267 		rw_exit(&ipst->ips_ip_g_nd_lock);
10268 		/*
10269 		 * We don't understand this subioctl of ND_GET / ND_SET.
10270 		 * Maybe intended for some driver / module below us
10271 		 */
10272 		if (q->q_next) {
10273 			putnext(q, mp);
10274 		} else {
10275 			iocp->ioc_error = ENOENT;
10276 			mp->b_datap->db_type = M_IOCNAK;
10277 			iocp->ioc_count = 0;
10278 			qreply(q, mp);
10279 		}
10280 		return;
10281 
10282 	case IP_IOCTL:
10283 		ip_wput_ioctl(q, mp);
10284 		return;
10285 	default:
10286 		cmn_err(CE_PANIC, "should not happen ");
10287 	}
10288 nak:
10289 	if (mp->b_cont != NULL) {
10290 		freemsg(mp->b_cont);
10291 		mp->b_cont = NULL;
10292 	}
10293 	iocp->ioc_error = EINVAL;
10294 	mp->b_datap->db_type = M_IOCNAK;
10295 	iocp->ioc_count = 0;
10296 	qreply(q, mp);
10297 }
10298 
10299 /* ip_wput hands off ARP IOCTL responses to us */
10300 void
10301 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10302 {
10303 	struct arpreq *ar;
10304 	struct xarpreq *xar;
10305 	area_t	*area;
10306 	mblk_t	*area_mp;
10307 	struct iocblk *iocp;
10308 	mblk_t	*orig_ioc_mp, *tmp;
10309 	struct iocblk	*orig_iocp;
10310 	ill_t *ill;
10311 	conn_t *connp = NULL;
10312 	uint_t ioc_id;
10313 	mblk_t *pending_mp;
10314 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10315 	int *flagsp;
10316 	char *storage = NULL;
10317 	sin_t *sin;
10318 	ipaddr_t addr;
10319 	int err;
10320 	ip_stack_t *ipst;
10321 
10322 	ill = q->q_ptr;
10323 	ASSERT(ill != NULL);
10324 	ipst = ill->ill_ipst;
10325 
10326 	/*
10327 	 * We should get back from ARP a packet chain that looks like:
10328 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10329 	 */
10330 	if (!(area_mp = mp->b_cont) ||
10331 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10332 	    !(orig_ioc_mp = area_mp->b_cont) ||
10333 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10334 		freemsg(mp);
10335 		return;
10336 	}
10337 
10338 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10339 
10340 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10341 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10342 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10343 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10344 		x_arp_ioctl = B_TRUE;
10345 		xar = (struct xarpreq *)tmp->b_rptr;
10346 		sin = (sin_t *)&xar->xarp_pa;
10347 		flagsp = &xar->xarp_flags;
10348 		storage = xar->xarp_ha.sdl_data;
10349 		if (xar->xarp_ha.sdl_nlen != 0)
10350 			ifx_arp_ioctl = B_TRUE;
10351 	} else {
10352 		ar = (struct arpreq *)tmp->b_rptr;
10353 		sin = (sin_t *)&ar->arp_pa;
10354 		flagsp = &ar->arp_flags;
10355 		storage = ar->arp_ha.sa_data;
10356 	}
10357 
10358 	iocp = (struct iocblk *)mp->b_rptr;
10359 
10360 	/*
10361 	 * Pick out the originating queue based on the ioc_id.
10362 	 */
10363 	ioc_id = iocp->ioc_id;
10364 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10365 	if (pending_mp == NULL) {
10366 		ASSERT(connp == NULL);
10367 		inet_freemsg(mp);
10368 		return;
10369 	}
10370 	ASSERT(connp != NULL);
10371 	q = CONNP_TO_WQ(connp);
10372 
10373 	/* Uncouple the internally generated IOCTL from the original one */
10374 	area = (area_t *)area_mp->b_rptr;
10375 	area_mp->b_cont = NULL;
10376 
10377 	/*
10378 	 * Restore the b_next and b_prev used by mi code. This is needed
10379 	 * to complete the ioctl using mi* functions. We stored them in
10380 	 * the pending mp prior to sending the request to ARP.
10381 	 */
10382 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10383 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10384 	inet_freemsg(pending_mp);
10385 
10386 	/*
10387 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10388 	 * Catch the case where there is an IRE_CACHE by no entry in the
10389 	 * arp table.
10390 	 */
10391 	addr = sin->sin_addr.s_addr;
10392 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10393 		ire_t			*ire;
10394 		dl_unitdata_req_t	*dlup;
10395 		mblk_t			*llmp;
10396 		int			addr_len;
10397 		ill_t			*ipsqill = NULL;
10398 
10399 		if (ifx_arp_ioctl) {
10400 			/*
10401 			 * There's no need to lookup the ill, since
10402 			 * we've already done that when we started
10403 			 * processing the ioctl and sent the message
10404 			 * to ARP on that ill.  So use the ill that
10405 			 * is stored in q->q_ptr.
10406 			 */
10407 			ipsqill = ill;
10408 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10409 			    ipsqill->ill_ipif, ALL_ZONES,
10410 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10411 		} else {
10412 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10413 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10414 			if (ire != NULL)
10415 				ipsqill = ire_to_ill(ire);
10416 		}
10417 
10418 		if ((x_arp_ioctl) && (ipsqill != NULL))
10419 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10420 
10421 		if (ire != NULL) {
10422 			/*
10423 			 * Since the ire obtained from cachetable is used for
10424 			 * mac addr copying below, treat an incomplete ire as if
10425 			 * as if we never found it.
10426 			 */
10427 			if (ire->ire_nce != NULL &&
10428 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10429 				ire_refrele(ire);
10430 				ire = NULL;
10431 				ipsqill = NULL;
10432 				goto errack;
10433 			}
10434 			*flagsp = ATF_INUSE;
10435 			llmp = (ire->ire_nce != NULL ?
10436 			    ire->ire_nce->nce_res_mp : NULL);
10437 			if (llmp != NULL && ipsqill != NULL) {
10438 				uchar_t *macaddr;
10439 
10440 				addr_len = ipsqill->ill_phys_addr_length;
10441 				if (x_arp_ioctl && ((addr_len +
10442 				    ipsqill->ill_name_length) >
10443 				    sizeof (xar->xarp_ha.sdl_data))) {
10444 					ire_refrele(ire);
10445 					freemsg(mp);
10446 					ip_ioctl_finish(q, orig_ioc_mp,
10447 					    EINVAL, NO_COPYOUT, NULL);
10448 					return;
10449 				}
10450 				*flagsp |= ATF_COM;
10451 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10452 				if (ipsqill->ill_sap_length < 0)
10453 					macaddr = llmp->b_rptr +
10454 					    dlup->dl_dest_addr_offset;
10455 				else
10456 					macaddr = llmp->b_rptr +
10457 					    dlup->dl_dest_addr_offset +
10458 					    ipsqill->ill_sap_length;
10459 				/*
10460 				 * For SIOCGARP, MAC address length
10461 				 * validation has already been done
10462 				 * before the ioctl was issued to ARP to
10463 				 * allow it to progress only on 6 byte
10464 				 * addressable (ethernet like) media. Thus
10465 				 * the mac address copying can not overwrite
10466 				 * the sa_data area below.
10467 				 */
10468 				bcopy(macaddr, storage, addr_len);
10469 			}
10470 			/* Ditch the internal IOCTL. */
10471 			freemsg(mp);
10472 			ire_refrele(ire);
10473 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10474 			return;
10475 		}
10476 	}
10477 
10478 	/*
10479 	 * Delete the coresponding IRE_CACHE if any.
10480 	 * Reset the error if there was one (in case there was no entry
10481 	 * in arp.)
10482 	 */
10483 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10484 		ipif_t *ipintf = NULL;
10485 
10486 		if (ifx_arp_ioctl) {
10487 			/*
10488 			 * There's no need to lookup the ill, since
10489 			 * we've already done that when we started
10490 			 * processing the ioctl and sent the message
10491 			 * to ARP on that ill.  So use the ill that
10492 			 * is stored in q->q_ptr.
10493 			 */
10494 			ipintf = ill->ill_ipif;
10495 		}
10496 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10497 			/*
10498 			 * The address in "addr" may be an entry for a
10499 			 * router. If that's true, then any off-net
10500 			 * IRE_CACHE entries that go through the router
10501 			 * with address "addr" must be clobbered. Use
10502 			 * ire_walk to achieve this goal.
10503 			 */
10504 			if (ifx_arp_ioctl)
10505 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10506 				    ire_delete_cache_gw, (char *)&addr, ill);
10507 			else
10508 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10509 				    ALL_ZONES, ipst);
10510 			iocp->ioc_error = 0;
10511 		}
10512 	}
10513 errack:
10514 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10515 		err = iocp->ioc_error;
10516 		freemsg(mp);
10517 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10518 		return;
10519 	}
10520 
10521 	/*
10522 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10523 	 * the area_t into the struct {x}arpreq.
10524 	 */
10525 	if (x_arp_ioctl) {
10526 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10527 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10528 		    sizeof (xar->xarp_ha.sdl_data)) {
10529 			freemsg(mp);
10530 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10531 			    NULL);
10532 			return;
10533 		}
10534 	}
10535 	*flagsp = ATF_INUSE;
10536 	if (area->area_flags & ACE_F_PERMANENT)
10537 		*flagsp |= ATF_PERM;
10538 	if (area->area_flags & ACE_F_PUBLISH)
10539 		*flagsp |= ATF_PUBL;
10540 	if (area->area_flags & ACE_F_AUTHORITY)
10541 		*flagsp |= ATF_AUTHORITY;
10542 	if (area->area_hw_addr_length != 0) {
10543 		*flagsp |= ATF_COM;
10544 		/*
10545 		 * For SIOCGARP, MAC address length validation has
10546 		 * already been done before the ioctl was issued to ARP
10547 		 * to allow it to progress only on 6 byte addressable
10548 		 * (ethernet like) media. Thus the mac address copying
10549 		 * can not overwrite the sa_data area below.
10550 		 */
10551 		bcopy((char *)area + area->area_hw_addr_offset,
10552 		    storage, area->area_hw_addr_length);
10553 	}
10554 
10555 	/* Ditch the internal IOCTL. */
10556 	freemsg(mp);
10557 	/* Complete the original. */
10558 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10559 }
10560 
10561 /*
10562  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10563  * interface) create the next available logical interface for this
10564  * physical interface.
10565  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10566  * ipif with the specified name.
10567  *
10568  * If the address family is not AF_UNSPEC then set the address as well.
10569  *
10570  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10571  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10572  *
10573  * Executed as a writer on the ill or ill group.
10574  * So no lock is needed to traverse the ipif chain, or examine the
10575  * phyint flags.
10576  */
10577 /* ARGSUSED */
10578 int
10579 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10580     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10581 {
10582 	mblk_t	*mp1;
10583 	struct lifreq *lifr;
10584 	boolean_t	isv6;
10585 	boolean_t	exists;
10586 	char 	*name;
10587 	char	*endp;
10588 	char	*cp;
10589 	int	namelen;
10590 	ipif_t	*ipif;
10591 	long	id;
10592 	ipsq_t	*ipsq;
10593 	ill_t	*ill;
10594 	sin_t	*sin;
10595 	int	err = 0;
10596 	boolean_t found_sep = B_FALSE;
10597 	conn_t	*connp;
10598 	zoneid_t zoneid;
10599 	int	orig_ifindex = 0;
10600 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10601 
10602 	ASSERT(q->q_next == NULL);
10603 	ip1dbg(("ip_sioctl_addif\n"));
10604 	/* Existence of mp1 has been checked in ip_wput_nondata */
10605 	mp1 = mp->b_cont->b_cont;
10606 	/*
10607 	 * Null terminate the string to protect against buffer
10608 	 * overrun. String was generated by user code and may not
10609 	 * be trusted.
10610 	 */
10611 	lifr = (struct lifreq *)mp1->b_rptr;
10612 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10613 	name = lifr->lifr_name;
10614 	ASSERT(CONN_Q(q));
10615 	connp = Q_TO_CONN(q);
10616 	isv6 = connp->conn_af_isv6;
10617 	zoneid = connp->conn_zoneid;
10618 	namelen = mi_strlen(name);
10619 	if (namelen == 0)
10620 		return (EINVAL);
10621 
10622 	exists = B_FALSE;
10623 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10624 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10625 		/*
10626 		 * Allow creating lo0 using SIOCLIFADDIF.
10627 		 * can't be any other writer thread. So can pass null below
10628 		 * for the last 4 args to ipif_lookup_name.
10629 		 */
10630 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10631 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10632 		/* Prevent any further action */
10633 		if (ipif == NULL) {
10634 			return (ENOBUFS);
10635 		} else if (!exists) {
10636 			/* We created the ipif now and as writer */
10637 			ipif_refrele(ipif);
10638 			return (0);
10639 		} else {
10640 			ill = ipif->ipif_ill;
10641 			ill_refhold(ill);
10642 			ipif_refrele(ipif);
10643 		}
10644 	} else {
10645 		/* Look for a colon in the name. */
10646 		endp = &name[namelen];
10647 		for (cp = endp; --cp > name; ) {
10648 			if (*cp == IPIF_SEPARATOR_CHAR) {
10649 				found_sep = B_TRUE;
10650 				/*
10651 				 * Reject any non-decimal aliases for plumbing
10652 				 * of logical interfaces. Aliases with leading
10653 				 * zeroes are also rejected as they introduce
10654 				 * ambiguity in the naming of the interfaces.
10655 				 * Comparing with "0" takes care of all such
10656 				 * cases.
10657 				 */
10658 				if ((strncmp("0", cp+1, 1)) == 0)
10659 					return (EINVAL);
10660 
10661 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10662 				    id <= 0 || *endp != '\0') {
10663 					return (EINVAL);
10664 				}
10665 				*cp = '\0';
10666 				break;
10667 			}
10668 		}
10669 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10670 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10671 		if (found_sep)
10672 			*cp = IPIF_SEPARATOR_CHAR;
10673 		if (ill == NULL)
10674 			return (err);
10675 	}
10676 
10677 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10678 	    B_TRUE);
10679 
10680 	/*
10681 	 * Release the refhold due to the lookup, now that we are excl
10682 	 * or we are just returning
10683 	 */
10684 	ill_refrele(ill);
10685 
10686 	if (ipsq == NULL)
10687 		return (EINPROGRESS);
10688 
10689 	/*
10690 	 * If the interface is failed, inactive or offlined, look for a working
10691 	 * interface in the ill group and create the ipif there. If we can't
10692 	 * find a good interface, create the ipif anyway so that in.mpathd can
10693 	 * move it to the first repaired interface.
10694 	 */
10695 	if ((ill->ill_phyint->phyint_flags &
10696 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10697 	    ill->ill_phyint->phyint_groupname_len != 0) {
10698 		phyint_t *phyi;
10699 		char *groupname = ill->ill_phyint->phyint_groupname;
10700 
10701 		/*
10702 		 * We're looking for a working interface, but it doesn't matter
10703 		 * if it's up or down; so instead of following the group lists,
10704 		 * we look at each physical interface and compare the groupname.
10705 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10706 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10707 		 * Otherwise we create the ipif on the failed interface.
10708 		 */
10709 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10710 		phyi = avl_first(&ipst->ips_phyint_g_list->
10711 		    phyint_list_avl_by_index);
10712 		for (; phyi != NULL;
10713 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10714 		    phyint_list_avl_by_index,
10715 		    phyi, AVL_AFTER)) {
10716 			if (phyi->phyint_groupname_len == 0)
10717 				continue;
10718 			ASSERT(phyi->phyint_groupname != NULL);
10719 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10720 			    !(phyi->phyint_flags &
10721 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10722 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10723 			    (phyi->phyint_illv4 != NULL))) {
10724 				break;
10725 			}
10726 		}
10727 		rw_exit(&ipst->ips_ill_g_lock);
10728 
10729 		if (phyi != NULL) {
10730 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10731 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10732 			    phyi->phyint_illv4);
10733 		}
10734 	}
10735 
10736 	/*
10737 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10738 	 * before or after us.
10739 	 */
10740 	ASSERT(IAM_WRITER_ILL(ill));
10741 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10742 
10743 	if (found_sep && orig_ifindex == 0) {
10744 		/* Now see if there is an IPIF with this unit number. */
10745 		for (ipif = ill->ill_ipif; ipif != NULL;
10746 		    ipif = ipif->ipif_next) {
10747 			if (ipif->ipif_id == id) {
10748 				err = EEXIST;
10749 				goto done;
10750 			}
10751 		}
10752 	}
10753 
10754 	/*
10755 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10756 	 * of lo0. We never come here when we plumb lo0:0. It
10757 	 * happens in ipif_lookup_on_name.
10758 	 * The specified unit number is ignored when we create the ipif on a
10759 	 * different interface. However, we save it in ipif_orig_ipifid below so
10760 	 * that the ipif fails back to the right position.
10761 	 */
10762 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10763 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10764 		err = ENOBUFS;
10765 		goto done;
10766 	}
10767 
10768 	/* Return created name with ioctl */
10769 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10770 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10771 	ip1dbg(("created %s\n", lifr->lifr_name));
10772 
10773 	/* Set address */
10774 	sin = (sin_t *)&lifr->lifr_addr;
10775 	if (sin->sin_family != AF_UNSPEC) {
10776 		err = ip_sioctl_addr(ipif, sin, q, mp,
10777 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10778 	}
10779 
10780 	/* Set ifindex and unit number for failback */
10781 	if (err == 0 && orig_ifindex != 0) {
10782 		ipif->ipif_orig_ifindex = orig_ifindex;
10783 		if (found_sep) {
10784 			ipif->ipif_orig_ipifid = id;
10785 		}
10786 	}
10787 
10788 done:
10789 	ipsq_exit(ipsq);
10790 	return (err);
10791 }
10792 
10793 /*
10794  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10795  * interface) delete it based on the IP address (on this physical interface).
10796  * Otherwise delete it based on the ipif_id.
10797  * Also, special handling to allow a removeif of lo0.
10798  */
10799 /* ARGSUSED */
10800 int
10801 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10802     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10803 {
10804 	conn_t		*connp;
10805 	ill_t		*ill = ipif->ipif_ill;
10806 	boolean_t	 success;
10807 	ip_stack_t	*ipst;
10808 
10809 	ipst = CONNQ_TO_IPST(q);
10810 
10811 	ASSERT(q->q_next == NULL);
10812 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10813 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10814 	ASSERT(IAM_WRITER_IPIF(ipif));
10815 
10816 	connp = Q_TO_CONN(q);
10817 	/*
10818 	 * Special case for unplumbing lo0 (the loopback physical interface).
10819 	 * If unplumbing lo0, the incoming address structure has been
10820 	 * initialized to all zeros. When unplumbing lo0, all its logical
10821 	 * interfaces must be removed too.
10822 	 *
10823 	 * Note that this interface may be called to remove a specific
10824 	 * loopback logical interface (eg, lo0:1). But in that case
10825 	 * ipif->ipif_id != 0 so that the code path for that case is the
10826 	 * same as any other interface (meaning it skips the code directly
10827 	 * below).
10828 	 */
10829 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10830 		if (sin->sin_family == AF_UNSPEC &&
10831 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10832 			/*
10833 			 * Mark it condemned. No new ref. will be made to ill.
10834 			 */
10835 			mutex_enter(&ill->ill_lock);
10836 			ill->ill_state_flags |= ILL_CONDEMNED;
10837 			for (ipif = ill->ill_ipif; ipif != NULL;
10838 			    ipif = ipif->ipif_next) {
10839 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10840 			}
10841 			mutex_exit(&ill->ill_lock);
10842 
10843 			ipif = ill->ill_ipif;
10844 			/* unplumb the loopback interface */
10845 			ill_delete(ill);
10846 			mutex_enter(&connp->conn_lock);
10847 			mutex_enter(&ill->ill_lock);
10848 			ASSERT(ill->ill_group == NULL);
10849 
10850 			/* Are any references to this ill active */
10851 			if (ill_is_freeable(ill)) {
10852 				mutex_exit(&ill->ill_lock);
10853 				mutex_exit(&connp->conn_lock);
10854 				ill_delete_tail(ill);
10855 				mi_free(ill);
10856 				return (0);
10857 			}
10858 			success = ipsq_pending_mp_add(connp, ipif,
10859 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10860 			mutex_exit(&connp->conn_lock);
10861 			mutex_exit(&ill->ill_lock);
10862 			if (success)
10863 				return (EINPROGRESS);
10864 			else
10865 				return (EINTR);
10866 		}
10867 	}
10868 
10869 	/*
10870 	 * We are exclusive on the ipsq, so an ill move will be serialized
10871 	 * before or after us.
10872 	 */
10873 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10874 
10875 	if (ipif->ipif_id == 0) {
10876 
10877 		ipsq_t *ipsq;
10878 
10879 		/* Find based on address */
10880 		if (ipif->ipif_isv6) {
10881 			sin6_t *sin6;
10882 
10883 			if (sin->sin_family != AF_INET6)
10884 				return (EAFNOSUPPORT);
10885 
10886 			sin6 = (sin6_t *)sin;
10887 			/* We are a writer, so we should be able to lookup */
10888 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10889 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10890 			if (ipif == NULL) {
10891 				/*
10892 				 * Maybe the address in on another interface in
10893 				 * the same IPMP group? We check this below.
10894 				 */
10895 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10896 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10897 				    ipst);
10898 			}
10899 		} else {
10900 			ipaddr_t addr;
10901 
10902 			if (sin->sin_family != AF_INET)
10903 				return (EAFNOSUPPORT);
10904 
10905 			addr = sin->sin_addr.s_addr;
10906 			/* We are a writer, so we should be able to lookup */
10907 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10908 			    NULL, NULL, NULL, ipst);
10909 			if (ipif == NULL) {
10910 				/*
10911 				 * Maybe the address in on another interface in
10912 				 * the same IPMP group? We check this below.
10913 				 */
10914 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10915 				    NULL, NULL, NULL, NULL, ipst);
10916 			}
10917 		}
10918 		if (ipif == NULL) {
10919 			return (EADDRNOTAVAIL);
10920 		}
10921 
10922 		/*
10923 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10924 		 * lifr_name of the physical interface but with an ip address
10925 		 * lifr_addr of a logical interface plumbed over it.
10926 		 * So update ipsq_current_ipif once ipif points to the
10927 		 * correct interface after doing ipif_lookup_addr().
10928 		 */
10929 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10930 		ASSERT(ipsq != NULL);
10931 
10932 		mutex_enter(&ipsq->ipsq_lock);
10933 		ipsq->ipsq_current_ipif = ipif;
10934 		mutex_exit(&ipsq->ipsq_lock);
10935 
10936 		/*
10937 		 * When the address to be removed is hosted on a different
10938 		 * interface, we check if the interface is in the same IPMP
10939 		 * group as the specified one; if so we proceed with the
10940 		 * removal.
10941 		 * ill->ill_group is NULL when the ill is down, so we have to
10942 		 * compare the group names instead.
10943 		 */
10944 		if (ipif->ipif_ill != ill &&
10945 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10946 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10947 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10948 		    ill->ill_phyint->phyint_groupname) != 0)) {
10949 			ipif_refrele(ipif);
10950 			return (EADDRNOTAVAIL);
10951 		}
10952 
10953 		/* This is a writer */
10954 		ipif_refrele(ipif);
10955 	}
10956 
10957 	/*
10958 	 * Can not delete instance zero since it is tied to the ill.
10959 	 */
10960 	if (ipif->ipif_id == 0)
10961 		return (EBUSY);
10962 
10963 	mutex_enter(&ill->ill_lock);
10964 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10965 	mutex_exit(&ill->ill_lock);
10966 
10967 	ipif_free(ipif);
10968 
10969 	mutex_enter(&connp->conn_lock);
10970 	mutex_enter(&ill->ill_lock);
10971 
10972 
10973 	/* Are any references to this ipif active */
10974 	if (ipif_is_freeable(ipif)) {
10975 		mutex_exit(&ill->ill_lock);
10976 		mutex_exit(&connp->conn_lock);
10977 		ipif_non_duplicate(ipif);
10978 		ipif_down_tail(ipif);
10979 		ipif_free_tail(ipif); /* frees ipif */
10980 		return (0);
10981 	}
10982 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10983 	    IPIF_FREE);
10984 	mutex_exit(&ill->ill_lock);
10985 	mutex_exit(&connp->conn_lock);
10986 	if (success)
10987 		return (EINPROGRESS);
10988 	else
10989 		return (EINTR);
10990 }
10991 
10992 /*
10993  * Restart the removeif ioctl. The refcnt has gone down to 0.
10994  * The ipif is already condemned. So can't find it thru lookups.
10995  */
10996 /* ARGSUSED */
10997 int
10998 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10999     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11000 {
11001 	ill_t *ill = ipif->ipif_ill;
11002 
11003 	ASSERT(IAM_WRITER_IPIF(ipif));
11004 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11005 
11006 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11007 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11008 
11009 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11010 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
11011 		ill_delete_tail(ill);
11012 		mi_free(ill);
11013 		return (0);
11014 	}
11015 
11016 	ipif_non_duplicate(ipif);
11017 	ipif_down_tail(ipif);
11018 	ipif_free_tail(ipif);
11019 
11020 	ILL_UNMARK_CHANGING(ill);
11021 	return (0);
11022 }
11023 
11024 /*
11025  * Set the local interface address.
11026  * Allow an address of all zero when the interface is down.
11027  */
11028 /* ARGSUSED */
11029 int
11030 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11031     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11032 {
11033 	int err = 0;
11034 	in6_addr_t v6addr;
11035 	boolean_t need_up = B_FALSE;
11036 
11037 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11038 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11039 
11040 	ASSERT(IAM_WRITER_IPIF(ipif));
11041 
11042 	if (ipif->ipif_isv6) {
11043 		sin6_t *sin6;
11044 		ill_t *ill;
11045 		phyint_t *phyi;
11046 
11047 		if (sin->sin_family != AF_INET6)
11048 			return (EAFNOSUPPORT);
11049 
11050 		sin6 = (sin6_t *)sin;
11051 		v6addr = sin6->sin6_addr;
11052 		ill = ipif->ipif_ill;
11053 		phyi = ill->ill_phyint;
11054 
11055 		/*
11056 		 * Enforce that true multicast interfaces have a link-local
11057 		 * address for logical unit 0.
11058 		 */
11059 		if (ipif->ipif_id == 0 &&
11060 		    (ill->ill_flags & ILLF_MULTICAST) &&
11061 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11062 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11063 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11064 			return (EADDRNOTAVAIL);
11065 		}
11066 
11067 		/*
11068 		 * up interfaces shouldn't have the unspecified address
11069 		 * unless they also have the IPIF_NOLOCAL flags set and
11070 		 * have a subnet assigned.
11071 		 */
11072 		if ((ipif->ipif_flags & IPIF_UP) &&
11073 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11074 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11075 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11076 			return (EADDRNOTAVAIL);
11077 		}
11078 
11079 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11080 			return (EADDRNOTAVAIL);
11081 	} else {
11082 		ipaddr_t addr;
11083 
11084 		if (sin->sin_family != AF_INET)
11085 			return (EAFNOSUPPORT);
11086 
11087 		addr = sin->sin_addr.s_addr;
11088 
11089 		/* Allow 0 as the local address. */
11090 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11091 			return (EADDRNOTAVAIL);
11092 
11093 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11094 	}
11095 
11096 	/*
11097 	 * Even if there is no change we redo things just to rerun
11098 	 * ipif_set_default.
11099 	 */
11100 	if (ipif->ipif_flags & IPIF_UP) {
11101 		/*
11102 		 * Setting a new local address, make sure
11103 		 * we have net and subnet bcast ire's for
11104 		 * the old address if we need them.
11105 		 */
11106 		if (!ipif->ipif_isv6)
11107 			ipif_check_bcast_ires(ipif);
11108 		/*
11109 		 * If the interface is already marked up,
11110 		 * we call ipif_down which will take care
11111 		 * of ditching any IREs that have been set
11112 		 * up based on the old interface address.
11113 		 */
11114 		err = ipif_logical_down(ipif, q, mp);
11115 		if (err == EINPROGRESS)
11116 			return (err);
11117 		ipif_down_tail(ipif);
11118 		need_up = 1;
11119 	}
11120 
11121 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11122 	return (err);
11123 }
11124 
11125 int
11126 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11127     boolean_t need_up)
11128 {
11129 	in6_addr_t v6addr;
11130 	in6_addr_t ov6addr;
11131 	ipaddr_t addr;
11132 	sin6_t	*sin6;
11133 	int	sinlen;
11134 	int	err = 0;
11135 	ill_t	*ill = ipif->ipif_ill;
11136 	boolean_t need_dl_down;
11137 	boolean_t need_arp_down;
11138 	struct iocblk *iocp;
11139 
11140 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11141 
11142 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11143 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11144 	ASSERT(IAM_WRITER_IPIF(ipif));
11145 
11146 	/* Must cancel any pending timer before taking the ill_lock */
11147 	if (ipif->ipif_recovery_id != 0)
11148 		(void) untimeout(ipif->ipif_recovery_id);
11149 	ipif->ipif_recovery_id = 0;
11150 
11151 	if (ipif->ipif_isv6) {
11152 		sin6 = (sin6_t *)sin;
11153 		v6addr = sin6->sin6_addr;
11154 		sinlen = sizeof (struct sockaddr_in6);
11155 	} else {
11156 		addr = sin->sin_addr.s_addr;
11157 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11158 		sinlen = sizeof (struct sockaddr_in);
11159 	}
11160 	mutex_enter(&ill->ill_lock);
11161 	ov6addr = ipif->ipif_v6lcl_addr;
11162 	ipif->ipif_v6lcl_addr = v6addr;
11163 	sctp_update_ipif_addr(ipif, ov6addr);
11164 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11165 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11166 	} else {
11167 		ipif->ipif_v6src_addr = v6addr;
11168 	}
11169 	ipif->ipif_addr_ready = 0;
11170 
11171 	/*
11172 	 * If the interface was previously marked as a duplicate, then since
11173 	 * we've now got a "new" address, it should no longer be considered a
11174 	 * duplicate -- even if the "new" address is the same as the old one.
11175 	 * Note that if all ipifs are down, we may have a pending ARP down
11176 	 * event to handle.  This is because we want to recover from duplicates
11177 	 * and thus delay tearing down ARP until the duplicates have been
11178 	 * removed or disabled.
11179 	 */
11180 	need_dl_down = need_arp_down = B_FALSE;
11181 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11182 		need_arp_down = !need_up;
11183 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11184 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11185 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11186 			need_dl_down = B_TRUE;
11187 		}
11188 	}
11189 
11190 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11191 	    !ill->ill_is_6to4tun) {
11192 		queue_t *wqp = ill->ill_wq;
11193 
11194 		/*
11195 		 * The local address of this interface is a 6to4 address,
11196 		 * check if this interface is in fact a 6to4 tunnel or just
11197 		 * an interface configured with a 6to4 address.  We are only
11198 		 * interested in the former.
11199 		 */
11200 		if (wqp != NULL) {
11201 			while ((wqp->q_next != NULL) &&
11202 			    (wqp->q_next->q_qinfo != NULL) &&
11203 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11204 
11205 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11206 				    == TUN6TO4_MODID) {
11207 					/* set for use in IP */
11208 					ill->ill_is_6to4tun = 1;
11209 					break;
11210 				}
11211 				wqp = wqp->q_next;
11212 			}
11213 		}
11214 	}
11215 
11216 	ipif_set_default(ipif);
11217 
11218 	/*
11219 	 * When publishing an interface address change event, we only notify
11220 	 * the event listeners of the new address.  It is assumed that if they
11221 	 * actively care about the addresses assigned that they will have
11222 	 * already discovered the previous address assigned (if there was one.)
11223 	 *
11224 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11225 	 */
11226 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11227 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11228 		    NE_ADDRESS_CHANGE, sin, sinlen);
11229 	}
11230 
11231 	mutex_exit(&ill->ill_lock);
11232 
11233 	if (need_up) {
11234 		/*
11235 		 * Now bring the interface back up.  If this
11236 		 * is the only IPIF for the ILL, ipif_up
11237 		 * will have to re-bind to the device, so
11238 		 * we may get back EINPROGRESS, in which
11239 		 * case, this IOCTL will get completed in
11240 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11241 		 */
11242 		err = ipif_up(ipif, q, mp);
11243 	}
11244 
11245 	if (need_dl_down)
11246 		ill_dl_down(ill);
11247 	if (need_arp_down)
11248 		ipif_arp_down(ipif);
11249 
11250 	return (err);
11251 }
11252 
11253 
11254 /*
11255  * Restart entry point to restart the address set operation after the
11256  * refcounts have dropped to zero.
11257  */
11258 /* ARGSUSED */
11259 int
11260 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11261     ip_ioctl_cmd_t *ipip, void *ifreq)
11262 {
11263 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11264 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11265 	ASSERT(IAM_WRITER_IPIF(ipif));
11266 	ipif_down_tail(ipif);
11267 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11268 }
11269 
11270 /* ARGSUSED */
11271 int
11272 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11273     ip_ioctl_cmd_t *ipip, void *if_req)
11274 {
11275 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11276 	struct lifreq *lifr = (struct lifreq *)if_req;
11277 
11278 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11279 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11280 	/*
11281 	 * The net mask and address can't change since we have a
11282 	 * reference to the ipif. So no lock is necessary.
11283 	 */
11284 	if (ipif->ipif_isv6) {
11285 		*sin6 = sin6_null;
11286 		sin6->sin6_family = AF_INET6;
11287 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11288 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11289 		lifr->lifr_addrlen =
11290 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11291 	} else {
11292 		*sin = sin_null;
11293 		sin->sin_family = AF_INET;
11294 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11295 		if (ipip->ipi_cmd_type == LIF_CMD) {
11296 			lifr->lifr_addrlen =
11297 			    ip_mask_to_plen(ipif->ipif_net_mask);
11298 		}
11299 	}
11300 	return (0);
11301 }
11302 
11303 /*
11304  * Set the destination address for a pt-pt interface.
11305  */
11306 /* ARGSUSED */
11307 int
11308 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11309     ip_ioctl_cmd_t *ipip, void *if_req)
11310 {
11311 	int err = 0;
11312 	in6_addr_t v6addr;
11313 	boolean_t need_up = B_FALSE;
11314 
11315 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11316 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11317 	ASSERT(IAM_WRITER_IPIF(ipif));
11318 
11319 	if (ipif->ipif_isv6) {
11320 		sin6_t *sin6;
11321 
11322 		if (sin->sin_family != AF_INET6)
11323 			return (EAFNOSUPPORT);
11324 
11325 		sin6 = (sin6_t *)sin;
11326 		v6addr = sin6->sin6_addr;
11327 
11328 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11329 			return (EADDRNOTAVAIL);
11330 	} else {
11331 		ipaddr_t addr;
11332 
11333 		if (sin->sin_family != AF_INET)
11334 			return (EAFNOSUPPORT);
11335 
11336 		addr = sin->sin_addr.s_addr;
11337 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11338 			return (EADDRNOTAVAIL);
11339 
11340 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11341 	}
11342 
11343 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11344 		return (0);	/* No change */
11345 
11346 	if (ipif->ipif_flags & IPIF_UP) {
11347 		/*
11348 		 * If the interface is already marked up,
11349 		 * we call ipif_down which will take care
11350 		 * of ditching any IREs that have been set
11351 		 * up based on the old pp dst address.
11352 		 */
11353 		err = ipif_logical_down(ipif, q, mp);
11354 		if (err == EINPROGRESS)
11355 			return (err);
11356 		ipif_down_tail(ipif);
11357 		need_up = B_TRUE;
11358 	}
11359 	/*
11360 	 * could return EINPROGRESS. If so ioctl will complete in
11361 	 * ip_rput_dlpi_writer
11362 	 */
11363 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11364 	return (err);
11365 }
11366 
11367 static int
11368 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11369     boolean_t need_up)
11370 {
11371 	in6_addr_t v6addr;
11372 	ill_t	*ill = ipif->ipif_ill;
11373 	int	err = 0;
11374 	boolean_t need_dl_down;
11375 	boolean_t need_arp_down;
11376 
11377 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11378 	    ipif->ipif_id, (void *)ipif));
11379 
11380 	/* Must cancel any pending timer before taking the ill_lock */
11381 	if (ipif->ipif_recovery_id != 0)
11382 		(void) untimeout(ipif->ipif_recovery_id);
11383 	ipif->ipif_recovery_id = 0;
11384 
11385 	if (ipif->ipif_isv6) {
11386 		sin6_t *sin6;
11387 
11388 		sin6 = (sin6_t *)sin;
11389 		v6addr = sin6->sin6_addr;
11390 	} else {
11391 		ipaddr_t addr;
11392 
11393 		addr = sin->sin_addr.s_addr;
11394 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11395 	}
11396 	mutex_enter(&ill->ill_lock);
11397 	/* Set point to point destination address. */
11398 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11399 		/*
11400 		 * Allow this as a means of creating logical
11401 		 * pt-pt interfaces on top of e.g. an Ethernet.
11402 		 * XXX Undocumented HACK for testing.
11403 		 * pt-pt interfaces are created with NUD disabled.
11404 		 */
11405 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11406 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11407 		if (ipif->ipif_isv6)
11408 			ill->ill_flags |= ILLF_NONUD;
11409 	}
11410 
11411 	/*
11412 	 * If the interface was previously marked as a duplicate, then since
11413 	 * we've now got a "new" address, it should no longer be considered a
11414 	 * duplicate -- even if the "new" address is the same as the old one.
11415 	 * Note that if all ipifs are down, we may have a pending ARP down
11416 	 * event to handle.
11417 	 */
11418 	need_dl_down = need_arp_down = B_FALSE;
11419 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11420 		need_arp_down = !need_up;
11421 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11422 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11423 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11424 			need_dl_down = B_TRUE;
11425 		}
11426 	}
11427 
11428 	/* Set the new address. */
11429 	ipif->ipif_v6pp_dst_addr = v6addr;
11430 	/* Make sure subnet tracks pp_dst */
11431 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11432 	mutex_exit(&ill->ill_lock);
11433 
11434 	if (need_up) {
11435 		/*
11436 		 * Now bring the interface back up.  If this
11437 		 * is the only IPIF for the ILL, ipif_up
11438 		 * will have to re-bind to the device, so
11439 		 * we may get back EINPROGRESS, in which
11440 		 * case, this IOCTL will get completed in
11441 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11442 		 */
11443 		err = ipif_up(ipif, q, mp);
11444 	}
11445 
11446 	if (need_dl_down)
11447 		ill_dl_down(ill);
11448 
11449 	if (need_arp_down)
11450 		ipif_arp_down(ipif);
11451 	return (err);
11452 }
11453 
11454 /*
11455  * Restart entry point to restart the dstaddress set operation after the
11456  * refcounts have dropped to zero.
11457  */
11458 /* ARGSUSED */
11459 int
11460 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11461     ip_ioctl_cmd_t *ipip, void *ifreq)
11462 {
11463 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11464 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11465 	ipif_down_tail(ipif);
11466 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11467 }
11468 
11469 /* ARGSUSED */
11470 int
11471 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11472     ip_ioctl_cmd_t *ipip, void *if_req)
11473 {
11474 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11475 
11476 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11477 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11478 	/*
11479 	 * Get point to point destination address. The addresses can't
11480 	 * change since we hold a reference to the ipif.
11481 	 */
11482 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11483 		return (EADDRNOTAVAIL);
11484 
11485 	if (ipif->ipif_isv6) {
11486 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11487 		*sin6 = sin6_null;
11488 		sin6->sin6_family = AF_INET6;
11489 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11490 	} else {
11491 		*sin = sin_null;
11492 		sin->sin_family = AF_INET;
11493 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11494 	}
11495 	return (0);
11496 }
11497 
11498 /*
11499  * part of ipmp, make this func return the active/inactive state and
11500  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11501  */
11502 /*
11503  * This function either sets or clears the IFF_INACTIVE flag.
11504  *
11505  * As long as there are some addresses or multicast memberships on the
11506  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11507  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11508  * will be used for outbound packets.
11509  *
11510  * Caller needs to verify the validity of setting IFF_INACTIVE.
11511  */
11512 static void
11513 phyint_inactive(phyint_t *phyi)
11514 {
11515 	ill_t *ill_v4;
11516 	ill_t *ill_v6;
11517 	ipif_t *ipif;
11518 	ilm_t *ilm;
11519 
11520 	ill_v4 = phyi->phyint_illv4;
11521 	ill_v6 = phyi->phyint_illv6;
11522 
11523 	/*
11524 	 * No need for a lock while traversing the list since iam
11525 	 * a writer
11526 	 */
11527 	if (ill_v4 != NULL) {
11528 		ASSERT(IAM_WRITER_ILL(ill_v4));
11529 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11530 		    ipif = ipif->ipif_next) {
11531 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11532 				mutex_enter(&phyi->phyint_lock);
11533 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11534 				mutex_exit(&phyi->phyint_lock);
11535 				return;
11536 			}
11537 		}
11538 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11539 		    ilm = ilm->ilm_next) {
11540 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11541 				mutex_enter(&phyi->phyint_lock);
11542 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11543 				mutex_exit(&phyi->phyint_lock);
11544 				return;
11545 			}
11546 		}
11547 	}
11548 	if (ill_v6 != NULL) {
11549 		ill_v6 = phyi->phyint_illv6;
11550 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11551 		    ipif = ipif->ipif_next) {
11552 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11553 				mutex_enter(&phyi->phyint_lock);
11554 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11555 				mutex_exit(&phyi->phyint_lock);
11556 				return;
11557 			}
11558 		}
11559 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11560 		    ilm = ilm->ilm_next) {
11561 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11562 				mutex_enter(&phyi->phyint_lock);
11563 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11564 				mutex_exit(&phyi->phyint_lock);
11565 				return;
11566 			}
11567 		}
11568 	}
11569 	mutex_enter(&phyi->phyint_lock);
11570 	phyi->phyint_flags |= PHYI_INACTIVE;
11571 	mutex_exit(&phyi->phyint_lock);
11572 }
11573 
11574 /*
11575  * This function is called only when the phyint flags change. Currently
11576  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11577  * that we can select a good ill.
11578  */
11579 static void
11580 ip_redo_nomination(phyint_t *phyi)
11581 {
11582 	ill_t *ill_v4;
11583 
11584 	ill_v4 = phyi->phyint_illv4;
11585 
11586 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11587 		ASSERT(IAM_WRITER_ILL(ill_v4));
11588 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11589 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11590 	}
11591 }
11592 
11593 /*
11594  * Heuristic to check if ill is INACTIVE.
11595  * Checks if ill has an ipif with an usable ip address.
11596  *
11597  * Return values:
11598  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11599  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11600  */
11601 static boolean_t
11602 ill_is_inactive(ill_t *ill)
11603 {
11604 	ipif_t *ipif;
11605 
11606 	/* Check whether it is in an IPMP group */
11607 	if (ill->ill_phyint->phyint_groupname == NULL)
11608 		return (B_FALSE);
11609 
11610 	if (ill->ill_ipif_up_count == 0)
11611 		return (B_TRUE);
11612 
11613 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11614 		uint64_t flags = ipif->ipif_flags;
11615 
11616 		/*
11617 		 * This ipif is usable if it is IPIF_UP and not a
11618 		 * dedicated test address.  A dedicated test address
11619 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11620 		 * (note in particular that V6 test addresses are
11621 		 * link-local data addresses and thus are marked
11622 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11623 		 */
11624 		if ((flags & IPIF_UP) &&
11625 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11626 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11627 			return (B_FALSE);
11628 	}
11629 	return (B_TRUE);
11630 }
11631 
11632 /*
11633  * Set interface flags.
11634  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11635  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11636  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11637  *
11638  * NOTE : We really don't enforce that ipif_id zero should be used
11639  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11640  *	  is because applications generally does SICGLIFFLAGS and
11641  *	  ORs in the new flags (that affects the logical) and does a
11642  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11643  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11644  *	  flags that will be turned on is correct with respect to
11645  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11646  */
11647 /* ARGSUSED */
11648 int
11649 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11650     ip_ioctl_cmd_t *ipip, void *if_req)
11651 {
11652 	uint64_t turn_on;
11653 	uint64_t turn_off;
11654 	int	err;
11655 	phyint_t *phyi;
11656 	ill_t *ill;
11657 	uint64_t intf_flags;
11658 	boolean_t phyint_flags_modified = B_FALSE;
11659 	uint64_t flags;
11660 	struct ifreq *ifr;
11661 	struct lifreq *lifr;
11662 	boolean_t set_linklocal = B_FALSE;
11663 	boolean_t zero_source = B_FALSE;
11664 	ip_stack_t *ipst;
11665 
11666 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11667 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11668 
11669 	ASSERT(IAM_WRITER_IPIF(ipif));
11670 
11671 	ill = ipif->ipif_ill;
11672 	phyi = ill->ill_phyint;
11673 	ipst = ill->ill_ipst;
11674 
11675 	if (ipip->ipi_cmd_type == IF_CMD) {
11676 		ifr = (struct ifreq *)if_req;
11677 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11678 	} else {
11679 		lifr = (struct lifreq *)if_req;
11680 		flags = lifr->lifr_flags;
11681 	}
11682 
11683 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11684 
11685 	/*
11686 	 * Have the flags been set correctly until now?
11687 	 */
11688 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11689 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11690 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11691 	/*
11692 	 * Compare the new flags to the old, and partition
11693 	 * into those coming on and those going off.
11694 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11695 	 */
11696 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11697 		flags |= intf_flags & ~0xFFFF;
11698 
11699 	/*
11700 	 * First check which bits will change and then which will
11701 	 * go on and off
11702 	 */
11703 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11704 	if (!turn_on)
11705 		return (0);	/* No change */
11706 
11707 	turn_off = intf_flags & turn_on;
11708 	turn_on ^= turn_off;
11709 	err = 0;
11710 
11711 	/*
11712 	 * Don't allow any bits belonging to the logical interface
11713 	 * to be set or cleared on the replacement ipif that was
11714 	 * created temporarily during a MOVE.
11715 	 */
11716 	if (ipif->ipif_replace_zero &&
11717 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11718 		return (EINVAL);
11719 	}
11720 
11721 	/*
11722 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11723 	 * IPv6 interfaces.
11724 	 */
11725 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11726 		return (EINVAL);
11727 
11728 	/*
11729 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11730 	 */
11731 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11732 		return (EINVAL);
11733 
11734 	/*
11735 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11736 	 * interfaces.  It makes no sense in that context.
11737 	 */
11738 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11739 		return (EINVAL);
11740 
11741 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11742 		zero_source = B_TRUE;
11743 
11744 	/*
11745 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11746 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11747 	 * If the link local address isn't set, and can be set, it will get
11748 	 * set later on in this function.
11749 	 */
11750 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11751 	    (flags & IFF_UP) && !zero_source &&
11752 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11753 		if (ipif_cant_setlinklocal(ipif))
11754 			return (EINVAL);
11755 		set_linklocal = B_TRUE;
11756 	}
11757 
11758 	/*
11759 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11760 	 * same time. No need to grab ill_g_usesrc_lock here, see
11761 	 * synchronization notes in ip.c
11762 	 */
11763 	if (turn_on & PHYI_STANDBY &&
11764 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11765 		return (EINVAL);
11766 	}
11767 
11768 	/*
11769 	 * If we modify physical interface flags, we'll potentially need to
11770 	 * send up two routing socket messages for the changes (one for the
11771 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11772 	 */
11773 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11774 		phyint_flags_modified = B_TRUE;
11775 
11776 	/*
11777 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11778 	 * we need to flush the IRE_CACHES belonging to this ill.
11779 	 * We handle this case here without doing the DOWN/UP dance
11780 	 * like it is done for other flags. If some other flags are
11781 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11782 	 * below will handle it by bringing it down and then
11783 	 * bringing it UP.
11784 	 */
11785 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11786 		ill_t *ill_v4, *ill_v6;
11787 
11788 		ill_v4 = phyi->phyint_illv4;
11789 		ill_v6 = phyi->phyint_illv6;
11790 
11791 		/*
11792 		 * First set the INACTIVE flag if needed. Then delete the ires.
11793 		 * ire_add will atomically prevent creating new IRE_CACHEs
11794 		 * unless hidden flag is set.
11795 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11796 		 */
11797 		if ((turn_on & PHYI_FAILED) &&
11798 		    ((intf_flags & PHYI_STANDBY) ||
11799 		    !ipst->ips_ipmp_enable_failback)) {
11800 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11801 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11802 		}
11803 		if ((turn_off & PHYI_FAILED) &&
11804 		    ((intf_flags & PHYI_STANDBY) ||
11805 		    (!ipst->ips_ipmp_enable_failback &&
11806 		    ill_is_inactive(ill)))) {
11807 			phyint_inactive(phyi);
11808 		}
11809 
11810 		if (turn_on & PHYI_STANDBY) {
11811 			/*
11812 			 * We implicitly set INACTIVE only when STANDBY is set.
11813 			 * INACTIVE is also set on non-STANDBY phyint when user
11814 			 * disables FAILBACK using configuration file.
11815 			 * Do not allow STANDBY to be set on such INACTIVE
11816 			 * phyint
11817 			 */
11818 			if (phyi->phyint_flags & PHYI_INACTIVE)
11819 				return (EINVAL);
11820 			if (!(phyi->phyint_flags & PHYI_FAILED))
11821 				phyint_inactive(phyi);
11822 		}
11823 		if (turn_off & PHYI_STANDBY) {
11824 			if (ipst->ips_ipmp_enable_failback) {
11825 				/*
11826 				 * Reset PHYI_INACTIVE.
11827 				 */
11828 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11829 			} else if (ill_is_inactive(ill) &&
11830 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11831 				/*
11832 				 * Need to set INACTIVE, when user sets
11833 				 * STANDBY on a non-STANDBY phyint and
11834 				 * later resets STANDBY
11835 				 */
11836 				phyint_inactive(phyi);
11837 			}
11838 		}
11839 		/*
11840 		 * We should always send up a message so that the
11841 		 * daemons come to know of it. Note that the zeroth
11842 		 * interface can be down and the check below for IPIF_UP
11843 		 * will not make sense as we are actually setting
11844 		 * a phyint flag here. We assume that the ipif used
11845 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11846 		 * send up any message for non-zero ipifs).
11847 		 */
11848 		phyint_flags_modified = B_TRUE;
11849 
11850 		if (ill_v4 != NULL) {
11851 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11852 			    IRE_CACHE, ill_stq_cache_delete,
11853 			    (char *)ill_v4, ill_v4);
11854 			illgrp_reset_schednext(ill_v4);
11855 		}
11856 		if (ill_v6 != NULL) {
11857 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11858 			    IRE_CACHE, ill_stq_cache_delete,
11859 			    (char *)ill_v6, ill_v6);
11860 			illgrp_reset_schednext(ill_v6);
11861 		}
11862 	}
11863 
11864 	/*
11865 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11866 	 * status of the interface and, if the interface is part of an IPMP
11867 	 * group, all other interfaces that are part of the same IPMP
11868 	 * group.
11869 	 */
11870 	if ((turn_on | turn_off) & ILLF_ROUTER)
11871 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11872 
11873 	/*
11874 	 * If the interface is not UP and we are not going to
11875 	 * bring it UP, record the flags and return. When the
11876 	 * interface comes UP later, the right actions will be
11877 	 * taken.
11878 	 */
11879 	if (!(ipif->ipif_flags & IPIF_UP) &&
11880 	    !(turn_on & IPIF_UP)) {
11881 		/* Record new flags in their respective places. */
11882 		mutex_enter(&ill->ill_lock);
11883 		mutex_enter(&ill->ill_phyint->phyint_lock);
11884 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11885 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11886 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11887 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11888 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11889 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11890 		mutex_exit(&ill->ill_lock);
11891 		mutex_exit(&ill->ill_phyint->phyint_lock);
11892 
11893 		/*
11894 		 * We do the broadcast and nomination here rather
11895 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11896 		 * the case of FAILBACK from INACTIVE standby to the
11897 		 * interface that has been repaired, PHYI_FAILED has not
11898 		 * been cleared yet. If there are only two interfaces in
11899 		 * that group, all we have is a FAILED and INACTIVE
11900 		 * interface. If we do the nomination soon after a failback,
11901 		 * the broadcast nomination code would select the
11902 		 * INACTIVE interface for receiving broadcasts as FAILED is
11903 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11904 		 * receive broadcast packets, we need to redo nomination
11905 		 * when the FAILED is cleared here. Thus, in general we
11906 		 * always do the nomination here for FAILED, STANDBY
11907 		 * and OFFLINE.
11908 		 */
11909 		if (((turn_on | turn_off) &
11910 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11911 			ip_redo_nomination(phyi);
11912 		}
11913 		if (phyint_flags_modified) {
11914 			if (phyi->phyint_illv4 != NULL) {
11915 				ip_rts_ifmsg(phyi->phyint_illv4->
11916 				    ill_ipif);
11917 			}
11918 			if (phyi->phyint_illv6 != NULL) {
11919 				ip_rts_ifmsg(phyi->phyint_illv6->
11920 				    ill_ipif);
11921 			}
11922 		}
11923 		return (0);
11924 	} else if (set_linklocal || zero_source) {
11925 		mutex_enter(&ill->ill_lock);
11926 		if (set_linklocal)
11927 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11928 		if (zero_source)
11929 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11930 		mutex_exit(&ill->ill_lock);
11931 	}
11932 
11933 	/*
11934 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11935 	 * or point-to-point interfaces with an unspecified destination. We do
11936 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11937 	 * have a subnet assigned, which is how in.ndpd currently manages its
11938 	 * onlink prefix list when no addresses are configured with those
11939 	 * prefixes.
11940 	 */
11941 	if (ipif->ipif_isv6 &&
11942 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11943 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11944 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11945 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11946 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11947 		return (EINVAL);
11948 	}
11949 
11950 	/*
11951 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11952 	 * from being brought up.
11953 	 */
11954 	if (!ipif->ipif_isv6 &&
11955 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11956 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11957 		return (EINVAL);
11958 	}
11959 
11960 	/*
11961 	 * The only flag changes that we currently take specific action on
11962 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11963 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11964 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11965 	 * the flags and bringing it back up again.
11966 	 */
11967 	if ((turn_on|turn_off) &
11968 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11969 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11970 		/*
11971 		 * Taking this ipif down, make sure we have
11972 		 * valid net and subnet bcast ire's for other
11973 		 * logical interfaces, if we need them.
11974 		 */
11975 		if (!ipif->ipif_isv6)
11976 			ipif_check_bcast_ires(ipif);
11977 
11978 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11979 		    !(turn_off & IPIF_UP)) {
11980 			if (ipif->ipif_flags & IPIF_UP)
11981 				ill->ill_logical_down = 1;
11982 			turn_on &= ~IPIF_UP;
11983 		}
11984 		err = ipif_down(ipif, q, mp);
11985 		ip1dbg(("ipif_down returns %d err ", err));
11986 		if (err == EINPROGRESS)
11987 			return (err);
11988 		ipif_down_tail(ipif);
11989 	}
11990 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11991 }
11992 
11993 static int
11994 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11995 {
11996 	ill_t	*ill;
11997 	phyint_t *phyi;
11998 	uint64_t turn_on;
11999 	uint64_t turn_off;
12000 	uint64_t intf_flags;
12001 	boolean_t phyint_flags_modified = B_FALSE;
12002 	int	err = 0;
12003 	boolean_t set_linklocal = B_FALSE;
12004 	boolean_t zero_source = B_FALSE;
12005 
12006 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12007 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12008 
12009 	ASSERT(IAM_WRITER_IPIF(ipif));
12010 
12011 	ill = ipif->ipif_ill;
12012 	phyi = ill->ill_phyint;
12013 
12014 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12015 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12016 
12017 	turn_off = intf_flags & turn_on;
12018 	turn_on ^= turn_off;
12019 
12020 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12021 		phyint_flags_modified = B_TRUE;
12022 
12023 	/*
12024 	 * Now we change the flags. Track current value of
12025 	 * other flags in their respective places.
12026 	 */
12027 	mutex_enter(&ill->ill_lock);
12028 	mutex_enter(&phyi->phyint_lock);
12029 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12030 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12031 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12032 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12033 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12034 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12035 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12036 		set_linklocal = B_TRUE;
12037 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12038 	}
12039 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12040 		zero_source = B_TRUE;
12041 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12042 	}
12043 	mutex_exit(&ill->ill_lock);
12044 	mutex_exit(&phyi->phyint_lock);
12045 
12046 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12047 		ip_redo_nomination(phyi);
12048 
12049 	if (set_linklocal)
12050 		(void) ipif_setlinklocal(ipif);
12051 
12052 	if (zero_source)
12053 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12054 	else
12055 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12056 
12057 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
12058 		/*
12059 		 * XXX ipif_up really does not know whether a phyint flags
12060 		 * was modified or not. So, it sends up information on
12061 		 * only one routing sockets message. As we don't bring up
12062 		 * the interface and also set STANDBY/FAILED simultaneously
12063 		 * it should be okay.
12064 		 */
12065 		err = ipif_up(ipif, q, mp);
12066 	} else {
12067 		/*
12068 		 * Make sure routing socket sees all changes to the flags.
12069 		 * ipif_up_done* handles this when we use ipif_up.
12070 		 */
12071 		if (phyint_flags_modified) {
12072 			if (phyi->phyint_illv4 != NULL) {
12073 				ip_rts_ifmsg(phyi->phyint_illv4->
12074 				    ill_ipif);
12075 			}
12076 			if (phyi->phyint_illv6 != NULL) {
12077 				ip_rts_ifmsg(phyi->phyint_illv6->
12078 				    ill_ipif);
12079 			}
12080 		} else {
12081 			ip_rts_ifmsg(ipif);
12082 		}
12083 		/*
12084 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12085 		 * this in need_up case.
12086 		 */
12087 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12088 	}
12089 	return (err);
12090 }
12091 
12092 /*
12093  * Restart the flags operation now that the refcounts have dropped to zero.
12094  */
12095 /* ARGSUSED */
12096 int
12097 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12098     ip_ioctl_cmd_t *ipip, void *if_req)
12099 {
12100 	uint64_t flags;
12101 	struct ifreq *ifr = if_req;
12102 	struct lifreq *lifr = if_req;
12103 
12104 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12105 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12106 
12107 	ipif_down_tail(ipif);
12108 	if (ipip->ipi_cmd_type == IF_CMD) {
12109 		/* cast to uint16_t prevents unwanted sign extension */
12110 		flags = (uint16_t)ifr->ifr_flags;
12111 	} else {
12112 		flags = lifr->lifr_flags;
12113 	}
12114 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
12115 }
12116 
12117 /*
12118  * Can operate on either a module or a driver queue.
12119  */
12120 /* ARGSUSED */
12121 int
12122 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12123     ip_ioctl_cmd_t *ipip, void *if_req)
12124 {
12125 	/*
12126 	 * Has the flags been set correctly till now ?
12127 	 */
12128 	ill_t *ill = ipif->ipif_ill;
12129 	phyint_t *phyi = ill->ill_phyint;
12130 
12131 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12132 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12133 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12134 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12135 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12136 
12137 	/*
12138 	 * Need a lock since some flags can be set even when there are
12139 	 * references to the ipif.
12140 	 */
12141 	mutex_enter(&ill->ill_lock);
12142 	if (ipip->ipi_cmd_type == IF_CMD) {
12143 		struct ifreq *ifr = (struct ifreq *)if_req;
12144 
12145 		/* Get interface flags (low 16 only). */
12146 		ifr->ifr_flags = ((ipif->ipif_flags |
12147 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12148 	} else {
12149 		struct lifreq *lifr = (struct lifreq *)if_req;
12150 
12151 		/* Get interface flags. */
12152 		lifr->lifr_flags = ipif->ipif_flags |
12153 		    ill->ill_flags | phyi->phyint_flags;
12154 	}
12155 	mutex_exit(&ill->ill_lock);
12156 	return (0);
12157 }
12158 
12159 /* ARGSUSED */
12160 int
12161 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12162     ip_ioctl_cmd_t *ipip, void *if_req)
12163 {
12164 	int mtu;
12165 	int ip_min_mtu;
12166 	struct ifreq	*ifr;
12167 	struct lifreq *lifr;
12168 	ire_t	*ire;
12169 	ip_stack_t *ipst;
12170 
12171 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12172 	    ipif->ipif_id, (void *)ipif));
12173 	if (ipip->ipi_cmd_type == IF_CMD) {
12174 		ifr = (struct ifreq *)if_req;
12175 		mtu = ifr->ifr_metric;
12176 	} else {
12177 		lifr = (struct lifreq *)if_req;
12178 		mtu = lifr->lifr_mtu;
12179 	}
12180 
12181 	if (ipif->ipif_isv6)
12182 		ip_min_mtu = IPV6_MIN_MTU;
12183 	else
12184 		ip_min_mtu = IP_MIN_MTU;
12185 
12186 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12187 		return (EINVAL);
12188 
12189 	/*
12190 	 * Change the MTU size in all relevant ire's.
12191 	 * Mtu change Vs. new ire creation - protocol below.
12192 	 * First change ipif_mtu and the ire_max_frag of the
12193 	 * interface ire. Then do an ire walk and change the
12194 	 * ire_max_frag of all affected ires. During ire_add
12195 	 * under the bucket lock, set the ire_max_frag of the
12196 	 * new ire being created from the ipif/ire from which
12197 	 * it is being derived. If an mtu change happens after
12198 	 * the ire is added, the new ire will be cleaned up.
12199 	 * Conversely if the mtu change happens before the ire
12200 	 * is added, ire_add will see the new value of the mtu.
12201 	 */
12202 	ipif->ipif_mtu = mtu;
12203 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12204 
12205 	if (ipif->ipif_isv6)
12206 		ire = ipif_to_ire_v6(ipif);
12207 	else
12208 		ire = ipif_to_ire(ipif);
12209 	if (ire != NULL) {
12210 		ire->ire_max_frag = ipif->ipif_mtu;
12211 		ire_refrele(ire);
12212 	}
12213 	ipst = ipif->ipif_ill->ill_ipst;
12214 	if (ipif->ipif_flags & IPIF_UP) {
12215 		if (ipif->ipif_isv6)
12216 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12217 			    ipst);
12218 		else
12219 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12220 			    ipst);
12221 	}
12222 	/* Update the MTU in SCTP's list */
12223 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12224 	return (0);
12225 }
12226 
12227 /* Get interface MTU. */
12228 /* ARGSUSED */
12229 int
12230 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12231 	ip_ioctl_cmd_t *ipip, void *if_req)
12232 {
12233 	struct ifreq	*ifr;
12234 	struct lifreq	*lifr;
12235 
12236 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12237 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12238 	if (ipip->ipi_cmd_type == IF_CMD) {
12239 		ifr = (struct ifreq *)if_req;
12240 		ifr->ifr_metric = ipif->ipif_mtu;
12241 	} else {
12242 		lifr = (struct lifreq *)if_req;
12243 		lifr->lifr_mtu = ipif->ipif_mtu;
12244 	}
12245 	return (0);
12246 }
12247 
12248 /* Set interface broadcast address. */
12249 /* ARGSUSED2 */
12250 int
12251 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12252 	ip_ioctl_cmd_t *ipip, void *if_req)
12253 {
12254 	ipaddr_t addr;
12255 	ire_t	*ire;
12256 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12257 
12258 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12259 	    ipif->ipif_id));
12260 
12261 	ASSERT(IAM_WRITER_IPIF(ipif));
12262 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12263 		return (EADDRNOTAVAIL);
12264 
12265 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12266 
12267 	if (sin->sin_family != AF_INET)
12268 		return (EAFNOSUPPORT);
12269 
12270 	addr = sin->sin_addr.s_addr;
12271 	if (ipif->ipif_flags & IPIF_UP) {
12272 		/*
12273 		 * If we are already up, make sure the new
12274 		 * broadcast address makes sense.  If it does,
12275 		 * there should be an IRE for it already.
12276 		 * Don't match on ipif, only on the ill
12277 		 * since we are sharing these now. Don't use
12278 		 * MATCH_IRE_ILL_GROUP as we are looking for
12279 		 * the broadcast ire on this ill and each ill
12280 		 * in the group has its own broadcast ire.
12281 		 */
12282 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12283 		    ipif, ALL_ZONES, NULL,
12284 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12285 		if (ire == NULL) {
12286 			return (EINVAL);
12287 		} else {
12288 			ire_refrele(ire);
12289 		}
12290 	}
12291 	/*
12292 	 * Changing the broadcast addr for this ipif.
12293 	 * Make sure we have valid net and subnet bcast
12294 	 * ire's for other logical interfaces, if needed.
12295 	 */
12296 	if (addr != ipif->ipif_brd_addr)
12297 		ipif_check_bcast_ires(ipif);
12298 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12299 	return (0);
12300 }
12301 
12302 /* Get interface broadcast address. */
12303 /* ARGSUSED */
12304 int
12305 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12306     ip_ioctl_cmd_t *ipip, void *if_req)
12307 {
12308 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12309 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12310 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12311 		return (EADDRNOTAVAIL);
12312 
12313 	/* IPIF_BROADCAST not possible with IPv6 */
12314 	ASSERT(!ipif->ipif_isv6);
12315 	*sin = sin_null;
12316 	sin->sin_family = AF_INET;
12317 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12318 	return (0);
12319 }
12320 
12321 /*
12322  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12323  */
12324 /* ARGSUSED */
12325 int
12326 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12327     ip_ioctl_cmd_t *ipip, void *if_req)
12328 {
12329 	int err = 0;
12330 	in6_addr_t v6mask;
12331 
12332 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12333 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12334 
12335 	ASSERT(IAM_WRITER_IPIF(ipif));
12336 
12337 	if (ipif->ipif_isv6) {
12338 		sin6_t *sin6;
12339 
12340 		if (sin->sin_family != AF_INET6)
12341 			return (EAFNOSUPPORT);
12342 
12343 		sin6 = (sin6_t *)sin;
12344 		v6mask = sin6->sin6_addr;
12345 	} else {
12346 		ipaddr_t mask;
12347 
12348 		if (sin->sin_family != AF_INET)
12349 			return (EAFNOSUPPORT);
12350 
12351 		mask = sin->sin_addr.s_addr;
12352 		V4MASK_TO_V6(mask, v6mask);
12353 	}
12354 
12355 	/*
12356 	 * No big deal if the interface isn't already up, or the mask
12357 	 * isn't really changing, or this is pt-pt.
12358 	 */
12359 	if (!(ipif->ipif_flags & IPIF_UP) ||
12360 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12361 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12362 		ipif->ipif_v6net_mask = v6mask;
12363 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12364 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12365 			    ipif->ipif_v6net_mask,
12366 			    ipif->ipif_v6subnet);
12367 		}
12368 		return (0);
12369 	}
12370 	/*
12371 	 * Make sure we have valid net and subnet broadcast ire's
12372 	 * for the old netmask, if needed by other logical interfaces.
12373 	 */
12374 	if (!ipif->ipif_isv6)
12375 		ipif_check_bcast_ires(ipif);
12376 
12377 	err = ipif_logical_down(ipif, q, mp);
12378 	if (err == EINPROGRESS)
12379 		return (err);
12380 	ipif_down_tail(ipif);
12381 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12382 	return (err);
12383 }
12384 
12385 static int
12386 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12387 {
12388 	in6_addr_t v6mask;
12389 	int err = 0;
12390 
12391 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12392 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12393 
12394 	if (ipif->ipif_isv6) {
12395 		sin6_t *sin6;
12396 
12397 		sin6 = (sin6_t *)sin;
12398 		v6mask = sin6->sin6_addr;
12399 	} else {
12400 		ipaddr_t mask;
12401 
12402 		mask = sin->sin_addr.s_addr;
12403 		V4MASK_TO_V6(mask, v6mask);
12404 	}
12405 
12406 	ipif->ipif_v6net_mask = v6mask;
12407 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12408 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12409 		    ipif->ipif_v6subnet);
12410 	}
12411 	err = ipif_up(ipif, q, mp);
12412 
12413 	if (err == 0 || err == EINPROGRESS) {
12414 		/*
12415 		 * The interface must be DL_BOUND if this packet has to
12416 		 * go out on the wire. Since we only go through a logical
12417 		 * down and are bound with the driver during an internal
12418 		 * down/up that is satisfied.
12419 		 */
12420 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12421 			/* Potentially broadcast an address mask reply. */
12422 			ipif_mask_reply(ipif);
12423 		}
12424 	}
12425 	return (err);
12426 }
12427 
12428 /* ARGSUSED */
12429 int
12430 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12431     ip_ioctl_cmd_t *ipip, void *if_req)
12432 {
12433 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12434 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12435 	ipif_down_tail(ipif);
12436 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12437 }
12438 
12439 /* Get interface net mask. */
12440 /* ARGSUSED */
12441 int
12442 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12443     ip_ioctl_cmd_t *ipip, void *if_req)
12444 {
12445 	struct lifreq *lifr = (struct lifreq *)if_req;
12446 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12447 
12448 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12449 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12450 
12451 	/*
12452 	 * net mask can't change since we have a reference to the ipif.
12453 	 */
12454 	if (ipif->ipif_isv6) {
12455 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12456 		*sin6 = sin6_null;
12457 		sin6->sin6_family = AF_INET6;
12458 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12459 		lifr->lifr_addrlen =
12460 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12461 	} else {
12462 		*sin = sin_null;
12463 		sin->sin_family = AF_INET;
12464 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12465 		if (ipip->ipi_cmd_type == LIF_CMD) {
12466 			lifr->lifr_addrlen =
12467 			    ip_mask_to_plen(ipif->ipif_net_mask);
12468 		}
12469 	}
12470 	return (0);
12471 }
12472 
12473 /* ARGSUSED */
12474 int
12475 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12476     ip_ioctl_cmd_t *ipip, void *if_req)
12477 {
12478 
12479 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12480 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12481 	/*
12482 	 * Set interface metric.  We don't use this for
12483 	 * anything but we keep track of it in case it is
12484 	 * important to routing applications or such.
12485 	 */
12486 	if (ipip->ipi_cmd_type == IF_CMD) {
12487 		struct ifreq    *ifr;
12488 
12489 		ifr = (struct ifreq *)if_req;
12490 		ipif->ipif_metric = ifr->ifr_metric;
12491 	} else {
12492 		struct lifreq   *lifr;
12493 
12494 		lifr = (struct lifreq *)if_req;
12495 		ipif->ipif_metric = lifr->lifr_metric;
12496 	}
12497 	return (0);
12498 }
12499 
12500 /* ARGSUSED */
12501 int
12502 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12503     ip_ioctl_cmd_t *ipip, void *if_req)
12504 {
12505 	/* Get interface metric. */
12506 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12507 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12508 	if (ipip->ipi_cmd_type == IF_CMD) {
12509 		struct ifreq    *ifr;
12510 
12511 		ifr = (struct ifreq *)if_req;
12512 		ifr->ifr_metric = ipif->ipif_metric;
12513 	} else {
12514 		struct lifreq   *lifr;
12515 
12516 		lifr = (struct lifreq *)if_req;
12517 		lifr->lifr_metric = ipif->ipif_metric;
12518 	}
12519 
12520 	return (0);
12521 }
12522 
12523 /* ARGSUSED */
12524 int
12525 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12526     ip_ioctl_cmd_t *ipip, void *if_req)
12527 {
12528 
12529 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12530 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12531 	/*
12532 	 * Set the muxid returned from I_PLINK.
12533 	 */
12534 	if (ipip->ipi_cmd_type == IF_CMD) {
12535 		struct ifreq *ifr = (struct ifreq *)if_req;
12536 
12537 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12538 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12539 	} else {
12540 		struct lifreq *lifr = (struct lifreq *)if_req;
12541 
12542 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12543 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12544 	}
12545 	return (0);
12546 }
12547 
12548 /* ARGSUSED */
12549 int
12550 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12551     ip_ioctl_cmd_t *ipip, void *if_req)
12552 {
12553 
12554 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12555 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12556 	/*
12557 	 * Get the muxid saved in ill for I_PUNLINK.
12558 	 */
12559 	if (ipip->ipi_cmd_type == IF_CMD) {
12560 		struct ifreq *ifr = (struct ifreq *)if_req;
12561 
12562 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12563 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12564 	} else {
12565 		struct lifreq *lifr = (struct lifreq *)if_req;
12566 
12567 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12568 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12569 	}
12570 	return (0);
12571 }
12572 
12573 /*
12574  * Set the subnet prefix. Does not modify the broadcast address.
12575  */
12576 /* ARGSUSED */
12577 int
12578 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12579     ip_ioctl_cmd_t *ipip, void *if_req)
12580 {
12581 	int err = 0;
12582 	in6_addr_t v6addr;
12583 	in6_addr_t v6mask;
12584 	boolean_t need_up = B_FALSE;
12585 	int addrlen;
12586 
12587 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12588 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12589 
12590 	ASSERT(IAM_WRITER_IPIF(ipif));
12591 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12592 
12593 	if (ipif->ipif_isv6) {
12594 		sin6_t *sin6;
12595 
12596 		if (sin->sin_family != AF_INET6)
12597 			return (EAFNOSUPPORT);
12598 
12599 		sin6 = (sin6_t *)sin;
12600 		v6addr = sin6->sin6_addr;
12601 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12602 			return (EADDRNOTAVAIL);
12603 	} else {
12604 		ipaddr_t addr;
12605 
12606 		if (sin->sin_family != AF_INET)
12607 			return (EAFNOSUPPORT);
12608 
12609 		addr = sin->sin_addr.s_addr;
12610 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12611 			return (EADDRNOTAVAIL);
12612 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12613 		/* Add 96 bits */
12614 		addrlen += IPV6_ABITS - IP_ABITS;
12615 	}
12616 
12617 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12618 		return (EINVAL);
12619 
12620 	/* Check if bits in the address is set past the mask */
12621 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12622 		return (EINVAL);
12623 
12624 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12625 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12626 		return (0);	/* No change */
12627 
12628 	if (ipif->ipif_flags & IPIF_UP) {
12629 		/*
12630 		 * If the interface is already marked up,
12631 		 * we call ipif_down which will take care
12632 		 * of ditching any IREs that have been set
12633 		 * up based on the old interface address.
12634 		 */
12635 		err = ipif_logical_down(ipif, q, mp);
12636 		if (err == EINPROGRESS)
12637 			return (err);
12638 		ipif_down_tail(ipif);
12639 		need_up = B_TRUE;
12640 	}
12641 
12642 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12643 	return (err);
12644 }
12645 
12646 static int
12647 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12648     queue_t *q, mblk_t *mp, boolean_t need_up)
12649 {
12650 	ill_t	*ill = ipif->ipif_ill;
12651 	int	err = 0;
12652 
12653 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12654 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12655 
12656 	/* Set the new address. */
12657 	mutex_enter(&ill->ill_lock);
12658 	ipif->ipif_v6net_mask = v6mask;
12659 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12660 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12661 		    ipif->ipif_v6subnet);
12662 	}
12663 	mutex_exit(&ill->ill_lock);
12664 
12665 	if (need_up) {
12666 		/*
12667 		 * Now bring the interface back up.  If this
12668 		 * is the only IPIF for the ILL, ipif_up
12669 		 * will have to re-bind to the device, so
12670 		 * we may get back EINPROGRESS, in which
12671 		 * case, this IOCTL will get completed in
12672 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12673 		 */
12674 		err = ipif_up(ipif, q, mp);
12675 		if (err == EINPROGRESS)
12676 			return (err);
12677 	}
12678 	return (err);
12679 }
12680 
12681 /* ARGSUSED */
12682 int
12683 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12684     ip_ioctl_cmd_t *ipip, void *if_req)
12685 {
12686 	int	addrlen;
12687 	in6_addr_t v6addr;
12688 	in6_addr_t v6mask;
12689 	struct lifreq *lifr = (struct lifreq *)if_req;
12690 
12691 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12692 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12693 	ipif_down_tail(ipif);
12694 
12695 	addrlen = lifr->lifr_addrlen;
12696 	if (ipif->ipif_isv6) {
12697 		sin6_t *sin6;
12698 
12699 		sin6 = (sin6_t *)sin;
12700 		v6addr = sin6->sin6_addr;
12701 	} else {
12702 		ipaddr_t addr;
12703 
12704 		addr = sin->sin_addr.s_addr;
12705 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12706 		addrlen += IPV6_ABITS - IP_ABITS;
12707 	}
12708 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12709 
12710 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12711 }
12712 
12713 /* ARGSUSED */
12714 int
12715 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12716     ip_ioctl_cmd_t *ipip, void *if_req)
12717 {
12718 	struct lifreq *lifr = (struct lifreq *)if_req;
12719 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12720 
12721 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12722 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12723 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12724 
12725 	if (ipif->ipif_isv6) {
12726 		*sin6 = sin6_null;
12727 		sin6->sin6_family = AF_INET6;
12728 		sin6->sin6_addr = ipif->ipif_v6subnet;
12729 		lifr->lifr_addrlen =
12730 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12731 	} else {
12732 		*sin = sin_null;
12733 		sin->sin_family = AF_INET;
12734 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12735 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12736 	}
12737 	return (0);
12738 }
12739 
12740 /*
12741  * Set the IPv6 address token.
12742  */
12743 /* ARGSUSED */
12744 int
12745 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12746     ip_ioctl_cmd_t *ipi, void *if_req)
12747 {
12748 	ill_t *ill = ipif->ipif_ill;
12749 	int err;
12750 	in6_addr_t v6addr;
12751 	in6_addr_t v6mask;
12752 	boolean_t need_up = B_FALSE;
12753 	int i;
12754 	sin6_t *sin6 = (sin6_t *)sin;
12755 	struct lifreq *lifr = (struct lifreq *)if_req;
12756 	int addrlen;
12757 
12758 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12759 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12760 	ASSERT(IAM_WRITER_IPIF(ipif));
12761 
12762 	addrlen = lifr->lifr_addrlen;
12763 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12764 	if (ipif->ipif_id != 0)
12765 		return (EINVAL);
12766 
12767 	if (!ipif->ipif_isv6)
12768 		return (EINVAL);
12769 
12770 	if (addrlen > IPV6_ABITS)
12771 		return (EINVAL);
12772 
12773 	v6addr = sin6->sin6_addr;
12774 
12775 	/*
12776 	 * The length of the token is the length from the end.  To get
12777 	 * the proper mask for this, compute the mask of the bits not
12778 	 * in the token; ie. the prefix, and then xor to get the mask.
12779 	 */
12780 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12781 		return (EINVAL);
12782 	for (i = 0; i < 4; i++) {
12783 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12784 	}
12785 
12786 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12787 	    ill->ill_token_length == addrlen)
12788 		return (0);	/* No change */
12789 
12790 	if (ipif->ipif_flags & IPIF_UP) {
12791 		err = ipif_logical_down(ipif, q, mp);
12792 		if (err == EINPROGRESS)
12793 			return (err);
12794 		ipif_down_tail(ipif);
12795 		need_up = B_TRUE;
12796 	}
12797 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12798 	return (err);
12799 }
12800 
12801 static int
12802 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12803     mblk_t *mp, boolean_t need_up)
12804 {
12805 	in6_addr_t v6addr;
12806 	in6_addr_t v6mask;
12807 	ill_t	*ill = ipif->ipif_ill;
12808 	int	i;
12809 	int	err = 0;
12810 
12811 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12812 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12813 	v6addr = sin6->sin6_addr;
12814 	/*
12815 	 * The length of the token is the length from the end.  To get
12816 	 * the proper mask for this, compute the mask of the bits not
12817 	 * in the token; ie. the prefix, and then xor to get the mask.
12818 	 */
12819 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12820 	for (i = 0; i < 4; i++)
12821 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12822 
12823 	mutex_enter(&ill->ill_lock);
12824 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12825 	ill->ill_token_length = addrlen;
12826 	mutex_exit(&ill->ill_lock);
12827 
12828 	if (need_up) {
12829 		/*
12830 		 * Now bring the interface back up.  If this
12831 		 * is the only IPIF for the ILL, ipif_up
12832 		 * will have to re-bind to the device, so
12833 		 * we may get back EINPROGRESS, in which
12834 		 * case, this IOCTL will get completed in
12835 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12836 		 */
12837 		err = ipif_up(ipif, q, mp);
12838 		if (err == EINPROGRESS)
12839 			return (err);
12840 	}
12841 	return (err);
12842 }
12843 
12844 /* ARGSUSED */
12845 int
12846 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12847     ip_ioctl_cmd_t *ipi, void *if_req)
12848 {
12849 	ill_t *ill;
12850 	sin6_t *sin6 = (sin6_t *)sin;
12851 	struct lifreq *lifr = (struct lifreq *)if_req;
12852 
12853 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12854 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12855 	if (ipif->ipif_id != 0)
12856 		return (EINVAL);
12857 
12858 	ill = ipif->ipif_ill;
12859 	if (!ill->ill_isv6)
12860 		return (ENXIO);
12861 
12862 	*sin6 = sin6_null;
12863 	sin6->sin6_family = AF_INET6;
12864 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12865 	sin6->sin6_addr = ill->ill_token;
12866 	lifr->lifr_addrlen = ill->ill_token_length;
12867 	return (0);
12868 }
12869 
12870 /*
12871  * Set (hardware) link specific information that might override
12872  * what was acquired through the DL_INFO_ACK.
12873  * The logic is as follows.
12874  *
12875  * become exclusive
12876  * set CHANGING flag
12877  * change mtu on affected IREs
12878  * clear CHANGING flag
12879  *
12880  * An ire add that occurs before the CHANGING flag is set will have its mtu
12881  * changed by the ip_sioctl_lnkinfo.
12882  *
12883  * During the time the CHANGING flag is set, no new ires will be added to the
12884  * bucket, and ire add will fail (due the CHANGING flag).
12885  *
12886  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12887  * before it is added to the bucket.
12888  *
12889  * Obviously only 1 thread can set the CHANGING flag and we need to become
12890  * exclusive to set the flag.
12891  */
12892 /* ARGSUSED */
12893 int
12894 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12895     ip_ioctl_cmd_t *ipi, void *if_req)
12896 {
12897 	ill_t		*ill = ipif->ipif_ill;
12898 	ipif_t		*nipif;
12899 	int		ip_min_mtu;
12900 	boolean_t	mtu_walk = B_FALSE;
12901 	struct lifreq	*lifr = (struct lifreq *)if_req;
12902 	lif_ifinfo_req_t *lir;
12903 	ire_t		*ire;
12904 
12905 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12906 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12907 	lir = &lifr->lifr_ifinfo;
12908 	ASSERT(IAM_WRITER_IPIF(ipif));
12909 
12910 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12911 	if (ipif->ipif_id != 0)
12912 		return (EINVAL);
12913 
12914 	/* Set interface MTU. */
12915 	if (ipif->ipif_isv6)
12916 		ip_min_mtu = IPV6_MIN_MTU;
12917 	else
12918 		ip_min_mtu = IP_MIN_MTU;
12919 
12920 	/*
12921 	 * Verify values before we set anything. Allow zero to
12922 	 * mean unspecified.
12923 	 */
12924 	if (lir->lir_maxmtu != 0 &&
12925 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12926 	    lir->lir_maxmtu < ip_min_mtu))
12927 		return (EINVAL);
12928 	if (lir->lir_reachtime != 0 &&
12929 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12930 		return (EINVAL);
12931 	if (lir->lir_reachretrans != 0 &&
12932 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12933 		return (EINVAL);
12934 
12935 	mutex_enter(&ill->ill_lock);
12936 	ill->ill_state_flags |= ILL_CHANGING;
12937 	for (nipif = ill->ill_ipif; nipif != NULL;
12938 	    nipif = nipif->ipif_next) {
12939 		nipif->ipif_state_flags |= IPIF_CHANGING;
12940 	}
12941 
12942 	mutex_exit(&ill->ill_lock);
12943 
12944 	if (lir->lir_maxmtu != 0) {
12945 		ill->ill_max_mtu = lir->lir_maxmtu;
12946 		ill->ill_mtu_userspecified = 1;
12947 		mtu_walk = B_TRUE;
12948 	}
12949 
12950 	if (lir->lir_reachtime != 0)
12951 		ill->ill_reachable_time = lir->lir_reachtime;
12952 
12953 	if (lir->lir_reachretrans != 0)
12954 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12955 
12956 	ill->ill_max_hops = lir->lir_maxhops;
12957 
12958 	ill->ill_max_buf = ND_MAX_Q;
12959 
12960 	if (mtu_walk) {
12961 		/*
12962 		 * Set the MTU on all ipifs associated with this ill except
12963 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12964 		 */
12965 		for (nipif = ill->ill_ipif; nipif != NULL;
12966 		    nipif = nipif->ipif_next) {
12967 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12968 				continue;
12969 
12970 			nipif->ipif_mtu = ill->ill_max_mtu;
12971 
12972 			if (!(nipif->ipif_flags & IPIF_UP))
12973 				continue;
12974 
12975 			if (nipif->ipif_isv6)
12976 				ire = ipif_to_ire_v6(nipif);
12977 			else
12978 				ire = ipif_to_ire(nipif);
12979 			if (ire != NULL) {
12980 				ire->ire_max_frag = ipif->ipif_mtu;
12981 				ire_refrele(ire);
12982 			}
12983 
12984 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12985 			    nipif, ill);
12986 		}
12987 	}
12988 
12989 	mutex_enter(&ill->ill_lock);
12990 	for (nipif = ill->ill_ipif; nipif != NULL;
12991 	    nipif = nipif->ipif_next) {
12992 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12993 	}
12994 	ILL_UNMARK_CHANGING(ill);
12995 	mutex_exit(&ill->ill_lock);
12996 
12997 	return (0);
12998 }
12999 
13000 /* ARGSUSED */
13001 int
13002 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13003     ip_ioctl_cmd_t *ipi, void *if_req)
13004 {
13005 	struct lif_ifinfo_req *lir;
13006 	ill_t *ill = ipif->ipif_ill;
13007 
13008 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13009 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13010 	if (ipif->ipif_id != 0)
13011 		return (EINVAL);
13012 
13013 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13014 	lir->lir_maxhops = ill->ill_max_hops;
13015 	lir->lir_reachtime = ill->ill_reachable_time;
13016 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13017 	lir->lir_maxmtu = ill->ill_max_mtu;
13018 
13019 	return (0);
13020 }
13021 
13022 /*
13023  * Return best guess as to the subnet mask for the specified address.
13024  * Based on the subnet masks for all the configured interfaces.
13025  *
13026  * We end up returning a zero mask in the case of default, multicast or
13027  * experimental.
13028  */
13029 static ipaddr_t
13030 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13031 {
13032 	ipaddr_t net_mask;
13033 	ill_t	*ill;
13034 	ipif_t	*ipif;
13035 	ill_walk_context_t ctx;
13036 	ipif_t	*fallback_ipif = NULL;
13037 
13038 	net_mask = ip_net_mask(addr);
13039 	if (net_mask == 0) {
13040 		*ipifp = NULL;
13041 		return (0);
13042 	}
13043 
13044 	/* Let's check to see if this is maybe a local subnet route. */
13045 	/* this function only applies to IPv4 interfaces */
13046 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13047 	ill = ILL_START_WALK_V4(&ctx, ipst);
13048 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13049 		mutex_enter(&ill->ill_lock);
13050 		for (ipif = ill->ill_ipif; ipif != NULL;
13051 		    ipif = ipif->ipif_next) {
13052 			if (!IPIF_CAN_LOOKUP(ipif))
13053 				continue;
13054 			if (!(ipif->ipif_flags & IPIF_UP))
13055 				continue;
13056 			if ((ipif->ipif_subnet & net_mask) ==
13057 			    (addr & net_mask)) {
13058 				/*
13059 				 * Don't trust pt-pt interfaces if there are
13060 				 * other interfaces.
13061 				 */
13062 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13063 					if (fallback_ipif == NULL) {
13064 						ipif_refhold_locked(ipif);
13065 						fallback_ipif = ipif;
13066 					}
13067 					continue;
13068 				}
13069 
13070 				/*
13071 				 * Fine. Just assume the same net mask as the
13072 				 * directly attached subnet interface is using.
13073 				 */
13074 				ipif_refhold_locked(ipif);
13075 				mutex_exit(&ill->ill_lock);
13076 				rw_exit(&ipst->ips_ill_g_lock);
13077 				if (fallback_ipif != NULL)
13078 					ipif_refrele(fallback_ipif);
13079 				*ipifp = ipif;
13080 				return (ipif->ipif_net_mask);
13081 			}
13082 		}
13083 		mutex_exit(&ill->ill_lock);
13084 	}
13085 	rw_exit(&ipst->ips_ill_g_lock);
13086 
13087 	*ipifp = fallback_ipif;
13088 	return ((fallback_ipif != NULL) ?
13089 	    fallback_ipif->ipif_net_mask : net_mask);
13090 }
13091 
13092 /*
13093  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13094  */
13095 static void
13096 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13097 {
13098 	IOCP	iocp;
13099 	ipft_t	*ipft;
13100 	ipllc_t	*ipllc;
13101 	mblk_t	*mp1;
13102 	cred_t	*cr;
13103 	int	error = 0;
13104 	conn_t	*connp;
13105 
13106 	ip1dbg(("ip_wput_ioctl"));
13107 	iocp = (IOCP)mp->b_rptr;
13108 	mp1 = mp->b_cont;
13109 	if (mp1 == NULL) {
13110 		iocp->ioc_error = EINVAL;
13111 		mp->b_datap->db_type = M_IOCNAK;
13112 		iocp->ioc_count = 0;
13113 		qreply(q, mp);
13114 		return;
13115 	}
13116 
13117 	/*
13118 	 * These IOCTLs provide various control capabilities to
13119 	 * upstream agents such as ULPs and processes.	There
13120 	 * are currently two such IOCTLs implemented.  They
13121 	 * are used by TCP to provide update information for
13122 	 * existing IREs and to forcibly delete an IRE for a
13123 	 * host that is not responding, thereby forcing an
13124 	 * attempt at a new route.
13125 	 */
13126 	iocp->ioc_error = EINVAL;
13127 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13128 		goto done;
13129 
13130 	ipllc = (ipllc_t *)mp1->b_rptr;
13131 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13132 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13133 			break;
13134 	}
13135 	/*
13136 	 * prefer credential from mblk over ioctl;
13137 	 * see ip_sioctl_copyin_setup
13138 	 */
13139 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13140 
13141 	/*
13142 	 * Refhold the conn in case the request gets queued up in some lookup
13143 	 */
13144 	ASSERT(CONN_Q(q));
13145 	connp = Q_TO_CONN(q);
13146 	CONN_INC_REF(connp);
13147 	if (ipft->ipft_pfi &&
13148 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13149 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13150 		error = (*ipft->ipft_pfi)(q,
13151 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13152 	}
13153 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13154 		/*
13155 		 * CONN_OPER_PENDING_DONE happens in the function called
13156 		 * through ipft_pfi above.
13157 		 */
13158 		return;
13159 	}
13160 
13161 	CONN_OPER_PENDING_DONE(connp);
13162 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13163 		freemsg(mp);
13164 		return;
13165 	}
13166 	iocp->ioc_error = error;
13167 
13168 done:
13169 	mp->b_datap->db_type = M_IOCACK;
13170 	if (iocp->ioc_error)
13171 		iocp->ioc_count = 0;
13172 	qreply(q, mp);
13173 }
13174 
13175 /*
13176  * Lookup an ipif using the sequence id (ipif_seqid)
13177  */
13178 ipif_t *
13179 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13180 {
13181 	ipif_t *ipif;
13182 
13183 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13184 
13185 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13186 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13187 			return (ipif);
13188 	}
13189 	return (NULL);
13190 }
13191 
13192 /*
13193  * Assign a unique id for the ipif. This is used later when we send
13194  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13195  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13196  * IRE is added, we verify that ipif has not disappeared.
13197  */
13198 
13199 static void
13200 ipif_assign_seqid(ipif_t *ipif)
13201 {
13202 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13203 
13204 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13205 }
13206 
13207 /*
13208  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13209  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13210  * be inserted into the first space available in the list. The value of
13211  * ipif_id will then be set to the appropriate value for its position.
13212  */
13213 static int
13214 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13215 {
13216 	ill_t *ill;
13217 	ipif_t *tipif;
13218 	ipif_t **tipifp;
13219 	int id;
13220 	ip_stack_t	*ipst;
13221 
13222 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13223 	    IAM_WRITER_IPIF(ipif));
13224 
13225 	ill = ipif->ipif_ill;
13226 	ASSERT(ill != NULL);
13227 	ipst = ill->ill_ipst;
13228 
13229 	/*
13230 	 * In the case of lo0:0 we already hold the ill_g_lock.
13231 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13232 	 * ipif_insert. Another such caller is ipif_move.
13233 	 */
13234 	if (acquire_g_lock)
13235 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13236 	if (acquire_ill_lock)
13237 		mutex_enter(&ill->ill_lock);
13238 	id = ipif->ipif_id;
13239 	tipifp = &(ill->ill_ipif);
13240 	if (id == -1) {	/* need to find a real id */
13241 		id = 0;
13242 		while ((tipif = *tipifp) != NULL) {
13243 			ASSERT(tipif->ipif_id >= id);
13244 			if (tipif->ipif_id != id)
13245 				break; /* non-consecutive id */
13246 			id++;
13247 			tipifp = &(tipif->ipif_next);
13248 		}
13249 		/* limit number of logical interfaces */
13250 		if (id >= ipst->ips_ip_addrs_per_if) {
13251 			if (acquire_ill_lock)
13252 				mutex_exit(&ill->ill_lock);
13253 			if (acquire_g_lock)
13254 				rw_exit(&ipst->ips_ill_g_lock);
13255 			return (-1);
13256 		}
13257 		ipif->ipif_id = id; /* assign new id */
13258 	} else if (id < ipst->ips_ip_addrs_per_if) {
13259 		/* we have a real id; insert ipif in the right place */
13260 		while ((tipif = *tipifp) != NULL) {
13261 			ASSERT(tipif->ipif_id != id);
13262 			if (tipif->ipif_id > id)
13263 				break; /* found correct location */
13264 			tipifp = &(tipif->ipif_next);
13265 		}
13266 	} else {
13267 		if (acquire_ill_lock)
13268 			mutex_exit(&ill->ill_lock);
13269 		if (acquire_g_lock)
13270 			rw_exit(&ipst->ips_ill_g_lock);
13271 		return (-1);
13272 	}
13273 
13274 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13275 
13276 	ipif->ipif_next = tipif;
13277 	*tipifp = ipif;
13278 	if (acquire_ill_lock)
13279 		mutex_exit(&ill->ill_lock);
13280 	if (acquire_g_lock)
13281 		rw_exit(&ipst->ips_ill_g_lock);
13282 	return (0);
13283 }
13284 
13285 static void
13286 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13287 {
13288 	ipif_t	**ipifp;
13289 	ill_t	*ill = ipif->ipif_ill;
13290 
13291 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13292 	if (acquire_ill_lock)
13293 		mutex_enter(&ill->ill_lock);
13294 	else
13295 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13296 
13297 	ipifp = &ill->ill_ipif;
13298 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13299 		if (*ipifp == ipif) {
13300 			*ipifp = ipif->ipif_next;
13301 			break;
13302 		}
13303 	}
13304 
13305 	if (acquire_ill_lock)
13306 		mutex_exit(&ill->ill_lock);
13307 }
13308 
13309 /*
13310  * Allocate and initialize a new interface control structure.  (Always
13311  * called as writer.)
13312  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13313  * is not part of the global linked list of ills. ipif_seqid is unique
13314  * in the system and to preserve the uniqueness, it is assigned only
13315  * when ill becomes part of the global list. At that point ill will
13316  * have a name. If it doesn't get assigned here, it will get assigned
13317  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13318  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13319  * the interface flags or any other information from the DL_INFO_ACK for
13320  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13321  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13322  * second DL_INFO_ACK comes in from the driver.
13323  */
13324 static ipif_t *
13325 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13326 {
13327 	ipif_t	*ipif;
13328 	phyint_t *phyi;
13329 
13330 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13331 	    ill->ill_name, id, (void *)ill));
13332 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13333 
13334 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13335 		return (NULL);
13336 	*ipif = ipif_zero;	/* start clean */
13337 
13338 	ipif->ipif_ill = ill;
13339 	ipif->ipif_id = id;	/* could be -1 */
13340 	/*
13341 	 * Inherit the zoneid from the ill; for the shared stack instance
13342 	 * this is always the global zone
13343 	 */
13344 	ipif->ipif_zoneid = ill->ill_zoneid;
13345 
13346 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13347 
13348 	ipif->ipif_refcnt = 0;
13349 	ipif->ipif_saved_ire_cnt = 0;
13350 
13351 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13352 		mi_free(ipif);
13353 		return (NULL);
13354 	}
13355 	/* -1 id should have been replaced by real id */
13356 	id = ipif->ipif_id;
13357 	ASSERT(id >= 0);
13358 
13359 	if (ill->ill_name[0] != '\0')
13360 		ipif_assign_seqid(ipif);
13361 
13362 	/*
13363 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13364 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13365 	 * ioctl sets ipif_orig_ipifid to zero.
13366 	 */
13367 	ipif->ipif_orig_ipifid = id;
13368 
13369 	/*
13370 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13371 	 * The ipif is still not up and can't be looked up until the
13372 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13373 	 */
13374 	mutex_enter(&ill->ill_lock);
13375 	mutex_enter(&ill->ill_phyint->phyint_lock);
13376 	/*
13377 	 * Set the running flag when logical interface zero is created.
13378 	 * For subsequent logical interfaces, a DLPI link down
13379 	 * notification message may have cleared the running flag to
13380 	 * indicate the link is down, so we shouldn't just blindly set it.
13381 	 */
13382 	if (id == 0)
13383 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13384 	ipif->ipif_ire_type = ire_type;
13385 	phyi = ill->ill_phyint;
13386 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13387 
13388 	if (ipif->ipif_isv6) {
13389 		ill->ill_flags |= ILLF_IPV6;
13390 	} else {
13391 		ipaddr_t inaddr_any = INADDR_ANY;
13392 
13393 		ill->ill_flags |= ILLF_IPV4;
13394 
13395 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13396 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13397 		    &ipif->ipif_v6lcl_addr);
13398 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13399 		    &ipif->ipif_v6src_addr);
13400 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13401 		    &ipif->ipif_v6subnet);
13402 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13403 		    &ipif->ipif_v6net_mask);
13404 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13405 		    &ipif->ipif_v6brd_addr);
13406 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13407 		    &ipif->ipif_v6pp_dst_addr);
13408 	}
13409 
13410 	/*
13411 	 * Don't set the interface flags etc. now, will do it in
13412 	 * ip_ll_subnet_defaults.
13413 	 */
13414 	if (!initialize) {
13415 		mutex_exit(&ill->ill_lock);
13416 		mutex_exit(&ill->ill_phyint->phyint_lock);
13417 		return (ipif);
13418 	}
13419 	ipif->ipif_mtu = ill->ill_max_mtu;
13420 
13421 	if (ill->ill_bcast_addr_length != 0) {
13422 		/*
13423 		 * Later detect lack of DLPI driver multicast
13424 		 * capability by catching DL_ENABMULTI errors in
13425 		 * ip_rput_dlpi.
13426 		 */
13427 		ill->ill_flags |= ILLF_MULTICAST;
13428 		if (!ipif->ipif_isv6)
13429 			ipif->ipif_flags |= IPIF_BROADCAST;
13430 	} else {
13431 		if (ill->ill_net_type != IRE_LOOPBACK) {
13432 			if (ipif->ipif_isv6)
13433 				/*
13434 				 * Note: xresolv interfaces will eventually need
13435 				 * NOARP set here as well, but that will require
13436 				 * those external resolvers to have some
13437 				 * knowledge of that flag and act appropriately.
13438 				 * Not to be changed at present.
13439 				 */
13440 				ill->ill_flags |= ILLF_NONUD;
13441 			else
13442 				ill->ill_flags |= ILLF_NOARP;
13443 		}
13444 		if (ill->ill_phys_addr_length == 0) {
13445 			if (ill->ill_media &&
13446 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13447 				ipif->ipif_flags |= IPIF_NOXMIT;
13448 				phyi->phyint_flags |= PHYI_VIRTUAL;
13449 			} else {
13450 				/* pt-pt supports multicast. */
13451 				ill->ill_flags |= ILLF_MULTICAST;
13452 				if (ill->ill_net_type == IRE_LOOPBACK) {
13453 					phyi->phyint_flags |=
13454 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13455 				} else {
13456 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13457 				}
13458 			}
13459 		}
13460 	}
13461 	mutex_exit(&ill->ill_lock);
13462 	mutex_exit(&ill->ill_phyint->phyint_lock);
13463 	return (ipif);
13464 }
13465 
13466 /*
13467  * If appropriate, send a message up to the resolver delete the entry
13468  * for the address of this interface which is going out of business.
13469  * (Always called as writer).
13470  *
13471  * NOTE : We need to check for NULL mps as some of the fields are
13472  *	  initialized only for some interface types. See ipif_resolver_up()
13473  *	  for details.
13474  */
13475 void
13476 ipif_arp_down(ipif_t *ipif)
13477 {
13478 	mblk_t	*mp;
13479 	ill_t	*ill = ipif->ipif_ill;
13480 
13481 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13482 	ASSERT(IAM_WRITER_IPIF(ipif));
13483 
13484 	/* Delete the mapping for the local address */
13485 	mp = ipif->ipif_arp_del_mp;
13486 	if (mp != NULL) {
13487 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13488 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13489 		putnext(ill->ill_rq, mp);
13490 		ipif->ipif_arp_del_mp = NULL;
13491 	}
13492 
13493 	/*
13494 	 * If this is the last ipif that is going down and there are no
13495 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13496 	 * clean up ARP completely.
13497 	 */
13498 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13499 
13500 		/* Send up AR_INTERFACE_DOWN message */
13501 		mp = ill->ill_arp_down_mp;
13502 		if (mp != NULL) {
13503 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13504 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13505 			    ipif->ipif_id));
13506 			putnext(ill->ill_rq, mp);
13507 			ill->ill_arp_down_mp = NULL;
13508 		}
13509 
13510 		/* Tell ARP to delete the multicast mappings */
13511 		mp = ill->ill_arp_del_mapping_mp;
13512 		if (mp != NULL) {
13513 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13514 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13515 			    ipif->ipif_id));
13516 			putnext(ill->ill_rq, mp);
13517 			ill->ill_arp_del_mapping_mp = NULL;
13518 		}
13519 	}
13520 }
13521 
13522 /*
13523  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13524  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13525  * that it wants the add_mp allocated in this function to be returned
13526  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13527  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13528  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13529  * as it does a ipif_arp_down after calling this function - which will
13530  * remove what we add here.
13531  *
13532  * Returns -1 on failures and 0 on success.
13533  */
13534 int
13535 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13536 {
13537 	mblk_t	*del_mp = NULL;
13538 	mblk_t *add_mp = NULL;
13539 	mblk_t *mp;
13540 	ill_t	*ill = ipif->ipif_ill;
13541 	phyint_t *phyi = ill->ill_phyint;
13542 	ipaddr_t addr, mask, extract_mask = 0;
13543 	arma_t	*arma;
13544 	uint8_t *maddr, *bphys_addr;
13545 	uint32_t hw_start;
13546 	dl_unitdata_req_t *dlur;
13547 
13548 	ASSERT(IAM_WRITER_IPIF(ipif));
13549 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13550 		return (0);
13551 
13552 	/*
13553 	 * Delete the existing mapping from ARP. Normally ipif_down
13554 	 * -> ipif_arp_down should send this up to ARP. The only
13555 	 * reason we would find this when we are switching from
13556 	 * Multicast to Broadcast where we did not do a down.
13557 	 */
13558 	mp = ill->ill_arp_del_mapping_mp;
13559 	if (mp != NULL) {
13560 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13561 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13562 		putnext(ill->ill_rq, mp);
13563 		ill->ill_arp_del_mapping_mp = NULL;
13564 	}
13565 
13566 	if (arp_add_mapping_mp != NULL)
13567 		*arp_add_mapping_mp = NULL;
13568 
13569 	/*
13570 	 * Check that the address is not to long for the constant
13571 	 * length reserved in the template arma_t.
13572 	 */
13573 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13574 		return (-1);
13575 
13576 	/* Add mapping mblk */
13577 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13578 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13579 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13580 	    (caddr_t)&addr);
13581 	if (add_mp == NULL)
13582 		return (-1);
13583 	arma = (arma_t *)add_mp->b_rptr;
13584 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13585 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13586 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13587 
13588 	/*
13589 	 * Determine the broadcast address.
13590 	 */
13591 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13592 	if (ill->ill_sap_length < 0)
13593 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13594 	else
13595 		bphys_addr = (uchar_t *)dlur +
13596 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13597 	/*
13598 	 * Check PHYI_MULTI_BCAST and length of physical
13599 	 * address to determine if we use the mapping or the
13600 	 * broadcast address.
13601 	 */
13602 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13603 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13604 		    bphys_addr, maddr, &hw_start, &extract_mask))
13605 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13606 
13607 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13608 	    (ill->ill_flags & ILLF_MULTICAST)) {
13609 		/* Make sure this will not match the "exact" entry. */
13610 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13611 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13612 		    (caddr_t)&addr);
13613 		if (del_mp == NULL) {
13614 			freemsg(add_mp);
13615 			return (-1);
13616 		}
13617 		bcopy(&extract_mask, (char *)arma +
13618 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13619 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13620 			/* Use link-layer broadcast address for MULTI_BCAST */
13621 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13622 			ip2dbg(("ipif_arp_setup_multicast: adding"
13623 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13624 		} else {
13625 			arma->arma_hw_mapping_start = hw_start;
13626 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13627 			    " ARP setup for %s\n", ill->ill_name));
13628 		}
13629 	} else {
13630 		freemsg(add_mp);
13631 		ASSERT(del_mp == NULL);
13632 		/* It is neither MULTICAST nor MULTI_BCAST */
13633 		return (0);
13634 	}
13635 	ASSERT(add_mp != NULL && del_mp != NULL);
13636 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13637 	ill->ill_arp_del_mapping_mp = del_mp;
13638 	if (arp_add_mapping_mp != NULL) {
13639 		/* The caller just wants the mblks allocated */
13640 		*arp_add_mapping_mp = add_mp;
13641 	} else {
13642 		/* The caller wants us to send it to arp */
13643 		putnext(ill->ill_rq, add_mp);
13644 	}
13645 	return (0);
13646 }
13647 
13648 /*
13649  * Get the resolver set up for a new interface address.
13650  * (Always called as writer.)
13651  * Called both for IPv4 and IPv6 interfaces,
13652  * though it only sets up the resolver for v6
13653  * if it's an xresolv interface (one using an external resolver).
13654  * Honors ILLF_NOARP.
13655  * The enumerated value res_act is used to tune the behavior.
13656  * If set to Res_act_initial, then we set up all the resolver
13657  * structures for a new interface.  If set to Res_act_move, then
13658  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13659  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13660  * asynchronous hardware address change notification.  If set to
13661  * Res_act_defend, then we tell ARP that it needs to send a single
13662  * gratuitous message in defense of the address.
13663  * Returns error on failure.
13664  */
13665 int
13666 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13667 {
13668 	caddr_t	addr;
13669 	mblk_t	*arp_up_mp = NULL;
13670 	mblk_t	*arp_down_mp = NULL;
13671 	mblk_t	*arp_add_mp = NULL;
13672 	mblk_t	*arp_del_mp = NULL;
13673 	mblk_t	*arp_add_mapping_mp = NULL;
13674 	mblk_t	*arp_del_mapping_mp = NULL;
13675 	ill_t	*ill = ipif->ipif_ill;
13676 	uchar_t	*area_p = NULL;
13677 	uchar_t	*ared_p = NULL;
13678 	int	err = ENOMEM;
13679 	boolean_t was_dup;
13680 
13681 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13682 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13683 	ASSERT(IAM_WRITER_IPIF(ipif));
13684 
13685 	was_dup = B_FALSE;
13686 	if (res_act == Res_act_initial) {
13687 		ipif->ipif_addr_ready = 0;
13688 		/*
13689 		 * We're bringing an interface up here.  There's no way that we
13690 		 * should need to shut down ARP now.
13691 		 */
13692 		mutex_enter(&ill->ill_lock);
13693 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13694 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13695 			ill->ill_ipif_dup_count--;
13696 			was_dup = B_TRUE;
13697 		}
13698 		mutex_exit(&ill->ill_lock);
13699 	}
13700 	if (ipif->ipif_recovery_id != 0)
13701 		(void) untimeout(ipif->ipif_recovery_id);
13702 	ipif->ipif_recovery_id = 0;
13703 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13704 		ipif->ipif_addr_ready = 1;
13705 		return (0);
13706 	}
13707 	/* NDP will set the ipif_addr_ready flag when it's ready */
13708 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13709 		return (0);
13710 
13711 	if (ill->ill_isv6) {
13712 		/*
13713 		 * External resolver for IPv6
13714 		 */
13715 		ASSERT(res_act == Res_act_initial);
13716 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13717 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13718 			area_p = (uchar_t *)&ip6_area_template;
13719 			ared_p = (uchar_t *)&ip6_ared_template;
13720 		}
13721 	} else {
13722 		/*
13723 		 * IPv4 arp case. If the ARP stream has already started
13724 		 * closing, fail this request for ARP bringup. Else
13725 		 * record the fact that an ARP bringup is pending.
13726 		 */
13727 		mutex_enter(&ill->ill_lock);
13728 		if (ill->ill_arp_closing) {
13729 			mutex_exit(&ill->ill_lock);
13730 			err = EINVAL;
13731 			goto failed;
13732 		} else {
13733 			if (ill->ill_ipif_up_count == 0 &&
13734 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13735 				ill->ill_arp_bringup_pending = 1;
13736 			mutex_exit(&ill->ill_lock);
13737 		}
13738 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13739 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13740 			area_p = (uchar_t *)&ip_area_template;
13741 			ared_p = (uchar_t *)&ip_ared_template;
13742 		}
13743 	}
13744 
13745 	/*
13746 	 * Add an entry for the local address in ARP only if it
13747 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13748 	 */
13749 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13750 		area_t *area;
13751 
13752 		/* Now ask ARP to publish our address. */
13753 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13754 		if (arp_add_mp == NULL)
13755 			goto failed;
13756 		area = (area_t *)arp_add_mp->b_rptr;
13757 		if (res_act != Res_act_initial) {
13758 			/*
13759 			 * Copy the new hardware address and length into
13760 			 * arp_add_mp to be sent to ARP.
13761 			 */
13762 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13763 			bcopy(ill->ill_phys_addr,
13764 			    ((char *)area + area->area_hw_addr_offset),
13765 			    area->area_hw_addr_length);
13766 		}
13767 
13768 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13769 		    ACE_F_MYADDR;
13770 
13771 		if (res_act == Res_act_defend) {
13772 			area->area_flags |= ACE_F_DEFEND;
13773 			/*
13774 			 * If we're just defending our address now, then
13775 			 * there's no need to set up ARP multicast mappings.
13776 			 * The publish command is enough.
13777 			 */
13778 			goto done;
13779 		}
13780 
13781 		if (res_act != Res_act_initial)
13782 			goto arp_setup_multicast;
13783 
13784 		/*
13785 		 * Allocate an ARP deletion message so we know we can tell ARP
13786 		 * when the interface goes down.
13787 		 */
13788 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13789 		if (arp_del_mp == NULL)
13790 			goto failed;
13791 
13792 	} else {
13793 		if (res_act != Res_act_initial)
13794 			goto done;
13795 	}
13796 	/*
13797 	 * Need to bring up ARP or setup multicast mapping only
13798 	 * when the first interface is coming UP.
13799 	 */
13800 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13801 	    was_dup) {
13802 		goto done;
13803 	}
13804 
13805 	/*
13806 	 * Allocate an ARP down message (to be saved) and an ARP up
13807 	 * message.
13808 	 */
13809 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13810 	if (arp_down_mp == NULL)
13811 		goto failed;
13812 
13813 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13814 	if (arp_up_mp == NULL)
13815 		goto failed;
13816 
13817 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13818 		goto done;
13819 
13820 arp_setup_multicast:
13821 	/*
13822 	 * Setup the multicast mappings. This function initializes
13823 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13824 	 * IPv6.
13825 	 */
13826 	if (!ill->ill_isv6) {
13827 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13828 		if (err != 0)
13829 			goto failed;
13830 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13831 		ASSERT(arp_add_mapping_mp != NULL);
13832 	}
13833 
13834 done:
13835 	if (arp_del_mp != NULL) {
13836 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13837 		ipif->ipif_arp_del_mp = arp_del_mp;
13838 	}
13839 	if (arp_down_mp != NULL) {
13840 		ASSERT(ill->ill_arp_down_mp == NULL);
13841 		ill->ill_arp_down_mp = arp_down_mp;
13842 	}
13843 	if (arp_del_mapping_mp != NULL) {
13844 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13845 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13846 	}
13847 	if (arp_up_mp != NULL) {
13848 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13849 		    ill->ill_name, ipif->ipif_id));
13850 		putnext(ill->ill_rq, arp_up_mp);
13851 	}
13852 	if (arp_add_mp != NULL) {
13853 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13854 		    ill->ill_name, ipif->ipif_id));
13855 		/*
13856 		 * If it's an extended ARP implementation, then we'll wait to
13857 		 * hear that DAD has finished before using the interface.
13858 		 */
13859 		if (!ill->ill_arp_extend)
13860 			ipif->ipif_addr_ready = 1;
13861 		putnext(ill->ill_rq, arp_add_mp);
13862 	} else {
13863 		ipif->ipif_addr_ready = 1;
13864 	}
13865 	if (arp_add_mapping_mp != NULL) {
13866 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13867 		    ill->ill_name, ipif->ipif_id));
13868 		putnext(ill->ill_rq, arp_add_mapping_mp);
13869 	}
13870 	if (res_act != Res_act_initial)
13871 		return (0);
13872 
13873 	if (ill->ill_flags & ILLF_NOARP)
13874 		err = ill_arp_off(ill);
13875 	else
13876 		err = ill_arp_on(ill);
13877 	if (err != 0) {
13878 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13879 		freemsg(ipif->ipif_arp_del_mp);
13880 		freemsg(ill->ill_arp_down_mp);
13881 		freemsg(ill->ill_arp_del_mapping_mp);
13882 		ipif->ipif_arp_del_mp = NULL;
13883 		ill->ill_arp_down_mp = NULL;
13884 		ill->ill_arp_del_mapping_mp = NULL;
13885 		return (err);
13886 	}
13887 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13888 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13889 
13890 failed:
13891 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13892 	freemsg(arp_add_mp);
13893 	freemsg(arp_del_mp);
13894 	freemsg(arp_add_mapping_mp);
13895 	freemsg(arp_up_mp);
13896 	freemsg(arp_down_mp);
13897 	ill->ill_arp_bringup_pending = 0;
13898 	return (err);
13899 }
13900 
13901 /*
13902  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13903  * just gone back up.
13904  */
13905 static void
13906 ipif_arp_start_dad(ipif_t *ipif)
13907 {
13908 	ill_t *ill = ipif->ipif_ill;
13909 	mblk_t *arp_add_mp;
13910 	area_t *area;
13911 
13912 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13913 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13914 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13915 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13916 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13917 		/*
13918 		 * If we can't contact ARP for some reason, that's not really a
13919 		 * problem.  Just send out the routing socket notification that
13920 		 * DAD completion would have done, and continue.
13921 		 */
13922 		ipif_mask_reply(ipif);
13923 		ip_rts_ifmsg(ipif);
13924 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13925 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13926 		ipif->ipif_addr_ready = 1;
13927 		return;
13928 	}
13929 
13930 	/* Setting the 'unverified' flag restarts DAD */
13931 	area = (area_t *)arp_add_mp->b_rptr;
13932 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13933 	    ACE_F_UNVERIFIED;
13934 	putnext(ill->ill_rq, arp_add_mp);
13935 }
13936 
13937 static void
13938 ipif_ndp_start_dad(ipif_t *ipif)
13939 {
13940 	nce_t *nce;
13941 
13942 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13943 	if (nce == NULL)
13944 		return;
13945 
13946 	if (!ndp_restart_dad(nce)) {
13947 		/*
13948 		 * If we can't restart DAD for some reason, that's not really a
13949 		 * problem.  Just send out the routing socket notification that
13950 		 * DAD completion would have done, and continue.
13951 		 */
13952 		ip_rts_ifmsg(ipif);
13953 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13954 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13955 		ipif->ipif_addr_ready = 1;
13956 	}
13957 	NCE_REFRELE(nce);
13958 }
13959 
13960 /*
13961  * Restart duplicate address detection on all interfaces on the given ill.
13962  *
13963  * This is called when an interface transitions from down to up
13964  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13965  *
13966  * Note that since the underlying physical link has transitioned, we must cause
13967  * at least one routing socket message to be sent here, either via DAD
13968  * completion or just by default on the first ipif.  (If we don't do this, then
13969  * in.mpathd will see long delays when doing link-based failure recovery.)
13970  */
13971 void
13972 ill_restart_dad(ill_t *ill, boolean_t went_up)
13973 {
13974 	ipif_t *ipif;
13975 
13976 	if (ill == NULL)
13977 		return;
13978 
13979 	/*
13980 	 * If layer two doesn't support duplicate address detection, then just
13981 	 * send the routing socket message now and be done with it.
13982 	 */
13983 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13984 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13985 		ip_rts_ifmsg(ill->ill_ipif);
13986 		return;
13987 	}
13988 
13989 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13990 		if (went_up) {
13991 			if (ipif->ipif_flags & IPIF_UP) {
13992 				if (ill->ill_isv6)
13993 					ipif_ndp_start_dad(ipif);
13994 				else
13995 					ipif_arp_start_dad(ipif);
13996 			} else if (ill->ill_isv6 &&
13997 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13998 				/*
13999 				 * For IPv4, the ARP module itself will
14000 				 * automatically start the DAD process when it
14001 				 * sees DL_NOTE_LINK_UP.  We respond to the
14002 				 * AR_CN_READY at the completion of that task.
14003 				 * For IPv6, we must kick off the bring-up
14004 				 * process now.
14005 				 */
14006 				ndp_do_recovery(ipif);
14007 			} else {
14008 				/*
14009 				 * Unfortunately, the first ipif is "special"
14010 				 * and represents the underlying ill in the
14011 				 * routing socket messages.  Thus, when this
14012 				 * one ipif is down, we must still notify so
14013 				 * that the user knows the IFF_RUNNING status
14014 				 * change.  (If the first ipif is up, then
14015 				 * we'll handle eventual routing socket
14016 				 * notification via DAD completion.)
14017 				 */
14018 				if (ipif == ill->ill_ipif)
14019 					ip_rts_ifmsg(ill->ill_ipif);
14020 			}
14021 		} else {
14022 			/*
14023 			 * After link down, we'll need to send a new routing
14024 			 * message when the link comes back, so clear
14025 			 * ipif_addr_ready.
14026 			 */
14027 			ipif->ipif_addr_ready = 0;
14028 		}
14029 	}
14030 
14031 	/*
14032 	 * If we've torn down links, then notify the user right away.
14033 	 */
14034 	if (!went_up)
14035 		ip_rts_ifmsg(ill->ill_ipif);
14036 }
14037 
14038 /*
14039  * Wakeup all threads waiting to enter the ipsq, and sleeping
14040  * on any of the ills in this ipsq. The ill_lock of the ill
14041  * must be held so that waiters don't miss wakeups
14042  */
14043 static void
14044 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14045 {
14046 	phyint_t *phyint;
14047 
14048 	phyint = ipsq->ipsq_phyint_list;
14049 	while (phyint != NULL) {
14050 		if (phyint->phyint_illv4) {
14051 			if (!caller_holds_lock)
14052 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14053 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14054 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14055 			if (!caller_holds_lock)
14056 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14057 		}
14058 		if (phyint->phyint_illv6) {
14059 			if (!caller_holds_lock)
14060 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14061 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14062 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14063 			if (!caller_holds_lock)
14064 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14065 		}
14066 		phyint = phyint->phyint_ipsq_next;
14067 	}
14068 }
14069 
14070 static ipsq_t *
14071 ipsq_create(char *groupname, ip_stack_t *ipst)
14072 {
14073 	ipsq_t	*ipsq;
14074 
14075 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14076 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14077 	if (ipsq == NULL) {
14078 		return (NULL);
14079 	}
14080 
14081 	if (groupname != NULL)
14082 		(void) strcpy(ipsq->ipsq_name, groupname);
14083 	else
14084 		ipsq->ipsq_name[0] = '\0';
14085 
14086 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14087 	ipsq->ipsq_flags |= IPSQ_GROUP;
14088 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14089 	ipst->ips_ipsq_g_head = ipsq;
14090 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14091 	return (ipsq);
14092 }
14093 
14094 /*
14095  * Return an ipsq correspoding to the groupname. If 'create' is true
14096  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14097  * uniquely with an IPMP group. However during IPMP groupname operations,
14098  * multiple IPMP groups may be associated with a single ipsq. But no
14099  * IPMP group can be associated with more than 1 ipsq at any time.
14100  * For example
14101  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14102  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14103  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14104  *
14105  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14106  * status shown below during the execution of the above command.
14107  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14108  *
14109  * After the completion of the above groupname command we return to the stable
14110  * state shown below.
14111  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14112  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14113  *
14114  * Because of the above, we don't search based on the ipsq_name since that
14115  * would miss the correct ipsq during certain windows as shown above.
14116  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14117  * natural state.
14118  */
14119 static ipsq_t *
14120 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14121     ip_stack_t *ipst)
14122 {
14123 	ipsq_t	*ipsq;
14124 	int	group_len;
14125 	phyint_t *phyint;
14126 
14127 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14128 
14129 	group_len = strlen(groupname);
14130 	ASSERT(group_len != 0);
14131 	group_len++;
14132 
14133 	for (ipsq = ipst->ips_ipsq_g_head;
14134 	    ipsq != NULL;
14135 	    ipsq = ipsq->ipsq_next) {
14136 		/*
14137 		 * When an ipsq is being split, and ill_split_ipsq
14138 		 * calls this function, we exclude it from being considered.
14139 		 */
14140 		if (ipsq == exclude_ipsq)
14141 			continue;
14142 
14143 		/*
14144 		 * Compare against the ipsq_name. The groupname change happens
14145 		 * in 2 phases. The 1st phase merges the from group into
14146 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14147 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14148 		 * ipsq_name. At this point the phyint_groupname has not been
14149 		 * updated.
14150 		 */
14151 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14152 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14153 			/*
14154 			 * Verify that an ipmp groupname is exactly
14155 			 * part of 1 ipsq and is not found in any other
14156 			 * ipsq.
14157 			 */
14158 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14159 			    NULL);
14160 			return (ipsq);
14161 		}
14162 
14163 		/*
14164 		 * Comparison against ipsq_name alone is not sufficient.
14165 		 * In the case when groups are currently being
14166 		 * merged, the ipsq could hold other IPMP groups temporarily.
14167 		 * so we walk the phyint list and compare against the
14168 		 * phyint_groupname as well.
14169 		 */
14170 		phyint = ipsq->ipsq_phyint_list;
14171 		while (phyint != NULL) {
14172 			if ((group_len == phyint->phyint_groupname_len) &&
14173 			    (bcmp(phyint->phyint_groupname, groupname,
14174 			    group_len) == 0)) {
14175 				/*
14176 				 * Verify that an ipmp groupname is exactly
14177 				 * part of 1 ipsq and is not found in any other
14178 				 * ipsq.
14179 				 */
14180 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14181 				    ipst) == NULL);
14182 				return (ipsq);
14183 			}
14184 			phyint = phyint->phyint_ipsq_next;
14185 		}
14186 	}
14187 	if (create)
14188 		ipsq = ipsq_create(groupname, ipst);
14189 	return (ipsq);
14190 }
14191 
14192 static void
14193 ipsq_delete(ipsq_t *ipsq)
14194 {
14195 	ipsq_t *nipsq;
14196 	ipsq_t *pipsq = NULL;
14197 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14198 
14199 	/*
14200 	 * We don't hold the ipsq lock, but we are sure no new
14201 	 * messages can land up, since the ipsq_refs is zero.
14202 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14203 	 * is associated with this ipsq. (Lookups are based on ill_name
14204 	 * or phyint_groupname)
14205 	 */
14206 	ASSERT(ipsq->ipsq_refs == 0);
14207 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14208 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14209 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14210 		/*
14211 		 * This is not the ipsq of an IPMP group.
14212 		 */
14213 		ipsq->ipsq_ipst = NULL;
14214 		kmem_free(ipsq, sizeof (ipsq_t));
14215 		return;
14216 	}
14217 
14218 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14219 
14220 	/*
14221 	 * Locate the ipsq  before we can remove it from
14222 	 * the singly linked list of ipsq's.
14223 	 */
14224 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14225 	    nipsq = nipsq->ipsq_next) {
14226 		if (nipsq == ipsq) {
14227 			break;
14228 		}
14229 		pipsq = nipsq;
14230 	}
14231 
14232 	ASSERT(nipsq == ipsq);
14233 
14234 	/* unlink ipsq from the list */
14235 	if (pipsq != NULL)
14236 		pipsq->ipsq_next = ipsq->ipsq_next;
14237 	else
14238 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14239 	ipsq->ipsq_ipst = NULL;
14240 	kmem_free(ipsq, sizeof (ipsq_t));
14241 	rw_exit(&ipst->ips_ill_g_lock);
14242 }
14243 
14244 static void
14245 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14246     queue_t *q)
14247 {
14248 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14249 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14250 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14251 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14252 	ASSERT(current_mp != NULL);
14253 
14254 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14255 	    NEW_OP, NULL);
14256 
14257 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14258 	    new_ipsq->ipsq_xopq_mphead != NULL);
14259 
14260 	/*
14261 	 * move from old ipsq to the new ipsq.
14262 	 */
14263 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14264 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14265 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14266 
14267 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14268 }
14269 
14270 void
14271 ill_group_cleanup(ill_t *ill)
14272 {
14273 	ill_t *ill_v4;
14274 	ill_t *ill_v6;
14275 	ipif_t *ipif;
14276 
14277 	ill_v4 = ill->ill_phyint->phyint_illv4;
14278 	ill_v6 = ill->ill_phyint->phyint_illv6;
14279 
14280 	if (ill_v4 != NULL) {
14281 		mutex_enter(&ill_v4->ill_lock);
14282 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14283 		    ipif = ipif->ipif_next) {
14284 			IPIF_UNMARK_MOVING(ipif);
14285 		}
14286 		ill_v4->ill_up_ipifs = B_FALSE;
14287 		mutex_exit(&ill_v4->ill_lock);
14288 	}
14289 
14290 	if (ill_v6 != NULL) {
14291 		mutex_enter(&ill_v6->ill_lock);
14292 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14293 		    ipif = ipif->ipif_next) {
14294 			IPIF_UNMARK_MOVING(ipif);
14295 		}
14296 		ill_v6->ill_up_ipifs = B_FALSE;
14297 		mutex_exit(&ill_v6->ill_lock);
14298 	}
14299 }
14300 /*
14301  * This function is called when an ill has had a change in its group status
14302  * to bring up all the ipifs that were up before the change.
14303  */
14304 int
14305 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14306 {
14307 	ipif_t *ipif;
14308 	ill_t *ill_v4;
14309 	ill_t *ill_v6;
14310 	ill_t *from_ill;
14311 	int err = 0;
14312 
14313 	ASSERT(IAM_WRITER_ILL(ill));
14314 
14315 	/*
14316 	 * Except for ipif_state_flags and ill_state_flags the other
14317 	 * fields of the ipif/ill that are modified below are protected
14318 	 * implicitly since we are a writer. We would have tried to down
14319 	 * even an ipif that was already down, in ill_down_ipifs. So we
14320 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14321 	 */
14322 	ill_v4 = ill->ill_phyint->phyint_illv4;
14323 	ill_v6 = ill->ill_phyint->phyint_illv6;
14324 	if (ill_v4 != NULL) {
14325 		ill_v4->ill_up_ipifs = B_TRUE;
14326 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14327 		    ipif = ipif->ipif_next) {
14328 			mutex_enter(&ill_v4->ill_lock);
14329 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14330 			IPIF_UNMARK_MOVING(ipif);
14331 			mutex_exit(&ill_v4->ill_lock);
14332 			if (ipif->ipif_was_up) {
14333 				if (!(ipif->ipif_flags & IPIF_UP))
14334 					err = ipif_up(ipif, q, mp);
14335 				ipif->ipif_was_up = B_FALSE;
14336 				if (err != 0) {
14337 					/*
14338 					 * Can there be any other error ?
14339 					 */
14340 					ASSERT(err == EINPROGRESS);
14341 					return (err);
14342 				}
14343 			}
14344 		}
14345 		mutex_enter(&ill_v4->ill_lock);
14346 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14347 		mutex_exit(&ill_v4->ill_lock);
14348 		ill_v4->ill_up_ipifs = B_FALSE;
14349 		if (ill_v4->ill_move_in_progress) {
14350 			ASSERT(ill_v4->ill_move_peer != NULL);
14351 			ill_v4->ill_move_in_progress = B_FALSE;
14352 			from_ill = ill_v4->ill_move_peer;
14353 			from_ill->ill_move_in_progress = B_FALSE;
14354 			from_ill->ill_move_peer = NULL;
14355 			mutex_enter(&from_ill->ill_lock);
14356 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14357 			mutex_exit(&from_ill->ill_lock);
14358 			if (ill_v6 == NULL) {
14359 				if (from_ill->ill_phyint->phyint_flags &
14360 				    PHYI_STANDBY) {
14361 					phyint_inactive(from_ill->ill_phyint);
14362 				}
14363 				if (ill_v4->ill_phyint->phyint_flags &
14364 				    PHYI_STANDBY) {
14365 					phyint_inactive(ill_v4->ill_phyint);
14366 				}
14367 			}
14368 			ill_v4->ill_move_peer = NULL;
14369 		}
14370 	}
14371 
14372 	if (ill_v6 != NULL) {
14373 		ill_v6->ill_up_ipifs = B_TRUE;
14374 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14375 		    ipif = ipif->ipif_next) {
14376 			mutex_enter(&ill_v6->ill_lock);
14377 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14378 			IPIF_UNMARK_MOVING(ipif);
14379 			mutex_exit(&ill_v6->ill_lock);
14380 			if (ipif->ipif_was_up) {
14381 				if (!(ipif->ipif_flags & IPIF_UP))
14382 					err = ipif_up(ipif, q, mp);
14383 				ipif->ipif_was_up = B_FALSE;
14384 				if (err != 0) {
14385 					/*
14386 					 * Can there be any other error ?
14387 					 */
14388 					ASSERT(err == EINPROGRESS);
14389 					return (err);
14390 				}
14391 			}
14392 		}
14393 		mutex_enter(&ill_v6->ill_lock);
14394 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14395 		mutex_exit(&ill_v6->ill_lock);
14396 		ill_v6->ill_up_ipifs = B_FALSE;
14397 		if (ill_v6->ill_move_in_progress) {
14398 			ASSERT(ill_v6->ill_move_peer != NULL);
14399 			ill_v6->ill_move_in_progress = B_FALSE;
14400 			from_ill = ill_v6->ill_move_peer;
14401 			from_ill->ill_move_in_progress = B_FALSE;
14402 			from_ill->ill_move_peer = NULL;
14403 			mutex_enter(&from_ill->ill_lock);
14404 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14405 			mutex_exit(&from_ill->ill_lock);
14406 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14407 				phyint_inactive(from_ill->ill_phyint);
14408 			}
14409 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14410 				phyint_inactive(ill_v6->ill_phyint);
14411 			}
14412 			ill_v6->ill_move_peer = NULL;
14413 		}
14414 	}
14415 	return (0);
14416 }
14417 
14418 /*
14419  * bring down all the approriate ipifs.
14420  */
14421 /* ARGSUSED */
14422 static void
14423 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14424 {
14425 	ipif_t *ipif;
14426 
14427 	ASSERT(IAM_WRITER_ILL(ill));
14428 
14429 	/*
14430 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14431 	 * are modified below are protected implicitly since we are a writer
14432 	 */
14433 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14434 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14435 			continue;
14436 		/*
14437 		 * Don't bring down the LINK LOCAL addresses as they are tied
14438 		 * to physical interface and they don't move. Treat them as
14439 		 * IPIF_NOFAILOVER.
14440 		 */
14441 		if (chk_nofailover && ill->ill_isv6 &&
14442 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))
14443 			continue;
14444 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14445 			/*
14446 			 * We go through the ipif_down logic even if the ipif
14447 			 * is already down, since routes can be added based
14448 			 * on down ipifs. Going through ipif_down once again
14449 			 * will delete any IREs created based on these routes.
14450 			 */
14451 			if (ipif->ipif_flags & IPIF_UP)
14452 				ipif->ipif_was_up = B_TRUE;
14453 			/*
14454 			 * If called with chk_nofailover true ipif is moving.
14455 			 */
14456 			mutex_enter(&ill->ill_lock);
14457 			if (chk_nofailover) {
14458 				ipif->ipif_state_flags |=
14459 				    IPIF_MOVING | IPIF_CHANGING;
14460 			} else {
14461 				ipif->ipif_state_flags |= IPIF_CHANGING;
14462 			}
14463 			mutex_exit(&ill->ill_lock);
14464 			/*
14465 			 * Need to re-create net/subnet bcast ires if
14466 			 * they are dependent on ipif.
14467 			 */
14468 			if (!ipif->ipif_isv6)
14469 				ipif_check_bcast_ires(ipif);
14470 			(void) ipif_logical_down(ipif, NULL, NULL);
14471 			ipif_non_duplicate(ipif);
14472 			ipif_down_tail(ipif);
14473 		}
14474 	}
14475 }
14476 
14477 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14478 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14479 	(ipsq)->ipsq_refs++;				\
14480 }
14481 
14482 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14483 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14484 	(ipsq)->ipsq_refs--;				\
14485 	if ((ipsq)->ipsq_refs == 0)				\
14486 		(ipsq)->ipsq_name[0] = '\0'; 		\
14487 }
14488 
14489 /*
14490  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14491  * new_ipsq.
14492  */
14493 static void
14494 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14495 {
14496 	phyint_t *phyint;
14497 	phyint_t *next_phyint;
14498 
14499 	/*
14500 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14501 	 * writer and the ill_lock of the ill in question. Also the dest
14502 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14503 	 */
14504 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14505 
14506 	phyint = cur_ipsq->ipsq_phyint_list;
14507 	cur_ipsq->ipsq_phyint_list = NULL;
14508 	while (phyint != NULL) {
14509 		next_phyint = phyint->phyint_ipsq_next;
14510 		IPSQ_DEC_REF(cur_ipsq, ipst);
14511 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14512 		new_ipsq->ipsq_phyint_list = phyint;
14513 		IPSQ_INC_REF(new_ipsq, ipst);
14514 		phyint->phyint_ipsq = new_ipsq;
14515 		phyint = next_phyint;
14516 	}
14517 }
14518 
14519 #define	SPLIT_SUCCESS		0
14520 #define	SPLIT_NOT_NEEDED	1
14521 #define	SPLIT_FAILED		2
14522 
14523 int
14524 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14525     ip_stack_t *ipst)
14526 {
14527 	ipsq_t *newipsq = NULL;
14528 
14529 	/*
14530 	 * Assertions denote pre-requisites for changing the ipsq of
14531 	 * a phyint
14532 	 */
14533 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14534 	/*
14535 	 * <ill-phyint> assocs can't change while ill_g_lock
14536 	 * is held as writer. See ill_phyint_reinit()
14537 	 */
14538 	ASSERT(phyint->phyint_illv4 == NULL ||
14539 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14540 	ASSERT(phyint->phyint_illv6 == NULL ||
14541 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14542 
14543 	if ((phyint->phyint_groupname_len !=
14544 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14545 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14546 	    phyint->phyint_groupname_len) != 0)) {
14547 		/*
14548 		 * Once we fail in creating a new ipsq due to memory shortage,
14549 		 * don't attempt to create new ipsq again, based on another
14550 		 * phyint, since we want all phyints belonging to an IPMP group
14551 		 * to be in the same ipsq even in the event of mem alloc fails.
14552 		 */
14553 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14554 		    cur_ipsq, ipst);
14555 		if (newipsq == NULL) {
14556 			/* Memory allocation failure */
14557 			return (SPLIT_FAILED);
14558 		} else {
14559 			/* ipsq_refs protected by ill_g_lock (writer) */
14560 			IPSQ_DEC_REF(cur_ipsq, ipst);
14561 			phyint->phyint_ipsq = newipsq;
14562 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14563 			newipsq->ipsq_phyint_list = phyint;
14564 			IPSQ_INC_REF(newipsq, ipst);
14565 			return (SPLIT_SUCCESS);
14566 		}
14567 	}
14568 	return (SPLIT_NOT_NEEDED);
14569 }
14570 
14571 /*
14572  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14573  * to do this split
14574  */
14575 static int
14576 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14577 {
14578 	ipsq_t *newipsq;
14579 
14580 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14581 	/*
14582 	 * <ill-phyint> assocs can't change while ill_g_lock
14583 	 * is held as writer. See ill_phyint_reinit()
14584 	 */
14585 
14586 	ASSERT(phyint->phyint_illv4 == NULL ||
14587 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14588 	ASSERT(phyint->phyint_illv6 == NULL ||
14589 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14590 
14591 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14592 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14593 		/*
14594 		 * ipsq_init failed due to no memory
14595 		 * caller will use the same ipsq
14596 		 */
14597 		return (SPLIT_FAILED);
14598 	}
14599 
14600 	/* ipsq_ref is protected by ill_g_lock (writer) */
14601 	IPSQ_DEC_REF(cur_ipsq, ipst);
14602 
14603 	/*
14604 	 * This is a new ipsq that is unknown to the world.
14605 	 * So we don't need to hold ipsq_lock,
14606 	 */
14607 	newipsq = phyint->phyint_ipsq;
14608 	newipsq->ipsq_writer = NULL;
14609 	newipsq->ipsq_reentry_cnt--;
14610 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14611 #ifdef DEBUG
14612 	newipsq->ipsq_depth = 0;
14613 #endif
14614 
14615 	return (SPLIT_SUCCESS);
14616 }
14617 
14618 /*
14619  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14620  * ipsq's representing their individual groups or themselves. Return
14621  * whether split needs to be retried again later.
14622  */
14623 static boolean_t
14624 ill_split_ipsq(ipsq_t *cur_ipsq)
14625 {
14626 	phyint_t *phyint;
14627 	phyint_t *next_phyint;
14628 	int	error;
14629 	boolean_t need_retry = B_FALSE;
14630 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14631 
14632 	phyint = cur_ipsq->ipsq_phyint_list;
14633 	cur_ipsq->ipsq_phyint_list = NULL;
14634 	while (phyint != NULL) {
14635 		next_phyint = phyint->phyint_ipsq_next;
14636 		/*
14637 		 * 'created' will tell us whether the callee actually
14638 		 * created an ipsq. Lack of memory may force the callee
14639 		 * to return without creating an ipsq.
14640 		 */
14641 		if (phyint->phyint_groupname == NULL) {
14642 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14643 		} else {
14644 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14645 			    need_retry, ipst);
14646 		}
14647 
14648 		switch (error) {
14649 		case SPLIT_FAILED:
14650 			need_retry = B_TRUE;
14651 			/* FALLTHRU */
14652 		case SPLIT_NOT_NEEDED:
14653 			/*
14654 			 * Keep it on the list.
14655 			 */
14656 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14657 			cur_ipsq->ipsq_phyint_list = phyint;
14658 			break;
14659 		case SPLIT_SUCCESS:
14660 			break;
14661 		default:
14662 			ASSERT(0);
14663 		}
14664 
14665 		phyint = next_phyint;
14666 	}
14667 	return (need_retry);
14668 }
14669 
14670 /*
14671  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14672  * and return the ills in the list. This list will be
14673  * needed to unlock all the ills later on by the caller.
14674  * The <ill-ipsq> associations could change between the
14675  * lock and unlock. Hence the unlock can't traverse the
14676  * ipsq to get the list of ills.
14677  */
14678 static int
14679 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14680 {
14681 	int	cnt = 0;
14682 	phyint_t	*phyint;
14683 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14684 
14685 	/*
14686 	 * The caller holds ill_g_lock to ensure that the ill memberships
14687 	 * of the ipsq don't change
14688 	 */
14689 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14690 
14691 	phyint = ipsq->ipsq_phyint_list;
14692 	while (phyint != NULL) {
14693 		if (phyint->phyint_illv4 != NULL) {
14694 			ASSERT(cnt < list_max);
14695 			list[cnt++] = phyint->phyint_illv4;
14696 		}
14697 		if (phyint->phyint_illv6 != NULL) {
14698 			ASSERT(cnt < list_max);
14699 			list[cnt++] = phyint->phyint_illv6;
14700 		}
14701 		phyint = phyint->phyint_ipsq_next;
14702 	}
14703 	ill_lock_ills(list, cnt);
14704 	return (cnt);
14705 }
14706 
14707 void
14708 ill_lock_ills(ill_t **list, int cnt)
14709 {
14710 	int	i;
14711 
14712 	if (cnt > 1) {
14713 		boolean_t try_again;
14714 		do {
14715 			try_again = B_FALSE;
14716 			for (i = 0; i < cnt - 1; i++) {
14717 				if (list[i] < list[i + 1]) {
14718 					ill_t	*tmp;
14719 
14720 					/* swap the elements */
14721 					tmp = list[i];
14722 					list[i] = list[i + 1];
14723 					list[i + 1] = tmp;
14724 					try_again = B_TRUE;
14725 				}
14726 			}
14727 		} while (try_again);
14728 	}
14729 
14730 	for (i = 0; i < cnt; i++) {
14731 		if (i == 0) {
14732 			if (list[i] != NULL)
14733 				mutex_enter(&list[i]->ill_lock);
14734 			else
14735 				return;
14736 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14737 			mutex_enter(&list[i]->ill_lock);
14738 		}
14739 	}
14740 }
14741 
14742 void
14743 ill_unlock_ills(ill_t **list, int cnt)
14744 {
14745 	int	i;
14746 
14747 	for (i = 0; i < cnt; i++) {
14748 		if ((i == 0) && (list[i] != NULL)) {
14749 			mutex_exit(&list[i]->ill_lock);
14750 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14751 			mutex_exit(&list[i]->ill_lock);
14752 		}
14753 	}
14754 }
14755 
14756 /*
14757  * Merge all the ills from 1 ipsq group into another ipsq group.
14758  * The source ipsq group is specified by the ipsq associated with
14759  * 'from_ill'. The destination ipsq group is specified by the ipsq
14760  * associated with 'to_ill' or 'groupname' respectively.
14761  * Note that ipsq itself does not have a reference count mechanism
14762  * and functions don't look up an ipsq and pass it around. Instead
14763  * functions pass around an ill or groupname, and the ipsq is looked
14764  * up from the ill or groupname and the required operation performed
14765  * atomically with the lookup on the ipsq.
14766  */
14767 static int
14768 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14769     queue_t *q)
14770 {
14771 	ipsq_t *old_ipsq;
14772 	ipsq_t *new_ipsq;
14773 	ill_t	**ill_list;
14774 	int	cnt;
14775 	size_t	ill_list_size;
14776 	boolean_t became_writer_on_new_sq = B_FALSE;
14777 	ip_stack_t	*ipst = from_ill->ill_ipst;
14778 
14779 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14780 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14781 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14782 
14783 	/*
14784 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14785 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14786 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14787 	 */
14788 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14789 
14790 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14791 	if (groupname != NULL)
14792 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14793 	else {
14794 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14795 	}
14796 
14797 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14798 
14799 	/*
14800 	 * both groups are on the same ipsq.
14801 	 */
14802 	if (old_ipsq == new_ipsq) {
14803 		rw_exit(&ipst->ips_ill_g_lock);
14804 		return (0);
14805 	}
14806 
14807 	cnt = old_ipsq->ipsq_refs << 1;
14808 	ill_list_size = cnt * sizeof (ill_t *);
14809 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14810 	if (ill_list == NULL) {
14811 		rw_exit(&ipst->ips_ill_g_lock);
14812 		return (ENOMEM);
14813 	}
14814 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14815 
14816 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14817 	mutex_enter(&new_ipsq->ipsq_lock);
14818 	if ((new_ipsq->ipsq_writer == NULL &&
14819 	    new_ipsq->ipsq_current_ipif == NULL) ||
14820 	    (new_ipsq->ipsq_writer == curthread)) {
14821 		new_ipsq->ipsq_writer = curthread;
14822 		new_ipsq->ipsq_reentry_cnt++;
14823 		became_writer_on_new_sq = B_TRUE;
14824 	}
14825 
14826 	/*
14827 	 * We are holding ill_g_lock as writer and all the ill locks of
14828 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14829 	 * message can land up on the old ipsq even though we don't hold the
14830 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14831 	 */
14832 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14833 
14834 	/*
14835 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14836 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14837 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14838 	 */
14839 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14840 
14841 	/*
14842 	 * Mark the new ipsq as needing a split since it is currently
14843 	 * being shared by more than 1 IPMP group. The split will
14844 	 * occur at the end of ipsq_exit
14845 	 */
14846 	new_ipsq->ipsq_split = B_TRUE;
14847 
14848 	/* Now release all the locks */
14849 	mutex_exit(&new_ipsq->ipsq_lock);
14850 	ill_unlock_ills(ill_list, cnt);
14851 	rw_exit(&ipst->ips_ill_g_lock);
14852 
14853 	kmem_free(ill_list, ill_list_size);
14854 
14855 	/*
14856 	 * If we succeeded in becoming writer on the new ipsq, then
14857 	 * drain the new ipsq and start processing  all enqueued messages
14858 	 * including the current ioctl we are processing which is either
14859 	 * a set groupname or failover/failback.
14860 	 */
14861 	if (became_writer_on_new_sq)
14862 		ipsq_exit(new_ipsq);
14863 
14864 	/*
14865 	 * syncq has been changed and all the messages have been moved.
14866 	 */
14867 	mutex_enter(&old_ipsq->ipsq_lock);
14868 	old_ipsq->ipsq_current_ipif = NULL;
14869 	old_ipsq->ipsq_current_ioctl = 0;
14870 	old_ipsq->ipsq_current_done = B_TRUE;
14871 	mutex_exit(&old_ipsq->ipsq_lock);
14872 	return (EINPROGRESS);
14873 }
14874 
14875 /*
14876  * Delete and add the loopback copy and non-loopback copy of
14877  * the BROADCAST ire corresponding to ill and addr. Used to
14878  * group broadcast ires together when ill becomes part of
14879  * a group.
14880  *
14881  * This function is also called when ill is leaving the group
14882  * so that the ires belonging to the group gets re-grouped.
14883  */
14884 static void
14885 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14886 {
14887 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14888 	ire_t **ire_ptpn = &ire_head;
14889 	ip_stack_t	*ipst = ill->ill_ipst;
14890 
14891 	/*
14892 	 * The loopback and non-loopback IREs are inserted in the order in which
14893 	 * they're found, on the basis that they are correctly ordered (loopback
14894 	 * first).
14895 	 */
14896 	for (;;) {
14897 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14898 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14899 		if (ire == NULL)
14900 			break;
14901 
14902 		/*
14903 		 * we are passing in KM_SLEEP because it is not easy to
14904 		 * go back to a sane state in case of memory failure.
14905 		 */
14906 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14907 		ASSERT(nire != NULL);
14908 		bzero(nire, sizeof (ire_t));
14909 		/*
14910 		 * Don't use ire_max_frag directly since we don't
14911 		 * hold on to 'ire' until we add the new ire 'nire' and
14912 		 * we don't want the new ire to have a dangling reference
14913 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14914 		 * be in sync with the ipif_mtu of the associate ipif.
14915 		 * For eg. this happens as a result of SIOCSLIFNAME,
14916 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14917 		 * the driver. A change in ire_max_frag triggered as
14918 		 * as a result of path mtu discovery, or due to an
14919 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14920 		 * route change -mtu command does not apply to broadcast ires.
14921 		 *
14922 		 * XXX We need a recovery strategy here if ire_init fails
14923 		 */
14924 		if (ire_init(nire,
14925 		    (uchar_t *)&ire->ire_addr,
14926 		    (uchar_t *)&ire->ire_mask,
14927 		    (uchar_t *)&ire->ire_src_addr,
14928 		    (uchar_t *)&ire->ire_gateway_addr,
14929 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14930 		    &ire->ire_ipif->ipif_mtu,
14931 		    ire->ire_nce,
14932 		    ire->ire_rfq,
14933 		    ire->ire_stq,
14934 		    ire->ire_type,
14935 		    ire->ire_ipif,
14936 		    ire->ire_cmask,
14937 		    ire->ire_phandle,
14938 		    ire->ire_ihandle,
14939 		    ire->ire_flags,
14940 		    &ire->ire_uinfo,
14941 		    NULL,
14942 		    NULL,
14943 		    ipst) == NULL) {
14944 			cmn_err(CE_PANIC, "ire_init() failed");
14945 		}
14946 		ire_delete(ire);
14947 		ire_refrele(ire);
14948 
14949 		/*
14950 		 * The newly created IREs are inserted at the tail of the list
14951 		 * starting with ire_head. As we've just allocated them no one
14952 		 * knows about them so it's safe.
14953 		 */
14954 		*ire_ptpn = nire;
14955 		ire_ptpn = &nire->ire_next;
14956 	}
14957 
14958 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14959 		int error;
14960 		ire_t *oire;
14961 		/* unlink the IRE from our list before calling ire_add() */
14962 		nire_next = nire->ire_next;
14963 		nire->ire_next = NULL;
14964 
14965 		/* ire_add adds the ire at the right place in the list */
14966 		oire = nire;
14967 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14968 		ASSERT(error == 0);
14969 		ASSERT(oire == nire);
14970 		ire_refrele(nire);	/* Held in ire_add */
14971 	}
14972 }
14973 
14974 /*
14975  * This function is usually called when an ill is inserted in
14976  * a group and all the ipifs are already UP. As all the ipifs
14977  * are already UP, the broadcast ires have already been created
14978  * and been inserted. But, ire_add_v4 would not have grouped properly.
14979  * We need to re-group for the benefit of ip_wput_ire which
14980  * expects BROADCAST ires to be grouped properly to avoid sending
14981  * more than one copy of the broadcast packet per group.
14982  *
14983  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14984  *	  because when ipif_up_done ends up calling this, ires have
14985  *        already been added before illgrp_insert i.e before ill_group
14986  *	  has been initialized.
14987  */
14988 static void
14989 ill_group_bcast_for_xmit(ill_t *ill)
14990 {
14991 	ill_group_t *illgrp;
14992 	ipif_t *ipif;
14993 	ipaddr_t addr;
14994 	ipaddr_t net_mask;
14995 	ipaddr_t subnet_netmask;
14996 
14997 	illgrp = ill->ill_group;
14998 
14999 	/*
15000 	 * This function is called even when an ill is deleted from
15001 	 * the group. Hence, illgrp could be null.
15002 	 */
15003 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15004 		return;
15005 
15006 	/*
15007 	 * Delete all the BROADCAST ires matching this ill and add
15008 	 * them back. This time, ire_add_v4 should take care of
15009 	 * grouping them with others because ill is part of the
15010 	 * group.
15011 	 */
15012 	ill_bcast_delete_and_add(ill, 0);
15013 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15014 
15015 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15016 
15017 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15018 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15019 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15020 		} else {
15021 			net_mask = htonl(IN_CLASSA_NET);
15022 		}
15023 		addr = net_mask & ipif->ipif_subnet;
15024 		ill_bcast_delete_and_add(ill, addr);
15025 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15026 
15027 		subnet_netmask = ipif->ipif_net_mask;
15028 		addr = ipif->ipif_subnet;
15029 		ill_bcast_delete_and_add(ill, addr);
15030 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15031 	}
15032 }
15033 
15034 /*
15035  * This function is called from illgrp_delete when ill is being deleted
15036  * from the group.
15037  *
15038  * As ill is not there in the group anymore, any address belonging
15039  * to this ill should be cleared of IRE_MARK_NORECV.
15040  */
15041 static void
15042 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15043 {
15044 	ire_t *ire;
15045 	irb_t *irb;
15046 	ip_stack_t	*ipst = ill->ill_ipst;
15047 
15048 	ASSERT(ill->ill_group == NULL);
15049 
15050 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15051 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15052 
15053 	if (ire != NULL) {
15054 		/*
15055 		 * IPMP and plumbing operations are serialized on the ipsq, so
15056 		 * no one will insert or delete a broadcast ire under our feet.
15057 		 */
15058 		irb = ire->ire_bucket;
15059 		rw_enter(&irb->irb_lock, RW_READER);
15060 		ire_refrele(ire);
15061 
15062 		for (; ire != NULL; ire = ire->ire_next) {
15063 			if (ire->ire_addr != addr)
15064 				break;
15065 			if (ire_to_ill(ire) != ill)
15066 				continue;
15067 
15068 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15069 			ire->ire_marks &= ~IRE_MARK_NORECV;
15070 		}
15071 		rw_exit(&irb->irb_lock);
15072 	}
15073 }
15074 
15075 /*
15076  * This function must be called only after the broadcast ires
15077  * have been grouped together. For a given address addr, nominate
15078  * only one of the ires whose interface is not FAILED or OFFLINE.
15079  *
15080  * This is also called when an ipif goes down, so that we can nominate
15081  * a different ire with the same address for receiving.
15082  */
15083 static void
15084 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15085 {
15086 	irb_t *irb;
15087 	ire_t *ire;
15088 	ire_t *ire1;
15089 	ire_t *save_ire;
15090 	ire_t **irep = NULL;
15091 	boolean_t first = B_TRUE;
15092 	ire_t *clear_ire = NULL;
15093 	ire_t *start_ire = NULL;
15094 	ire_t	*new_lb_ire;
15095 	ire_t	*new_nlb_ire;
15096 	boolean_t new_lb_ire_used = B_FALSE;
15097 	boolean_t new_nlb_ire_used = B_FALSE;
15098 	uint64_t match_flags;
15099 	uint64_t phyi_flags;
15100 	boolean_t fallback = B_FALSE;
15101 	uint_t	max_frag;
15102 
15103 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15104 	    NULL, MATCH_IRE_TYPE, ipst);
15105 	/*
15106 	 * We may not be able to find some ires if a previous
15107 	 * ire_create failed. This happens when an ipif goes
15108 	 * down and we are unable to create BROADCAST ires due
15109 	 * to memory failure. Thus, we have to check for NULL
15110 	 * below. This should handle the case for LOOPBACK,
15111 	 * POINTOPOINT and interfaces with some POINTOPOINT
15112 	 * logicals for which there are no BROADCAST ires.
15113 	 */
15114 	if (ire == NULL)
15115 		return;
15116 	/*
15117 	 * Currently IRE_BROADCASTS are deleted when an ipif
15118 	 * goes down which runs exclusively. Thus, setting
15119 	 * IRE_MARK_RCVD should not race with ire_delete marking
15120 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15121 	 * be consistent with other parts of the code that walks
15122 	 * a given bucket.
15123 	 */
15124 	save_ire = ire;
15125 	irb = ire->ire_bucket;
15126 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15127 	if (new_lb_ire == NULL) {
15128 		ire_refrele(ire);
15129 		return;
15130 	}
15131 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15132 	if (new_nlb_ire == NULL) {
15133 		ire_refrele(ire);
15134 		kmem_cache_free(ire_cache, new_lb_ire);
15135 		return;
15136 	}
15137 	IRB_REFHOLD(irb);
15138 	rw_enter(&irb->irb_lock, RW_WRITER);
15139 	/*
15140 	 * Get to the first ire matching the address and the
15141 	 * group. If the address does not match we are done
15142 	 * as we could not find the IRE. If the address matches
15143 	 * we should get to the first one matching the group.
15144 	 */
15145 	while (ire != NULL) {
15146 		if (ire->ire_addr != addr ||
15147 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15148 			break;
15149 		}
15150 		ire = ire->ire_next;
15151 	}
15152 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15153 	start_ire = ire;
15154 redo:
15155 	while (ire != NULL && ire->ire_addr == addr &&
15156 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15157 		/*
15158 		 * The first ire for any address within a group
15159 		 * should always be the one with IRE_MARK_NORECV cleared
15160 		 * so that ip_wput_ire can avoid searching for one.
15161 		 * Note down the insertion point which will be used
15162 		 * later.
15163 		 */
15164 		if (first && (irep == NULL))
15165 			irep = ire->ire_ptpn;
15166 		/*
15167 		 * PHYI_FAILED is set when the interface fails.
15168 		 * This interface might have become good, but the
15169 		 * daemon has not yet detected. We should still
15170 		 * not receive on this. PHYI_OFFLINE should never
15171 		 * be picked as this has been offlined and soon
15172 		 * be removed.
15173 		 */
15174 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15175 		if (phyi_flags & PHYI_OFFLINE) {
15176 			ire->ire_marks |= IRE_MARK_NORECV;
15177 			ire = ire->ire_next;
15178 			continue;
15179 		}
15180 		if (phyi_flags & match_flags) {
15181 			ire->ire_marks |= IRE_MARK_NORECV;
15182 			ire = ire->ire_next;
15183 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15184 			    PHYI_INACTIVE) {
15185 				fallback = B_TRUE;
15186 			}
15187 			continue;
15188 		}
15189 		if (first) {
15190 			/*
15191 			 * We will move this to the front of the list later
15192 			 * on.
15193 			 */
15194 			clear_ire = ire;
15195 			ire->ire_marks &= ~IRE_MARK_NORECV;
15196 		} else {
15197 			ire->ire_marks |= IRE_MARK_NORECV;
15198 		}
15199 		first = B_FALSE;
15200 		ire = ire->ire_next;
15201 	}
15202 	/*
15203 	 * If we never nominated anybody, try nominating at least
15204 	 * an INACTIVE, if we found one. Do it only once though.
15205 	 */
15206 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15207 	    fallback) {
15208 		match_flags = PHYI_FAILED;
15209 		ire = start_ire;
15210 		irep = NULL;
15211 		goto redo;
15212 	}
15213 	ire_refrele(save_ire);
15214 
15215 	/*
15216 	 * irep non-NULL indicates that we entered the while loop
15217 	 * above. If clear_ire is at the insertion point, we don't
15218 	 * have to do anything. clear_ire will be NULL if all the
15219 	 * interfaces are failed.
15220 	 *
15221 	 * We cannot unlink and reinsert the ire at the right place
15222 	 * in the list since there can be other walkers of this bucket.
15223 	 * Instead we delete and recreate the ire
15224 	 */
15225 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15226 		ire_t *clear_ire_stq = NULL;
15227 
15228 		bzero(new_lb_ire, sizeof (ire_t));
15229 		/* XXX We need a recovery strategy here. */
15230 		if (ire_init(new_lb_ire,
15231 		    (uchar_t *)&clear_ire->ire_addr,
15232 		    (uchar_t *)&clear_ire->ire_mask,
15233 		    (uchar_t *)&clear_ire->ire_src_addr,
15234 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15235 		    &clear_ire->ire_max_frag,
15236 		    NULL, /* let ire_nce_init derive the resolver info */
15237 		    clear_ire->ire_rfq,
15238 		    clear_ire->ire_stq,
15239 		    clear_ire->ire_type,
15240 		    clear_ire->ire_ipif,
15241 		    clear_ire->ire_cmask,
15242 		    clear_ire->ire_phandle,
15243 		    clear_ire->ire_ihandle,
15244 		    clear_ire->ire_flags,
15245 		    &clear_ire->ire_uinfo,
15246 		    NULL,
15247 		    NULL,
15248 		    ipst) == NULL)
15249 			cmn_err(CE_PANIC, "ire_init() failed");
15250 		if (clear_ire->ire_stq == NULL) {
15251 			ire_t *ire_next = clear_ire->ire_next;
15252 			if (ire_next != NULL &&
15253 			    ire_next->ire_stq != NULL &&
15254 			    ire_next->ire_addr == clear_ire->ire_addr &&
15255 			    ire_next->ire_ipif->ipif_ill ==
15256 			    clear_ire->ire_ipif->ipif_ill) {
15257 				clear_ire_stq = ire_next;
15258 
15259 				bzero(new_nlb_ire, sizeof (ire_t));
15260 				/* XXX We need a recovery strategy here. */
15261 				if (ire_init(new_nlb_ire,
15262 				    (uchar_t *)&clear_ire_stq->ire_addr,
15263 				    (uchar_t *)&clear_ire_stq->ire_mask,
15264 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15265 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15266 				    &clear_ire_stq->ire_max_frag,
15267 				    NULL,
15268 				    clear_ire_stq->ire_rfq,
15269 				    clear_ire_stq->ire_stq,
15270 				    clear_ire_stq->ire_type,
15271 				    clear_ire_stq->ire_ipif,
15272 				    clear_ire_stq->ire_cmask,
15273 				    clear_ire_stq->ire_phandle,
15274 				    clear_ire_stq->ire_ihandle,
15275 				    clear_ire_stq->ire_flags,
15276 				    &clear_ire_stq->ire_uinfo,
15277 				    NULL,
15278 				    NULL,
15279 				    ipst) == NULL)
15280 					cmn_err(CE_PANIC, "ire_init() failed");
15281 			}
15282 		}
15283 
15284 		/*
15285 		 * Delete the ire. We can't call ire_delete() since
15286 		 * we are holding the bucket lock. We can't release the
15287 		 * bucket lock since we can't allow irep to change. So just
15288 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15289 		 * ire from the list and do the refrele.
15290 		 */
15291 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15292 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15293 
15294 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15295 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15296 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15297 		}
15298 
15299 		/*
15300 		 * Also take care of otherfields like ib/ob pkt count
15301 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15302 		 */
15303 
15304 		/* Set the max_frag before adding the ire */
15305 		max_frag = *new_lb_ire->ire_max_fragp;
15306 		new_lb_ire->ire_max_fragp = NULL;
15307 		new_lb_ire->ire_max_frag = max_frag;
15308 
15309 		/* Add the new ire's. Insert at *irep */
15310 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15311 		ire1 = *irep;
15312 		if (ire1 != NULL)
15313 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15314 		new_lb_ire->ire_next = ire1;
15315 		/* Link the new one in. */
15316 		new_lb_ire->ire_ptpn = irep;
15317 		membar_producer();
15318 		*irep = new_lb_ire;
15319 		new_lb_ire_used = B_TRUE;
15320 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15321 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15322 		DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), new_lb_ire->ire_ipif,
15323 		    (char *), "ire", (void *), new_lb_ire);
15324 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15325 
15326 		if (clear_ire_stq != NULL) {
15327 			/* Set the max_frag before adding the ire */
15328 			max_frag = *new_nlb_ire->ire_max_fragp;
15329 			new_nlb_ire->ire_max_fragp = NULL;
15330 			new_nlb_ire->ire_max_frag = max_frag;
15331 
15332 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15333 			irep = &new_lb_ire->ire_next;
15334 			/* Add the new ire. Insert at *irep */
15335 			ire1 = *irep;
15336 			if (ire1 != NULL)
15337 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15338 			new_nlb_ire->ire_next = ire1;
15339 			/* Link the new one in. */
15340 			new_nlb_ire->ire_ptpn = irep;
15341 			membar_producer();
15342 			*irep = new_nlb_ire;
15343 			new_nlb_ire_used = B_TRUE;
15344 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15345 			    ire_stats_inserted);
15346 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15347 			DTRACE_PROBE3(ipif__incr__cnt,
15348 			    (ipif_t *), new_nlb_ire->ire_ipif,
15349 			    (char *), "ire", (void *), new_nlb_ire);
15350 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15351 			DTRACE_PROBE3(ill__incr__cnt,
15352 			    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15353 			    (char *), "ire", (void *), new_nlb_ire);
15354 			((ill_t *)(new_nlb_ire->ire_stq->q_ptr))->ill_ire_cnt++;
15355 		}
15356 	}
15357 	rw_exit(&irb->irb_lock);
15358 	if (!new_lb_ire_used)
15359 		kmem_cache_free(ire_cache, new_lb_ire);
15360 	if (!new_nlb_ire_used)
15361 		kmem_cache_free(ire_cache, new_nlb_ire);
15362 	IRB_REFRELE(irb);
15363 }
15364 
15365 /*
15366  * Whenever an ipif goes down we have to renominate a different
15367  * broadcast ire to receive. Whenever an ipif comes up, we need
15368  * to make sure that we have only one nominated to receive.
15369  */
15370 static void
15371 ipif_renominate_bcast(ipif_t *ipif)
15372 {
15373 	ill_t *ill = ipif->ipif_ill;
15374 	ipaddr_t subnet_addr;
15375 	ipaddr_t net_addr;
15376 	ipaddr_t net_mask = 0;
15377 	ipaddr_t subnet_netmask;
15378 	ipaddr_t addr;
15379 	ill_group_t *illgrp;
15380 	ip_stack_t	*ipst = ill->ill_ipst;
15381 
15382 	illgrp = ill->ill_group;
15383 	/*
15384 	 * If this is the last ipif going down, it might take
15385 	 * the ill out of the group. In that case ipif_down ->
15386 	 * illgrp_delete takes care of doing the nomination.
15387 	 * ipif_down does not call for this case.
15388 	 */
15389 	ASSERT(illgrp != NULL);
15390 
15391 	/* There could not have been any ires associated with this */
15392 	if (ipif->ipif_subnet == 0)
15393 		return;
15394 
15395 	ill_mark_bcast(illgrp, 0, ipst);
15396 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15397 
15398 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15399 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15400 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15401 	} else {
15402 		net_mask = htonl(IN_CLASSA_NET);
15403 	}
15404 	addr = net_mask & ipif->ipif_subnet;
15405 	ill_mark_bcast(illgrp, addr, ipst);
15406 
15407 	net_addr = ~net_mask | addr;
15408 	ill_mark_bcast(illgrp, net_addr, ipst);
15409 
15410 	subnet_netmask = ipif->ipif_net_mask;
15411 	addr = ipif->ipif_subnet;
15412 	ill_mark_bcast(illgrp, addr, ipst);
15413 
15414 	subnet_addr = ~subnet_netmask | addr;
15415 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15416 }
15417 
15418 /*
15419  * Whenever we form or delete ill groups, we need to nominate one set of
15420  * BROADCAST ires for receiving in the group.
15421  *
15422  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15423  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15424  *    for ill_ipif_up_count to be non-zero. This is the only case where
15425  *    ill_ipif_up_count is zero and we would still find the ires.
15426  *
15427  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15428  *    ipif is UP and we just have to do the nomination.
15429  *
15430  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15431  *    from the group. So, we have to do the nomination.
15432  *
15433  * Because of (3), there could be just one ill in the group. But we have
15434  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15435  * Thus, this function does not optimize when there is only one ill as
15436  * it is not correct for (3).
15437  */
15438 static void
15439 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15440 {
15441 	ill_t *ill;
15442 	ipif_t *ipif;
15443 	ipaddr_t subnet_addr;
15444 	ipaddr_t prev_subnet_addr = 0;
15445 	ipaddr_t net_addr;
15446 	ipaddr_t prev_net_addr = 0;
15447 	ipaddr_t net_mask = 0;
15448 	ipaddr_t subnet_netmask;
15449 	ipaddr_t addr;
15450 	ip_stack_t	*ipst;
15451 
15452 	/*
15453 	 * When the last memeber is leaving, there is nothing to
15454 	 * nominate.
15455 	 */
15456 	if (illgrp->illgrp_ill_count == 0) {
15457 		ASSERT(illgrp->illgrp_ill == NULL);
15458 		return;
15459 	}
15460 
15461 	ill = illgrp->illgrp_ill;
15462 	ASSERT(!ill->ill_isv6);
15463 	ipst = ill->ill_ipst;
15464 	/*
15465 	 * We assume that ires with same address and belonging to the
15466 	 * same group, has been grouped together. Nominating a *single*
15467 	 * ill in the group for sending and receiving broadcast is done
15468 	 * by making sure that the first BROADCAST ire (which will be
15469 	 * the one returned by ire_ctable_lookup for ip_rput and the
15470 	 * one that will be used in ip_wput_ire) will be the one that
15471 	 * will not have IRE_MARK_NORECV set.
15472 	 *
15473 	 * 1) ip_rput checks and discards packets received on ires marked
15474 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15475 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15476 	 *    first ire in the group for every broadcast address in the group.
15477 	 *    ip_rput will accept packets only on the first ire i.e only
15478 	 *    one copy of the ill.
15479 	 *
15480 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15481 	 *    packet for the whole group. It needs to send out on the ill
15482 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15483 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15484 	 *    the copy echoed back on other port where the ire is not marked
15485 	 *    with IRE_MARK_NORECV.
15486 	 *
15487 	 * Note that we just need to have the first IRE either loopback or
15488 	 * non-loopback (either of them may not exist if ire_create failed
15489 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15490 	 * always hit the first one and hence will always accept one copy.
15491 	 *
15492 	 * We have a broadcast ire per ill for all the unique prefixes
15493 	 * hosted on that ill. As we don't have a way of knowing the
15494 	 * unique prefixes on a given ill and hence in the whole group,
15495 	 * we just call ill_mark_bcast on all the prefixes that exist
15496 	 * in the group. For the common case of one prefix, the code
15497 	 * below optimizes by remebering the last address used for
15498 	 * markng. In the case of multiple prefixes, this will still
15499 	 * optimize depending the order of prefixes.
15500 	 *
15501 	 * The only unique address across the whole group is 0.0.0.0 and
15502 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15503 	 * the first ire in the bucket for receiving and disables the
15504 	 * others.
15505 	 */
15506 	ill_mark_bcast(illgrp, 0, ipst);
15507 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15508 	for (; ill != NULL; ill = ill->ill_group_next) {
15509 
15510 		for (ipif = ill->ill_ipif; ipif != NULL;
15511 		    ipif = ipif->ipif_next) {
15512 
15513 			if (!(ipif->ipif_flags & IPIF_UP) ||
15514 			    ipif->ipif_subnet == 0) {
15515 				continue;
15516 			}
15517 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15518 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15519 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15520 			} else {
15521 				net_mask = htonl(IN_CLASSA_NET);
15522 			}
15523 			addr = net_mask & ipif->ipif_subnet;
15524 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15525 				ill_mark_bcast(illgrp, addr, ipst);
15526 				net_addr = ~net_mask | addr;
15527 				ill_mark_bcast(illgrp, net_addr, ipst);
15528 			}
15529 			prev_net_addr = addr;
15530 
15531 			subnet_netmask = ipif->ipif_net_mask;
15532 			addr = ipif->ipif_subnet;
15533 			if (prev_subnet_addr == 0 ||
15534 			    prev_subnet_addr != addr) {
15535 				ill_mark_bcast(illgrp, addr, ipst);
15536 				subnet_addr = ~subnet_netmask | addr;
15537 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15538 			}
15539 			prev_subnet_addr = addr;
15540 		}
15541 	}
15542 }
15543 
15544 /*
15545  * This function is called while forming ill groups.
15546  *
15547  * Currently, we handle only allmulti groups. We want to join
15548  * allmulti on only one of the ills in the groups. In future,
15549  * when we have link aggregation, we may have to join normal
15550  * multicast groups on multiple ills as switch does inbound load
15551  * balancing. Following are the functions that calls this
15552  * function :
15553  *
15554  * 1) ill_recover_multicast : Interface is coming back UP.
15555  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15556  *    will call ill_recover_multicast to recover all the multicast
15557  *    groups. We need to make sure that only one member is joined
15558  *    in the ill group.
15559  *
15560  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15561  *    Somebody is joining allmulti. We need to make sure that only one
15562  *    member is joined in the group.
15563  *
15564  * 3) illgrp_insert : If allmulti has already joined, we need to make
15565  *    sure that only one member is joined in the group.
15566  *
15567  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15568  *    allmulti who we have nominated. We need to pick someother ill.
15569  *
15570  * 5) illgrp_delete : The ill we nominated is leaving the group,
15571  *    we need to pick a new ill to join the group.
15572  *
15573  * For (1), (2), (5) - we just have to check whether there is
15574  * a good ill joined in the group. If we could not find any ills
15575  * joined the group, we should join.
15576  *
15577  * For (4), the one that was nominated to receive, left the group.
15578  * There could be nobody joined in the group when this function is
15579  * called.
15580  *
15581  * For (3) - we need to explicitly check whether there are multiple
15582  * ills joined in the group.
15583  *
15584  * For simplicity, we don't differentiate any of the above cases. We
15585  * just leave the group if it is joined on any of them and join on
15586  * the first good ill.
15587  */
15588 int
15589 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15590 {
15591 	ilm_t *ilm;
15592 	ill_t *ill;
15593 	ill_t *fallback_inactive_ill = NULL;
15594 	ill_t *fallback_failed_ill = NULL;
15595 	int ret = 0;
15596 
15597 	/*
15598 	 * Leave the allmulti on all the ills and start fresh.
15599 	 */
15600 	for (ill = illgrp->illgrp_ill; ill != NULL;
15601 	    ill = ill->ill_group_next) {
15602 		if (ill->ill_join_allmulti)
15603 			(void) ip_leave_allmulti(ill->ill_ipif);
15604 	}
15605 
15606 	/*
15607 	 * Choose a good ill. Fallback to inactive or failed if
15608 	 * none available. We need to fallback to FAILED in the
15609 	 * case where we have 2 interfaces in a group - where
15610 	 * one of them is failed and another is a good one and
15611 	 * the good one (not marked inactive) is leaving the group.
15612 	 */
15613 	ret = 0;
15614 	for (ill = illgrp->illgrp_ill; ill != NULL;
15615 	    ill = ill->ill_group_next) {
15616 		/* Never pick an offline interface */
15617 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15618 			continue;
15619 
15620 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15621 			fallback_failed_ill = ill;
15622 			continue;
15623 		}
15624 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15625 			fallback_inactive_ill = ill;
15626 			continue;
15627 		}
15628 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15629 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15630 				ret = ip_join_allmulti(ill->ill_ipif);
15631 				/*
15632 				 * ip_join_allmulti can fail because of memory
15633 				 * failures. So, make sure we join at least
15634 				 * on one ill.
15635 				 */
15636 				if (ill->ill_join_allmulti)
15637 					return (0);
15638 			}
15639 		}
15640 	}
15641 	if (ret != 0) {
15642 		/*
15643 		 * If we tried nominating above and failed to do so,
15644 		 * return error. We might have tried multiple times.
15645 		 * But, return the latest error.
15646 		 */
15647 		return (ret);
15648 	}
15649 	if ((ill = fallback_inactive_ill) != NULL) {
15650 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15651 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15652 				ret = ip_join_allmulti(ill->ill_ipif);
15653 				return (ret);
15654 			}
15655 		}
15656 	} else if ((ill = fallback_failed_ill) != NULL) {
15657 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15658 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15659 				ret = ip_join_allmulti(ill->ill_ipif);
15660 				return (ret);
15661 			}
15662 		}
15663 	}
15664 	return (0);
15665 }
15666 
15667 /*
15668  * This function is called from illgrp_delete after it is
15669  * deleted from the group to reschedule responsibilities
15670  * to a different ill.
15671  */
15672 static void
15673 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15674 {
15675 	ilm_t	*ilm;
15676 	ipif_t	*ipif;
15677 	ipaddr_t subnet_addr;
15678 	ipaddr_t net_addr;
15679 	ipaddr_t net_mask = 0;
15680 	ipaddr_t subnet_netmask;
15681 	ipaddr_t addr;
15682 	ip_stack_t *ipst = ill->ill_ipst;
15683 
15684 	ASSERT(ill->ill_group == NULL);
15685 	/*
15686 	 * Broadcast Responsibility:
15687 	 *
15688 	 * 1. If this ill has been nominated for receiving broadcast
15689 	 * packets, we need to find a new one. Before we find a new
15690 	 * one, we need to re-group the ires that are part of this new
15691 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15692 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15693 	 * thing for us.
15694 	 *
15695 	 * 2. If this ill was not nominated for receiving broadcast
15696 	 * packets, we need to clear the IRE_MARK_NORECV flag
15697 	 * so that we continue to send up broadcast packets.
15698 	 */
15699 	if (!ill->ill_isv6) {
15700 		/*
15701 		 * Case 1 above : No optimization here. Just redo the
15702 		 * nomination.
15703 		 */
15704 		ill_group_bcast_for_xmit(ill);
15705 		ill_nominate_bcast_rcv(illgrp);
15706 
15707 		/*
15708 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15709 		 */
15710 		ill_clear_bcast_mark(ill, 0);
15711 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15712 
15713 		for (ipif = ill->ill_ipif; ipif != NULL;
15714 		    ipif = ipif->ipif_next) {
15715 
15716 			if (!(ipif->ipif_flags & IPIF_UP) ||
15717 			    ipif->ipif_subnet == 0) {
15718 				continue;
15719 			}
15720 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15721 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15722 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15723 			} else {
15724 				net_mask = htonl(IN_CLASSA_NET);
15725 			}
15726 			addr = net_mask & ipif->ipif_subnet;
15727 			ill_clear_bcast_mark(ill, addr);
15728 
15729 			net_addr = ~net_mask | addr;
15730 			ill_clear_bcast_mark(ill, net_addr);
15731 
15732 			subnet_netmask = ipif->ipif_net_mask;
15733 			addr = ipif->ipif_subnet;
15734 			ill_clear_bcast_mark(ill, addr);
15735 
15736 			subnet_addr = ~subnet_netmask | addr;
15737 			ill_clear_bcast_mark(ill, subnet_addr);
15738 		}
15739 	}
15740 
15741 	/*
15742 	 * Multicast Responsibility.
15743 	 *
15744 	 * If we have joined allmulti on this one, find a new member
15745 	 * in the group to join allmulti. As this ill is already part
15746 	 * of allmulti, we don't have to join on this one.
15747 	 *
15748 	 * If we have not joined allmulti on this one, there is no
15749 	 * responsibility to handoff. But we need to take new
15750 	 * responsibility i.e, join allmulti on this one if we need
15751 	 * to.
15752 	 */
15753 	if (ill->ill_join_allmulti) {
15754 		(void) ill_nominate_mcast_rcv(illgrp);
15755 	} else {
15756 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15757 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15758 				(void) ip_join_allmulti(ill->ill_ipif);
15759 				break;
15760 			}
15761 		}
15762 	}
15763 
15764 	/*
15765 	 * We intentionally do the flushing of IRE_CACHES only matching
15766 	 * on the ill and not on groups. Note that we are already deleted
15767 	 * from the group.
15768 	 *
15769 	 * This will make sure that all IRE_CACHES whose stq is pointing
15770 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15771 	 * deleted and IRE_CACHES that are not pointing at this ill will
15772 	 * be left alone.
15773 	 */
15774 	ire_walk_ill(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_CACHE,
15775 	    illgrp_cache_delete, ill, ill);
15776 
15777 	/*
15778 	 * Some conn may have cached one of the IREs deleted above. By removing
15779 	 * the ire reference, we clean up the extra reference to the ill held in
15780 	 * ire->ire_stq.
15781 	 */
15782 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15783 
15784 	/*
15785 	 * Re-do source address selection for all the members in the
15786 	 * group, if they borrowed source address from one of the ipifs
15787 	 * in this ill.
15788 	 */
15789 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15790 		if (ill->ill_isv6) {
15791 			ipif_update_other_ipifs_v6(ipif, illgrp);
15792 		} else {
15793 			ipif_update_other_ipifs(ipif, illgrp);
15794 		}
15795 	}
15796 }
15797 
15798 /*
15799  * Delete the ill from the group. The caller makes sure that it is
15800  * in a group and it okay to delete from the group. So, we always
15801  * delete here.
15802  */
15803 static void
15804 illgrp_delete(ill_t *ill)
15805 {
15806 	ill_group_t *illgrp;
15807 	ill_group_t *tmpg;
15808 	ill_t *tmp_ill;
15809 	ip_stack_t	*ipst = ill->ill_ipst;
15810 
15811 	/*
15812 	 * Reset illgrp_ill_schednext if it was pointing at us.
15813 	 * We need to do this before we set ill_group to NULL.
15814 	 */
15815 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15816 	mutex_enter(&ill->ill_lock);
15817 
15818 	illgrp_reset_schednext(ill);
15819 
15820 	illgrp = ill->ill_group;
15821 
15822 	/* Delete the ill from illgrp. */
15823 	if (illgrp->illgrp_ill == ill) {
15824 		illgrp->illgrp_ill = ill->ill_group_next;
15825 	} else {
15826 		tmp_ill = illgrp->illgrp_ill;
15827 		while (tmp_ill->ill_group_next != ill) {
15828 			tmp_ill = tmp_ill->ill_group_next;
15829 			ASSERT(tmp_ill != NULL);
15830 		}
15831 		tmp_ill->ill_group_next = ill->ill_group_next;
15832 	}
15833 	ill->ill_group = NULL;
15834 	ill->ill_group_next = NULL;
15835 
15836 	illgrp->illgrp_ill_count--;
15837 	mutex_exit(&ill->ill_lock);
15838 	rw_exit(&ipst->ips_ill_g_lock);
15839 
15840 	/*
15841 	 * As this ill is leaving the group, we need to hand off
15842 	 * the responsibilities to the other ills in the group, if
15843 	 * this ill had some responsibilities.
15844 	 */
15845 
15846 	ill_handoff_responsibility(ill, illgrp);
15847 
15848 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15849 
15850 	if (illgrp->illgrp_ill_count == 0) {
15851 
15852 		ASSERT(illgrp->illgrp_ill == NULL);
15853 		if (ill->ill_isv6) {
15854 			if (illgrp == ipst->ips_illgrp_head_v6) {
15855 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15856 			} else {
15857 				tmpg = ipst->ips_illgrp_head_v6;
15858 				while (tmpg->illgrp_next != illgrp) {
15859 					tmpg = tmpg->illgrp_next;
15860 					ASSERT(tmpg != NULL);
15861 				}
15862 				tmpg->illgrp_next = illgrp->illgrp_next;
15863 			}
15864 		} else {
15865 			if (illgrp == ipst->ips_illgrp_head_v4) {
15866 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15867 			} else {
15868 				tmpg = ipst->ips_illgrp_head_v4;
15869 				while (tmpg->illgrp_next != illgrp) {
15870 					tmpg = tmpg->illgrp_next;
15871 					ASSERT(tmpg != NULL);
15872 				}
15873 				tmpg->illgrp_next = illgrp->illgrp_next;
15874 			}
15875 		}
15876 		mutex_destroy(&illgrp->illgrp_lock);
15877 		mi_free(illgrp);
15878 	}
15879 	rw_exit(&ipst->ips_ill_g_lock);
15880 
15881 	/*
15882 	 * Even though the ill is out of the group its not necessary
15883 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15884 	 * We will split the ipsq when phyint_groupname is set to NULL.
15885 	 */
15886 
15887 	/*
15888 	 * Send a routing sockets message if we are deleting from
15889 	 * groups with names.
15890 	 */
15891 	if (ill->ill_phyint->phyint_groupname_len != 0)
15892 		ip_rts_ifmsg(ill->ill_ipif);
15893 }
15894 
15895 /*
15896  * Re-do source address selection. This is normally called when
15897  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15898  * ipif comes up.
15899  */
15900 void
15901 ill_update_source_selection(ill_t *ill)
15902 {
15903 	ipif_t *ipif;
15904 
15905 	ASSERT(IAM_WRITER_ILL(ill));
15906 
15907 	if (ill->ill_group != NULL)
15908 		ill = ill->ill_group->illgrp_ill;
15909 
15910 	for (; ill != NULL; ill = ill->ill_group_next) {
15911 		for (ipif = ill->ill_ipif; ipif != NULL;
15912 		    ipif = ipif->ipif_next) {
15913 			if (ill->ill_isv6)
15914 				ipif_recreate_interface_routes_v6(NULL, ipif);
15915 			else
15916 				ipif_recreate_interface_routes(NULL, ipif);
15917 		}
15918 	}
15919 }
15920 
15921 /*
15922  * Insert ill in a group headed by illgrp_head. The caller can either
15923  * pass a groupname in which case we search for a group with the
15924  * same name to insert in or pass a group to insert in. This function
15925  * would only search groups with names.
15926  *
15927  * NOTE : The caller should make sure that there is at least one ipif
15928  *	  UP on this ill so that illgrp_scheduler can pick this ill
15929  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15930  *	  already sent a DL_UNBIND to the driver and we don't want to
15931  *	  send anymore packets. We don't assert for ipif_up_count
15932  *	  to be greater than zero, because ipif_up_done wants to call
15933  *	  this function before bumping up the ipif_up_count. See
15934  *	  ipif_up_done() for details.
15935  */
15936 int
15937 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15938     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15939 {
15940 	ill_group_t *illgrp;
15941 	ill_t *prev_ill;
15942 	phyint_t *phyi;
15943 	ip_stack_t	*ipst = ill->ill_ipst;
15944 
15945 	ASSERT(ill->ill_group == NULL);
15946 
15947 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15948 	mutex_enter(&ill->ill_lock);
15949 
15950 	if (groupname != NULL) {
15951 		/*
15952 		 * Look for a group with a matching groupname to insert.
15953 		 */
15954 		for (illgrp = *illgrp_head; illgrp != NULL;
15955 		    illgrp = illgrp->illgrp_next) {
15956 
15957 			ill_t *tmp_ill;
15958 
15959 			/*
15960 			 * If we have an ill_group_t in the list which has
15961 			 * no ill_t assigned then we must be in the process of
15962 			 * removing this group. We skip this as illgrp_delete()
15963 			 * will remove it from the list.
15964 			 */
15965 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15966 				ASSERT(illgrp->illgrp_ill_count == 0);
15967 				continue;
15968 			}
15969 
15970 			ASSERT(tmp_ill->ill_phyint != NULL);
15971 			phyi = tmp_ill->ill_phyint;
15972 			/*
15973 			 * Look at groups which has names only.
15974 			 */
15975 			if (phyi->phyint_groupname_len == 0)
15976 				continue;
15977 			/*
15978 			 * Names are stored in the phyint common to both
15979 			 * IPv4 and IPv6.
15980 			 */
15981 			if (mi_strcmp(phyi->phyint_groupname,
15982 			    groupname) == 0) {
15983 				break;
15984 			}
15985 		}
15986 	} else {
15987 		/*
15988 		 * If the caller passes in a NULL "grp_to_insert", we
15989 		 * allocate one below and insert this singleton.
15990 		 */
15991 		illgrp = grp_to_insert;
15992 	}
15993 
15994 	ill->ill_group_next = NULL;
15995 
15996 	if (illgrp == NULL) {
15997 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15998 		if (illgrp == NULL) {
15999 			return (ENOMEM);
16000 		}
16001 		illgrp->illgrp_next = *illgrp_head;
16002 		*illgrp_head = illgrp;
16003 		illgrp->illgrp_ill = ill;
16004 		illgrp->illgrp_ill_count = 1;
16005 		ill->ill_group = illgrp;
16006 		/*
16007 		 * Used in illgrp_scheduler to protect multiple threads
16008 		 * from traversing the list.
16009 		 */
16010 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16011 	} else {
16012 		ASSERT(ill->ill_net_type ==
16013 		    illgrp->illgrp_ill->ill_net_type);
16014 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16015 
16016 		/* Insert ill at tail of this group */
16017 		prev_ill = illgrp->illgrp_ill;
16018 		while (prev_ill->ill_group_next != NULL)
16019 			prev_ill = prev_ill->ill_group_next;
16020 		prev_ill->ill_group_next = ill;
16021 		ill->ill_group = illgrp;
16022 		illgrp->illgrp_ill_count++;
16023 		/*
16024 		 * Inherit group properties. Currently only forwarding
16025 		 * is the property we try to keep the same with all the
16026 		 * ills. When there are more, we will abstract this into
16027 		 * a function.
16028 		 */
16029 		ill->ill_flags &= ~ILLF_ROUTER;
16030 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16031 	}
16032 	mutex_exit(&ill->ill_lock);
16033 	rw_exit(&ipst->ips_ill_g_lock);
16034 
16035 	/*
16036 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16037 	 *    may be zero as it has not yet been bumped. But the ires
16038 	 *    have already been added. So, we do the nomination here
16039 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16040 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16041 	 *    ill_ipif_up_count here while nominating broadcast ires for
16042 	 *    receive.
16043 	 *
16044 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16045 	 *    to group them properly as ire_add() has already happened
16046 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16047 	 *    case, we need to do it here anyway.
16048 	 */
16049 	if (!ill->ill_isv6) {
16050 		ill_group_bcast_for_xmit(ill);
16051 		ill_nominate_bcast_rcv(illgrp);
16052 	}
16053 
16054 	if (!ipif_is_coming_up) {
16055 		/*
16056 		 * When ipif_up_done() calls this function, the multicast
16057 		 * groups have not been joined yet. So, there is no point in
16058 		 * nomination. ip_join_allmulti will handle groups when
16059 		 * ill_recover_multicast is called from ipif_up_done() later.
16060 		 */
16061 		(void) ill_nominate_mcast_rcv(illgrp);
16062 		/*
16063 		 * ipif_up_done calls ill_update_source_selection
16064 		 * anyway. Moreover, we don't want to re-create
16065 		 * interface routes while ipif_up_done() still has reference
16066 		 * to them. Refer to ipif_up_done() for more details.
16067 		 */
16068 		ill_update_source_selection(ill);
16069 	}
16070 
16071 	/*
16072 	 * Send a routing sockets message if we are inserting into
16073 	 * groups with names.
16074 	 */
16075 	if (groupname != NULL)
16076 		ip_rts_ifmsg(ill->ill_ipif);
16077 	return (0);
16078 }
16079 
16080 /*
16081  * Return the first phyint matching the groupname. There could
16082  * be more than one when there are ill groups.
16083  *
16084  * If 'usable' is set, then we exclude ones that are marked with any of
16085  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16086  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16087  * emulation of ipmp.
16088  */
16089 phyint_t *
16090 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16091 {
16092 	phyint_t *phyi;
16093 
16094 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16095 	/*
16096 	 * Group names are stored in the phyint - a common structure
16097 	 * to both IPv4 and IPv6.
16098 	 */
16099 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16100 	for (; phyi != NULL;
16101 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16102 	    phyi, AVL_AFTER)) {
16103 		if (phyi->phyint_groupname_len == 0)
16104 			continue;
16105 		/*
16106 		 * Skip the ones that should not be used since the callers
16107 		 * sometime use this for sending packets.
16108 		 */
16109 		if (usable && (phyi->phyint_flags &
16110 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16111 			continue;
16112 
16113 		ASSERT(phyi->phyint_groupname != NULL);
16114 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16115 			return (phyi);
16116 	}
16117 	return (NULL);
16118 }
16119 
16120 
16121 /*
16122  * Return the first usable phyint matching the group index. By 'usable'
16123  * we exclude ones that are marked ununsable with any of
16124  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16125  *
16126  * Used only for the ipmp/netinfo emulation of ipmp.
16127  */
16128 phyint_t *
16129 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16130 {
16131 	phyint_t *phyi;
16132 
16133 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16134 
16135 	if (!ipst->ips_ipmp_hook_emulation)
16136 		return (NULL);
16137 
16138 	/*
16139 	 * Group indicies are stored in the phyint - a common structure
16140 	 * to both IPv4 and IPv6.
16141 	 */
16142 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16143 	for (; phyi != NULL;
16144 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16145 	    phyi, AVL_AFTER)) {
16146 		/* Ignore the ones that do not have a group */
16147 		if (phyi->phyint_groupname_len == 0)
16148 			continue;
16149 
16150 		ASSERT(phyi->phyint_group_ifindex != 0);
16151 		/*
16152 		 * Skip the ones that should not be used since the callers
16153 		 * sometime use this for sending packets.
16154 		 */
16155 		if (phyi->phyint_flags &
16156 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16157 			continue;
16158 		if (phyi->phyint_group_ifindex == group_ifindex)
16159 			return (phyi);
16160 	}
16161 	return (NULL);
16162 }
16163 
16164 /*
16165  * MT notes on creation and deletion of IPMP groups
16166  *
16167  * Creation and deletion of IPMP groups introduce the need to merge or
16168  * split the associated serialization objects i.e the ipsq's. Normally all
16169  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16170  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16171  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16172  * is a need to change the <ill-ipsq> association and we have to operate on both
16173  * the source and destination IPMP groups. For eg. attempting to set the
16174  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16175  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16176  * source or destination IPMP group are mapped to a single ipsq for executing
16177  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16178  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16179  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16180  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16181  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16182  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16183  *
16184  * In the above example the ioctl handling code locates the current ipsq of hme0
16185  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16186  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16187  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16188  * the destination ipsq. If the destination ipsq is not busy, it also enters
16189  * the destination ipsq exclusively. Now the actual groupname setting operation
16190  * can proceed. If the destination ipsq is busy, the operation is enqueued
16191  * on the destination (merged) ipsq and will be handled in the unwind from
16192  * ipsq_exit.
16193  *
16194  * To prevent other threads accessing the ill while the group name change is
16195  * in progres, we bring down the ipifs which also removes the ill from the
16196  * group. The group is changed in phyint and when the first ipif on the ill
16197  * is brought up, the ill is inserted into the right IPMP group by
16198  * illgrp_insert.
16199  */
16200 /* ARGSUSED */
16201 int
16202 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16203     ip_ioctl_cmd_t *ipip, void *ifreq)
16204 {
16205 	int i;
16206 	char *tmp;
16207 	int namelen;
16208 	ill_t *ill = ipif->ipif_ill;
16209 	ill_t *ill_v4, *ill_v6;
16210 	int err = 0;
16211 	phyint_t *phyi;
16212 	phyint_t *phyi_tmp;
16213 	struct lifreq *lifr;
16214 	mblk_t	*mp1;
16215 	char *groupname;
16216 	ipsq_t *ipsq;
16217 	ip_stack_t	*ipst = ill->ill_ipst;
16218 
16219 	ASSERT(IAM_WRITER_IPIF(ipif));
16220 
16221 	/* Existance verified in ip_wput_nondata */
16222 	mp1 = mp->b_cont->b_cont;
16223 	lifr = (struct lifreq *)mp1->b_rptr;
16224 	groupname = lifr->lifr_groupname;
16225 
16226 	if (ipif->ipif_id != 0)
16227 		return (EINVAL);
16228 
16229 	phyi = ill->ill_phyint;
16230 	ASSERT(phyi != NULL);
16231 
16232 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16233 		return (EINVAL);
16234 
16235 	tmp = groupname;
16236 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16237 		;
16238 
16239 	if (i == LIFNAMSIZ) {
16240 		/* no null termination */
16241 		return (EINVAL);
16242 	}
16243 
16244 	/*
16245 	 * Calculate the namelen exclusive of the null
16246 	 * termination character.
16247 	 */
16248 	namelen = tmp - groupname;
16249 
16250 	ill_v4 = phyi->phyint_illv4;
16251 	ill_v6 = phyi->phyint_illv6;
16252 
16253 	/*
16254 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16255 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16256 	 * synchronization notes in ip.c
16257 	 */
16258 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16259 		return (EINVAL);
16260 	}
16261 
16262 	/*
16263 	 * mark the ill as changing.
16264 	 * this should queue all new requests on the syncq.
16265 	 */
16266 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16267 
16268 	if (ill_v4 != NULL)
16269 		ill_v4->ill_state_flags |= ILL_CHANGING;
16270 	if (ill_v6 != NULL)
16271 		ill_v6->ill_state_flags |= ILL_CHANGING;
16272 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16273 
16274 	if (namelen == 0) {
16275 		/*
16276 		 * Null string means remove this interface from the
16277 		 * existing group.
16278 		 */
16279 		if (phyi->phyint_groupname_len == 0) {
16280 			/*
16281 			 * Never was in a group.
16282 			 */
16283 			err = 0;
16284 			goto done;
16285 		}
16286 
16287 		/*
16288 		 * IPv4 or IPv6 may be temporarily out of the group when all
16289 		 * the ipifs are down. Thus, we need to check for ill_group to
16290 		 * be non-NULL.
16291 		 */
16292 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16293 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16294 			mutex_enter(&ill_v4->ill_lock);
16295 			if (!ill_is_quiescent(ill_v4)) {
16296 				/*
16297 				 * ipsq_pending_mp_add will not fail since
16298 				 * connp is NULL
16299 				 */
16300 				(void) ipsq_pending_mp_add(NULL,
16301 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16302 				mutex_exit(&ill_v4->ill_lock);
16303 				err = EINPROGRESS;
16304 				goto done;
16305 			}
16306 			mutex_exit(&ill_v4->ill_lock);
16307 		}
16308 
16309 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16310 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16311 			mutex_enter(&ill_v6->ill_lock);
16312 			if (!ill_is_quiescent(ill_v6)) {
16313 				(void) ipsq_pending_mp_add(NULL,
16314 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16315 				mutex_exit(&ill_v6->ill_lock);
16316 				err = EINPROGRESS;
16317 				goto done;
16318 			}
16319 			mutex_exit(&ill_v6->ill_lock);
16320 		}
16321 
16322 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16323 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16324 		mutex_enter(&phyi->phyint_lock);
16325 		ASSERT(phyi->phyint_groupname != NULL);
16326 		mi_free(phyi->phyint_groupname);
16327 		phyi->phyint_groupname = NULL;
16328 		phyi->phyint_groupname_len = 0;
16329 
16330 		/* Restore the ifindex used to be the per interface one */
16331 		phyi->phyint_group_ifindex = 0;
16332 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16333 		mutex_exit(&phyi->phyint_lock);
16334 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16335 		rw_exit(&ipst->ips_ill_g_lock);
16336 		err = ill_up_ipifs(ill, q, mp);
16337 
16338 		/*
16339 		 * set the split flag so that the ipsq can be split
16340 		 */
16341 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16342 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16343 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16344 
16345 	} else {
16346 		if (phyi->phyint_groupname_len != 0) {
16347 			ASSERT(phyi->phyint_groupname != NULL);
16348 			/* Are we inserting in the same group ? */
16349 			if (mi_strcmp(groupname,
16350 			    phyi->phyint_groupname) == 0) {
16351 				err = 0;
16352 				goto done;
16353 			}
16354 		}
16355 
16356 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16357 		/*
16358 		 * Merge ipsq for the group's.
16359 		 * This check is here as multiple groups/ills might be
16360 		 * sharing the same ipsq.
16361 		 * If we have to merege than the operation is restarted
16362 		 * on the new ipsq.
16363 		 */
16364 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16365 		if (phyi->phyint_ipsq != ipsq) {
16366 			rw_exit(&ipst->ips_ill_g_lock);
16367 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16368 			goto done;
16369 		}
16370 		/*
16371 		 * Running exclusive on new ipsq.
16372 		 */
16373 
16374 		ASSERT(ipsq != NULL);
16375 		ASSERT(ipsq->ipsq_writer == curthread);
16376 
16377 		/*
16378 		 * Check whether the ill_type and ill_net_type matches before
16379 		 * we allocate any memory so that the cleanup is easier.
16380 		 *
16381 		 * We can't group dissimilar ones as we can't load spread
16382 		 * packets across the group because of potential link-level
16383 		 * header differences.
16384 		 */
16385 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16386 		if (phyi_tmp != NULL) {
16387 			if ((ill_v4 != NULL &&
16388 			    phyi_tmp->phyint_illv4 != NULL) &&
16389 			    ((ill_v4->ill_net_type !=
16390 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16391 			    (ill_v4->ill_type !=
16392 			    phyi_tmp->phyint_illv4->ill_type))) {
16393 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16394 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16395 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16396 				rw_exit(&ipst->ips_ill_g_lock);
16397 				return (EINVAL);
16398 			}
16399 			if ((ill_v6 != NULL &&
16400 			    phyi_tmp->phyint_illv6 != NULL) &&
16401 			    ((ill_v6->ill_net_type !=
16402 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16403 			    (ill_v6->ill_type !=
16404 			    phyi_tmp->phyint_illv6->ill_type))) {
16405 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16406 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16407 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16408 				rw_exit(&ipst->ips_ill_g_lock);
16409 				return (EINVAL);
16410 			}
16411 		}
16412 
16413 		rw_exit(&ipst->ips_ill_g_lock);
16414 
16415 		/*
16416 		 * bring down all v4 ipifs.
16417 		 */
16418 		if (ill_v4 != NULL) {
16419 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16420 		}
16421 
16422 		/*
16423 		 * bring down all v6 ipifs.
16424 		 */
16425 		if (ill_v6 != NULL) {
16426 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16427 		}
16428 
16429 		/*
16430 		 * make sure all ipifs are down and there are no active
16431 		 * references. Call to ipsq_pending_mp_add will not fail
16432 		 * since connp is NULL.
16433 		 */
16434 		if (ill_v4 != NULL) {
16435 			mutex_enter(&ill_v4->ill_lock);
16436 			if (!ill_is_quiescent(ill_v4)) {
16437 				(void) ipsq_pending_mp_add(NULL,
16438 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16439 				mutex_exit(&ill_v4->ill_lock);
16440 				err = EINPROGRESS;
16441 				goto done;
16442 			}
16443 			mutex_exit(&ill_v4->ill_lock);
16444 		}
16445 
16446 		if (ill_v6 != NULL) {
16447 			mutex_enter(&ill_v6->ill_lock);
16448 			if (!ill_is_quiescent(ill_v6)) {
16449 				(void) ipsq_pending_mp_add(NULL,
16450 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16451 				mutex_exit(&ill_v6->ill_lock);
16452 				err = EINPROGRESS;
16453 				goto done;
16454 			}
16455 			mutex_exit(&ill_v6->ill_lock);
16456 		}
16457 
16458 		/*
16459 		 * allocate including space for null terminator
16460 		 * before we insert.
16461 		 */
16462 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16463 		if (tmp == NULL)
16464 			return (ENOMEM);
16465 
16466 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16467 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16468 		mutex_enter(&phyi->phyint_lock);
16469 		if (phyi->phyint_groupname_len != 0) {
16470 			ASSERT(phyi->phyint_groupname != NULL);
16471 			mi_free(phyi->phyint_groupname);
16472 		}
16473 
16474 		/*
16475 		 * setup the new group name.
16476 		 */
16477 		phyi->phyint_groupname = tmp;
16478 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16479 		phyi->phyint_groupname_len = namelen + 1;
16480 
16481 		if (ipst->ips_ipmp_hook_emulation) {
16482 			/*
16483 			 * If the group already exists we use the existing
16484 			 * group_ifindex, otherwise we pick a new index here.
16485 			 */
16486 			if (phyi_tmp != NULL) {
16487 				phyi->phyint_group_ifindex =
16488 				    phyi_tmp->phyint_group_ifindex;
16489 			} else {
16490 				/* XXX We need a recovery strategy here. */
16491 				if (!ip_assign_ifindex(
16492 				    &phyi->phyint_group_ifindex, ipst))
16493 					cmn_err(CE_PANIC,
16494 					    "ip_assign_ifindex() failed");
16495 			}
16496 		}
16497 		/*
16498 		 * Select whether the netinfo and hook use the per-interface
16499 		 * or per-group ifindex.
16500 		 */
16501 		if (ipst->ips_ipmp_hook_emulation)
16502 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16503 		else
16504 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16505 
16506 		if (ipst->ips_ipmp_hook_emulation &&
16507 		    phyi_tmp != NULL) {
16508 			/* First phyint in group - group PLUMB event */
16509 			ill_nic_info_plumb(ill, B_TRUE);
16510 		}
16511 		mutex_exit(&phyi->phyint_lock);
16512 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16513 		rw_exit(&ipst->ips_ill_g_lock);
16514 
16515 		err = ill_up_ipifs(ill, q, mp);
16516 	}
16517 
16518 done:
16519 	/*
16520 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16521 	 */
16522 	if (err != EINPROGRESS) {
16523 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16524 		if (ill_v4 != NULL)
16525 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16526 		if (ill_v6 != NULL)
16527 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16528 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16529 	}
16530 	return (err);
16531 }
16532 
16533 /* ARGSUSED */
16534 int
16535 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16536     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16537 {
16538 	ill_t *ill;
16539 	phyint_t *phyi;
16540 	struct lifreq *lifr;
16541 	mblk_t	*mp1;
16542 
16543 	/* Existence verified in ip_wput_nondata */
16544 	mp1 = mp->b_cont->b_cont;
16545 	lifr = (struct lifreq *)mp1->b_rptr;
16546 	ill = ipif->ipif_ill;
16547 	phyi = ill->ill_phyint;
16548 
16549 	lifr->lifr_groupname[0] = '\0';
16550 	/*
16551 	 * ill_group may be null if all the interfaces
16552 	 * are down. But still, the phyint should always
16553 	 * hold the name.
16554 	 */
16555 	if (phyi->phyint_groupname_len != 0) {
16556 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16557 		    phyi->phyint_groupname_len);
16558 	}
16559 
16560 	return (0);
16561 }
16562 
16563 
16564 typedef struct conn_move_s {
16565 	ill_t	*cm_from_ill;
16566 	ill_t	*cm_to_ill;
16567 	int	cm_ifindex;
16568 } conn_move_t;
16569 
16570 /*
16571  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16572  */
16573 static void
16574 conn_move(conn_t *connp, caddr_t arg)
16575 {
16576 	conn_move_t *connm;
16577 	int ifindex;
16578 	int i;
16579 	ill_t *from_ill;
16580 	ill_t *to_ill;
16581 	ilg_t *ilg;
16582 	ilm_t *ret_ilm;
16583 
16584 	connm = (conn_move_t *)arg;
16585 	ifindex = connm->cm_ifindex;
16586 	from_ill = connm->cm_from_ill;
16587 	to_ill = connm->cm_to_ill;
16588 
16589 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16590 
16591 	/* All multicast fields protected by conn_lock */
16592 	mutex_enter(&connp->conn_lock);
16593 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16594 	if ((connp->conn_outgoing_ill == from_ill) &&
16595 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16596 		connp->conn_outgoing_ill = to_ill;
16597 		connp->conn_incoming_ill = to_ill;
16598 	}
16599 
16600 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16601 
16602 	if ((connp->conn_multicast_ill == from_ill) &&
16603 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16604 		connp->conn_multicast_ill = connm->cm_to_ill;
16605 	}
16606 
16607 	/*
16608 	 * Change the ilg_ill to point to the new one. This assumes
16609 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16610 	 * has been told to receive packets on this interface.
16611 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16612 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16613 	 * some ilms may not have moved. We check to see whether
16614 	 * the ilms have moved to to_ill. We can't check on from_ill
16615 	 * as in the process of moving, we could have split an ilm
16616 	 * in to two - which has the same orig_ifindex and v6group.
16617 	 *
16618 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16619 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16620 	 */
16621 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16622 		ilg = &connp->conn_ilg[i];
16623 		if ((ilg->ilg_ill == from_ill) &&
16624 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16625 			/* ifindex != 0 indicates failback */
16626 			if (ifindex != 0) {
16627 				connp->conn_ilg[i].ilg_ill = to_ill;
16628 				continue;
16629 			}
16630 
16631 			mutex_enter(&to_ill->ill_lock);
16632 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16633 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16634 			    connp->conn_zoneid);
16635 			mutex_exit(&to_ill->ill_lock);
16636 
16637 			if (ret_ilm != NULL)
16638 				connp->conn_ilg[i].ilg_ill = to_ill;
16639 		}
16640 	}
16641 	mutex_exit(&connp->conn_lock);
16642 }
16643 
16644 static void
16645 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16646 {
16647 	conn_move_t connm;
16648 	ip_stack_t	*ipst = from_ill->ill_ipst;
16649 
16650 	connm.cm_from_ill = from_ill;
16651 	connm.cm_to_ill = to_ill;
16652 	connm.cm_ifindex = ifindex;
16653 
16654 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16655 }
16656 
16657 /*
16658  * ilm has been moved from from_ill to to_ill.
16659  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16660  * appropriately.
16661  *
16662  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16663  *	  the code there de-references ipif_ill to get the ill to
16664  *	  send multicast requests. It does not work as ipif is on its
16665  *	  move and already moved when this function is called.
16666  *	  Thus, we need to use from_ill and to_ill send down multicast
16667  *	  requests.
16668  */
16669 static void
16670 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16671 {
16672 	ipif_t *ipif;
16673 	ilm_t *ilm;
16674 
16675 	/*
16676 	 * See whether we need to send down DL_ENABMULTI_REQ on
16677 	 * to_ill as ilm has just been added.
16678 	 */
16679 	ASSERT(IAM_WRITER_ILL(to_ill));
16680 	ASSERT(IAM_WRITER_ILL(from_ill));
16681 
16682 	ILM_WALKER_HOLD(to_ill);
16683 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16684 
16685 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16686 			continue;
16687 		/*
16688 		 * no locks held, ill/ipif cannot dissappear as long
16689 		 * as we are writer.
16690 		 */
16691 		ipif = to_ill->ill_ipif;
16692 		/*
16693 		 * No need to hold any lock as we are the writer and this
16694 		 * can only be changed by a writer.
16695 		 */
16696 		ilm->ilm_is_new = B_FALSE;
16697 
16698 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16699 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16700 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16701 			    "resolver\n"));
16702 			continue;		/* Must be IRE_IF_NORESOLVER */
16703 		}
16704 
16705 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16706 			ip1dbg(("ilm_send_multicast_reqs: "
16707 			    "to_ill MULTI_BCAST\n"));
16708 			goto from;
16709 		}
16710 
16711 		if (to_ill->ill_isv6)
16712 			mld_joingroup(ilm);
16713 		else
16714 			igmp_joingroup(ilm);
16715 
16716 		if (to_ill->ill_ipif_up_count == 0) {
16717 			/*
16718 			 * Nobody there. All multicast addresses will be
16719 			 * re-joined when we get the DL_BIND_ACK bringing the
16720 			 * interface up.
16721 			 */
16722 			ilm->ilm_notify_driver = B_FALSE;
16723 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16724 			goto from;
16725 		}
16726 
16727 		/*
16728 		 * For allmulti address, we want to join on only one interface.
16729 		 * Checking for ilm_numentries_v6 is not correct as you may
16730 		 * find an ilm with zero address on to_ill, but we may not
16731 		 * have nominated to_ill for receiving. Thus, if we have
16732 		 * nominated from_ill (ill_join_allmulti is set), nominate
16733 		 * only if to_ill is not already nominated (to_ill normally
16734 		 * should not have been nominated if "from_ill" has already
16735 		 * been nominated. As we don't prevent failovers from happening
16736 		 * across groups, we don't assert).
16737 		 */
16738 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16739 			/*
16740 			 * There is no need to hold ill locks as we are
16741 			 * writer on both ills and when ill_join_allmulti
16742 			 * is changed the thread is always a writer.
16743 			 */
16744 			if (from_ill->ill_join_allmulti &&
16745 			    !to_ill->ill_join_allmulti) {
16746 				(void) ip_join_allmulti(to_ill->ill_ipif);
16747 			}
16748 		} else if (ilm->ilm_notify_driver) {
16749 
16750 			/*
16751 			 * This is a newly moved ilm so we need to tell the
16752 			 * driver about the new group. There can be more than
16753 			 * one ilm's for the same group in the list each with a
16754 			 * different orig_ifindex. We have to inform the driver
16755 			 * once. In ilm_move_v[4,6] we only set the flag
16756 			 * ilm_notify_driver for the first ilm.
16757 			 */
16758 
16759 			(void) ip_ll_send_enabmulti_req(to_ill,
16760 			    &ilm->ilm_v6addr);
16761 		}
16762 
16763 		ilm->ilm_notify_driver = B_FALSE;
16764 
16765 		/*
16766 		 * See whether we need to send down DL_DISABMULTI_REQ on
16767 		 * from_ill as ilm has just been removed.
16768 		 */
16769 from:
16770 		ipif = from_ill->ill_ipif;
16771 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16772 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16773 			ip1dbg(("ilm_send_multicast_reqs: "
16774 			    "from_ill not resolver\n"));
16775 			continue;		/* Must be IRE_IF_NORESOLVER */
16776 		}
16777 
16778 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16779 			ip1dbg(("ilm_send_multicast_reqs: "
16780 			    "from_ill MULTI_BCAST\n"));
16781 			continue;
16782 		}
16783 
16784 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16785 			if (from_ill->ill_join_allmulti)
16786 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16787 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16788 			(void) ip_ll_send_disabmulti_req(from_ill,
16789 			    &ilm->ilm_v6addr);
16790 		}
16791 	}
16792 	ILM_WALKER_RELE(to_ill);
16793 }
16794 
16795 /*
16796  * This function is called when all multicast memberships needs
16797  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16798  * called only once unlike the IPv4 counterpart where it is called after
16799  * every logical interface is moved. The reason is due to multicast
16800  * memberships are joined using an interface address in IPv4 while in
16801  * IPv6, interface index is used.
16802  */
16803 static void
16804 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16805 {
16806 	ilm_t	*ilm;
16807 	ilm_t	*ilm_next;
16808 	ilm_t	*new_ilm;
16809 	ilm_t	**ilmp;
16810 	int	count;
16811 	char buf[INET6_ADDRSTRLEN];
16812 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16813 	ip_stack_t	*ipst = from_ill->ill_ipst;
16814 
16815 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16816 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16817 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16818 
16819 	if (ifindex == 0) {
16820 		/*
16821 		 * Form the solicited node mcast address which is used later.
16822 		 */
16823 		ipif_t *ipif;
16824 
16825 		ipif = from_ill->ill_ipif;
16826 		ASSERT(ipif->ipif_id == 0);
16827 
16828 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16829 	}
16830 
16831 	ilmp = &from_ill->ill_ilm;
16832 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16833 		ilm_next = ilm->ilm_next;
16834 
16835 		if (ilm->ilm_flags & ILM_DELETED) {
16836 			ilmp = &ilm->ilm_next;
16837 			continue;
16838 		}
16839 
16840 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16841 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16842 		ASSERT(ilm->ilm_orig_ifindex != 0);
16843 		if (ilm->ilm_orig_ifindex == ifindex) {
16844 			/*
16845 			 * We are failing back multicast memberships.
16846 			 * If the same ilm exists in to_ill, it means somebody
16847 			 * has joined the same group there e.g. ff02::1
16848 			 * is joined within the kernel when the interfaces
16849 			 * came UP.
16850 			 */
16851 			ASSERT(ilm->ilm_ipif == NULL);
16852 			if (new_ilm != NULL) {
16853 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16854 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16855 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16856 					new_ilm->ilm_is_new = B_TRUE;
16857 				}
16858 			} else {
16859 				/*
16860 				 * check if we can just move the ilm
16861 				 */
16862 				if (from_ill->ill_ilm_walker_cnt != 0) {
16863 					/*
16864 					 * We have walkers we cannot move
16865 					 * the ilm, so allocate a new ilm,
16866 					 * this (old) ilm will be marked
16867 					 * ILM_DELETED at the end of the loop
16868 					 * and will be freed when the
16869 					 * last walker exits.
16870 					 */
16871 					new_ilm = (ilm_t *)mi_zalloc
16872 					    (sizeof (ilm_t));
16873 					if (new_ilm == NULL) {
16874 						ip0dbg(("ilm_move_v6: "
16875 						    "FAILBACK of IPv6"
16876 						    " multicast address %s : "
16877 						    "from %s to"
16878 						    " %s failed : ENOMEM \n",
16879 						    inet_ntop(AF_INET6,
16880 						    &ilm->ilm_v6addr, buf,
16881 						    sizeof (buf)),
16882 						    from_ill->ill_name,
16883 						    to_ill->ill_name));
16884 
16885 							ilmp = &ilm->ilm_next;
16886 							continue;
16887 					}
16888 					*new_ilm = *ilm;
16889 					/*
16890 					 * we don't want new_ilm linked to
16891 					 * ilm's filter list.
16892 					 */
16893 					new_ilm->ilm_filter = NULL;
16894 				} else {
16895 					/*
16896 					 * No walkers we can move the ilm.
16897 					 * lets take it out of the list.
16898 					 */
16899 					*ilmp = ilm->ilm_next;
16900 					ilm->ilm_next = NULL;
16901 					DTRACE_PROBE3(ill__decr__cnt,
16902 					    (ill_t *), from_ill,
16903 					    (char *), "ilm", (void *), ilm);
16904 					ASSERT(from_ill->ill_ilm_cnt > 0);
16905 					from_ill->ill_ilm_cnt--;
16906 
16907 					new_ilm = ilm;
16908 				}
16909 
16910 				/*
16911 				 * if this is the first ilm for the group
16912 				 * set ilm_notify_driver so that we notify the
16913 				 * driver in ilm_send_multicast_reqs.
16914 				 */
16915 				if (ilm_lookup_ill_v6(to_ill,
16916 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16917 					new_ilm->ilm_notify_driver = B_TRUE;
16918 
16919 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16920 				    (char *), "ilm", (void *), new_ilm);
16921 				new_ilm->ilm_ill = to_ill;
16922 				to_ill->ill_ilm_cnt++;
16923 
16924 				/* Add to the to_ill's list */
16925 				new_ilm->ilm_next = to_ill->ill_ilm;
16926 				to_ill->ill_ilm = new_ilm;
16927 				/*
16928 				 * set the flag so that mld_joingroup is
16929 				 * called in ilm_send_multicast_reqs().
16930 				 */
16931 				new_ilm->ilm_is_new = B_TRUE;
16932 			}
16933 			goto bottom;
16934 		} else if (ifindex != 0) {
16935 			/*
16936 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16937 			 * has not matched above, look at the next ilm.
16938 			 */
16939 			ilmp = &ilm->ilm_next;
16940 			continue;
16941 		}
16942 		/*
16943 		 * If we are here, it means ifindex is 0. Failover
16944 		 * everything.
16945 		 *
16946 		 * We need to handle solicited node mcast address
16947 		 * and all_nodes mcast address differently as they
16948 		 * are joined witin the kenrel (ipif_multicast_up)
16949 		 * and potentially from the userland. We are called
16950 		 * after the ipifs of from_ill has been moved.
16951 		 * If we still find ilms on ill with solicited node
16952 		 * mcast address or all_nodes mcast address, it must
16953 		 * belong to the UP interface that has not moved e.g.
16954 		 * ipif_id 0 with the link local prefix does not move.
16955 		 * We join this on the new ill accounting for all the
16956 		 * userland memberships so that applications don't
16957 		 * see any failure.
16958 		 *
16959 		 * We need to make sure that we account only for the
16960 		 * solicited node and all node multicast addresses
16961 		 * that was brought UP on these. In the case of
16962 		 * a failover from A to B, we might have ilms belonging
16963 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16964 		 * for the membership from the userland. If we are failing
16965 		 * over from B to C now, we will find the ones belonging
16966 		 * to A on B. These don't account for the ill_ipif_up_count.
16967 		 * They just move from B to C. The check below on
16968 		 * ilm_orig_ifindex ensures that.
16969 		 */
16970 		if ((ilm->ilm_orig_ifindex ==
16971 		    from_ill->ill_phyint->phyint_ifindex) &&
16972 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16973 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16974 		    &ilm->ilm_v6addr))) {
16975 			ASSERT(ilm->ilm_refcnt > 0);
16976 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16977 			/*
16978 			 * For indentation reasons, we are not using a
16979 			 * "else" here.
16980 			 */
16981 			if (count == 0) {
16982 				ilmp = &ilm->ilm_next;
16983 				continue;
16984 			}
16985 			ilm->ilm_refcnt -= count;
16986 			if (new_ilm != NULL) {
16987 				/*
16988 				 * Can find one with the same
16989 				 * ilm_orig_ifindex, if we are failing
16990 				 * over to a STANDBY. This happens
16991 				 * when somebody wants to join a group
16992 				 * on a STANDBY interface and we
16993 				 * internally join on a different one.
16994 				 * If we had joined on from_ill then, a
16995 				 * failover now will find a new ilm
16996 				 * with this index.
16997 				 */
16998 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16999 				    " new ilm on %s, group address %s\n",
17000 				    to_ill->ill_name,
17001 				    inet_ntop(AF_INET6,
17002 				    &ilm->ilm_v6addr, buf,
17003 				    sizeof (buf))));
17004 				new_ilm->ilm_refcnt += count;
17005 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17006 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17007 					new_ilm->ilm_is_new = B_TRUE;
17008 				}
17009 			} else {
17010 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17011 				if (new_ilm == NULL) {
17012 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17013 					    " multicast address %s : from %s to"
17014 					    " %s failed : ENOMEM \n",
17015 					    inet_ntop(AF_INET6,
17016 					    &ilm->ilm_v6addr, buf,
17017 					    sizeof (buf)), from_ill->ill_name,
17018 					    to_ill->ill_name));
17019 					ilmp = &ilm->ilm_next;
17020 					continue;
17021 				}
17022 				*new_ilm = *ilm;
17023 				new_ilm->ilm_filter = NULL;
17024 				new_ilm->ilm_refcnt = count;
17025 				new_ilm->ilm_timer = INFINITY;
17026 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17027 				new_ilm->ilm_is_new = B_TRUE;
17028 				/*
17029 				 * If the to_ill has not joined this
17030 				 * group we need to tell the driver in
17031 				 * ill_send_multicast_reqs.
17032 				 */
17033 				if (ilm_lookup_ill_v6(to_ill,
17034 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17035 					new_ilm->ilm_notify_driver = B_TRUE;
17036 
17037 				new_ilm->ilm_ill = to_ill;
17038 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17039 				    (char *), "ilm", (void *), new_ilm);
17040 				to_ill->ill_ilm_cnt++;
17041 
17042 				/* Add to the to_ill's list */
17043 				new_ilm->ilm_next = to_ill->ill_ilm;
17044 				to_ill->ill_ilm = new_ilm;
17045 				ASSERT(new_ilm->ilm_ipif == NULL);
17046 			}
17047 			if (ilm->ilm_refcnt == 0) {
17048 				goto bottom;
17049 			} else {
17050 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17051 				CLEAR_SLIST(new_ilm->ilm_filter);
17052 				ilmp = &ilm->ilm_next;
17053 			}
17054 			continue;
17055 		} else {
17056 			/*
17057 			 * ifindex = 0 means, move everything pointing at
17058 			 * from_ill. We are doing this becuase ill has
17059 			 * either FAILED or became INACTIVE.
17060 			 *
17061 			 * As we would like to move things later back to
17062 			 * from_ill, we want to retain the identity of this
17063 			 * ilm. Thus, we don't blindly increment the reference
17064 			 * count on the ilms matching the address alone. We
17065 			 * need to match on the ilm_orig_index also. new_ilm
17066 			 * was obtained by matching ilm_orig_index also.
17067 			 */
17068 			if (new_ilm != NULL) {
17069 				/*
17070 				 * This is possible only if a previous restore
17071 				 * was incomplete i.e restore to
17072 				 * ilm_orig_ifindex left some ilms because
17073 				 * of some failures. Thus when we are failing
17074 				 * again, we might find our old friends there.
17075 				 */
17076 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17077 				    " on %s, group address %s\n",
17078 				    to_ill->ill_name,
17079 				    inet_ntop(AF_INET6,
17080 				    &ilm->ilm_v6addr, buf,
17081 				    sizeof (buf))));
17082 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17083 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17084 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17085 					new_ilm->ilm_is_new = B_TRUE;
17086 				}
17087 			} else {
17088 				if (from_ill->ill_ilm_walker_cnt != 0) {
17089 					new_ilm = (ilm_t *)
17090 					    mi_zalloc(sizeof (ilm_t));
17091 					if (new_ilm == NULL) {
17092 						ip0dbg(("ilm_move_v6: "
17093 						    "FAILOVER of IPv6"
17094 						    " multicast address %s : "
17095 						    "from %s to"
17096 						    " %s failed : ENOMEM \n",
17097 						    inet_ntop(AF_INET6,
17098 						    &ilm->ilm_v6addr, buf,
17099 						    sizeof (buf)),
17100 						    from_ill->ill_name,
17101 						    to_ill->ill_name));
17102 
17103 							ilmp = &ilm->ilm_next;
17104 							continue;
17105 					}
17106 					*new_ilm = *ilm;
17107 					new_ilm->ilm_filter = NULL;
17108 				} else {
17109 					*ilmp = ilm->ilm_next;
17110 					DTRACE_PROBE3(ill__decr__cnt,
17111 					    (ill_t *), from_ill,
17112 					    (char *), "ilm", (void *), ilm);
17113 					ASSERT(from_ill->ill_ilm_cnt > 0);
17114 					from_ill->ill_ilm_cnt--;
17115 
17116 					new_ilm = ilm;
17117 				}
17118 				/*
17119 				 * If the to_ill has not joined this
17120 				 * group we need to tell the driver in
17121 				 * ill_send_multicast_reqs.
17122 				 */
17123 				if (ilm_lookup_ill_v6(to_ill,
17124 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17125 					new_ilm->ilm_notify_driver = B_TRUE;
17126 
17127 				/* Add to the to_ill's list */
17128 				new_ilm->ilm_next = to_ill->ill_ilm;
17129 				to_ill->ill_ilm = new_ilm;
17130 				ASSERT(ilm->ilm_ipif == NULL);
17131 				new_ilm->ilm_ill = to_ill;
17132 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17133 				    (char *), "ilm", (void *), new_ilm);
17134 				to_ill->ill_ilm_cnt++;
17135 				new_ilm->ilm_is_new = B_TRUE;
17136 			}
17137 
17138 		}
17139 
17140 bottom:
17141 		/*
17142 		 * Revert multicast filter state to (EXCLUDE, NULL).
17143 		 * new_ilm->ilm_is_new should already be set if needed.
17144 		 */
17145 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17146 		CLEAR_SLIST(new_ilm->ilm_filter);
17147 		/*
17148 		 * We allocated/got a new ilm, free the old one.
17149 		 */
17150 		if (new_ilm != ilm) {
17151 			if (from_ill->ill_ilm_walker_cnt == 0) {
17152 				*ilmp = ilm->ilm_next;
17153 
17154 				ASSERT(ilm->ilm_ipif == NULL); /* ipv6 */
17155 				DTRACE_PROBE3(ill__decr__cnt, (ill_t *),
17156 				    from_ill, (char *), "ilm", (void *), ilm);
17157 				ASSERT(from_ill->ill_ilm_cnt > 0);
17158 				from_ill->ill_ilm_cnt--;
17159 
17160 				ilm_inactive(ilm); /* frees this ilm */
17161 
17162 			} else {
17163 				ilm->ilm_flags |= ILM_DELETED;
17164 				from_ill->ill_ilm_cleanup_reqd = 1;
17165 				ilmp = &ilm->ilm_next;
17166 			}
17167 		}
17168 	}
17169 }
17170 
17171 /*
17172  * Move all the multicast memberships to to_ill. Called when
17173  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17174  * different from IPv6 counterpart as multicast memberships are associated
17175  * with ills in IPv6. This function is called after every ipif is moved
17176  * unlike IPv6, where it is moved only once.
17177  */
17178 static void
17179 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17180 {
17181 	ilm_t	*ilm;
17182 	ilm_t	*ilm_next;
17183 	ilm_t	*new_ilm;
17184 	ilm_t	**ilmp;
17185 	ip_stack_t	*ipst = from_ill->ill_ipst;
17186 
17187 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17188 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17189 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17190 
17191 	ilmp = &from_ill->ill_ilm;
17192 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17193 		ilm_next = ilm->ilm_next;
17194 
17195 		if (ilm->ilm_flags & ILM_DELETED) {
17196 			ilmp = &ilm->ilm_next;
17197 			continue;
17198 		}
17199 
17200 		ASSERT(ilm->ilm_ipif != NULL);
17201 
17202 		if (ilm->ilm_ipif != ipif) {
17203 			ilmp = &ilm->ilm_next;
17204 			continue;
17205 		}
17206 
17207 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17208 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17209 			new_ilm = ilm_lookup_ipif(ipif,
17210 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17211 			if (new_ilm != NULL) {
17212 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17213 				/*
17214 				 * We still need to deal with the from_ill.
17215 				 */
17216 				new_ilm->ilm_is_new = B_TRUE;
17217 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17218 				CLEAR_SLIST(new_ilm->ilm_filter);
17219 				ASSERT(ilm->ilm_ipif == ipif);
17220 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17221 				if (from_ill->ill_ilm_walker_cnt == 0) {
17222 					DTRACE_PROBE3(ill__decr__cnt,
17223 					    (ill_t *), from_ill,
17224 					    (char *), "ilm", (void *), ilm);
17225 					ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17226 				}
17227 				goto delete_ilm;
17228 			}
17229 			/*
17230 			 * If we could not find one e.g. ipif is
17231 			 * still down on to_ill, we add this ilm
17232 			 * on ill_new to preserve the reference
17233 			 * count.
17234 			 */
17235 		}
17236 		/*
17237 		 * When ipifs move, ilms always move with it
17238 		 * to the NEW ill. Thus we should never be
17239 		 * able to find ilm till we really move it here.
17240 		 */
17241 		ASSERT(ilm_lookup_ipif(ipif,
17242 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17243 
17244 		if (from_ill->ill_ilm_walker_cnt != 0) {
17245 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17246 			if (new_ilm == NULL) {
17247 				char buf[INET6_ADDRSTRLEN];
17248 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17249 				    " multicast address %s : "
17250 				    "from %s to"
17251 				    " %s failed : ENOMEM \n",
17252 				    inet_ntop(AF_INET,
17253 				    &ilm->ilm_v6addr, buf,
17254 				    sizeof (buf)),
17255 				    from_ill->ill_name,
17256 				    to_ill->ill_name));
17257 
17258 				ilmp = &ilm->ilm_next;
17259 				continue;
17260 			}
17261 			*new_ilm = *ilm;
17262 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
17263 			    (char *), "ilm", (void *), ilm);
17264 			new_ilm->ilm_ipif->ipif_ilm_cnt++;
17265 			/* We don't want new_ilm linked to ilm's filter list */
17266 			new_ilm->ilm_filter = NULL;
17267 		} else {
17268 			/* Remove from the list */
17269 			*ilmp = ilm->ilm_next;
17270 			new_ilm = ilm;
17271 		}
17272 
17273 		/*
17274 		 * If we have never joined this group on the to_ill
17275 		 * make sure we tell the driver.
17276 		 */
17277 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17278 		    ALL_ZONES) == NULL)
17279 			new_ilm->ilm_notify_driver = B_TRUE;
17280 
17281 		/* Add to the to_ill's list */
17282 		new_ilm->ilm_next = to_ill->ill_ilm;
17283 		to_ill->ill_ilm = new_ilm;
17284 		new_ilm->ilm_is_new = B_TRUE;
17285 
17286 		/*
17287 		 * Revert multicast filter state to (EXCLUDE, NULL)
17288 		 */
17289 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17290 		CLEAR_SLIST(new_ilm->ilm_filter);
17291 
17292 		/*
17293 		 * Delete only if we have allocated a new ilm.
17294 		 */
17295 		if (new_ilm != ilm) {
17296 delete_ilm:
17297 			if (from_ill->ill_ilm_walker_cnt == 0) {
17298 				/* Remove from the list */
17299 				*ilmp = ilm->ilm_next;
17300 				ilm->ilm_next = NULL;
17301 				DTRACE_PROBE3(ipif__decr__cnt,
17302 				    (ipif_t *), ilm->ilm_ipif,
17303 				    (char *), "ilm", (void *), ilm);
17304 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17305 				ilm->ilm_ipif->ipif_ilm_cnt--;
17306 				ilm_inactive(ilm);
17307 			} else {
17308 				ilm->ilm_flags |= ILM_DELETED;
17309 				from_ill->ill_ilm_cleanup_reqd = 1;
17310 				ilmp = &ilm->ilm_next;
17311 			}
17312 		}
17313 	}
17314 }
17315 
17316 static uint_t
17317 ipif_get_id(ill_t *ill, uint_t id)
17318 {
17319 	uint_t	unit;
17320 	ipif_t	*tipif;
17321 	boolean_t found = B_FALSE;
17322 	ip_stack_t	*ipst = ill->ill_ipst;
17323 
17324 	/*
17325 	 * During failback, we want to go back to the same id
17326 	 * instead of the smallest id so that the original
17327 	 * configuration is maintained. id is non-zero in that
17328 	 * case.
17329 	 */
17330 	if (id != 0) {
17331 		/*
17332 		 * While failing back, if we still have an ipif with
17333 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17334 		 * as soon as we return from this function. It was
17335 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17336 		 * we can choose the smallest id. Thus we return zero
17337 		 * in that case ignoring the hint.
17338 		 */
17339 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17340 			return (0);
17341 		for (tipif = ill->ill_ipif; tipif != NULL;
17342 		    tipif = tipif->ipif_next) {
17343 			if (tipif->ipif_id == id) {
17344 				found = B_TRUE;
17345 				break;
17346 			}
17347 		}
17348 		/*
17349 		 * If somebody already plumbed another logical
17350 		 * with the same id, we won't be able to find it.
17351 		 */
17352 		if (!found)
17353 			return (id);
17354 	}
17355 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17356 		found = B_FALSE;
17357 		for (tipif = ill->ill_ipif; tipif != NULL;
17358 		    tipif = tipif->ipif_next) {
17359 			if (tipif->ipif_id == unit) {
17360 				found = B_TRUE;
17361 				break;
17362 			}
17363 		}
17364 		if (!found)
17365 			break;
17366 	}
17367 	return (unit);
17368 }
17369 
17370 /* ARGSUSED */
17371 static int
17372 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17373     ipif_t **rep_ipif_ptr)
17374 {
17375 	ill_t	*from_ill;
17376 	ipif_t	*rep_ipif;
17377 	uint_t	unit;
17378 	int err = 0;
17379 	ipif_t	*to_ipif;
17380 	struct iocblk	*iocp;
17381 	boolean_t failback_cmd;
17382 	boolean_t remove_ipif;
17383 	int	rc;
17384 	ip_stack_t	*ipst;
17385 
17386 	ASSERT(IAM_WRITER_ILL(to_ill));
17387 	ASSERT(IAM_WRITER_IPIF(ipif));
17388 
17389 	iocp = (struct iocblk *)mp->b_rptr;
17390 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17391 	remove_ipif = B_FALSE;
17392 
17393 	from_ill = ipif->ipif_ill;
17394 	ipst = from_ill->ill_ipst;
17395 
17396 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17397 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17398 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17399 
17400 	/*
17401 	 * Don't move LINK LOCAL addresses as they are tied to
17402 	 * physical interface.
17403 	 */
17404 	if (from_ill->ill_isv6 &&
17405 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17406 		ipif->ipif_was_up = B_FALSE;
17407 		IPIF_UNMARK_MOVING(ipif);
17408 		return (0);
17409 	}
17410 
17411 	/*
17412 	 * We set the ipif_id to maximum so that the search for
17413 	 * ipif_id will pick the lowest number i.e 0 in the
17414 	 * following 2 cases :
17415 	 *
17416 	 * 1) We have a replacement ipif at the head of to_ill.
17417 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17418 	 *    on to_ill and hence the MOVE might fail. We want to
17419 	 *    remove it only if we could move the ipif. Thus, by
17420 	 *    setting it to the MAX value, we make the search in
17421 	 *    ipif_get_id return the zeroth id.
17422 	 *
17423 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17424 	 *    we might just have a zero address plumbed on the ipif
17425 	 *    with zero id in the case of IPv4. We remove that while
17426 	 *    doing the failback. We want to remove it only if we
17427 	 *    could move the ipif. Thus, by setting it to the MAX
17428 	 *    value, we make the search in ipif_get_id return the
17429 	 *    zeroth id.
17430 	 *
17431 	 * Both (1) and (2) are done only when when we are moving
17432 	 * an ipif (either due to failover/failback) which originally
17433 	 * belonged to this interface i.e the ipif_orig_ifindex is
17434 	 * the same as to_ill's ifindex. This is needed so that
17435 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17436 	 * from B -> A (B is being removed from the group) and
17437 	 * FAILBACK from A -> B restores the original configuration.
17438 	 * Without the check for orig_ifindex, the second FAILOVER
17439 	 * could make the ipif belonging to B replace the A's zeroth
17440 	 * ipif and the subsequent failback re-creating the replacement
17441 	 * ipif again.
17442 	 *
17443 	 * NOTE : We created the replacement ipif when we did a
17444 	 * FAILOVER (See below). We could check for FAILBACK and
17445 	 * then look for replacement ipif to be removed. But we don't
17446 	 * want to do that because we wan't to allow the possibility
17447 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17448 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17449 	 * from B -> A.
17450 	 */
17451 	to_ipif = to_ill->ill_ipif;
17452 	if ((to_ill->ill_phyint->phyint_ifindex ==
17453 	    ipif->ipif_orig_ifindex) &&
17454 	    to_ipif->ipif_replace_zero) {
17455 		ASSERT(to_ipif->ipif_id == 0);
17456 		remove_ipif = B_TRUE;
17457 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17458 	}
17459 	/*
17460 	 * Find the lowest logical unit number on the to_ill.
17461 	 * If we are failing back, try to get the original id
17462 	 * rather than the lowest one so that the original
17463 	 * configuration is maintained.
17464 	 *
17465 	 * XXX need a better scheme for this.
17466 	 */
17467 	if (failback_cmd) {
17468 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17469 	} else {
17470 		unit = ipif_get_id(to_ill, 0);
17471 	}
17472 
17473 	/* Reset back to zero in case we fail below */
17474 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17475 		to_ipif->ipif_id = 0;
17476 
17477 	if (unit == ipst->ips_ip_addrs_per_if) {
17478 		ipif->ipif_was_up = B_FALSE;
17479 		IPIF_UNMARK_MOVING(ipif);
17480 		return (EINVAL);
17481 	}
17482 
17483 	/*
17484 	 * ipif is ready to move from "from_ill" to "to_ill".
17485 	 *
17486 	 * 1) If we are moving ipif with id zero, create a
17487 	 *    replacement ipif for this ipif on from_ill. If this fails
17488 	 *    fail the MOVE operation.
17489 	 *
17490 	 * 2) Remove the replacement ipif on to_ill if any.
17491 	 *    We could remove the replacement ipif when we are moving
17492 	 *    the ipif with id zero. But what if somebody already
17493 	 *    unplumbed it ? Thus we always remove it if it is present.
17494 	 *    We want to do it only if we are sure we are going to
17495 	 *    move the ipif to to_ill which is why there are no
17496 	 *    returns due to error till ipif is linked to to_ill.
17497 	 *    Note that the first ipif that we failback will always
17498 	 *    be zero if it is present.
17499 	 */
17500 	if (ipif->ipif_id == 0) {
17501 		ipaddr_t inaddr_any = INADDR_ANY;
17502 
17503 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17504 		if (rep_ipif == NULL) {
17505 			ipif->ipif_was_up = B_FALSE;
17506 			IPIF_UNMARK_MOVING(ipif);
17507 			return (ENOMEM);
17508 		}
17509 		*rep_ipif = ipif_zero;
17510 		/*
17511 		 * Before we put the ipif on the list, store the addresses
17512 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17513 		 * assumes so. This logic is not any different from what
17514 		 * ipif_allocate does.
17515 		 */
17516 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17517 		    &rep_ipif->ipif_v6lcl_addr);
17518 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17519 		    &rep_ipif->ipif_v6src_addr);
17520 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17521 		    &rep_ipif->ipif_v6subnet);
17522 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17523 		    &rep_ipif->ipif_v6net_mask);
17524 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17525 		    &rep_ipif->ipif_v6brd_addr);
17526 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17527 		    &rep_ipif->ipif_v6pp_dst_addr);
17528 		/*
17529 		 * We mark IPIF_NOFAILOVER so that this can never
17530 		 * move.
17531 		 */
17532 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17533 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17534 		rep_ipif->ipif_replace_zero = B_TRUE;
17535 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17536 		    MUTEX_DEFAULT, NULL);
17537 		rep_ipif->ipif_id = 0;
17538 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17539 		rep_ipif->ipif_ill = from_ill;
17540 		rep_ipif->ipif_orig_ifindex =
17541 		    from_ill->ill_phyint->phyint_ifindex;
17542 		/* Insert at head */
17543 		rep_ipif->ipif_next = from_ill->ill_ipif;
17544 		from_ill->ill_ipif = rep_ipif;
17545 		/*
17546 		 * We don't really care to let apps know about
17547 		 * this interface.
17548 		 */
17549 	}
17550 
17551 	if (remove_ipif) {
17552 		/*
17553 		 * We set to a max value above for this case to get
17554 		 * id zero. ASSERT that we did get one.
17555 		 */
17556 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17557 		rep_ipif = to_ipif;
17558 		to_ill->ill_ipif = rep_ipif->ipif_next;
17559 		rep_ipif->ipif_next = NULL;
17560 		/*
17561 		 * If some apps scanned and find this interface,
17562 		 * it is time to let them know, so that they can
17563 		 * delete it.
17564 		 */
17565 
17566 		*rep_ipif_ptr = rep_ipif;
17567 	}
17568 
17569 	/* Get it out of the ILL interface list. */
17570 	ipif_remove(ipif, B_FALSE);
17571 
17572 	/* Assign the new ill */
17573 	ipif->ipif_ill = to_ill;
17574 	ipif->ipif_id = unit;
17575 	/* id has already been checked */
17576 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17577 	ASSERT(rc == 0);
17578 	/* Let SCTP update its list */
17579 	sctp_move_ipif(ipif, from_ill, to_ill);
17580 	/*
17581 	 * Handle the failover and failback of ipif_t between
17582 	 * ill_t that have differing maximum mtu values.
17583 	 */
17584 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17585 		if (ipif->ipif_saved_mtu == 0) {
17586 			/*
17587 			 * As this ipif_t is moving to an ill_t
17588 			 * that has a lower ill_max_mtu, its
17589 			 * ipif_mtu needs to be saved so it can
17590 			 * be restored during failback or during
17591 			 * failover to an ill_t which has a
17592 			 * higher ill_max_mtu.
17593 			 */
17594 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17595 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17596 		} else {
17597 			/*
17598 			 * The ipif_t is, once again, moving to
17599 			 * an ill_t that has a lower maximum mtu
17600 			 * value.
17601 			 */
17602 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17603 		}
17604 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17605 	    ipif->ipif_saved_mtu != 0) {
17606 		/*
17607 		 * The mtu of this ipif_t had to be reduced
17608 		 * during an earlier failover; this is an
17609 		 * opportunity for it to be increased (either as
17610 		 * part of another failover or a failback).
17611 		 */
17612 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17613 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17614 			ipif->ipif_saved_mtu = 0;
17615 		} else {
17616 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17617 		}
17618 	}
17619 
17620 	/*
17621 	 * We preserve all the other fields of the ipif including
17622 	 * ipif_saved_ire_mp. The routes that are saved here will
17623 	 * be recreated on the new interface and back on the old
17624 	 * interface when we move back.
17625 	 */
17626 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17627 
17628 	return (err);
17629 }
17630 
17631 static int
17632 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17633     int ifindex, ipif_t **rep_ipif_ptr)
17634 {
17635 	ipif_t *mipif;
17636 	ipif_t *ipif_next;
17637 	int err;
17638 
17639 	/*
17640 	 * We don't really try to MOVE back things if some of the
17641 	 * operations fail. The daemon will take care of moving again
17642 	 * later on.
17643 	 */
17644 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17645 		ipif_next = mipif->ipif_next;
17646 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17647 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17648 
17649 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17650 
17651 			/*
17652 			 * When the MOVE fails, it is the job of the
17653 			 * application to take care of this properly
17654 			 * i.e try again if it is ENOMEM.
17655 			 */
17656 			if (mipif->ipif_ill != from_ill) {
17657 				/*
17658 				 * ipif has moved.
17659 				 *
17660 				 * Move the multicast memberships associated
17661 				 * with this ipif to the new ill. For IPv6, we
17662 				 * do it once after all the ipifs are moved
17663 				 * (in ill_move) as they are not associated
17664 				 * with ipifs.
17665 				 *
17666 				 * We need to move the ilms as the ipif has
17667 				 * already been moved to a new ill even
17668 				 * in the case of errors. Neither
17669 				 * ilm_free(ipif) will find the ilm
17670 				 * when somebody unplumbs this ipif nor
17671 				 * ilm_delete(ilm) will be able to find the
17672 				 * ilm, if we don't move now.
17673 				 */
17674 				if (!from_ill->ill_isv6)
17675 					ilm_move_v4(from_ill, to_ill, mipif);
17676 			}
17677 
17678 			if (err != 0)
17679 				return (err);
17680 		}
17681 	}
17682 	return (0);
17683 }
17684 
17685 static int
17686 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17687 {
17688 	int ifindex;
17689 	int err;
17690 	struct iocblk	*iocp;
17691 	ipif_t	*ipif;
17692 	ipif_t *rep_ipif_ptr = NULL;
17693 	ipif_t	*from_ipif = NULL;
17694 	boolean_t check_rep_if = B_FALSE;
17695 	ip_stack_t	*ipst = from_ill->ill_ipst;
17696 
17697 	iocp = (struct iocblk *)mp->b_rptr;
17698 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17699 		/*
17700 		 * Move everything pointing at from_ill to to_ill.
17701 		 * We acheive this by passing in 0 as ifindex.
17702 		 */
17703 		ifindex = 0;
17704 	} else {
17705 		/*
17706 		 * Move everything pointing at from_ill whose original
17707 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17708 		 * We acheive this by passing in ifindex rather than 0.
17709 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17710 		 */
17711 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17712 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17713 	}
17714 
17715 	/*
17716 	 * Determine if there is at least one ipif that would move from
17717 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17718 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17719 	 * the move, in which case we need to quiesce the replacement ipif also.
17720 	 */
17721 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17722 	    from_ipif = from_ipif->ipif_next) {
17723 		if (((ifindex == 0) ||
17724 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17725 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17726 			check_rep_if = B_TRUE;
17727 			break;
17728 		}
17729 	}
17730 
17731 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17732 
17733 	GRAB_ILL_LOCKS(from_ill, to_ill);
17734 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17735 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17736 		    mp, ILL_MOVE_OK);
17737 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17738 		return (EINPROGRESS);
17739 	}
17740 
17741 	/* Check if the replacement ipif is quiescent to delete */
17742 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17743 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17744 		to_ill->ill_ipif->ipif_state_flags |=
17745 		    IPIF_MOVING | IPIF_CHANGING;
17746 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17747 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17748 			    mp, ILL_MOVE_OK);
17749 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17750 			return (EINPROGRESS);
17751 		}
17752 	}
17753 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17754 
17755 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17756 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17757 	GRAB_ILL_LOCKS(from_ill, to_ill);
17758 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17759 
17760 	/* ilm_move is done inside ipif_move for IPv4 */
17761 	if (err == 0 && from_ill->ill_isv6)
17762 		ilm_move_v6(from_ill, to_ill, ifindex);
17763 
17764 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17765 	rw_exit(&ipst->ips_ill_g_lock);
17766 
17767 	/*
17768 	 * send rts messages and multicast messages.
17769 	 */
17770 	if (rep_ipif_ptr != NULL) {
17771 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17772 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17773 			rep_ipif_ptr->ipif_recovery_id = 0;
17774 		}
17775 		ip_rts_ifmsg(rep_ipif_ptr);
17776 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17777 #ifdef DEBUG
17778 		ipif_trace_cleanup(rep_ipif_ptr);
17779 #endif
17780 		mi_free(rep_ipif_ptr);
17781 	}
17782 
17783 	conn_move_ill(from_ill, to_ill, ifindex);
17784 
17785 	return (err);
17786 }
17787 
17788 /*
17789  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17790  * Also checks for the validity of the arguments.
17791  * Note: We are already exclusive inside the from group.
17792  * It is upto the caller to release refcnt on the to_ill's.
17793  */
17794 static int
17795 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17796     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17797 {
17798 	int dst_index;
17799 	ipif_t *ipif_v4, *ipif_v6;
17800 	struct lifreq *lifr;
17801 	mblk_t *mp1;
17802 	boolean_t exists;
17803 	sin_t	*sin;
17804 	int	err = 0;
17805 	ip_stack_t	*ipst;
17806 
17807 	if (CONN_Q(q))
17808 		ipst = CONNQ_TO_IPST(q);
17809 	else
17810 		ipst = ILLQ_TO_IPST(q);
17811 
17812 	if ((mp1 = mp->b_cont) == NULL)
17813 		return (EPROTO);
17814 
17815 	if ((mp1 = mp1->b_cont) == NULL)
17816 		return (EPROTO);
17817 
17818 	lifr = (struct lifreq *)mp1->b_rptr;
17819 	sin = (sin_t *)&lifr->lifr_addr;
17820 
17821 	/*
17822 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17823 	 * specific operations.
17824 	 */
17825 	if (sin->sin_family != AF_UNSPEC)
17826 		return (EINVAL);
17827 
17828 	/*
17829 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17830 	 * NULLs for the last 4 args and we know the lookup won't fail
17831 	 * with EINPROGRESS.
17832 	 */
17833 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17834 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17835 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17836 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17837 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17838 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17839 
17840 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17841 		return (ENXIO);
17842 
17843 	if (ipif_v4 != NULL) {
17844 		ASSERT(ipif_v4->ipif_refcnt != 0);
17845 		if (ipif_v4->ipif_id != 0) {
17846 			err = EINVAL;
17847 			goto done;
17848 		}
17849 
17850 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17851 		*ill_from_v4 = ipif_v4->ipif_ill;
17852 	}
17853 
17854 	if (ipif_v6 != NULL) {
17855 		ASSERT(ipif_v6->ipif_refcnt != 0);
17856 		if (ipif_v6->ipif_id != 0) {
17857 			err = EINVAL;
17858 			goto done;
17859 		}
17860 
17861 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17862 		*ill_from_v6 = ipif_v6->ipif_ill;
17863 	}
17864 
17865 	err = 0;
17866 	dst_index = lifr->lifr_movetoindex;
17867 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17868 	    q, mp, ip_process_ioctl, &err, ipst);
17869 	if (err != 0) {
17870 		/*
17871 		 * A move may be in progress, EINPROGRESS looking up the "to"
17872 		 * ill means changes already done to the "from" ipsq need to
17873 		 * be undone to avoid potential deadlocks.
17874 		 *
17875 		 * ENXIO will usually be because there is only v6 on the ill,
17876 		 * that's not treated as an error unless an ENXIO is also
17877 		 * seen when looking up the v6 "to" ill.
17878 		 *
17879 		 * If EINPROGRESS, the mp has been enqueued and can not be
17880 		 * used to look up the v6 "to" ill, but a preemptive clean
17881 		 * up of changes to the v6 "from" ipsq is done.
17882 		 */
17883 		if (err == EINPROGRESS) {
17884 			if (*ill_from_v4 != NULL) {
17885 				ill_t   *from_ill;
17886 				ipsq_t  *from_ipsq;
17887 
17888 				from_ill = ipif_v4->ipif_ill;
17889 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17890 
17891 				mutex_enter(&from_ipsq->ipsq_lock);
17892 				from_ipsq->ipsq_current_ipif = NULL;
17893 				mutex_exit(&from_ipsq->ipsq_lock);
17894 			}
17895 			if (*ill_from_v6 != NULL) {
17896 				ill_t   *from_ill;
17897 				ipsq_t  *from_ipsq;
17898 
17899 				from_ill = ipif_v6->ipif_ill;
17900 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17901 
17902 				mutex_enter(&from_ipsq->ipsq_lock);
17903 				from_ipsq->ipsq_current_ipif = NULL;
17904 				mutex_exit(&from_ipsq->ipsq_lock);
17905 			}
17906 			goto done;
17907 		}
17908 		ASSERT(err == ENXIO);
17909 		err = 0;
17910 	}
17911 
17912 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17913 	    q, mp, ip_process_ioctl, &err, ipst);
17914 	if (err != 0) {
17915 		/*
17916 		 * A move may be in progress, EINPROGRESS looking up the "to"
17917 		 * ill means changes already done to the "from" ipsq need to
17918 		 * be undone to avoid potential deadlocks.
17919 		 */
17920 		if (err == EINPROGRESS) {
17921 			if (*ill_from_v6 != NULL) {
17922 				ill_t   *from_ill;
17923 				ipsq_t  *from_ipsq;
17924 
17925 				from_ill = ipif_v6->ipif_ill;
17926 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17927 
17928 				mutex_enter(&from_ipsq->ipsq_lock);
17929 				from_ipsq->ipsq_current_ipif = NULL;
17930 				mutex_exit(&from_ipsq->ipsq_lock);
17931 			}
17932 			goto done;
17933 		}
17934 		ASSERT(err == ENXIO);
17935 
17936 		/* Both v4 and v6 lookup failed */
17937 		if (*ill_to_v4 == NULL) {
17938 			err = ENXIO;
17939 			goto done;
17940 		}
17941 		err = 0;
17942 	}
17943 
17944 	/*
17945 	 * If we have something to MOVE i.e "from" not NULL,
17946 	 * "to" should be non-NULL.
17947 	 */
17948 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17949 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17950 		err = EINVAL;
17951 	}
17952 
17953 done:
17954 	if (ipif_v4 != NULL)
17955 		ipif_refrele(ipif_v4);
17956 	if (ipif_v6 != NULL)
17957 		ipif_refrele(ipif_v6);
17958 	return (err);
17959 }
17960 
17961 /*
17962  * FAILOVER and FAILBACK are modelled as MOVE operations.
17963  *
17964  * We don't check whether the MOVE is within the same group or
17965  * not, because this ioctl can be used as a generic mechanism
17966  * to failover from interface A to B, though things will function
17967  * only if they are really part of the same group. Moreover,
17968  * all ipifs may be down and hence temporarily out of the group.
17969  *
17970  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17971  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17972  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17973  * have been deleted and there are no active references. Once quiescent the
17974  * ipif's are moved and brought up on the new ill.
17975  *
17976  * Normally the source ill and destination ill belong to the same IPMP group
17977  * and hence the same ipsq_t. In the event they don't belong to the same
17978  * same group the two ipsq's are first merged into one ipsq - that of the
17979  * to_ill. The multicast memberships on the source and destination ill cannot
17980  * change during the move operation since multicast joins/leaves also have to
17981  * execute on the same ipsq and are hence serialized.
17982  */
17983 /* ARGSUSED */
17984 int
17985 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17986     ip_ioctl_cmd_t *ipip, void *ifreq)
17987 {
17988 	ill_t *ill_to_v4 = NULL;
17989 	ill_t *ill_to_v6 = NULL;
17990 	ill_t *ill_from_v4 = NULL;
17991 	ill_t *ill_from_v6 = NULL;
17992 	int err = 0;
17993 
17994 	/*
17995 	 * setup from and to ill's, we can get EINPROGRESS only for
17996 	 * to_ill's.
17997 	 */
17998 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17999 	    &ill_to_v4, &ill_to_v6);
18000 
18001 	if (err != 0) {
18002 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18003 		goto done;
18004 	}
18005 
18006 	/*
18007 	 * nothing to do.
18008 	 */
18009 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18010 		goto done;
18011 	}
18012 
18013 	/*
18014 	 * nothing to do.
18015 	 */
18016 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18017 		goto done;
18018 	}
18019 
18020 	/*
18021 	 * Mark the ill as changing.
18022 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18023 	 * in ill_up_ipifs in case of error they are cleared below.
18024 	 */
18025 
18026 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18027 	if (ill_from_v4 != NULL)
18028 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18029 	if (ill_from_v6 != NULL)
18030 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18031 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18032 
18033 	/*
18034 	 * Make sure that both src and dst are
18035 	 * in the same syncq group. If not make it happen.
18036 	 * We are not holding any locks because we are the writer
18037 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18038 	 * to protect to_ipsq against changing.
18039 	 */
18040 	if (ill_from_v4 != NULL) {
18041 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18042 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18043 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18044 			    NULL, mp, q);
18045 			goto err_ret;
18046 
18047 		}
18048 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18049 	} else {
18050 
18051 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18052 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18053 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18054 			    NULL, mp, q);
18055 			goto err_ret;
18056 
18057 		}
18058 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18059 	}
18060 
18061 	/*
18062 	 * Now that the ipsq's have been merged and we are the writer
18063 	 * lets mark to_ill as changing as well.
18064 	 */
18065 
18066 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18067 	if (ill_to_v4 != NULL)
18068 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18069 	if (ill_to_v6 != NULL)
18070 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18071 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18072 
18073 	/*
18074 	 * Its ok for us to proceed with the move even if
18075 	 * ill_pending_mp is non null on one of the from ill's as the reply
18076 	 * should not be looking at the ipif, it should only care about the
18077 	 * ill itself.
18078 	 */
18079 
18080 	/*
18081 	 * lets move ipv4 first.
18082 	 */
18083 	if (ill_from_v4 != NULL) {
18084 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18085 		ill_from_v4->ill_move_in_progress = B_TRUE;
18086 		ill_to_v4->ill_move_in_progress = B_TRUE;
18087 		ill_to_v4->ill_move_peer = ill_from_v4;
18088 		ill_from_v4->ill_move_peer = ill_to_v4;
18089 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18090 	}
18091 
18092 	/*
18093 	 * Now lets move ipv6.
18094 	 */
18095 	if (err == 0 && ill_from_v6 != NULL) {
18096 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18097 		ill_from_v6->ill_move_in_progress = B_TRUE;
18098 		ill_to_v6->ill_move_in_progress = B_TRUE;
18099 		ill_to_v6->ill_move_peer = ill_from_v6;
18100 		ill_from_v6->ill_move_peer = ill_to_v6;
18101 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18102 	}
18103 
18104 err_ret:
18105 	/*
18106 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18107 	 * moved to become quiescent.
18108 	 */
18109 	if (err == EINPROGRESS) {
18110 		goto done;
18111 	}
18112 
18113 	/*
18114 	 * if err is set ill_up_ipifs will not be called
18115 	 * lets clear the flags.
18116 	 */
18117 
18118 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18119 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18120 	/*
18121 	 * Some of the clearing may be redundant. But it is simple
18122 	 * not making any extra checks.
18123 	 */
18124 	if (ill_from_v6 != NULL) {
18125 		ill_from_v6->ill_move_in_progress = B_FALSE;
18126 		ill_from_v6->ill_move_peer = NULL;
18127 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18128 	}
18129 	if (ill_from_v4 != NULL) {
18130 		ill_from_v4->ill_move_in_progress = B_FALSE;
18131 		ill_from_v4->ill_move_peer = NULL;
18132 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18133 	}
18134 	if (ill_to_v6 != NULL) {
18135 		ill_to_v6->ill_move_in_progress = B_FALSE;
18136 		ill_to_v6->ill_move_peer = NULL;
18137 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18138 	}
18139 	if (ill_to_v4 != NULL) {
18140 		ill_to_v4->ill_move_in_progress = B_FALSE;
18141 		ill_to_v4->ill_move_peer = NULL;
18142 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18143 	}
18144 
18145 	/*
18146 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18147 	 * Do this always to maintain proper state i.e even in case of errors.
18148 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18149 	 * we need not call on both v4 and v6 interfaces.
18150 	 */
18151 	if (ill_from_v4 != NULL) {
18152 		if ((ill_from_v4->ill_phyint->phyint_flags &
18153 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18154 			phyint_inactive(ill_from_v4->ill_phyint);
18155 		}
18156 	} else if (ill_from_v6 != NULL) {
18157 		if ((ill_from_v6->ill_phyint->phyint_flags &
18158 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18159 			phyint_inactive(ill_from_v6->ill_phyint);
18160 		}
18161 	}
18162 
18163 	if (ill_to_v4 != NULL) {
18164 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18165 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18166 		}
18167 	} else if (ill_to_v6 != NULL) {
18168 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18169 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18170 		}
18171 	}
18172 
18173 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18174 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18175 
18176 no_err:
18177 	/*
18178 	 * lets bring the interfaces up on the to_ill.
18179 	 */
18180 	if (err == 0) {
18181 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18182 		    q, mp);
18183 	}
18184 
18185 	if (err == 0) {
18186 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18187 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18188 
18189 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18190 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18191 	}
18192 done:
18193 
18194 	if (ill_to_v4 != NULL) {
18195 		ill_refrele(ill_to_v4);
18196 	}
18197 	if (ill_to_v6 != NULL) {
18198 		ill_refrele(ill_to_v6);
18199 	}
18200 
18201 	return (err);
18202 }
18203 
18204 static void
18205 ill_dl_down(ill_t *ill)
18206 {
18207 	/*
18208 	 * The ill is down; unbind but stay attached since we're still
18209 	 * associated with a PPA. If we have negotiated DLPI capabilites
18210 	 * with the data link service provider (IDS_OK) then reset them.
18211 	 * The interval between unbinding and rebinding is potentially
18212 	 * unbounded hence we cannot assume things will be the same.
18213 	 * The DLPI capabilities will be probed again when the data link
18214 	 * is brought up.
18215 	 */
18216 	mblk_t	*mp = ill->ill_unbind_mp;
18217 
18218 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18219 
18220 	ill->ill_unbind_mp = NULL;
18221 	if (mp != NULL) {
18222 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18223 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18224 		    ill->ill_name));
18225 		mutex_enter(&ill->ill_lock);
18226 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18227 		mutex_exit(&ill->ill_lock);
18228 		/*
18229 		 * Reset the capabilities if the negotiation is done or is
18230 		 * still in progress. Note that ill_capability_reset() will
18231 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18232 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18233 		 *
18234 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18235 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18236 		 * the capabilities renegotiation from happening.
18237 		 */
18238 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18239 			ill_capability_reset(ill);
18240 		ill->ill_capab_reneg = B_FALSE;
18241 
18242 		ill_dlpi_send(ill, mp);
18243 	}
18244 
18245 	/*
18246 	 * Toss all of our multicast memberships.  We could keep them, but
18247 	 * then we'd have to do bookkeeping of any joins and leaves performed
18248 	 * by the application while the the interface is down (we can't just
18249 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18250 	 * on a downed interface).
18251 	 */
18252 	ill_leave_multicast(ill);
18253 
18254 	mutex_enter(&ill->ill_lock);
18255 	ill->ill_dl_up = 0;
18256 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18257 	mutex_exit(&ill->ill_lock);
18258 }
18259 
18260 static void
18261 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18262 {
18263 	union DL_primitives *dlp;
18264 	t_uscalar_t prim;
18265 
18266 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18267 
18268 	dlp = (union DL_primitives *)mp->b_rptr;
18269 	prim = dlp->dl_primitive;
18270 
18271 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18272 	    dl_primstr(prim), prim, ill->ill_name));
18273 
18274 	switch (prim) {
18275 	case DL_PHYS_ADDR_REQ:
18276 	{
18277 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18278 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18279 		break;
18280 	}
18281 	case DL_BIND_REQ:
18282 		mutex_enter(&ill->ill_lock);
18283 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18284 		mutex_exit(&ill->ill_lock);
18285 		break;
18286 	}
18287 
18288 	/*
18289 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18290 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18291 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18292 	 */
18293 	mutex_enter(&ill->ill_lock);
18294 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18295 	    (prim == DL_UNBIND_REQ)) {
18296 		ill->ill_dlpi_pending = prim;
18297 	}
18298 	mutex_exit(&ill->ill_lock);
18299 
18300 	putnext(ill->ill_wq, mp);
18301 }
18302 
18303 /*
18304  * Helper function for ill_dlpi_send().
18305  */
18306 /* ARGSUSED */
18307 static void
18308 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18309 {
18310 	ill_dlpi_send(q->q_ptr, mp);
18311 }
18312 
18313 /*
18314  * Send a DLPI control message to the driver but make sure there
18315  * is only one outstanding message. Uses ill_dlpi_pending to tell
18316  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18317  * when an ACK or a NAK is received to process the next queued message.
18318  */
18319 void
18320 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18321 {
18322 	mblk_t **mpp;
18323 
18324 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18325 
18326 	/*
18327 	 * To ensure that any DLPI requests for current exclusive operation
18328 	 * are always completely sent before any DLPI messages for other
18329 	 * operations, require writer access before enqueuing.
18330 	 */
18331 	if (!IAM_WRITER_ILL(ill)) {
18332 		ill_refhold(ill);
18333 		/* qwriter_ip() does the ill_refrele() */
18334 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18335 		    NEW_OP, B_TRUE);
18336 		return;
18337 	}
18338 
18339 	mutex_enter(&ill->ill_lock);
18340 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18341 		/* Must queue message. Tail insertion */
18342 		mpp = &ill->ill_dlpi_deferred;
18343 		while (*mpp != NULL)
18344 			mpp = &((*mpp)->b_next);
18345 
18346 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18347 		    ill->ill_name));
18348 
18349 		*mpp = mp;
18350 		mutex_exit(&ill->ill_lock);
18351 		return;
18352 	}
18353 	mutex_exit(&ill->ill_lock);
18354 	ill_dlpi_dispatch(ill, mp);
18355 }
18356 
18357 /*
18358  * Send all deferred DLPI messages without waiting for their ACKs.
18359  */
18360 void
18361 ill_dlpi_send_deferred(ill_t *ill)
18362 {
18363 	mblk_t *mp, *nextmp;
18364 
18365 	/*
18366 	 * Clear ill_dlpi_pending so that the message is not queued in
18367 	 * ill_dlpi_send().
18368 	 */
18369 	mutex_enter(&ill->ill_lock);
18370 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18371 	mp = ill->ill_dlpi_deferred;
18372 	ill->ill_dlpi_deferred = NULL;
18373 	mutex_exit(&ill->ill_lock);
18374 
18375 	for (; mp != NULL; mp = nextmp) {
18376 		nextmp = mp->b_next;
18377 		mp->b_next = NULL;
18378 		ill_dlpi_send(ill, mp);
18379 	}
18380 }
18381 
18382 /*
18383  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18384  */
18385 boolean_t
18386 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18387 {
18388 	t_uscalar_t pending;
18389 
18390 	mutex_enter(&ill->ill_lock);
18391 	if (ill->ill_dlpi_pending == prim) {
18392 		mutex_exit(&ill->ill_lock);
18393 		return (B_TRUE);
18394 	}
18395 
18396 	/*
18397 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18398 	 * without waiting, so don't print any warnings in that case.
18399 	 */
18400 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18401 		mutex_exit(&ill->ill_lock);
18402 		return (B_FALSE);
18403 	}
18404 	pending = ill->ill_dlpi_pending;
18405 	mutex_exit(&ill->ill_lock);
18406 
18407 	if (pending == DL_PRIM_INVAL) {
18408 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18409 		    "received unsolicited ack for %s on %s\n",
18410 		    dl_primstr(prim), ill->ill_name);
18411 	} else {
18412 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18413 		    "received unexpected ack for %s on %s (expecting %s)\n",
18414 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18415 	}
18416 	return (B_FALSE);
18417 }
18418 
18419 /*
18420  * Complete the current DLPI operation associated with `prim' on `ill' and
18421  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18422  * operations and the ill's current exclusive IPSQ operation has finished
18423  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18424  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18425  * the comments above ipsq_current_finish() for details.
18426  */
18427 void
18428 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18429 {
18430 	mblk_t *mp;
18431 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18432 
18433 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18434 	mutex_enter(&ill->ill_lock);
18435 
18436 	ASSERT(prim != DL_PRIM_INVAL);
18437 	ASSERT(ill->ill_dlpi_pending == prim);
18438 
18439 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18440 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18441 
18442 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18443 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18444 
18445 		mutex_enter(&ipsq->ipsq_lock);
18446 		if (ipsq->ipsq_current_done)
18447 			ipsq->ipsq_current_ipif = NULL;
18448 		mutex_exit(&ipsq->ipsq_lock);
18449 
18450 		cv_signal(&ill->ill_cv);
18451 		mutex_exit(&ill->ill_lock);
18452 		return;
18453 	}
18454 
18455 	ill->ill_dlpi_deferred = mp->b_next;
18456 	mp->b_next = NULL;
18457 	mutex_exit(&ill->ill_lock);
18458 
18459 	ill_dlpi_dispatch(ill, mp);
18460 }
18461 
18462 void
18463 conn_delete_ire(conn_t *connp, caddr_t arg)
18464 {
18465 	ipif_t	*ipif = (ipif_t *)arg;
18466 	ire_t	*ire;
18467 
18468 	/*
18469 	 * Look at the cached ires on conns which has pointers to ipifs.
18470 	 * We just call ire_refrele which clears up the reference
18471 	 * to ire. Called when a conn closes. Also called from ipif_free
18472 	 * to cleanup indirect references to the stale ipif via the cached ire.
18473 	 */
18474 	mutex_enter(&connp->conn_lock);
18475 	ire = connp->conn_ire_cache;
18476 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18477 		connp->conn_ire_cache = NULL;
18478 		mutex_exit(&connp->conn_lock);
18479 		IRE_REFRELE_NOTR(ire);
18480 		return;
18481 	}
18482 	mutex_exit(&connp->conn_lock);
18483 
18484 }
18485 
18486 /*
18487  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18488  * of IREs. Those IREs may have been previously cached in the conn structure.
18489  * This ipcl_walk() walker function releases all references to such IREs based
18490  * on the condemned flag.
18491  */
18492 /* ARGSUSED */
18493 void
18494 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18495 {
18496 	ire_t	*ire;
18497 
18498 	mutex_enter(&connp->conn_lock);
18499 	ire = connp->conn_ire_cache;
18500 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18501 		connp->conn_ire_cache = NULL;
18502 		mutex_exit(&connp->conn_lock);
18503 		IRE_REFRELE_NOTR(ire);
18504 		return;
18505 	}
18506 	mutex_exit(&connp->conn_lock);
18507 }
18508 
18509 /*
18510  * Take down a specific interface, but don't lose any information about it.
18511  * Also delete interface from its interface group (ifgrp).
18512  * (Always called as writer.)
18513  * This function goes through the down sequence even if the interface is
18514  * already down. There are 2 reasons.
18515  * a. Currently we permit interface routes that depend on down interfaces
18516  *    to be added. This behaviour itself is questionable. However it appears
18517  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18518  *    time. We go thru the cleanup in order to remove these routes.
18519  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18520  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18521  *    down, but we need to cleanup i.e. do ill_dl_down and
18522  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18523  *
18524  * IP-MT notes:
18525  *
18526  * Model of reference to interfaces.
18527  *
18528  * The following members in ipif_t track references to the ipif.
18529  *	int     ipif_refcnt;    Active reference count
18530  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18531  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18532  *
18533  * The following members in ill_t track references to the ill.
18534  *	int             ill_refcnt;     active refcnt
18535  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18536  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18537  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18538  *
18539  * Reference to an ipif or ill can be obtained in any of the following ways.
18540  *
18541  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18542  * Pointers to ipif / ill from other data structures viz ire and conn.
18543  * Implicit reference to the ipif / ill by holding a reference to the ire.
18544  *
18545  * The ipif/ill lookup functions return a reference held ipif / ill.
18546  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18547  * This is a purely dynamic reference count associated with threads holding
18548  * references to the ipif / ill. Pointers from other structures do not
18549  * count towards this reference count.
18550  *
18551  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18552  * associated with the ipif/ill. This is incremented whenever a new
18553  * ire is created referencing the ipif/ill. This is done atomically inside
18554  * ire_add_v[46] where the ire is actually added to the ire hash table.
18555  * The count is decremented in ire_inactive where the ire is destroyed.
18556  *
18557  * nce's reference ill's thru nce_ill and the count of nce's associated with
18558  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18559  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18560  * table. Similarly it is decremented in ndp_inactive() where the nce
18561  * is destroyed.
18562  *
18563  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18564  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18565  * in ilm_walker_cleanup() or ilm_delete().
18566  *
18567  * Flow of ioctls involving interface down/up
18568  *
18569  * The following is the sequence of an attempt to set some critical flags on an
18570  * up interface.
18571  * ip_sioctl_flags
18572  * ipif_down
18573  * wait for ipif to be quiescent
18574  * ipif_down_tail
18575  * ip_sioctl_flags_tail
18576  *
18577  * All set ioctls that involve down/up sequence would have a skeleton similar
18578  * to the above. All the *tail functions are called after the refcounts have
18579  * dropped to the appropriate values.
18580  *
18581  * The mechanism to quiesce an ipif is as follows.
18582  *
18583  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18584  * on the ipif. Callers either pass a flag requesting wait or the lookup
18585  *  functions will return NULL.
18586  *
18587  * Delete all ires referencing this ipif
18588  *
18589  * Any thread attempting to do an ipif_refhold on an ipif that has been
18590  * obtained thru a cached pointer will first make sure that
18591  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18592  * increment the refcount.
18593  *
18594  * The above guarantees that the ipif refcount will eventually come down to
18595  * zero and the ipif will quiesce, once all threads that currently hold a
18596  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18597  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18598  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18599  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18600  * in ip.h
18601  *
18602  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18603  *
18604  * Threads trying to lookup an ipif or ill can pass a flag requesting
18605  * wait and restart if the ipif / ill cannot be looked up currently.
18606  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18607  * failure if the ipif is currently undergoing an exclusive operation, and
18608  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18609  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18610  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18611  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18612  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18613  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18614  * until we release the ipsq_lock, even though the the ill/ipif state flags
18615  * can change after we drop the ill_lock.
18616  *
18617  * An attempt to send out a packet using an ipif that is currently
18618  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18619  * operation and restart it later when the exclusive condition on the ipif ends.
18620  * This is an example of not passing the wait flag to the lookup functions. For
18621  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18622  * out a multicast packet on that ipif will fail while the ipif is
18623  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18624  * currently IPIF_CHANGING will also fail.
18625  */
18626 int
18627 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18628 {
18629 	ill_t		*ill = ipif->ipif_ill;
18630 	phyint_t	*phyi;
18631 	conn_t		*connp;
18632 	boolean_t	success;
18633 	boolean_t	ipif_was_up = B_FALSE;
18634 	ip_stack_t	*ipst = ill->ill_ipst;
18635 
18636 	ASSERT(IAM_WRITER_IPIF(ipif));
18637 
18638 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18639 
18640 	if (ipif->ipif_flags & IPIF_UP) {
18641 		mutex_enter(&ill->ill_lock);
18642 		ipif->ipif_flags &= ~IPIF_UP;
18643 		ASSERT(ill->ill_ipif_up_count > 0);
18644 		--ill->ill_ipif_up_count;
18645 		mutex_exit(&ill->ill_lock);
18646 		ipif_was_up = B_TRUE;
18647 		/* Update status in SCTP's list */
18648 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18649 	}
18650 
18651 	/*
18652 	 * Blow away memberships we established in ipif_multicast_up().
18653 	 */
18654 	ipif_multicast_down(ipif);
18655 
18656 	/*
18657 	 * Remove from the mapping for __sin6_src_id. We insert only
18658 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18659 	 * stored as mapped addresses, we need to check for mapped
18660 	 * INADDR_ANY also.
18661 	 */
18662 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18663 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18664 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18665 		int err;
18666 
18667 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18668 		    ipif->ipif_zoneid, ipst);
18669 		if (err != 0) {
18670 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18671 		}
18672 	}
18673 
18674 	/*
18675 	 * Before we delete the ill from the group (if any), we need
18676 	 * to make sure that we delete all the routes dependent on
18677 	 * this and also any ipifs dependent on this ipif for
18678 	 * source address. We need to do before we delete from
18679 	 * the group because
18680 	 *
18681 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18682 	 *
18683 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18684 	 *    for re-doing source address selection. Note that
18685 	 *    ipif_select_source[_v6] called from
18686 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18687 	 *    because we have already marked down here i.e cleared
18688 	 *    IPIF_UP.
18689 	 */
18690 	if (ipif->ipif_isv6) {
18691 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18692 		    ipst);
18693 	} else {
18694 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18695 		    ipst);
18696 	}
18697 
18698 	/*
18699 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18700 	 * ires have been deleted above. Otherwise a thread could end up
18701 	 * caching an ire in a conn after we have finished the cleanup of the
18702 	 * conn. The caching is done after making sure that the ire is not yet
18703 	 * condemned. Also documented in the block comment above ip_output
18704 	 */
18705 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18706 	/* Also, delete the ires cached in SCTP */
18707 	sctp_ire_cache_flush(ipif);
18708 
18709 	/*
18710 	 * Update any other ipifs which have used "our" local address as
18711 	 * a source address. This entails removing and recreating IRE_INTERFACE
18712 	 * entries for such ipifs.
18713 	 */
18714 	if (ipif->ipif_isv6)
18715 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18716 	else
18717 		ipif_update_other_ipifs(ipif, ill->ill_group);
18718 
18719 	if (ipif_was_up) {
18720 		/*
18721 		 * Check whether it is last ipif to leave this group.
18722 		 * If this is the last ipif to leave, we should remove
18723 		 * this ill from the group as ipif_select_source will not
18724 		 * be able to find any useful ipifs if this ill is selected
18725 		 * for load balancing.
18726 		 *
18727 		 * For nameless groups, we should call ifgrp_delete if this
18728 		 * belongs to some group. As this ipif is going down, we may
18729 		 * need to reconstruct groups.
18730 		 */
18731 		phyi = ill->ill_phyint;
18732 		/*
18733 		 * If the phyint_groupname_len is 0, it may or may not
18734 		 * be in the nameless group. If the phyint_groupname_len is
18735 		 * not 0, then this ill should be part of some group.
18736 		 * As we always insert this ill in the group if
18737 		 * phyint_groupname_len is not zero when the first ipif
18738 		 * comes up (in ipif_up_done), it should be in a group
18739 		 * when the namelen is not 0.
18740 		 *
18741 		 * NOTE : When we delete the ill from the group,it will
18742 		 * blow away all the IRE_CACHES pointing either at this ipif or
18743 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18744 		 * should be pointing at this ill.
18745 		 */
18746 		ASSERT(phyi->phyint_groupname_len == 0 ||
18747 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18748 
18749 		if (phyi->phyint_groupname_len != 0) {
18750 			if (ill->ill_ipif_up_count == 0)
18751 				illgrp_delete(ill);
18752 		}
18753 
18754 		/*
18755 		 * If we have deleted some of the broadcast ires associated
18756 		 * with this ipif, we need to re-nominate somebody else if
18757 		 * the ires that we deleted were the nominated ones.
18758 		 */
18759 		if (ill->ill_group != NULL && !ill->ill_isv6)
18760 			ipif_renominate_bcast(ipif);
18761 	}
18762 
18763 	/*
18764 	 * neighbor-discovery or arp entries for this interface.
18765 	 */
18766 	ipif_ndp_down(ipif);
18767 
18768 	/*
18769 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18770 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18771 	 * and ill_delete -> ipif_free -> ipif_down
18772 	 */
18773 	if (mp == NULL) {
18774 		ASSERT(q == NULL);
18775 		return (0);
18776 	}
18777 
18778 	if (CONN_Q(q)) {
18779 		connp = Q_TO_CONN(q);
18780 		mutex_enter(&connp->conn_lock);
18781 	} else {
18782 		connp = NULL;
18783 	}
18784 	mutex_enter(&ill->ill_lock);
18785 	/*
18786 	 * Are there any ire's pointing to this ipif that are still active ?
18787 	 * If this is the last ipif going down, are there any ire's pointing
18788 	 * to this ill that are still active ?
18789 	 */
18790 	if (ipif_is_quiescent(ipif)) {
18791 		mutex_exit(&ill->ill_lock);
18792 		if (connp != NULL)
18793 			mutex_exit(&connp->conn_lock);
18794 		return (0);
18795 	}
18796 
18797 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18798 	    ill->ill_name, (void *)ill));
18799 	/*
18800 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18801 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18802 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18803 	 */
18804 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18805 	if (!success) {
18806 		/* The conn is closing. So just return */
18807 		ASSERT(connp != NULL);
18808 		mutex_exit(&ill->ill_lock);
18809 		mutex_exit(&connp->conn_lock);
18810 		return (EINTR);
18811 	}
18812 
18813 	mutex_exit(&ill->ill_lock);
18814 	if (connp != NULL)
18815 		mutex_exit(&connp->conn_lock);
18816 	return (EINPROGRESS);
18817 }
18818 
18819 void
18820 ipif_down_tail(ipif_t *ipif)
18821 {
18822 	ill_t	*ill = ipif->ipif_ill;
18823 
18824 	/*
18825 	 * Skip any loopback interface (null wq).
18826 	 * If this is the last logical interface on the ill
18827 	 * have ill_dl_down tell the driver we are gone (unbind)
18828 	 * Note that lun 0 can ipif_down even though
18829 	 * there are other logical units that are up.
18830 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18831 	 */
18832 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18833 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18834 	    ill->ill_dl_up) {
18835 		ill_dl_down(ill);
18836 	}
18837 	ill->ill_logical_down = 0;
18838 
18839 	/*
18840 	 * Have to be after removing the routes in ipif_down_delete_ire.
18841 	 */
18842 	if (ipif->ipif_isv6) {
18843 		if (ill->ill_flags & ILLF_XRESOLV)
18844 			ipif_arp_down(ipif);
18845 	} else {
18846 		ipif_arp_down(ipif);
18847 	}
18848 
18849 	ip_rts_ifmsg(ipif);
18850 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18851 }
18852 
18853 /*
18854  * Bring interface logically down without bringing the physical interface
18855  * down e.g. when the netmask is changed. This avoids long lasting link
18856  * negotiations between an ethernet interface and a certain switches.
18857  */
18858 static int
18859 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18860 {
18861 	/*
18862 	 * The ill_logical_down flag is a transient flag. It is set here
18863 	 * and is cleared once the down has completed in ipif_down_tail.
18864 	 * This flag does not indicate whether the ill stream is in the
18865 	 * DL_BOUND state with the driver. Instead this flag is used by
18866 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18867 	 * the driver. The state of the ill stream i.e. whether it is
18868 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18869 	 */
18870 	ipif->ipif_ill->ill_logical_down = 1;
18871 	return (ipif_down(ipif, q, mp));
18872 }
18873 
18874 /*
18875  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18876  * If the usesrc client ILL is already part of a usesrc group or not,
18877  * in either case a ire_stq with the matching usesrc client ILL will
18878  * locate the IRE's that need to be deleted. We want IREs to be created
18879  * with the new source address.
18880  */
18881 static void
18882 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18883 {
18884 	ill_t	*ucill = (ill_t *)ill_arg;
18885 
18886 	ASSERT(IAM_WRITER_ILL(ucill));
18887 
18888 	if (ire->ire_stq == NULL)
18889 		return;
18890 
18891 	if ((ire->ire_type == IRE_CACHE) &&
18892 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18893 		ire_delete(ire);
18894 }
18895 
18896 /*
18897  * ire_walk routine to delete every IRE dependent on the interface
18898  * address that is going down.	(Always called as writer.)
18899  * Works for both v4 and v6.
18900  * In addition for checking for ire_ipif matches it also checks for
18901  * IRE_CACHE entries which have the same source address as the
18902  * disappearing ipif since ipif_select_source might have picked
18903  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18904  * care of any IRE_INTERFACE with the disappearing source address.
18905  */
18906 static void
18907 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18908 {
18909 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18910 	ill_t *ire_ill;
18911 	ill_t *ipif_ill;
18912 
18913 	ASSERT(IAM_WRITER_IPIF(ipif));
18914 	if (ire->ire_ipif == NULL)
18915 		return;
18916 
18917 	/*
18918 	 * For IPv4, we derive source addresses for an IRE from ipif's
18919 	 * belonging to the same IPMP group as the IRE's outgoing
18920 	 * interface.  If an IRE's outgoing interface isn't in the
18921 	 * same IPMP group as a particular ipif, then that ipif
18922 	 * couldn't have been used as a source address for this IRE.
18923 	 *
18924 	 * For IPv6, source addresses are only restricted to the IPMP group
18925 	 * if the IRE is for a link-local address or a multicast address.
18926 	 * Otherwise, source addresses for an IRE can be chosen from
18927 	 * interfaces other than the the outgoing interface for that IRE.
18928 	 *
18929 	 * For source address selection details, see ipif_select_source()
18930 	 * and ipif_select_source_v6().
18931 	 */
18932 	if (ire->ire_ipversion == IPV4_VERSION ||
18933 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18934 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18935 		ire_ill = ire->ire_ipif->ipif_ill;
18936 		ipif_ill = ipif->ipif_ill;
18937 
18938 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18939 			return;
18940 		}
18941 	}
18942 
18943 	if (ire->ire_ipif != ipif) {
18944 		/*
18945 		 * Look for a matching source address.
18946 		 */
18947 		if (ire->ire_type != IRE_CACHE)
18948 			return;
18949 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18950 			return;
18951 
18952 		if (ire->ire_ipversion == IPV4_VERSION) {
18953 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18954 				return;
18955 		} else {
18956 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18957 			    &ipif->ipif_v6lcl_addr))
18958 				return;
18959 		}
18960 		ire_delete(ire);
18961 		return;
18962 	}
18963 	/*
18964 	 * ire_delete() will do an ire_flush_cache which will delete
18965 	 * all ire_ipif matches
18966 	 */
18967 	ire_delete(ire);
18968 }
18969 
18970 /*
18971  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18972  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18973  * 2) when an interface is brought up or down (on that ill).
18974  * This ensures that the IRE_CACHE entries don't retain stale source
18975  * address selection results.
18976  */
18977 void
18978 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18979 {
18980 	ill_t	*ill = (ill_t *)ill_arg;
18981 	ill_t	*ipif_ill;
18982 
18983 	ASSERT(IAM_WRITER_ILL(ill));
18984 	/*
18985 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18986 	 * Hence this should be IRE_CACHE.
18987 	 */
18988 	ASSERT(ire->ire_type == IRE_CACHE);
18989 
18990 	/*
18991 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18992 	 * We are only interested in IRE_CACHES that has borrowed
18993 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18994 	 * for which we need to look at ire_ipif->ipif_ill match
18995 	 * with ill.
18996 	 */
18997 	ASSERT(ire->ire_ipif != NULL);
18998 	ipif_ill = ire->ire_ipif->ipif_ill;
18999 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19000 	    ipif_ill->ill_group == ill->ill_group)) {
19001 		ire_delete(ire);
19002 	}
19003 }
19004 
19005 /*
19006  * Delete all the ire whose stq references ill_arg.
19007  */
19008 static void
19009 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19010 {
19011 	ill_t	*ill = (ill_t *)ill_arg;
19012 	ill_t	*ire_ill;
19013 
19014 	ASSERT(IAM_WRITER_ILL(ill));
19015 	/*
19016 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19017 	 * Hence this should be IRE_CACHE.
19018 	 */
19019 	ASSERT(ire->ire_type == IRE_CACHE);
19020 
19021 	/*
19022 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19023 	 * matches ill. We are only interested in IRE_CACHES that
19024 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19025 	 * filtering here.
19026 	 */
19027 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19028 
19029 	if (ire_ill == ill)
19030 		ire_delete(ire);
19031 }
19032 
19033 /*
19034  * This is called when an ill leaves the group. We want to delete
19035  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19036  * pointing at ill.
19037  */
19038 static void
19039 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19040 {
19041 	ill_t	*ill = (ill_t *)ill_arg;
19042 
19043 	ASSERT(IAM_WRITER_ILL(ill));
19044 	ASSERT(ill->ill_group == NULL);
19045 	/*
19046 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19047 	 * Hence this should be IRE_CACHE.
19048 	 */
19049 	ASSERT(ire->ire_type == IRE_CACHE);
19050 	/*
19051 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19052 	 * matches ill. We are interested in both.
19053 	 */
19054 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19055 	    (ire->ire_ipif->ipif_ill == ill));
19056 
19057 	ire_delete(ire);
19058 }
19059 
19060 /*
19061  * Initiate deallocate of an IPIF. Always called as writer. Called by
19062  * ill_delete or ip_sioctl_removeif.
19063  */
19064 static void
19065 ipif_free(ipif_t *ipif)
19066 {
19067 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19068 
19069 	ASSERT(IAM_WRITER_IPIF(ipif));
19070 
19071 	if (ipif->ipif_recovery_id != 0)
19072 		(void) untimeout(ipif->ipif_recovery_id);
19073 	ipif->ipif_recovery_id = 0;
19074 
19075 	/* Remove conn references */
19076 	reset_conn_ipif(ipif);
19077 
19078 	/*
19079 	 * Make sure we have valid net and subnet broadcast ire's for the
19080 	 * other ipif's which share them with this ipif.
19081 	 */
19082 	if (!ipif->ipif_isv6)
19083 		ipif_check_bcast_ires(ipif);
19084 
19085 	/*
19086 	 * Take down the interface. We can be called either from ill_delete
19087 	 * or from ip_sioctl_removeif.
19088 	 */
19089 	(void) ipif_down(ipif, NULL, NULL);
19090 
19091 	/*
19092 	 * Now that the interface is down, there's no chance it can still
19093 	 * become a duplicate.  Cancel any timer that may have been set while
19094 	 * tearing down.
19095 	 */
19096 	if (ipif->ipif_recovery_id != 0)
19097 		(void) untimeout(ipif->ipif_recovery_id);
19098 	ipif->ipif_recovery_id = 0;
19099 
19100 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19101 	/* Remove pointers to this ill in the multicast routing tables */
19102 	reset_mrt_vif_ipif(ipif);
19103 	rw_exit(&ipst->ips_ill_g_lock);
19104 }
19105 
19106 /*
19107  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19108  * also ill_move().
19109  */
19110 static void
19111 ipif_free_tail(ipif_t *ipif)
19112 {
19113 	mblk_t	*mp;
19114 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19115 
19116 	/*
19117 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19118 	 */
19119 	mutex_enter(&ipif->ipif_saved_ire_lock);
19120 	mp = ipif->ipif_saved_ire_mp;
19121 	ipif->ipif_saved_ire_mp = NULL;
19122 	mutex_exit(&ipif->ipif_saved_ire_lock);
19123 	freemsg(mp);
19124 
19125 	/*
19126 	 * Need to hold both ill_g_lock and ill_lock while
19127 	 * inserting or removing an ipif from the linked list
19128 	 * of ipifs hanging off the ill.
19129 	 */
19130 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19131 
19132 	ASSERT(ilm_walk_ipif(ipif) == 0);
19133 
19134 #ifdef DEBUG
19135 	ipif_trace_cleanup(ipif);
19136 #endif
19137 
19138 	/* Ask SCTP to take it out of it list */
19139 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19140 
19141 	/* Get it out of the ILL interface list. */
19142 	ipif_remove(ipif, B_TRUE);
19143 	rw_exit(&ipst->ips_ill_g_lock);
19144 
19145 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19146 
19147 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19148 	ASSERT(ipif->ipif_recovery_id == 0);
19149 
19150 	/* Free the memory. */
19151 	mi_free(ipif);
19152 }
19153 
19154 /*
19155  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19156  * is zero.
19157  */
19158 void
19159 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19160 {
19161 	char	lbuf[LIFNAMSIZ];
19162 	char	*name;
19163 	size_t	name_len;
19164 
19165 	buf[0] = '\0';
19166 	name = ipif->ipif_ill->ill_name;
19167 	name_len = ipif->ipif_ill->ill_name_length;
19168 	if (ipif->ipif_id != 0) {
19169 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19170 		    ipif->ipif_id);
19171 		name = lbuf;
19172 		name_len = mi_strlen(name) + 1;
19173 	}
19174 	len -= 1;
19175 	buf[len] = '\0';
19176 	len = MIN(len, name_len);
19177 	bcopy(name, buf, len);
19178 }
19179 
19180 /*
19181  * Find an IPIF based on the name passed in.  Names can be of the
19182  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19183  * The <phys> string can have forms like <dev><#> (e.g., le0),
19184  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19185  * When there is no colon, the implied unit id is zero. <phys> must
19186  * correspond to the name of an ILL.  (May be called as writer.)
19187  */
19188 static ipif_t *
19189 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19190     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19191     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19192 {
19193 	char	*cp;
19194 	char	*endp;
19195 	long	id;
19196 	ill_t	*ill;
19197 	ipif_t	*ipif;
19198 	uint_t	ire_type;
19199 	boolean_t did_alloc = B_FALSE;
19200 	ipsq_t	*ipsq;
19201 
19202 	if (error != NULL)
19203 		*error = 0;
19204 
19205 	/*
19206 	 * If the caller wants to us to create the ipif, make sure we have a
19207 	 * valid zoneid
19208 	 */
19209 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19210 
19211 	if (namelen == 0) {
19212 		if (error != NULL)
19213 			*error = ENXIO;
19214 		return (NULL);
19215 	}
19216 
19217 	*exists = B_FALSE;
19218 	/* Look for a colon in the name. */
19219 	endp = &name[namelen];
19220 	for (cp = endp; --cp > name; ) {
19221 		if (*cp == IPIF_SEPARATOR_CHAR)
19222 			break;
19223 	}
19224 
19225 	if (*cp == IPIF_SEPARATOR_CHAR) {
19226 		/*
19227 		 * Reject any non-decimal aliases for logical
19228 		 * interfaces. Aliases with leading zeroes
19229 		 * are also rejected as they introduce ambiguity
19230 		 * in the naming of the interfaces.
19231 		 * In order to confirm with existing semantics,
19232 		 * and to not break any programs/script relying
19233 		 * on that behaviour, if<0>:0 is considered to be
19234 		 * a valid interface.
19235 		 *
19236 		 * If alias has two or more digits and the first
19237 		 * is zero, fail.
19238 		 */
19239 		if (&cp[2] < endp && cp[1] == '0') {
19240 			if (error != NULL)
19241 				*error = EINVAL;
19242 			return (NULL);
19243 		}
19244 	}
19245 
19246 	if (cp <= name) {
19247 		cp = endp;
19248 	} else {
19249 		*cp = '\0';
19250 	}
19251 
19252 	/*
19253 	 * Look up the ILL, based on the portion of the name
19254 	 * before the slash. ill_lookup_on_name returns a held ill.
19255 	 * Temporary to check whether ill exists already. If so
19256 	 * ill_lookup_on_name will clear it.
19257 	 */
19258 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19259 	    q, mp, func, error, &did_alloc, ipst);
19260 	if (cp != endp)
19261 		*cp = IPIF_SEPARATOR_CHAR;
19262 	if (ill == NULL)
19263 		return (NULL);
19264 
19265 	/* Establish the unit number in the name. */
19266 	id = 0;
19267 	if (cp < endp && *endp == '\0') {
19268 		/* If there was a colon, the unit number follows. */
19269 		cp++;
19270 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19271 			ill_refrele(ill);
19272 			if (error != NULL)
19273 				*error = ENXIO;
19274 			return (NULL);
19275 		}
19276 	}
19277 
19278 	GRAB_CONN_LOCK(q);
19279 	mutex_enter(&ill->ill_lock);
19280 	/* Now see if there is an IPIF with this unit number. */
19281 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19282 		if (ipif->ipif_id == id) {
19283 			if (zoneid != ALL_ZONES &&
19284 			    zoneid != ipif->ipif_zoneid &&
19285 			    ipif->ipif_zoneid != ALL_ZONES) {
19286 				mutex_exit(&ill->ill_lock);
19287 				RELEASE_CONN_LOCK(q);
19288 				ill_refrele(ill);
19289 				if (error != NULL)
19290 					*error = ENXIO;
19291 				return (NULL);
19292 			}
19293 			/*
19294 			 * The block comment at the start of ipif_down
19295 			 * explains the use of the macros used below
19296 			 */
19297 			if (IPIF_CAN_LOOKUP(ipif)) {
19298 				ipif_refhold_locked(ipif);
19299 				mutex_exit(&ill->ill_lock);
19300 				if (!did_alloc)
19301 					*exists = B_TRUE;
19302 				/*
19303 				 * Drop locks before calling ill_refrele
19304 				 * since it can potentially call into
19305 				 * ipif_ill_refrele_tail which can end up
19306 				 * in trying to acquire any lock.
19307 				 */
19308 				RELEASE_CONN_LOCK(q);
19309 				ill_refrele(ill);
19310 				return (ipif);
19311 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19312 				ipsq = ill->ill_phyint->phyint_ipsq;
19313 				mutex_enter(&ipsq->ipsq_lock);
19314 				mutex_exit(&ill->ill_lock);
19315 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19316 				mutex_exit(&ipsq->ipsq_lock);
19317 				RELEASE_CONN_LOCK(q);
19318 				ill_refrele(ill);
19319 				if (error != NULL)
19320 					*error = EINPROGRESS;
19321 				return (NULL);
19322 			}
19323 		}
19324 	}
19325 	RELEASE_CONN_LOCK(q);
19326 
19327 	if (!do_alloc) {
19328 		mutex_exit(&ill->ill_lock);
19329 		ill_refrele(ill);
19330 		if (error != NULL)
19331 			*error = ENXIO;
19332 		return (NULL);
19333 	}
19334 
19335 	/*
19336 	 * If none found, atomically allocate and return a new one.
19337 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19338 	 * to support "receive only" use of lo0:1 etc. as is still done
19339 	 * below as an initial guess.
19340 	 * However, this is now likely to be overriden later in ipif_up_done()
19341 	 * when we know for sure what address has been configured on the
19342 	 * interface, since we might have more than one loopback interface
19343 	 * with a loopback address, e.g. in the case of zones, and all the
19344 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19345 	 */
19346 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19347 		ire_type = IRE_LOOPBACK;
19348 	else
19349 		ire_type = IRE_LOCAL;
19350 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19351 	if (ipif != NULL)
19352 		ipif_refhold_locked(ipif);
19353 	else if (error != NULL)
19354 		*error = ENOMEM;
19355 	mutex_exit(&ill->ill_lock);
19356 	ill_refrele(ill);
19357 	return (ipif);
19358 }
19359 
19360 /*
19361  * This routine is called whenever a new address comes up on an ipif.  If
19362  * we are configured to respond to address mask requests, then we are supposed
19363  * to broadcast an address mask reply at this time.  This routine is also
19364  * called if we are already up, but a netmask change is made.  This is legal
19365  * but might not make the system manager very popular.	(May be called
19366  * as writer.)
19367  */
19368 void
19369 ipif_mask_reply(ipif_t *ipif)
19370 {
19371 	icmph_t	*icmph;
19372 	ipha_t	*ipha;
19373 	mblk_t	*mp;
19374 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19375 
19376 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19377 
19378 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19379 		return;
19380 
19381 	/* ICMP mask reply is IPv4 only */
19382 	ASSERT(!ipif->ipif_isv6);
19383 	/* ICMP mask reply is not for a loopback interface */
19384 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19385 
19386 	mp = allocb(REPLY_LEN, BPRI_HI);
19387 	if (mp == NULL)
19388 		return;
19389 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19390 
19391 	ipha = (ipha_t *)mp->b_rptr;
19392 	bzero(ipha, REPLY_LEN);
19393 	*ipha = icmp_ipha;
19394 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19395 	ipha->ipha_src = ipif->ipif_src_addr;
19396 	ipha->ipha_dst = ipif->ipif_brd_addr;
19397 	ipha->ipha_length = htons(REPLY_LEN);
19398 	ipha->ipha_ident = 0;
19399 
19400 	icmph = (icmph_t *)&ipha[1];
19401 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19402 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19403 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19404 
19405 	put(ipif->ipif_wq, mp);
19406 
19407 #undef	REPLY_LEN
19408 }
19409 
19410 /*
19411  * When the mtu in the ipif changes, we call this routine through ire_walk
19412  * to update all the relevant IREs.
19413  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19414  */
19415 static void
19416 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19417 {
19418 	ipif_t *ipif = (ipif_t *)ipif_arg;
19419 
19420 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19421 		return;
19422 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19423 }
19424 
19425 /*
19426  * When the mtu in the ill changes, we call this routine through ire_walk
19427  * to update all the relevant IREs.
19428  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19429  */
19430 void
19431 ill_mtu_change(ire_t *ire, char *ill_arg)
19432 {
19433 	ill_t	*ill = (ill_t *)ill_arg;
19434 
19435 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19436 		return;
19437 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19438 }
19439 
19440 /*
19441  * Join the ipif specific multicast groups.
19442  * Must be called after a mapping has been set up in the resolver.  (Always
19443  * called as writer.)
19444  */
19445 void
19446 ipif_multicast_up(ipif_t *ipif)
19447 {
19448 	int err, index;
19449 	ill_t *ill;
19450 
19451 	ASSERT(IAM_WRITER_IPIF(ipif));
19452 
19453 	ill = ipif->ipif_ill;
19454 	index = ill->ill_phyint->phyint_ifindex;
19455 
19456 	ip1dbg(("ipif_multicast_up\n"));
19457 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19458 		return;
19459 
19460 	if (ipif->ipif_isv6) {
19461 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19462 			return;
19463 
19464 		/* Join the all hosts multicast address */
19465 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19466 		/*
19467 		 * Passing B_TRUE means we have to join the multicast
19468 		 * membership on this interface even though this is
19469 		 * FAILED. If we join on a different one in the group,
19470 		 * we will not be able to delete the membership later
19471 		 * as we currently don't track where we join when we
19472 		 * join within the kernel unlike applications where
19473 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19474 		 * for more on this.
19475 		 */
19476 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19477 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19478 		if (err != 0) {
19479 			ip0dbg(("ipif_multicast_up: "
19480 			    "all_hosts_mcast failed %d\n",
19481 			    err));
19482 			return;
19483 		}
19484 		/*
19485 		 * Enable multicast for the solicited node multicast address
19486 		 */
19487 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19488 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19489 
19490 			ipv6_multi.s6_addr32[3] |=
19491 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19492 
19493 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19494 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19495 			    NULL);
19496 			if (err != 0) {
19497 				ip0dbg(("ipif_multicast_up: solicited MC"
19498 				    " failed %d\n", err));
19499 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19500 				    ill, ill->ill_phyint->phyint_ifindex,
19501 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19502 				return;
19503 			}
19504 		}
19505 	} else {
19506 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19507 			return;
19508 
19509 		/* Join the all hosts multicast address */
19510 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19511 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19512 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19513 		if (err) {
19514 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19515 			return;
19516 		}
19517 	}
19518 	ipif->ipif_multicast_up = 1;
19519 }
19520 
19521 /*
19522  * Blow away any multicast groups that we joined in ipif_multicast_up().
19523  * (Explicit memberships are blown away in ill_leave_multicast() when the
19524  * ill is brought down.)
19525  */
19526 static void
19527 ipif_multicast_down(ipif_t *ipif)
19528 {
19529 	int err;
19530 
19531 	ASSERT(IAM_WRITER_IPIF(ipif));
19532 
19533 	ip1dbg(("ipif_multicast_down\n"));
19534 	if (!ipif->ipif_multicast_up)
19535 		return;
19536 
19537 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19538 
19539 	if (!ipif->ipif_isv6) {
19540 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19541 		    B_TRUE);
19542 		if (err != 0)
19543 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19544 
19545 		ipif->ipif_multicast_up = 0;
19546 		return;
19547 	}
19548 
19549 	/*
19550 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19551 	 * we should look for ilms on this ill rather than the ones that have
19552 	 * been failed over here.  They are here temporarily. As
19553 	 * ipif_multicast_up has joined on this ill, we should delete only
19554 	 * from this ill.
19555 	 */
19556 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19557 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19558 	    B_TRUE, B_TRUE);
19559 	if (err != 0) {
19560 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19561 		    err));
19562 	}
19563 	/*
19564 	 * Disable multicast for the solicited node multicast address
19565 	 */
19566 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19567 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19568 
19569 		ipv6_multi.s6_addr32[3] |=
19570 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19571 
19572 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19573 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19574 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19575 
19576 		if (err != 0) {
19577 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19578 			    err));
19579 		}
19580 	}
19581 
19582 	ipif->ipif_multicast_up = 0;
19583 }
19584 
19585 /*
19586  * Used when an interface comes up to recreate any extra routes on this
19587  * interface.
19588  */
19589 static ire_t **
19590 ipif_recover_ire(ipif_t *ipif)
19591 {
19592 	mblk_t	*mp;
19593 	ire_t	**ipif_saved_irep;
19594 	ire_t	**irep;
19595 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19596 
19597 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19598 	    ipif->ipif_id));
19599 
19600 	mutex_enter(&ipif->ipif_saved_ire_lock);
19601 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19602 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19603 	if (ipif_saved_irep == NULL) {
19604 		mutex_exit(&ipif->ipif_saved_ire_lock);
19605 		return (NULL);
19606 	}
19607 
19608 	irep = ipif_saved_irep;
19609 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19610 		ire_t		*ire;
19611 		queue_t		*rfq;
19612 		queue_t		*stq;
19613 		ifrt_t		*ifrt;
19614 		uchar_t		*src_addr;
19615 		uchar_t		*gateway_addr;
19616 		ushort_t	type;
19617 
19618 		/*
19619 		 * When the ire was initially created and then added in
19620 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19621 		 * in the case of a traditional interface route, or as one of
19622 		 * the IRE_OFFSUBNET types (with the exception of
19623 		 * IRE_HOST types ire which is created by icmp_redirect() and
19624 		 * which we don't need to save or recover).  In the case where
19625 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19626 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19627 		 * to satisfy software like GateD and Sun Cluster which creates
19628 		 * routes using the the loopback interface's address as a
19629 		 * gateway.
19630 		 *
19631 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19632 		 * ire_create() will be called in the same way here as
19633 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19634 		 * the route looks like a traditional interface route (where
19635 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19636 		 * the saved ifrt->ifrt_type.  This means that in the case where
19637 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19638 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19639 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19640 		 */
19641 		ifrt = (ifrt_t *)mp->b_rptr;
19642 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19643 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19644 			rfq = NULL;
19645 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19646 			    ? ipif->ipif_rq : ipif->ipif_wq;
19647 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19648 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19649 			    : (uint8_t *)&ipif->ipif_src_addr;
19650 			gateway_addr = NULL;
19651 			type = ipif->ipif_net_type;
19652 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19653 			/* Recover multiroute broadcast IRE. */
19654 			rfq = ipif->ipif_rq;
19655 			stq = ipif->ipif_wq;
19656 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19657 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19658 			    : (uint8_t *)&ipif->ipif_src_addr;
19659 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19660 			type = ifrt->ifrt_type;
19661 		} else {
19662 			rfq = NULL;
19663 			stq = NULL;
19664 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19665 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19666 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19667 			type = ifrt->ifrt_type;
19668 		}
19669 
19670 		/*
19671 		 * Create a copy of the IRE with the saved address and netmask.
19672 		 */
19673 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19674 		    "0x%x/0x%x\n",
19675 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19676 		    ntohl(ifrt->ifrt_addr),
19677 		    ntohl(ifrt->ifrt_mask)));
19678 		ire = ire_create(
19679 		    (uint8_t *)&ifrt->ifrt_addr,
19680 		    (uint8_t *)&ifrt->ifrt_mask,
19681 		    src_addr,
19682 		    gateway_addr,
19683 		    &ifrt->ifrt_max_frag,
19684 		    NULL,
19685 		    rfq,
19686 		    stq,
19687 		    type,
19688 		    ipif,
19689 		    0,
19690 		    0,
19691 		    0,
19692 		    ifrt->ifrt_flags,
19693 		    &ifrt->ifrt_iulp_info,
19694 		    NULL,
19695 		    NULL,
19696 		    ipst);
19697 
19698 		if (ire == NULL) {
19699 			mutex_exit(&ipif->ipif_saved_ire_lock);
19700 			kmem_free(ipif_saved_irep,
19701 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19702 			return (NULL);
19703 		}
19704 
19705 		/*
19706 		 * Some software (for example, GateD and Sun Cluster) attempts
19707 		 * to create (what amount to) IRE_PREFIX routes with the
19708 		 * loopback address as the gateway.  This is primarily done to
19709 		 * set up prefixes with the RTF_REJECT flag set (for example,
19710 		 * when generating aggregate routes.)
19711 		 *
19712 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19713 		 * IRE_LOOPBACK, then we map the request into a
19714 		 * IRE_IF_NORESOLVER.
19715 		 */
19716 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19717 			ire->ire_type = IRE_IF_NORESOLVER;
19718 		/*
19719 		 * ire held by ire_add, will be refreled' towards the
19720 		 * the end of ipif_up_done
19721 		 */
19722 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19723 		*irep = ire;
19724 		irep++;
19725 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19726 	}
19727 	mutex_exit(&ipif->ipif_saved_ire_lock);
19728 	return (ipif_saved_irep);
19729 }
19730 
19731 /*
19732  * Used to set the netmask and broadcast address to default values when the
19733  * interface is brought up.  (Always called as writer.)
19734  */
19735 static void
19736 ipif_set_default(ipif_t *ipif)
19737 {
19738 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19739 
19740 	if (!ipif->ipif_isv6) {
19741 		/*
19742 		 * Interface holds an IPv4 address. Default
19743 		 * mask is the natural netmask.
19744 		 */
19745 		if (!ipif->ipif_net_mask) {
19746 			ipaddr_t	v4mask;
19747 
19748 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19749 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19750 		}
19751 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19752 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19753 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19754 		} else {
19755 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19756 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19757 		}
19758 		/*
19759 		 * NOTE: SunOS 4.X does this even if the broadcast address
19760 		 * has been already set thus we do the same here.
19761 		 */
19762 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19763 			ipaddr_t	v4addr;
19764 
19765 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19766 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19767 		}
19768 	} else {
19769 		/*
19770 		 * Interface holds an IPv6-only address.  Default
19771 		 * mask is all-ones.
19772 		 */
19773 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19774 			ipif->ipif_v6net_mask = ipv6_all_ones;
19775 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19776 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19777 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19778 		} else {
19779 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19780 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19781 		}
19782 	}
19783 }
19784 
19785 /*
19786  * Return 0 if this address can be used as local address without causing
19787  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19788  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19789  * Special checks are needed to allow the same IPv6 link-local address
19790  * on different ills.
19791  * TODO: allowing the same site-local address on different ill's.
19792  */
19793 int
19794 ip_addr_availability_check(ipif_t *new_ipif)
19795 {
19796 	in6_addr_t our_v6addr;
19797 	ill_t *ill;
19798 	ipif_t *ipif;
19799 	ill_walk_context_t ctx;
19800 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19801 
19802 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19803 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19804 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19805 
19806 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19807 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19808 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19809 		return (0);
19810 
19811 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19812 
19813 	if (new_ipif->ipif_isv6)
19814 		ill = ILL_START_WALK_V6(&ctx, ipst);
19815 	else
19816 		ill = ILL_START_WALK_V4(&ctx, ipst);
19817 
19818 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19819 		for (ipif = ill->ill_ipif; ipif != NULL;
19820 		    ipif = ipif->ipif_next) {
19821 			if ((ipif == new_ipif) ||
19822 			    !(ipif->ipif_flags & IPIF_UP) ||
19823 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19824 				continue;
19825 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19826 			    &our_v6addr)) {
19827 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19828 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19829 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19830 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19831 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19832 				    new_ipif->ipif_ill != ill)
19833 					continue;
19834 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19835 				    new_ipif->ipif_ill != ill)
19836 					continue;
19837 				else if (new_ipif->ipif_zoneid !=
19838 				    ipif->ipif_zoneid &&
19839 				    ipif->ipif_zoneid != ALL_ZONES &&
19840 				    IS_LOOPBACK(ill))
19841 					continue;
19842 				else if (new_ipif->ipif_ill == ill)
19843 					return (EADDRINUSE);
19844 				else
19845 					return (EADDRNOTAVAIL);
19846 			}
19847 		}
19848 	}
19849 
19850 	return (0);
19851 }
19852 
19853 /*
19854  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19855  * IREs for the ipif.
19856  * When the routine returns EINPROGRESS then mp has been consumed and
19857  * the ioctl will be acked from ip_rput_dlpi.
19858  */
19859 static int
19860 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19861 {
19862 	ill_t	*ill = ipif->ipif_ill;
19863 	boolean_t isv6 = ipif->ipif_isv6;
19864 	int	err = 0;
19865 	boolean_t success;
19866 
19867 	ASSERT(IAM_WRITER_IPIF(ipif));
19868 
19869 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19870 
19871 	/* Shouldn't get here if it is already up. */
19872 	if (ipif->ipif_flags & IPIF_UP)
19873 		return (EALREADY);
19874 
19875 	/* Skip arp/ndp for any loopback interface. */
19876 	if (ill->ill_wq != NULL) {
19877 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19878 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19879 
19880 		if (!ill->ill_dl_up) {
19881 			/*
19882 			 * ill_dl_up is not yet set. i.e. we are yet to
19883 			 * DL_BIND with the driver and this is the first
19884 			 * logical interface on the ill to become "up".
19885 			 * Tell the driver to get going (via DL_BIND_REQ).
19886 			 * Note that changing "significant" IFF_ flags
19887 			 * address/netmask etc cause a down/up dance, but
19888 			 * does not cause an unbind (DL_UNBIND) with the driver
19889 			 */
19890 			return (ill_dl_up(ill, ipif, mp, q));
19891 		}
19892 
19893 		/*
19894 		 * ipif_resolver_up may end up sending an
19895 		 * AR_INTERFACE_UP message to ARP, which would, in
19896 		 * turn send a DLPI message to the driver. ioctls are
19897 		 * serialized and so we cannot send more than one
19898 		 * interface up message at a time. If ipif_resolver_up
19899 		 * does send an interface up message to ARP, we get
19900 		 * EINPROGRESS and we will complete in ip_arp_done.
19901 		 */
19902 
19903 		ASSERT(connp != NULL || !CONN_Q(q));
19904 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19905 		if (connp != NULL)
19906 			mutex_enter(&connp->conn_lock);
19907 		mutex_enter(&ill->ill_lock);
19908 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19909 		mutex_exit(&ill->ill_lock);
19910 		if (connp != NULL)
19911 			mutex_exit(&connp->conn_lock);
19912 		if (!success)
19913 			return (EINTR);
19914 
19915 		/*
19916 		 * Crank up IPv6 neighbor discovery
19917 		 * Unlike ARP, this should complete when
19918 		 * ipif_ndp_up returns. However, for
19919 		 * ILLF_XRESOLV interfaces we also send a
19920 		 * AR_INTERFACE_UP to the external resolver.
19921 		 * That ioctl will complete in ip_rput.
19922 		 */
19923 		if (isv6) {
19924 			err = ipif_ndp_up(ipif);
19925 			if (err != 0) {
19926 				if (err != EINPROGRESS)
19927 					mp = ipsq_pending_mp_get(ipsq, &connp);
19928 				return (err);
19929 			}
19930 		}
19931 		/* Now, ARP */
19932 		err = ipif_resolver_up(ipif, Res_act_initial);
19933 		if (err == EINPROGRESS) {
19934 			/* We will complete it in ip_arp_done */
19935 			return (err);
19936 		}
19937 		mp = ipsq_pending_mp_get(ipsq, &connp);
19938 		ASSERT(mp != NULL);
19939 		if (err != 0)
19940 			return (err);
19941 	} else {
19942 		/*
19943 		 * Interfaces without underlying hardware don't do duplicate
19944 		 * address detection.
19945 		 */
19946 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19947 		ipif->ipif_addr_ready = 1;
19948 	}
19949 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19950 }
19951 
19952 /*
19953  * Perform a bind for the physical device.
19954  * When the routine returns EINPROGRESS then mp has been consumed and
19955  * the ioctl will be acked from ip_rput_dlpi.
19956  * Allocate an unbind message and save it until ipif_down.
19957  */
19958 static int
19959 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19960 {
19961 	areq_t	*areq;
19962 	mblk_t	*areq_mp = NULL;
19963 	mblk_t	*bind_mp = NULL;
19964 	mblk_t	*unbind_mp = NULL;
19965 	conn_t	*connp;
19966 	boolean_t success;
19967 	uint16_t sap_addr;
19968 
19969 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19970 	ASSERT(IAM_WRITER_ILL(ill));
19971 	ASSERT(mp != NULL);
19972 
19973 	/* Create a resolver cookie for ARP */
19974 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19975 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19976 		if (areq_mp == NULL)
19977 			return (ENOMEM);
19978 
19979 		freemsg(ill->ill_resolver_mp);
19980 		ill->ill_resolver_mp = areq_mp;
19981 		areq = (areq_t *)areq_mp->b_rptr;
19982 		sap_addr = ill->ill_sap;
19983 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19984 	}
19985 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19986 	    DL_BIND_REQ);
19987 	if (bind_mp == NULL)
19988 		goto bad;
19989 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19990 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19991 
19992 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19993 	if (unbind_mp == NULL)
19994 		goto bad;
19995 
19996 	/*
19997 	 * Record state needed to complete this operation when the
19998 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19999 	 */
20000 	ASSERT(WR(q)->q_next == NULL);
20001 	connp = Q_TO_CONN(q);
20002 
20003 	mutex_enter(&connp->conn_lock);
20004 	mutex_enter(&ipif->ipif_ill->ill_lock);
20005 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20006 	mutex_exit(&ipif->ipif_ill->ill_lock);
20007 	mutex_exit(&connp->conn_lock);
20008 	if (!success)
20009 		goto bad;
20010 
20011 	/*
20012 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20013 	 * the interface goes down.
20014 	 */
20015 	ASSERT(ill->ill_unbind_mp == NULL);
20016 	ill->ill_unbind_mp = unbind_mp;
20017 
20018 	ill_dlpi_send(ill, bind_mp);
20019 	/* Send down link-layer capabilities probe if not already done. */
20020 	ill_capability_probe(ill);
20021 
20022 	/*
20023 	 * Sysid used to rely on the fact that netboots set domainname
20024 	 * and the like. Now that miniroot boots aren't strictly netboots
20025 	 * and miniroot network configuration is driven from userland
20026 	 * these things still need to be set. This situation can be detected
20027 	 * by comparing the interface being configured here to the one
20028 	 * dhcifname was set to reference by the boot loader. Once sysid is
20029 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20030 	 */
20031 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
20032 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
20033 	    (strlen(srpc_domain) == 0)) {
20034 		if (dhcpinit() != 0)
20035 			cmn_err(CE_WARN, "no cached dhcp response");
20036 	}
20037 
20038 	/*
20039 	 * This operation will complete in ip_rput_dlpi with either
20040 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20041 	 */
20042 	return (EINPROGRESS);
20043 bad:
20044 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20045 	/*
20046 	 * We don't have to check for possible removal from illgrp
20047 	 * as we have not yet inserted in illgrp. For groups
20048 	 * without names, this ipif is still not UP and hence
20049 	 * this could not have possibly had any influence in forming
20050 	 * groups.
20051 	 */
20052 
20053 	freemsg(bind_mp);
20054 	freemsg(unbind_mp);
20055 	return (ENOMEM);
20056 }
20057 
20058 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20059 
20060 /*
20061  * DLPI and ARP is up.
20062  * Create all the IREs associated with an interface bring up multicast.
20063  * Set the interface flag and finish other initialization
20064  * that potentially had to be differed to after DL_BIND_ACK.
20065  */
20066 int
20067 ipif_up_done(ipif_t *ipif)
20068 {
20069 	ire_t	*ire_array[20];
20070 	ire_t	**irep = ire_array;
20071 	ire_t	**irep1;
20072 	ipaddr_t net_mask = 0;
20073 	ipaddr_t subnet_mask, route_mask;
20074 	ill_t	*ill = ipif->ipif_ill;
20075 	queue_t	*stq;
20076 	ipif_t	 *src_ipif;
20077 	ipif_t   *tmp_ipif;
20078 	boolean_t	flush_ire_cache = B_TRUE;
20079 	int	err = 0;
20080 	phyint_t *phyi;
20081 	ire_t	**ipif_saved_irep = NULL;
20082 	int ipif_saved_ire_cnt;
20083 	int	cnt;
20084 	boolean_t	src_ipif_held = B_FALSE;
20085 	boolean_t	ire_added = B_FALSE;
20086 	boolean_t	loopback = B_FALSE;
20087 	ip_stack_t	*ipst = ill->ill_ipst;
20088 
20089 	ip1dbg(("ipif_up_done(%s:%u)\n",
20090 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20091 	/* Check if this is a loopback interface */
20092 	if (ipif->ipif_ill->ill_wq == NULL)
20093 		loopback = B_TRUE;
20094 
20095 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20096 	/*
20097 	 * If all other interfaces for this ill are down or DEPRECATED,
20098 	 * or otherwise unsuitable for source address selection, remove
20099 	 * any IRE_CACHE entries for this ill to make sure source
20100 	 * address selection gets to take this new ipif into account.
20101 	 * No need to hold ill_lock while traversing the ipif list since
20102 	 * we are writer
20103 	 */
20104 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20105 	    tmp_ipif = tmp_ipif->ipif_next) {
20106 		if (((tmp_ipif->ipif_flags &
20107 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20108 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20109 		    (tmp_ipif == ipif))
20110 			continue;
20111 		/* first useable pre-existing interface */
20112 		flush_ire_cache = B_FALSE;
20113 		break;
20114 	}
20115 	if (flush_ire_cache)
20116 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20117 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20118 
20119 	/*
20120 	 * Figure out which way the send-to queue should go.  Only
20121 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20122 	 * should show up here.
20123 	 */
20124 	switch (ill->ill_net_type) {
20125 	case IRE_IF_RESOLVER:
20126 		stq = ill->ill_rq;
20127 		break;
20128 	case IRE_IF_NORESOLVER:
20129 	case IRE_LOOPBACK:
20130 		stq = ill->ill_wq;
20131 		break;
20132 	default:
20133 		return (EINVAL);
20134 	}
20135 
20136 	if (IS_LOOPBACK(ill)) {
20137 		/*
20138 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20139 		 * ipif_lookup_on_name(), but in the case of zones we can have
20140 		 * several loopback addresses on lo0. So all the interfaces with
20141 		 * loopback addresses need to be marked IRE_LOOPBACK.
20142 		 */
20143 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20144 		    htonl(INADDR_LOOPBACK))
20145 			ipif->ipif_ire_type = IRE_LOOPBACK;
20146 		else
20147 			ipif->ipif_ire_type = IRE_LOCAL;
20148 	}
20149 
20150 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20151 		/*
20152 		 * Can't use our source address. Select a different
20153 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20154 		 */
20155 		src_ipif = ipif_select_source(ipif->ipif_ill,
20156 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20157 		if (src_ipif == NULL)
20158 			src_ipif = ipif;	/* Last resort */
20159 		else
20160 			src_ipif_held = B_TRUE;
20161 	} else {
20162 		src_ipif = ipif;
20163 	}
20164 
20165 	/* Create all the IREs associated with this interface */
20166 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20167 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20168 
20169 		/*
20170 		 * If we're on a labeled system then make sure that zone-
20171 		 * private addresses have proper remote host database entries.
20172 		 */
20173 		if (is_system_labeled() &&
20174 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20175 		    !tsol_check_interface_address(ipif))
20176 			return (EINVAL);
20177 
20178 		/* Register the source address for __sin6_src_id */
20179 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20180 		    ipif->ipif_zoneid, ipst);
20181 		if (err != 0) {
20182 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20183 			return (err);
20184 		}
20185 
20186 		/* If the interface address is set, create the local IRE. */
20187 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20188 		    (void *)ipif,
20189 		    ipif->ipif_ire_type,
20190 		    ntohl(ipif->ipif_lcl_addr)));
20191 		*irep++ = ire_create(
20192 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20193 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20194 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20195 		    NULL,				/* no gateway */
20196 		    &ip_loopback_mtuplus,		/* max frag size */
20197 		    NULL,
20198 		    ipif->ipif_rq,			/* recv-from queue */
20199 		    NULL,				/* no send-to queue */
20200 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20201 		    ipif,
20202 		    0,
20203 		    0,
20204 		    0,
20205 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20206 		    RTF_PRIVATE : 0,
20207 		    &ire_uinfo_null,
20208 		    NULL,
20209 		    NULL,
20210 		    ipst);
20211 	} else {
20212 		ip1dbg((
20213 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20214 		    ipif->ipif_ire_type,
20215 		    ntohl(ipif->ipif_lcl_addr),
20216 		    (uint_t)ipif->ipif_flags));
20217 	}
20218 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20219 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20220 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20221 	} else {
20222 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20223 	}
20224 
20225 	subnet_mask = ipif->ipif_net_mask;
20226 
20227 	/*
20228 	 * If mask was not specified, use natural netmask of
20229 	 * interface address. Also, store this mask back into the
20230 	 * ipif struct.
20231 	 */
20232 	if (subnet_mask == 0) {
20233 		subnet_mask = net_mask;
20234 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20235 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20236 		    ipif->ipif_v6subnet);
20237 	}
20238 
20239 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20240 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20241 	    ipif->ipif_subnet != INADDR_ANY) {
20242 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20243 
20244 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20245 			route_mask = IP_HOST_MASK;
20246 		} else {
20247 			route_mask = subnet_mask;
20248 		}
20249 
20250 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20251 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20252 		    (void *)ipif, (void *)ill,
20253 		    ill->ill_net_type,
20254 		    ntohl(ipif->ipif_subnet)));
20255 		*irep++ = ire_create(
20256 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20257 		    (uchar_t *)&route_mask,		/* mask */
20258 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20259 		    NULL,				/* no gateway */
20260 		    &ipif->ipif_mtu,			/* max frag */
20261 		    NULL,
20262 		    NULL,				/* no recv queue */
20263 		    stq,				/* send-to queue */
20264 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20265 		    ipif,
20266 		    0,
20267 		    0,
20268 		    0,
20269 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20270 		    &ire_uinfo_null,
20271 		    NULL,
20272 		    NULL,
20273 		    ipst);
20274 	}
20275 
20276 	/*
20277 	 * Create any necessary broadcast IREs.
20278 	 */
20279 	if (ipif->ipif_flags & IPIF_BROADCAST)
20280 		irep = ipif_create_bcast_ires(ipif, irep);
20281 
20282 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20283 
20284 	/* If an earlier ire_create failed, get out now */
20285 	for (irep1 = irep; irep1 > ire_array; ) {
20286 		irep1--;
20287 		if (*irep1 == NULL) {
20288 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20289 			err = ENOMEM;
20290 			goto bad;
20291 		}
20292 	}
20293 
20294 	/*
20295 	 * Need to atomically check for ip_addr_availablity_check
20296 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20297 	 * from group also.The ill_g_lock is grabbed as reader
20298 	 * just to make sure no new ills or new ipifs are being added
20299 	 * to the system while we are checking the uniqueness of addresses.
20300 	 */
20301 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20302 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20303 	/* Mark it up, and increment counters. */
20304 	ipif->ipif_flags |= IPIF_UP;
20305 	ill->ill_ipif_up_count++;
20306 	err = ip_addr_availability_check(ipif);
20307 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20308 	rw_exit(&ipst->ips_ill_g_lock);
20309 
20310 	if (err != 0) {
20311 		/*
20312 		 * Our address may already be up on the same ill. In this case,
20313 		 * the ARP entry for our ipif replaced the one for the other
20314 		 * ipif. So we don't want to delete it (otherwise the other ipif
20315 		 * would be unable to send packets).
20316 		 * ip_addr_availability_check() identifies this case for us and
20317 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20318 		 * which is the expected error code.
20319 		 */
20320 		if (err == EADDRINUSE) {
20321 			freemsg(ipif->ipif_arp_del_mp);
20322 			ipif->ipif_arp_del_mp = NULL;
20323 			err = EADDRNOTAVAIL;
20324 		}
20325 		ill->ill_ipif_up_count--;
20326 		ipif->ipif_flags &= ~IPIF_UP;
20327 		goto bad;
20328 	}
20329 
20330 	/*
20331 	 * Add in all newly created IREs.  ire_create_bcast() has
20332 	 * already checked for duplicates of the IRE_BROADCAST type.
20333 	 * We want to add before we call ifgrp_insert which wants
20334 	 * to know whether IRE_IF_RESOLVER exists or not.
20335 	 *
20336 	 * NOTE : We refrele the ire though we may branch to "bad"
20337 	 *	  later on where we do ire_delete. This is okay
20338 	 *	  because nobody can delete it as we are running
20339 	 *	  exclusively.
20340 	 */
20341 	for (irep1 = irep; irep1 > ire_array; ) {
20342 		irep1--;
20343 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20344 		/*
20345 		 * refheld by ire_add. refele towards the end of the func
20346 		 */
20347 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20348 	}
20349 	ire_added = B_TRUE;
20350 	/*
20351 	 * Form groups if possible.
20352 	 *
20353 	 * If we are supposed to be in a ill_group with a name, insert it
20354 	 * now as we know that at least one ipif is UP. Otherwise form
20355 	 * nameless groups.
20356 	 *
20357 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20358 	 * this ipif into the appropriate interface group, or create a
20359 	 * new one. If this is already in a nameless group, we try to form
20360 	 * a bigger group looking at other ills potentially sharing this
20361 	 * ipif's prefix.
20362 	 */
20363 	phyi = ill->ill_phyint;
20364 	if (phyi->phyint_groupname_len != 0) {
20365 		ASSERT(phyi->phyint_groupname != NULL);
20366 		if (ill->ill_ipif_up_count == 1) {
20367 			ASSERT(ill->ill_group == NULL);
20368 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20369 			    phyi->phyint_groupname, NULL, B_TRUE);
20370 			if (err != 0) {
20371 				ip1dbg(("ipif_up_done: illgrp allocation "
20372 				    "failed, error %d\n", err));
20373 				goto bad;
20374 			}
20375 		}
20376 		ASSERT(ill->ill_group != NULL);
20377 	}
20378 
20379 	/*
20380 	 * When this is part of group, we need to make sure that
20381 	 * any broadcast ires created because of this ipif coming
20382 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20383 	 * so that we don't receive duplicate broadcast packets.
20384 	 */
20385 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20386 		ipif_renominate_bcast(ipif);
20387 
20388 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20389 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20390 	ipif_saved_irep = ipif_recover_ire(ipif);
20391 
20392 	if (!loopback) {
20393 		/*
20394 		 * If the broadcast address has been set, make sure it makes
20395 		 * sense based on the interface address.
20396 		 * Only match on ill since we are sharing broadcast addresses.
20397 		 */
20398 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20399 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20400 			ire_t	*ire;
20401 
20402 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20403 			    IRE_BROADCAST, ipif, ALL_ZONES,
20404 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20405 
20406 			if (ire == NULL) {
20407 				/*
20408 				 * If there isn't a matching broadcast IRE,
20409 				 * revert to the default for this netmask.
20410 				 */
20411 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20412 				mutex_enter(&ipif->ipif_ill->ill_lock);
20413 				ipif_set_default(ipif);
20414 				mutex_exit(&ipif->ipif_ill->ill_lock);
20415 			} else {
20416 				ire_refrele(ire);
20417 			}
20418 		}
20419 
20420 	}
20421 
20422 	/* This is the first interface on this ill */
20423 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20424 		/*
20425 		 * Need to recover all multicast memberships in the driver.
20426 		 * This had to be deferred until we had attached.
20427 		 */
20428 		ill_recover_multicast(ill);
20429 	}
20430 	/* Join the allhosts multicast address */
20431 	ipif_multicast_up(ipif);
20432 
20433 	if (!loopback) {
20434 		/*
20435 		 * See whether anybody else would benefit from the
20436 		 * new ipif that we added. We call this always rather
20437 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20438 		 * ipif is for the benefit of illgrp_insert (done above)
20439 		 * which does not do source address selection as it does
20440 		 * not want to re-create interface routes that we are
20441 		 * having reference to it here.
20442 		 */
20443 		ill_update_source_selection(ill);
20444 	}
20445 
20446 	for (irep1 = irep; irep1 > ire_array; ) {
20447 		irep1--;
20448 		if (*irep1 != NULL) {
20449 			/* was held in ire_add */
20450 			ire_refrele(*irep1);
20451 		}
20452 	}
20453 
20454 	cnt = ipif_saved_ire_cnt;
20455 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20456 		if (*irep1 != NULL) {
20457 			/* was held in ire_add */
20458 			ire_refrele(*irep1);
20459 		}
20460 	}
20461 
20462 	if (!loopback && ipif->ipif_addr_ready) {
20463 		/* Broadcast an address mask reply. */
20464 		ipif_mask_reply(ipif);
20465 	}
20466 	if (ipif_saved_irep != NULL) {
20467 		kmem_free(ipif_saved_irep,
20468 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20469 	}
20470 	if (src_ipif_held)
20471 		ipif_refrele(src_ipif);
20472 
20473 	/*
20474 	 * This had to be deferred until we had bound.  Tell routing sockets and
20475 	 * others that this interface is up if it looks like the address has
20476 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20477 	 * duplicate address detection to do its thing.
20478 	 */
20479 	if (ipif->ipif_addr_ready) {
20480 		ip_rts_ifmsg(ipif);
20481 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20482 		/* Let SCTP update the status for this ipif */
20483 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20484 	}
20485 	return (0);
20486 
20487 bad:
20488 	ip1dbg(("ipif_up_done: FAILED \n"));
20489 	/*
20490 	 * We don't have to bother removing from ill groups because
20491 	 *
20492 	 * 1) For groups with names, we insert only when the first ipif
20493 	 *    comes up. In that case if it fails, it will not be in any
20494 	 *    group. So, we need not try to remove for that case.
20495 	 *
20496 	 * 2) For groups without names, either we tried to insert ipif_ill
20497 	 *    in a group as singleton or found some other group to become
20498 	 *    a bigger group. For the former, if it fails we don't have
20499 	 *    anything to do as ipif_ill is not in the group and for the
20500 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20501 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20502 	 */
20503 	while (irep > ire_array) {
20504 		irep--;
20505 		if (*irep != NULL) {
20506 			ire_delete(*irep);
20507 			if (ire_added)
20508 				ire_refrele(*irep);
20509 		}
20510 	}
20511 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20512 
20513 	if (ipif_saved_irep != NULL) {
20514 		kmem_free(ipif_saved_irep,
20515 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20516 	}
20517 	if (src_ipif_held)
20518 		ipif_refrele(src_ipif);
20519 
20520 	ipif_arp_down(ipif);
20521 	return (err);
20522 }
20523 
20524 /*
20525  * Turn off the ARP with the ILLF_NOARP flag.
20526  */
20527 static int
20528 ill_arp_off(ill_t *ill)
20529 {
20530 	mblk_t	*arp_off_mp = NULL;
20531 	mblk_t	*arp_on_mp = NULL;
20532 
20533 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20534 
20535 	ASSERT(IAM_WRITER_ILL(ill));
20536 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20537 
20538 	/*
20539 	 * If the on message is still around we've already done
20540 	 * an arp_off without doing an arp_on thus there is no
20541 	 * work needed.
20542 	 */
20543 	if (ill->ill_arp_on_mp != NULL)
20544 		return (0);
20545 
20546 	/*
20547 	 * Allocate an ARP on message (to be saved) and an ARP off message
20548 	 */
20549 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20550 	if (!arp_off_mp)
20551 		return (ENOMEM);
20552 
20553 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20554 	if (!arp_on_mp)
20555 		goto failed;
20556 
20557 	ASSERT(ill->ill_arp_on_mp == NULL);
20558 	ill->ill_arp_on_mp = arp_on_mp;
20559 
20560 	/* Send an AR_INTERFACE_OFF request */
20561 	putnext(ill->ill_rq, arp_off_mp);
20562 	return (0);
20563 failed:
20564 
20565 	if (arp_off_mp)
20566 		freemsg(arp_off_mp);
20567 	return (ENOMEM);
20568 }
20569 
20570 /*
20571  * Turn on ARP by turning off the ILLF_NOARP flag.
20572  */
20573 static int
20574 ill_arp_on(ill_t *ill)
20575 {
20576 	mblk_t	*mp;
20577 
20578 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20579 
20580 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20581 
20582 	ASSERT(IAM_WRITER_ILL(ill));
20583 	/*
20584 	 * Send an AR_INTERFACE_ON request if we have already done
20585 	 * an arp_off (which allocated the message).
20586 	 */
20587 	if (ill->ill_arp_on_mp != NULL) {
20588 		mp = ill->ill_arp_on_mp;
20589 		ill->ill_arp_on_mp = NULL;
20590 		putnext(ill->ill_rq, mp);
20591 	}
20592 	return (0);
20593 }
20594 
20595 /*
20596  * Called after either deleting ill from the group or when setting
20597  * FAILED or STANDBY on the interface.
20598  */
20599 static void
20600 illgrp_reset_schednext(ill_t *ill)
20601 {
20602 	ill_group_t *illgrp;
20603 	ill_t *save_ill;
20604 
20605 	ASSERT(IAM_WRITER_ILL(ill));
20606 	/*
20607 	 * When called from illgrp_delete, ill_group will be non-NULL.
20608 	 * But when called from ip_sioctl_flags, it could be NULL if
20609 	 * somebody is setting FAILED/INACTIVE on some interface which
20610 	 * is not part of a group.
20611 	 */
20612 	illgrp = ill->ill_group;
20613 	if (illgrp == NULL)
20614 		return;
20615 	if (illgrp->illgrp_ill_schednext != ill)
20616 		return;
20617 
20618 	illgrp->illgrp_ill_schednext = NULL;
20619 	save_ill = ill;
20620 	/*
20621 	 * Choose a good ill to be the next one for
20622 	 * outbound traffic. As the flags FAILED/STANDBY is
20623 	 * not yet marked when called from ip_sioctl_flags,
20624 	 * we check for ill separately.
20625 	 */
20626 	for (ill = illgrp->illgrp_ill; ill != NULL;
20627 	    ill = ill->ill_group_next) {
20628 		if ((ill != save_ill) &&
20629 		    !(ill->ill_phyint->phyint_flags &
20630 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20631 			illgrp->illgrp_ill_schednext = ill;
20632 			return;
20633 		}
20634 	}
20635 }
20636 
20637 /*
20638  * Given an ill, find the next ill in the group to be scheduled.
20639  * (This should be called by ip_newroute() before ire_create().)
20640  * The passed in ill may be pulled out of the group, after we have picked
20641  * up a different outgoing ill from the same group. However ire add will
20642  * atomically check this.
20643  */
20644 ill_t *
20645 illgrp_scheduler(ill_t *ill)
20646 {
20647 	ill_t *retill;
20648 	ill_group_t *illgrp;
20649 	int illcnt;
20650 	int i;
20651 	uint64_t flags;
20652 	ip_stack_t	*ipst = ill->ill_ipst;
20653 
20654 	/*
20655 	 * We don't use a lock to check for the ill_group. If this ill
20656 	 * is currently being inserted we may end up just returning this
20657 	 * ill itself. That is ok.
20658 	 */
20659 	if (ill->ill_group == NULL) {
20660 		ill_refhold(ill);
20661 		return (ill);
20662 	}
20663 
20664 	/*
20665 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20666 	 * a set of stable ills. No ill can be added or deleted or change
20667 	 * group while we hold the reader lock.
20668 	 */
20669 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20670 	if ((illgrp = ill->ill_group) == NULL) {
20671 		rw_exit(&ipst->ips_ill_g_lock);
20672 		ill_refhold(ill);
20673 		return (ill);
20674 	}
20675 
20676 	illcnt = illgrp->illgrp_ill_count;
20677 	mutex_enter(&illgrp->illgrp_lock);
20678 	retill = illgrp->illgrp_ill_schednext;
20679 
20680 	if (retill == NULL)
20681 		retill = illgrp->illgrp_ill;
20682 
20683 	/*
20684 	 * We do a circular search beginning at illgrp_ill_schednext
20685 	 * or illgrp_ill. We don't check the flags against the ill lock
20686 	 * since it can change anytime. The ire creation will be atomic
20687 	 * and will fail if the ill is FAILED or OFFLINE.
20688 	 */
20689 	for (i = 0; i < illcnt; i++) {
20690 		flags = retill->ill_phyint->phyint_flags;
20691 
20692 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20693 		    ILL_CAN_LOOKUP(retill)) {
20694 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20695 			ill_refhold(retill);
20696 			break;
20697 		}
20698 		retill = retill->ill_group_next;
20699 		if (retill == NULL)
20700 			retill = illgrp->illgrp_ill;
20701 	}
20702 	mutex_exit(&illgrp->illgrp_lock);
20703 	rw_exit(&ipst->ips_ill_g_lock);
20704 
20705 	return (i == illcnt ? NULL : retill);
20706 }
20707 
20708 /*
20709  * Checks for availbility of a usable source address (if there is one) when the
20710  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20711  * this selection is done regardless of the destination.
20712  */
20713 boolean_t
20714 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20715 {
20716 	uint_t	ifindex;
20717 	ipif_t	*ipif = NULL;
20718 	ill_t	*uill;
20719 	boolean_t isv6;
20720 	ip_stack_t	*ipst = ill->ill_ipst;
20721 
20722 	ASSERT(ill != NULL);
20723 
20724 	isv6 = ill->ill_isv6;
20725 	ifindex = ill->ill_usesrc_ifindex;
20726 	if (ifindex != 0) {
20727 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20728 		    NULL, ipst);
20729 		if (uill == NULL)
20730 			return (NULL);
20731 		mutex_enter(&uill->ill_lock);
20732 		for (ipif = uill->ill_ipif; ipif != NULL;
20733 		    ipif = ipif->ipif_next) {
20734 			if (!IPIF_CAN_LOOKUP(ipif))
20735 				continue;
20736 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20737 				continue;
20738 			if (!(ipif->ipif_flags & IPIF_UP))
20739 				continue;
20740 			if (ipif->ipif_zoneid != zoneid)
20741 				continue;
20742 			if ((isv6 &&
20743 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20744 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20745 				continue;
20746 			mutex_exit(&uill->ill_lock);
20747 			ill_refrele(uill);
20748 			return (B_TRUE);
20749 		}
20750 		mutex_exit(&uill->ill_lock);
20751 		ill_refrele(uill);
20752 	}
20753 	return (B_FALSE);
20754 }
20755 
20756 /*
20757  * Determine the best source address given a destination address and an ill.
20758  * Prefers non-deprecated over deprecated but will return a deprecated
20759  * address if there is no other choice. If there is a usable source address
20760  * on the interface pointed to by ill_usesrc_ifindex then that is given
20761  * first preference.
20762  *
20763  * Returns NULL if there is no suitable source address for the ill.
20764  * This only occurs when there is no valid source address for the ill.
20765  */
20766 ipif_t *
20767 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20768 {
20769 	ipif_t *ipif;
20770 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20771 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20772 	int index = 0;
20773 	boolean_t wrapped = B_FALSE;
20774 	boolean_t same_subnet_only = B_FALSE;
20775 	boolean_t ipif_same_found, ipif_other_found;
20776 	boolean_t specific_found;
20777 	ill_t	*till, *usill = NULL;
20778 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20779 	ip_stack_t	*ipst = ill->ill_ipst;
20780 
20781 	if (ill->ill_usesrc_ifindex != 0) {
20782 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20783 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20784 		if (usill != NULL)
20785 			ill = usill;	/* Select source from usesrc ILL */
20786 		else
20787 			return (NULL);
20788 	}
20789 
20790 	/*
20791 	 * If we're dealing with an unlabeled destination on a labeled system,
20792 	 * make sure that we ignore source addresses that are incompatible with
20793 	 * the destination's default label.  That destination's default label
20794 	 * must dominate the minimum label on the source address.
20795 	 */
20796 	dst_rhtp = NULL;
20797 	if (is_system_labeled()) {
20798 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20799 		if (dst_rhtp == NULL)
20800 			return (NULL);
20801 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20802 			TPC_RELE(dst_rhtp);
20803 			dst_rhtp = NULL;
20804 		}
20805 	}
20806 
20807 	/*
20808 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20809 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20810 	 * After selecting the right ipif, under ill_lock make sure ipif is
20811 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20812 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20813 	 * but not under a lock.
20814 	 */
20815 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20816 
20817 retry:
20818 	till = ill;
20819 	ipif_arr[0] = NULL;
20820 
20821 	if (till->ill_group != NULL)
20822 		till = till->ill_group->illgrp_ill;
20823 
20824 	/*
20825 	 * Choose one good source address from each ill across the group.
20826 	 * If possible choose a source address in the same subnet as
20827 	 * the destination address.
20828 	 *
20829 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20830 	 * This is okay because of the following.
20831 	 *
20832 	 *    If PHYI_FAILED is set and we still have non-deprecated
20833 	 *    addresses, it means the addresses have not yet been
20834 	 *    failed over to a different interface. We potentially
20835 	 *    select them to create IRE_CACHES, which will be later
20836 	 *    flushed when the addresses move over.
20837 	 *
20838 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20839 	 *    addresses, it means either the user has configured them
20840 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20841 	 *    been moved over. For the former, in.mpathd does a failover
20842 	 *    when the interface becomes INACTIVE and hence we should
20843 	 *    not find them. Once INACTIVE is set, we don't allow them
20844 	 *    to create logical interfaces anymore. For the latter, a
20845 	 *    flush will happen when INACTIVE is cleared which will
20846 	 *    flush the IRE_CACHES.
20847 	 *
20848 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20849 	 *    over soon. We potentially select them to create IRE_CACHEs,
20850 	 *    which will be later flushed when the addresses move over.
20851 	 *
20852 	 * NOTE : As ipif_select_source is called to borrow source address
20853 	 * for an ipif that is part of a group, source address selection
20854 	 * will be re-done whenever the group changes i.e either an
20855 	 * insertion/deletion in the group.
20856 	 *
20857 	 * Fill ipif_arr[] with source addresses, using these rules:
20858 	 *
20859 	 *	1. At most one source address from a given ill ends up
20860 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20861 	 *	   associated with a given ill ends up in ipif_arr[].
20862 	 *
20863 	 *	2. If there is at least one non-deprecated ipif in the
20864 	 *	   IPMP group with a source address on the same subnet as
20865 	 *	   our destination, then fill ipif_arr[] only with
20866 	 *	   source addresses on the same subnet as our destination.
20867 	 *	   Note that because of (1), only the first
20868 	 *	   non-deprecated ipif found with a source address
20869 	 *	   matching the destination ends up in ipif_arr[].
20870 	 *
20871 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20872 	 *	   addresses not in the same subnet as our destination.
20873 	 *	   Again, because of (1), only the first off-subnet source
20874 	 *	   address will be chosen.
20875 	 *
20876 	 *	4. If there are no non-deprecated ipifs, then just use
20877 	 *	   the source address associated with the last deprecated
20878 	 *	   one we find that happens to be on the same subnet,
20879 	 *	   otherwise the first one not in the same subnet.
20880 	 */
20881 	specific_found = B_FALSE;
20882 	for (; till != NULL; till = till->ill_group_next) {
20883 		ipif_same_found = B_FALSE;
20884 		ipif_other_found = B_FALSE;
20885 		for (ipif = till->ill_ipif; ipif != NULL;
20886 		    ipif = ipif->ipif_next) {
20887 			if (!IPIF_CAN_LOOKUP(ipif))
20888 				continue;
20889 			/* Always skip NOLOCAL and ANYCAST interfaces */
20890 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20891 				continue;
20892 			if (!(ipif->ipif_flags & IPIF_UP) ||
20893 			    !ipif->ipif_addr_ready)
20894 				continue;
20895 			if (ipif->ipif_zoneid != zoneid &&
20896 			    ipif->ipif_zoneid != ALL_ZONES)
20897 				continue;
20898 			/*
20899 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20900 			 * but are not valid as source addresses.
20901 			 */
20902 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20903 				continue;
20904 
20905 			/*
20906 			 * Check compatibility of local address for
20907 			 * destination's default label if we're on a labeled
20908 			 * system.  Incompatible addresses can't be used at
20909 			 * all.
20910 			 */
20911 			if (dst_rhtp != NULL) {
20912 				boolean_t incompat;
20913 
20914 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20915 				    IPV4_VERSION, B_FALSE);
20916 				if (src_rhtp == NULL)
20917 					continue;
20918 				incompat =
20919 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20920 				    src_rhtp->tpc_tp.tp_doi !=
20921 				    dst_rhtp->tpc_tp.tp_doi ||
20922 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20923 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20924 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20925 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20926 				TPC_RELE(src_rhtp);
20927 				if (incompat)
20928 					continue;
20929 			}
20930 
20931 			/*
20932 			 * We prefer not to use all all-zones addresses, if we
20933 			 * can avoid it, as they pose problems with unlabeled
20934 			 * destinations.
20935 			 */
20936 			if (ipif->ipif_zoneid != ALL_ZONES) {
20937 				if (!specific_found &&
20938 				    (!same_subnet_only ||
20939 				    (ipif->ipif_net_mask & dst) ==
20940 				    ipif->ipif_subnet)) {
20941 					index = 0;
20942 					specific_found = B_TRUE;
20943 					ipif_other_found = B_FALSE;
20944 				}
20945 			} else {
20946 				if (specific_found)
20947 					continue;
20948 			}
20949 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20950 				if (ipif_dep == NULL ||
20951 				    (ipif->ipif_net_mask & dst) ==
20952 				    ipif->ipif_subnet)
20953 					ipif_dep = ipif;
20954 				continue;
20955 			}
20956 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20957 				/* found a source address in the same subnet */
20958 				if (!same_subnet_only) {
20959 					same_subnet_only = B_TRUE;
20960 					index = 0;
20961 				}
20962 				ipif_same_found = B_TRUE;
20963 			} else {
20964 				if (same_subnet_only || ipif_other_found)
20965 					continue;
20966 				ipif_other_found = B_TRUE;
20967 			}
20968 			ipif_arr[index++] = ipif;
20969 			if (index == MAX_IPIF_SELECT_SOURCE) {
20970 				wrapped = B_TRUE;
20971 				index = 0;
20972 			}
20973 			if (ipif_same_found)
20974 				break;
20975 		}
20976 	}
20977 
20978 	if (ipif_arr[0] == NULL) {
20979 		ipif = ipif_dep;
20980 	} else {
20981 		if (wrapped)
20982 			index = MAX_IPIF_SELECT_SOURCE;
20983 		ipif = ipif_arr[ipif_rand(ipst) % index];
20984 		ASSERT(ipif != NULL);
20985 	}
20986 
20987 	if (ipif != NULL) {
20988 		mutex_enter(&ipif->ipif_ill->ill_lock);
20989 		if (!IPIF_CAN_LOOKUP(ipif)) {
20990 			mutex_exit(&ipif->ipif_ill->ill_lock);
20991 			goto retry;
20992 		}
20993 		ipif_refhold_locked(ipif);
20994 		mutex_exit(&ipif->ipif_ill->ill_lock);
20995 	}
20996 
20997 	rw_exit(&ipst->ips_ill_g_lock);
20998 	if (usill != NULL)
20999 		ill_refrele(usill);
21000 	if (dst_rhtp != NULL)
21001 		TPC_RELE(dst_rhtp);
21002 
21003 #ifdef DEBUG
21004 	if (ipif == NULL) {
21005 		char buf1[INET6_ADDRSTRLEN];
21006 
21007 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21008 		    ill->ill_name,
21009 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21010 	} else {
21011 		char buf1[INET6_ADDRSTRLEN];
21012 		char buf2[INET6_ADDRSTRLEN];
21013 
21014 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21015 		    ipif->ipif_ill->ill_name,
21016 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21017 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21018 		    buf2, sizeof (buf2))));
21019 	}
21020 #endif /* DEBUG */
21021 	return (ipif);
21022 }
21023 
21024 
21025 /*
21026  * If old_ipif is not NULL, see if ipif was derived from old
21027  * ipif and if so, recreate the interface route by re-doing
21028  * source address selection. This happens when ipif_down ->
21029  * ipif_update_other_ipifs calls us.
21030  *
21031  * If old_ipif is NULL, just redo the source address selection
21032  * if needed. This happens when illgrp_insert or ipif_up_done
21033  * calls us.
21034  */
21035 static void
21036 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21037 {
21038 	ire_t *ire;
21039 	ire_t *ipif_ire;
21040 	queue_t *stq;
21041 	ipif_t *nipif;
21042 	ill_t *ill;
21043 	boolean_t need_rele = B_FALSE;
21044 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21045 
21046 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21047 	ASSERT(IAM_WRITER_IPIF(ipif));
21048 
21049 	ill = ipif->ipif_ill;
21050 	if (!(ipif->ipif_flags &
21051 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21052 		/*
21053 		 * Can't possibly have borrowed the source
21054 		 * from old_ipif.
21055 		 */
21056 		return;
21057 	}
21058 
21059 	/*
21060 	 * Is there any work to be done? No work if the address
21061 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21062 	 * ipif_select_source() does not borrow addresses from
21063 	 * NOLOCAL and ANYCAST interfaces).
21064 	 */
21065 	if ((old_ipif != NULL) &&
21066 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21067 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21068 	    (old_ipif->ipif_flags &
21069 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21070 		return;
21071 	}
21072 
21073 	/*
21074 	 * Perform the same checks as when creating the
21075 	 * IRE_INTERFACE in ipif_up_done.
21076 	 */
21077 	if (!(ipif->ipif_flags & IPIF_UP))
21078 		return;
21079 
21080 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21081 	    (ipif->ipif_subnet == INADDR_ANY))
21082 		return;
21083 
21084 	ipif_ire = ipif_to_ire(ipif);
21085 	if (ipif_ire == NULL)
21086 		return;
21087 
21088 	/*
21089 	 * We know that ipif uses some other source for its
21090 	 * IRE_INTERFACE. Is it using the source of this
21091 	 * old_ipif?
21092 	 */
21093 	if (old_ipif != NULL &&
21094 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21095 		ire_refrele(ipif_ire);
21096 		return;
21097 	}
21098 	if (ip_debug > 2) {
21099 		/* ip1dbg */
21100 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21101 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21102 	}
21103 
21104 	stq = ipif_ire->ire_stq;
21105 
21106 	/*
21107 	 * Can't use our source address. Select a different
21108 	 * source address for the IRE_INTERFACE.
21109 	 */
21110 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21111 	if (nipif == NULL) {
21112 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21113 		nipif = ipif;
21114 	} else {
21115 		need_rele = B_TRUE;
21116 	}
21117 
21118 	ire = ire_create(
21119 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21120 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21121 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21122 	    NULL,				/* no gateway */
21123 	    &ipif->ipif_mtu,			/* max frag */
21124 	    NULL,				/* no src nce */
21125 	    NULL,				/* no recv from queue */
21126 	    stq,				/* send-to queue */
21127 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21128 	    ipif,
21129 	    0,
21130 	    0,
21131 	    0,
21132 	    0,
21133 	    &ire_uinfo_null,
21134 	    NULL,
21135 	    NULL,
21136 	    ipst);
21137 
21138 	if (ire != NULL) {
21139 		ire_t *ret_ire;
21140 		int error;
21141 
21142 		/*
21143 		 * We don't need ipif_ire anymore. We need to delete
21144 		 * before we add so that ire_add does not detect
21145 		 * duplicates.
21146 		 */
21147 		ire_delete(ipif_ire);
21148 		ret_ire = ire;
21149 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21150 		ASSERT(error == 0);
21151 		ASSERT(ire == ret_ire);
21152 		/* Held in ire_add */
21153 		ire_refrele(ret_ire);
21154 	}
21155 	/*
21156 	 * Either we are falling through from above or could not
21157 	 * allocate a replacement.
21158 	 */
21159 	ire_refrele(ipif_ire);
21160 	if (need_rele)
21161 		ipif_refrele(nipif);
21162 }
21163 
21164 /*
21165  * This old_ipif is going away.
21166  *
21167  * Determine if any other ipif's is using our address as
21168  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21169  * IPIF_DEPRECATED).
21170  * Find the IRE_INTERFACE for such ipifs and recreate them
21171  * to use an different source address following the rules in
21172  * ipif_up_done.
21173  *
21174  * This function takes an illgrp as an argument so that illgrp_delete
21175  * can call this to update source address even after deleting the
21176  * old_ipif->ipif_ill from the ill group.
21177  */
21178 static void
21179 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21180 {
21181 	ipif_t *ipif;
21182 	ill_t *ill;
21183 	char	buf[INET6_ADDRSTRLEN];
21184 
21185 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21186 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21187 
21188 	ill = old_ipif->ipif_ill;
21189 
21190 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21191 	    ill->ill_name,
21192 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21193 	    buf, sizeof (buf))));
21194 	/*
21195 	 * If this part of a group, look at all ills as ipif_select_source
21196 	 * borrows source address across all the ills in the group.
21197 	 */
21198 	if (illgrp != NULL)
21199 		ill = illgrp->illgrp_ill;
21200 
21201 	for (; ill != NULL; ill = ill->ill_group_next) {
21202 		for (ipif = ill->ill_ipif; ipif != NULL;
21203 		    ipif = ipif->ipif_next) {
21204 
21205 			if (ipif == old_ipif)
21206 				continue;
21207 
21208 			ipif_recreate_interface_routes(old_ipif, ipif);
21209 		}
21210 	}
21211 }
21212 
21213 /* ARGSUSED */
21214 int
21215 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21216 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21217 {
21218 	/*
21219 	 * ill_phyint_reinit merged the v4 and v6 into a single
21220 	 * ipsq. Could also have become part of a ipmp group in the
21221 	 * process, and we might not have been able to complete the
21222 	 * operation in ipif_set_values, if we could not become
21223 	 * exclusive.  If so restart it here.
21224 	 */
21225 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21226 }
21227 
21228 /*
21229  * Can operate on either a module or a driver queue.
21230  * Returns an error if not a module queue.
21231  */
21232 /* ARGSUSED */
21233 int
21234 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21235     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21236 {
21237 	queue_t		*q1 = q;
21238 	char 		*cp;
21239 	char		interf_name[LIFNAMSIZ];
21240 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21241 
21242 	if (q->q_next == NULL) {
21243 		ip1dbg((
21244 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21245 		return (EINVAL);
21246 	}
21247 
21248 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21249 		return (EALREADY);
21250 
21251 	do {
21252 		q1 = q1->q_next;
21253 	} while (q1->q_next);
21254 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21255 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21256 
21257 	/*
21258 	 * Here we are not going to delay the ioack until after
21259 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21260 	 * original ioctl message before sending the requests.
21261 	 */
21262 	return (ipif_set_values(q, mp, interf_name, &ppa));
21263 }
21264 
21265 /* ARGSUSED */
21266 int
21267 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21268     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21269 {
21270 	return (ENXIO);
21271 }
21272 
21273 /*
21274  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21275  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21276  * ire_check_and_create_bcast()).
21277  */
21278 static ire_t **
21279 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21280 {
21281 	ipaddr_t addr;
21282 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21283 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21284 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21285 
21286 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21287 
21288 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21289 
21290 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21291 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21292 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21293 
21294 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21295 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21296 
21297 	/*
21298 	 * For backward compatibility, we create net broadcast IREs based on
21299 	 * the old "IP address class system", since some old machines only
21300 	 * respond to these class derived net broadcast.  However, we must not
21301 	 * create these net broadcast IREs if the subnetmask is shorter than
21302 	 * the IP address class based derived netmask.  Otherwise, we may
21303 	 * create a net broadcast address which is the same as an IP address
21304 	 * on the subnet -- and then TCP will refuse to talk to that address.
21305 	 */
21306 	if (netmask < subnetmask) {
21307 		addr = netmask & ipif->ipif_subnet;
21308 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21309 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21310 		    flags);
21311 	}
21312 
21313 	/*
21314 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21315 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21316 	 * created.  Creating these broadcast IREs will only create confusion
21317 	 * as `addr' will be the same as the IP address.
21318 	 */
21319 	if (subnetmask != 0xFFFFFFFF) {
21320 		addr = ipif->ipif_subnet;
21321 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21322 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21323 		    irep, flags);
21324 	}
21325 
21326 	return (irep);
21327 }
21328 
21329 /*
21330  * Broadcast IRE info structure used in the functions below.  Since we
21331  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21332  */
21333 typedef struct bcast_ireinfo {
21334 	uchar_t		bi_type;	/* BCAST_* value from below */
21335 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21336 			bi_needrep:1,	/* do we need to replace it? */
21337 			bi_haverep:1,	/* have we replaced it? */
21338 			bi_pad:5;
21339 	ipaddr_t	bi_addr;	/* IRE address */
21340 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21341 } bcast_ireinfo_t;
21342 
21343 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21344 
21345 /*
21346  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21347  * return B_TRUE if it should immediately be used to recreate the IRE.
21348  */
21349 static boolean_t
21350 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21351 {
21352 	ipaddr_t addr;
21353 
21354 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21355 
21356 	switch (bireinfop->bi_type) {
21357 	case BCAST_NET:
21358 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21359 		if (addr != bireinfop->bi_addr)
21360 			return (B_FALSE);
21361 		break;
21362 	case BCAST_SUBNET:
21363 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21364 			return (B_FALSE);
21365 		break;
21366 	}
21367 
21368 	bireinfop->bi_needrep = 1;
21369 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21370 		if (bireinfop->bi_backup == NULL)
21371 			bireinfop->bi_backup = ipif;
21372 		return (B_FALSE);
21373 	}
21374 	return (B_TRUE);
21375 }
21376 
21377 /*
21378  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21379  * them ala ire_check_and_create_bcast().
21380  */
21381 static ire_t **
21382 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21383 {
21384 	ipaddr_t mask, addr;
21385 
21386 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21387 
21388 	addr = bireinfop->bi_addr;
21389 	irep = ire_create_bcast(ipif, addr, irep);
21390 
21391 	switch (bireinfop->bi_type) {
21392 	case BCAST_NET:
21393 		mask = ip_net_mask(ipif->ipif_subnet);
21394 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21395 		break;
21396 	case BCAST_SUBNET:
21397 		mask = ipif->ipif_net_mask;
21398 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21399 		break;
21400 	}
21401 
21402 	bireinfop->bi_haverep = 1;
21403 	return (irep);
21404 }
21405 
21406 /*
21407  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21408  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21409  * that are going away are still needed.  If so, have ipif_create_bcast()
21410  * recreate them (except for the deprecated case, as explained below).
21411  */
21412 static ire_t **
21413 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21414     ire_t **irep)
21415 {
21416 	int i;
21417 	ipif_t *ipif;
21418 
21419 	ASSERT(!ill->ill_isv6);
21420 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21421 		/*
21422 		 * Skip this ipif if it's (a) the one being taken down, (b)
21423 		 * not in the same zone, or (c) has no valid local address.
21424 		 */
21425 		if (ipif == test_ipif ||
21426 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21427 		    ipif->ipif_subnet == 0 ||
21428 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21429 		    (IPIF_UP|IPIF_BROADCAST))
21430 			continue;
21431 
21432 		/*
21433 		 * For each dying IRE that hasn't yet been replaced, see if
21434 		 * `ipif' needs it and whether the IRE should be recreated on
21435 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21436 		 * will return B_FALSE even if `ipif' needs the IRE on the
21437 		 * hopes that we'll later find a needy non-deprecated ipif.
21438 		 * However, the ipif is recorded in bi_backup for possible
21439 		 * subsequent use by ipif_check_bcast_ires().
21440 		 */
21441 		for (i = 0; i < BCAST_COUNT; i++) {
21442 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21443 				continue;
21444 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21445 				continue;
21446 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21447 		}
21448 
21449 		/*
21450 		 * If we've replaced all of the broadcast IREs that are going
21451 		 * to be taken down, we know we're done.
21452 		 */
21453 		for (i = 0; i < BCAST_COUNT; i++) {
21454 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21455 				break;
21456 		}
21457 		if (i == BCAST_COUNT)
21458 			break;
21459 	}
21460 	return (irep);
21461 }
21462 
21463 /*
21464  * Check if `test_ipif' (which is going away) is associated with any existing
21465  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21466  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21467  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21468  *
21469  * This is necessary because broadcast IREs are shared.  In particular, a
21470  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21471  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21472  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21473  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21474  * same zone, they will share the same set of broadcast IREs.
21475  *
21476  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21477  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21478  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21479  */
21480 static void
21481 ipif_check_bcast_ires(ipif_t *test_ipif)
21482 {
21483 	ill_t		*ill = test_ipif->ipif_ill;
21484 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21485 	ire_t		**irep1, **irep = &ire_array[0];
21486 	uint_t 		i, willdie;
21487 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21488 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21489 
21490 	ASSERT(!test_ipif->ipif_isv6);
21491 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21492 
21493 	/*
21494 	 * No broadcast IREs for the LOOPBACK interface
21495 	 * or others such as point to point and IPIF_NOXMIT.
21496 	 */
21497 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21498 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21499 		return;
21500 
21501 	bzero(bireinfo, sizeof (bireinfo));
21502 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21503 	bireinfo[0].bi_addr = 0;
21504 
21505 	bireinfo[1].bi_type = BCAST_ALLONES;
21506 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21507 
21508 	bireinfo[2].bi_type = BCAST_NET;
21509 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21510 
21511 	if (test_ipif->ipif_net_mask != 0)
21512 		mask = test_ipif->ipif_net_mask;
21513 	bireinfo[3].bi_type = BCAST_SUBNET;
21514 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21515 
21516 	/*
21517 	 * Figure out what (if any) broadcast IREs will die as a result of
21518 	 * `test_ipif' going away.  If none will die, we're done.
21519 	 */
21520 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21521 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21522 		    test_ipif, ALL_ZONES, NULL,
21523 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21524 		if (ire != NULL) {
21525 			willdie++;
21526 			bireinfo[i].bi_willdie = 1;
21527 			ire_refrele(ire);
21528 		}
21529 	}
21530 
21531 	if (willdie == 0)
21532 		return;
21533 
21534 	/*
21535 	 * Walk through all the ipifs that will be affected by the dying IREs,
21536 	 * and recreate the IREs as necessary.
21537 	 */
21538 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21539 
21540 	/*
21541 	 * Scan through the set of broadcast IREs and see if there are any
21542 	 * that we need to replace that have not yet been replaced.  If so,
21543 	 * replace them using the appropriate backup ipif.
21544 	 */
21545 	for (i = 0; i < BCAST_COUNT; i++) {
21546 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21547 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21548 			    &bireinfo[i], irep);
21549 	}
21550 
21551 	/*
21552 	 * If we can't create all of them, don't add any of them.  (Code in
21553 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21554 	 * non-loopback copy and loopback copy for a given address.)
21555 	 */
21556 	for (irep1 = irep; irep1 > ire_array; ) {
21557 		irep1--;
21558 		if (*irep1 == NULL) {
21559 			ip0dbg(("ipif_check_bcast_ires: can't create "
21560 			    "IRE_BROADCAST, memory allocation failure\n"));
21561 			while (irep > ire_array) {
21562 				irep--;
21563 				if (*irep != NULL)
21564 					ire_delete(*irep);
21565 			}
21566 			return;
21567 		}
21568 	}
21569 
21570 	for (irep1 = irep; irep1 > ire_array; ) {
21571 		irep1--;
21572 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21573 			ire_refrele(*irep1);		/* Held in ire_add */
21574 	}
21575 }
21576 
21577 /*
21578  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21579  * from lifr_flags and the name from lifr_name.
21580  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21581  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21582  * Returns EINPROGRESS when mp has been consumed by queueing it on
21583  * ill_pending_mp and the ioctl will complete in ip_rput.
21584  *
21585  * Can operate on either a module or a driver queue.
21586  * Returns an error if not a module queue.
21587  */
21588 /* ARGSUSED */
21589 int
21590 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21591     ip_ioctl_cmd_t *ipip, void *if_req)
21592 {
21593 	ill_t	*ill = q->q_ptr;
21594 	phyint_t *phyi;
21595 	ip_stack_t *ipst;
21596 	struct lifreq *lifr = if_req;
21597 
21598 	ASSERT(ipif != NULL);
21599 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21600 
21601 	if (q->q_next == NULL) {
21602 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21603 		return (EINVAL);
21604 	}
21605 
21606 	/*
21607 	 * If we are not writer on 'q' then this interface exists already
21608 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21609 	 * so return EALREADY.
21610 	 */
21611 	if (ill != ipif->ipif_ill)
21612 		return (EALREADY);
21613 
21614 	if (ill->ill_name[0] != '\0')
21615 		return (EALREADY);
21616 
21617 	/*
21618 	 * Set all the flags. Allows all kinds of override. Provide some
21619 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21620 	 * unless there is either multicast/broadcast support in the driver
21621 	 * or it is a pt-pt link.
21622 	 */
21623 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21624 		/* Meaningless to IP thus don't allow them to be set. */
21625 		ip1dbg(("ip_setname: EINVAL 1\n"));
21626 		return (EINVAL);
21627 	}
21628 
21629 	/*
21630 	 * If there's another ill already with the requested name, ensure
21631 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21632 	 * fuse together two unrelated ills, which will cause chaos.
21633 	 */
21634 	ipst = ill->ill_ipst;
21635 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21636 	    lifr->lifr_name, NULL);
21637 	if (phyi != NULL) {
21638 		ill_t *ill_mate = phyi->phyint_illv4;
21639 
21640 		if (ill_mate == NULL)
21641 			ill_mate = phyi->phyint_illv6;
21642 		ASSERT(ill_mate != NULL);
21643 
21644 		if (ill_mate->ill_media->ip_m_mac_type !=
21645 		    ill->ill_media->ip_m_mac_type) {
21646 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21647 			    "use the same ill name on differing media\n"));
21648 			return (EINVAL);
21649 		}
21650 	}
21651 
21652 	/*
21653 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21654 	 * ill_bcast_addr_length info.
21655 	 */
21656 	if (!ill->ill_needs_attach &&
21657 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21658 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21659 	    ill->ill_bcast_addr_length == 0)) {
21660 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21661 		ip1dbg(("ip_setname: EINVAL 2\n"));
21662 		return (EINVAL);
21663 	}
21664 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21665 	    ((lifr->lifr_flags & IFF_IPV6) ||
21666 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21667 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21668 		ip1dbg(("ip_setname: EINVAL 3\n"));
21669 		return (EINVAL);
21670 	}
21671 	if (lifr->lifr_flags & IFF_UP) {
21672 		/* Can only be set with SIOCSLIFFLAGS */
21673 		ip1dbg(("ip_setname: EINVAL 4\n"));
21674 		return (EINVAL);
21675 	}
21676 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21677 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21678 		ip1dbg(("ip_setname: EINVAL 5\n"));
21679 		return (EINVAL);
21680 	}
21681 	/*
21682 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21683 	 */
21684 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21685 	    !(lifr->lifr_flags & IFF_IPV6) &&
21686 	    !(ipif->ipif_isv6)) {
21687 		ip1dbg(("ip_setname: EINVAL 6\n"));
21688 		return (EINVAL);
21689 	}
21690 
21691 	/*
21692 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21693 	 * we have all the flags here. So, we assign rather than we OR.
21694 	 * We can't OR the flags here because we don't want to set
21695 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21696 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21697 	 * on lifr_flags value here.
21698 	 */
21699 	/*
21700 	 * This ill has not been inserted into the global list.
21701 	 * So we are still single threaded and don't need any lock
21702 	 */
21703 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21704 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21705 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21706 
21707 	/* We started off as V4. */
21708 	if (ill->ill_flags & ILLF_IPV6) {
21709 		ill->ill_phyint->phyint_illv6 = ill;
21710 		ill->ill_phyint->phyint_illv4 = NULL;
21711 	}
21712 
21713 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21714 }
21715 
21716 /* ARGSUSED */
21717 int
21718 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21719     ip_ioctl_cmd_t *ipip, void *if_req)
21720 {
21721 	/*
21722 	 * ill_phyint_reinit merged the v4 and v6 into a single
21723 	 * ipsq. Could also have become part of a ipmp group in the
21724 	 * process, and we might not have been able to complete the
21725 	 * slifname in ipif_set_values, if we could not become
21726 	 * exclusive.  If so restart it here
21727 	 */
21728 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21729 }
21730 
21731 /*
21732  * Return a pointer to the ipif which matches the index, IP version type and
21733  * zoneid.
21734  */
21735 ipif_t *
21736 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21737     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21738 {
21739 	ill_t	*ill;
21740 	ipif_t	*ipif = NULL;
21741 
21742 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21743 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21744 
21745 	if (err != NULL)
21746 		*err = 0;
21747 
21748 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21749 	if (ill != NULL) {
21750 		mutex_enter(&ill->ill_lock);
21751 		for (ipif = ill->ill_ipif; ipif != NULL;
21752 		    ipif = ipif->ipif_next) {
21753 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21754 			    zoneid == ipif->ipif_zoneid ||
21755 			    ipif->ipif_zoneid == ALL_ZONES)) {
21756 				ipif_refhold_locked(ipif);
21757 				break;
21758 			}
21759 		}
21760 		mutex_exit(&ill->ill_lock);
21761 		ill_refrele(ill);
21762 		if (ipif == NULL && err != NULL)
21763 			*err = ENXIO;
21764 	}
21765 	return (ipif);
21766 }
21767 
21768 typedef struct conn_change_s {
21769 	uint_t cc_old_ifindex;
21770 	uint_t cc_new_ifindex;
21771 } conn_change_t;
21772 
21773 /*
21774  * ipcl_walk function for changing interface index.
21775  */
21776 static void
21777 conn_change_ifindex(conn_t *connp, caddr_t arg)
21778 {
21779 	conn_change_t *connc;
21780 	uint_t old_ifindex;
21781 	uint_t new_ifindex;
21782 	int i;
21783 	ilg_t *ilg;
21784 
21785 	connc = (conn_change_t *)arg;
21786 	old_ifindex = connc->cc_old_ifindex;
21787 	new_ifindex = connc->cc_new_ifindex;
21788 
21789 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21790 		connp->conn_orig_bound_ifindex = new_ifindex;
21791 
21792 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21793 		connp->conn_orig_multicast_ifindex = new_ifindex;
21794 
21795 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21796 		ilg = &connp->conn_ilg[i];
21797 		if (ilg->ilg_orig_ifindex == old_ifindex)
21798 			ilg->ilg_orig_ifindex = new_ifindex;
21799 	}
21800 }
21801 
21802 /*
21803  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21804  * to new_index if it matches the old_index.
21805  *
21806  * Failovers typically happen within a group of ills. But somebody
21807  * can remove an ill from the group after a failover happened. If
21808  * we are setting the ifindex after this, we potentially need to
21809  * look at all the ills rather than just the ones in the group.
21810  * We cut down the work by looking at matching ill_net_types
21811  * and ill_types as we could not possibly grouped them together.
21812  */
21813 static void
21814 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21815 {
21816 	ill_t *ill;
21817 	ipif_t *ipif;
21818 	uint_t old_ifindex;
21819 	uint_t new_ifindex;
21820 	ilm_t *ilm;
21821 	ill_walk_context_t ctx;
21822 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21823 
21824 	old_ifindex = connc->cc_old_ifindex;
21825 	new_ifindex = connc->cc_new_ifindex;
21826 
21827 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21828 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21829 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21830 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21831 		    (ill_orig->ill_type != ill->ill_type)) {
21832 			continue;
21833 		}
21834 		for (ipif = ill->ill_ipif; ipif != NULL;
21835 		    ipif = ipif->ipif_next) {
21836 			if (ipif->ipif_orig_ifindex == old_ifindex)
21837 				ipif->ipif_orig_ifindex = new_ifindex;
21838 		}
21839 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21840 			if (ilm->ilm_orig_ifindex == old_ifindex)
21841 				ilm->ilm_orig_ifindex = new_ifindex;
21842 		}
21843 	}
21844 	rw_exit(&ipst->ips_ill_g_lock);
21845 }
21846 
21847 /*
21848  * We first need to ensure that the new index is unique, and
21849  * then carry the change across both v4 and v6 ill representation
21850  * of the physical interface.
21851  */
21852 /* ARGSUSED */
21853 int
21854 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21855     ip_ioctl_cmd_t *ipip, void *ifreq)
21856 {
21857 	ill_t		*ill;
21858 	ill_t		*ill_other;
21859 	phyint_t	*phyi;
21860 	int		old_index;
21861 	conn_change_t	connc;
21862 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21863 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21864 	uint_t	index;
21865 	ill_t	*ill_v4;
21866 	ill_t	*ill_v6;
21867 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21868 
21869 	if (ipip->ipi_cmd_type == IF_CMD)
21870 		index = ifr->ifr_index;
21871 	else
21872 		index = lifr->lifr_index;
21873 
21874 	/*
21875 	 * Only allow on physical interface. Also, index zero is illegal.
21876 	 *
21877 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21878 	 *
21879 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21880 	 *    implies a possible failback might have to happen. As failback
21881 	 *    depends on the old index, we should fail setting the index.
21882 	 *
21883 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21884 	 *    any addresses or multicast memberships are failed over to
21885 	 *    a non-STANDBY interface. As failback depends on the old
21886 	 *    index, we should fail setting the index for this case also.
21887 	 *
21888 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21889 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21890 	 */
21891 	ill = ipif->ipif_ill;
21892 	phyi = ill->ill_phyint;
21893 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21894 	    ipif->ipif_id != 0 || index == 0) {
21895 		return (EINVAL);
21896 	}
21897 	old_index = phyi->phyint_ifindex;
21898 
21899 	/* If the index is not changing, no work to do */
21900 	if (old_index == index)
21901 		return (0);
21902 
21903 	/*
21904 	 * Use ill_lookup_on_ifindex to determine if the
21905 	 * new index is unused and if so allow the change.
21906 	 */
21907 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21908 	    ipst);
21909 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21910 	    ipst);
21911 	if (ill_v6 != NULL || ill_v4 != NULL) {
21912 		if (ill_v4 != NULL)
21913 			ill_refrele(ill_v4);
21914 		if (ill_v6 != NULL)
21915 			ill_refrele(ill_v6);
21916 		return (EBUSY);
21917 	}
21918 
21919 	/*
21920 	 * The new index is unused. Set it in the phyint.
21921 	 * Locate the other ill so that we can send a routing
21922 	 * sockets message.
21923 	 */
21924 	if (ill->ill_isv6) {
21925 		ill_other = phyi->phyint_illv4;
21926 	} else {
21927 		ill_other = phyi->phyint_illv6;
21928 	}
21929 
21930 	phyi->phyint_ifindex = index;
21931 
21932 	/* Update SCTP's ILL list */
21933 	sctp_ill_reindex(ill, old_index);
21934 
21935 	connc.cc_old_ifindex = old_index;
21936 	connc.cc_new_ifindex = index;
21937 	ip_change_ifindex(ill, &connc);
21938 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21939 
21940 	/* Send the routing sockets message */
21941 	ip_rts_ifmsg(ipif);
21942 	if (ill_other != NULL)
21943 		ip_rts_ifmsg(ill_other->ill_ipif);
21944 
21945 	return (0);
21946 }
21947 
21948 /* ARGSUSED */
21949 int
21950 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21951     ip_ioctl_cmd_t *ipip, void *ifreq)
21952 {
21953 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21954 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21955 
21956 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21957 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21958 	/* Get the interface index */
21959 	if (ipip->ipi_cmd_type == IF_CMD) {
21960 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21961 	} else {
21962 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21963 	}
21964 	return (0);
21965 }
21966 
21967 /* ARGSUSED */
21968 int
21969 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21970     ip_ioctl_cmd_t *ipip, void *ifreq)
21971 {
21972 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21973 
21974 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21975 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21976 	/* Get the interface zone */
21977 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21978 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21979 	return (0);
21980 }
21981 
21982 /*
21983  * Set the zoneid of an interface.
21984  */
21985 /* ARGSUSED */
21986 int
21987 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21988     ip_ioctl_cmd_t *ipip, void *ifreq)
21989 {
21990 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21991 	int err = 0;
21992 	boolean_t need_up = B_FALSE;
21993 	zone_t *zptr;
21994 	zone_status_t status;
21995 	zoneid_t zoneid;
21996 
21997 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21998 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21999 		if (!is_system_labeled())
22000 			return (ENOTSUP);
22001 		zoneid = GLOBAL_ZONEID;
22002 	}
22003 
22004 	/* cannot assign instance zero to a non-global zone */
22005 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22006 		return (ENOTSUP);
22007 
22008 	/*
22009 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22010 	 * the event of a race with the zone shutdown processing, since IP
22011 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22012 	 * interface will be cleaned up even if the zone is shut down
22013 	 * immediately after the status check. If the interface can't be brought
22014 	 * down right away, and the zone is shut down before the restart
22015 	 * function is called, we resolve the possible races by rechecking the
22016 	 * zone status in the restart function.
22017 	 */
22018 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22019 		return (EINVAL);
22020 	status = zone_status_get(zptr);
22021 	zone_rele(zptr);
22022 
22023 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22024 		return (EINVAL);
22025 
22026 	if (ipif->ipif_flags & IPIF_UP) {
22027 		/*
22028 		 * If the interface is already marked up,
22029 		 * we call ipif_down which will take care
22030 		 * of ditching any IREs that have been set
22031 		 * up based on the old interface address.
22032 		 */
22033 		err = ipif_logical_down(ipif, q, mp);
22034 		if (err == EINPROGRESS)
22035 			return (err);
22036 		ipif_down_tail(ipif);
22037 		need_up = B_TRUE;
22038 	}
22039 
22040 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22041 	return (err);
22042 }
22043 
22044 static int
22045 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22046     queue_t *q, mblk_t *mp, boolean_t need_up)
22047 {
22048 	int	err = 0;
22049 	ip_stack_t	*ipst;
22050 
22051 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22052 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22053 
22054 	if (CONN_Q(q))
22055 		ipst = CONNQ_TO_IPST(q);
22056 	else
22057 		ipst = ILLQ_TO_IPST(q);
22058 
22059 	/*
22060 	 * For exclusive stacks we don't allow a different zoneid than
22061 	 * global.
22062 	 */
22063 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22064 	    zoneid != GLOBAL_ZONEID)
22065 		return (EINVAL);
22066 
22067 	/* Set the new zone id. */
22068 	ipif->ipif_zoneid = zoneid;
22069 
22070 	/* Update sctp list */
22071 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22072 
22073 	if (need_up) {
22074 		/*
22075 		 * Now bring the interface back up.  If this
22076 		 * is the only IPIF for the ILL, ipif_up
22077 		 * will have to re-bind to the device, so
22078 		 * we may get back EINPROGRESS, in which
22079 		 * case, this IOCTL will get completed in
22080 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22081 		 */
22082 		err = ipif_up(ipif, q, mp);
22083 	}
22084 	return (err);
22085 }
22086 
22087 /* ARGSUSED */
22088 int
22089 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22090     ip_ioctl_cmd_t *ipip, void *if_req)
22091 {
22092 	struct lifreq *lifr = (struct lifreq *)if_req;
22093 	zoneid_t zoneid;
22094 	zone_t *zptr;
22095 	zone_status_t status;
22096 
22097 	ASSERT(ipif->ipif_id != 0);
22098 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22099 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22100 		zoneid = GLOBAL_ZONEID;
22101 
22102 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22103 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22104 
22105 	/*
22106 	 * We recheck the zone status to resolve the following race condition:
22107 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22108 	 * 2) hme0:1 is up and can't be brought down right away;
22109 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22110 	 * 3) zone "myzone" is halted; the zone status switches to
22111 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22112 	 * the interfaces to remove - hme0:1 is not returned because it's not
22113 	 * yet in "myzone", so it won't be removed;
22114 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22115 	 * status check here, we would have hme0:1 in "myzone" after it's been
22116 	 * destroyed.
22117 	 * Note that if the status check fails, we need to bring the interface
22118 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22119 	 * ipif_up_done[_v6]().
22120 	 */
22121 	status = ZONE_IS_UNINITIALIZED;
22122 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22123 		status = zone_status_get(zptr);
22124 		zone_rele(zptr);
22125 	}
22126 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22127 		if (ipif->ipif_isv6) {
22128 			(void) ipif_up_done_v6(ipif);
22129 		} else {
22130 			(void) ipif_up_done(ipif);
22131 		}
22132 		return (EINVAL);
22133 	}
22134 
22135 	ipif_down_tail(ipif);
22136 
22137 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22138 	    B_TRUE));
22139 }
22140 
22141 /* ARGSUSED */
22142 int
22143 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22144 	ip_ioctl_cmd_t *ipip, void *ifreq)
22145 {
22146 	struct lifreq	*lifr = ifreq;
22147 
22148 	ASSERT(q->q_next == NULL);
22149 	ASSERT(CONN_Q(q));
22150 
22151 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22152 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22153 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22154 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22155 
22156 	return (0);
22157 }
22158 
22159 /* Find the previous ILL in this usesrc group */
22160 static ill_t *
22161 ill_prev_usesrc(ill_t *uill)
22162 {
22163 	ill_t *ill;
22164 
22165 	for (ill = uill->ill_usesrc_grp_next;
22166 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22167 	    ill = ill->ill_usesrc_grp_next)
22168 		/* do nothing */;
22169 	return (ill);
22170 }
22171 
22172 /*
22173  * Release all members of the usesrc group. This routine is called
22174  * from ill_delete when the interface being unplumbed is the
22175  * group head.
22176  */
22177 static void
22178 ill_disband_usesrc_group(ill_t *uill)
22179 {
22180 	ill_t *next_ill, *tmp_ill;
22181 	ip_stack_t	*ipst = uill->ill_ipst;
22182 
22183 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22184 	next_ill = uill->ill_usesrc_grp_next;
22185 
22186 	do {
22187 		ASSERT(next_ill != NULL);
22188 		tmp_ill = next_ill->ill_usesrc_grp_next;
22189 		ASSERT(tmp_ill != NULL);
22190 		next_ill->ill_usesrc_grp_next = NULL;
22191 		next_ill->ill_usesrc_ifindex = 0;
22192 		next_ill = tmp_ill;
22193 	} while (next_ill->ill_usesrc_ifindex != 0);
22194 	uill->ill_usesrc_grp_next = NULL;
22195 }
22196 
22197 /*
22198  * Remove the client usesrc ILL from the list and relink to a new list
22199  */
22200 int
22201 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22202 {
22203 	ill_t *ill, *tmp_ill;
22204 	ip_stack_t	*ipst = ucill->ill_ipst;
22205 
22206 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22207 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22208 
22209 	/*
22210 	 * Check if the usesrc client ILL passed in is not already
22211 	 * in use as a usesrc ILL i.e one whose source address is
22212 	 * in use OR a usesrc ILL is not already in use as a usesrc
22213 	 * client ILL
22214 	 */
22215 	if ((ucill->ill_usesrc_ifindex == 0) ||
22216 	    (uill->ill_usesrc_ifindex != 0)) {
22217 		return (-1);
22218 	}
22219 
22220 	ill = ill_prev_usesrc(ucill);
22221 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22222 
22223 	/* Remove from the current list */
22224 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22225 		/* Only two elements in the list */
22226 		ASSERT(ill->ill_usesrc_ifindex == 0);
22227 		ill->ill_usesrc_grp_next = NULL;
22228 	} else {
22229 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22230 	}
22231 
22232 	if (ifindex == 0) {
22233 		ucill->ill_usesrc_ifindex = 0;
22234 		ucill->ill_usesrc_grp_next = NULL;
22235 		return (0);
22236 	}
22237 
22238 	ucill->ill_usesrc_ifindex = ifindex;
22239 	tmp_ill = uill->ill_usesrc_grp_next;
22240 	uill->ill_usesrc_grp_next = ucill;
22241 	ucill->ill_usesrc_grp_next =
22242 	    (tmp_ill != NULL) ? tmp_ill : uill;
22243 	return (0);
22244 }
22245 
22246 /*
22247  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22248  * ip.c for locking details.
22249  */
22250 /* ARGSUSED */
22251 int
22252 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22253     ip_ioctl_cmd_t *ipip, void *ifreq)
22254 {
22255 	struct lifreq *lifr = (struct lifreq *)ifreq;
22256 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22257 	    ill_flag_changed = B_FALSE;
22258 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22259 	int err = 0, ret;
22260 	uint_t ifindex;
22261 	phyint_t *us_phyint, *us_cli_phyint;
22262 	ipsq_t *ipsq = NULL;
22263 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22264 
22265 	ASSERT(IAM_WRITER_IPIF(ipif));
22266 	ASSERT(q->q_next == NULL);
22267 	ASSERT(CONN_Q(q));
22268 
22269 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22270 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22271 
22272 	ASSERT(us_cli_phyint != NULL);
22273 
22274 	/*
22275 	 * If the client ILL is being used for IPMP, abort.
22276 	 * Note, this can be done before ipsq_try_enter since we are already
22277 	 * exclusive on this ILL
22278 	 */
22279 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22280 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22281 		return (EINVAL);
22282 	}
22283 
22284 	ifindex = lifr->lifr_index;
22285 	if (ifindex == 0) {
22286 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22287 			/* non usesrc group interface, nothing to reset */
22288 			return (0);
22289 		}
22290 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22291 		/* valid reset request */
22292 		reset_flg = B_TRUE;
22293 	}
22294 
22295 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22296 	    ip_process_ioctl, &err, ipst);
22297 
22298 	if (usesrc_ill == NULL) {
22299 		return (err);
22300 	}
22301 
22302 	/*
22303 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22304 	 * group nor can either of the interfaces be used for standy. So
22305 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22306 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22307 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22308 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22309 	 * the usesrc_cli_ill
22310 	 */
22311 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22312 	    NEW_OP, B_TRUE);
22313 	if (ipsq == NULL) {
22314 		err = EINPROGRESS;
22315 		/* Operation enqueued on the ipsq of the usesrc ILL */
22316 		goto done;
22317 	}
22318 
22319 	/* Check if the usesrc_ill is used for IPMP */
22320 	us_phyint = usesrc_ill->ill_phyint;
22321 	if ((us_phyint->phyint_groupname != NULL) ||
22322 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22323 		err = EINVAL;
22324 		goto done;
22325 	}
22326 
22327 	/*
22328 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22329 	 * already a client then return EINVAL
22330 	 */
22331 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22332 		err = EINVAL;
22333 		goto done;
22334 	}
22335 
22336 	/*
22337 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22338 	 * be then this is a duplicate operation.
22339 	 */
22340 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22341 		err = 0;
22342 		goto done;
22343 	}
22344 
22345 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22346 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22347 	    usesrc_ill->ill_isv6));
22348 
22349 	/*
22350 	 * The next step ensures that no new ires will be created referencing
22351 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22352 	 * we go through an ire walk deleting all ire caches that reference
22353 	 * the client ill. New ires referencing the client ill that are added
22354 	 * to the ire table before the ILL_CHANGING flag is set, will be
22355 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22356 	 * the client ill while the ILL_CHANGING flag is set will be failed
22357 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22358 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22359 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22360 	 * belong to the same usesrc group.
22361 	 */
22362 	mutex_enter(&usesrc_cli_ill->ill_lock);
22363 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22364 	mutex_exit(&usesrc_cli_ill->ill_lock);
22365 	ill_flag_changed = B_TRUE;
22366 
22367 	if (ipif->ipif_isv6)
22368 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22369 		    ALL_ZONES, ipst);
22370 	else
22371 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22372 		    ALL_ZONES, ipst);
22373 
22374 	/*
22375 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22376 	 * and the ill_usesrc_ifindex fields
22377 	 */
22378 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22379 
22380 	if (reset_flg) {
22381 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22382 		if (ret != 0) {
22383 			err = EINVAL;
22384 		}
22385 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22386 		goto done;
22387 	}
22388 
22389 	/*
22390 	 * Four possibilities to consider:
22391 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22392 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22393 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22394 	 * 4. Both are part of their respective usesrc groups
22395 	 */
22396 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22397 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22398 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22399 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22400 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22401 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22402 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22403 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22404 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22405 		/* Insert at head of list */
22406 		usesrc_cli_ill->ill_usesrc_grp_next =
22407 		    usesrc_ill->ill_usesrc_grp_next;
22408 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22409 	} else {
22410 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22411 		    ifindex);
22412 		if (ret != 0)
22413 			err = EINVAL;
22414 	}
22415 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22416 
22417 done:
22418 	if (ill_flag_changed) {
22419 		mutex_enter(&usesrc_cli_ill->ill_lock);
22420 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22421 		mutex_exit(&usesrc_cli_ill->ill_lock);
22422 	}
22423 	if (ipsq != NULL)
22424 		ipsq_exit(ipsq);
22425 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22426 	ill_refrele(usesrc_ill);
22427 	return (err);
22428 }
22429 
22430 /*
22431  * comparison function used by avl.
22432  */
22433 static int
22434 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22435 {
22436 
22437 	uint_t index;
22438 
22439 	ASSERT(phyip != NULL && index_ptr != NULL);
22440 
22441 	index = *((uint_t *)index_ptr);
22442 	/*
22443 	 * let the phyint with the lowest index be on top.
22444 	 */
22445 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22446 		return (1);
22447 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22448 		return (-1);
22449 	return (0);
22450 }
22451 
22452 /*
22453  * comparison function used by avl.
22454  */
22455 static int
22456 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22457 {
22458 	ill_t *ill;
22459 	int res = 0;
22460 
22461 	ASSERT(phyip != NULL && name_ptr != NULL);
22462 
22463 	if (((phyint_t *)phyip)->phyint_illv4)
22464 		ill = ((phyint_t *)phyip)->phyint_illv4;
22465 	else
22466 		ill = ((phyint_t *)phyip)->phyint_illv6;
22467 	ASSERT(ill != NULL);
22468 
22469 	res = strcmp(ill->ill_name, (char *)name_ptr);
22470 	if (res > 0)
22471 		return (1);
22472 	else if (res < 0)
22473 		return (-1);
22474 	return (0);
22475 }
22476 /*
22477  * This function is called from ill_delete when the ill is being
22478  * unplumbed. We remove the reference from the phyint and we also
22479  * free the phyint when there are no more references to it.
22480  */
22481 static void
22482 ill_phyint_free(ill_t *ill)
22483 {
22484 	phyint_t *phyi;
22485 	phyint_t *next_phyint;
22486 	ipsq_t *cur_ipsq;
22487 	ip_stack_t	*ipst = ill->ill_ipst;
22488 
22489 	ASSERT(ill->ill_phyint != NULL);
22490 
22491 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22492 	phyi = ill->ill_phyint;
22493 	ill->ill_phyint = NULL;
22494 	/*
22495 	 * ill_init allocates a phyint always to store the copy
22496 	 * of flags relevant to phyint. At that point in time, we could
22497 	 * not assign the name and hence phyint_illv4/v6 could not be
22498 	 * initialized. Later in ipif_set_values, we assign the name to
22499 	 * the ill, at which point in time we assign phyint_illv4/v6.
22500 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22501 	 */
22502 	if (ill->ill_flags & ILLF_IPV6) {
22503 		phyi->phyint_illv6 = NULL;
22504 	} else {
22505 		phyi->phyint_illv4 = NULL;
22506 	}
22507 	/*
22508 	 * ipif_down removes it from the group when the last ipif goes
22509 	 * down.
22510 	 */
22511 	ASSERT(ill->ill_group == NULL);
22512 
22513 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22514 		return;
22515 
22516 	/*
22517 	 * Make sure this phyint was put in the list.
22518 	 */
22519 	if (phyi->phyint_ifindex > 0) {
22520 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22521 		    phyi);
22522 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22523 		    phyi);
22524 	}
22525 	/*
22526 	 * remove phyint from the ipsq list.
22527 	 */
22528 	cur_ipsq = phyi->phyint_ipsq;
22529 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22530 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22531 	} else {
22532 		next_phyint = cur_ipsq->ipsq_phyint_list;
22533 		while (next_phyint != NULL) {
22534 			if (next_phyint->phyint_ipsq_next == phyi) {
22535 				next_phyint->phyint_ipsq_next =
22536 				    phyi->phyint_ipsq_next;
22537 				break;
22538 			}
22539 			next_phyint = next_phyint->phyint_ipsq_next;
22540 		}
22541 		ASSERT(next_phyint != NULL);
22542 	}
22543 	IPSQ_DEC_REF(cur_ipsq, ipst);
22544 
22545 	if (phyi->phyint_groupname_len != 0) {
22546 		ASSERT(phyi->phyint_groupname != NULL);
22547 		mi_free(phyi->phyint_groupname);
22548 	}
22549 	mi_free(phyi);
22550 }
22551 
22552 /*
22553  * Attach the ill to the phyint structure which can be shared by both
22554  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22555  * function is called from ipif_set_values and ill_lookup_on_name (for
22556  * loopback) where we know the name of the ill. We lookup the ill and if
22557  * there is one present already with the name use that phyint. Otherwise
22558  * reuse the one allocated by ill_init.
22559  */
22560 static void
22561 ill_phyint_reinit(ill_t *ill)
22562 {
22563 	boolean_t isv6 = ill->ill_isv6;
22564 	phyint_t *phyi_old;
22565 	phyint_t *phyi;
22566 	avl_index_t where = 0;
22567 	ill_t	*ill_other = NULL;
22568 	ipsq_t	*ipsq;
22569 	ip_stack_t	*ipst = ill->ill_ipst;
22570 
22571 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22572 
22573 	phyi_old = ill->ill_phyint;
22574 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22575 	    phyi_old->phyint_illv6 == NULL));
22576 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22577 	    phyi_old->phyint_illv4 == NULL));
22578 	ASSERT(phyi_old->phyint_ifindex == 0);
22579 
22580 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22581 	    ill->ill_name, &where);
22582 
22583 	/*
22584 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22585 	 *    the global list of ills. So no other thread could have located
22586 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22587 	 * 2. Now locate the other protocol instance of this ill.
22588 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22589 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22590 	 *    of neither ill can change.
22591 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22592 	 *    other ill.
22593 	 * 5. Release all locks.
22594 	 */
22595 
22596 	/*
22597 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22598 	 * we are initializing IPv4.
22599 	 */
22600 	if (phyi != NULL) {
22601 		ill_other = (isv6) ? phyi->phyint_illv4 :
22602 		    phyi->phyint_illv6;
22603 		ASSERT(ill_other->ill_phyint != NULL);
22604 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22605 		    (!isv6 && ill_other->ill_isv6));
22606 		GRAB_ILL_LOCKS(ill, ill_other);
22607 		/*
22608 		 * We are potentially throwing away phyint_flags which
22609 		 * could be different from the one that we obtain from
22610 		 * ill_other->ill_phyint. But it is okay as we are assuming
22611 		 * that the state maintained within IP is correct.
22612 		 */
22613 		mutex_enter(&phyi->phyint_lock);
22614 		if (isv6) {
22615 			ASSERT(phyi->phyint_illv6 == NULL);
22616 			phyi->phyint_illv6 = ill;
22617 		} else {
22618 			ASSERT(phyi->phyint_illv4 == NULL);
22619 			phyi->phyint_illv4 = ill;
22620 		}
22621 		/*
22622 		 * This is a new ill, currently undergoing SLIFNAME
22623 		 * So we could not have joined an IPMP group until now.
22624 		 */
22625 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22626 		    phyi_old->phyint_groupname == NULL);
22627 
22628 		/*
22629 		 * This phyi_old is going away. Decref ipsq_refs and
22630 		 * assert it is zero. The ipsq itself will be freed in
22631 		 * ipsq_exit
22632 		 */
22633 		ipsq = phyi_old->phyint_ipsq;
22634 		IPSQ_DEC_REF(ipsq, ipst);
22635 		ASSERT(ipsq->ipsq_refs == 0);
22636 		/* Get the singleton phyint out of the ipsq list */
22637 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22638 		ipsq->ipsq_phyint_list = NULL;
22639 		phyi_old->phyint_illv4 = NULL;
22640 		phyi_old->phyint_illv6 = NULL;
22641 		mi_free(phyi_old);
22642 	} else {
22643 		mutex_enter(&ill->ill_lock);
22644 		/*
22645 		 * We don't need to acquire any lock, since
22646 		 * the ill is not yet visible globally  and we
22647 		 * have not yet released the ill_g_lock.
22648 		 */
22649 		phyi = phyi_old;
22650 		mutex_enter(&phyi->phyint_lock);
22651 		/* XXX We need a recovery strategy here. */
22652 		if (!phyint_assign_ifindex(phyi, ipst))
22653 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22654 
22655 		/* No IPMP group yet, thus the hook uses the ifindex */
22656 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22657 
22658 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22659 		    (void *)phyi, where);
22660 
22661 		(void) avl_find(&ipst->ips_phyint_g_list->
22662 		    phyint_list_avl_by_index,
22663 		    &phyi->phyint_ifindex, &where);
22664 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22665 		    (void *)phyi, where);
22666 	}
22667 
22668 	/*
22669 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22670 	 * pending mp is not affected because that is per ill basis.
22671 	 */
22672 	ill->ill_phyint = phyi;
22673 
22674 	/*
22675 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22676 	 * We do this here as when the first ipif was allocated,
22677 	 * ipif_allocate does not know the right interface index.
22678 	 */
22679 
22680 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22681 	/*
22682 	 * Now that the phyint's ifindex has been assigned, complete the
22683 	 * remaining
22684 	 */
22685 
22686 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22687 	if (ill->ill_isv6) {
22688 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22689 		    ill->ill_phyint->phyint_ifindex;
22690 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22691 	} else {
22692 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22693 	}
22694 
22695 	/*
22696 	 * Generate an event within the hooks framework to indicate that
22697 	 * a new interface has just been added to IP.  For this event to
22698 	 * be generated, the network interface must, at least, have an
22699 	 * ifindex assigned to it.
22700 	 *
22701 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22702 	 * that the ordering of delivered events to listeners matches the
22703 	 * order of them in the kernel.
22704 	 *
22705 	 * This function could be called from ill_lookup_on_name. In that case
22706 	 * the interface is loopback "lo", which will not generate a NIC event.
22707 	 */
22708 	if (ill->ill_name_length <= 2 ||
22709 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22710 		/*
22711 		 * Generate nic plumb event for ill_name even if
22712 		 * ipmp_hook_emulation is set. That avoids generating events
22713 		 * for the ill_names should ipmp_hook_emulation be turned on
22714 		 * later.
22715 		 */
22716 		ill_nic_info_plumb(ill, B_FALSE);
22717 	}
22718 	RELEASE_ILL_LOCKS(ill, ill_other);
22719 	mutex_exit(&phyi->phyint_lock);
22720 }
22721 
22722 /*
22723  * Allocate a NE_PLUMB nic info event and store in the ill.
22724  * If 'group' is set we do it for the group name, otherwise the ill name.
22725  * It will be sent when we leave the ipsq.
22726  */
22727 void
22728 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22729 {
22730 	phyint_t	*phyi = ill->ill_phyint;
22731 	char		*name;
22732 	int		namelen;
22733 
22734 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22735 
22736 	if (group) {
22737 		ASSERT(phyi->phyint_groupname_len != 0);
22738 		namelen = phyi->phyint_groupname_len;
22739 		name = phyi->phyint_groupname;
22740 	} else {
22741 		namelen = ill->ill_name_length;
22742 		name = ill->ill_name;
22743 	}
22744 
22745 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22746 }
22747 
22748 /*
22749  * Unhook the nic event message from the ill and enqueue it
22750  * into the nic event taskq.
22751  */
22752 void
22753 ill_nic_info_dispatch(ill_t *ill)
22754 {
22755 	hook_nic_event_int_t *info;
22756 
22757 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22758 
22759 	if ((info = ill->ill_nic_event_info) != NULL) {
22760 		if (ddi_taskq_dispatch(eventq_queue_nic,
22761 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22762 			ip2dbg(("ill_nic_info_dispatch: "
22763 			    "ddi_taskq_dispatch failed\n"));
22764 			if (info->hnei_event.hne_data != NULL) {
22765 				kmem_free(info->hnei_event.hne_data,
22766 				    info->hnei_event.hne_datalen);
22767 			}
22768 			kmem_free(info, sizeof (*info));
22769 		}
22770 		ill->ill_nic_event_info = NULL;
22771 	}
22772 }
22773 
22774 /*
22775  * Notify any downstream modules of the name of this interface.
22776  * An M_IOCTL is used even though we don't expect a successful reply.
22777  * Any reply message from the driver (presumably an M_IOCNAK) will
22778  * eventually get discarded somewhere upstream.  The message format is
22779  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22780  * to IP.
22781  */
22782 static void
22783 ip_ifname_notify(ill_t *ill, queue_t *q)
22784 {
22785 	mblk_t *mp1, *mp2;
22786 	struct iocblk *iocp;
22787 	struct lifreq *lifr;
22788 
22789 	mp1 = mkiocb(SIOCSLIFNAME);
22790 	if (mp1 == NULL)
22791 		return;
22792 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22793 	if (mp2 == NULL) {
22794 		freeb(mp1);
22795 		return;
22796 	}
22797 
22798 	mp1->b_cont = mp2;
22799 	iocp = (struct iocblk *)mp1->b_rptr;
22800 	iocp->ioc_count = sizeof (struct lifreq);
22801 
22802 	lifr = (struct lifreq *)mp2->b_rptr;
22803 	mp2->b_wptr += sizeof (struct lifreq);
22804 	bzero(lifr, sizeof (struct lifreq));
22805 
22806 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22807 	lifr->lifr_ppa = ill->ill_ppa;
22808 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22809 
22810 	putnext(q, mp1);
22811 }
22812 
22813 static int
22814 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22815 {
22816 	int err;
22817 	ip_stack_t	*ipst = ill->ill_ipst;
22818 
22819 	/* Set the obsolete NDD per-interface forwarding name. */
22820 	err = ill_set_ndd_name(ill);
22821 	if (err != 0) {
22822 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22823 		    err);
22824 	}
22825 
22826 	/* Tell downstream modules where they are. */
22827 	ip_ifname_notify(ill, q);
22828 
22829 	/*
22830 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22831 	 * Error cases are ENOMEM ...
22832 	 */
22833 	err = ill_dl_phys(ill, ipif, mp, q);
22834 
22835 	/*
22836 	 * If there is no IRE expiration timer running, get one started.
22837 	 * igmp and mld timers will be triggered by the first multicast
22838 	 */
22839 	if (ipst->ips_ip_ire_expire_id == 0) {
22840 		/*
22841 		 * acquire the lock and check again.
22842 		 */
22843 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22844 		if (ipst->ips_ip_ire_expire_id == 0) {
22845 			ipst->ips_ip_ire_expire_id = timeout(
22846 			    ip_trash_timer_expire, ipst,
22847 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22848 		}
22849 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22850 	}
22851 
22852 	if (ill->ill_isv6) {
22853 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22854 		if (ipst->ips_mld_slowtimeout_id == 0) {
22855 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22856 			    (void *)ipst,
22857 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22858 		}
22859 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22860 	} else {
22861 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22862 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22863 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22864 			    (void *)ipst,
22865 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22866 		}
22867 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22868 	}
22869 
22870 	return (err);
22871 }
22872 
22873 /*
22874  * Common routine for ppa and ifname setting. Should be called exclusive.
22875  *
22876  * Returns EINPROGRESS when mp has been consumed by queueing it on
22877  * ill_pending_mp and the ioctl will complete in ip_rput.
22878  *
22879  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22880  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22881  * For SLIFNAME, we pass these values back to the userland.
22882  */
22883 static int
22884 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22885 {
22886 	ill_t	*ill;
22887 	ipif_t	*ipif;
22888 	ipsq_t	*ipsq;
22889 	char	*ppa_ptr;
22890 	char	*old_ptr;
22891 	char	old_char;
22892 	int	error;
22893 	ip_stack_t	*ipst;
22894 
22895 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22896 	ASSERT(q->q_next != NULL);
22897 	ASSERT(interf_name != NULL);
22898 
22899 	ill = (ill_t *)q->q_ptr;
22900 	ipst = ill->ill_ipst;
22901 
22902 	ASSERT(ill->ill_ipst != NULL);
22903 	ASSERT(ill->ill_name[0] == '\0');
22904 	ASSERT(IAM_WRITER_ILL(ill));
22905 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22906 	ASSERT(ill->ill_ppa == UINT_MAX);
22907 
22908 	/* The ppa is sent down by ifconfig or is chosen */
22909 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22910 		return (EINVAL);
22911 	}
22912 
22913 	/*
22914 	 * make sure ppa passed in is same as ppa in the name.
22915 	 * This check is not made when ppa == UINT_MAX in that case ppa
22916 	 * in the name could be anything. System will choose a ppa and
22917 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22918 	 */
22919 	if (*new_ppa_ptr != UINT_MAX) {
22920 		/* stoi changes the pointer */
22921 		old_ptr = ppa_ptr;
22922 		/*
22923 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22924 		 * (they don't have an externally visible ppa).  We assign one
22925 		 * here so that we can manage the interface.  Note that in
22926 		 * the past this value was always 0 for DLPI 1 drivers.
22927 		 */
22928 		if (*new_ppa_ptr == 0)
22929 			*new_ppa_ptr = stoi(&old_ptr);
22930 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22931 			return (EINVAL);
22932 	}
22933 	/*
22934 	 * terminate string before ppa
22935 	 * save char at that location.
22936 	 */
22937 	old_char = ppa_ptr[0];
22938 	ppa_ptr[0] = '\0';
22939 
22940 	ill->ill_ppa = *new_ppa_ptr;
22941 	/*
22942 	 * Finish as much work now as possible before calling ill_glist_insert
22943 	 * which makes the ill globally visible and also merges it with the
22944 	 * other protocol instance of this phyint. The remaining work is
22945 	 * done after entering the ipsq which may happen sometime later.
22946 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22947 	 */
22948 	ipif = ill->ill_ipif;
22949 
22950 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22951 	ipif_assign_seqid(ipif);
22952 
22953 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22954 		ill->ill_flags |= ILLF_IPV4;
22955 
22956 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22957 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22958 
22959 	if (ill->ill_flags & ILLF_IPV6) {
22960 
22961 		ill->ill_isv6 = B_TRUE;
22962 		if (ill->ill_rq != NULL) {
22963 			ill->ill_rq->q_qinfo = &iprinitv6;
22964 			ill->ill_wq->q_qinfo = &ipwinitv6;
22965 		}
22966 
22967 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22968 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22969 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22970 		ipif->ipif_v6subnet = ipv6_all_zeros;
22971 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22972 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22973 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22974 		/*
22975 		 * point-to-point or Non-mulicast capable
22976 		 * interfaces won't do NUD unless explicitly
22977 		 * configured to do so.
22978 		 */
22979 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22980 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22981 			ill->ill_flags |= ILLF_NONUD;
22982 		}
22983 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22984 		if (ill->ill_flags & ILLF_NOARP) {
22985 			/*
22986 			 * Note: xresolv interfaces will eventually need
22987 			 * NOARP set here as well, but that will require
22988 			 * those external resolvers to have some
22989 			 * knowledge of that flag and act appropriately.
22990 			 * Not to be changed at present.
22991 			 */
22992 			ill->ill_flags &= ~ILLF_NOARP;
22993 		}
22994 		/*
22995 		 * Set the ILLF_ROUTER flag according to the global
22996 		 * IPv6 forwarding policy.
22997 		 */
22998 		if (ipst->ips_ipv6_forward != 0)
22999 			ill->ill_flags |= ILLF_ROUTER;
23000 	} else if (ill->ill_flags & ILLF_IPV4) {
23001 		ill->ill_isv6 = B_FALSE;
23002 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23003 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23004 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23005 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23006 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23007 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23008 		/*
23009 		 * Set the ILLF_ROUTER flag according to the global
23010 		 * IPv4 forwarding policy.
23011 		 */
23012 		if (ipst->ips_ip_g_forward != 0)
23013 			ill->ill_flags |= ILLF_ROUTER;
23014 	}
23015 
23016 	ASSERT(ill->ill_phyint != NULL);
23017 
23018 	/*
23019 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23020 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23021 	 */
23022 	if (!ill_allocate_mibs(ill))
23023 		return (ENOMEM);
23024 
23025 	/*
23026 	 * Pick a default sap until we get the DL_INFO_ACK back from
23027 	 * the driver.
23028 	 */
23029 	if (ill->ill_sap == 0) {
23030 		if (ill->ill_isv6)
23031 			ill->ill_sap  = IP6_DL_SAP;
23032 		else
23033 			ill->ill_sap  = IP_DL_SAP;
23034 	}
23035 
23036 	ill->ill_ifname_pending = 1;
23037 	ill->ill_ifname_pending_err = 0;
23038 
23039 	ill_refhold(ill);
23040 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23041 	if ((error = ill_glist_insert(ill, interf_name,
23042 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23043 		ill->ill_ppa = UINT_MAX;
23044 		ill->ill_name[0] = '\0';
23045 		/*
23046 		 * undo null termination done above.
23047 		 */
23048 		ppa_ptr[0] = old_char;
23049 		rw_exit(&ipst->ips_ill_g_lock);
23050 		ill_refrele(ill);
23051 		return (error);
23052 	}
23053 
23054 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23055 
23056 	/*
23057 	 * When we return the buffer pointed to by interf_name should contain
23058 	 * the same name as in ill_name.
23059 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23060 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23061 	 * so copy full name and update the ppa ptr.
23062 	 * When ppa passed in != UINT_MAX all values are correct just undo
23063 	 * null termination, this saves a bcopy.
23064 	 */
23065 	if (*new_ppa_ptr == UINT_MAX) {
23066 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23067 		*new_ppa_ptr = ill->ill_ppa;
23068 	} else {
23069 		/*
23070 		 * undo null termination done above.
23071 		 */
23072 		ppa_ptr[0] = old_char;
23073 	}
23074 
23075 	/* Let SCTP know about this ILL */
23076 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23077 
23078 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23079 	    B_TRUE);
23080 
23081 	rw_exit(&ipst->ips_ill_g_lock);
23082 	ill_refrele(ill);
23083 	if (ipsq == NULL)
23084 		return (EINPROGRESS);
23085 
23086 	/*
23087 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23088 	 */
23089 	if (ipsq->ipsq_current_ipif == NULL)
23090 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23091 	else
23092 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23093 
23094 	error = ipif_set_values_tail(ill, ipif, mp, q);
23095 	ipsq_exit(ipsq);
23096 	if (error != 0 && error != EINPROGRESS) {
23097 		/*
23098 		 * restore previous values
23099 		 */
23100 		ill->ill_isv6 = B_FALSE;
23101 	}
23102 	return (error);
23103 }
23104 
23105 
23106 void
23107 ipif_init(ip_stack_t *ipst)
23108 {
23109 	hrtime_t hrt;
23110 	int i;
23111 
23112 	/*
23113 	 * Can't call drv_getparm here as it is too early in the boot.
23114 	 * As we use ipif_src_random just for picking a different
23115 	 * source address everytime, this need not be really random.
23116 	 */
23117 	hrt = gethrtime();
23118 	ipst->ips_ipif_src_random =
23119 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23120 
23121 	for (i = 0; i < MAX_G_HEADS; i++) {
23122 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23123 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23124 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23125 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23126 	}
23127 
23128 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23129 	    ill_phyint_compare_index,
23130 	    sizeof (phyint_t),
23131 	    offsetof(struct phyint, phyint_avl_by_index));
23132 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23133 	    ill_phyint_compare_name,
23134 	    sizeof (phyint_t),
23135 	    offsetof(struct phyint, phyint_avl_by_name));
23136 }
23137 
23138 /*
23139  * Lookup the ipif corresponding to the onlink destination address. For
23140  * point-to-point interfaces, it matches with remote endpoint destination
23141  * address. For point-to-multipoint interfaces it only tries to match the
23142  * destination with the interface's subnet address. The longest, most specific
23143  * match is found to take care of such rare network configurations like -
23144  * le0: 129.146.1.1/16
23145  * le1: 129.146.2.2/24
23146  * It is used only by SO_DONTROUTE at the moment.
23147  */
23148 ipif_t *
23149 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23150 {
23151 	ipif_t	*ipif, *best_ipif;
23152 	ill_t	*ill;
23153 	ill_walk_context_t ctx;
23154 
23155 	ASSERT(zoneid != ALL_ZONES);
23156 	best_ipif = NULL;
23157 
23158 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23159 	ill = ILL_START_WALK_V4(&ctx, ipst);
23160 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23161 		mutex_enter(&ill->ill_lock);
23162 		for (ipif = ill->ill_ipif; ipif != NULL;
23163 		    ipif = ipif->ipif_next) {
23164 			if (!IPIF_CAN_LOOKUP(ipif))
23165 				continue;
23166 			if (ipif->ipif_zoneid != zoneid &&
23167 			    ipif->ipif_zoneid != ALL_ZONES)
23168 				continue;
23169 			/*
23170 			 * Point-to-point case. Look for exact match with
23171 			 * destination address.
23172 			 */
23173 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23174 				if (ipif->ipif_pp_dst_addr == addr) {
23175 					ipif_refhold_locked(ipif);
23176 					mutex_exit(&ill->ill_lock);
23177 					rw_exit(&ipst->ips_ill_g_lock);
23178 					if (best_ipif != NULL)
23179 						ipif_refrele(best_ipif);
23180 					return (ipif);
23181 				}
23182 			} else if (ipif->ipif_subnet == (addr &
23183 			    ipif->ipif_net_mask)) {
23184 				/*
23185 				 * Point-to-multipoint case. Looping through to
23186 				 * find the most specific match. If there are
23187 				 * multiple best match ipif's then prefer ipif's
23188 				 * that are UP. If there is only one best match
23189 				 * ipif and it is DOWN we must still return it.
23190 				 */
23191 				if ((best_ipif == NULL) ||
23192 				    (ipif->ipif_net_mask >
23193 				    best_ipif->ipif_net_mask) ||
23194 				    ((ipif->ipif_net_mask ==
23195 				    best_ipif->ipif_net_mask) &&
23196 				    ((ipif->ipif_flags & IPIF_UP) &&
23197 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23198 					ipif_refhold_locked(ipif);
23199 					mutex_exit(&ill->ill_lock);
23200 					rw_exit(&ipst->ips_ill_g_lock);
23201 					if (best_ipif != NULL)
23202 						ipif_refrele(best_ipif);
23203 					best_ipif = ipif;
23204 					rw_enter(&ipst->ips_ill_g_lock,
23205 					    RW_READER);
23206 					mutex_enter(&ill->ill_lock);
23207 				}
23208 			}
23209 		}
23210 		mutex_exit(&ill->ill_lock);
23211 	}
23212 	rw_exit(&ipst->ips_ill_g_lock);
23213 	return (best_ipif);
23214 }
23215 
23216 /*
23217  * Save enough information so that we can recreate the IRE if
23218  * the interface goes down and then up.
23219  */
23220 static void
23221 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23222 {
23223 	mblk_t	*save_mp;
23224 
23225 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23226 	if (save_mp != NULL) {
23227 		ifrt_t	*ifrt;
23228 
23229 		save_mp->b_wptr += sizeof (ifrt_t);
23230 		ifrt = (ifrt_t *)save_mp->b_rptr;
23231 		bzero(ifrt, sizeof (ifrt_t));
23232 		ifrt->ifrt_type = ire->ire_type;
23233 		ifrt->ifrt_addr = ire->ire_addr;
23234 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23235 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23236 		ifrt->ifrt_mask = ire->ire_mask;
23237 		ifrt->ifrt_flags = ire->ire_flags;
23238 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23239 		mutex_enter(&ipif->ipif_saved_ire_lock);
23240 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23241 		ipif->ipif_saved_ire_mp = save_mp;
23242 		ipif->ipif_saved_ire_cnt++;
23243 		mutex_exit(&ipif->ipif_saved_ire_lock);
23244 	}
23245 }
23246 
23247 static void
23248 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23249 {
23250 	mblk_t	**mpp;
23251 	mblk_t	*mp;
23252 	ifrt_t	*ifrt;
23253 
23254 	/* Remove from ipif_saved_ire_mp list if it is there */
23255 	mutex_enter(&ipif->ipif_saved_ire_lock);
23256 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23257 	    mpp = &(*mpp)->b_cont) {
23258 		/*
23259 		 * On a given ipif, the triple of address, gateway and
23260 		 * mask is unique for each saved IRE (in the case of
23261 		 * ordinary interface routes, the gateway address is
23262 		 * all-zeroes).
23263 		 */
23264 		mp = *mpp;
23265 		ifrt = (ifrt_t *)mp->b_rptr;
23266 		if (ifrt->ifrt_addr == ire->ire_addr &&
23267 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23268 		    ifrt->ifrt_mask == ire->ire_mask) {
23269 			*mpp = mp->b_cont;
23270 			ipif->ipif_saved_ire_cnt--;
23271 			freeb(mp);
23272 			break;
23273 		}
23274 	}
23275 	mutex_exit(&ipif->ipif_saved_ire_lock);
23276 }
23277 
23278 /*
23279  * IP multirouting broadcast routes handling
23280  * Append CGTP broadcast IREs to regular ones created
23281  * at ifconfig time.
23282  */
23283 static void
23284 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23285 {
23286 	ire_t *ire_prim;
23287 
23288 	ASSERT(ire != NULL);
23289 	ASSERT(ire_dst != NULL);
23290 
23291 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23292 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23293 	if (ire_prim != NULL) {
23294 		/*
23295 		 * We are in the special case of broadcasts for
23296 		 * CGTP. We add an IRE_BROADCAST that holds
23297 		 * the RTF_MULTIRT flag, the destination
23298 		 * address of ire_dst and the low level
23299 		 * info of ire_prim. In other words, CGTP
23300 		 * broadcast is added to the redundant ipif.
23301 		 */
23302 		ipif_t *ipif_prim;
23303 		ire_t  *bcast_ire;
23304 
23305 		ipif_prim = ire_prim->ire_ipif;
23306 
23307 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23308 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23309 		    (void *)ire_dst, (void *)ire_prim,
23310 		    (void *)ipif_prim));
23311 
23312 		bcast_ire = ire_create(
23313 		    (uchar_t *)&ire->ire_addr,
23314 		    (uchar_t *)&ip_g_all_ones,
23315 		    (uchar_t *)&ire_dst->ire_src_addr,
23316 		    (uchar_t *)&ire->ire_gateway_addr,
23317 		    &ipif_prim->ipif_mtu,
23318 		    NULL,
23319 		    ipif_prim->ipif_rq,
23320 		    ipif_prim->ipif_wq,
23321 		    IRE_BROADCAST,
23322 		    ipif_prim,
23323 		    0,
23324 		    0,
23325 		    0,
23326 		    ire->ire_flags,
23327 		    &ire_uinfo_null,
23328 		    NULL,
23329 		    NULL,
23330 		    ipst);
23331 
23332 		if (bcast_ire != NULL) {
23333 
23334 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23335 			    B_FALSE) == 0) {
23336 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23337 				    "added bcast_ire %p\n",
23338 				    (void *)bcast_ire));
23339 
23340 				ipif_save_ire(bcast_ire->ire_ipif,
23341 				    bcast_ire);
23342 				ire_refrele(bcast_ire);
23343 			}
23344 		}
23345 		ire_refrele(ire_prim);
23346 	}
23347 }
23348 
23349 
23350 /*
23351  * IP multirouting broadcast routes handling
23352  * Remove the broadcast ire
23353  */
23354 static void
23355 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23356 {
23357 	ire_t *ire_dst;
23358 
23359 	ASSERT(ire != NULL);
23360 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23361 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23362 	if (ire_dst != NULL) {
23363 		ire_t *ire_prim;
23364 
23365 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23366 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23367 		if (ire_prim != NULL) {
23368 			ipif_t *ipif_prim;
23369 			ire_t  *bcast_ire;
23370 
23371 			ipif_prim = ire_prim->ire_ipif;
23372 
23373 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23374 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23375 			    (void *)ire_dst, (void *)ire_prim,
23376 			    (void *)ipif_prim));
23377 
23378 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23379 			    ire->ire_gateway_addr,
23380 			    IRE_BROADCAST,
23381 			    ipif_prim, ALL_ZONES,
23382 			    NULL,
23383 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23384 			    MATCH_IRE_MASK, ipst);
23385 
23386 			if (bcast_ire != NULL) {
23387 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23388 				    "looked up bcast_ire %p\n",
23389 				    (void *)bcast_ire));
23390 				ipif_remove_ire(bcast_ire->ire_ipif,
23391 				    bcast_ire);
23392 				ire_delete(bcast_ire);
23393 				ire_refrele(bcast_ire);
23394 			}
23395 			ire_refrele(ire_prim);
23396 		}
23397 		ire_refrele(ire_dst);
23398 	}
23399 }
23400 
23401 /*
23402  * IPsec hardware acceleration capabilities related functions.
23403  */
23404 
23405 /*
23406  * Free a per-ill IPsec capabilities structure.
23407  */
23408 static void
23409 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23410 {
23411 	if (capab->auth_hw_algs != NULL)
23412 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23413 	if (capab->encr_hw_algs != NULL)
23414 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23415 	if (capab->encr_algparm != NULL)
23416 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23417 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23418 }
23419 
23420 /*
23421  * Allocate a new per-ill IPsec capabilities structure. This structure
23422  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23423  * an array which specifies, for each algorithm, whether this algorithm
23424  * is supported by the ill or not.
23425  */
23426 static ill_ipsec_capab_t *
23427 ill_ipsec_capab_alloc(void)
23428 {
23429 	ill_ipsec_capab_t *capab;
23430 	uint_t nelems;
23431 
23432 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23433 	if (capab == NULL)
23434 		return (NULL);
23435 
23436 	/* we need one bit per algorithm */
23437 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23438 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23439 
23440 	/* allocate memory to store algorithm flags */
23441 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23442 	if (capab->encr_hw_algs == NULL)
23443 		goto nomem;
23444 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23445 	if (capab->auth_hw_algs == NULL)
23446 		goto nomem;
23447 	/*
23448 	 * Leave encr_algparm NULL for now since we won't need it half
23449 	 * the time
23450 	 */
23451 	return (capab);
23452 
23453 nomem:
23454 	ill_ipsec_capab_free(capab);
23455 	return (NULL);
23456 }
23457 
23458 /*
23459  * Resize capability array.  Since we're exclusive, this is OK.
23460  */
23461 static boolean_t
23462 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23463 {
23464 	ipsec_capab_algparm_t *nalp, *oalp;
23465 	uint32_t olen, nlen;
23466 
23467 	oalp = capab->encr_algparm;
23468 	olen = capab->encr_algparm_size;
23469 
23470 	if (oalp != NULL) {
23471 		if (algid < capab->encr_algparm_end)
23472 			return (B_TRUE);
23473 	}
23474 
23475 	nlen = (algid + 1) * sizeof (*nalp);
23476 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23477 	if (nalp == NULL)
23478 		return (B_FALSE);
23479 
23480 	if (oalp != NULL) {
23481 		bcopy(oalp, nalp, olen);
23482 		kmem_free(oalp, olen);
23483 	}
23484 	capab->encr_algparm = nalp;
23485 	capab->encr_algparm_size = nlen;
23486 	capab->encr_algparm_end = algid + 1;
23487 
23488 	return (B_TRUE);
23489 }
23490 
23491 /*
23492  * Compare the capabilities of the specified ill with the protocol
23493  * and algorithms specified by the SA passed as argument.
23494  * If they match, returns B_TRUE, B_FALSE if they do not match.
23495  *
23496  * The ill can be passed as a pointer to it, or by specifying its index
23497  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23498  *
23499  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23500  * packet is eligible for hardware acceleration, and by
23501  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23502  * to a particular ill.
23503  */
23504 boolean_t
23505 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23506     ipsa_t *sa, netstack_t *ns)
23507 {
23508 	boolean_t sa_isv6;
23509 	uint_t algid;
23510 	struct ill_ipsec_capab_s *cpp;
23511 	boolean_t need_refrele = B_FALSE;
23512 	ip_stack_t	*ipst = ns->netstack_ip;
23513 
23514 	if (ill == NULL) {
23515 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23516 		    NULL, NULL, NULL, ipst);
23517 		if (ill == NULL) {
23518 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23519 			return (B_FALSE);
23520 		}
23521 		need_refrele = B_TRUE;
23522 	}
23523 
23524 	/*
23525 	 * Use the address length specified by the SA to determine
23526 	 * if it corresponds to a IPv6 address, and fail the matching
23527 	 * if the isv6 flag passed as argument does not match.
23528 	 * Note: this check is used for SADB capability checking before
23529 	 * sending SA information to an ill.
23530 	 */
23531 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23532 	if (sa_isv6 != ill_isv6)
23533 		/* protocol mismatch */
23534 		goto done;
23535 
23536 	/*
23537 	 * Check if the ill supports the protocol, algorithm(s) and
23538 	 * key size(s) specified by the SA, and get the pointers to
23539 	 * the algorithms supported by the ill.
23540 	 */
23541 	switch (sa->ipsa_type) {
23542 
23543 	case SADB_SATYPE_ESP:
23544 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23545 			/* ill does not support ESP acceleration */
23546 			goto done;
23547 		cpp = ill->ill_ipsec_capab_esp;
23548 		algid = sa->ipsa_auth_alg;
23549 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23550 			goto done;
23551 		algid = sa->ipsa_encr_alg;
23552 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23553 			goto done;
23554 		if (algid < cpp->encr_algparm_end) {
23555 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23556 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23557 				goto done;
23558 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23559 				goto done;
23560 		}
23561 		break;
23562 
23563 	case SADB_SATYPE_AH:
23564 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23565 			/* ill does not support AH acceleration */
23566 			goto done;
23567 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23568 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23569 			goto done;
23570 		break;
23571 	}
23572 
23573 	if (need_refrele)
23574 		ill_refrele(ill);
23575 	return (B_TRUE);
23576 done:
23577 	if (need_refrele)
23578 		ill_refrele(ill);
23579 	return (B_FALSE);
23580 }
23581 
23582 /*
23583  * Add a new ill to the list of IPsec capable ills.
23584  * Called from ill_capability_ipsec_ack() when an ACK was received
23585  * indicating that IPsec hardware processing was enabled for an ill.
23586  *
23587  * ill must point to the ill for which acceleration was enabled.
23588  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23589  */
23590 static void
23591 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23592 {
23593 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23594 	uint_t sa_type;
23595 	uint_t ipproto;
23596 	ip_stack_t	*ipst = ill->ill_ipst;
23597 
23598 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23599 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23600 
23601 	switch (dl_cap) {
23602 	case DL_CAPAB_IPSEC_AH:
23603 		sa_type = SADB_SATYPE_AH;
23604 		ills = &ipst->ips_ipsec_capab_ills_ah;
23605 		ipproto = IPPROTO_AH;
23606 		break;
23607 	case DL_CAPAB_IPSEC_ESP:
23608 		sa_type = SADB_SATYPE_ESP;
23609 		ills = &ipst->ips_ipsec_capab_ills_esp;
23610 		ipproto = IPPROTO_ESP;
23611 		break;
23612 	}
23613 
23614 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23615 
23616 	/*
23617 	 * Add ill index to list of hardware accelerators. If
23618 	 * already in list, do nothing.
23619 	 */
23620 	for (cur_ill = *ills; cur_ill != NULL &&
23621 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23622 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23623 		;
23624 
23625 	if (cur_ill == NULL) {
23626 		/* if this is a new entry for this ill */
23627 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23628 		if (new_ill == NULL) {
23629 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23630 			return;
23631 		}
23632 
23633 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23634 		new_ill->ill_isv6 = ill->ill_isv6;
23635 		new_ill->next = *ills;
23636 		*ills = new_ill;
23637 	} else if (!sadb_resync) {
23638 		/* not resync'ing SADB and an entry exists for this ill */
23639 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23640 		return;
23641 	}
23642 
23643 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23644 
23645 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23646 		/*
23647 		 * IPsec module for protocol loaded, initiate dump
23648 		 * of the SADB to this ill.
23649 		 */
23650 		sadb_ill_download(ill, sa_type);
23651 }
23652 
23653 /*
23654  * Remove an ill from the list of IPsec capable ills.
23655  */
23656 static void
23657 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23658 {
23659 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23660 	ip_stack_t	*ipst = ill->ill_ipst;
23661 
23662 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23663 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23664 
23665 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23666 	    &ipst->ips_ipsec_capab_ills_esp;
23667 
23668 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23669 
23670 	prev_ill = NULL;
23671 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23672 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23673 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23674 		;
23675 	if (cur_ill == NULL) {
23676 		/* entry not found */
23677 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23678 		return;
23679 	}
23680 	if (prev_ill == NULL) {
23681 		/* entry at front of list */
23682 		*ills = NULL;
23683 	} else {
23684 		prev_ill->next = cur_ill->next;
23685 	}
23686 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23687 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23688 }
23689 
23690 /*
23691  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23692  * supporting the specified IPsec protocol acceleration.
23693  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23694  * We free the mblk and, if sa is non-null, release the held referece.
23695  */
23696 void
23697 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23698     netstack_t *ns)
23699 {
23700 	ipsec_capab_ill_t *ici, *cur_ici;
23701 	ill_t *ill;
23702 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23703 	ip_stack_t	*ipst = ns->netstack_ip;
23704 
23705 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23706 	    ipst->ips_ipsec_capab_ills_esp;
23707 
23708 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23709 
23710 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23711 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23712 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23713 
23714 		/*
23715 		 * Handle the case where the ill goes away while the SADB is
23716 		 * attempting to send messages.  If it's going away, it's
23717 		 * nuking its shadow SADB, so we don't care..
23718 		 */
23719 
23720 		if (ill == NULL)
23721 			continue;
23722 
23723 		if (sa != NULL) {
23724 			/*
23725 			 * Make sure capabilities match before
23726 			 * sending SA to ill.
23727 			 */
23728 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23729 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23730 				ill_refrele(ill);
23731 				continue;
23732 			}
23733 
23734 			mutex_enter(&sa->ipsa_lock);
23735 			sa->ipsa_flags |= IPSA_F_HW;
23736 			mutex_exit(&sa->ipsa_lock);
23737 		}
23738 
23739 		/*
23740 		 * Copy template message, and add it to the front
23741 		 * of the mblk ship list. We want to avoid holding
23742 		 * the ipsec_capab_ills_lock while sending the
23743 		 * message to the ills.
23744 		 *
23745 		 * The b_next and b_prev are temporarily used
23746 		 * to build a list of mblks to be sent down, and to
23747 		 * save the ill to which they must be sent.
23748 		 */
23749 		nmp = copymsg(mp);
23750 		if (nmp == NULL) {
23751 			ill_refrele(ill);
23752 			continue;
23753 		}
23754 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23755 		nmp->b_next = mp_ship_list;
23756 		mp_ship_list = nmp;
23757 		nmp->b_prev = (mblk_t *)ill;
23758 	}
23759 
23760 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23761 
23762 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23763 		/* restore the mblk to a sane state */
23764 		next_mp = nmp->b_next;
23765 		nmp->b_next = NULL;
23766 		ill = (ill_t *)nmp->b_prev;
23767 		nmp->b_prev = NULL;
23768 
23769 		ill_dlpi_send(ill, nmp);
23770 		ill_refrele(ill);
23771 	}
23772 
23773 	if (sa != NULL)
23774 		IPSA_REFRELE(sa);
23775 	freemsg(mp);
23776 }
23777 
23778 /*
23779  * Derive an interface id from the link layer address.
23780  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23781  */
23782 static boolean_t
23783 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23784 {
23785 	char		*addr;
23786 
23787 	if (phys_length != ETHERADDRL)
23788 		return (B_FALSE);
23789 
23790 	/* Form EUI-64 like address */
23791 	addr = (char *)&v6addr->s6_addr32[2];
23792 	bcopy((char *)phys_addr, addr, 3);
23793 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23794 	addr[3] = (char)0xff;
23795 	addr[4] = (char)0xfe;
23796 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23797 	return (B_TRUE);
23798 }
23799 
23800 /* ARGSUSED */
23801 static boolean_t
23802 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23803 {
23804 	return (B_FALSE);
23805 }
23806 
23807 /* ARGSUSED */
23808 static boolean_t
23809 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23810     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23811 {
23812 	/*
23813 	 * Multicast address mappings used over Ethernet/802.X.
23814 	 * This address is used as a base for mappings.
23815 	 */
23816 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23817 	    0x00, 0x00, 0x00};
23818 
23819 	/*
23820 	 * Extract low order 32 bits from IPv6 multicast address.
23821 	 * Or that into the link layer address, starting from the
23822 	 * second byte.
23823 	 */
23824 	*hw_start = 2;
23825 	v6_extract_mask->s6_addr32[0] = 0;
23826 	v6_extract_mask->s6_addr32[1] = 0;
23827 	v6_extract_mask->s6_addr32[2] = 0;
23828 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23829 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23830 	return (B_TRUE);
23831 }
23832 
23833 /*
23834  * Indicate by return value whether multicast is supported. If not,
23835  * this code should not touch/change any parameters.
23836  */
23837 /* ARGSUSED */
23838 static boolean_t
23839 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23840     uint32_t *hw_start, ipaddr_t *extract_mask)
23841 {
23842 	/*
23843 	 * Multicast address mappings used over Ethernet/802.X.
23844 	 * This address is used as a base for mappings.
23845 	 */
23846 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23847 	    0x00, 0x00, 0x00 };
23848 
23849 	if (phys_length != ETHERADDRL)
23850 		return (B_FALSE);
23851 
23852 	*extract_mask = htonl(0x007fffff);
23853 	*hw_start = 2;
23854 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23855 	return (B_TRUE);
23856 }
23857 
23858 /*
23859  * Derive IPoIB interface id from the link layer address.
23860  */
23861 static boolean_t
23862 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23863 {
23864 	char		*addr;
23865 
23866 	if (phys_length != 20)
23867 		return (B_FALSE);
23868 	addr = (char *)&v6addr->s6_addr32[2];
23869 	bcopy(phys_addr + 12, addr, 8);
23870 	/*
23871 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23872 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23873 	 * rules. In these cases, the IBA considers these GUIDs to be in
23874 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23875 	 * required; vendors are required not to assign global EUI-64's
23876 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23877 	 * of the interface identifier. Whether the GUID is in modified
23878 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23879 	 * bit set to 1.
23880 	 */
23881 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23882 	return (B_TRUE);
23883 }
23884 
23885 /*
23886  * Note on mapping from multicast IP addresses to IPoIB multicast link
23887  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23888  * The format of an IPoIB multicast address is:
23889  *
23890  *  4 byte QPN      Scope Sign.  Pkey
23891  * +--------------------------------------------+
23892  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23893  * +--------------------------------------------+
23894  *
23895  * The Scope and Pkey components are properties of the IBA port and
23896  * network interface. They can be ascertained from the broadcast address.
23897  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23898  */
23899 
23900 static boolean_t
23901 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23902     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23903 {
23904 	/*
23905 	 * Base IPoIB IPv6 multicast address used for mappings.
23906 	 * Does not contain the IBA scope/Pkey values.
23907 	 */
23908 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23909 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23910 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23911 
23912 	/*
23913 	 * Extract low order 80 bits from IPv6 multicast address.
23914 	 * Or that into the link layer address, starting from the
23915 	 * sixth byte.
23916 	 */
23917 	*hw_start = 6;
23918 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23919 
23920 	/*
23921 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23922 	 */
23923 	*(maddr + 5) = *(bphys_addr + 5);
23924 	*(maddr + 8) = *(bphys_addr + 8);
23925 	*(maddr + 9) = *(bphys_addr + 9);
23926 
23927 	v6_extract_mask->s6_addr32[0] = 0;
23928 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23929 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23930 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23931 	return (B_TRUE);
23932 }
23933 
23934 static boolean_t
23935 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23936     uint32_t *hw_start, ipaddr_t *extract_mask)
23937 {
23938 	/*
23939 	 * Base IPoIB IPv4 multicast address used for mappings.
23940 	 * Does not contain the IBA scope/Pkey values.
23941 	 */
23942 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23943 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23944 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23945 
23946 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23947 		return (B_FALSE);
23948 
23949 	/*
23950 	 * Extract low order 28 bits from IPv4 multicast address.
23951 	 * Or that into the link layer address, starting from the
23952 	 * sixteenth byte.
23953 	 */
23954 	*extract_mask = htonl(0x0fffffff);
23955 	*hw_start = 16;
23956 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23957 
23958 	/*
23959 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23960 	 */
23961 	*(maddr + 5) = *(bphys_addr + 5);
23962 	*(maddr + 8) = *(bphys_addr + 8);
23963 	*(maddr + 9) = *(bphys_addr + 9);
23964 	return (B_TRUE);
23965 }
23966 
23967 /*
23968  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23969  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23970  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23971  * the link-local address is preferred.
23972  */
23973 boolean_t
23974 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23975 {
23976 	ipif_t	*ipif;
23977 	ipif_t	*maybe_ipif = NULL;
23978 
23979 	mutex_enter(&ill->ill_lock);
23980 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23981 		mutex_exit(&ill->ill_lock);
23982 		if (ipifp != NULL)
23983 			*ipifp = NULL;
23984 		return (B_FALSE);
23985 	}
23986 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23987 		if (!IPIF_CAN_LOOKUP(ipif))
23988 			continue;
23989 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23990 		    ipif->ipif_zoneid != ALL_ZONES)
23991 			continue;
23992 		if ((ipif->ipif_flags & flags) != flags)
23993 			continue;
23994 
23995 		if (ipifp == NULL) {
23996 			mutex_exit(&ill->ill_lock);
23997 			ASSERT(maybe_ipif == NULL);
23998 			return (B_TRUE);
23999 		}
24000 		if (!ill->ill_isv6 ||
24001 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24002 			ipif_refhold_locked(ipif);
24003 			mutex_exit(&ill->ill_lock);
24004 			*ipifp = ipif;
24005 			return (B_TRUE);
24006 		}
24007 		if (maybe_ipif == NULL)
24008 			maybe_ipif = ipif;
24009 	}
24010 	if (ipifp != NULL) {
24011 		if (maybe_ipif != NULL)
24012 			ipif_refhold_locked(maybe_ipif);
24013 		*ipifp = maybe_ipif;
24014 	}
24015 	mutex_exit(&ill->ill_lock);
24016 	return (maybe_ipif != NULL);
24017 }
24018 
24019 /*
24020  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24021  */
24022 boolean_t
24023 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24024 {
24025 	ill_t *illg;
24026 	ip_stack_t	*ipst = ill->ill_ipst;
24027 
24028 	/*
24029 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24030 	 */
24031 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24032 		return (B_TRUE);
24033 	}
24034 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24035 	if (ill->ill_group == NULL) {
24036 		/* ill not in a group */
24037 		rw_exit(&ipst->ips_ill_g_lock);
24038 		return (B_FALSE);
24039 	}
24040 
24041 	/*
24042 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24043 	 * group. We need to look for an ipif in the zone on all the ills in the
24044 	 * group.
24045 	 */
24046 	illg = ill->ill_group->illgrp_ill;
24047 	do {
24048 		/*
24049 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24050 		 * that it's not there.
24051 		 */
24052 		if (illg != ill &&
24053 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24054 			break;
24055 		}
24056 	} while ((illg = illg->ill_group_next) != NULL);
24057 	rw_exit(&ipst->ips_ill_g_lock);
24058 	return (illg != NULL);
24059 }
24060 
24061 /*
24062  * Check if this ill is only being used to send ICMP probes for IPMP
24063  */
24064 boolean_t
24065 ill_is_probeonly(ill_t *ill)
24066 {
24067 	/*
24068 	 * Check if the interface is FAILED, or INACTIVE
24069 	 */
24070 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24071 		return (B_TRUE);
24072 
24073 	return (B_FALSE);
24074 }
24075 
24076 /*
24077  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24078  * If a pointer to an ipif_t is returned then the caller will need to do
24079  * an ill_refrele().
24080  *
24081  * If there is no real interface which matches the ifindex, then it looks
24082  * for a group that has a matching index. In the case of a group match the
24083  * lifidx must be zero. We don't need emulate the logical interfaces
24084  * since IP Filter's use of netinfo doesn't use that.
24085  */
24086 ipif_t *
24087 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24088     ip_stack_t *ipst)
24089 {
24090 	ipif_t *ipif;
24091 	ill_t *ill;
24092 
24093 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24094 	    ipst);
24095 
24096 	if (ill == NULL) {
24097 		/* Fallback to group names only if hook_emulation set */
24098 		if (!ipst->ips_ipmp_hook_emulation)
24099 			return (NULL);
24100 
24101 		if (lifidx != 0)
24102 			return (NULL);
24103 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24104 		if (ill == NULL)
24105 			return (NULL);
24106 	}
24107 
24108 	mutex_enter(&ill->ill_lock);
24109 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24110 		mutex_exit(&ill->ill_lock);
24111 		ill_refrele(ill);
24112 		return (NULL);
24113 	}
24114 
24115 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24116 		if (!IPIF_CAN_LOOKUP(ipif))
24117 			continue;
24118 		if (lifidx == ipif->ipif_id) {
24119 			ipif_refhold_locked(ipif);
24120 			break;
24121 		}
24122 	}
24123 
24124 	mutex_exit(&ill->ill_lock);
24125 	ill_refrele(ill);
24126 	return (ipif);
24127 }
24128 
24129 /*
24130  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24131  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24132  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24133  * for details.
24134  */
24135 void
24136 ill_fastpath_flush(ill_t *ill)
24137 {
24138 	ip_stack_t *ipst = ill->ill_ipst;
24139 
24140 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24141 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24142 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24143 }
24144 
24145 /*
24146  * Set the physical address information for `ill' to the contents of the
24147  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24148  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24149  * EINPROGRESS will be returned.
24150  */
24151 int
24152 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24153 {
24154 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24155 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24156 
24157 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24158 
24159 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24160 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24161 		/* Changing DL_IPV6_TOKEN is not yet supported */
24162 		return (0);
24163 	}
24164 
24165 	/*
24166 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24167 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24168 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24169 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24170 	 */
24171 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24172 		freemsg(mp);
24173 		return (ENOMEM);
24174 	}
24175 
24176 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24177 
24178 	/*
24179 	 * If we can quiesce the ill, then set the address.  If not, then
24180 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24181 	 */
24182 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24183 	mutex_enter(&ill->ill_lock);
24184 	if (!ill_is_quiescent(ill)) {
24185 		/* call cannot fail since `conn_t *' argument is NULL */
24186 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24187 		    mp, ILL_DOWN);
24188 		mutex_exit(&ill->ill_lock);
24189 		return (EINPROGRESS);
24190 	}
24191 	mutex_exit(&ill->ill_lock);
24192 
24193 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24194 	return (0);
24195 }
24196 
24197 /*
24198  * Once the ill associated with `q' has quiesced, set its physical address
24199  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24200  * are passed (linked by b_cont), since we sometimes need to save two distinct
24201  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24202  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24203  * is quiesced, we know any stale IREs with the old address information have
24204  * already been removed, so we don't need to call ill_fastpath_flush().
24205  */
24206 /* ARGSUSED */
24207 static void
24208 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24209 {
24210 	ill_t		*ill = q->q_ptr;
24211 	mblk_t		*addrmp2 = unlinkb(addrmp);
24212 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24213 	uint_t		addrlen, addroff;
24214 
24215 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24216 
24217 	addroff	= dlindp->dl_addr_offset;
24218 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24219 
24220 	switch (dlindp->dl_data) {
24221 	case DL_IPV6_LINK_LAYER_ADDR:
24222 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24223 		freemsg(addrmp2);
24224 		break;
24225 
24226 	case DL_CURR_PHYS_ADDR:
24227 		freemsg(ill->ill_phys_addr_mp);
24228 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24229 		ill->ill_phys_addr_mp = addrmp;
24230 		ill->ill_phys_addr_length = addrlen;
24231 
24232 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24233 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24234 		else
24235 			freemsg(addrmp2);
24236 		break;
24237 	default:
24238 		ASSERT(0);
24239 	}
24240 
24241 	/*
24242 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24243 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24244 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24245 	 * brought up.
24246 	 */
24247 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24248 		ipsq_current_finish(ipsq);
24249 }
24250 
24251 /*
24252  * Helper routine for setting the ill_nd_lla fields.
24253  */
24254 void
24255 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24256 {
24257 	freemsg(ill->ill_nd_lla_mp);
24258 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24259 	ill->ill_nd_lla_mp = ndmp;
24260 	ill->ill_nd_lla_len = addrlen;
24261 }
24262 
24263 major_t IP_MAJ;
24264 #define	IP	"ip"
24265 
24266 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24267 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24268 
24269 /*
24270  * Issue REMOVEIF ioctls to have the loopback interfaces
24271  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24272  * the former going away when the user-level processes in the zone
24273  * are killed  * and the latter are cleaned up by the stream head
24274  * str_stack_shutdown callback that undoes all I_PLINKs.
24275  */
24276 void
24277 ip_loopback_cleanup(ip_stack_t *ipst)
24278 {
24279 	int error;
24280 	ldi_handle_t	lh = NULL;
24281 	ldi_ident_t	li = NULL;
24282 	int		rval;
24283 	cred_t		*cr;
24284 	struct strioctl iocb;
24285 	struct lifreq	lifreq;
24286 
24287 	IP_MAJ = ddi_name_to_major(IP);
24288 
24289 #ifdef NS_DEBUG
24290 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24291 	    ipst->ips_netstack->netstack_stackid);
24292 #endif
24293 
24294 	bzero(&lifreq, sizeof (lifreq));
24295 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24296 
24297 	error = ldi_ident_from_major(IP_MAJ, &li);
24298 	if (error) {
24299 #ifdef DEBUG
24300 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24301 		    error);
24302 #endif
24303 		return;
24304 	}
24305 
24306 	cr = zone_get_kcred(netstackid_to_zoneid(
24307 	    ipst->ips_netstack->netstack_stackid));
24308 	ASSERT(cr != NULL);
24309 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24310 	if (error) {
24311 #ifdef DEBUG
24312 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24313 		    error);
24314 #endif
24315 		goto out;
24316 	}
24317 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24318 	iocb.ic_timout = 15;
24319 	iocb.ic_len = sizeof (lifreq);
24320 	iocb.ic_dp = (char *)&lifreq;
24321 
24322 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24323 	/* LINTED - statement has no consequent */
24324 	if (error) {
24325 #ifdef NS_DEBUG
24326 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24327 		    "UDP6 error %d\n", error);
24328 #endif
24329 	}
24330 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24331 	lh = NULL;
24332 
24333 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24334 	if (error) {
24335 #ifdef NS_DEBUG
24336 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24337 		    error);
24338 #endif
24339 		goto out;
24340 	}
24341 
24342 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24343 	iocb.ic_timout = 15;
24344 	iocb.ic_len = sizeof (lifreq);
24345 	iocb.ic_dp = (char *)&lifreq;
24346 
24347 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24348 	/* LINTED - statement has no consequent */
24349 	if (error) {
24350 #ifdef NS_DEBUG
24351 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24352 		    "UDP error %d\n", error);
24353 #endif
24354 	}
24355 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24356 	lh = NULL;
24357 
24358 out:
24359 	/* Close layered handles */
24360 	if (lh)
24361 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24362 	if (li)
24363 		ldi_ident_release(li);
24364 
24365 	crfree(cr);
24366 }
24367 
24368 /*
24369  * This needs to be in-sync with nic_event_t definition
24370  */
24371 static const char *
24372 ill_hook_event2str(nic_event_t event)
24373 {
24374 	switch (event) {
24375 	case NE_PLUMB:
24376 		return ("PLUMB");
24377 	case NE_UNPLUMB:
24378 		return ("UNPLUMB");
24379 	case NE_UP:
24380 		return ("UP");
24381 	case NE_DOWN:
24382 		return ("DOWN");
24383 	case NE_ADDRESS_CHANGE:
24384 		return ("ADDRESS_CHANGE");
24385 	default:
24386 		return ("UNKNOWN");
24387 	}
24388 }
24389 
24390 static void
24391 ill_hook_event_destroy(ill_t *ill)
24392 {
24393 	hook_nic_event_int_t	*info;
24394 
24395 	if ((info = ill->ill_nic_event_info) != NULL) {
24396 		if (info->hnei_event.hne_data != NULL) {
24397 			kmem_free(info->hnei_event.hne_data,
24398 			    info->hnei_event.hne_datalen);
24399 		}
24400 		kmem_free(info, sizeof (*info));
24401 
24402 		ill->ill_nic_event_info = NULL;
24403 	}
24404 
24405 }
24406 
24407 boolean_t
24408 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24409     nic_event_data_t data, size_t datalen)
24410 {
24411 	ip_stack_t		*ipst = ill->ill_ipst;
24412 	hook_nic_event_int_t	*info;
24413 	const char		*str = NULL;
24414 
24415 	/* destroy nic event info if it exists */
24416 	if ((info = ill->ill_nic_event_info) != NULL) {
24417 		str = ill_hook_event2str(info->hnei_event.hne_event);
24418 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24419 		    "attached for %s\n", str, ill->ill_name));
24420 		ill_hook_event_destroy(ill);
24421 	}
24422 
24423 	/* create a new nic event info */
24424 	info = kmem_alloc(sizeof (*info), KM_NOSLEEP);
24425 	if (info == NULL)
24426 		goto fail;
24427 
24428 	ill->ill_nic_event_info = info;
24429 
24430 	if (event == NE_UNPLUMB)
24431 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
24432 	else
24433 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24434 	info->hnei_event.hne_lif = lif;
24435 	info->hnei_event.hne_event = event;
24436 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
24437 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24438 	info->hnei_event.hne_data = NULL;
24439 	info->hnei_event.hne_datalen = 0;
24440 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
24441 
24442 	if (data != NULL && datalen != 0) {
24443 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24444 		if (info->hnei_event.hne_data != NULL) {
24445 			bcopy(data, info->hnei_event.hne_data, datalen);
24446 			info->hnei_event.hne_datalen = datalen;
24447 		} else {
24448 			ill_hook_event_destroy(ill);
24449 			goto fail;
24450 		}
24451 	}
24452 
24453 	return (B_TRUE);
24454 fail:
24455 	str = ill_hook_event2str(event);
24456 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24457 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24458 	return (B_FALSE);
24459 }
24460