xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 6cefaae1e90a413ba01560575bb3998e1a3df40e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 #include <sys/kmem.h>
48 #include <sys/systm.h>
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <sys/isa_defs.h>
52 #include <net/if.h>
53 #include <net/if_arp.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 #include <sys/sockio.h>
58 #include <netinet/in.h>
59 #include <netinet/ip6.h>
60 #include <netinet/icmp6.h>
61 #include <netinet/igmp_var.h>
62 #include <sys/strsun.h>
63 #include <sys/policy.h>
64 #include <sys/ethernet.h>
65 
66 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
67 #include <inet/mi.h>
68 #include <inet/nd.h>
69 #include <inet/arp.h>
70 #include <inet/mib2.h>
71 #include <inet/ip.h>
72 #include <inet/ip6.h>
73 #include <inet/ip6_asp.h>
74 #include <inet/tcp.h>
75 #include <inet/ip_multi.h>
76 #include <inet/ip_ire.h>
77 #include <inet/ip_ftable.h>
78 #include <inet/ip_rts.h>
79 #include <inet/ip_ndp.h>
80 #include <inet/ip_if.h>
81 #include <inet/ip_impl.h>
82 #include <inet/tun.h>
83 #include <inet/sctp_ip.h>
84 #include <inet/ip_netinfo.h>
85 #include <inet/mib2.h>
86 
87 #include <net/pfkeyv2.h>
88 #include <inet/ipsec_info.h>
89 #include <inet/sadb.h>
90 #include <inet/ipsec_impl.h>
91 #include <sys/iphada.h>
92 
93 
94 #include <netinet/igmp.h>
95 #include <inet/ip_listutils.h>
96 #include <inet/ipclassifier.h>
97 #include <sys/mac.h>
98 
99 #include <sys/systeminfo.h>
100 #include <sys/bootconf.h>
101 
102 #include <sys/tsol/tndb.h>
103 #include <sys/tsol/tnet.h>
104 
105 /* The character which tells where the ill_name ends */
106 #define	IPIF_SEPARATOR_CHAR	':'
107 
108 /* IP ioctl function table entry */
109 typedef struct ipft_s {
110 	int	ipft_cmd;
111 	pfi_t	ipft_pfi;
112 	int	ipft_min_size;
113 	int	ipft_flags;
114 } ipft_t;
115 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
116 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
117 
118 typedef struct ip_sock_ar_s {
119 	union {
120 		area_t	ip_sock_area;
121 		ared_t	ip_sock_ared;
122 		areq_t	ip_sock_areq;
123 	} ip_sock_ar_u;
124 	queue_t	*ip_sock_ar_q;
125 } ip_sock_ar_t;
126 
127 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
128 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
129 		    char *value, caddr_t cp, cred_t *ioc_cr);
130 
131 static boolean_t ill_is_quiescent(ill_t *);
132 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
133 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
134 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
135     mblk_t *mp, boolean_t need_up);
136 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
137     mblk_t *mp, boolean_t need_up);
138 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
139     queue_t *q, mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
141     mblk_t *mp);
142 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
143     mblk_t *mp);
144 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
145     queue_t *q, mblk_t *mp, boolean_t need_up);
146 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
147     int ioccmd, struct linkblk *li, boolean_t doconsist);
148 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
149 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
150 static void	ipsq_flush(ill_t *ill);
151 
152 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
153     queue_t *q, mblk_t *mp, boolean_t need_up);
154 static void	ipsq_delete(ipsq_t *);
155 
156 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
157 		    boolean_t initialize);
158 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
159 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
160 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
161 		    boolean_t isv6);
162 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
163 static void	ipif_delete_cache_ire(ire_t *, char *);
164 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
165 static void	ipif_free(ipif_t *ipif);
166 static void	ipif_free_tail(ipif_t *ipif);
167 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
168 static void	ipif_multicast_down(ipif_t *ipif);
169 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
170 static void	ipif_set_default(ipif_t *ipif);
171 static int	ipif_set_values(queue_t *q, mblk_t *mp,
172     char *interf_name, uint_t *ppa);
173 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
174     queue_t *q);
175 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
176     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
177     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
178 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
179 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
180 
181 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
182 static int	ill_arp_off(ill_t *ill);
183 static int	ill_arp_on(ill_t *ill);
184 static void	ill_delete_interface_type(ill_if_t *);
185 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
186 static void	ill_dl_down(ill_t *ill);
187 static void	ill_down(ill_t *ill);
188 static void	ill_downi(ire_t *ire, char *ill_arg);
189 static void	ill_free_mib(ill_t *ill);
190 static void	ill_glist_delete(ill_t *);
191 static boolean_t ill_has_usable_ipif(ill_t *);
192 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
193 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
194 static void	ill_phyint_free(ill_t *ill);
195 static void	ill_phyint_reinit(ill_t *ill);
196 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
197 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
198 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
199 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
200 static void	ill_stq_cache_delete(ire_t *, char *);
201 
202 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
203 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
204 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
205     in6_addr_t *);
206 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
207     ipaddr_t *);
208 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
209 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     in6_addr_t *);
211 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
212     ipaddr_t *);
213 
214 static void	ipif_save_ire(ipif_t *, ire_t *);
215 static void	ipif_remove_ire(ipif_t *, ire_t *);
216 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
217 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
218 
219 /*
220  * Per-ill IPsec capabilities management.
221  */
222 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
223 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
224 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
225 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
226 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
227 static void ill_capability_proto(ill_t *, int, mblk_t *);
228 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
229     boolean_t);
230 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
231 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
233 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
235 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
236 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
237 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
238     dl_capability_sub_t *);
239 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
240 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
241 static void ill_capability_lso_reset(ill_t *, mblk_t **);
242 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
243 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
244 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
245 static void	ill_capability_dls_disable(ill_t *);
246 
247 static void	illgrp_cache_delete(ire_t *, char *);
248 static void	illgrp_delete(ill_t *ill);
249 static void	illgrp_reset_schednext(ill_t *ill);
250 
251 static ill_t	*ill_prev_usesrc(ill_t *);
252 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
253 static void	ill_disband_usesrc_group(ill_t *);
254 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
255 
256 #ifdef DEBUG
257 static	void	ill_trace_cleanup(const ill_t *);
258 static	void	ipif_trace_cleanup(const ipif_t *);
259 #endif
260 
261 /*
262  * if we go over the memory footprint limit more than once in this msec
263  * interval, we'll start pruning aggressively.
264  */
265 int ip_min_frag_prune_time = 0;
266 
267 /*
268  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
269  * and the IPsec DOI
270  */
271 #define	MAX_IPSEC_ALGS	256
272 
273 #define	BITSPERBYTE	8
274 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
275 
276 #define	IPSEC_ALG_ENABLE(algs, algid) \
277 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
278 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
279 
280 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
281 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
282 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
283 
284 typedef uint8_t ipsec_capab_elem_t;
285 
286 /*
287  * Per-algorithm parameters.  Note that at present, only encryption
288  * algorithms have variable keysize (IKE does not provide a way to negotiate
289  * auth algorithm keysize).
290  *
291  * All sizes here are in bits.
292  */
293 typedef struct
294 {
295 	uint16_t	minkeylen;
296 	uint16_t	maxkeylen;
297 } ipsec_capab_algparm_t;
298 
299 /*
300  * Per-ill capabilities.
301  */
302 struct ill_ipsec_capab_s {
303 	ipsec_capab_elem_t *encr_hw_algs;
304 	ipsec_capab_elem_t *auth_hw_algs;
305 	uint32_t algs_size;	/* size of _hw_algs in bytes */
306 	/* algorithm key lengths */
307 	ipsec_capab_algparm_t *encr_algparm;
308 	uint32_t encr_algparm_size;
309 	uint32_t encr_algparm_end;
310 };
311 
312 /*
313  * The field values are larger than strictly necessary for simple
314  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
315  */
316 static area_t	ip_area_template = {
317 	AR_ENTRY_ADD,			/* area_cmd */
318 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
319 					/* area_name_offset */
320 	/* area_name_length temporarily holds this structure length */
321 	sizeof (area_t),			/* area_name_length */
322 	IP_ARP_PROTO_TYPE,		/* area_proto */
323 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
324 	IP_ADDR_LEN,			/* area_proto_addr_length */
325 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
326 					/* area_proto_mask_offset */
327 	0,				/* area_flags */
328 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
329 					/* area_hw_addr_offset */
330 	/* Zero length hw_addr_length means 'use your idea of the address' */
331 	0				/* area_hw_addr_length */
332 };
333 
334 /*
335  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
336  * support
337  */
338 static area_t	ip6_area_template = {
339 	AR_ENTRY_ADD,			/* area_cmd */
340 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
341 					/* area_name_offset */
342 	/* area_name_length temporarily holds this structure length */
343 	sizeof (area_t),			/* area_name_length */
344 	IP_ARP_PROTO_TYPE,		/* area_proto */
345 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
346 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
347 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
348 					/* area_proto_mask_offset */
349 	0,				/* area_flags */
350 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
351 					/* area_hw_addr_offset */
352 	/* Zero length hw_addr_length means 'use your idea of the address' */
353 	0				/* area_hw_addr_length */
354 };
355 
356 static ared_t	ip_ared_template = {
357 	AR_ENTRY_DELETE,
358 	sizeof (ared_t) + IP_ADDR_LEN,
359 	sizeof (ared_t),
360 	IP_ARP_PROTO_TYPE,
361 	sizeof (ared_t),
362 	IP_ADDR_LEN,
363 	0
364 };
365 
366 static ared_t	ip6_ared_template = {
367 	AR_ENTRY_DELETE,
368 	sizeof (ared_t) + IPV6_ADDR_LEN,
369 	sizeof (ared_t),
370 	IP_ARP_PROTO_TYPE,
371 	sizeof (ared_t),
372 	IPV6_ADDR_LEN,
373 	0
374 };
375 
376 /*
377  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
378  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
379  * areq is used).
380  */
381 static areq_t	ip_areq_template = {
382 	AR_ENTRY_QUERY,			/* cmd */
383 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
384 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
385 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
386 	sizeof (areq_t),			/* target addr offset */
387 	IP_ADDR_LEN,			/* target addr_length */
388 	0,				/* flags */
389 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
390 	IP_ADDR_LEN,			/* sender addr length */
391 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
392 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
393 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
394 	/* anything else filled in by the code */
395 };
396 
397 static arc_t	ip_aru_template = {
398 	AR_INTERFACE_UP,
399 	sizeof (arc_t),		/* Name offset */
400 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
401 };
402 
403 static arc_t	ip_ard_template = {
404 	AR_INTERFACE_DOWN,
405 	sizeof (arc_t),		/* Name offset */
406 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
407 };
408 
409 static arc_t	ip_aron_template = {
410 	AR_INTERFACE_ON,
411 	sizeof (arc_t),		/* Name offset */
412 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
413 };
414 
415 static arc_t	ip_aroff_template = {
416 	AR_INTERFACE_OFF,
417 	sizeof (arc_t),		/* Name offset */
418 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
419 };
420 
421 static arma_t	ip_arma_multi_template = {
422 	AR_MAPPING_ADD,
423 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
424 				/* Name offset */
425 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
426 	IP_ARP_PROTO_TYPE,
427 	sizeof (arma_t),			/* proto_addr_offset */
428 	IP_ADDR_LEN,				/* proto_addr_length */
429 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
430 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
431 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
432 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
433 	IP_MAX_HW_LEN,				/* hw_addr_length */
434 	0,					/* hw_mapping_start */
435 };
436 
437 static ipft_t	ip_ioctl_ftbl[] = {
438 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
439 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
440 		IPFT_F_NO_REPLY },
441 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
444 	{ 0 }
445 };
446 
447 /* Simple ICMP IP Header Template */
448 static ipha_t icmp_ipha = {
449 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
450 };
451 
452 /* Flag descriptors for ip_ipif_report */
453 static nv_t	ipif_nv_tbl[] = {
454 	{ IPIF_UP,		"UP" },
455 	{ IPIF_BROADCAST,	"BROADCAST" },
456 	{ ILLF_DEBUG,		"DEBUG" },
457 	{ PHYI_LOOPBACK,	"LOOPBACK" },
458 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
459 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
460 	{ PHYI_RUNNING,		"RUNNING" },
461 	{ ILLF_NOARP,		"NOARP" },
462 	{ PHYI_PROMISC,		"PROMISC" },
463 	{ PHYI_ALLMULTI,	"ALLMULTI" },
464 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
465 	{ ILLF_MULTICAST,	"MULTICAST" },
466 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
467 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
468 	{ IPIF_DHCPRUNNING,	"DHCP" },
469 	{ IPIF_PRIVATE,		"PRIVATE" },
470 	{ IPIF_NOXMIT,		"NOXMIT" },
471 	{ IPIF_NOLOCAL,		"NOLOCAL" },
472 	{ IPIF_DEPRECATED,	"DEPRECATED" },
473 	{ IPIF_PREFERRED,	"PREFERRED" },
474 	{ IPIF_TEMPORARY,	"TEMPORARY" },
475 	{ IPIF_ADDRCONF,	"ADDRCONF" },
476 	{ PHYI_VIRTUAL,		"VIRTUAL" },
477 	{ ILLF_ROUTER,		"ROUTER" },
478 	{ ILLF_NONUD,		"NONUD" },
479 	{ IPIF_ANYCAST,		"ANYCAST" },
480 	{ ILLF_NORTEXCH,	"NORTEXCH" },
481 	{ ILLF_IPV4,		"IPV4" },
482 	{ ILLF_IPV6,		"IPV6" },
483 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
484 	{ PHYI_FAILED,		"FAILED" },
485 	{ PHYI_STANDBY,		"STANDBY" },
486 	{ PHYI_INACTIVE,	"INACTIVE" },
487 	{ PHYI_OFFLINE,		"OFFLINE" },
488 };
489 
490 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
491 
492 static ip_m_t   ip_m_tbl[] = {
493 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
494 	    ip_ether_v6intfid },
495 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_nodef_v6intfid },
497 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_ether_v6intfid },
503 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
504 	    ip_ib_v6intfid },
505 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
506 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
507 	    ip_nodef_v6intfid }
508 };
509 
510 static ill_t	ill_null;		/* Empty ILL for init. */
511 char	ipif_loopback_name[] = "lo0";
512 static char *ipv4_forward_suffix = ":ip_forwarding";
513 static char *ipv6_forward_suffix = ":ip6_forwarding";
514 static	sin6_t	sin6_null;	/* Zero address for quick clears */
515 static	sin_t	sin_null;	/* Zero address for quick clears */
516 
517 /* When set search for unused ipif_seqid */
518 static ipif_t	ipif_zero;
519 
520 /*
521  * ppa arena is created after these many
522  * interfaces have been plumbed.
523  */
524 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
525 
526 /*
527  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
528  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
529  * set through platform specific code (Niagara/Ontario).
530  */
531 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
532 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
533 
534 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
535 
536 static uint_t
537 ipif_rand(ip_stack_t *ipst)
538 {
539 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
540 	    12345;
541 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
542 }
543 
544 /*
545  * Allocate per-interface mibs.
546  * Returns true if ok. False otherwise.
547  *  ipsq  may not yet be allocated (loopback case ).
548  */
549 static boolean_t
550 ill_allocate_mibs(ill_t *ill)
551 {
552 	/* Already allocated? */
553 	if (ill->ill_ip_mib != NULL) {
554 		if (ill->ill_isv6)
555 			ASSERT(ill->ill_icmp6_mib != NULL);
556 		return (B_TRUE);
557 	}
558 
559 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
560 	    KM_NOSLEEP);
561 	if (ill->ill_ip_mib == NULL) {
562 		return (B_FALSE);
563 	}
564 
565 	/* Setup static information */
566 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
567 	    sizeof (mib2_ipIfStatsEntry_t));
568 	if (ill->ill_isv6) {
569 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
570 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
571 		    sizeof (mib2_ipv6AddrEntry_t));
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
573 		    sizeof (mib2_ipv6RouteEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
575 		    sizeof (mib2_ipv6NetToMediaEntry_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
577 		    sizeof (ipv6_member_t));
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
579 		    sizeof (ipv6_grpsrc_t));
580 	} else {
581 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
582 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
583 		    sizeof (mib2_ipAddrEntry_t));
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
585 		    sizeof (mib2_ipRouteEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
587 		    sizeof (mib2_ipNetToMediaEntry_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
589 		    sizeof (ip_member_t));
590 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
591 		    sizeof (ip_grpsrc_t));
592 
593 		/*
594 		 * For a v4 ill, we are done at this point, because per ill
595 		 * icmp mibs are only used for v6.
596 		 */
597 		return (B_TRUE);
598 	}
599 
600 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
601 	    KM_NOSLEEP);
602 	if (ill->ill_icmp6_mib == NULL) {
603 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
604 		ill->ill_ip_mib = NULL;
605 		return (B_FALSE);
606 	}
607 	/* static icmp info */
608 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
609 	    sizeof (mib2_ipv6IfIcmpEntry_t);
610 	/*
611 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
612 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
613 	 * -> ill_phyint_reinit
614 	 */
615 	return (B_TRUE);
616 }
617 
618 /*
619  * Common code for preparation of ARP commands.  Two points to remember:
620  * 	1) The ill_name is tacked on at the end of the allocated space so
621  *	   the templates name_offset field must contain the total space
622  *	   to allocate less the name length.
623  *
624  *	2) The templates name_length field should contain the *template*
625  *	   length.  We use it as a parameter to bcopy() and then write
626  *	   the real ill_name_length into the name_length field of the copy.
627  * (Always called as writer.)
628  */
629 mblk_t *
630 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
631 {
632 	arc_t	*arc = (arc_t *)template;
633 	char	*cp;
634 	int	len;
635 	mblk_t	*mp;
636 	uint_t	name_length = ill->ill_name_length;
637 	uint_t	template_len = arc->arc_name_length;
638 
639 	len = arc->arc_name_offset + name_length;
640 	mp = allocb(len, BPRI_HI);
641 	if (mp == NULL)
642 		return (NULL);
643 	cp = (char *)mp->b_rptr;
644 	mp->b_wptr = (uchar_t *)&cp[len];
645 	if (template_len)
646 		bcopy(template, cp, template_len);
647 	if (len > template_len)
648 		bzero(&cp[template_len], len - template_len);
649 	mp->b_datap->db_type = M_PROTO;
650 
651 	arc = (arc_t *)cp;
652 	arc->arc_name_length = name_length;
653 	cp = (char *)arc + arc->arc_name_offset;
654 	bcopy(ill->ill_name, cp, name_length);
655 
656 	if (addr) {
657 		area_t	*area = (area_t *)mp->b_rptr;
658 
659 		cp = (char *)area + area->area_proto_addr_offset;
660 		bcopy(addr, cp, area->area_proto_addr_length);
661 		if (area->area_cmd == AR_ENTRY_ADD) {
662 			cp = (char *)area;
663 			len = area->area_proto_addr_length;
664 			if (area->area_proto_mask_offset)
665 				cp += area->area_proto_mask_offset;
666 			else
667 				cp += area->area_proto_addr_offset + len;
668 			while (len-- > 0)
669 				*cp++ = (char)~0;
670 		}
671 	}
672 	return (mp);
673 }
674 
675 mblk_t *
676 ipif_area_alloc(ipif_t *ipif)
677 {
678 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
679 	    (char *)&ipif->ipif_lcl_addr));
680 }
681 
682 mblk_t *
683 ipif_ared_alloc(ipif_t *ipif)
684 {
685 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
686 	    (char *)&ipif->ipif_lcl_addr));
687 }
688 
689 mblk_t *
690 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
691 {
692 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
693 	    (char *)&addr));
694 }
695 
696 /*
697  * Completely vaporize a lower level tap and all associated interfaces.
698  * ill_delete is called only out of ip_close when the device control
699  * stream is being closed.
700  */
701 void
702 ill_delete(ill_t *ill)
703 {
704 	ipif_t	*ipif;
705 	ill_t	*prev_ill;
706 	ip_stack_t	*ipst = ill->ill_ipst;
707 
708 	/*
709 	 * ill_delete may be forcibly entering the ipsq. The previous
710 	 * ioctl may not have completed and may need to be aborted.
711 	 * ipsq_flush takes care of it. If we don't need to enter the
712 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
713 	 * ill_delete_tail is sufficient.
714 	 */
715 	ipsq_flush(ill);
716 
717 	/*
718 	 * Nuke all interfaces.  ipif_free will take down the interface,
719 	 * remove it from the list, and free the data structure.
720 	 * Walk down the ipif list and remove the logical interfaces
721 	 * first before removing the main ipif. We can't unplumb
722 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
723 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
724 	 * POINTOPOINT.
725 	 *
726 	 * If ill_ipif was not properly initialized (i.e low on memory),
727 	 * then no interfaces to clean up. In this case just clean up the
728 	 * ill.
729 	 */
730 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
731 		ipif_free(ipif);
732 
733 	/*
734 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
735 	 * So nobody can be using this mp now. Free the mp allocated for
736 	 * honoring ILLF_NOARP
737 	 */
738 	freemsg(ill->ill_arp_on_mp);
739 	ill->ill_arp_on_mp = NULL;
740 
741 	/* Clean up msgs on pending upcalls for mrouted */
742 	reset_mrt_ill(ill);
743 
744 	/*
745 	 * ipif_free -> reset_conn_ipif will remove all multicast
746 	 * references for IPv4. For IPv6, we need to do it here as
747 	 * it points only at ills.
748 	 */
749 	reset_conn_ill(ill);
750 
751 	/*
752 	 * Remove multicast references added as a result of calls to
753 	 * ip_join_allmulti().
754 	 */
755 	ip_purge_allmulti(ill);
756 
757 	/*
758 	 * ill_down will arrange to blow off any IRE's dependent on this
759 	 * ILL, and shut down fragmentation reassembly.
760 	 */
761 	ill_down(ill);
762 
763 	/* Let SCTP know, so that it can remove this from its list. */
764 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
765 
766 	/*
767 	 * If an address on this ILL is being used as a source address then
768 	 * clear out the pointers in other ILLs that point to this ILL.
769 	 */
770 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
771 	if (ill->ill_usesrc_grp_next != NULL) {
772 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
773 			ill_disband_usesrc_group(ill);
774 		} else {	/* consumer of the usesrc ILL */
775 			prev_ill = ill_prev_usesrc(ill);
776 			prev_ill->ill_usesrc_grp_next =
777 			    ill->ill_usesrc_grp_next;
778 		}
779 	}
780 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
781 }
782 
783 static void
784 ipif_non_duplicate(ipif_t *ipif)
785 {
786 	ill_t *ill = ipif->ipif_ill;
787 	mutex_enter(&ill->ill_lock);
788 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
789 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
790 		ASSERT(ill->ill_ipif_dup_count > 0);
791 		ill->ill_ipif_dup_count--;
792 	}
793 	mutex_exit(&ill->ill_lock);
794 }
795 
796 /*
797  * ill_delete_tail is called from ip_modclose after all references
798  * to the closing ill are gone. The wait is done in ip_modclose
799  */
800 void
801 ill_delete_tail(ill_t *ill)
802 {
803 	mblk_t	**mpp;
804 	ipif_t	*ipif;
805 	ip_stack_t	*ipst = ill->ill_ipst;
806 
807 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
808 		ipif_non_duplicate(ipif);
809 		ipif_down_tail(ipif);
810 	}
811 
812 	ASSERT(ill->ill_ipif_dup_count == 0 &&
813 	    ill->ill_arp_down_mp == NULL &&
814 	    ill->ill_arp_del_mapping_mp == NULL);
815 
816 	/*
817 	 * If polling capability is enabled (which signifies direct
818 	 * upcall into IP and driver has ill saved as a handle),
819 	 * we need to make sure that unbind has completed before we
820 	 * let the ill disappear and driver no longer has any reference
821 	 * to this ill.
822 	 */
823 	mutex_enter(&ill->ill_lock);
824 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
825 		cv_wait(&ill->ill_cv, &ill->ill_lock);
826 	mutex_exit(&ill->ill_lock);
827 
828 	/*
829 	 * Clean up polling and soft ring capabilities
830 	 */
831 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
832 		ill_capability_dls_disable(ill);
833 
834 	if (ill->ill_net_type != IRE_LOOPBACK)
835 		qprocsoff(ill->ill_rq);
836 
837 	/*
838 	 * We do an ipsq_flush once again now. New messages could have
839 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
840 	 * could also have landed up if an ioctl thread had looked up
841 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
842 	 * enqueued the ioctl when we did the ipsq_flush last time.
843 	 */
844 	ipsq_flush(ill);
845 
846 	/*
847 	 * Free capabilities.
848 	 */
849 	if (ill->ill_ipsec_capab_ah != NULL) {
850 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
851 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
852 		ill->ill_ipsec_capab_ah = NULL;
853 	}
854 
855 	if (ill->ill_ipsec_capab_esp != NULL) {
856 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
857 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
858 		ill->ill_ipsec_capab_esp = NULL;
859 	}
860 
861 	if (ill->ill_mdt_capab != NULL) {
862 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
863 		ill->ill_mdt_capab = NULL;
864 	}
865 
866 	if (ill->ill_hcksum_capab != NULL) {
867 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
868 		ill->ill_hcksum_capab = NULL;
869 	}
870 
871 	if (ill->ill_zerocopy_capab != NULL) {
872 		kmem_free(ill->ill_zerocopy_capab,
873 		    sizeof (ill_zerocopy_capab_t));
874 		ill->ill_zerocopy_capab = NULL;
875 	}
876 
877 	if (ill->ill_lso_capab != NULL) {
878 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
879 		ill->ill_lso_capab = NULL;
880 	}
881 
882 	if (ill->ill_dls_capab != NULL) {
883 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
884 		ill->ill_dls_capab->ill_unbind_conn = NULL;
885 		kmem_free(ill->ill_dls_capab,
886 		    sizeof (ill_dls_capab_t) +
887 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
888 		ill->ill_dls_capab = NULL;
889 	}
890 
891 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
892 
893 	while (ill->ill_ipif != NULL)
894 		ipif_free_tail(ill->ill_ipif);
895 
896 	/*
897 	 * We have removed all references to ilm from conn and the ones joined
898 	 * within the kernel.
899 	 *
900 	 * We don't walk conns, mrts and ires because
901 	 *
902 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
903 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
904 	 *    ill references.
905 	 */
906 	ASSERT(ilm_walk_ill(ill) == 0);
907 	/*
908 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
909 	 * could free the phyint. No more reference to the phyint after this
910 	 * point.
911 	 */
912 	(void) ill_glist_delete(ill);
913 
914 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
915 	if (ill->ill_ndd_name != NULL)
916 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
917 	rw_exit(&ipst->ips_ip_g_nd_lock);
918 
919 	if (ill->ill_frag_ptr != NULL) {
920 		uint_t count;
921 
922 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
923 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
924 		}
925 		mi_free(ill->ill_frag_ptr);
926 		ill->ill_frag_ptr = NULL;
927 		ill->ill_frag_hash_tbl = NULL;
928 	}
929 
930 	freemsg(ill->ill_nd_lla_mp);
931 	/* Free all retained control messages. */
932 	mpp = &ill->ill_first_mp_to_free;
933 	do {
934 		while (mpp[0]) {
935 			mblk_t  *mp;
936 			mblk_t  *mp1;
937 
938 			mp = mpp[0];
939 			mpp[0] = mp->b_next;
940 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
941 				mp1->b_next = NULL;
942 				mp1->b_prev = NULL;
943 			}
944 			freemsg(mp);
945 		}
946 	} while (mpp++ != &ill->ill_last_mp_to_free);
947 
948 	ill_free_mib(ill);
949 
950 #ifdef DEBUG
951 	ill_trace_cleanup(ill);
952 #endif
953 
954 	/* Drop refcnt here */
955 	netstack_rele(ill->ill_ipst->ips_netstack);
956 	ill->ill_ipst = NULL;
957 }
958 
959 static void
960 ill_free_mib(ill_t *ill)
961 {
962 	ip_stack_t *ipst = ill->ill_ipst;
963 
964 	/*
965 	 * MIB statistics must not be lost, so when an interface
966 	 * goes away the counter values will be added to the global
967 	 * MIBs.
968 	 */
969 	if (ill->ill_ip_mib != NULL) {
970 		if (ill->ill_isv6) {
971 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
972 			    ill->ill_ip_mib);
973 		} else {
974 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
975 			    ill->ill_ip_mib);
976 		}
977 
978 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
979 		ill->ill_ip_mib = NULL;
980 	}
981 	if (ill->ill_icmp6_mib != NULL) {
982 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
983 		    ill->ill_icmp6_mib);
984 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
985 		ill->ill_icmp6_mib = NULL;
986 	}
987 }
988 
989 /*
990  * Concatenate together a physical address and a sap.
991  *
992  * Sap_lengths are interpreted as follows:
993  *   sap_length == 0	==>	no sap
994  *   sap_length > 0	==>	sap is at the head of the dlpi address
995  *   sap_length < 0	==>	sap is at the tail of the dlpi address
996  */
997 static void
998 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
999     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
1000 {
1001 	uint16_t sap_addr = (uint16_t)sap_src;
1002 
1003 	if (sap_length == 0) {
1004 		if (phys_src == NULL)
1005 			bzero(dst, phys_length);
1006 		else
1007 			bcopy(phys_src, dst, phys_length);
1008 	} else if (sap_length < 0) {
1009 		if (phys_src == NULL)
1010 			bzero(dst, phys_length);
1011 		else
1012 			bcopy(phys_src, dst, phys_length);
1013 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1014 	} else {
1015 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1016 		if (phys_src == NULL)
1017 			bzero((char *)dst + sap_length, phys_length);
1018 		else
1019 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1020 	}
1021 }
1022 
1023 /*
1024  * Generate a dl_unitdata_req mblk for the device and address given.
1025  * addr_length is the length of the physical portion of the address.
1026  * If addr is NULL include an all zero address of the specified length.
1027  * TRUE? In any case, addr_length is taken to be the entire length of the
1028  * dlpi address, including the absolute value of sap_length.
1029  */
1030 mblk_t *
1031 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1032 		t_scalar_t sap_length)
1033 {
1034 	dl_unitdata_req_t *dlur;
1035 	mblk_t	*mp;
1036 	t_scalar_t	abs_sap_length;		/* absolute value */
1037 
1038 	abs_sap_length = ABS(sap_length);
1039 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1040 	    DL_UNITDATA_REQ);
1041 	if (mp == NULL)
1042 		return (NULL);
1043 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1044 	/* HACK: accomodate incompatible DLPI drivers */
1045 	if (addr_length == 8)
1046 		addr_length = 6;
1047 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1048 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1049 	dlur->dl_priority.dl_min = 0;
1050 	dlur->dl_priority.dl_max = 0;
1051 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1052 	    (uchar_t *)&dlur[1]);
1053 	return (mp);
1054 }
1055 
1056 /*
1057  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1058  * Return an error if we already have 1 or more ioctls in progress.
1059  * This is used only for non-exclusive ioctls. Currently this is used
1060  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1061  * and thus need to use ipsq_pending_mp_add.
1062  */
1063 boolean_t
1064 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1065 {
1066 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1067 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1068 	/*
1069 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1070 	 */
1071 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1072 	    (add_mp->b_datap->db_type == M_IOCTL));
1073 
1074 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1075 	/*
1076 	 * Return error if the conn has started closing. The conn
1077 	 * could have finished cleaning up the pending mp list,
1078 	 * If so we should not add another mp to the list negating
1079 	 * the cleanup.
1080 	 */
1081 	if (connp->conn_state_flags & CONN_CLOSING)
1082 		return (B_FALSE);
1083 	/*
1084 	 * Add the pending mp to the head of the list, chained by b_next.
1085 	 * Note down the conn on which the ioctl request came, in b_prev.
1086 	 * This will be used to later get the conn, when we get a response
1087 	 * on the ill queue, from some other module (typically arp)
1088 	 */
1089 	add_mp->b_next = (void *)ill->ill_pending_mp;
1090 	add_mp->b_queue = CONNP_TO_WQ(connp);
1091 	ill->ill_pending_mp = add_mp;
1092 	if (connp != NULL)
1093 		connp->conn_oper_pending_ill = ill;
1094 	return (B_TRUE);
1095 }
1096 
1097 /*
1098  * Retrieve the ill_pending_mp and return it. We have to walk the list
1099  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1100  */
1101 mblk_t *
1102 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1103 {
1104 	mblk_t	*prev = NULL;
1105 	mblk_t	*curr = NULL;
1106 	uint_t	id;
1107 	conn_t	*connp;
1108 
1109 	/*
1110 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1111 	 * up the pending mp, but it does not know the ioc_id and
1112 	 * passes in a zero for it.
1113 	 */
1114 	mutex_enter(&ill->ill_lock);
1115 	if (ioc_id != 0)
1116 		*connpp = NULL;
1117 
1118 	/* Search the list for the appropriate ioctl based on ioc_id */
1119 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1120 	    prev = curr, curr = curr->b_next) {
1121 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1122 		connp = Q_TO_CONN(curr->b_queue);
1123 		/* Match based on the ioc_id or based on the conn */
1124 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1125 			break;
1126 	}
1127 
1128 	if (curr != NULL) {
1129 		/* Unlink the mblk from the pending mp list */
1130 		if (prev != NULL) {
1131 			prev->b_next = curr->b_next;
1132 		} else {
1133 			ASSERT(ill->ill_pending_mp == curr);
1134 			ill->ill_pending_mp = curr->b_next;
1135 		}
1136 
1137 		/*
1138 		 * conn refcnt must have been bumped up at the start of
1139 		 * the ioctl. So we can safely access the conn.
1140 		 */
1141 		ASSERT(CONN_Q(curr->b_queue));
1142 		*connpp = Q_TO_CONN(curr->b_queue);
1143 		curr->b_next = NULL;
1144 		curr->b_queue = NULL;
1145 	}
1146 
1147 	mutex_exit(&ill->ill_lock);
1148 
1149 	return (curr);
1150 }
1151 
1152 /*
1153  * Add the pending mp to the list. There can be only 1 pending mp
1154  * in the list. Any exclusive ioctl that needs to wait for a response
1155  * from another module or driver needs to use this function to set
1156  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1157  * the other module/driver. This is also used while waiting for the
1158  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1159  */
1160 boolean_t
1161 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1162     int waitfor)
1163 {
1164 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1165 
1166 	ASSERT(IAM_WRITER_IPIF(ipif));
1167 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1168 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1169 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1170 	/*
1171 	 * The caller may be using a different ipif than the one passed into
1172 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1173 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1174 	 * that `ipsq_current_ipif == ipif'.
1175 	 */
1176 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1177 
1178 	/*
1179 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1180 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1181 	 */
1182 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1183 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1184 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1185 
1186 	if (connp != NULL) {
1187 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1188 		/*
1189 		 * Return error if the conn has started closing. The conn
1190 		 * could have finished cleaning up the pending mp list,
1191 		 * If so we should not add another mp to the list negating
1192 		 * the cleanup.
1193 		 */
1194 		if (connp->conn_state_flags & CONN_CLOSING)
1195 			return (B_FALSE);
1196 	}
1197 	mutex_enter(&ipsq->ipsq_lock);
1198 	ipsq->ipsq_pending_ipif = ipif;
1199 	/*
1200 	 * Note down the queue in b_queue. This will be returned by
1201 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1202 	 * the processing
1203 	 */
1204 	add_mp->b_next = NULL;
1205 	add_mp->b_queue = q;
1206 	ipsq->ipsq_pending_mp = add_mp;
1207 	ipsq->ipsq_waitfor = waitfor;
1208 
1209 	if (connp != NULL)
1210 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1211 	mutex_exit(&ipsq->ipsq_lock);
1212 	return (B_TRUE);
1213 }
1214 
1215 /*
1216  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1217  * queued in the list.
1218  */
1219 mblk_t *
1220 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1221 {
1222 	mblk_t	*curr = NULL;
1223 
1224 	mutex_enter(&ipsq->ipsq_lock);
1225 	*connpp = NULL;
1226 	if (ipsq->ipsq_pending_mp == NULL) {
1227 		mutex_exit(&ipsq->ipsq_lock);
1228 		return (NULL);
1229 	}
1230 
1231 	/* There can be only 1 such excl message */
1232 	curr = ipsq->ipsq_pending_mp;
1233 	ASSERT(curr != NULL && curr->b_next == NULL);
1234 	ipsq->ipsq_pending_ipif = NULL;
1235 	ipsq->ipsq_pending_mp = NULL;
1236 	ipsq->ipsq_waitfor = 0;
1237 	mutex_exit(&ipsq->ipsq_lock);
1238 
1239 	if (CONN_Q(curr->b_queue)) {
1240 		/*
1241 		 * This mp did a refhold on the conn, at the start of the ioctl.
1242 		 * So we can safely return a pointer to the conn to the caller.
1243 		 */
1244 		*connpp = Q_TO_CONN(curr->b_queue);
1245 	} else {
1246 		*connpp = NULL;
1247 	}
1248 	curr->b_next = NULL;
1249 	curr->b_prev = NULL;
1250 	return (curr);
1251 }
1252 
1253 /*
1254  * Cleanup the ioctl mp queued in ipsq_pending_mp
1255  * - Called in the ill_delete path
1256  * - Called in the M_ERROR or M_HANGUP path on the ill.
1257  * - Called in the conn close path.
1258  */
1259 boolean_t
1260 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1261 {
1262 	mblk_t	*mp;
1263 	ipsq_t	*ipsq;
1264 	queue_t	*q;
1265 	ipif_t	*ipif;
1266 
1267 	ASSERT(IAM_WRITER_ILL(ill));
1268 	ipsq = ill->ill_phyint->phyint_ipsq;
1269 	mutex_enter(&ipsq->ipsq_lock);
1270 	/*
1271 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1272 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1273 	 * even if it is meant for another ill, since we have to enqueue
1274 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1275 	 * If connp is non-null we are called from the conn close path.
1276 	 */
1277 	mp = ipsq->ipsq_pending_mp;
1278 	if (mp == NULL || (connp != NULL &&
1279 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1280 		mutex_exit(&ipsq->ipsq_lock);
1281 		return (B_FALSE);
1282 	}
1283 	/* Now remove from the ipsq_pending_mp */
1284 	ipsq->ipsq_pending_mp = NULL;
1285 	q = mp->b_queue;
1286 	mp->b_next = NULL;
1287 	mp->b_prev = NULL;
1288 	mp->b_queue = NULL;
1289 
1290 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1291 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1292 	if (ill->ill_move_in_progress) {
1293 		ILL_CLEAR_MOVE(ill);
1294 	} else if (ill->ill_up_ipifs) {
1295 		ill_group_cleanup(ill);
1296 	}
1297 
1298 	ipif = ipsq->ipsq_pending_ipif;
1299 	ipsq->ipsq_pending_ipif = NULL;
1300 	ipsq->ipsq_waitfor = 0;
1301 	ipsq->ipsq_current_ipif = NULL;
1302 	ipsq->ipsq_current_ioctl = 0;
1303 	ipsq->ipsq_current_done = B_TRUE;
1304 	mutex_exit(&ipsq->ipsq_lock);
1305 
1306 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1307 		if (connp == NULL) {
1308 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1309 		} else {
1310 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1311 			mutex_enter(&ipif->ipif_ill->ill_lock);
1312 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1313 			mutex_exit(&ipif->ipif_ill->ill_lock);
1314 		}
1315 	} else {
1316 		/*
1317 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1318 		 * be just inet_freemsg. we have to restart it
1319 		 * otherwise the thread will be stuck.
1320 		 */
1321 		inet_freemsg(mp);
1322 	}
1323 	return (B_TRUE);
1324 }
1325 
1326 /*
1327  * The ill is closing. Cleanup all the pending mps. Called exclusively
1328  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1329  * knows this ill, and hence nobody can add an mp to this list
1330  */
1331 static void
1332 ill_pending_mp_cleanup(ill_t *ill)
1333 {
1334 	mblk_t	*mp;
1335 	queue_t	*q;
1336 
1337 	ASSERT(IAM_WRITER_ILL(ill));
1338 
1339 	mutex_enter(&ill->ill_lock);
1340 	/*
1341 	 * Every mp on the pending mp list originating from an ioctl
1342 	 * added 1 to the conn refcnt, at the start of the ioctl.
1343 	 * So bump it down now.  See comments in ip_wput_nondata()
1344 	 */
1345 	while (ill->ill_pending_mp != NULL) {
1346 		mp = ill->ill_pending_mp;
1347 		ill->ill_pending_mp = mp->b_next;
1348 		mutex_exit(&ill->ill_lock);
1349 
1350 		q = mp->b_queue;
1351 		ASSERT(CONN_Q(q));
1352 		mp->b_next = NULL;
1353 		mp->b_prev = NULL;
1354 		mp->b_queue = NULL;
1355 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1356 		mutex_enter(&ill->ill_lock);
1357 	}
1358 	ill->ill_pending_ipif = NULL;
1359 
1360 	mutex_exit(&ill->ill_lock);
1361 }
1362 
1363 /*
1364  * Called in the conn close path and ill delete path
1365  */
1366 static void
1367 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1368 {
1369 	ipsq_t	*ipsq;
1370 	mblk_t	*prev;
1371 	mblk_t	*curr;
1372 	mblk_t	*next;
1373 	queue_t	*q;
1374 	mblk_t	*tmp_list = NULL;
1375 
1376 	ASSERT(IAM_WRITER_ILL(ill));
1377 	if (connp != NULL)
1378 		q = CONNP_TO_WQ(connp);
1379 	else
1380 		q = ill->ill_wq;
1381 
1382 	ipsq = ill->ill_phyint->phyint_ipsq;
1383 	/*
1384 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1385 	 * In the case of ioctl from a conn, there can be only 1 mp
1386 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1387 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1388 	 * ioctls meant for this ill form conn's are not flushed. They will
1389 	 * be processed during ipsq_exit and will not find the ill and will
1390 	 * return error.
1391 	 */
1392 	mutex_enter(&ipsq->ipsq_lock);
1393 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1394 	    curr = next) {
1395 		next = curr->b_next;
1396 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1397 			/* Unlink the mblk from the pending mp list */
1398 			if (prev != NULL) {
1399 				prev->b_next = curr->b_next;
1400 			} else {
1401 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1402 				ipsq->ipsq_xopq_mphead = curr->b_next;
1403 			}
1404 			if (ipsq->ipsq_xopq_mptail == curr)
1405 				ipsq->ipsq_xopq_mptail = prev;
1406 			/*
1407 			 * Create a temporary list and release the ipsq lock
1408 			 * New elements are added to the head of the tmp_list
1409 			 */
1410 			curr->b_next = tmp_list;
1411 			tmp_list = curr;
1412 		} else {
1413 			prev = curr;
1414 		}
1415 	}
1416 	mutex_exit(&ipsq->ipsq_lock);
1417 
1418 	while (tmp_list != NULL) {
1419 		curr = tmp_list;
1420 		tmp_list = curr->b_next;
1421 		curr->b_next = NULL;
1422 		curr->b_prev = NULL;
1423 		curr->b_queue = NULL;
1424 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1425 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1426 			    CONN_CLOSE : NO_COPYOUT, NULL);
1427 		} else {
1428 			/*
1429 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1430 			 * this can't be just inet_freemsg. we have to
1431 			 * restart it otherwise the thread will be stuck.
1432 			 */
1433 			inet_freemsg(curr);
1434 		}
1435 	}
1436 }
1437 
1438 /*
1439  * This conn has started closing. Cleanup any pending ioctl from this conn.
1440  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1441  */
1442 void
1443 conn_ioctl_cleanup(conn_t *connp)
1444 {
1445 	mblk_t *curr;
1446 	ipsq_t	*ipsq;
1447 	ill_t	*ill;
1448 	boolean_t refheld;
1449 
1450 	/*
1451 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1452 	 * ioctl has not yet started, the mp is pending in the list headed by
1453 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1454 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1455 	 * is currently executing now the mp is not queued anywhere but
1456 	 * conn_oper_pending_ill is null. The conn close will wait
1457 	 * till the conn_ref drops to zero.
1458 	 */
1459 	mutex_enter(&connp->conn_lock);
1460 	ill = connp->conn_oper_pending_ill;
1461 	if (ill == NULL) {
1462 		mutex_exit(&connp->conn_lock);
1463 		return;
1464 	}
1465 
1466 	curr = ill_pending_mp_get(ill, &connp, 0);
1467 	if (curr != NULL) {
1468 		mutex_exit(&connp->conn_lock);
1469 		CONN_DEC_REF(connp);
1470 		inet_freemsg(curr);
1471 		return;
1472 	}
1473 	/*
1474 	 * We may not be able to refhold the ill if the ill/ipif
1475 	 * is changing. But we need to make sure that the ill will
1476 	 * not vanish. So we just bump up the ill_waiter count.
1477 	 */
1478 	refheld = ill_waiter_inc(ill);
1479 	mutex_exit(&connp->conn_lock);
1480 	if (refheld) {
1481 		if (ipsq_enter(ill, B_TRUE)) {
1482 			ill_waiter_dcr(ill);
1483 			/*
1484 			 * Check whether this ioctl has started and is
1485 			 * pending now in ipsq_pending_mp. If it is not
1486 			 * found there then check whether this ioctl has
1487 			 * not even started and is in the ipsq_xopq list.
1488 			 */
1489 			if (!ipsq_pending_mp_cleanup(ill, connp))
1490 				ipsq_xopq_mp_cleanup(ill, connp);
1491 			ipsq = ill->ill_phyint->phyint_ipsq;
1492 			ipsq_exit(ipsq);
1493 			return;
1494 		}
1495 	}
1496 
1497 	/*
1498 	 * The ill is also closing and we could not bump up the
1499 	 * ill_waiter_count or we could not enter the ipsq. Leave
1500 	 * the cleanup to ill_delete
1501 	 */
1502 	mutex_enter(&connp->conn_lock);
1503 	while (connp->conn_oper_pending_ill != NULL)
1504 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1505 	mutex_exit(&connp->conn_lock);
1506 	if (refheld)
1507 		ill_waiter_dcr(ill);
1508 }
1509 
1510 /*
1511  * ipcl_walk function for cleaning up conn_*_ill fields.
1512  */
1513 static void
1514 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1515 {
1516 	ill_t	*ill = (ill_t *)arg;
1517 	ire_t	*ire;
1518 
1519 	mutex_enter(&connp->conn_lock);
1520 	if (connp->conn_multicast_ill == ill) {
1521 		/* Revert to late binding */
1522 		connp->conn_multicast_ill = NULL;
1523 		connp->conn_orig_multicast_ifindex = 0;
1524 	}
1525 	if (connp->conn_incoming_ill == ill)
1526 		connp->conn_incoming_ill = NULL;
1527 	if (connp->conn_outgoing_ill == ill)
1528 		connp->conn_outgoing_ill = NULL;
1529 	if (connp->conn_outgoing_pill == ill)
1530 		connp->conn_outgoing_pill = NULL;
1531 	if (connp->conn_nofailover_ill == ill)
1532 		connp->conn_nofailover_ill = NULL;
1533 	if (connp->conn_dhcpinit_ill == ill) {
1534 		connp->conn_dhcpinit_ill = NULL;
1535 		ASSERT(ill->ill_dhcpinit != 0);
1536 		atomic_dec_32(&ill->ill_dhcpinit);
1537 	}
1538 	if (connp->conn_ire_cache != NULL) {
1539 		ire = connp->conn_ire_cache;
1540 		/*
1541 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1542 		 * interface X and ipif coming from interface Y, if interface
1543 		 * X and Y are part of the same IPMPgroup. Thus whenever
1544 		 * interface X goes down, remove all references to it by
1545 		 * checking both on ire_ipif and ire_stq.
1546 		 */
1547 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1548 		    (ire->ire_type == IRE_CACHE &&
1549 		    ire->ire_stq == ill->ill_wq)) {
1550 			connp->conn_ire_cache = NULL;
1551 			mutex_exit(&connp->conn_lock);
1552 			ire_refrele_notr(ire);
1553 			return;
1554 		}
1555 	}
1556 	mutex_exit(&connp->conn_lock);
1557 }
1558 
1559 /* ARGSUSED */
1560 void
1561 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1562 {
1563 	ill_t	*ill = q->q_ptr;
1564 	ipif_t	*ipif;
1565 
1566 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1567 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1568 		ipif_non_duplicate(ipif);
1569 		ipif_down_tail(ipif);
1570 	}
1571 	freemsg(mp);
1572 	ipsq_current_finish(ipsq);
1573 }
1574 
1575 /*
1576  * ill_down_start is called when we want to down this ill and bring it up again
1577  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1578  * all interfaces, but don't tear down any plumbing.
1579  */
1580 boolean_t
1581 ill_down_start(queue_t *q, mblk_t *mp)
1582 {
1583 	ill_t	*ill = q->q_ptr;
1584 	ipif_t	*ipif;
1585 
1586 	ASSERT(IAM_WRITER_ILL(ill));
1587 
1588 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1589 		(void) ipif_down(ipif, NULL, NULL);
1590 
1591 	ill_down(ill);
1592 
1593 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1594 
1595 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1596 
1597 	/*
1598 	 * Atomically test and add the pending mp if references are active.
1599 	 */
1600 	mutex_enter(&ill->ill_lock);
1601 	if (!ill_is_quiescent(ill)) {
1602 		/* call cannot fail since `conn_t *' argument is NULL */
1603 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1604 		    mp, ILL_DOWN);
1605 		mutex_exit(&ill->ill_lock);
1606 		return (B_FALSE);
1607 	}
1608 	mutex_exit(&ill->ill_lock);
1609 	return (B_TRUE);
1610 }
1611 
1612 static void
1613 ill_down(ill_t *ill)
1614 {
1615 	ip_stack_t	*ipst = ill->ill_ipst;
1616 
1617 	/* Blow off any IREs dependent on this ILL. */
1618 	ire_walk(ill_downi, (char *)ill, ipst);
1619 
1620 	/* Remove any conn_*_ill depending on this ill */
1621 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1622 
1623 	if (ill->ill_group != NULL) {
1624 		illgrp_delete(ill);
1625 	}
1626 }
1627 
1628 /*
1629  * ire_walk routine used to delete every IRE that depends on queues
1630  * associated with 'ill'.  (Always called as writer.)
1631  */
1632 static void
1633 ill_downi(ire_t *ire, char *ill_arg)
1634 {
1635 	ill_t	*ill = (ill_t *)ill_arg;
1636 
1637 	/*
1638 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1639 	 * interface X and ipif coming from interface Y, if interface
1640 	 * X and Y are part of the same IPMP group. Thus whenever interface
1641 	 * X goes down, remove all references to it by checking both
1642 	 * on ire_ipif and ire_stq.
1643 	 */
1644 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1645 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1646 		ire_delete(ire);
1647 	}
1648 }
1649 
1650 /*
1651  * Remove ire/nce from the fastpath list.
1652  */
1653 void
1654 ill_fastpath_nack(ill_t *ill)
1655 {
1656 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1657 }
1658 
1659 /* Consume an M_IOCACK of the fastpath probe. */
1660 void
1661 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1662 {
1663 	mblk_t	*mp1 = mp;
1664 
1665 	/*
1666 	 * If this was the first attempt turn on the fastpath probing.
1667 	 */
1668 	mutex_enter(&ill->ill_lock);
1669 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1670 		ill->ill_dlpi_fastpath_state = IDS_OK;
1671 	mutex_exit(&ill->ill_lock);
1672 
1673 	/* Free the M_IOCACK mblk, hold on to the data */
1674 	mp = mp->b_cont;
1675 	freeb(mp1);
1676 	if (mp == NULL)
1677 		return;
1678 	if (mp->b_cont != NULL) {
1679 		/*
1680 		 * Update all IRE's or NCE's that are waiting for
1681 		 * fastpath update.
1682 		 */
1683 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1684 		mp1 = mp->b_cont;
1685 		freeb(mp);
1686 		mp = mp1;
1687 	} else {
1688 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1689 	}
1690 
1691 	freeb(mp);
1692 }
1693 
1694 /*
1695  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1696  * The data portion of the request is a dl_unitdata_req_t template for
1697  * what we would send downstream in the absence of a fastpath confirmation.
1698  */
1699 int
1700 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1701 {
1702 	struct iocblk	*ioc;
1703 	mblk_t	*mp;
1704 
1705 	if (dlur_mp == NULL)
1706 		return (EINVAL);
1707 
1708 	mutex_enter(&ill->ill_lock);
1709 	switch (ill->ill_dlpi_fastpath_state) {
1710 	case IDS_FAILED:
1711 		/*
1712 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1713 		 * support it.
1714 		 */
1715 		mutex_exit(&ill->ill_lock);
1716 		return (ENOTSUP);
1717 	case IDS_UNKNOWN:
1718 		/* This is the first probe */
1719 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1720 		break;
1721 	default:
1722 		break;
1723 	}
1724 	mutex_exit(&ill->ill_lock);
1725 
1726 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1727 		return (EAGAIN);
1728 
1729 	mp->b_cont = copyb(dlur_mp);
1730 	if (mp->b_cont == NULL) {
1731 		freeb(mp);
1732 		return (EAGAIN);
1733 	}
1734 
1735 	ioc = (struct iocblk *)mp->b_rptr;
1736 	ioc->ioc_count = msgdsize(mp->b_cont);
1737 
1738 	putnext(ill->ill_wq, mp);
1739 	return (0);
1740 }
1741 
1742 void
1743 ill_capability_probe(ill_t *ill)
1744 {
1745 	/*
1746 	 * Do so only if capabilities are still unknown.
1747 	 */
1748 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1749 		return;
1750 
1751 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1752 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1753 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1754 }
1755 
1756 void
1757 ill_capability_reset(ill_t *ill)
1758 {
1759 	mblk_t *sc_mp = NULL;
1760 	mblk_t *tmp;
1761 
1762 	/*
1763 	 * Note here that we reset the state to UNKNOWN, and later send
1764 	 * down the DL_CAPABILITY_REQ without first setting the state to
1765 	 * INPROGRESS.  We do this in order to distinguish the
1766 	 * DL_CAPABILITY_ACK response which may come back in response to
1767 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1768 	 * also handle the case where the driver doesn't send us back
1769 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1770 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1771 	 * features are turned off until the state reaches IDS_OK.
1772 	 */
1773 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1774 	ill->ill_capab_reneg = B_FALSE;
1775 
1776 	/*
1777 	 * Disable sub-capabilities and request a list of sub-capability
1778 	 * messages which will be sent down to the driver.  Each handler
1779 	 * allocates the corresponding dl_capability_sub_t inside an
1780 	 * mblk, and links it to the existing sc_mp mblk, or return it
1781 	 * as sc_mp if it's the first sub-capability (the passed in
1782 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1783 	 * sc_mp will be pulled-up, before passing it downstream.
1784 	 */
1785 	ill_capability_mdt_reset(ill, &sc_mp);
1786 	ill_capability_hcksum_reset(ill, &sc_mp);
1787 	ill_capability_zerocopy_reset(ill, &sc_mp);
1788 	ill_capability_ipsec_reset(ill, &sc_mp);
1789 	ill_capability_dls_reset(ill, &sc_mp);
1790 	ill_capability_lso_reset(ill, &sc_mp);
1791 
1792 	/* Nothing to send down in order to disable the capabilities? */
1793 	if (sc_mp == NULL)
1794 		return;
1795 
1796 	tmp = msgpullup(sc_mp, -1);
1797 	freemsg(sc_mp);
1798 	if ((sc_mp = tmp) == NULL) {
1799 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1800 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1801 		return;
1802 	}
1803 
1804 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1805 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1806 }
1807 
1808 /*
1809  * Request or set new-style hardware capabilities supported by DLS provider.
1810  */
1811 static void
1812 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1813 {
1814 	mblk_t *mp;
1815 	dl_capability_req_t *capb;
1816 	size_t size = 0;
1817 	uint8_t *ptr;
1818 
1819 	if (reqp != NULL)
1820 		size = MBLKL(reqp);
1821 
1822 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1823 	if (mp == NULL) {
1824 		freemsg(reqp);
1825 		return;
1826 	}
1827 	ptr = mp->b_rptr;
1828 
1829 	capb = (dl_capability_req_t *)ptr;
1830 	ptr += sizeof (dl_capability_req_t);
1831 
1832 	if (reqp != NULL) {
1833 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1834 		capb->dl_sub_length = size;
1835 		bcopy(reqp->b_rptr, ptr, size);
1836 		ptr += size;
1837 		mp->b_cont = reqp->b_cont;
1838 		freeb(reqp);
1839 	}
1840 	ASSERT(ptr == mp->b_wptr);
1841 
1842 	ill_dlpi_send(ill, mp);
1843 }
1844 
1845 static void
1846 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1847 {
1848 	dl_capab_id_t *id_ic;
1849 	uint_t sub_dl_cap = outers->dl_cap;
1850 	dl_capability_sub_t *inners;
1851 	uint8_t *capend;
1852 
1853 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1854 
1855 	/*
1856 	 * Note: range checks here are not absolutely sufficient to
1857 	 * make us robust against malformed messages sent by drivers;
1858 	 * this is in keeping with the rest of IP's dlpi handling.
1859 	 * (Remember, it's coming from something else in the kernel
1860 	 * address space)
1861 	 */
1862 
1863 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1864 	if (capend > mp->b_wptr) {
1865 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1866 		    "malformed sub-capability too long for mblk");
1867 		return;
1868 	}
1869 
1870 	id_ic = (dl_capab_id_t *)(outers + 1);
1871 
1872 	if (outers->dl_length < sizeof (*id_ic) ||
1873 	    (inners = &id_ic->id_subcap,
1874 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1875 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1876 		    "encapsulated capab type %d too long for mblk",
1877 		    inners->dl_cap);
1878 		return;
1879 	}
1880 
1881 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1882 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1883 		    "isn't as expected; pass-thru module(s) detected, "
1884 		    "discarding capability\n", inners->dl_cap));
1885 		return;
1886 	}
1887 
1888 	/* Process the encapsulated sub-capability */
1889 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1890 }
1891 
1892 /*
1893  * Process Multidata Transmit capability negotiation ack received from a
1894  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1895  * DL_CAPABILITY_ACK message.
1896  */
1897 static void
1898 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1899 {
1900 	mblk_t *nmp = NULL;
1901 	dl_capability_req_t *oc;
1902 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1903 	ill_mdt_capab_t **ill_mdt_capab;
1904 	uint_t sub_dl_cap = isub->dl_cap;
1905 	uint8_t *capend;
1906 
1907 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1908 
1909 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1910 
1911 	/*
1912 	 * Note: range checks here are not absolutely sufficient to
1913 	 * make us robust against malformed messages sent by drivers;
1914 	 * this is in keeping with the rest of IP's dlpi handling.
1915 	 * (Remember, it's coming from something else in the kernel
1916 	 * address space)
1917 	 */
1918 
1919 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1920 	if (capend > mp->b_wptr) {
1921 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1922 		    "malformed sub-capability too long for mblk");
1923 		return;
1924 	}
1925 
1926 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1927 
1928 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1929 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1930 		    "unsupported MDT sub-capability (version %d, expected %d)",
1931 		    mdt_ic->mdt_version, MDT_VERSION_2);
1932 		return;
1933 	}
1934 
1935 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1936 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1937 		    "capability isn't as expected; pass-thru module(s) "
1938 		    "detected, discarding capability\n"));
1939 		return;
1940 	}
1941 
1942 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1943 
1944 		if (*ill_mdt_capab == NULL) {
1945 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1946 			    KM_NOSLEEP);
1947 
1948 			if (*ill_mdt_capab == NULL) {
1949 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1950 				    "could not enable MDT version %d "
1951 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1952 				    ill->ill_name);
1953 				return;
1954 			}
1955 		}
1956 
1957 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1958 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1959 		    "header spaces, %d max pld bufs, %d span limit)\n",
1960 		    ill->ill_name, MDT_VERSION_2,
1961 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1962 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1963 
1964 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1965 		(*ill_mdt_capab)->ill_mdt_on = 1;
1966 		/*
1967 		 * Round the following values to the nearest 32-bit; ULP
1968 		 * may further adjust them to accomodate for additional
1969 		 * protocol headers.  We pass these values to ULP during
1970 		 * bind time.
1971 		 */
1972 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1973 		    roundup(mdt_ic->mdt_hdr_head, 4);
1974 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1975 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1976 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1977 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1978 
1979 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1980 	} else {
1981 		uint_t size;
1982 		uchar_t *rptr;
1983 
1984 		size = sizeof (dl_capability_req_t) +
1985 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1986 
1987 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1988 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1989 			    "could not enable MDT for %s (ENOMEM)\n",
1990 			    ill->ill_name);
1991 			return;
1992 		}
1993 
1994 		rptr = nmp->b_rptr;
1995 		/* initialize dl_capability_req_t */
1996 		oc = (dl_capability_req_t *)nmp->b_rptr;
1997 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1998 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1999 		    sizeof (dl_capab_mdt_t);
2000 		nmp->b_rptr += sizeof (dl_capability_req_t);
2001 
2002 		/* initialize dl_capability_sub_t */
2003 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2004 		nmp->b_rptr += sizeof (*isub);
2005 
2006 		/* initialize dl_capab_mdt_t */
2007 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2008 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2009 
2010 		nmp->b_rptr = rptr;
2011 
2012 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2013 		    "to enable MDT version %d\n", ill->ill_name,
2014 		    MDT_VERSION_2));
2015 
2016 		/* set ENABLE flag */
2017 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2018 
2019 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2020 		ill_dlpi_send(ill, nmp);
2021 	}
2022 }
2023 
2024 static void
2025 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2026 {
2027 	mblk_t *mp;
2028 	dl_capab_mdt_t *mdt_subcap;
2029 	dl_capability_sub_t *dl_subcap;
2030 	int size;
2031 
2032 	if (!ILL_MDT_CAPABLE(ill))
2033 		return;
2034 
2035 	ASSERT(ill->ill_mdt_capab != NULL);
2036 	/*
2037 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2038 	 * structure since it's possible that another thread is still
2039 	 * referring to it.  The structure only gets deallocated when
2040 	 * we destroy the ill.
2041 	 */
2042 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2043 
2044 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2045 
2046 	mp = allocb(size, BPRI_HI);
2047 	if (mp == NULL) {
2048 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2049 		    "request to disable MDT\n"));
2050 		return;
2051 	}
2052 
2053 	mp->b_wptr = mp->b_rptr + size;
2054 
2055 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2056 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2057 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2058 
2059 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2060 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2061 	mdt_subcap->mdt_flags = 0;
2062 	mdt_subcap->mdt_hdr_head = 0;
2063 	mdt_subcap->mdt_hdr_tail = 0;
2064 
2065 	if (*sc_mp != NULL)
2066 		linkb(*sc_mp, mp);
2067 	else
2068 		*sc_mp = mp;
2069 }
2070 
2071 /*
2072  * Send a DL_NOTIFY_REQ to the specified ill to enable
2073  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2074  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2075  * acceleration.
2076  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2077  */
2078 static boolean_t
2079 ill_enable_promisc_notify(ill_t *ill)
2080 {
2081 	mblk_t *mp;
2082 	dl_notify_req_t *req;
2083 
2084 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2085 
2086 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2087 	if (mp == NULL)
2088 		return (B_FALSE);
2089 
2090 	req = (dl_notify_req_t *)mp->b_rptr;
2091 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2092 	    DL_NOTE_PROMISC_OFF_PHYS;
2093 
2094 	ill_dlpi_send(ill, mp);
2095 
2096 	return (B_TRUE);
2097 }
2098 
2099 /*
2100  * Allocate an IPsec capability request which will be filled by our
2101  * caller to turn on support for one or more algorithms.
2102  */
2103 static mblk_t *
2104 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2105 {
2106 	mblk_t *nmp;
2107 	dl_capability_req_t	*ocap;
2108 	dl_capab_ipsec_t	*ocip;
2109 	dl_capab_ipsec_t	*icip;
2110 	uint8_t			*ptr;
2111 	icip = (dl_capab_ipsec_t *)(isub + 1);
2112 
2113 	/*
2114 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2115 	 * PROMISC_ON/OFF notification from the provider. We need to
2116 	 * do this before enabling the algorithms to avoid leakage of
2117 	 * cleartext packets.
2118 	 */
2119 
2120 	if (!ill_enable_promisc_notify(ill))
2121 		return (NULL);
2122 
2123 	/*
2124 	 * Allocate new mblk which will contain a new capability
2125 	 * request to enable the capabilities.
2126 	 */
2127 
2128 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2129 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2130 	if (nmp == NULL)
2131 		return (NULL);
2132 
2133 	ptr = nmp->b_rptr;
2134 
2135 	/* initialize dl_capability_req_t */
2136 	ocap = (dl_capability_req_t *)ptr;
2137 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2138 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2139 	ptr += sizeof (dl_capability_req_t);
2140 
2141 	/* initialize dl_capability_sub_t */
2142 	bcopy(isub, ptr, sizeof (*isub));
2143 	ptr += sizeof (*isub);
2144 
2145 	/* initialize dl_capab_ipsec_t */
2146 	ocip = (dl_capab_ipsec_t *)ptr;
2147 	bcopy(icip, ocip, sizeof (*icip));
2148 
2149 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2150 	return (nmp);
2151 }
2152 
2153 /*
2154  * Process an IPsec capability negotiation ack received from a DLS Provider.
2155  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2156  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2157  */
2158 static void
2159 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2160 {
2161 	dl_capab_ipsec_t	*icip;
2162 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2163 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2164 	uint_t cipher, nciphers;
2165 	mblk_t *nmp;
2166 	uint_t alg_len;
2167 	boolean_t need_sadb_dump;
2168 	uint_t sub_dl_cap = isub->dl_cap;
2169 	ill_ipsec_capab_t **ill_capab;
2170 	uint64_t ill_capab_flag;
2171 	uint8_t *capend, *ciphend;
2172 	boolean_t sadb_resync;
2173 
2174 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2175 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2176 
2177 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2178 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2179 		ill_capab_flag = ILL_CAPAB_AH;
2180 	} else {
2181 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2182 		ill_capab_flag = ILL_CAPAB_ESP;
2183 	}
2184 
2185 	/*
2186 	 * If the ill capability structure exists, then this incoming
2187 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2188 	 * If this is so, then we'd need to resynchronize the SADB
2189 	 * after re-enabling the offloaded ciphers.
2190 	 */
2191 	sadb_resync = (*ill_capab != NULL);
2192 
2193 	/*
2194 	 * Note: range checks here are not absolutely sufficient to
2195 	 * make us robust against malformed messages sent by drivers;
2196 	 * this is in keeping with the rest of IP's dlpi handling.
2197 	 * (Remember, it's coming from something else in the kernel
2198 	 * address space)
2199 	 */
2200 
2201 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2202 	if (capend > mp->b_wptr) {
2203 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2204 		    "malformed sub-capability too long for mblk");
2205 		return;
2206 	}
2207 
2208 	/*
2209 	 * There are two types of acks we process here:
2210 	 * 1. acks in reply to a (first form) generic capability req
2211 	 *    (no ENABLE flag set)
2212 	 * 2. acks in reply to a ENABLE capability req.
2213 	 *    (ENABLE flag set)
2214 	 *
2215 	 * We process the subcapability passed as argument as follows:
2216 	 * 1 do initializations
2217 	 *   1.1 initialize nmp = NULL
2218 	 *   1.2 set need_sadb_dump to B_FALSE
2219 	 * 2 for each cipher in subcapability:
2220 	 *   2.1 if ENABLE flag is set:
2221 	 *	2.1.1 update per-ill ipsec capabilities info
2222 	 *	2.1.2 set need_sadb_dump to B_TRUE
2223 	 *   2.2 if ENABLE flag is not set:
2224 	 *	2.2.1 if nmp is NULL:
2225 	 *		2.2.1.1 allocate and initialize nmp
2226 	 *		2.2.1.2 init current pos in nmp
2227 	 *	2.2.2 copy current cipher to current pos in nmp
2228 	 *	2.2.3 set ENABLE flag in nmp
2229 	 *	2.2.4 update current pos
2230 	 * 3 if nmp is not equal to NULL, send enable request
2231 	 *   3.1 send capability request
2232 	 * 4 if need_sadb_dump is B_TRUE
2233 	 *   4.1 enable promiscuous on/off notifications
2234 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2235 	 *	AH or ESP SA's to interface.
2236 	 */
2237 
2238 	nmp = NULL;
2239 	oalg = NULL;
2240 	need_sadb_dump = B_FALSE;
2241 	icip = (dl_capab_ipsec_t *)(isub + 1);
2242 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2243 
2244 	nciphers = icip->cip_nciphers;
2245 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2246 
2247 	if (ciphend > capend) {
2248 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2249 		    "too many ciphers for sub-capability len");
2250 		return;
2251 	}
2252 
2253 	for (cipher = 0; cipher < nciphers; cipher++) {
2254 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2255 
2256 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2257 			/*
2258 			 * TBD: when we provide a way to disable capabilities
2259 			 * from above, need to manage the request-pending state
2260 			 * and fail if we were not expecting this ACK.
2261 			 */
2262 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2263 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2264 
2265 			/*
2266 			 * Update IPsec capabilities for this ill
2267 			 */
2268 
2269 			if (*ill_capab == NULL) {
2270 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2271 				    ("ill_capability_ipsec_ack: "
2272 				    "allocating ipsec_capab for ill\n"));
2273 				*ill_capab = ill_ipsec_capab_alloc();
2274 
2275 				if (*ill_capab == NULL) {
2276 					cmn_err(CE_WARN,
2277 					    "ill_capability_ipsec_ack: "
2278 					    "could not enable IPsec Hardware "
2279 					    "acceleration for %s (ENOMEM)\n",
2280 					    ill->ill_name);
2281 					return;
2282 				}
2283 			}
2284 
2285 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2286 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2287 
2288 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2289 				cmn_err(CE_WARN,
2290 				    "ill_capability_ipsec_ack: "
2291 				    "malformed IPsec algorithm id %d",
2292 				    ialg->alg_prim);
2293 				continue;
2294 			}
2295 
2296 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2297 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2298 				    ialg->alg_prim);
2299 			} else {
2300 				ipsec_capab_algparm_t *alp;
2301 
2302 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2303 				    ialg->alg_prim);
2304 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2305 				    ialg->alg_prim)) {
2306 					cmn_err(CE_WARN,
2307 					    "ill_capability_ipsec_ack: "
2308 					    "no space for IPsec alg id %d",
2309 					    ialg->alg_prim);
2310 					continue;
2311 				}
2312 				alp = &((*ill_capab)->encr_algparm[
2313 				    ialg->alg_prim]);
2314 				alp->minkeylen = ialg->alg_minbits;
2315 				alp->maxkeylen = ialg->alg_maxbits;
2316 			}
2317 			ill->ill_capabilities |= ill_capab_flag;
2318 			/*
2319 			 * indicate that a capability was enabled, which
2320 			 * will be used below to kick off a SADB dump
2321 			 * to the ill.
2322 			 */
2323 			need_sadb_dump = B_TRUE;
2324 		} else {
2325 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2326 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2327 			    ialg->alg_prim));
2328 
2329 			if (nmp == NULL) {
2330 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2331 				if (nmp == NULL) {
2332 					/*
2333 					 * Sending the PROMISC_ON/OFF
2334 					 * notification request failed.
2335 					 * We cannot enable the algorithms
2336 					 * since the Provider will not
2337 					 * notify IP of promiscous mode
2338 					 * changes, which could lead
2339 					 * to leakage of packets.
2340 					 */
2341 					cmn_err(CE_WARN,
2342 					    "ill_capability_ipsec_ack: "
2343 					    "could not enable IPsec Hardware "
2344 					    "acceleration for %s (ENOMEM)\n",
2345 					    ill->ill_name);
2346 					return;
2347 				}
2348 				/* ptr to current output alg specifier */
2349 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2350 			}
2351 
2352 			/*
2353 			 * Copy current alg specifier, set ENABLE
2354 			 * flag, and advance to next output alg.
2355 			 * For now we enable all IPsec capabilities.
2356 			 */
2357 			ASSERT(oalg != NULL);
2358 			bcopy(ialg, oalg, alg_len);
2359 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2360 			nmp->b_wptr += alg_len;
2361 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2362 		}
2363 
2364 		/* move to next input algorithm specifier */
2365 		ialg = (dl_capab_ipsec_alg_t *)
2366 		    ((char *)ialg + alg_len);
2367 	}
2368 
2369 	if (nmp != NULL)
2370 		/*
2371 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2372 		 * IPsec hardware acceleration.
2373 		 */
2374 		ill_dlpi_send(ill, nmp);
2375 
2376 	if (need_sadb_dump)
2377 		/*
2378 		 * An acknowledgement corresponding to a request to
2379 		 * enable acceleration was received, notify SADB.
2380 		 */
2381 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2382 }
2383 
2384 /*
2385  * Given an mblk with enough space in it, create sub-capability entries for
2386  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2387  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2388  * in preparation for the reset the DL_CAPABILITY_REQ message.
2389  */
2390 static void
2391 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2392     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2393 {
2394 	dl_capab_ipsec_t *oipsec;
2395 	dl_capab_ipsec_alg_t *oalg;
2396 	dl_capability_sub_t *dl_subcap;
2397 	int i, k;
2398 
2399 	ASSERT(nciphers > 0);
2400 	ASSERT(ill_cap != NULL);
2401 	ASSERT(mp != NULL);
2402 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2403 
2404 	/* dl_capability_sub_t for "stype" */
2405 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2406 	dl_subcap->dl_cap = stype;
2407 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2408 	mp->b_wptr += sizeof (dl_capability_sub_t);
2409 
2410 	/* dl_capab_ipsec_t for "stype" */
2411 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2412 	oipsec->cip_version = 1;
2413 	oipsec->cip_nciphers = nciphers;
2414 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2415 
2416 	/* create entries for "stype" AUTH ciphers */
2417 	for (i = 0; i < ill_cap->algs_size; i++) {
2418 		for (k = 0; k < BITSPERBYTE; k++) {
2419 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2420 				continue;
2421 
2422 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2423 			bzero((void *)oalg, sizeof (*oalg));
2424 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2425 			oalg->alg_prim = k + (BITSPERBYTE * i);
2426 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2427 		}
2428 	}
2429 	/* create entries for "stype" ENCR ciphers */
2430 	for (i = 0; i < ill_cap->algs_size; i++) {
2431 		for (k = 0; k < BITSPERBYTE; k++) {
2432 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2433 				continue;
2434 
2435 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2436 			bzero((void *)oalg, sizeof (*oalg));
2437 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2438 			oalg->alg_prim = k + (BITSPERBYTE * i);
2439 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2440 		}
2441 	}
2442 }
2443 
2444 /*
2445  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2446  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2447  * POPC instruction, but our macro is more flexible for an arbitrary length
2448  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2449  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2450  * stays that way, we can reduce the number of iterations required.
2451  */
2452 #define	COUNT_1S(val, sum) {					\
2453 	uint8_t x = val & 0xff;					\
2454 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2455 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2456 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2457 }
2458 
2459 /* ARGSUSED */
2460 static void
2461 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2462 {
2463 	mblk_t *mp;
2464 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2465 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2466 	uint64_t ill_capabilities = ill->ill_capabilities;
2467 	int ah_cnt = 0, esp_cnt = 0;
2468 	int ah_len = 0, esp_len = 0;
2469 	int i, size = 0;
2470 
2471 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2472 		return;
2473 
2474 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2475 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2476 
2477 	/* Find out the number of ciphers for AH */
2478 	if (cap_ah != NULL) {
2479 		for (i = 0; i < cap_ah->algs_size; i++) {
2480 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2481 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2482 		}
2483 		if (ah_cnt > 0) {
2484 			size += sizeof (dl_capability_sub_t) +
2485 			    sizeof (dl_capab_ipsec_t);
2486 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2487 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2488 			size += ah_len;
2489 		}
2490 	}
2491 
2492 	/* Find out the number of ciphers for ESP */
2493 	if (cap_esp != NULL) {
2494 		for (i = 0; i < cap_esp->algs_size; i++) {
2495 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2496 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2497 		}
2498 		if (esp_cnt > 0) {
2499 			size += sizeof (dl_capability_sub_t) +
2500 			    sizeof (dl_capab_ipsec_t);
2501 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2502 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2503 			size += esp_len;
2504 		}
2505 	}
2506 
2507 	if (size == 0) {
2508 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2509 		    "there's nothing to reset\n"));
2510 		return;
2511 	}
2512 
2513 	mp = allocb(size, BPRI_HI);
2514 	if (mp == NULL) {
2515 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2516 		    "request to disable IPSEC Hardware Acceleration\n"));
2517 		return;
2518 	}
2519 
2520 	/*
2521 	 * Clear the capability flags for IPsec HA but retain the ill
2522 	 * capability structures since it's possible that another thread
2523 	 * is still referring to them.  The structures only get deallocated
2524 	 * when we destroy the ill.
2525 	 *
2526 	 * Various places check the flags to see if the ill is capable of
2527 	 * hardware acceleration, and by clearing them we ensure that new
2528 	 * outbound IPsec packets are sent down encrypted.
2529 	 */
2530 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2531 
2532 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2533 	if (ah_cnt > 0) {
2534 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2535 		    cap_ah, mp);
2536 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2537 	}
2538 
2539 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2540 	if (esp_cnt > 0) {
2541 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2542 		    cap_esp, mp);
2543 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2544 	}
2545 
2546 	/*
2547 	 * At this point we've composed a bunch of sub-capabilities to be
2548 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2549 	 * by the caller.  Upon receiving this reset message, the driver
2550 	 * must stop inbound decryption (by destroying all inbound SAs)
2551 	 * and let the corresponding packets come in encrypted.
2552 	 */
2553 
2554 	if (*sc_mp != NULL)
2555 		linkb(*sc_mp, mp);
2556 	else
2557 		*sc_mp = mp;
2558 }
2559 
2560 static void
2561 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2562     boolean_t encapsulated)
2563 {
2564 	boolean_t legacy = B_FALSE;
2565 
2566 	/*
2567 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2568 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2569 	 * instructed the driver to disable its advertised capabilities,
2570 	 * so there's no point in accepting any response at this moment.
2571 	 */
2572 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2573 		return;
2574 
2575 	/*
2576 	 * Note that only the following two sub-capabilities may be
2577 	 * considered as "legacy", since their original definitions
2578 	 * do not incorporate the dl_mid_t module ID token, and hence
2579 	 * may require the use of the wrapper sub-capability.
2580 	 */
2581 	switch (subp->dl_cap) {
2582 	case DL_CAPAB_IPSEC_AH:
2583 	case DL_CAPAB_IPSEC_ESP:
2584 		legacy = B_TRUE;
2585 		break;
2586 	}
2587 
2588 	/*
2589 	 * For legacy sub-capabilities which don't incorporate a queue_t
2590 	 * pointer in their structures, discard them if we detect that
2591 	 * there are intermediate modules in between IP and the driver.
2592 	 */
2593 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2594 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2595 		    "%d discarded; %d module(s) present below IP\n",
2596 		    subp->dl_cap, ill->ill_lmod_cnt));
2597 		return;
2598 	}
2599 
2600 	switch (subp->dl_cap) {
2601 	case DL_CAPAB_IPSEC_AH:
2602 	case DL_CAPAB_IPSEC_ESP:
2603 		ill_capability_ipsec_ack(ill, mp, subp);
2604 		break;
2605 	case DL_CAPAB_MDT:
2606 		ill_capability_mdt_ack(ill, mp, subp);
2607 		break;
2608 	case DL_CAPAB_HCKSUM:
2609 		ill_capability_hcksum_ack(ill, mp, subp);
2610 		break;
2611 	case DL_CAPAB_ZEROCOPY:
2612 		ill_capability_zerocopy_ack(ill, mp, subp);
2613 		break;
2614 	case DL_CAPAB_POLL:
2615 		if (!SOFT_RINGS_ENABLED())
2616 			ill_capability_dls_ack(ill, mp, subp);
2617 		break;
2618 	case DL_CAPAB_SOFT_RING:
2619 		if (SOFT_RINGS_ENABLED())
2620 			ill_capability_dls_ack(ill, mp, subp);
2621 		break;
2622 	case DL_CAPAB_LSO:
2623 		ill_capability_lso_ack(ill, mp, subp);
2624 		break;
2625 	default:
2626 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2627 		    subp->dl_cap));
2628 	}
2629 }
2630 
2631 /*
2632  * As part of negotiating polling capability, the driver tells us
2633  * the default (or normal) blanking interval and packet threshold
2634  * (the receive timer fires if blanking interval is reached or
2635  * the packet threshold is reached).
2636  *
2637  * As part of manipulating the polling interval, we always use our
2638  * estimated interval (avg service time * number of packets queued
2639  * on the squeue) but we try to blank for a minimum of
2640  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2641  * packet threshold during this time. When we are not in polling mode
2642  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2643  * rr_min_blank_ratio but up the packet cnt by a ratio of
2644  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2645  * possible although for a shorter interval.
2646  */
2647 #define	RR_MAX_BLANK_RATIO	20
2648 #define	RR_MIN_BLANK_RATIO	10
2649 #define	RR_MAX_PKT_CNT_RATIO	3
2650 #define	RR_MIN_PKT_CNT_RATIO	3
2651 
2652 /*
2653  * These can be tuned via /etc/system.
2654  */
2655 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2656 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2657 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2658 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2659 
2660 static mac_resource_handle_t
2661 ill_ring_add(void *arg, mac_resource_t *mrp)
2662 {
2663 	ill_t			*ill = (ill_t *)arg;
2664 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2665 	ill_rx_ring_t		*rx_ring;
2666 	int			ip_rx_index;
2667 
2668 	ASSERT(mrp != NULL);
2669 	if (mrp->mr_type != MAC_RX_FIFO) {
2670 		return (NULL);
2671 	}
2672 	ASSERT(ill != NULL);
2673 	ASSERT(ill->ill_dls_capab != NULL);
2674 
2675 	mutex_enter(&ill->ill_lock);
2676 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2677 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2678 		ASSERT(rx_ring != NULL);
2679 
2680 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2681 			time_t normal_blank_time =
2682 			    mrfp->mrf_normal_blank_time;
2683 			uint_t normal_pkt_cnt =
2684 			    mrfp->mrf_normal_pkt_count;
2685 
2686 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2687 
2688 	rx_ring->rr_blank = mrfp->mrf_blank;
2689 	rx_ring->rr_handle = mrfp->mrf_arg;
2690 	rx_ring->rr_ill = ill;
2691 	rx_ring->rr_normal_blank_time = normal_blank_time;
2692 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2693 
2694 			rx_ring->rr_max_blank_time =
2695 			    normal_blank_time * rr_max_blank_ratio;
2696 			rx_ring->rr_min_blank_time =
2697 			    normal_blank_time * rr_min_blank_ratio;
2698 			rx_ring->rr_max_pkt_cnt =
2699 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2700 			rx_ring->rr_min_pkt_cnt =
2701 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2702 
2703 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2704 			mutex_exit(&ill->ill_lock);
2705 
2706 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2707 			    (int), ip_rx_index);
2708 			return ((mac_resource_handle_t)rx_ring);
2709 		}
2710 	}
2711 
2712 	/*
2713 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2714 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2715 	 * should be made configurable. Meanwhile it cause no panic because
2716 	 * driver will pass ip_input a NULL handle which will make
2717 	 * IP allocate the default squeue and Polling mode will not
2718 	 * be used for this ring.
2719 	 */
2720 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2721 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2722 
2723 	mutex_exit(&ill->ill_lock);
2724 	return (NULL);
2725 }
2726 
2727 static boolean_t
2728 ill_capability_dls_init(ill_t *ill)
2729 {
2730 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2731 	conn_t 			*connp;
2732 	size_t			sz;
2733 	ip_stack_t *ipst = ill->ill_ipst;
2734 
2735 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2736 		if (ill_dls == NULL) {
2737 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2738 			    "soft_ring enabled for ill=%s (%p) but data "
2739 			    "structs uninitialized\n", ill->ill_name,
2740 			    (void *)ill);
2741 		}
2742 		return (B_TRUE);
2743 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2744 		if (ill_dls == NULL) {
2745 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2746 			    "polling enabled for ill=%s (%p) but data "
2747 			    "structs uninitialized\n", ill->ill_name,
2748 			    (void *)ill);
2749 		}
2750 		return (B_TRUE);
2751 	}
2752 
2753 	if (ill_dls != NULL) {
2754 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2755 		/* Soft_Ring or polling is being re-enabled */
2756 
2757 		connp = ill_dls->ill_unbind_conn;
2758 		ASSERT(rx_ring != NULL);
2759 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2760 		bzero((void *)rx_ring,
2761 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2762 		ill_dls->ill_ring_tbl = rx_ring;
2763 		ill_dls->ill_unbind_conn = connp;
2764 		return (B_TRUE);
2765 	}
2766 
2767 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2768 	    ipst->ips_netstack)) == NULL)
2769 		return (B_FALSE);
2770 
2771 	sz = sizeof (ill_dls_capab_t);
2772 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2773 
2774 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2775 	if (ill_dls == NULL) {
2776 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2777 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2778 		    (void *)ill);
2779 		CONN_DEC_REF(connp);
2780 		return (B_FALSE);
2781 	}
2782 
2783 	/* Allocate space to hold ring table */
2784 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2785 	ill->ill_dls_capab = ill_dls;
2786 	ill_dls->ill_unbind_conn = connp;
2787 	return (B_TRUE);
2788 }
2789 
2790 /*
2791  * ill_capability_dls_disable: disable soft_ring and/or polling
2792  * capability. Since any of the rings might already be in use, need
2793  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2794  * direct calls if necessary.
2795  */
2796 static void
2797 ill_capability_dls_disable(ill_t *ill)
2798 {
2799 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2800 
2801 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2802 		ip_squeue_clean_all(ill);
2803 		ill_dls->ill_tx = NULL;
2804 		ill_dls->ill_tx_handle = NULL;
2805 		ill_dls->ill_dls_change_status = NULL;
2806 		ill_dls->ill_dls_bind = NULL;
2807 		ill_dls->ill_dls_unbind = NULL;
2808 	}
2809 
2810 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2811 }
2812 
2813 static void
2814 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2815     dl_capability_sub_t *isub)
2816 {
2817 	uint_t			size;
2818 	uchar_t			*rptr;
2819 	dl_capab_dls_t	dls, *odls;
2820 	ill_dls_capab_t	*ill_dls;
2821 	mblk_t			*nmp = NULL;
2822 	dl_capability_req_t	*ocap;
2823 	uint_t			sub_dl_cap = isub->dl_cap;
2824 
2825 	if (!ill_capability_dls_init(ill))
2826 		return;
2827 	ill_dls = ill->ill_dls_capab;
2828 
2829 	/* Copy locally to get the members aligned */
2830 	bcopy((void *)idls, (void *)&dls,
2831 	    sizeof (dl_capab_dls_t));
2832 
2833 	/* Get the tx function and handle from dld */
2834 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2835 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2836 
2837 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2838 		ill_dls->ill_dls_change_status =
2839 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2840 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2841 		ill_dls->ill_dls_unbind =
2842 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2843 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2844 	}
2845 
2846 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2847 	    isub->dl_length;
2848 
2849 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2850 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2851 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2852 		    ill->ill_name, (void *)ill);
2853 		return;
2854 	}
2855 
2856 	/* initialize dl_capability_req_t */
2857 	rptr = nmp->b_rptr;
2858 	ocap = (dl_capability_req_t *)rptr;
2859 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2860 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2861 	rptr += sizeof (dl_capability_req_t);
2862 
2863 	/* initialize dl_capability_sub_t */
2864 	bcopy(isub, rptr, sizeof (*isub));
2865 	rptr += sizeof (*isub);
2866 
2867 	odls = (dl_capab_dls_t *)rptr;
2868 	rptr += sizeof (dl_capab_dls_t);
2869 
2870 	/* initialize dl_capab_dls_t to be sent down */
2871 	dls.dls_rx_handle = (uintptr_t)ill;
2872 	dls.dls_rx = (uintptr_t)ip_input;
2873 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2874 
2875 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2876 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2877 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2878 		dls.dls_flags = SOFT_RING_ENABLE;
2879 	} else {
2880 		dls.dls_flags = POLL_ENABLE;
2881 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2882 		    "to enable polling\n", ill->ill_name));
2883 	}
2884 	bcopy((void *)&dls, (void *)odls,
2885 	    sizeof (dl_capab_dls_t));
2886 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2887 	/*
2888 	 * nmp points to a DL_CAPABILITY_REQ message to
2889 	 * enable either soft_ring or polling
2890 	 */
2891 	ill_dlpi_send(ill, nmp);
2892 }
2893 
2894 static void
2895 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2896 {
2897 	mblk_t *mp;
2898 	dl_capab_dls_t *idls;
2899 	dl_capability_sub_t *dl_subcap;
2900 	int size;
2901 
2902 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2903 		return;
2904 
2905 	ASSERT(ill->ill_dls_capab != NULL);
2906 
2907 	size = sizeof (*dl_subcap) + sizeof (*idls);
2908 
2909 	mp = allocb(size, BPRI_HI);
2910 	if (mp == NULL) {
2911 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2912 		    "request to disable soft_ring\n"));
2913 		return;
2914 	}
2915 
2916 	mp->b_wptr = mp->b_rptr + size;
2917 
2918 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2919 	dl_subcap->dl_length = sizeof (*idls);
2920 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2921 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2922 	else
2923 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2924 
2925 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2926 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2927 		idls->dls_flags = SOFT_RING_DISABLE;
2928 	else
2929 		idls->dls_flags = POLL_DISABLE;
2930 
2931 	if (*sc_mp != NULL)
2932 		linkb(*sc_mp, mp);
2933 	else
2934 		*sc_mp = mp;
2935 }
2936 
2937 /*
2938  * Process a soft_ring/poll capability negotiation ack received
2939  * from a DLS Provider.isub must point to the sub-capability
2940  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2941  */
2942 static void
2943 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2944 {
2945 	dl_capab_dls_t		*idls;
2946 	uint_t			sub_dl_cap = isub->dl_cap;
2947 	uint8_t			*capend;
2948 
2949 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2950 	    sub_dl_cap == DL_CAPAB_POLL);
2951 
2952 	if (ill->ill_isv6)
2953 		return;
2954 
2955 	/*
2956 	 * Note: range checks here are not absolutely sufficient to
2957 	 * make us robust against malformed messages sent by drivers;
2958 	 * this is in keeping with the rest of IP's dlpi handling.
2959 	 * (Remember, it's coming from something else in the kernel
2960 	 * address space)
2961 	 */
2962 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2963 	if (capend > mp->b_wptr) {
2964 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2965 		    "malformed sub-capability too long for mblk");
2966 		return;
2967 	}
2968 
2969 	/*
2970 	 * There are two types of acks we process here:
2971 	 * 1. acks in reply to a (first form) generic capability req
2972 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2973 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2974 	 *    capability req.
2975 	 */
2976 	idls = (dl_capab_dls_t *)(isub + 1);
2977 
2978 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2979 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2980 		    "capability isn't as expected; pass-thru "
2981 		    "module(s) detected, discarding capability\n"));
2982 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2983 			/*
2984 			 * This is a capability renegotitation case.
2985 			 * The interface better be unusable at this
2986 			 * point other wise bad things will happen
2987 			 * if we disable direct calls on a running
2988 			 * and up interface.
2989 			 */
2990 			ill_capability_dls_disable(ill);
2991 		}
2992 		return;
2993 	}
2994 
2995 	switch (idls->dls_flags) {
2996 	default:
2997 		/* Disable if unknown flag */
2998 	case SOFT_RING_DISABLE:
2999 	case POLL_DISABLE:
3000 		ill_capability_dls_disable(ill);
3001 		break;
3002 	case SOFT_RING_CAPABLE:
3003 	case POLL_CAPABLE:
3004 		/*
3005 		 * If the capability was already enabled, its safe
3006 		 * to disable it first to get rid of stale information
3007 		 * and then start enabling it again.
3008 		 */
3009 		ill_capability_dls_disable(ill);
3010 		ill_capability_dls_capable(ill, idls, isub);
3011 		break;
3012 	case SOFT_RING_ENABLE:
3013 	case POLL_ENABLE:
3014 		mutex_enter(&ill->ill_lock);
3015 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3016 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3017 			ASSERT(ill->ill_dls_capab != NULL);
3018 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3019 		}
3020 		if (sub_dl_cap == DL_CAPAB_POLL &&
3021 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3022 			ASSERT(ill->ill_dls_capab != NULL);
3023 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3024 			ip1dbg(("ill_capability_dls_ack: interface %s "
3025 			    "has enabled polling\n", ill->ill_name));
3026 		}
3027 		mutex_exit(&ill->ill_lock);
3028 		break;
3029 	}
3030 }
3031 
3032 /*
3033  * Process a hardware checksum offload capability negotiation ack received
3034  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3035  * of a DL_CAPABILITY_ACK message.
3036  */
3037 static void
3038 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3039 {
3040 	dl_capability_req_t	*ocap;
3041 	dl_capab_hcksum_t	*ihck, *ohck;
3042 	ill_hcksum_capab_t	**ill_hcksum;
3043 	mblk_t			*nmp = NULL;
3044 	uint_t			sub_dl_cap = isub->dl_cap;
3045 	uint8_t			*capend;
3046 
3047 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3048 
3049 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3050 
3051 	/*
3052 	 * Note: range checks here are not absolutely sufficient to
3053 	 * make us robust against malformed messages sent by drivers;
3054 	 * this is in keeping with the rest of IP's dlpi handling.
3055 	 * (Remember, it's coming from something else in the kernel
3056 	 * address space)
3057 	 */
3058 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3059 	if (capend > mp->b_wptr) {
3060 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3061 		    "malformed sub-capability too long for mblk");
3062 		return;
3063 	}
3064 
3065 	/*
3066 	 * There are two types of acks we process here:
3067 	 * 1. acks in reply to a (first form) generic capability req
3068 	 *    (no ENABLE flag set)
3069 	 * 2. acks in reply to a ENABLE capability req.
3070 	 *    (ENABLE flag set)
3071 	 */
3072 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3073 
3074 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3075 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3076 		    "unsupported hardware checksum "
3077 		    "sub-capability (version %d, expected %d)",
3078 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3079 		return;
3080 	}
3081 
3082 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3083 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3084 		    "checksum capability isn't as expected; pass-thru "
3085 		    "module(s) detected, discarding capability\n"));
3086 		return;
3087 	}
3088 
3089 #define	CURR_HCKSUM_CAPAB				\
3090 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3091 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3092 
3093 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3094 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3095 		/* do ENABLE processing */
3096 		if (*ill_hcksum == NULL) {
3097 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3098 			    KM_NOSLEEP);
3099 
3100 			if (*ill_hcksum == NULL) {
3101 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3102 				    "could not enable hcksum version %d "
3103 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3104 				    ill->ill_name);
3105 				return;
3106 			}
3107 		}
3108 
3109 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3110 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3111 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3112 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3113 		    "has enabled hardware checksumming\n ",
3114 		    ill->ill_name));
3115 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3116 		/*
3117 		 * Enabling hardware checksum offload
3118 		 * Currently IP supports {TCP,UDP}/IPv4
3119 		 * partial and full cksum offload and
3120 		 * IPv4 header checksum offload.
3121 		 * Allocate new mblk which will
3122 		 * contain a new capability request
3123 		 * to enable hardware checksum offload.
3124 		 */
3125 		uint_t	size;
3126 		uchar_t	*rptr;
3127 
3128 		size = sizeof (dl_capability_req_t) +
3129 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3130 
3131 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3132 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3133 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3134 			    ill->ill_name);
3135 			return;
3136 		}
3137 
3138 		rptr = nmp->b_rptr;
3139 		/* initialize dl_capability_req_t */
3140 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3141 		ocap->dl_sub_offset =
3142 		    sizeof (dl_capability_req_t);
3143 		ocap->dl_sub_length =
3144 		    sizeof (dl_capability_sub_t) +
3145 		    isub->dl_length;
3146 		nmp->b_rptr += sizeof (dl_capability_req_t);
3147 
3148 		/* initialize dl_capability_sub_t */
3149 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3150 		nmp->b_rptr += sizeof (*isub);
3151 
3152 		/* initialize dl_capab_hcksum_t */
3153 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3154 		bcopy(ihck, ohck, sizeof (*ihck));
3155 
3156 		nmp->b_rptr = rptr;
3157 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3158 
3159 		/* Set ENABLE flag */
3160 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3161 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3162 
3163 		/*
3164 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3165 		 * hardware checksum acceleration.
3166 		 */
3167 		ill_dlpi_send(ill, nmp);
3168 	} else {
3169 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3170 		    "advertised %x hardware checksum capability flags\n",
3171 		    ill->ill_name, ihck->hcksum_txflags));
3172 	}
3173 }
3174 
3175 static void
3176 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3177 {
3178 	mblk_t *mp;
3179 	dl_capab_hcksum_t *hck_subcap;
3180 	dl_capability_sub_t *dl_subcap;
3181 	int size;
3182 
3183 	if (!ILL_HCKSUM_CAPABLE(ill))
3184 		return;
3185 
3186 	ASSERT(ill->ill_hcksum_capab != NULL);
3187 	/*
3188 	 * Clear the capability flag for hardware checksum offload but
3189 	 * retain the ill_hcksum_capab structure since it's possible that
3190 	 * another thread is still referring to it.  The structure only
3191 	 * gets deallocated when we destroy the ill.
3192 	 */
3193 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3194 
3195 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3196 
3197 	mp = allocb(size, BPRI_HI);
3198 	if (mp == NULL) {
3199 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3200 		    "request to disable hardware checksum offload\n"));
3201 		return;
3202 	}
3203 
3204 	mp->b_wptr = mp->b_rptr + size;
3205 
3206 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3207 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3208 	dl_subcap->dl_length = sizeof (*hck_subcap);
3209 
3210 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3211 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3212 	hck_subcap->hcksum_txflags = 0;
3213 
3214 	if (*sc_mp != NULL)
3215 		linkb(*sc_mp, mp);
3216 	else
3217 		*sc_mp = mp;
3218 }
3219 
3220 static void
3221 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3222 {
3223 	mblk_t *nmp = NULL;
3224 	dl_capability_req_t *oc;
3225 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3226 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3227 	uint_t sub_dl_cap = isub->dl_cap;
3228 	uint8_t *capend;
3229 
3230 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3231 
3232 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3233 
3234 	/*
3235 	 * Note: range checks here are not absolutely sufficient to
3236 	 * make us robust against malformed messages sent by drivers;
3237 	 * this is in keeping with the rest of IP's dlpi handling.
3238 	 * (Remember, it's coming from something else in the kernel
3239 	 * address space)
3240 	 */
3241 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3242 	if (capend > mp->b_wptr) {
3243 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3244 		    "malformed sub-capability too long for mblk");
3245 		return;
3246 	}
3247 
3248 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3249 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3250 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3251 		    "unsupported ZEROCOPY sub-capability (version %d, "
3252 		    "expected %d)", zc_ic->zerocopy_version,
3253 		    ZEROCOPY_VERSION_1);
3254 		return;
3255 	}
3256 
3257 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3258 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3259 		    "capability isn't as expected; pass-thru module(s) "
3260 		    "detected, discarding capability\n"));
3261 		return;
3262 	}
3263 
3264 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3265 		if (*ill_zerocopy_capab == NULL) {
3266 			*ill_zerocopy_capab =
3267 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3268 			    KM_NOSLEEP);
3269 
3270 			if (*ill_zerocopy_capab == NULL) {
3271 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3272 				    "could not enable Zero-copy version %d "
3273 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3274 				    ill->ill_name);
3275 				return;
3276 			}
3277 		}
3278 
3279 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3280 		    "supports Zero-copy version %d\n", ill->ill_name,
3281 		    ZEROCOPY_VERSION_1));
3282 
3283 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3284 		    zc_ic->zerocopy_version;
3285 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3286 		    zc_ic->zerocopy_flags;
3287 
3288 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3289 	} else {
3290 		uint_t size;
3291 		uchar_t *rptr;
3292 
3293 		size = sizeof (dl_capability_req_t) +
3294 		    sizeof (dl_capability_sub_t) +
3295 		    sizeof (dl_capab_zerocopy_t);
3296 
3297 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3298 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3299 			    "could not enable zerocopy for %s (ENOMEM)\n",
3300 			    ill->ill_name);
3301 			return;
3302 		}
3303 
3304 		rptr = nmp->b_rptr;
3305 		/* initialize dl_capability_req_t */
3306 		oc = (dl_capability_req_t *)rptr;
3307 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3308 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3309 		    sizeof (dl_capab_zerocopy_t);
3310 		rptr += sizeof (dl_capability_req_t);
3311 
3312 		/* initialize dl_capability_sub_t */
3313 		bcopy(isub, rptr, sizeof (*isub));
3314 		rptr += sizeof (*isub);
3315 
3316 		/* initialize dl_capab_zerocopy_t */
3317 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3318 		*zc_oc = *zc_ic;
3319 
3320 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3321 		    "to enable zero-copy version %d\n", ill->ill_name,
3322 		    ZEROCOPY_VERSION_1));
3323 
3324 		/* set VMSAFE_MEM flag */
3325 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3326 
3327 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3328 		ill_dlpi_send(ill, nmp);
3329 	}
3330 }
3331 
3332 static void
3333 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3334 {
3335 	mblk_t *mp;
3336 	dl_capab_zerocopy_t *zerocopy_subcap;
3337 	dl_capability_sub_t *dl_subcap;
3338 	int size;
3339 
3340 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3341 		return;
3342 
3343 	ASSERT(ill->ill_zerocopy_capab != NULL);
3344 	/*
3345 	 * Clear the capability flag for Zero-copy but retain the
3346 	 * ill_zerocopy_capab structure since it's possible that another
3347 	 * thread is still referring to it.  The structure only gets
3348 	 * deallocated when we destroy the ill.
3349 	 */
3350 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3351 
3352 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3353 
3354 	mp = allocb(size, BPRI_HI);
3355 	if (mp == NULL) {
3356 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3357 		    "request to disable Zero-copy\n"));
3358 		return;
3359 	}
3360 
3361 	mp->b_wptr = mp->b_rptr + size;
3362 
3363 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3364 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3365 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3366 
3367 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3368 	zerocopy_subcap->zerocopy_version =
3369 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3370 	zerocopy_subcap->zerocopy_flags = 0;
3371 
3372 	if (*sc_mp != NULL)
3373 		linkb(*sc_mp, mp);
3374 	else
3375 		*sc_mp = mp;
3376 }
3377 
3378 /*
3379  * Process Large Segment Offload capability negotiation ack received from a
3380  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3381  * DL_CAPABILITY_ACK message.
3382  */
3383 static void
3384 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3385 {
3386 	mblk_t *nmp = NULL;
3387 	dl_capability_req_t *oc;
3388 	dl_capab_lso_t *lso_ic, *lso_oc;
3389 	ill_lso_capab_t **ill_lso_capab;
3390 	uint_t sub_dl_cap = isub->dl_cap;
3391 	uint8_t *capend;
3392 
3393 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3394 
3395 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3396 
3397 	/*
3398 	 * Note: range checks here are not absolutely sufficient to
3399 	 * make us robust against malformed messages sent by drivers;
3400 	 * this is in keeping with the rest of IP's dlpi handling.
3401 	 * (Remember, it's coming from something else in the kernel
3402 	 * address space)
3403 	 */
3404 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3405 	if (capend > mp->b_wptr) {
3406 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3407 		    "malformed sub-capability too long for mblk");
3408 		return;
3409 	}
3410 
3411 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3412 
3413 	if (lso_ic->lso_version != LSO_VERSION_1) {
3414 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3415 		    "unsupported LSO sub-capability (version %d, expected %d)",
3416 		    lso_ic->lso_version, LSO_VERSION_1);
3417 		return;
3418 	}
3419 
3420 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3421 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3422 		    "capability isn't as expected; pass-thru module(s) "
3423 		    "detected, discarding capability\n"));
3424 		return;
3425 	}
3426 
3427 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3428 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3429 		if (*ill_lso_capab == NULL) {
3430 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3431 			    KM_NOSLEEP);
3432 
3433 			if (*ill_lso_capab == NULL) {
3434 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3435 				    "could not enable LSO version %d "
3436 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3437 				    ill->ill_name);
3438 				return;
3439 			}
3440 		}
3441 
3442 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3443 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3444 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3445 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3446 
3447 		ip1dbg(("ill_capability_lso_ack: interface %s "
3448 		    "has enabled LSO\n ", ill->ill_name));
3449 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3450 		uint_t size;
3451 		uchar_t *rptr;
3452 
3453 		size = sizeof (dl_capability_req_t) +
3454 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3455 
3456 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3457 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3458 			    "could not enable LSO for %s (ENOMEM)\n",
3459 			    ill->ill_name);
3460 			return;
3461 		}
3462 
3463 		rptr = nmp->b_rptr;
3464 		/* initialize dl_capability_req_t */
3465 		oc = (dl_capability_req_t *)nmp->b_rptr;
3466 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3467 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3468 		    sizeof (dl_capab_lso_t);
3469 		nmp->b_rptr += sizeof (dl_capability_req_t);
3470 
3471 		/* initialize dl_capability_sub_t */
3472 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3473 		nmp->b_rptr += sizeof (*isub);
3474 
3475 		/* initialize dl_capab_lso_t */
3476 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3477 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3478 
3479 		nmp->b_rptr = rptr;
3480 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3481 
3482 		/* set ENABLE flag */
3483 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3484 
3485 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3486 		ill_dlpi_send(ill, nmp);
3487 	} else {
3488 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3489 		    "advertised %x LSO capability flags\n",
3490 		    ill->ill_name, lso_ic->lso_flags));
3491 	}
3492 }
3493 
3494 static void
3495 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3496 {
3497 	mblk_t *mp;
3498 	dl_capab_lso_t *lso_subcap;
3499 	dl_capability_sub_t *dl_subcap;
3500 	int size;
3501 
3502 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3503 		return;
3504 
3505 	ASSERT(ill->ill_lso_capab != NULL);
3506 	/*
3507 	 * Clear the capability flag for LSO but retain the
3508 	 * ill_lso_capab structure since it's possible that another
3509 	 * thread is still referring to it.  The structure only gets
3510 	 * deallocated when we destroy the ill.
3511 	 */
3512 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3513 
3514 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3515 
3516 	mp = allocb(size, BPRI_HI);
3517 	if (mp == NULL) {
3518 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3519 		    "request to disable LSO\n"));
3520 		return;
3521 	}
3522 
3523 	mp->b_wptr = mp->b_rptr + size;
3524 
3525 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3526 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3527 	dl_subcap->dl_length = sizeof (*lso_subcap);
3528 
3529 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3530 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3531 	lso_subcap->lso_flags = 0;
3532 
3533 	if (*sc_mp != NULL)
3534 		linkb(*sc_mp, mp);
3535 	else
3536 		*sc_mp = mp;
3537 }
3538 
3539 /*
3540  * Consume a new-style hardware capabilities negotiation ack.
3541  * Called from ip_rput_dlpi_writer().
3542  */
3543 void
3544 ill_capability_ack(ill_t *ill, mblk_t *mp)
3545 {
3546 	dl_capability_ack_t *capp;
3547 	dl_capability_sub_t *subp, *endp;
3548 
3549 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3550 		ill->ill_dlpi_capab_state = IDS_OK;
3551 
3552 	capp = (dl_capability_ack_t *)mp->b_rptr;
3553 
3554 	if (capp->dl_sub_length == 0)
3555 		/* no new-style capabilities */
3556 		return;
3557 
3558 	/* make sure the driver supplied correct dl_sub_length */
3559 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3560 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3561 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3562 		return;
3563 	}
3564 
3565 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3566 	/*
3567 	 * There are sub-capabilities. Process the ones we know about.
3568 	 * Loop until we don't have room for another sub-cap header..
3569 	 */
3570 	for (subp = SC(capp, capp->dl_sub_offset),
3571 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3572 	    subp <= endp;
3573 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3574 
3575 		switch (subp->dl_cap) {
3576 		case DL_CAPAB_ID_WRAPPER:
3577 			ill_capability_id_ack(ill, mp, subp);
3578 			break;
3579 		default:
3580 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3581 			break;
3582 		}
3583 	}
3584 #undef SC
3585 }
3586 
3587 /*
3588  * This routine is called to scan the fragmentation reassembly table for
3589  * the specified ILL for any packets that are starting to smell.
3590  * dead_interval is the maximum time in seconds that will be tolerated.  It
3591  * will either be the value specified in ip_g_frag_timeout, or zero if the
3592  * ILL is shutting down and it is time to blow everything off.
3593  *
3594  * It returns the number of seconds (as a time_t) that the next frag timer
3595  * should be scheduled for, 0 meaning that the timer doesn't need to be
3596  * re-started.  Note that the method of calculating next_timeout isn't
3597  * entirely accurate since time will flow between the time we grab
3598  * current_time and the time we schedule the next timeout.  This isn't a
3599  * big problem since this is the timer for sending an ICMP reassembly time
3600  * exceeded messages, and it doesn't have to be exactly accurate.
3601  *
3602  * This function is
3603  * sometimes called as writer, although this is not required.
3604  */
3605 time_t
3606 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3607 {
3608 	ipfb_t	*ipfb;
3609 	ipfb_t	*endp;
3610 	ipf_t	*ipf;
3611 	ipf_t	*ipfnext;
3612 	mblk_t	*mp;
3613 	time_t	current_time = gethrestime_sec();
3614 	time_t	next_timeout = 0;
3615 	uint32_t	hdr_length;
3616 	mblk_t	*send_icmp_head;
3617 	mblk_t	*send_icmp_head_v6;
3618 	zoneid_t zoneid;
3619 	ip_stack_t *ipst = ill->ill_ipst;
3620 
3621 	ipfb = ill->ill_frag_hash_tbl;
3622 	if (ipfb == NULL)
3623 		return (B_FALSE);
3624 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3625 	/* Walk the frag hash table. */
3626 	for (; ipfb < endp; ipfb++) {
3627 		send_icmp_head = NULL;
3628 		send_icmp_head_v6 = NULL;
3629 		mutex_enter(&ipfb->ipfb_lock);
3630 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3631 			time_t frag_time = current_time - ipf->ipf_timestamp;
3632 			time_t frag_timeout;
3633 
3634 			if (frag_time < dead_interval) {
3635 				/*
3636 				 * There are some outstanding fragments
3637 				 * that will timeout later.  Make note of
3638 				 * the time so that we can reschedule the
3639 				 * next timeout appropriately.
3640 				 */
3641 				frag_timeout = dead_interval - frag_time;
3642 				if (next_timeout == 0 ||
3643 				    frag_timeout < next_timeout) {
3644 					next_timeout = frag_timeout;
3645 				}
3646 				break;
3647 			}
3648 			/* Time's up.  Get it out of here. */
3649 			hdr_length = ipf->ipf_nf_hdr_len;
3650 			ipfnext = ipf->ipf_hash_next;
3651 			if (ipfnext)
3652 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3653 			*ipf->ipf_ptphn = ipfnext;
3654 			mp = ipf->ipf_mp->b_cont;
3655 			for (; mp; mp = mp->b_cont) {
3656 				/* Extra points for neatness. */
3657 				IP_REASS_SET_START(mp, 0);
3658 				IP_REASS_SET_END(mp, 0);
3659 			}
3660 			mp = ipf->ipf_mp->b_cont;
3661 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3662 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3663 			ipfb->ipfb_count -= ipf->ipf_count;
3664 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3665 			ipfb->ipfb_frag_pkts--;
3666 			/*
3667 			 * We do not send any icmp message from here because
3668 			 * we currently are holding the ipfb_lock for this
3669 			 * hash chain. If we try and send any icmp messages
3670 			 * from here we may end up via a put back into ip
3671 			 * trying to get the same lock, causing a recursive
3672 			 * mutex panic. Instead we build a list and send all
3673 			 * the icmp messages after we have dropped the lock.
3674 			 */
3675 			if (ill->ill_isv6) {
3676 				if (hdr_length != 0) {
3677 					mp->b_next = send_icmp_head_v6;
3678 					send_icmp_head_v6 = mp;
3679 				} else {
3680 					freemsg(mp);
3681 				}
3682 			} else {
3683 				if (hdr_length != 0) {
3684 					mp->b_next = send_icmp_head;
3685 					send_icmp_head = mp;
3686 				} else {
3687 					freemsg(mp);
3688 				}
3689 			}
3690 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3691 			freeb(ipf->ipf_mp);
3692 		}
3693 		mutex_exit(&ipfb->ipfb_lock);
3694 		/*
3695 		 * Now need to send any icmp messages that we delayed from
3696 		 * above.
3697 		 */
3698 		while (send_icmp_head_v6 != NULL) {
3699 			ip6_t *ip6h;
3700 
3701 			mp = send_icmp_head_v6;
3702 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3703 			mp->b_next = NULL;
3704 			if (mp->b_datap->db_type == M_CTL)
3705 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3706 			else
3707 				ip6h = (ip6_t *)mp->b_rptr;
3708 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3709 			    ill, ipst);
3710 			if (zoneid == ALL_ZONES) {
3711 				freemsg(mp);
3712 			} else {
3713 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3714 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3715 				    B_FALSE, zoneid, ipst);
3716 			}
3717 		}
3718 		while (send_icmp_head != NULL) {
3719 			ipaddr_t dst;
3720 
3721 			mp = send_icmp_head;
3722 			send_icmp_head = send_icmp_head->b_next;
3723 			mp->b_next = NULL;
3724 
3725 			if (mp->b_datap->db_type == M_CTL)
3726 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3727 			else
3728 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3729 
3730 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3731 			if (zoneid == ALL_ZONES) {
3732 				freemsg(mp);
3733 			} else {
3734 				icmp_time_exceeded(ill->ill_wq, mp,
3735 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3736 				    ipst);
3737 			}
3738 		}
3739 	}
3740 	/*
3741 	 * A non-dying ILL will use the return value to decide whether to
3742 	 * restart the frag timer, and for how long.
3743 	 */
3744 	return (next_timeout);
3745 }
3746 
3747 /*
3748  * This routine is called when the approximate count of mblk memory used
3749  * for the specified ILL has exceeded max_count.
3750  */
3751 void
3752 ill_frag_prune(ill_t *ill, uint_t max_count)
3753 {
3754 	ipfb_t	*ipfb;
3755 	ipf_t	*ipf;
3756 	size_t	count;
3757 
3758 	/*
3759 	 * If we are here within ip_min_frag_prune_time msecs remove
3760 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3761 	 * ill_frag_free_num_pkts.
3762 	 */
3763 	mutex_enter(&ill->ill_lock);
3764 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3765 	    (ip_min_frag_prune_time != 0 ?
3766 	    ip_min_frag_prune_time : msec_per_tick)) {
3767 
3768 		ill->ill_frag_free_num_pkts++;
3769 
3770 	} else {
3771 		ill->ill_frag_free_num_pkts = 0;
3772 	}
3773 	ill->ill_last_frag_clean_time = lbolt;
3774 	mutex_exit(&ill->ill_lock);
3775 
3776 	/*
3777 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3778 	 */
3779 	if (ill->ill_frag_free_num_pkts != 0) {
3780 		int ix;
3781 
3782 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3783 			ipfb = &ill->ill_frag_hash_tbl[ix];
3784 			mutex_enter(&ipfb->ipfb_lock);
3785 			if (ipfb->ipfb_ipf != NULL) {
3786 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3787 				    ill->ill_frag_free_num_pkts);
3788 			}
3789 			mutex_exit(&ipfb->ipfb_lock);
3790 		}
3791 	}
3792 	/*
3793 	 * While the reassembly list for this ILL is too big, prune a fragment
3794 	 * queue by age, oldest first.
3795 	 */
3796 	while (ill->ill_frag_count > max_count) {
3797 		int	ix;
3798 		ipfb_t	*oipfb = NULL;
3799 		uint_t	oldest = UINT_MAX;
3800 
3801 		count = 0;
3802 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3803 			ipfb = &ill->ill_frag_hash_tbl[ix];
3804 			mutex_enter(&ipfb->ipfb_lock);
3805 			ipf = ipfb->ipfb_ipf;
3806 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3807 				oldest = ipf->ipf_gen;
3808 				oipfb = ipfb;
3809 			}
3810 			count += ipfb->ipfb_count;
3811 			mutex_exit(&ipfb->ipfb_lock);
3812 		}
3813 		if (oipfb == NULL)
3814 			break;
3815 
3816 		if (count <= max_count)
3817 			return;	/* Somebody beat us to it, nothing to do */
3818 		mutex_enter(&oipfb->ipfb_lock);
3819 		ipf = oipfb->ipfb_ipf;
3820 		if (ipf != NULL) {
3821 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3822 		}
3823 		mutex_exit(&oipfb->ipfb_lock);
3824 	}
3825 }
3826 
3827 /*
3828  * free 'free_cnt' fragmented packets starting at ipf.
3829  */
3830 void
3831 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3832 {
3833 	size_t	count;
3834 	mblk_t	*mp;
3835 	mblk_t	*tmp;
3836 	ipf_t **ipfp = ipf->ipf_ptphn;
3837 
3838 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3839 	ASSERT(ipfp != NULL);
3840 	ASSERT(ipf != NULL);
3841 
3842 	while (ipf != NULL && free_cnt-- > 0) {
3843 		count = ipf->ipf_count;
3844 		mp = ipf->ipf_mp;
3845 		ipf = ipf->ipf_hash_next;
3846 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3847 			IP_REASS_SET_START(tmp, 0);
3848 			IP_REASS_SET_END(tmp, 0);
3849 		}
3850 		atomic_add_32(&ill->ill_frag_count, -count);
3851 		ASSERT(ipfb->ipfb_count >= count);
3852 		ipfb->ipfb_count -= count;
3853 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3854 		ipfb->ipfb_frag_pkts--;
3855 		freemsg(mp);
3856 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3857 	}
3858 
3859 	if (ipf)
3860 		ipf->ipf_ptphn = ipfp;
3861 	ipfp[0] = ipf;
3862 }
3863 
3864 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3865 	"obsolete and may be removed in a future release of Solaris.  Use " \
3866 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3867 
3868 /*
3869  * For obsolete per-interface forwarding configuration;
3870  * called in response to ND_GET.
3871  */
3872 /* ARGSUSED */
3873 static int
3874 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3875 {
3876 	ill_t *ill = (ill_t *)cp;
3877 
3878 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3879 
3880 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3881 	return (0);
3882 }
3883 
3884 /*
3885  * For obsolete per-interface forwarding configuration;
3886  * called in response to ND_SET.
3887  */
3888 /* ARGSUSED */
3889 static int
3890 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3891     cred_t *ioc_cr)
3892 {
3893 	long value;
3894 	int retval;
3895 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3896 
3897 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3898 
3899 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3900 	    value < 0 || value > 1) {
3901 		return (EINVAL);
3902 	}
3903 
3904 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3905 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3906 	rw_exit(&ipst->ips_ill_g_lock);
3907 	return (retval);
3908 }
3909 
3910 /*
3911  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3912  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3913  * up RTS_IFINFO routing socket messages for each interface whose flags we
3914  * change.
3915  */
3916 int
3917 ill_forward_set(ill_t *ill, boolean_t enable)
3918 {
3919 	ill_group_t *illgrp;
3920 	ip_stack_t	*ipst = ill->ill_ipst;
3921 
3922 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3923 
3924 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3925 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3926 		return (0);
3927 
3928 	if (IS_LOOPBACK(ill))
3929 		return (EINVAL);
3930 
3931 	/*
3932 	 * If the ill is in an IPMP group, set the forwarding policy on all
3933 	 * members of the group to the same value.
3934 	 */
3935 	illgrp = ill->ill_group;
3936 	if (illgrp != NULL) {
3937 		ill_t *tmp_ill;
3938 
3939 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3940 		    tmp_ill = tmp_ill->ill_group_next) {
3941 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3942 			    (enable ? "Enabling" : "Disabling"),
3943 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3944 			    tmp_ill->ill_name));
3945 			mutex_enter(&tmp_ill->ill_lock);
3946 			if (enable)
3947 				tmp_ill->ill_flags |= ILLF_ROUTER;
3948 			else
3949 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3950 			mutex_exit(&tmp_ill->ill_lock);
3951 			if (tmp_ill->ill_isv6)
3952 				ill_set_nce_router_flags(tmp_ill, enable);
3953 			/* Notify routing socket listeners of this change. */
3954 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3955 		}
3956 	} else {
3957 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3958 		    (enable ? "Enabling" : "Disabling"),
3959 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3960 		mutex_enter(&ill->ill_lock);
3961 		if (enable)
3962 			ill->ill_flags |= ILLF_ROUTER;
3963 		else
3964 			ill->ill_flags &= ~ILLF_ROUTER;
3965 		mutex_exit(&ill->ill_lock);
3966 		if (ill->ill_isv6)
3967 			ill_set_nce_router_flags(ill, enable);
3968 		/* Notify routing socket listeners of this change. */
3969 		ip_rts_ifmsg(ill->ill_ipif);
3970 	}
3971 
3972 	return (0);
3973 }
3974 
3975 /*
3976  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3977  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3978  * set or clear.
3979  */
3980 static void
3981 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3982 {
3983 	ipif_t *ipif;
3984 	nce_t *nce;
3985 
3986 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3987 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3988 		if (nce != NULL) {
3989 			mutex_enter(&nce->nce_lock);
3990 			if (enable)
3991 				nce->nce_flags |= NCE_F_ISROUTER;
3992 			else
3993 				nce->nce_flags &= ~NCE_F_ISROUTER;
3994 			mutex_exit(&nce->nce_lock);
3995 			NCE_REFRELE(nce);
3996 		}
3997 	}
3998 }
3999 
4000 /*
4001  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4002  * for this ill.  Make sure the v6/v4 question has been answered about this
4003  * ill.  The creation of this ndd variable is only for backwards compatibility.
4004  * The preferred way to control per-interface IP forwarding is through the
4005  * ILLF_ROUTER interface flag.
4006  */
4007 static int
4008 ill_set_ndd_name(ill_t *ill)
4009 {
4010 	char *suffix;
4011 	ip_stack_t	*ipst = ill->ill_ipst;
4012 
4013 	ASSERT(IAM_WRITER_ILL(ill));
4014 
4015 	if (ill->ill_isv6)
4016 		suffix = ipv6_forward_suffix;
4017 	else
4018 		suffix = ipv4_forward_suffix;
4019 
4020 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4021 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4022 	/*
4023 	 * Copies over the '\0'.
4024 	 * Note that strlen(suffix) is always bounded.
4025 	 */
4026 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4027 	    strlen(suffix) + 1);
4028 
4029 	/*
4030 	 * Use of the nd table requires holding the reader lock.
4031 	 * Modifying the nd table thru nd_load/nd_unload requires
4032 	 * the writer lock.
4033 	 */
4034 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4035 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4036 	    nd_ill_forward_set, (caddr_t)ill)) {
4037 		/*
4038 		 * If the nd_load failed, it only meant that it could not
4039 		 * allocate a new bunch of room for further NDD expansion.
4040 		 * Because of that, the ill_ndd_name will be set to 0, and
4041 		 * this interface is at the mercy of the global ip_forwarding
4042 		 * variable.
4043 		 */
4044 		rw_exit(&ipst->ips_ip_g_nd_lock);
4045 		ill->ill_ndd_name = NULL;
4046 		return (ENOMEM);
4047 	}
4048 	rw_exit(&ipst->ips_ip_g_nd_lock);
4049 	return (0);
4050 }
4051 
4052 /*
4053  * Intializes the context structure and returns the first ill in the list
4054  * cuurently start_list and end_list can have values:
4055  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4056  * IP_V4_G_HEAD		Traverse IPV4 list only.
4057  * IP_V6_G_HEAD		Traverse IPV6 list only.
4058  */
4059 
4060 /*
4061  * We don't check for CONDEMNED ills here. Caller must do that if
4062  * necessary under the ill lock.
4063  */
4064 ill_t *
4065 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4066     ip_stack_t *ipst)
4067 {
4068 	ill_if_t *ifp;
4069 	ill_t *ill;
4070 	avl_tree_t *avl_tree;
4071 
4072 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4073 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4074 
4075 	/*
4076 	 * setup the lists to search
4077 	 */
4078 	if (end_list != MAX_G_HEADS) {
4079 		ctx->ctx_current_list = start_list;
4080 		ctx->ctx_last_list = end_list;
4081 	} else {
4082 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4083 		ctx->ctx_current_list = 0;
4084 	}
4085 
4086 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4087 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4088 		if (ifp != (ill_if_t *)
4089 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4090 			avl_tree = &ifp->illif_avl_by_ppa;
4091 			ill = avl_first(avl_tree);
4092 			/*
4093 			 * ill is guaranteed to be non NULL or ifp should have
4094 			 * not existed.
4095 			 */
4096 			ASSERT(ill != NULL);
4097 			return (ill);
4098 		}
4099 		ctx->ctx_current_list++;
4100 	}
4101 
4102 	return (NULL);
4103 }
4104 
4105 /*
4106  * returns the next ill in the list. ill_first() must have been called
4107  * before calling ill_next() or bad things will happen.
4108  */
4109 
4110 /*
4111  * We don't check for CONDEMNED ills here. Caller must do that if
4112  * necessary under the ill lock.
4113  */
4114 ill_t *
4115 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4116 {
4117 	ill_if_t *ifp;
4118 	ill_t *ill;
4119 	ip_stack_t	*ipst = lastill->ill_ipst;
4120 
4121 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4122 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4123 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4124 	    AVL_AFTER)) != NULL) {
4125 		return (ill);
4126 	}
4127 
4128 	/* goto next ill_ifp in the list. */
4129 	ifp = lastill->ill_ifptr->illif_next;
4130 
4131 	/* make sure not at end of circular list */
4132 	while (ifp ==
4133 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4134 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4135 			return (NULL);
4136 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4137 	}
4138 
4139 	return (avl_first(&ifp->illif_avl_by_ppa));
4140 }
4141 
4142 /*
4143  * Check interface name for correct format which is name+ppa.
4144  * name can contain characters and digits, the right most digits
4145  * make up the ppa number. use of octal is not allowed, name must contain
4146  * a ppa, return pointer to the start of ppa.
4147  * In case of error return NULL.
4148  */
4149 static char *
4150 ill_get_ppa_ptr(char *name)
4151 {
4152 	int namelen = mi_strlen(name);
4153 
4154 	int len = namelen;
4155 
4156 	name += len;
4157 	while (len > 0) {
4158 		name--;
4159 		if (*name < '0' || *name > '9')
4160 			break;
4161 		len--;
4162 	}
4163 
4164 	/* empty string, all digits, or no trailing digits */
4165 	if (len == 0 || len == (int)namelen)
4166 		return (NULL);
4167 
4168 	name++;
4169 	/* check for attempted use of octal */
4170 	if (*name == '0' && len != (int)namelen - 1)
4171 		return (NULL);
4172 	return (name);
4173 }
4174 
4175 /*
4176  * use avl tree to locate the ill.
4177  */
4178 static ill_t *
4179 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4180     ipsq_func_t func, int *error, ip_stack_t *ipst)
4181 {
4182 	char *ppa_ptr = NULL;
4183 	int len;
4184 	uint_t ppa;
4185 	ill_t *ill = NULL;
4186 	ill_if_t *ifp;
4187 	int list;
4188 	ipsq_t *ipsq;
4189 
4190 	if (error != NULL)
4191 		*error = 0;
4192 
4193 	/*
4194 	 * get ppa ptr
4195 	 */
4196 	if (isv6)
4197 		list = IP_V6_G_HEAD;
4198 	else
4199 		list = IP_V4_G_HEAD;
4200 
4201 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4202 		if (error != NULL)
4203 			*error = ENXIO;
4204 		return (NULL);
4205 	}
4206 
4207 	len = ppa_ptr - name + 1;
4208 
4209 	ppa = stoi(&ppa_ptr);
4210 
4211 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4212 
4213 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4214 		/*
4215 		 * match is done on len - 1 as the name is not null
4216 		 * terminated it contains ppa in addition to the interface
4217 		 * name.
4218 		 */
4219 		if ((ifp->illif_name_len == len) &&
4220 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4221 			break;
4222 		} else {
4223 			ifp = ifp->illif_next;
4224 		}
4225 	}
4226 
4227 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4228 		/*
4229 		 * Even the interface type does not exist.
4230 		 */
4231 		if (error != NULL)
4232 			*error = ENXIO;
4233 		return (NULL);
4234 	}
4235 
4236 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4237 	if (ill != NULL) {
4238 		/*
4239 		 * The block comment at the start of ipif_down
4240 		 * explains the use of the macros used below
4241 		 */
4242 		GRAB_CONN_LOCK(q);
4243 		mutex_enter(&ill->ill_lock);
4244 		if (ILL_CAN_LOOKUP(ill)) {
4245 			ill_refhold_locked(ill);
4246 			mutex_exit(&ill->ill_lock);
4247 			RELEASE_CONN_LOCK(q);
4248 			return (ill);
4249 		} else if (ILL_CAN_WAIT(ill, q)) {
4250 			ipsq = ill->ill_phyint->phyint_ipsq;
4251 			mutex_enter(&ipsq->ipsq_lock);
4252 			mutex_exit(&ill->ill_lock);
4253 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4254 			mutex_exit(&ipsq->ipsq_lock);
4255 			RELEASE_CONN_LOCK(q);
4256 			if (error != NULL)
4257 				*error = EINPROGRESS;
4258 			return (NULL);
4259 		}
4260 		mutex_exit(&ill->ill_lock);
4261 		RELEASE_CONN_LOCK(q);
4262 	}
4263 	if (error != NULL)
4264 		*error = ENXIO;
4265 	return (NULL);
4266 }
4267 
4268 /*
4269  * comparison function for use with avl.
4270  */
4271 static int
4272 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4273 {
4274 	uint_t ppa;
4275 	uint_t ill_ppa;
4276 
4277 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4278 
4279 	ppa = *((uint_t *)ppa_ptr);
4280 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4281 	/*
4282 	 * We want the ill with the lowest ppa to be on the
4283 	 * top.
4284 	 */
4285 	if (ill_ppa < ppa)
4286 		return (1);
4287 	if (ill_ppa > ppa)
4288 		return (-1);
4289 	return (0);
4290 }
4291 
4292 /*
4293  * remove an interface type from the global list.
4294  */
4295 static void
4296 ill_delete_interface_type(ill_if_t *interface)
4297 {
4298 	ASSERT(interface != NULL);
4299 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4300 
4301 	avl_destroy(&interface->illif_avl_by_ppa);
4302 	if (interface->illif_ppa_arena != NULL)
4303 		vmem_destroy(interface->illif_ppa_arena);
4304 
4305 	remque(interface);
4306 
4307 	mi_free(interface);
4308 }
4309 
4310 /*
4311  * remove ill from the global list.
4312  */
4313 static void
4314 ill_glist_delete(ill_t *ill)
4315 {
4316 	ip_stack_t	*ipst;
4317 
4318 	if (ill == NULL)
4319 		return;
4320 	ipst = ill->ill_ipst;
4321 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4322 
4323 	/*
4324 	 * If the ill was never inserted into the AVL tree
4325 	 * we skip the if branch.
4326 	 */
4327 	if (ill->ill_ifptr != NULL) {
4328 		/*
4329 		 * remove from AVL tree and free ppa number
4330 		 */
4331 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4332 
4333 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4334 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4335 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4336 		}
4337 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4338 			ill_delete_interface_type(ill->ill_ifptr);
4339 		}
4340 
4341 		/*
4342 		 * Indicate ill is no longer in the list.
4343 		 */
4344 		ill->ill_ifptr = NULL;
4345 		ill->ill_name_length = 0;
4346 		ill->ill_name[0] = '\0';
4347 		ill->ill_ppa = UINT_MAX;
4348 	}
4349 
4350 	/* Generate one last event for this ill. */
4351 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
4352 	    ill->ill_name_length);
4353 
4354 	ill_phyint_free(ill);
4355 	rw_exit(&ipst->ips_ill_g_lock);
4356 }
4357 
4358 /*
4359  * allocate a ppa, if the number of plumbed interfaces of this type are
4360  * less than ill_no_arena do a linear search to find a unused ppa.
4361  * When the number goes beyond ill_no_arena switch to using an arena.
4362  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4363  * is the return value for an error condition, so allocation starts at one
4364  * and is decremented by one.
4365  */
4366 static int
4367 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4368 {
4369 	ill_t *tmp_ill;
4370 	uint_t start, end;
4371 	int ppa;
4372 
4373 	if (ifp->illif_ppa_arena == NULL &&
4374 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4375 		/*
4376 		 * Create an arena.
4377 		 */
4378 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4379 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4380 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4381 			/* allocate what has already been assigned */
4382 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4383 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4384 		    tmp_ill, AVL_AFTER)) {
4385 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4386 			    1,		/* size */
4387 			    1,		/* align/quantum */
4388 			    0,		/* phase */
4389 			    0,		/* nocross */
4390 			    /* minaddr */
4391 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4392 			    /* maxaddr */
4393 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4394 			    VM_NOSLEEP|VM_FIRSTFIT);
4395 			if (ppa == 0) {
4396 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4397 				    " failed while switching"));
4398 				vmem_destroy(ifp->illif_ppa_arena);
4399 				ifp->illif_ppa_arena = NULL;
4400 				break;
4401 			}
4402 		}
4403 	}
4404 
4405 	if (ifp->illif_ppa_arena != NULL) {
4406 		if (ill->ill_ppa == UINT_MAX) {
4407 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4408 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4409 			if (ppa == 0)
4410 				return (EAGAIN);
4411 			ill->ill_ppa = --ppa;
4412 		} else {
4413 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4414 			    1, 		/* size */
4415 			    1, 		/* align/quantum */
4416 			    0, 		/* phase */
4417 			    0, 		/* nocross */
4418 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4419 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4420 			    VM_NOSLEEP|VM_FIRSTFIT);
4421 			/*
4422 			 * Most likely the allocation failed because
4423 			 * the requested ppa was in use.
4424 			 */
4425 			if (ppa == 0)
4426 				return (EEXIST);
4427 		}
4428 		return (0);
4429 	}
4430 
4431 	/*
4432 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4433 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4434 	 */
4435 	if (ill->ill_ppa == UINT_MAX) {
4436 		end = UINT_MAX - 1;
4437 		start = 0;
4438 	} else {
4439 		end = start = ill->ill_ppa;
4440 	}
4441 
4442 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4443 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4444 		if (start++ >= end) {
4445 			if (ill->ill_ppa == UINT_MAX)
4446 				return (EAGAIN);
4447 			else
4448 				return (EEXIST);
4449 		}
4450 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4451 	}
4452 	ill->ill_ppa = start;
4453 	return (0);
4454 }
4455 
4456 /*
4457  * Insert ill into the list of configured ill's. Once this function completes,
4458  * the ill is globally visible and is available through lookups. More precisely
4459  * this happens after the caller drops the ill_g_lock.
4460  */
4461 static int
4462 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4463 {
4464 	ill_if_t *ill_interface;
4465 	avl_index_t where = 0;
4466 	int error;
4467 	int name_length;
4468 	int index;
4469 	boolean_t check_length = B_FALSE;
4470 	ip_stack_t	*ipst = ill->ill_ipst;
4471 
4472 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4473 
4474 	name_length = mi_strlen(name) + 1;
4475 
4476 	if (isv6)
4477 		index = IP_V6_G_HEAD;
4478 	else
4479 		index = IP_V4_G_HEAD;
4480 
4481 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4482 	/*
4483 	 * Search for interface type based on name
4484 	 */
4485 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4486 		if ((ill_interface->illif_name_len == name_length) &&
4487 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4488 			break;
4489 		}
4490 		ill_interface = ill_interface->illif_next;
4491 	}
4492 
4493 	/*
4494 	 * Interface type not found, create one.
4495 	 */
4496 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4497 
4498 		ill_g_head_t ghead;
4499 
4500 		/*
4501 		 * allocate ill_if_t structure
4502 		 */
4503 
4504 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4505 		if (ill_interface == NULL) {
4506 			return (ENOMEM);
4507 		}
4508 
4509 
4510 
4511 		(void) strcpy(ill_interface->illif_name, name);
4512 		ill_interface->illif_name_len = name_length;
4513 
4514 		avl_create(&ill_interface->illif_avl_by_ppa,
4515 		    ill_compare_ppa, sizeof (ill_t),
4516 		    offsetof(struct ill_s, ill_avl_byppa));
4517 
4518 		/*
4519 		 * link the structure in the back to maintain order
4520 		 * of configuration for ifconfig output.
4521 		 */
4522 		ghead = ipst->ips_ill_g_heads[index];
4523 		insque(ill_interface, ghead.ill_g_list_tail);
4524 
4525 	}
4526 
4527 	if (ill->ill_ppa == UINT_MAX)
4528 		check_length = B_TRUE;
4529 
4530 	error = ill_alloc_ppa(ill_interface, ill);
4531 	if (error != 0) {
4532 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4533 			ill_delete_interface_type(ill->ill_ifptr);
4534 		return (error);
4535 	}
4536 
4537 	/*
4538 	 * When the ppa is choosen by the system, check that there is
4539 	 * enough space to insert ppa. if a specific ppa was passed in this
4540 	 * check is not required as the interface name passed in will have
4541 	 * the right ppa in it.
4542 	 */
4543 	if (check_length) {
4544 		/*
4545 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4546 		 */
4547 		char buf[sizeof (uint_t) * 3];
4548 
4549 		/*
4550 		 * convert ppa to string to calculate the amount of space
4551 		 * required for it in the name.
4552 		 */
4553 		numtos(ill->ill_ppa, buf);
4554 
4555 		/* Do we have enough space to insert ppa ? */
4556 
4557 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4558 			/* Free ppa and interface type struct */
4559 			if (ill_interface->illif_ppa_arena != NULL) {
4560 				vmem_free(ill_interface->illif_ppa_arena,
4561 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4562 			}
4563 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4564 				ill_delete_interface_type(ill->ill_ifptr);
4565 
4566 			return (EINVAL);
4567 		}
4568 	}
4569 
4570 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4571 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4572 
4573 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4574 	    &where);
4575 	ill->ill_ifptr = ill_interface;
4576 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4577 
4578 	ill_phyint_reinit(ill);
4579 	return (0);
4580 }
4581 
4582 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4583 static boolean_t
4584 ipsq_init(ill_t *ill)
4585 {
4586 	ipsq_t  *ipsq;
4587 
4588 	/* Init the ipsq and impicitly enter as writer */
4589 	ill->ill_phyint->phyint_ipsq =
4590 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4591 	if (ill->ill_phyint->phyint_ipsq == NULL)
4592 		return (B_FALSE);
4593 	ipsq = ill->ill_phyint->phyint_ipsq;
4594 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4595 	ill->ill_phyint->phyint_ipsq_next = NULL;
4596 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4597 	ipsq->ipsq_refs = 1;
4598 	ipsq->ipsq_writer = curthread;
4599 	ipsq->ipsq_reentry_cnt = 1;
4600 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4601 #ifdef DEBUG
4602 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4603 	    IPSQ_STACK_DEPTH);
4604 #endif
4605 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4606 	return (B_TRUE);
4607 }
4608 
4609 /*
4610  * ill_init is called by ip_open when a device control stream is opened.
4611  * It does a few initializations, and shoots a DL_INFO_REQ message down
4612  * to the driver.  The response is later picked up in ip_rput_dlpi and
4613  * used to set up default mechanisms for talking to the driver.  (Always
4614  * called as writer.)
4615  *
4616  * If this function returns error, ip_open will call ip_close which in
4617  * turn will call ill_delete to clean up any memory allocated here that
4618  * is not yet freed.
4619  */
4620 int
4621 ill_init(queue_t *q, ill_t *ill)
4622 {
4623 	int	count;
4624 	dl_info_req_t	*dlir;
4625 	mblk_t	*info_mp;
4626 	uchar_t *frag_ptr;
4627 
4628 	/*
4629 	 * The ill is initialized to zero by mi_alloc*(). In addition
4630 	 * some fields already contain valid values, initialized in
4631 	 * ip_open(), before we reach here.
4632 	 */
4633 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4634 
4635 	ill->ill_rq = q;
4636 	ill->ill_wq = WR(q);
4637 
4638 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4639 	    BPRI_HI);
4640 	if (info_mp == NULL)
4641 		return (ENOMEM);
4642 
4643 	/*
4644 	 * Allocate sufficient space to contain our fragment hash table and
4645 	 * the device name.
4646 	 */
4647 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4648 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4649 	if (frag_ptr == NULL) {
4650 		freemsg(info_mp);
4651 		return (ENOMEM);
4652 	}
4653 	ill->ill_frag_ptr = frag_ptr;
4654 	ill->ill_frag_free_num_pkts = 0;
4655 	ill->ill_last_frag_clean_time = 0;
4656 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4657 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4658 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4659 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4660 		    NULL, MUTEX_DEFAULT, NULL);
4661 	}
4662 
4663 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4664 	if (ill->ill_phyint == NULL) {
4665 		freemsg(info_mp);
4666 		mi_free(frag_ptr);
4667 		return (ENOMEM);
4668 	}
4669 
4670 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4671 	/*
4672 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4673 	 * at this point because of the following reason. If we can't
4674 	 * enter the ipsq at some point and cv_wait, the writer that
4675 	 * wakes us up tries to locate us using the list of all phyints
4676 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4677 	 * If we don't set it now, we risk a missed wakeup.
4678 	 */
4679 	ill->ill_phyint->phyint_illv4 = ill;
4680 	ill->ill_ppa = UINT_MAX;
4681 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4682 
4683 	if (!ipsq_init(ill)) {
4684 		freemsg(info_mp);
4685 		mi_free(frag_ptr);
4686 		mi_free(ill->ill_phyint);
4687 		return (ENOMEM);
4688 	}
4689 
4690 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4691 
4692 	/* Frag queue limit stuff */
4693 	ill->ill_frag_count = 0;
4694 	ill->ill_ipf_gen = 0;
4695 
4696 	ill->ill_global_timer = INFINITY;
4697 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4698 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4699 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4700 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4701 
4702 	/*
4703 	 * Initialize IPv6 configuration variables.  The IP module is always
4704 	 * opened as an IPv4 module.  Instead tracking down the cases where
4705 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4706 	 * here for convenience, this has no effect until the ill is set to do
4707 	 * IPv6.
4708 	 */
4709 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4710 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4711 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4712 	ill->ill_max_buf = ND_MAX_Q;
4713 	ill->ill_refcnt = 0;
4714 
4715 	/* Send down the Info Request to the driver. */
4716 	info_mp->b_datap->db_type = M_PCPROTO;
4717 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4718 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4719 	dlir->dl_primitive = DL_INFO_REQ;
4720 
4721 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4722 
4723 	qprocson(q);
4724 	ill_dlpi_send(ill, info_mp);
4725 
4726 	return (0);
4727 }
4728 
4729 /*
4730  * ill_dls_info
4731  * creates datalink socket info from the device.
4732  */
4733 int
4734 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4735 {
4736 	size_t	len;
4737 	ill_t	*ill = ipif->ipif_ill;
4738 
4739 	sdl->sdl_family = AF_LINK;
4740 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4741 	sdl->sdl_type = ill->ill_type;
4742 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4743 	len = strlen(sdl->sdl_data);
4744 	ASSERT(len < 256);
4745 	sdl->sdl_nlen = (uchar_t)len;
4746 	sdl->sdl_alen = ill->ill_phys_addr_length;
4747 	sdl->sdl_slen = 0;
4748 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4749 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4750 
4751 	return (sizeof (struct sockaddr_dl));
4752 }
4753 
4754 /*
4755  * ill_xarp_info
4756  * creates xarp info from the device.
4757  */
4758 static int
4759 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4760 {
4761 	sdl->sdl_family = AF_LINK;
4762 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4763 	sdl->sdl_type = ill->ill_type;
4764 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4765 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4766 	sdl->sdl_alen = ill->ill_phys_addr_length;
4767 	sdl->sdl_slen = 0;
4768 	return (sdl->sdl_nlen);
4769 }
4770 
4771 static int
4772 loopback_kstat_update(kstat_t *ksp, int rw)
4773 {
4774 	kstat_named_t *kn;
4775 	netstackid_t	stackid;
4776 	netstack_t	*ns;
4777 	ip_stack_t	*ipst;
4778 
4779 	if (ksp == NULL || ksp->ks_data == NULL)
4780 		return (EIO);
4781 
4782 	if (rw == KSTAT_WRITE)
4783 		return (EACCES);
4784 
4785 	kn = KSTAT_NAMED_PTR(ksp);
4786 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4787 
4788 	ns = netstack_find_by_stackid(stackid);
4789 	if (ns == NULL)
4790 		return (-1);
4791 
4792 	ipst = ns->netstack_ip;
4793 	if (ipst == NULL) {
4794 		netstack_rele(ns);
4795 		return (-1);
4796 	}
4797 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4798 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4799 	netstack_rele(ns);
4800 	return (0);
4801 }
4802 
4803 /*
4804  * Has ifindex been plumbed already.
4805  * Compares both phyint_ifindex and phyint_group_ifindex.
4806  */
4807 static boolean_t
4808 phyint_exists(uint_t index, ip_stack_t *ipst)
4809 {
4810 	phyint_t *phyi;
4811 
4812 	ASSERT(index != 0);
4813 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4814 	/*
4815 	 * Indexes are stored in the phyint - a common structure
4816 	 * to both IPv4 and IPv6.
4817 	 */
4818 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4819 	for (; phyi != NULL;
4820 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4821 	    phyi, AVL_AFTER)) {
4822 		if (phyi->phyint_ifindex == index ||
4823 		    phyi->phyint_group_ifindex == index)
4824 			return (B_TRUE);
4825 	}
4826 	return (B_FALSE);
4827 }
4828 
4829 /* Pick a unique ifindex */
4830 boolean_t
4831 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4832 {
4833 	uint_t starting_index;
4834 
4835 	if (!ipst->ips_ill_index_wrap) {
4836 		*indexp = ipst->ips_ill_index++;
4837 		if (ipst->ips_ill_index == 0) {
4838 			/* Reached the uint_t limit Next time wrap  */
4839 			ipst->ips_ill_index_wrap = B_TRUE;
4840 		}
4841 		return (B_TRUE);
4842 	}
4843 
4844 	/*
4845 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4846 	 * at this point and don't want to call any function that attempts
4847 	 * to get the lock again.
4848 	 */
4849 	starting_index = ipst->ips_ill_index++;
4850 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4851 		if (ipst->ips_ill_index != 0 &&
4852 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4853 			/* found unused index - use it */
4854 			*indexp = ipst->ips_ill_index;
4855 			return (B_TRUE);
4856 		}
4857 	}
4858 
4859 	/*
4860 	 * all interface indicies are inuse.
4861 	 */
4862 	return (B_FALSE);
4863 }
4864 
4865 /*
4866  * Assign a unique interface index for the phyint.
4867  */
4868 static boolean_t
4869 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4870 {
4871 	ASSERT(phyi->phyint_ifindex == 0);
4872 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4873 }
4874 
4875 /*
4876  * Return a pointer to the ill which matches the supplied name.  Note that
4877  * the ill name length includes the null termination character.  (May be
4878  * called as writer.)
4879  * If do_alloc and the interface is "lo0" it will be automatically created.
4880  * Cannot bump up reference on condemned ills. So dup detect can't be done
4881  * using this func.
4882  */
4883 ill_t *
4884 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4885     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4886     ip_stack_t *ipst)
4887 {
4888 	ill_t	*ill;
4889 	ipif_t	*ipif;
4890 	kstat_named_t	*kn;
4891 	boolean_t isloopback;
4892 	ipsq_t *old_ipsq;
4893 	in6_addr_t ov6addr;
4894 
4895 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4896 
4897 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4898 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4899 	rw_exit(&ipst->ips_ill_g_lock);
4900 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4901 		return (ill);
4902 
4903 	/*
4904 	 * Couldn't find it.  Does this happen to be a lookup for the
4905 	 * loopback device and are we allowed to allocate it?
4906 	 */
4907 	if (!isloopback || !do_alloc)
4908 		return (NULL);
4909 
4910 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4911 
4912 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4913 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4914 		rw_exit(&ipst->ips_ill_g_lock);
4915 		return (ill);
4916 	}
4917 
4918 	/* Create the loopback device on demand */
4919 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4920 	    sizeof (ipif_loopback_name), BPRI_MED));
4921 	if (ill == NULL)
4922 		goto done;
4923 
4924 	*ill = ill_null;
4925 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4926 	ill->ill_ipst = ipst;
4927 	netstack_hold(ipst->ips_netstack);
4928 	/*
4929 	 * For exclusive stacks we set the zoneid to zero
4930 	 * to make IP operate as if in the global zone.
4931 	 */
4932 	ill->ill_zoneid = GLOBAL_ZONEID;
4933 
4934 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4935 	if (ill->ill_phyint == NULL)
4936 		goto done;
4937 
4938 	if (isv6)
4939 		ill->ill_phyint->phyint_illv6 = ill;
4940 	else
4941 		ill->ill_phyint->phyint_illv4 = ill;
4942 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4943 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4944 	/* Add room for tcp+ip headers */
4945 	if (isv6) {
4946 		ill->ill_isv6 = B_TRUE;
4947 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4948 	} else {
4949 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4950 	}
4951 	if (!ill_allocate_mibs(ill))
4952 		goto done;
4953 	ill->ill_max_mtu = ill->ill_max_frag;
4954 	/*
4955 	 * ipif_loopback_name can't be pointed at directly because its used
4956 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4957 	 * from the glist, ill_glist_delete() sets the first character of
4958 	 * ill_name to '\0'.
4959 	 */
4960 	ill->ill_name = (char *)ill + sizeof (*ill);
4961 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4962 	ill->ill_name_length = sizeof (ipif_loopback_name);
4963 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4964 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4965 
4966 	ill->ill_global_timer = INFINITY;
4967 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4968 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4969 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4970 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4971 
4972 	/* No resolver here. */
4973 	ill->ill_net_type = IRE_LOOPBACK;
4974 
4975 	/* Initialize the ipsq */
4976 	if (!ipsq_init(ill))
4977 		goto done;
4978 
4979 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4980 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4981 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4982 #ifdef DEBUG
4983 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4984 #endif
4985 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4986 	if (ipif == NULL)
4987 		goto done;
4988 
4989 	ill->ill_flags = ILLF_MULTICAST;
4990 
4991 	ov6addr = ipif->ipif_v6lcl_addr;
4992 	/* Set up default loopback address and mask. */
4993 	if (!isv6) {
4994 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4995 
4996 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4997 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4998 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4999 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5000 		    ipif->ipif_v6subnet);
5001 		ill->ill_flags |= ILLF_IPV4;
5002 	} else {
5003 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5004 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5005 		ipif->ipif_v6net_mask = ipv6_all_ones;
5006 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5007 		    ipif->ipif_v6subnet);
5008 		ill->ill_flags |= ILLF_IPV6;
5009 	}
5010 
5011 	/*
5012 	 * Chain us in at the end of the ill list. hold the ill
5013 	 * before we make it globally visible. 1 for the lookup.
5014 	 */
5015 	ill->ill_refcnt = 0;
5016 	ill_refhold(ill);
5017 
5018 	ill->ill_frag_count = 0;
5019 	ill->ill_frag_free_num_pkts = 0;
5020 	ill->ill_last_frag_clean_time = 0;
5021 
5022 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5023 
5024 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5025 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5026 
5027 	/* Let SCTP know so that it can add this to its list */
5028 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5029 
5030 	/*
5031 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5032 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5033 	 * requires to be after ill_glist_insert() since we need the
5034 	 * ill_index set. Pass on ipv6_loopback as the old address.
5035 	 */
5036 	sctp_update_ipif_addr(ipif, ov6addr);
5037 
5038 	/*
5039 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5040 	 */
5041 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5042 		/* Loopback ills aren't in any IPMP group */
5043 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5044 		ipsq_delete(old_ipsq);
5045 	}
5046 
5047 	/*
5048 	 * Delay this till the ipif is allocated as ipif_allocate
5049 	 * de-references ill_phyint for getting the ifindex. We
5050 	 * can't do this before ipif_allocate because ill_phyint_reinit
5051 	 * -> phyint_assign_ifindex expects ipif to be present.
5052 	 */
5053 	mutex_enter(&ill->ill_phyint->phyint_lock);
5054 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5055 	mutex_exit(&ill->ill_phyint->phyint_lock);
5056 
5057 	if (ipst->ips_loopback_ksp == NULL) {
5058 		/* Export loopback interface statistics */
5059 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5060 		    ipif_loopback_name, "net",
5061 		    KSTAT_TYPE_NAMED, 2, 0,
5062 		    ipst->ips_netstack->netstack_stackid);
5063 		if (ipst->ips_loopback_ksp != NULL) {
5064 			ipst->ips_loopback_ksp->ks_update =
5065 			    loopback_kstat_update;
5066 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5067 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5068 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5069 			ipst->ips_loopback_ksp->ks_private =
5070 			    (void *)(uintptr_t)ipst->ips_netstack->
5071 			    netstack_stackid;
5072 			kstat_install(ipst->ips_loopback_ksp);
5073 		}
5074 	}
5075 
5076 	if (error != NULL)
5077 		*error = 0;
5078 	*did_alloc = B_TRUE;
5079 	rw_exit(&ipst->ips_ill_g_lock);
5080 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
5081 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
5082 	return (ill);
5083 done:
5084 	if (ill != NULL) {
5085 		if (ill->ill_phyint != NULL) {
5086 			ipsq_t	*ipsq;
5087 
5088 			ipsq = ill->ill_phyint->phyint_ipsq;
5089 			if (ipsq != NULL) {
5090 				ipsq->ipsq_ipst = NULL;
5091 				kmem_free(ipsq, sizeof (ipsq_t));
5092 			}
5093 			mi_free(ill->ill_phyint);
5094 		}
5095 		ill_free_mib(ill);
5096 		if (ill->ill_ipst != NULL)
5097 			netstack_rele(ill->ill_ipst->ips_netstack);
5098 		mi_free(ill);
5099 	}
5100 	rw_exit(&ipst->ips_ill_g_lock);
5101 	if (error != NULL)
5102 		*error = ENOMEM;
5103 	return (NULL);
5104 }
5105 
5106 /*
5107  * For IPP calls - use the ip_stack_t for global stack.
5108  */
5109 ill_t *
5110 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5111     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5112 {
5113 	ip_stack_t	*ipst;
5114 	ill_t		*ill;
5115 
5116 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5117 	if (ipst == NULL) {
5118 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5119 		return (NULL);
5120 	}
5121 
5122 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5123 	netstack_rele(ipst->ips_netstack);
5124 	return (ill);
5125 }
5126 
5127 /*
5128  * Return a pointer to the ill which matches the index and IP version type.
5129  */
5130 ill_t *
5131 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5132     ipsq_func_t func, int *err, ip_stack_t *ipst)
5133 {
5134 	ill_t	*ill;
5135 	ipsq_t  *ipsq;
5136 	phyint_t *phyi;
5137 
5138 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5139 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5140 
5141 	if (err != NULL)
5142 		*err = 0;
5143 
5144 	/*
5145 	 * Indexes are stored in the phyint - a common structure
5146 	 * to both IPv4 and IPv6.
5147 	 */
5148 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5149 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5150 	    (void *) &index, NULL);
5151 	if (phyi != NULL) {
5152 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5153 		if (ill != NULL) {
5154 			/*
5155 			 * The block comment at the start of ipif_down
5156 			 * explains the use of the macros used below
5157 			 */
5158 			GRAB_CONN_LOCK(q);
5159 			mutex_enter(&ill->ill_lock);
5160 			if (ILL_CAN_LOOKUP(ill)) {
5161 				ill_refhold_locked(ill);
5162 				mutex_exit(&ill->ill_lock);
5163 				RELEASE_CONN_LOCK(q);
5164 				rw_exit(&ipst->ips_ill_g_lock);
5165 				return (ill);
5166 			} else if (ILL_CAN_WAIT(ill, q)) {
5167 				ipsq = ill->ill_phyint->phyint_ipsq;
5168 				mutex_enter(&ipsq->ipsq_lock);
5169 				rw_exit(&ipst->ips_ill_g_lock);
5170 				mutex_exit(&ill->ill_lock);
5171 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5172 				mutex_exit(&ipsq->ipsq_lock);
5173 				RELEASE_CONN_LOCK(q);
5174 				if (err != NULL)
5175 					*err = EINPROGRESS;
5176 				return (NULL);
5177 			}
5178 			RELEASE_CONN_LOCK(q);
5179 			mutex_exit(&ill->ill_lock);
5180 		}
5181 	}
5182 	rw_exit(&ipst->ips_ill_g_lock);
5183 	if (err != NULL)
5184 		*err = ENXIO;
5185 	return (NULL);
5186 }
5187 
5188 /*
5189  * Return the ifindex next in sequence after the passed in ifindex.
5190  * If there is no next ifindex for the given protocol, return 0.
5191  */
5192 uint_t
5193 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5194 {
5195 	phyint_t *phyi;
5196 	phyint_t *phyi_initial;
5197 	uint_t   ifindex;
5198 
5199 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5200 
5201 	if (index == 0) {
5202 		phyi = avl_first(
5203 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5204 	} else {
5205 		phyi = phyi_initial = avl_find(
5206 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5207 		    (void *) &index, NULL);
5208 	}
5209 
5210 	for (; phyi != NULL;
5211 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5212 	    phyi, AVL_AFTER)) {
5213 		/*
5214 		 * If we're not returning the first interface in the tree
5215 		 * and we still haven't moved past the phyint_t that
5216 		 * corresponds to index, avl_walk needs to be called again
5217 		 */
5218 		if (!((index != 0) && (phyi == phyi_initial))) {
5219 			if (isv6) {
5220 				if ((phyi->phyint_illv6) &&
5221 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5222 				    (phyi->phyint_illv6->ill_isv6 == 1))
5223 					break;
5224 			} else {
5225 				if ((phyi->phyint_illv4) &&
5226 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5227 				    (phyi->phyint_illv4->ill_isv6 == 0))
5228 					break;
5229 			}
5230 		}
5231 	}
5232 
5233 	rw_exit(&ipst->ips_ill_g_lock);
5234 
5235 	if (phyi != NULL)
5236 		ifindex = phyi->phyint_ifindex;
5237 	else
5238 		ifindex = 0;
5239 
5240 	return (ifindex);
5241 }
5242 
5243 /*
5244  * Return the ifindex for the named interface.
5245  * If there is no next ifindex for the interface, return 0.
5246  */
5247 uint_t
5248 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5249 {
5250 	phyint_t	*phyi;
5251 	avl_index_t	where = 0;
5252 	uint_t		ifindex;
5253 
5254 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5255 
5256 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5257 	    name, &where)) == NULL) {
5258 		rw_exit(&ipst->ips_ill_g_lock);
5259 		return (0);
5260 	}
5261 
5262 	ifindex = phyi->phyint_ifindex;
5263 
5264 	rw_exit(&ipst->ips_ill_g_lock);
5265 
5266 	return (ifindex);
5267 }
5268 
5269 /*
5270  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5271  * that gives a running thread a reference to the ill. This reference must be
5272  * released by the thread when it is done accessing the ill and related
5273  * objects. ill_refcnt can not be used to account for static references
5274  * such as other structures pointing to an ill. Callers must generally
5275  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5276  * or be sure that the ill is not being deleted or changing state before
5277  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5278  * ill won't change any of its critical state such as address, netmask etc.
5279  */
5280 void
5281 ill_refhold(ill_t *ill)
5282 {
5283 	mutex_enter(&ill->ill_lock);
5284 	ill->ill_refcnt++;
5285 	ILL_TRACE_REF(ill);
5286 	mutex_exit(&ill->ill_lock);
5287 }
5288 
5289 void
5290 ill_refhold_locked(ill_t *ill)
5291 {
5292 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5293 	ill->ill_refcnt++;
5294 	ILL_TRACE_REF(ill);
5295 }
5296 
5297 int
5298 ill_check_and_refhold(ill_t *ill)
5299 {
5300 	mutex_enter(&ill->ill_lock);
5301 	if (ILL_CAN_LOOKUP(ill)) {
5302 		ill_refhold_locked(ill);
5303 		mutex_exit(&ill->ill_lock);
5304 		return (0);
5305 	}
5306 	mutex_exit(&ill->ill_lock);
5307 	return (ILL_LOOKUP_FAILED);
5308 }
5309 
5310 /*
5311  * Must not be called while holding any locks. Otherwise if this is
5312  * the last reference to be released, there is a chance of recursive mutex
5313  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5314  * to restart an ioctl.
5315  */
5316 void
5317 ill_refrele(ill_t *ill)
5318 {
5319 	mutex_enter(&ill->ill_lock);
5320 	ASSERT(ill->ill_refcnt != 0);
5321 	ill->ill_refcnt--;
5322 	ILL_UNTRACE_REF(ill);
5323 	if (ill->ill_refcnt != 0) {
5324 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5325 		mutex_exit(&ill->ill_lock);
5326 		return;
5327 	}
5328 
5329 	/* Drops the ill_lock */
5330 	ipif_ill_refrele_tail(ill);
5331 }
5332 
5333 /*
5334  * Obtain a weak reference count on the ill. This reference ensures the
5335  * ill won't be freed, but the ill may change any of its critical state
5336  * such as netmask, address etc. Returns an error if the ill has started
5337  * closing.
5338  */
5339 boolean_t
5340 ill_waiter_inc(ill_t *ill)
5341 {
5342 	mutex_enter(&ill->ill_lock);
5343 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5344 		mutex_exit(&ill->ill_lock);
5345 		return (B_FALSE);
5346 	}
5347 	ill->ill_waiters++;
5348 	mutex_exit(&ill->ill_lock);
5349 	return (B_TRUE);
5350 }
5351 
5352 void
5353 ill_waiter_dcr(ill_t *ill)
5354 {
5355 	mutex_enter(&ill->ill_lock);
5356 	ill->ill_waiters--;
5357 	if (ill->ill_waiters == 0)
5358 		cv_broadcast(&ill->ill_cv);
5359 	mutex_exit(&ill->ill_lock);
5360 }
5361 
5362 /*
5363  * Named Dispatch routine to produce a formatted report on all ILLs.
5364  * This report is accessed by using the ndd utility to "get" ND variable
5365  * "ip_ill_status".
5366  */
5367 /* ARGSUSED */
5368 int
5369 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5370 {
5371 	ill_t		*ill;
5372 	ill_walk_context_t ctx;
5373 	ip_stack_t	*ipst;
5374 
5375 	ipst = CONNQ_TO_IPST(q);
5376 
5377 	(void) mi_mpprintf(mp,
5378 	    "ILL      " MI_COL_HDRPAD_STR
5379 	/*   01234567[89ABCDEF] */
5380 	    "rq       " MI_COL_HDRPAD_STR
5381 	/*   01234567[89ABCDEF] */
5382 	    "wq       " MI_COL_HDRPAD_STR
5383 	/*   01234567[89ABCDEF] */
5384 	    "upcnt mxfrg err name");
5385 	/*   12345 12345 123 xxxxxxxx  */
5386 
5387 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5388 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5389 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5390 		(void) mi_mpprintf(mp,
5391 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5392 		    "%05u %05u %03d %s",
5393 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5394 		    ill->ill_ipif_up_count,
5395 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5396 	}
5397 	rw_exit(&ipst->ips_ill_g_lock);
5398 
5399 	return (0);
5400 }
5401 
5402 /*
5403  * Named Dispatch routine to produce a formatted report on all IPIFs.
5404  * This report is accessed by using the ndd utility to "get" ND variable
5405  * "ip_ipif_status".
5406  */
5407 /* ARGSUSED */
5408 int
5409 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5410 {
5411 	char	buf1[INET6_ADDRSTRLEN];
5412 	char	buf2[INET6_ADDRSTRLEN];
5413 	char	buf3[INET6_ADDRSTRLEN];
5414 	char	buf4[INET6_ADDRSTRLEN];
5415 	char	buf5[INET6_ADDRSTRLEN];
5416 	char	buf6[INET6_ADDRSTRLEN];
5417 	char	buf[LIFNAMSIZ];
5418 	ill_t	*ill;
5419 	ipif_t	*ipif;
5420 	nv_t	*nvp;
5421 	uint64_t flags;
5422 	zoneid_t zoneid;
5423 	ill_walk_context_t ctx;
5424 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5425 
5426 	(void) mi_mpprintf(mp,
5427 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5428 	    "\tlocal address\n"
5429 	    "\tsrc address\n"
5430 	    "\tsubnet\n"
5431 	    "\tmask\n"
5432 	    "\tbroadcast\n"
5433 	    "\tp-p-dst");
5434 
5435 	ASSERT(q->q_next == NULL);
5436 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5437 
5438 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5439 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5440 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5441 		for (ipif = ill->ill_ipif; ipif != NULL;
5442 		    ipif = ipif->ipif_next) {
5443 			if (zoneid != GLOBAL_ZONEID &&
5444 			    zoneid != ipif->ipif_zoneid &&
5445 			    ipif->ipif_zoneid != ALL_ZONES)
5446 				continue;
5447 
5448 			ipif_get_name(ipif, buf, sizeof (buf));
5449 			(void) mi_mpprintf(mp,
5450 			    MI_COL_PTRFMT_STR
5451 			    "%04u %05u %u/%u/%u %s %d",
5452 			    (void *)ipif,
5453 			    ipif->ipif_metric, ipif->ipif_mtu,
5454 			    ipif->ipif_ib_pkt_count,
5455 			    ipif->ipif_ob_pkt_count,
5456 			    ipif->ipif_fo_pkt_count,
5457 			    buf,
5458 			    ipif->ipif_zoneid);
5459 
5460 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5461 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5462 
5463 		/* Tack on text strings for any flags. */
5464 		nvp = ipif_nv_tbl;
5465 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5466 			if (nvp->nv_value & flags)
5467 				(void) mi_mpprintf_nr(mp, " %s",
5468 				    nvp->nv_name);
5469 		}
5470 		(void) mi_mpprintf(mp,
5471 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5472 		    inet_ntop(AF_INET6,
5473 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5474 		    inet_ntop(AF_INET6,
5475 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5476 		    inet_ntop(AF_INET6,
5477 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5478 		    inet_ntop(AF_INET6,
5479 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5480 		    inet_ntop(AF_INET6,
5481 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5482 		    inet_ntop(AF_INET6,
5483 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5484 		}
5485 	}
5486 	rw_exit(&ipst->ips_ill_g_lock);
5487 	return (0);
5488 }
5489 
5490 /*
5491  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5492  * driver.  We construct best guess defaults for lower level information that
5493  * we need.  If an interface is brought up without injection of any overriding
5494  * information from outside, we have to be ready to go with these defaults.
5495  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5496  * we primarely want the dl_provider_style.
5497  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5498  * at which point we assume the other part of the information is valid.
5499  */
5500 void
5501 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5502 {
5503 	uchar_t		*brdcst_addr;
5504 	uint_t		brdcst_addr_length, phys_addr_length;
5505 	t_scalar_t	sap_length;
5506 	dl_info_ack_t	*dlia;
5507 	ip_m_t		*ipm;
5508 	dl_qos_cl_sel1_t *sel1;
5509 
5510 	ASSERT(IAM_WRITER_ILL(ill));
5511 
5512 	/*
5513 	 * Till the ill is fully up ILL_CHANGING will be set and
5514 	 * the ill is not globally visible. So no need for a lock.
5515 	 */
5516 	dlia = (dl_info_ack_t *)mp->b_rptr;
5517 	ill->ill_mactype = dlia->dl_mac_type;
5518 
5519 	ipm = ip_m_lookup(dlia->dl_mac_type);
5520 	if (ipm == NULL) {
5521 		ipm = ip_m_lookup(DL_OTHER);
5522 		ASSERT(ipm != NULL);
5523 	}
5524 	ill->ill_media = ipm;
5525 
5526 	/*
5527 	 * When the new DLPI stuff is ready we'll pull lengths
5528 	 * from dlia.
5529 	 */
5530 	if (dlia->dl_version == DL_VERSION_2) {
5531 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5532 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5533 		    brdcst_addr_length);
5534 		if (brdcst_addr == NULL) {
5535 			brdcst_addr_length = 0;
5536 		}
5537 		sap_length = dlia->dl_sap_length;
5538 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5539 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5540 		    brdcst_addr_length, sap_length, phys_addr_length));
5541 	} else {
5542 		brdcst_addr_length = 6;
5543 		brdcst_addr = ip_six_byte_all_ones;
5544 		sap_length = -2;
5545 		phys_addr_length = brdcst_addr_length;
5546 	}
5547 
5548 	ill->ill_bcast_addr_length = brdcst_addr_length;
5549 	ill->ill_phys_addr_length = phys_addr_length;
5550 	ill->ill_sap_length = sap_length;
5551 	ill->ill_max_frag = dlia->dl_max_sdu;
5552 	ill->ill_max_mtu = ill->ill_max_frag;
5553 
5554 	ill->ill_type = ipm->ip_m_type;
5555 
5556 	if (!ill->ill_dlpi_style_set) {
5557 		if (dlia->dl_provider_style == DL_STYLE2)
5558 			ill->ill_needs_attach = 1;
5559 
5560 		/*
5561 		 * Allocate the first ipif on this ill. We don't delay it
5562 		 * further as ioctl handling assumes atleast one ipif to
5563 		 * be present.
5564 		 *
5565 		 * At this point we don't know whether the ill is v4 or v6.
5566 		 * We will know this whan the SIOCSLIFNAME happens and
5567 		 * the correct value for ill_isv6 will be assigned in
5568 		 * ipif_set_values(). We need to hold the ill lock and
5569 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5570 		 * the wakeup.
5571 		 */
5572 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5573 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5574 		mutex_enter(&ill->ill_lock);
5575 		ASSERT(ill->ill_dlpi_style_set == 0);
5576 		ill->ill_dlpi_style_set = 1;
5577 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5578 		cv_broadcast(&ill->ill_cv);
5579 		mutex_exit(&ill->ill_lock);
5580 		freemsg(mp);
5581 		return;
5582 	}
5583 	ASSERT(ill->ill_ipif != NULL);
5584 	/*
5585 	 * We know whether it is IPv4 or IPv6 now, as this is the
5586 	 * second DL_INFO_ACK we are recieving in response to the
5587 	 * DL_INFO_REQ sent in ipif_set_values.
5588 	 */
5589 	if (ill->ill_isv6)
5590 		ill->ill_sap = IP6_DL_SAP;
5591 	else
5592 		ill->ill_sap = IP_DL_SAP;
5593 	/*
5594 	 * Set ipif_mtu which is used to set the IRE's
5595 	 * ire_max_frag value. The driver could have sent
5596 	 * a different mtu from what it sent last time. No
5597 	 * need to call ipif_mtu_change because IREs have
5598 	 * not yet been created.
5599 	 */
5600 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5601 	/*
5602 	 * Clear all the flags that were set based on ill_bcast_addr_length
5603 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5604 	 * changed now and we need to re-evaluate.
5605 	 */
5606 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5607 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5608 
5609 	/*
5610 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5611 	 * changed now.
5612 	 */
5613 	if (ill->ill_bcast_addr_length == 0) {
5614 		if (ill->ill_resolver_mp != NULL)
5615 			freemsg(ill->ill_resolver_mp);
5616 		if (ill->ill_bcast_mp != NULL)
5617 			freemsg(ill->ill_bcast_mp);
5618 		if (ill->ill_flags & ILLF_XRESOLV)
5619 			ill->ill_net_type = IRE_IF_RESOLVER;
5620 		else
5621 			ill->ill_net_type = IRE_IF_NORESOLVER;
5622 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5623 		    ill->ill_phys_addr_length,
5624 		    ill->ill_sap,
5625 		    ill->ill_sap_length);
5626 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5627 
5628 		if (ill->ill_isv6)
5629 			/*
5630 			 * Note: xresolv interfaces will eventually need NOARP
5631 			 * set here as well, but that will require those
5632 			 * external resolvers to have some knowledge of
5633 			 * that flag and act appropriately. Not to be changed
5634 			 * at present.
5635 			 */
5636 			ill->ill_flags |= ILLF_NONUD;
5637 		else
5638 			ill->ill_flags |= ILLF_NOARP;
5639 
5640 		if (ill->ill_phys_addr_length == 0) {
5641 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5642 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5643 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5644 			} else {
5645 				/* pt-pt supports multicast. */
5646 				ill->ill_flags |= ILLF_MULTICAST;
5647 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5648 			}
5649 		}
5650 	} else {
5651 		ill->ill_net_type = IRE_IF_RESOLVER;
5652 		if (ill->ill_bcast_mp != NULL)
5653 			freemsg(ill->ill_bcast_mp);
5654 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5655 		    ill->ill_bcast_addr_length, ill->ill_sap,
5656 		    ill->ill_sap_length);
5657 		/*
5658 		 * Later detect lack of DLPI driver multicast
5659 		 * capability by catching DL_ENABMULTI errors in
5660 		 * ip_rput_dlpi.
5661 		 */
5662 		ill->ill_flags |= ILLF_MULTICAST;
5663 		if (!ill->ill_isv6)
5664 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5665 	}
5666 	/* By default an interface does not support any CoS marking */
5667 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5668 
5669 	/*
5670 	 * If we get QoS information in DL_INFO_ACK, the device supports
5671 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5672 	 */
5673 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5674 	    dlia->dl_qos_length);
5675 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5676 		ill->ill_flags |= ILLF_COS_ENABLED;
5677 	}
5678 
5679 	/* Clear any previous error indication. */
5680 	ill->ill_error = 0;
5681 	freemsg(mp);
5682 }
5683 
5684 /*
5685  * Perform various checks to verify that an address would make sense as a
5686  * local, remote, or subnet interface address.
5687  */
5688 static boolean_t
5689 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5690 {
5691 	ipaddr_t	net_mask;
5692 
5693 	/*
5694 	 * Don't allow all zeroes, or all ones, but allow
5695 	 * all ones netmask.
5696 	 */
5697 	if ((net_mask = ip_net_mask(addr)) == 0)
5698 		return (B_FALSE);
5699 	/* A given netmask overrides the "guess" netmask */
5700 	if (subnet_mask != 0)
5701 		net_mask = subnet_mask;
5702 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5703 	    (addr == (addr | ~net_mask)))) {
5704 		return (B_FALSE);
5705 	}
5706 
5707 	/*
5708 	 * Even if the netmask is all ones, we do not allow address to be
5709 	 * 255.255.255.255
5710 	 */
5711 	if (addr == INADDR_BROADCAST)
5712 		return (B_FALSE);
5713 
5714 	if (CLASSD(addr))
5715 		return (B_FALSE);
5716 
5717 	return (B_TRUE);
5718 }
5719 
5720 #define	V6_IPIF_LINKLOCAL(p)	\
5721 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5722 
5723 /*
5724  * Compare two given ipifs and check if the second one is better than
5725  * the first one using the order of preference (not taking deprecated
5726  * into acount) specified in ipif_lookup_multicast().
5727  */
5728 static boolean_t
5729 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5730 {
5731 	/* Check the least preferred first. */
5732 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5733 		/* If both ipifs are the same, use the first one. */
5734 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5735 			return (B_FALSE);
5736 		else
5737 			return (B_TRUE);
5738 	}
5739 
5740 	/* For IPv6, check for link local address. */
5741 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5742 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5743 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5744 			/* The second one is equal or less preferred. */
5745 			return (B_FALSE);
5746 		} else {
5747 			return (B_TRUE);
5748 		}
5749 	}
5750 
5751 	/* Then check for point to point interface. */
5752 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5753 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5754 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5755 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5756 			return (B_FALSE);
5757 		} else {
5758 			return (B_TRUE);
5759 		}
5760 	}
5761 
5762 	/* old_ipif is a normal interface, so no need to use the new one. */
5763 	return (B_FALSE);
5764 }
5765 
5766 /*
5767  * Find any non-virtual, not condemned, and up multicast capable interface
5768  * given an IP instance and zoneid.  Order of preference is:
5769  *
5770  * 1. normal
5771  * 1.1 normal, but deprecated
5772  * 2. point to point
5773  * 2.1 point to point, but deprecated
5774  * 3. link local
5775  * 3.1 link local, but deprecated
5776  * 4. loopback.
5777  */
5778 ipif_t *
5779 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5780 {
5781 	ill_t			*ill;
5782 	ill_walk_context_t	ctx;
5783 	ipif_t			*ipif;
5784 	ipif_t			*saved_ipif = NULL;
5785 	ipif_t			*dep_ipif = NULL;
5786 
5787 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5788 	if (isv6)
5789 		ill = ILL_START_WALK_V6(&ctx, ipst);
5790 	else
5791 		ill = ILL_START_WALK_V4(&ctx, ipst);
5792 
5793 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5794 		mutex_enter(&ill->ill_lock);
5795 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5796 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5797 			mutex_exit(&ill->ill_lock);
5798 			continue;
5799 		}
5800 		for (ipif = ill->ill_ipif; ipif != NULL;
5801 		    ipif = ipif->ipif_next) {
5802 			if (zoneid != ipif->ipif_zoneid &&
5803 			    zoneid != ALL_ZONES &&
5804 			    ipif->ipif_zoneid != ALL_ZONES) {
5805 				continue;
5806 			}
5807 			if (!(ipif->ipif_flags & IPIF_UP) ||
5808 			    !IPIF_CAN_LOOKUP(ipif)) {
5809 				continue;
5810 			}
5811 
5812 			/*
5813 			 * Found one candidate.  If it is deprecated,
5814 			 * remember it in dep_ipif.  If it is not deprecated,
5815 			 * remember it in saved_ipif.
5816 			 */
5817 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5818 				if (dep_ipif == NULL) {
5819 					dep_ipif = ipif;
5820 				} else if (ipif_comp_multi(dep_ipif, ipif,
5821 				    isv6)) {
5822 					/*
5823 					 * If the previous dep_ipif does not
5824 					 * belong to the same ill, we've done
5825 					 * a ipif_refhold() on it.  So we need
5826 					 * to release it.
5827 					 */
5828 					if (dep_ipif->ipif_ill != ill)
5829 						ipif_refrele(dep_ipif);
5830 					dep_ipif = ipif;
5831 				}
5832 				continue;
5833 			}
5834 			if (saved_ipif == NULL) {
5835 				saved_ipif = ipif;
5836 			} else {
5837 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5838 					if (saved_ipif->ipif_ill != ill)
5839 						ipif_refrele(saved_ipif);
5840 					saved_ipif = ipif;
5841 				}
5842 			}
5843 		}
5844 		/*
5845 		 * Before going to the next ill, do a ipif_refhold() on the
5846 		 * saved ones.
5847 		 */
5848 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5849 			ipif_refhold_locked(saved_ipif);
5850 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5851 			ipif_refhold_locked(dep_ipif);
5852 		mutex_exit(&ill->ill_lock);
5853 	}
5854 	rw_exit(&ipst->ips_ill_g_lock);
5855 
5856 	/*
5857 	 * If we have only the saved_ipif, return it.  But if we have both
5858 	 * saved_ipif and dep_ipif, check to see which one is better.
5859 	 */
5860 	if (saved_ipif != NULL) {
5861 		if (dep_ipif != NULL) {
5862 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5863 				ipif_refrele(saved_ipif);
5864 				return (dep_ipif);
5865 			} else {
5866 				ipif_refrele(dep_ipif);
5867 				return (saved_ipif);
5868 			}
5869 		}
5870 		return (saved_ipif);
5871 	} else {
5872 		return (dep_ipif);
5873 	}
5874 }
5875 
5876 /*
5877  * This function is called when an application does not specify an interface
5878  * to be used for multicast traffic (joining a group/sending data).  It
5879  * calls ire_lookup_multi() to look for an interface route for the
5880  * specified multicast group.  Doing this allows the administrator to add
5881  * prefix routes for multicast to indicate which interface to be used for
5882  * multicast traffic in the above scenario.  The route could be for all
5883  * multicast (224.0/4), for a single multicast group (a /32 route) or
5884  * anything in between.  If there is no such multicast route, we just find
5885  * any multicast capable interface and return it.  The returned ipif
5886  * is refhold'ed.
5887  */
5888 ipif_t *
5889 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5890 {
5891 	ire_t			*ire;
5892 	ipif_t			*ipif;
5893 
5894 	ire = ire_lookup_multi(group, zoneid, ipst);
5895 	if (ire != NULL) {
5896 		ipif = ire->ire_ipif;
5897 		ipif_refhold(ipif);
5898 		ire_refrele(ire);
5899 		return (ipif);
5900 	}
5901 
5902 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5903 }
5904 
5905 /*
5906  * Look for an ipif with the specified interface address and destination.
5907  * The destination address is used only for matching point-to-point interfaces.
5908  */
5909 ipif_t *
5910 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5911     ipsq_func_t func, int *error, ip_stack_t *ipst)
5912 {
5913 	ipif_t	*ipif;
5914 	ill_t	*ill;
5915 	ill_walk_context_t ctx;
5916 	ipsq_t	*ipsq;
5917 
5918 	if (error != NULL)
5919 		*error = 0;
5920 
5921 	/*
5922 	 * First match all the point-to-point interfaces
5923 	 * before looking at non-point-to-point interfaces.
5924 	 * This is done to avoid returning non-point-to-point
5925 	 * ipif instead of unnumbered point-to-point ipif.
5926 	 */
5927 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5928 	ill = ILL_START_WALK_V4(&ctx, ipst);
5929 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5930 		GRAB_CONN_LOCK(q);
5931 		mutex_enter(&ill->ill_lock);
5932 		for (ipif = ill->ill_ipif; ipif != NULL;
5933 		    ipif = ipif->ipif_next) {
5934 			/* Allow the ipif to be down */
5935 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5936 			    (ipif->ipif_lcl_addr == if_addr) &&
5937 			    (ipif->ipif_pp_dst_addr == dst)) {
5938 				/*
5939 				 * The block comment at the start of ipif_down
5940 				 * explains the use of the macros used below
5941 				 */
5942 				if (IPIF_CAN_LOOKUP(ipif)) {
5943 					ipif_refhold_locked(ipif);
5944 					mutex_exit(&ill->ill_lock);
5945 					RELEASE_CONN_LOCK(q);
5946 					rw_exit(&ipst->ips_ill_g_lock);
5947 					return (ipif);
5948 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5949 					ipsq = ill->ill_phyint->phyint_ipsq;
5950 					mutex_enter(&ipsq->ipsq_lock);
5951 					mutex_exit(&ill->ill_lock);
5952 					rw_exit(&ipst->ips_ill_g_lock);
5953 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5954 					    ill);
5955 					mutex_exit(&ipsq->ipsq_lock);
5956 					RELEASE_CONN_LOCK(q);
5957 					if (error != NULL)
5958 						*error = EINPROGRESS;
5959 					return (NULL);
5960 				}
5961 			}
5962 		}
5963 		mutex_exit(&ill->ill_lock);
5964 		RELEASE_CONN_LOCK(q);
5965 	}
5966 	rw_exit(&ipst->ips_ill_g_lock);
5967 
5968 	/* lookup the ipif based on interface address */
5969 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5970 	    ipst);
5971 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5972 	return (ipif);
5973 }
5974 
5975 /*
5976  * Look for an ipif with the specified address. For point-point links
5977  * we look for matches on either the destination address and the local
5978  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5979  * is set.
5980  * Matches on a specific ill if match_ill is set.
5981  */
5982 ipif_t *
5983 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5984     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5985 {
5986 	ipif_t  *ipif;
5987 	ill_t   *ill;
5988 	boolean_t ptp = B_FALSE;
5989 	ipsq_t	*ipsq;
5990 	ill_walk_context_t	ctx;
5991 
5992 	if (error != NULL)
5993 		*error = 0;
5994 
5995 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5996 	/*
5997 	 * Repeat twice, first based on local addresses and
5998 	 * next time for pointopoint.
5999 	 */
6000 repeat:
6001 	ill = ILL_START_WALK_V4(&ctx, ipst);
6002 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6003 		if (match_ill != NULL && ill != match_ill) {
6004 			continue;
6005 		}
6006 		GRAB_CONN_LOCK(q);
6007 		mutex_enter(&ill->ill_lock);
6008 		for (ipif = ill->ill_ipif; ipif != NULL;
6009 		    ipif = ipif->ipif_next) {
6010 			if (zoneid != ALL_ZONES &&
6011 			    zoneid != ipif->ipif_zoneid &&
6012 			    ipif->ipif_zoneid != ALL_ZONES)
6013 				continue;
6014 			/* Allow the ipif to be down */
6015 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6016 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6017 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6018 			    (ipif->ipif_pp_dst_addr == addr))) {
6019 				/*
6020 				 * The block comment at the start of ipif_down
6021 				 * explains the use of the macros used below
6022 				 */
6023 				if (IPIF_CAN_LOOKUP(ipif)) {
6024 					ipif_refhold_locked(ipif);
6025 					mutex_exit(&ill->ill_lock);
6026 					RELEASE_CONN_LOCK(q);
6027 					rw_exit(&ipst->ips_ill_g_lock);
6028 					return (ipif);
6029 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6030 					ipsq = ill->ill_phyint->phyint_ipsq;
6031 					mutex_enter(&ipsq->ipsq_lock);
6032 					mutex_exit(&ill->ill_lock);
6033 					rw_exit(&ipst->ips_ill_g_lock);
6034 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6035 					    ill);
6036 					mutex_exit(&ipsq->ipsq_lock);
6037 					RELEASE_CONN_LOCK(q);
6038 					if (error != NULL)
6039 						*error = EINPROGRESS;
6040 					return (NULL);
6041 				}
6042 			}
6043 		}
6044 		mutex_exit(&ill->ill_lock);
6045 		RELEASE_CONN_LOCK(q);
6046 	}
6047 
6048 	/* If we already did the ptp case, then we are done */
6049 	if (ptp) {
6050 		rw_exit(&ipst->ips_ill_g_lock);
6051 		if (error != NULL)
6052 			*error = ENXIO;
6053 		return (NULL);
6054 	}
6055 	ptp = B_TRUE;
6056 	goto repeat;
6057 }
6058 
6059 /*
6060  * Look for an ipif with the specified address. For point-point links
6061  * we look for matches on either the destination address and the local
6062  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6063  * is set.
6064  * Matches on a specific ill if match_ill is set.
6065  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6066  */
6067 zoneid_t
6068 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6069 {
6070 	zoneid_t zoneid;
6071 	ipif_t  *ipif;
6072 	ill_t   *ill;
6073 	boolean_t ptp = B_FALSE;
6074 	ill_walk_context_t	ctx;
6075 
6076 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6077 	/*
6078 	 * Repeat twice, first based on local addresses and
6079 	 * next time for pointopoint.
6080 	 */
6081 repeat:
6082 	ill = ILL_START_WALK_V4(&ctx, ipst);
6083 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6084 		if (match_ill != NULL && ill != match_ill) {
6085 			continue;
6086 		}
6087 		mutex_enter(&ill->ill_lock);
6088 		for (ipif = ill->ill_ipif; ipif != NULL;
6089 		    ipif = ipif->ipif_next) {
6090 			/* Allow the ipif to be down */
6091 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6092 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6093 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6094 			    (ipif->ipif_pp_dst_addr == addr)) &&
6095 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6096 				zoneid = ipif->ipif_zoneid;
6097 				mutex_exit(&ill->ill_lock);
6098 				rw_exit(&ipst->ips_ill_g_lock);
6099 				/*
6100 				 * If ipif_zoneid was ALL_ZONES then we have
6101 				 * a trusted extensions shared IP address.
6102 				 * In that case GLOBAL_ZONEID works to send.
6103 				 */
6104 				if (zoneid == ALL_ZONES)
6105 					zoneid = GLOBAL_ZONEID;
6106 				return (zoneid);
6107 			}
6108 		}
6109 		mutex_exit(&ill->ill_lock);
6110 	}
6111 
6112 	/* If we already did the ptp case, then we are done */
6113 	if (ptp) {
6114 		rw_exit(&ipst->ips_ill_g_lock);
6115 		return (ALL_ZONES);
6116 	}
6117 	ptp = B_TRUE;
6118 	goto repeat;
6119 }
6120 
6121 /*
6122  * Look for an ipif that matches the specified remote address i.e. the
6123  * ipif that would receive the specified packet.
6124  * First look for directly connected interfaces and then do a recursive
6125  * IRE lookup and pick the first ipif corresponding to the source address in the
6126  * ire.
6127  * Returns: held ipif
6128  */
6129 ipif_t *
6130 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6131 {
6132 	ipif_t	*ipif;
6133 	ire_t	*ire;
6134 	ip_stack_t	*ipst = ill->ill_ipst;
6135 
6136 	ASSERT(!ill->ill_isv6);
6137 
6138 	/*
6139 	 * Someone could be changing this ipif currently or change it
6140 	 * after we return this. Thus  a few packets could use the old
6141 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6142 	 * will atomically be updated or cleaned up with the new value
6143 	 * Thus we don't need a lock to check the flags or other attrs below.
6144 	 */
6145 	mutex_enter(&ill->ill_lock);
6146 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6147 		if (!IPIF_CAN_LOOKUP(ipif))
6148 			continue;
6149 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6150 		    ipif->ipif_zoneid != ALL_ZONES)
6151 			continue;
6152 		/* Allow the ipif to be down */
6153 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6154 			if ((ipif->ipif_pp_dst_addr == addr) ||
6155 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6156 			    ipif->ipif_lcl_addr == addr)) {
6157 				ipif_refhold_locked(ipif);
6158 				mutex_exit(&ill->ill_lock);
6159 				return (ipif);
6160 			}
6161 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6162 			ipif_refhold_locked(ipif);
6163 			mutex_exit(&ill->ill_lock);
6164 			return (ipif);
6165 		}
6166 	}
6167 	mutex_exit(&ill->ill_lock);
6168 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6169 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6170 	if (ire != NULL) {
6171 		/*
6172 		 * The callers of this function wants to know the
6173 		 * interface on which they have to send the replies
6174 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6175 		 * derived from different ills, we really don't care
6176 		 * what we return here.
6177 		 */
6178 		ipif = ire->ire_ipif;
6179 		if (ipif != NULL) {
6180 			ipif_refhold(ipif);
6181 			ire_refrele(ire);
6182 			return (ipif);
6183 		}
6184 		ire_refrele(ire);
6185 	}
6186 	/* Pick the first interface */
6187 	ipif = ipif_get_next_ipif(NULL, ill);
6188 	return (ipif);
6189 }
6190 
6191 /*
6192  * This func does not prevent refcnt from increasing. But if
6193  * the caller has taken steps to that effect, then this func
6194  * can be used to determine whether the ill has become quiescent
6195  */
6196 static boolean_t
6197 ill_is_quiescent(ill_t *ill)
6198 {
6199 	ipif_t	*ipif;
6200 
6201 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6202 
6203 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6204 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6205 			return (B_FALSE);
6206 		}
6207 	}
6208 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6209 		return (B_FALSE);
6210 	}
6211 	return (B_TRUE);
6212 }
6213 
6214 boolean_t
6215 ill_is_freeable(ill_t *ill)
6216 {
6217 	ipif_t	*ipif;
6218 
6219 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6220 
6221 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6222 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6223 			return (B_FALSE);
6224 		}
6225 	}
6226 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6227 		return (B_FALSE);
6228 	}
6229 	return (B_TRUE);
6230 }
6231 
6232 /*
6233  * This func does not prevent refcnt from increasing. But if
6234  * the caller has taken steps to that effect, then this func
6235  * can be used to determine whether the ipif has become quiescent
6236  */
6237 static boolean_t
6238 ipif_is_quiescent(ipif_t *ipif)
6239 {
6240 	ill_t *ill;
6241 
6242 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6243 
6244 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6245 		return (B_FALSE);
6246 	}
6247 
6248 	ill = ipif->ipif_ill;
6249 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6250 	    ill->ill_logical_down) {
6251 		return (B_TRUE);
6252 	}
6253 
6254 	/* This is the last ipif going down or being deleted on this ill */
6255 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6256 		return (B_FALSE);
6257 	}
6258 
6259 	return (B_TRUE);
6260 }
6261 
6262 /*
6263  * return true if the ipif can be destroyed: the ipif has to be quiescent
6264  * with zero references from ire/nce/ilm to it.
6265  */
6266 static boolean_t
6267 ipif_is_freeable(ipif_t *ipif)
6268 {
6269 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6270 	ASSERT(ipif->ipif_id != 0);
6271 	return (ipif->ipif_refcnt == 0 && IPIF_FREE_OK(ipif));
6272 }
6273 
6274 /*
6275  * This func does not prevent refcnt from increasing. But if
6276  * the caller has taken steps to that effect, then this func
6277  * can be used to determine whether the ipifs marked with IPIF_MOVING
6278  * have become quiescent and can be moved in a failover/failback.
6279  */
6280 static ipif_t *
6281 ill_quiescent_to_move(ill_t *ill)
6282 {
6283 	ipif_t  *ipif;
6284 
6285 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6286 
6287 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6288 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6289 			if (ipif->ipif_refcnt != 0 ||
6290 			    !IPIF_DOWN_OK(ipif)) {
6291 				return (ipif);
6292 			}
6293 		}
6294 	}
6295 	return (NULL);
6296 }
6297 
6298 /*
6299  * The ipif/ill/ire has been refreled. Do the tail processing.
6300  * Determine if the ipif or ill in question has become quiescent and if so
6301  * wakeup close and/or restart any queued pending ioctl that is waiting
6302  * for the ipif_down (or ill_down)
6303  */
6304 void
6305 ipif_ill_refrele_tail(ill_t *ill)
6306 {
6307 	mblk_t	*mp;
6308 	conn_t	*connp;
6309 	ipsq_t	*ipsq;
6310 	ipif_t	*ipif;
6311 	dl_notify_ind_t *dlindp;
6312 
6313 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6314 
6315 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6316 	    ill_is_freeable(ill)) {
6317 		/* ill_close may be waiting */
6318 		cv_broadcast(&ill->ill_cv);
6319 	}
6320 
6321 	/* ipsq can't change because ill_lock  is held */
6322 	ipsq = ill->ill_phyint->phyint_ipsq;
6323 	if (ipsq->ipsq_waitfor == 0) {
6324 		/* Not waiting for anything, just return. */
6325 		mutex_exit(&ill->ill_lock);
6326 		return;
6327 	}
6328 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6329 	    ipsq->ipsq_pending_ipif != NULL);
6330 	/*
6331 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6332 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6333 	 * be zero for restarting an ioctl that ends up downing the ill.
6334 	 */
6335 	ipif = ipsq->ipsq_pending_ipif;
6336 	if (ipif->ipif_ill != ill) {
6337 		/* The ioctl is pending on some other ill. */
6338 		mutex_exit(&ill->ill_lock);
6339 		return;
6340 	}
6341 
6342 	switch (ipsq->ipsq_waitfor) {
6343 	case IPIF_DOWN:
6344 		if (!ipif_is_quiescent(ipif)) {
6345 			mutex_exit(&ill->ill_lock);
6346 			return;
6347 		}
6348 		break;
6349 	case IPIF_FREE:
6350 		if (!ipif_is_freeable(ipif)) {
6351 			mutex_exit(&ill->ill_lock);
6352 			return;
6353 		}
6354 		break;
6355 
6356 	case ILL_DOWN:
6357 		if (!ill_is_quiescent(ill)) {
6358 			mutex_exit(&ill->ill_lock);
6359 			return;
6360 		}
6361 		break;
6362 	case ILL_FREE:
6363 		/*
6364 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6365 		 * waits synchronously in ip_close, and no message is queued in
6366 		 * ipsq_pending_mp at all in this case
6367 		 */
6368 		if (!ill_is_freeable(ill)) {
6369 			mutex_exit(&ill->ill_lock);
6370 			return;
6371 		}
6372 		break;
6373 
6374 	case ILL_MOVE_OK:
6375 		if (ill_quiescent_to_move(ill) != NULL) {
6376 			mutex_exit(&ill->ill_lock);
6377 			return;
6378 		}
6379 		break;
6380 	default:
6381 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6382 		    (void *)ipsq, ipsq->ipsq_waitfor);
6383 	}
6384 
6385 	/*
6386 	 * Incr refcnt for the qwriter_ip call below which
6387 	 * does a refrele
6388 	 */
6389 	ill_refhold_locked(ill);
6390 	mp = ipsq_pending_mp_get(ipsq, &connp);
6391 	mutex_exit(&ill->ill_lock);
6392 
6393 	ASSERT(mp != NULL);
6394 	/*
6395 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6396 	 * we can only get here when the current operation decides it
6397 	 * it needs to quiesce via ipsq_pending_mp_add().
6398 	 */
6399 	switch (mp->b_datap->db_type) {
6400 	case M_PCPROTO:
6401 	case M_PROTO:
6402 		/*
6403 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6404 		 */
6405 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6406 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6407 
6408 		switch (dlindp->dl_notification) {
6409 		case DL_NOTE_PHYS_ADDR:
6410 			qwriter_ip(ill, ill->ill_rq, mp,
6411 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6412 			return;
6413 		default:
6414 			ASSERT(0);
6415 		}
6416 		break;
6417 
6418 	case M_ERROR:
6419 	case M_HANGUP:
6420 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6421 		    B_TRUE);
6422 		return;
6423 
6424 	case M_IOCTL:
6425 	case M_IOCDATA:
6426 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6427 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6428 		return;
6429 
6430 	default:
6431 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6432 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6433 	}
6434 }
6435 
6436 #ifdef DEBUG
6437 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6438 static void
6439 th_trace_rrecord(th_trace_t *th_trace)
6440 {
6441 	tr_buf_t *tr_buf;
6442 	uint_t lastref;
6443 
6444 	lastref = th_trace->th_trace_lastref;
6445 	lastref++;
6446 	if (lastref == TR_BUF_MAX)
6447 		lastref = 0;
6448 	th_trace->th_trace_lastref = lastref;
6449 	tr_buf = &th_trace->th_trbuf[lastref];
6450 	tr_buf->tr_time = lbolt;
6451 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6452 }
6453 
6454 static void
6455 th_trace_free(void *value)
6456 {
6457 	th_trace_t *th_trace = value;
6458 
6459 	ASSERT(th_trace->th_refcnt == 0);
6460 	kmem_free(th_trace, sizeof (*th_trace));
6461 }
6462 
6463 /*
6464  * Find or create the per-thread hash table used to track object references.
6465  * The ipst argument is NULL if we shouldn't allocate.
6466  *
6467  * Accesses per-thread data, so there's no need to lock here.
6468  */
6469 static mod_hash_t *
6470 th_trace_gethash(ip_stack_t *ipst)
6471 {
6472 	th_hash_t *thh;
6473 
6474 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6475 		mod_hash_t *mh;
6476 		char name[256];
6477 		size_t objsize, rshift;
6478 		int retv;
6479 
6480 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6481 			return (NULL);
6482 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6483 		    (void *)curthread);
6484 
6485 		/*
6486 		 * We use mod_hash_create_extended here rather than the more
6487 		 * obvious mod_hash_create_ptrhash because the latter has a
6488 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6489 		 * block.
6490 		 */
6491 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6492 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6493 		rshift = highbit(objsize);
6494 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6495 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6496 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6497 		if (mh == NULL) {
6498 			kmem_free(thh, sizeof (*thh));
6499 			return (NULL);
6500 		}
6501 		thh->thh_hash = mh;
6502 		thh->thh_ipst = ipst;
6503 		/*
6504 		 * We trace ills, ipifs, ires, and nces.  All of these are
6505 		 * per-IP-stack, so the lock on the thread list is as well.
6506 		 */
6507 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6508 		list_insert_tail(&ip_thread_list, thh);
6509 		rw_exit(&ip_thread_rwlock);
6510 		retv = tsd_set(ip_thread_data, thh);
6511 		ASSERT(retv == 0);
6512 	}
6513 	return (thh != NULL ? thh->thh_hash : NULL);
6514 }
6515 
6516 boolean_t
6517 th_trace_ref(const void *obj, ip_stack_t *ipst)
6518 {
6519 	th_trace_t *th_trace;
6520 	mod_hash_t *mh;
6521 	mod_hash_val_t val;
6522 
6523 	if ((mh = th_trace_gethash(ipst)) == NULL)
6524 		return (B_FALSE);
6525 
6526 	/*
6527 	 * Attempt to locate the trace buffer for this obj and thread.
6528 	 * If it does not exist, then allocate a new trace buffer and
6529 	 * insert into the hash.
6530 	 */
6531 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6532 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6533 		if (th_trace == NULL)
6534 			return (B_FALSE);
6535 
6536 		th_trace->th_id = curthread;
6537 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6538 		    (mod_hash_val_t)th_trace) != 0) {
6539 			kmem_free(th_trace, sizeof (th_trace_t));
6540 			return (B_FALSE);
6541 		}
6542 	} else {
6543 		th_trace = (th_trace_t *)val;
6544 	}
6545 
6546 	ASSERT(th_trace->th_refcnt >= 0 &&
6547 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6548 
6549 	th_trace->th_refcnt++;
6550 	th_trace_rrecord(th_trace);
6551 	return (B_TRUE);
6552 }
6553 
6554 /*
6555  * For the purpose of tracing a reference release, we assume that global
6556  * tracing is always on and that the same thread initiated the reference hold
6557  * is releasing.
6558  */
6559 void
6560 th_trace_unref(const void *obj)
6561 {
6562 	int retv;
6563 	mod_hash_t *mh;
6564 	th_trace_t *th_trace;
6565 	mod_hash_val_t val;
6566 
6567 	mh = th_trace_gethash(NULL);
6568 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6569 	ASSERT(retv == 0);
6570 	th_trace = (th_trace_t *)val;
6571 
6572 	ASSERT(th_trace->th_refcnt > 0);
6573 	th_trace->th_refcnt--;
6574 	th_trace_rrecord(th_trace);
6575 }
6576 
6577 /*
6578  * If tracing has been disabled, then we assume that the reference counts are
6579  * now useless, and we clear them out before destroying the entries.
6580  */
6581 void
6582 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6583 {
6584 	th_hash_t	*thh;
6585 	mod_hash_t	*mh;
6586 	mod_hash_val_t	val;
6587 	th_trace_t	*th_trace;
6588 	int		retv;
6589 
6590 	rw_enter(&ip_thread_rwlock, RW_READER);
6591 	for (thh = list_head(&ip_thread_list); thh != NULL;
6592 	    thh = list_next(&ip_thread_list, thh)) {
6593 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6594 		    &val) == 0) {
6595 			th_trace = (th_trace_t *)val;
6596 			if (trace_disable)
6597 				th_trace->th_refcnt = 0;
6598 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6599 			ASSERT(retv == 0);
6600 		}
6601 	}
6602 	rw_exit(&ip_thread_rwlock);
6603 }
6604 
6605 void
6606 ipif_trace_ref(ipif_t *ipif)
6607 {
6608 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6609 
6610 	if (ipif->ipif_trace_disable)
6611 		return;
6612 
6613 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6614 		ipif->ipif_trace_disable = B_TRUE;
6615 		ipif_trace_cleanup(ipif);
6616 	}
6617 }
6618 
6619 void
6620 ipif_untrace_ref(ipif_t *ipif)
6621 {
6622 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6623 
6624 	if (!ipif->ipif_trace_disable)
6625 		th_trace_unref(ipif);
6626 }
6627 
6628 void
6629 ill_trace_ref(ill_t *ill)
6630 {
6631 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6632 
6633 	if (ill->ill_trace_disable)
6634 		return;
6635 
6636 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6637 		ill->ill_trace_disable = B_TRUE;
6638 		ill_trace_cleanup(ill);
6639 	}
6640 }
6641 
6642 void
6643 ill_untrace_ref(ill_t *ill)
6644 {
6645 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6646 
6647 	if (!ill->ill_trace_disable)
6648 		th_trace_unref(ill);
6649 }
6650 
6651 /*
6652  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6653  * failure, ipif_trace_disable is set.
6654  */
6655 static void
6656 ipif_trace_cleanup(const ipif_t *ipif)
6657 {
6658 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6659 }
6660 
6661 /*
6662  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6663  * failure, ill_trace_disable is set.
6664  */
6665 static void
6666 ill_trace_cleanup(const ill_t *ill)
6667 {
6668 	th_trace_cleanup(ill, ill->ill_trace_disable);
6669 }
6670 #endif /* DEBUG */
6671 
6672 void
6673 ipif_refhold_locked(ipif_t *ipif)
6674 {
6675 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6676 	ipif->ipif_refcnt++;
6677 	IPIF_TRACE_REF(ipif);
6678 }
6679 
6680 void
6681 ipif_refhold(ipif_t *ipif)
6682 {
6683 	ill_t	*ill;
6684 
6685 	ill = ipif->ipif_ill;
6686 	mutex_enter(&ill->ill_lock);
6687 	ipif->ipif_refcnt++;
6688 	IPIF_TRACE_REF(ipif);
6689 	mutex_exit(&ill->ill_lock);
6690 }
6691 
6692 /*
6693  * Must not be called while holding any locks. Otherwise if this is
6694  * the last reference to be released there is a chance of recursive mutex
6695  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6696  * to restart an ioctl.
6697  */
6698 void
6699 ipif_refrele(ipif_t *ipif)
6700 {
6701 	ill_t	*ill;
6702 
6703 	ill = ipif->ipif_ill;
6704 
6705 	mutex_enter(&ill->ill_lock);
6706 	ASSERT(ipif->ipif_refcnt != 0);
6707 	ipif->ipif_refcnt--;
6708 	IPIF_UNTRACE_REF(ipif);
6709 	if (ipif->ipif_refcnt != 0) {
6710 		mutex_exit(&ill->ill_lock);
6711 		return;
6712 	}
6713 
6714 	/* Drops the ill_lock */
6715 	ipif_ill_refrele_tail(ill);
6716 }
6717 
6718 ipif_t *
6719 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6720 {
6721 	ipif_t	*ipif;
6722 
6723 	mutex_enter(&ill->ill_lock);
6724 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6725 	    ipif != NULL; ipif = ipif->ipif_next) {
6726 		if (!IPIF_CAN_LOOKUP(ipif))
6727 			continue;
6728 		ipif_refhold_locked(ipif);
6729 		mutex_exit(&ill->ill_lock);
6730 		return (ipif);
6731 	}
6732 	mutex_exit(&ill->ill_lock);
6733 	return (NULL);
6734 }
6735 
6736 /*
6737  * TODO: make this table extendible at run time
6738  * Return a pointer to the mac type info for 'mac_type'
6739  */
6740 static ip_m_t *
6741 ip_m_lookup(t_uscalar_t mac_type)
6742 {
6743 	ip_m_t	*ipm;
6744 
6745 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6746 		if (ipm->ip_m_mac_type == mac_type)
6747 			return (ipm);
6748 	return (NULL);
6749 }
6750 
6751 /*
6752  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6753  * ipif_arg is passed in to associate it with the correct interface.
6754  * We may need to restart this operation if the ipif cannot be looked up
6755  * due to an exclusive operation that is currently in progress. The restart
6756  * entry point is specified by 'func'
6757  */
6758 int
6759 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6760     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6761     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6762     struct rtsa_s *sp, ip_stack_t *ipst)
6763 {
6764 	ire_t	*ire;
6765 	ire_t	*gw_ire = NULL;
6766 	ipif_t	*ipif = NULL;
6767 	boolean_t ipif_refheld = B_FALSE;
6768 	uint_t	type;
6769 	int	match_flags = MATCH_IRE_TYPE;
6770 	int	error;
6771 	tsol_gc_t *gc = NULL;
6772 	tsol_gcgrp_t *gcgrp = NULL;
6773 	boolean_t gcgrp_xtraref = B_FALSE;
6774 
6775 	ip1dbg(("ip_rt_add:"));
6776 
6777 	if (ire_arg != NULL)
6778 		*ire_arg = NULL;
6779 
6780 	/*
6781 	 * If this is the case of RTF_HOST being set, then we set the netmask
6782 	 * to all ones (regardless if one was supplied).
6783 	 */
6784 	if (flags & RTF_HOST)
6785 		mask = IP_HOST_MASK;
6786 
6787 	/*
6788 	 * Prevent routes with a zero gateway from being created (since
6789 	 * interfaces can currently be plumbed and brought up no assigned
6790 	 * address).
6791 	 */
6792 	if (gw_addr == 0)
6793 		return (ENETUNREACH);
6794 	/*
6795 	 * Get the ipif, if any, corresponding to the gw_addr
6796 	 */
6797 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6798 	    ipst);
6799 	if (ipif != NULL) {
6800 		if (IS_VNI(ipif->ipif_ill)) {
6801 			ipif_refrele(ipif);
6802 			return (EINVAL);
6803 		}
6804 		ipif_refheld = B_TRUE;
6805 	} else if (error == EINPROGRESS) {
6806 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6807 		return (EINPROGRESS);
6808 	} else {
6809 		error = 0;
6810 	}
6811 
6812 	if (ipif != NULL) {
6813 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6814 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6815 	} else {
6816 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6817 	}
6818 
6819 	/*
6820 	 * GateD will attempt to create routes with a loopback interface
6821 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6822 	 * these routes to be added, but create them as interface routes
6823 	 * since the gateway is an interface address.
6824 	 */
6825 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6826 		flags &= ~RTF_GATEWAY;
6827 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6828 		    mask == IP_HOST_MASK) {
6829 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6830 			    ALL_ZONES, NULL, match_flags, ipst);
6831 			if (ire != NULL) {
6832 				ire_refrele(ire);
6833 				if (ipif_refheld)
6834 					ipif_refrele(ipif);
6835 				return (EEXIST);
6836 			}
6837 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6838 			    "for 0x%x\n", (void *)ipif,
6839 			    ipif->ipif_ire_type,
6840 			    ntohl(ipif->ipif_lcl_addr)));
6841 			ire = ire_create(
6842 			    (uchar_t *)&dst_addr,	/* dest address */
6843 			    (uchar_t *)&mask,		/* mask */
6844 			    (uchar_t *)&ipif->ipif_src_addr,
6845 			    NULL,			/* no gateway */
6846 			    &ipif->ipif_mtu,
6847 			    NULL,
6848 			    ipif->ipif_rq,		/* recv-from queue */
6849 			    NULL,			/* no send-to queue */
6850 			    ipif->ipif_ire_type,	/* LOOPBACK */
6851 			    ipif,
6852 			    0,
6853 			    0,
6854 			    0,
6855 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6856 			    RTF_PRIVATE : 0,
6857 			    &ire_uinfo_null,
6858 			    NULL,
6859 			    NULL,
6860 			    ipst);
6861 
6862 			if (ire == NULL) {
6863 				if (ipif_refheld)
6864 					ipif_refrele(ipif);
6865 				return (ENOMEM);
6866 			}
6867 			error = ire_add(&ire, q, mp, func, B_FALSE);
6868 			if (error == 0)
6869 				goto save_ire;
6870 			if (ipif_refheld)
6871 				ipif_refrele(ipif);
6872 			return (error);
6873 
6874 		}
6875 	}
6876 
6877 	/*
6878 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6879 	 * and the gateway address provided is one of the system's interface
6880 	 * addresses.  By using the routing socket interface and supplying an
6881 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6882 	 * specifying an interface route to be created is available which uses
6883 	 * the interface index that specifies the outgoing interface rather than
6884 	 * the address of an outgoing interface (which may not be able to
6885 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6886 	 * flag, routes can be specified which not only specify the next-hop to
6887 	 * be used when routing to a certain prefix, but also which outgoing
6888 	 * interface should be used.
6889 	 *
6890 	 * Previously, interfaces would have unique addresses assigned to them
6891 	 * and so the address assigned to a particular interface could be used
6892 	 * to identify a particular interface.  One exception to this was the
6893 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6894 	 *
6895 	 * With the advent of IPv6 and its link-local addresses, this
6896 	 * restriction was relaxed and interfaces could share addresses between
6897 	 * themselves.  In fact, typically all of the link-local interfaces on
6898 	 * an IPv6 node or router will have the same link-local address.  In
6899 	 * order to differentiate between these interfaces, the use of an
6900 	 * interface index is necessary and this index can be carried inside a
6901 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6902 	 * of using the interface index, however, is that all of the ipif's that
6903 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6904 	 * cannot be used to differentiate between ipif's (or logical
6905 	 * interfaces) that belong to the same ill (physical interface).
6906 	 *
6907 	 * For example, in the following case involving IPv4 interfaces and
6908 	 * logical interfaces
6909 	 *
6910 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6911 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6912 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6913 	 *
6914 	 * the ipif's corresponding to each of these interface routes can be
6915 	 * uniquely identified by the "gateway" (actually interface address).
6916 	 *
6917 	 * In this case involving multiple IPv6 default routes to a particular
6918 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6919 	 * default route is of interest:
6920 	 *
6921 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6922 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6923 	 */
6924 
6925 	/* RTF_GATEWAY not set */
6926 	if (!(flags & RTF_GATEWAY)) {
6927 		queue_t	*stq;
6928 
6929 		if (sp != NULL) {
6930 			ip2dbg(("ip_rt_add: gateway security attributes "
6931 			    "cannot be set with interface route\n"));
6932 			if (ipif_refheld)
6933 				ipif_refrele(ipif);
6934 			return (EINVAL);
6935 		}
6936 
6937 		/*
6938 		 * As the interface index specified with the RTA_IFP sockaddr is
6939 		 * the same for all ipif's off of an ill, the matching logic
6940 		 * below uses MATCH_IRE_ILL if such an index was specified.
6941 		 * This means that routes sharing the same prefix when added
6942 		 * using a RTA_IFP sockaddr must have distinct interface
6943 		 * indices (namely, they must be on distinct ill's).
6944 		 *
6945 		 * On the other hand, since the gateway address will usually be
6946 		 * different for each ipif on the system, the matching logic
6947 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6948 		 * route.  This means that interface routes for the same prefix
6949 		 * can be created if they belong to distinct ipif's and if a
6950 		 * RTA_IFP sockaddr is not present.
6951 		 */
6952 		if (ipif_arg != NULL) {
6953 			if (ipif_refheld)  {
6954 				ipif_refrele(ipif);
6955 				ipif_refheld = B_FALSE;
6956 			}
6957 			ipif = ipif_arg;
6958 			match_flags |= MATCH_IRE_ILL;
6959 		} else {
6960 			/*
6961 			 * Check the ipif corresponding to the gw_addr
6962 			 */
6963 			if (ipif == NULL)
6964 				return (ENETUNREACH);
6965 			match_flags |= MATCH_IRE_IPIF;
6966 		}
6967 		ASSERT(ipif != NULL);
6968 
6969 		/*
6970 		 * We check for an existing entry at this point.
6971 		 *
6972 		 * Since a netmask isn't passed in via the ioctl interface
6973 		 * (SIOCADDRT), we don't check for a matching netmask in that
6974 		 * case.
6975 		 */
6976 		if (!ioctl_msg)
6977 			match_flags |= MATCH_IRE_MASK;
6978 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6979 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6980 		if (ire != NULL) {
6981 			ire_refrele(ire);
6982 			if (ipif_refheld)
6983 				ipif_refrele(ipif);
6984 			return (EEXIST);
6985 		}
6986 
6987 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6988 		    ? ipif->ipif_rq : ipif->ipif_wq;
6989 
6990 		/*
6991 		 * Create a copy of the IRE_LOOPBACK,
6992 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6993 		 * the modified address and netmask.
6994 		 */
6995 		ire = ire_create(
6996 		    (uchar_t *)&dst_addr,
6997 		    (uint8_t *)&mask,
6998 		    (uint8_t *)&ipif->ipif_src_addr,
6999 		    NULL,
7000 		    &ipif->ipif_mtu,
7001 		    NULL,
7002 		    NULL,
7003 		    stq,
7004 		    ipif->ipif_net_type,
7005 		    ipif,
7006 		    0,
7007 		    0,
7008 		    0,
7009 		    flags,
7010 		    &ire_uinfo_null,
7011 		    NULL,
7012 		    NULL,
7013 		    ipst);
7014 		if (ire == NULL) {
7015 			if (ipif_refheld)
7016 				ipif_refrele(ipif);
7017 			return (ENOMEM);
7018 		}
7019 
7020 		/*
7021 		 * Some software (for example, GateD and Sun Cluster) attempts
7022 		 * to create (what amount to) IRE_PREFIX routes with the
7023 		 * loopback address as the gateway.  This is primarily done to
7024 		 * set up prefixes with the RTF_REJECT flag set (for example,
7025 		 * when generating aggregate routes.)
7026 		 *
7027 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7028 		 * IRE_LOOPBACK, then we map the request into a
7029 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7030 		 * these interface routes, by definition, can only be that.
7031 		 *
7032 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7033 		 * routine, but rather using ire_create() directly.
7034 		 *
7035 		 */
7036 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7037 			ire->ire_type = IRE_IF_NORESOLVER;
7038 			ire->ire_flags |= RTF_BLACKHOLE;
7039 		}
7040 
7041 		error = ire_add(&ire, q, mp, func, B_FALSE);
7042 		if (error == 0)
7043 			goto save_ire;
7044 
7045 		/*
7046 		 * In the result of failure, ire_add() will have already
7047 		 * deleted the ire in question, so there is no need to
7048 		 * do that here.
7049 		 */
7050 		if (ipif_refheld)
7051 			ipif_refrele(ipif);
7052 		return (error);
7053 	}
7054 	if (ipif_refheld) {
7055 		ipif_refrele(ipif);
7056 		ipif_refheld = B_FALSE;
7057 	}
7058 
7059 	/*
7060 	 * Get an interface IRE for the specified gateway.
7061 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7062 	 * gateway, it is currently unreachable and we fail the request
7063 	 * accordingly.
7064 	 */
7065 	ipif = ipif_arg;
7066 	if (ipif_arg != NULL)
7067 		match_flags |= MATCH_IRE_ILL;
7068 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7069 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7070 	if (gw_ire == NULL)
7071 		return (ENETUNREACH);
7072 
7073 	/*
7074 	 * We create one of three types of IREs as a result of this request
7075 	 * based on the netmask.  A netmask of all ones (which is automatically
7076 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7077 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7078 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7079 	 * destination prefix.
7080 	 */
7081 	if (mask == IP_HOST_MASK)
7082 		type = IRE_HOST;
7083 	else if (mask == 0)
7084 		type = IRE_DEFAULT;
7085 	else
7086 		type = IRE_PREFIX;
7087 
7088 	/* check for a duplicate entry */
7089 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7090 	    NULL, ALL_ZONES, 0, NULL,
7091 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7092 	if (ire != NULL) {
7093 		ire_refrele(gw_ire);
7094 		ire_refrele(ire);
7095 		return (EEXIST);
7096 	}
7097 
7098 	/* Security attribute exists */
7099 	if (sp != NULL) {
7100 		tsol_gcgrp_addr_t ga;
7101 
7102 		/* find or create the gateway credentials group */
7103 		ga.ga_af = AF_INET;
7104 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7105 
7106 		/* we hold reference to it upon success */
7107 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7108 		if (gcgrp == NULL) {
7109 			ire_refrele(gw_ire);
7110 			return (ENOMEM);
7111 		}
7112 
7113 		/*
7114 		 * Create and add the security attribute to the group; a
7115 		 * reference to the group is made upon allocating a new
7116 		 * entry successfully.  If it finds an already-existing
7117 		 * entry for the security attribute in the group, it simply
7118 		 * returns it and no new reference is made to the group.
7119 		 */
7120 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7121 		if (gc == NULL) {
7122 			/* release reference held by gcgrp_lookup */
7123 			GCGRP_REFRELE(gcgrp);
7124 			ire_refrele(gw_ire);
7125 			return (ENOMEM);
7126 		}
7127 	}
7128 
7129 	/* Create the IRE. */
7130 	ire = ire_create(
7131 	    (uchar_t *)&dst_addr,		/* dest address */
7132 	    (uchar_t *)&mask,			/* mask */
7133 	    /* src address assigned by the caller? */
7134 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7135 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7136 	    (uchar_t *)&gw_addr,		/* gateway address */
7137 	    &gw_ire->ire_max_frag,
7138 	    NULL,				/* no src nce */
7139 	    NULL,				/* no recv-from queue */
7140 	    NULL,				/* no send-to queue */
7141 	    (ushort_t)type,			/* IRE type */
7142 	    ipif_arg,
7143 	    0,
7144 	    0,
7145 	    0,
7146 	    flags,
7147 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7148 	    gc,					/* security attribute */
7149 	    NULL,
7150 	    ipst);
7151 
7152 	/*
7153 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7154 	 * reference to the 'gcgrp'. We can now release the extra reference
7155 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7156 	 */
7157 	if (gcgrp_xtraref)
7158 		GCGRP_REFRELE(gcgrp);
7159 	if (ire == NULL) {
7160 		if (gc != NULL)
7161 			GC_REFRELE(gc);
7162 		ire_refrele(gw_ire);
7163 		return (ENOMEM);
7164 	}
7165 
7166 	/*
7167 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7168 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7169 	 */
7170 
7171 	/* Add the new IRE. */
7172 	error = ire_add(&ire, q, mp, func, B_FALSE);
7173 	if (error != 0) {
7174 		/*
7175 		 * In the result of failure, ire_add() will have already
7176 		 * deleted the ire in question, so there is no need to
7177 		 * do that here.
7178 		 */
7179 		ire_refrele(gw_ire);
7180 		return (error);
7181 	}
7182 
7183 	if (flags & RTF_MULTIRT) {
7184 		/*
7185 		 * Invoke the CGTP (multirouting) filtering module
7186 		 * to add the dst address in the filtering database.
7187 		 * Replicated inbound packets coming from that address
7188 		 * will be filtered to discard the duplicates.
7189 		 * It is not necessary to call the CGTP filter hook
7190 		 * when the dst address is a broadcast or multicast,
7191 		 * because an IP source address cannot be a broadcast
7192 		 * or a multicast.
7193 		 */
7194 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7195 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7196 		if (ire_dst != NULL) {
7197 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7198 			ire_refrele(ire_dst);
7199 			goto save_ire;
7200 		}
7201 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7202 		    !CLASSD(ire->ire_addr)) {
7203 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7204 			    ipst->ips_netstack->netstack_stackid,
7205 			    ire->ire_addr,
7206 			    ire->ire_gateway_addr,
7207 			    ire->ire_src_addr,
7208 			    gw_ire->ire_src_addr);
7209 			if (res != 0) {
7210 				ire_refrele(gw_ire);
7211 				ire_delete(ire);
7212 				return (res);
7213 			}
7214 		}
7215 	}
7216 
7217 	/*
7218 	 * Now that the prefix IRE entry has been created, delete any
7219 	 * existing gateway IRE cache entries as well as any IRE caches
7220 	 * using the gateway, and force them to be created through
7221 	 * ip_newroute.
7222 	 */
7223 	if (gc != NULL) {
7224 		ASSERT(gcgrp != NULL);
7225 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7226 	}
7227 
7228 save_ire:
7229 	if (gw_ire != NULL) {
7230 		ire_refrele(gw_ire);
7231 	}
7232 	if (ipif != NULL) {
7233 		/*
7234 		 * Save enough information so that we can recreate the IRE if
7235 		 * the interface goes down and then up.  The metrics associated
7236 		 * with the route will be saved as well when rts_setmetrics() is
7237 		 * called after the IRE has been created.  In the case where
7238 		 * memory cannot be allocated, none of this information will be
7239 		 * saved.
7240 		 */
7241 		ipif_save_ire(ipif, ire);
7242 	}
7243 	if (ioctl_msg)
7244 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7245 	if (ire_arg != NULL) {
7246 		/*
7247 		 * Store the ire that was successfully added into where ire_arg
7248 		 * points to so that callers don't have to look it up
7249 		 * themselves (but they are responsible for ire_refrele()ing
7250 		 * the ire when they are finished with it).
7251 		 */
7252 		*ire_arg = ire;
7253 	} else {
7254 		ire_refrele(ire);		/* Held in ire_add */
7255 	}
7256 	if (ipif_refheld)
7257 		ipif_refrele(ipif);
7258 	return (0);
7259 }
7260 
7261 /*
7262  * ip_rt_delete is called to delete an IPv4 route.
7263  * ipif_arg is passed in to associate it with the correct interface.
7264  * We may need to restart this operation if the ipif cannot be looked up
7265  * due to an exclusive operation that is currently in progress. The restart
7266  * entry point is specified by 'func'
7267  */
7268 /* ARGSUSED4 */
7269 int
7270 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7271     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7272     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7273 {
7274 	ire_t	*ire = NULL;
7275 	ipif_t	*ipif;
7276 	boolean_t ipif_refheld = B_FALSE;
7277 	uint_t	type;
7278 	uint_t	match_flags = MATCH_IRE_TYPE;
7279 	int	err = 0;
7280 
7281 	ip1dbg(("ip_rt_delete:"));
7282 	/*
7283 	 * If this is the case of RTF_HOST being set, then we set the netmask
7284 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7285 	 */
7286 	if (flags & RTF_HOST) {
7287 		mask = IP_HOST_MASK;
7288 		match_flags |= MATCH_IRE_MASK;
7289 	} else if (rtm_addrs & RTA_NETMASK) {
7290 		match_flags |= MATCH_IRE_MASK;
7291 	}
7292 
7293 	/*
7294 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7295 	 * we check if the gateway address is one of our interfaces first,
7296 	 * and fall back on RTF_GATEWAY routes.
7297 	 *
7298 	 * This makes it possible to delete an original
7299 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7300 	 *
7301 	 * As the interface index specified with the RTA_IFP sockaddr is the
7302 	 * same for all ipif's off of an ill, the matching logic below uses
7303 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7304 	 * sharing the same prefix and interface index as the the route
7305 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7306 	 * is specified in the request.
7307 	 *
7308 	 * On the other hand, since the gateway address will usually be
7309 	 * different for each ipif on the system, the matching logic
7310 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7311 	 * route.  This means that interface routes for the same prefix can be
7312 	 * uniquely identified if they belong to distinct ipif's and if a
7313 	 * RTA_IFP sockaddr is not present.
7314 	 *
7315 	 * For more detail on specifying routes by gateway address and by
7316 	 * interface index, see the comments in ip_rt_add().
7317 	 */
7318 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7319 	    ipst);
7320 	if (ipif != NULL)
7321 		ipif_refheld = B_TRUE;
7322 	else if (err == EINPROGRESS)
7323 		return (err);
7324 	else
7325 		err = 0;
7326 	if (ipif != NULL) {
7327 		if (ipif_arg != NULL) {
7328 			if (ipif_refheld) {
7329 				ipif_refrele(ipif);
7330 				ipif_refheld = B_FALSE;
7331 			}
7332 			ipif = ipif_arg;
7333 			match_flags |= MATCH_IRE_ILL;
7334 		} else {
7335 			match_flags |= MATCH_IRE_IPIF;
7336 		}
7337 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7338 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7339 			    ALL_ZONES, NULL, match_flags, ipst);
7340 		}
7341 		if (ire == NULL) {
7342 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7343 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7344 			    match_flags, ipst);
7345 		}
7346 	}
7347 
7348 	if (ire == NULL) {
7349 		/*
7350 		 * At this point, the gateway address is not one of our own
7351 		 * addresses or a matching interface route was not found.  We
7352 		 * set the IRE type to lookup based on whether
7353 		 * this is a host route, a default route or just a prefix.
7354 		 *
7355 		 * If an ipif_arg was passed in, then the lookup is based on an
7356 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7357 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7358 		 * set as the route being looked up is not a traditional
7359 		 * interface route.
7360 		 */
7361 		match_flags &= ~MATCH_IRE_IPIF;
7362 		match_flags |= MATCH_IRE_GW;
7363 		if (ipif_arg != NULL)
7364 			match_flags |= MATCH_IRE_ILL;
7365 		if (mask == IP_HOST_MASK)
7366 			type = IRE_HOST;
7367 		else if (mask == 0)
7368 			type = IRE_DEFAULT;
7369 		else
7370 			type = IRE_PREFIX;
7371 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7372 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7373 	}
7374 
7375 	if (ipif_refheld)
7376 		ipif_refrele(ipif);
7377 
7378 	/* ipif is not refheld anymore */
7379 	if (ire == NULL)
7380 		return (ESRCH);
7381 
7382 	if (ire->ire_flags & RTF_MULTIRT) {
7383 		/*
7384 		 * Invoke the CGTP (multirouting) filtering module
7385 		 * to remove the dst address from the filtering database.
7386 		 * Packets coming from that address will no longer be
7387 		 * filtered to remove duplicates.
7388 		 */
7389 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7390 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7391 			    ipst->ips_netstack->netstack_stackid,
7392 			    ire->ire_addr, ire->ire_gateway_addr);
7393 		}
7394 		ip_cgtp_bcast_delete(ire, ipst);
7395 	}
7396 
7397 	ipif = ire->ire_ipif;
7398 	if (ipif != NULL)
7399 		ipif_remove_ire(ipif, ire);
7400 	if (ioctl_msg)
7401 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7402 	ire_delete(ire);
7403 	ire_refrele(ire);
7404 	return (err);
7405 }
7406 
7407 /*
7408  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7409  */
7410 /* ARGSUSED */
7411 int
7412 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7413     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7414 {
7415 	ipaddr_t dst_addr;
7416 	ipaddr_t gw_addr;
7417 	ipaddr_t mask;
7418 	int error = 0;
7419 	mblk_t *mp1;
7420 	struct rtentry *rt;
7421 	ipif_t *ipif = NULL;
7422 	ip_stack_t	*ipst;
7423 
7424 	ASSERT(q->q_next == NULL);
7425 	ipst = CONNQ_TO_IPST(q);
7426 
7427 	ip1dbg(("ip_siocaddrt:"));
7428 	/* Existence of mp1 verified in ip_wput_nondata */
7429 	mp1 = mp->b_cont->b_cont;
7430 	rt = (struct rtentry *)mp1->b_rptr;
7431 
7432 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7433 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7434 
7435 	/*
7436 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7437 	 * to a particular host address.  In this case, we set the netmask to
7438 	 * all ones for the particular destination address.  Otherwise,
7439 	 * determine the netmask to be used based on dst_addr and the interfaces
7440 	 * in use.
7441 	 */
7442 	if (rt->rt_flags & RTF_HOST) {
7443 		mask = IP_HOST_MASK;
7444 	} else {
7445 		/*
7446 		 * Note that ip_subnet_mask returns a zero mask in the case of
7447 		 * default (an all-zeroes address).
7448 		 */
7449 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7450 	}
7451 
7452 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7453 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7454 	if (ipif != NULL)
7455 		ipif_refrele(ipif);
7456 	return (error);
7457 }
7458 
7459 /*
7460  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7461  */
7462 /* ARGSUSED */
7463 int
7464 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7465     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7466 {
7467 	ipaddr_t dst_addr;
7468 	ipaddr_t gw_addr;
7469 	ipaddr_t mask;
7470 	int error;
7471 	mblk_t *mp1;
7472 	struct rtentry *rt;
7473 	ipif_t *ipif = NULL;
7474 	ip_stack_t	*ipst;
7475 
7476 	ASSERT(q->q_next == NULL);
7477 	ipst = CONNQ_TO_IPST(q);
7478 
7479 	ip1dbg(("ip_siocdelrt:"));
7480 	/* Existence of mp1 verified in ip_wput_nondata */
7481 	mp1 = mp->b_cont->b_cont;
7482 	rt = (struct rtentry *)mp1->b_rptr;
7483 
7484 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7485 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7486 
7487 	/*
7488 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7489 	 * to a particular host address.  In this case, we set the netmask to
7490 	 * all ones for the particular destination address.  Otherwise,
7491 	 * determine the netmask to be used based on dst_addr and the interfaces
7492 	 * in use.
7493 	 */
7494 	if (rt->rt_flags & RTF_HOST) {
7495 		mask = IP_HOST_MASK;
7496 	} else {
7497 		/*
7498 		 * Note that ip_subnet_mask returns a zero mask in the case of
7499 		 * default (an all-zeroes address).
7500 		 */
7501 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7502 	}
7503 
7504 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7505 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7506 	    mp, ip_process_ioctl, ipst);
7507 	if (ipif != NULL)
7508 		ipif_refrele(ipif);
7509 	return (error);
7510 }
7511 
7512 /*
7513  * Enqueue the mp onto the ipsq, chained by b_next.
7514  * b_prev stores the function to be executed later, and b_queue the queue
7515  * where this mp originated.
7516  */
7517 void
7518 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7519     ill_t *pending_ill)
7520 {
7521 	conn_t	*connp = NULL;
7522 
7523 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7524 	ASSERT(func != NULL);
7525 
7526 	mp->b_queue = q;
7527 	mp->b_prev = (void *)func;
7528 	mp->b_next = NULL;
7529 
7530 	switch (type) {
7531 	case CUR_OP:
7532 		if (ipsq->ipsq_mptail != NULL) {
7533 			ASSERT(ipsq->ipsq_mphead != NULL);
7534 			ipsq->ipsq_mptail->b_next = mp;
7535 		} else {
7536 			ASSERT(ipsq->ipsq_mphead == NULL);
7537 			ipsq->ipsq_mphead = mp;
7538 		}
7539 		ipsq->ipsq_mptail = mp;
7540 		break;
7541 
7542 	case NEW_OP:
7543 		if (ipsq->ipsq_xopq_mptail != NULL) {
7544 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7545 			ipsq->ipsq_xopq_mptail->b_next = mp;
7546 		} else {
7547 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7548 			ipsq->ipsq_xopq_mphead = mp;
7549 		}
7550 		ipsq->ipsq_xopq_mptail = mp;
7551 		break;
7552 	default:
7553 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7554 	}
7555 
7556 	if (CONN_Q(q) && pending_ill != NULL) {
7557 		connp = Q_TO_CONN(q);
7558 
7559 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7560 		connp->conn_oper_pending_ill = pending_ill;
7561 	}
7562 }
7563 
7564 /*
7565  * Return the mp at the head of the ipsq. After emptying the ipsq
7566  * look at the next ioctl, if this ioctl is complete. Otherwise
7567  * return, we will resume when we complete the current ioctl.
7568  * The current ioctl will wait till it gets a response from the
7569  * driver below.
7570  */
7571 static mblk_t *
7572 ipsq_dq(ipsq_t *ipsq)
7573 {
7574 	mblk_t	*mp;
7575 
7576 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7577 
7578 	mp = ipsq->ipsq_mphead;
7579 	if (mp != NULL) {
7580 		ipsq->ipsq_mphead = mp->b_next;
7581 		if (ipsq->ipsq_mphead == NULL)
7582 			ipsq->ipsq_mptail = NULL;
7583 		mp->b_next = NULL;
7584 		return (mp);
7585 	}
7586 	if (ipsq->ipsq_current_ipif != NULL)
7587 		return (NULL);
7588 	mp = ipsq->ipsq_xopq_mphead;
7589 	if (mp != NULL) {
7590 		ipsq->ipsq_xopq_mphead = mp->b_next;
7591 		if (ipsq->ipsq_xopq_mphead == NULL)
7592 			ipsq->ipsq_xopq_mptail = NULL;
7593 		mp->b_next = NULL;
7594 		return (mp);
7595 	}
7596 	return (NULL);
7597 }
7598 
7599 /*
7600  * Enter the ipsq corresponding to ill, by waiting synchronously till
7601  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7602  * will have to drain completely before ipsq_enter returns success.
7603  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7604  * and the ipsq_exit logic will start the next enqueued ioctl after
7605  * completion of the current ioctl. If 'force' is used, we don't wait
7606  * for the enqueued ioctls. This is needed when a conn_close wants to
7607  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7608  * of an ill can also use this option. But we dont' use it currently.
7609  */
7610 #define	ENTER_SQ_WAIT_TICKS 100
7611 boolean_t
7612 ipsq_enter(ill_t *ill, boolean_t force)
7613 {
7614 	ipsq_t	*ipsq;
7615 	boolean_t waited_enough = B_FALSE;
7616 
7617 	/*
7618 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7619 	 * Since the <ill-ipsq> assocs could change while we wait for the
7620 	 * writer, it is easier to wait on a fixed global rather than try to
7621 	 * cv_wait on a changing ipsq.
7622 	 */
7623 	mutex_enter(&ill->ill_lock);
7624 	for (;;) {
7625 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7626 			mutex_exit(&ill->ill_lock);
7627 			return (B_FALSE);
7628 		}
7629 
7630 		ipsq = ill->ill_phyint->phyint_ipsq;
7631 		mutex_enter(&ipsq->ipsq_lock);
7632 		if (ipsq->ipsq_writer == NULL &&
7633 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7634 			break;
7635 		} else if (ipsq->ipsq_writer != NULL) {
7636 			mutex_exit(&ipsq->ipsq_lock);
7637 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7638 		} else {
7639 			mutex_exit(&ipsq->ipsq_lock);
7640 			if (force) {
7641 				(void) cv_timedwait(&ill->ill_cv,
7642 				    &ill->ill_lock,
7643 				    lbolt + ENTER_SQ_WAIT_TICKS);
7644 				waited_enough = B_TRUE;
7645 				continue;
7646 			} else {
7647 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7648 			}
7649 		}
7650 	}
7651 
7652 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7653 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7654 	ipsq->ipsq_writer = curthread;
7655 	ipsq->ipsq_reentry_cnt++;
7656 #ifdef DEBUG
7657 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7658 #endif
7659 	mutex_exit(&ipsq->ipsq_lock);
7660 	mutex_exit(&ill->ill_lock);
7661 	return (B_TRUE);
7662 }
7663 
7664 /*
7665  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7666  * certain critical operations like plumbing (i.e. most set ioctls),
7667  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7668  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7669  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7670  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7671  * threads executing in the ipsq. Responses from the driver pertain to the
7672  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7673  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7674  *
7675  * If a thread does not want to reenter the ipsq when it is already writer,
7676  * it must make sure that the specified reentry point to be called later
7677  * when the ipsq is empty, nor any code path starting from the specified reentry
7678  * point must never ever try to enter the ipsq again. Otherwise it can lead
7679  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7680  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7681  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7682  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7683  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7684  * ioctl if the current ioctl has completed. If the current ioctl is still
7685  * in progress it simply returns. The current ioctl could be waiting for
7686  * a response from another module (arp_ or the driver or could be waiting for
7687  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7688  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7689  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7690  * ipsq_current_ipif is clear which happens only on ioctl completion.
7691  */
7692 
7693 /*
7694  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7695  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7696  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7697  * completion.
7698  */
7699 ipsq_t *
7700 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7701     ipsq_func_t func, int type, boolean_t reentry_ok)
7702 {
7703 	ipsq_t	*ipsq;
7704 
7705 	/* Only 1 of ipif or ill can be specified */
7706 	ASSERT((ipif != NULL) ^ (ill != NULL));
7707 	if (ipif != NULL)
7708 		ill = ipif->ipif_ill;
7709 
7710 	/*
7711 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7712 	 * ipsq of an ill can't change when ill_lock is held.
7713 	 */
7714 	GRAB_CONN_LOCK(q);
7715 	mutex_enter(&ill->ill_lock);
7716 	ipsq = ill->ill_phyint->phyint_ipsq;
7717 	mutex_enter(&ipsq->ipsq_lock);
7718 
7719 	/*
7720 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7721 	 *    (Note: If the caller does not specify reentry_ok then neither
7722 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7723 	 *    again. Otherwise it can lead to an infinite loop
7724 	 * 2. Enter the ipsq if there is no current writer and this attempted
7725 	 *    entry is part of the current ioctl or operation
7726 	 * 3. Enter the ipsq if there is no current writer and this is a new
7727 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7728 	 *    empty and there is no ioctl (or operation) currently in progress
7729 	 */
7730 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7731 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7732 	    ipsq->ipsq_current_ipif == NULL))) ||
7733 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7734 		/* Success. */
7735 		ipsq->ipsq_reentry_cnt++;
7736 		ipsq->ipsq_writer = curthread;
7737 		mutex_exit(&ipsq->ipsq_lock);
7738 		mutex_exit(&ill->ill_lock);
7739 		RELEASE_CONN_LOCK(q);
7740 #ifdef DEBUG
7741 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7742 		    IPSQ_STACK_DEPTH);
7743 #endif
7744 		return (ipsq);
7745 	}
7746 
7747 	ipsq_enq(ipsq, q, mp, func, type, ill);
7748 
7749 	mutex_exit(&ipsq->ipsq_lock);
7750 	mutex_exit(&ill->ill_lock);
7751 	RELEASE_CONN_LOCK(q);
7752 	return (NULL);
7753 }
7754 
7755 /*
7756  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7757  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7758  * cannot be entered, the mp is queued for completion.
7759  */
7760 void
7761 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7762     boolean_t reentry_ok)
7763 {
7764 	ipsq_t	*ipsq;
7765 
7766 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7767 
7768 	/*
7769 	 * Drop the caller's refhold on the ill.  This is safe since we either
7770 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7771 	 * IPSQ, in which case we return without accessing ill anymore.  This
7772 	 * is needed because func needs to see the correct refcount.
7773 	 * e.g. removeif can work only then.
7774 	 */
7775 	ill_refrele(ill);
7776 	if (ipsq != NULL) {
7777 		(*func)(ipsq, q, mp, NULL);
7778 		ipsq_exit(ipsq);
7779 	}
7780 }
7781 
7782 /*
7783  * If there are more than ILL_GRP_CNT ills in a group,
7784  * we use kmem alloc'd buffers, else use the stack
7785  */
7786 #define	ILL_GRP_CNT	14
7787 /*
7788  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7789  * Called by a thread that is currently exclusive on this ipsq.
7790  */
7791 void
7792 ipsq_exit(ipsq_t *ipsq)
7793 {
7794 	queue_t	*q;
7795 	mblk_t	*mp;
7796 	ipsq_func_t	func;
7797 	int	next;
7798 	ill_t	**ill_list = NULL;
7799 	size_t	ill_list_size = 0;
7800 	int	cnt = 0;
7801 	boolean_t need_ipsq_free = B_FALSE;
7802 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7803 
7804 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7805 	mutex_enter(&ipsq->ipsq_lock);
7806 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7807 	if (ipsq->ipsq_reentry_cnt != 1) {
7808 		ipsq->ipsq_reentry_cnt--;
7809 		mutex_exit(&ipsq->ipsq_lock);
7810 		return;
7811 	}
7812 
7813 	mp = ipsq_dq(ipsq);
7814 	while (mp != NULL) {
7815 again:
7816 		mutex_exit(&ipsq->ipsq_lock);
7817 		func = (ipsq_func_t)mp->b_prev;
7818 		q = (queue_t *)mp->b_queue;
7819 		mp->b_prev = NULL;
7820 		mp->b_queue = NULL;
7821 
7822 		/*
7823 		 * If 'q' is an conn queue, it is valid, since we did a
7824 		 * a refhold on the connp, at the start of the ioctl.
7825 		 * If 'q' is an ill queue, it is valid, since close of an
7826 		 * ill will clean up the 'ipsq'.
7827 		 */
7828 		(*func)(ipsq, q, mp, NULL);
7829 
7830 		mutex_enter(&ipsq->ipsq_lock);
7831 		mp = ipsq_dq(ipsq);
7832 	}
7833 
7834 	mutex_exit(&ipsq->ipsq_lock);
7835 
7836 	/*
7837 	 * Need to grab the locks in the right order. Need to
7838 	 * atomically check (under ipsq_lock) that there are no
7839 	 * messages before relinquishing the ipsq. Also need to
7840 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7841 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7842 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7843 	 * to grab ill_g_lock as writer.
7844 	 */
7845 	rw_enter(&ipst->ips_ill_g_lock,
7846 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7847 
7848 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7849 	if (ipsq->ipsq_refs != 0) {
7850 		/* At most 2 ills v4/v6 per phyint */
7851 		cnt = ipsq->ipsq_refs << 1;
7852 		ill_list_size = cnt * sizeof (ill_t *);
7853 		/*
7854 		 * If memory allocation fails, we will do the split
7855 		 * the next time ipsq_exit is called for whatever reason.
7856 		 * As long as the ipsq_split flag is set the need to
7857 		 * split is remembered.
7858 		 */
7859 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7860 		if (ill_list != NULL)
7861 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7862 	}
7863 	mutex_enter(&ipsq->ipsq_lock);
7864 	mp = ipsq_dq(ipsq);
7865 	if (mp != NULL) {
7866 		/* oops, some message has landed up, we can't get out */
7867 		if (ill_list != NULL)
7868 			ill_unlock_ills(ill_list, cnt);
7869 		rw_exit(&ipst->ips_ill_g_lock);
7870 		if (ill_list != NULL)
7871 			kmem_free(ill_list, ill_list_size);
7872 		ill_list = NULL;
7873 		ill_list_size = 0;
7874 		cnt = 0;
7875 		goto again;
7876 	}
7877 
7878 	/*
7879 	 * Split only if no ioctl is pending and if memory alloc succeeded
7880 	 * above.
7881 	 */
7882 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7883 	    ill_list != NULL) {
7884 		/*
7885 		 * No new ill can join this ipsq since we are holding the
7886 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7887 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7888 		 * If so we will retry on the next ipsq_exit.
7889 		 */
7890 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7891 	}
7892 
7893 	/*
7894 	 * We are holding the ipsq lock, hence no new messages can
7895 	 * land up on the ipsq, and there are no messages currently.
7896 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7897 	 * atomically while holding ill locks.
7898 	 */
7899 	ipsq->ipsq_writer = NULL;
7900 	ipsq->ipsq_reentry_cnt--;
7901 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7902 #ifdef DEBUG
7903 	ipsq->ipsq_depth = 0;
7904 #endif
7905 	mutex_exit(&ipsq->ipsq_lock);
7906 	/*
7907 	 * For IPMP this should wake up all ills in this ipsq.
7908 	 * We need to hold the ill_lock while waking up waiters to
7909 	 * avoid missed wakeups. But there is no need to acquire all
7910 	 * the ill locks and then wakeup. If we have not acquired all
7911 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7912 	 * wakes up ills one at a time after getting the right ill_lock
7913 	 */
7914 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7915 	if (ill_list != NULL)
7916 		ill_unlock_ills(ill_list, cnt);
7917 	if (ipsq->ipsq_refs == 0)
7918 		need_ipsq_free = B_TRUE;
7919 	rw_exit(&ipst->ips_ill_g_lock);
7920 	if (ill_list != 0)
7921 		kmem_free(ill_list, ill_list_size);
7922 
7923 	if (need_ipsq_free) {
7924 		/*
7925 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7926 		 * looked up. ipsq can be looked up only thru ill or phyint
7927 		 * and there are no ills/phyint on this ipsq.
7928 		 */
7929 		ipsq_delete(ipsq);
7930 	}
7931 
7932 	/*
7933 	 * Now that we're outside the IPSQ, start any IGMP/MLD timers.  We
7934 	 * can't start these inside the IPSQ since e.g. igmp_start_timers() ->
7935 	 * untimeout() (inside the IPSQ, waiting for an executing timeout to
7936 	 * finish) could deadlock with igmp_timeout_handler() -> ipsq_enter()
7937 	 * (executing the timeout, waiting to get inside the IPSQ).
7938 	 *
7939 	 * However, there is one exception to the above: if this thread *is*
7940 	 * the IGMP/MLD timeout handler thread, then we must not start its
7941 	 * timer until the current handler is done.
7942 	 */
7943 	mutex_enter(&ipst->ips_igmp_timer_lock);
7944 	if (curthread != ipst->ips_igmp_timer_thread) {
7945 		next = ipst->ips_igmp_deferred_next;
7946 		ipst->ips_igmp_deferred_next = INFINITY;
7947 		mutex_exit(&ipst->ips_igmp_timer_lock);
7948 
7949 		if (next != INFINITY)
7950 			igmp_start_timers(next, ipst);
7951 	} else {
7952 		mutex_exit(&ipst->ips_igmp_timer_lock);
7953 	}
7954 
7955 	mutex_enter(&ipst->ips_mld_timer_lock);
7956 	if (curthread != ipst->ips_mld_timer_thread) {
7957 		next = ipst->ips_mld_deferred_next;
7958 		ipst->ips_mld_deferred_next = INFINITY;
7959 		mutex_exit(&ipst->ips_mld_timer_lock);
7960 
7961 		if (next != INFINITY)
7962 			mld_start_timers(next, ipst);
7963 	} else {
7964 		mutex_exit(&ipst->ips_mld_timer_lock);
7965 	}
7966 }
7967 
7968 /*
7969  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7970  * and `ioccmd'.
7971  */
7972 void
7973 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7974 {
7975 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7976 
7977 	mutex_enter(&ipsq->ipsq_lock);
7978 	ASSERT(ipsq->ipsq_current_ipif == NULL);
7979 	ASSERT(ipsq->ipsq_current_ioctl == 0);
7980 	ipsq->ipsq_current_done = B_FALSE;
7981 	ipsq->ipsq_current_ipif = ipif;
7982 	ipsq->ipsq_current_ioctl = ioccmd;
7983 	mutex_exit(&ipsq->ipsq_lock);
7984 }
7985 
7986 /*
7987  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
7988  * the next exclusive operation to begin once we ipsq_exit().  However, if
7989  * pending DLPI operations remain, then we will wait for the queue to drain
7990  * before allowing the next exclusive operation to begin.  This ensures that
7991  * DLPI operations from one exclusive operation are never improperly processed
7992  * as part of a subsequent exclusive operation.
7993  */
7994 void
7995 ipsq_current_finish(ipsq_t *ipsq)
7996 {
7997 	ipif_t *ipif = ipsq->ipsq_current_ipif;
7998 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
7999 
8000 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8001 
8002 	/*
8003 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8004 	 * (but in that case, IPIF_CHANGING will already be clear and no
8005 	 * pending DLPI messages can remain).
8006 	 */
8007 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8008 		ill_t *ill = ipif->ipif_ill;
8009 
8010 		mutex_enter(&ill->ill_lock);
8011 		dlpi_pending = ill->ill_dlpi_pending;
8012 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8013 		mutex_exit(&ill->ill_lock);
8014 	}
8015 
8016 	mutex_enter(&ipsq->ipsq_lock);
8017 	ipsq->ipsq_current_ioctl = 0;
8018 	ipsq->ipsq_current_done = B_TRUE;
8019 	if (dlpi_pending == DL_PRIM_INVAL)
8020 		ipsq->ipsq_current_ipif = NULL;
8021 	mutex_exit(&ipsq->ipsq_lock);
8022 }
8023 
8024 /*
8025  * The ill is closing. Flush all messages on the ipsq that originated
8026  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8027  * for this ill since ipsq_enter could not have entered until then.
8028  * New messages can't be queued since the CONDEMNED flag is set.
8029  */
8030 static void
8031 ipsq_flush(ill_t *ill)
8032 {
8033 	queue_t	*q;
8034 	mblk_t	*prev;
8035 	mblk_t	*mp;
8036 	mblk_t	*mp_next;
8037 	ipsq_t	*ipsq;
8038 
8039 	ASSERT(IAM_WRITER_ILL(ill));
8040 	ipsq = ill->ill_phyint->phyint_ipsq;
8041 	/*
8042 	 * Flush any messages sent up by the driver.
8043 	 */
8044 	mutex_enter(&ipsq->ipsq_lock);
8045 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8046 		mp_next = mp->b_next;
8047 		q = mp->b_queue;
8048 		if (q == ill->ill_rq || q == ill->ill_wq) {
8049 			/* Remove the mp from the ipsq */
8050 			if (prev == NULL)
8051 				ipsq->ipsq_mphead = mp->b_next;
8052 			else
8053 				prev->b_next = mp->b_next;
8054 			if (ipsq->ipsq_mptail == mp) {
8055 				ASSERT(mp_next == NULL);
8056 				ipsq->ipsq_mptail = prev;
8057 			}
8058 			inet_freemsg(mp);
8059 		} else {
8060 			prev = mp;
8061 		}
8062 	}
8063 	mutex_exit(&ipsq->ipsq_lock);
8064 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8065 	ipsq_xopq_mp_cleanup(ill, NULL);
8066 	ill_pending_mp_cleanup(ill);
8067 }
8068 
8069 /* ARGSUSED */
8070 int
8071 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8072     ip_ioctl_cmd_t *ipip, void *ifreq)
8073 {
8074 	ill_t	*ill;
8075 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8076 	boolean_t isv6;
8077 	conn_t	*connp;
8078 	ip_stack_t	*ipst;
8079 
8080 	connp = Q_TO_CONN(q);
8081 	ipst = connp->conn_netstack->netstack_ip;
8082 	isv6 = connp->conn_af_isv6;
8083 	/*
8084 	 * Set original index.
8085 	 * Failover and failback move logical interfaces
8086 	 * from one physical interface to another.  The
8087 	 * original index indicates the parent of a logical
8088 	 * interface, in other words, the physical interface
8089 	 * the logical interface will be moved back to on
8090 	 * failback.
8091 	 */
8092 
8093 	/*
8094 	 * Don't allow the original index to be changed
8095 	 * for non-failover addresses, autoconfigured
8096 	 * addresses, or IPv6 link local addresses.
8097 	 */
8098 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8099 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8100 		return (EINVAL);
8101 	}
8102 	/*
8103 	 * The new original index must be in use by some
8104 	 * physical interface.
8105 	 */
8106 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8107 	    NULL, NULL, ipst);
8108 	if (ill == NULL)
8109 		return (ENXIO);
8110 	ill_refrele(ill);
8111 
8112 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8113 	/*
8114 	 * When this ipif gets failed back, don't
8115 	 * preserve the original id, as it is no
8116 	 * longer applicable.
8117 	 */
8118 	ipif->ipif_orig_ipifid = 0;
8119 	/*
8120 	 * For IPv4, change the original index of any
8121 	 * multicast addresses associated with the
8122 	 * ipif to the new value.
8123 	 */
8124 	if (!isv6) {
8125 		ilm_t *ilm;
8126 
8127 		mutex_enter(&ipif->ipif_ill->ill_lock);
8128 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8129 		    ilm = ilm->ilm_next) {
8130 			if (ilm->ilm_ipif == ipif) {
8131 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8132 			}
8133 		}
8134 		mutex_exit(&ipif->ipif_ill->ill_lock);
8135 	}
8136 	return (0);
8137 }
8138 
8139 /* ARGSUSED */
8140 int
8141 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8142     ip_ioctl_cmd_t *ipip, void *ifreq)
8143 {
8144 	struct lifreq *lifr = (struct lifreq *)ifreq;
8145 
8146 	/*
8147 	 * Get the original interface index i.e the one
8148 	 * before FAILOVER if it ever happened.
8149 	 */
8150 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8151 	return (0);
8152 }
8153 
8154 /*
8155  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8156  * refhold and return the associated ipif
8157  */
8158 /* ARGSUSED */
8159 int
8160 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8161     cmd_info_t *ci, ipsq_func_t func)
8162 {
8163 	boolean_t exists;
8164 	struct iftun_req *ta;
8165 	ipif_t  *ipif;
8166 	ill_t   *ill;
8167 	boolean_t isv6;
8168 	mblk_t  *mp1;
8169 	int error;
8170 	conn_t  *connp;
8171 	ip_stack_t  *ipst;
8172 
8173 	/* Existence verified in ip_wput_nondata */
8174 	mp1 = mp->b_cont->b_cont;
8175 	ta = (struct iftun_req *)mp1->b_rptr;
8176 	/*
8177 	 * Null terminate the string to protect against buffer
8178 	 * overrun. String was generated by user code and may not
8179 	 * be trusted.
8180 	 */
8181 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8182 
8183 	connp = Q_TO_CONN(q);
8184 	isv6 = connp->conn_af_isv6;
8185 	ipst = connp->conn_netstack->netstack_ip;
8186 
8187 	/* Disallows implicit create */
8188 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8189 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8190 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8191 	if (ipif == NULL)
8192 		return (error);
8193 
8194 	if (ipif->ipif_id != 0) {
8195 		/*
8196 		 * We really don't want to set/get tunnel parameters
8197 		 * on virtual tunnel interfaces.  Only allow the
8198 		 * base tunnel to do these.
8199 		 */
8200 		ipif_refrele(ipif);
8201 		return (EINVAL);
8202 	}
8203 
8204 	/*
8205 	 * Send down to tunnel mod for ioctl processing.
8206 	 * Will finish ioctl in ip_rput_other().
8207 	 */
8208 	ill = ipif->ipif_ill;
8209 	if (ill->ill_net_type == IRE_LOOPBACK) {
8210 		ipif_refrele(ipif);
8211 		return (EOPNOTSUPP);
8212 	}
8213 
8214 	if (ill->ill_wq == NULL) {
8215 		ipif_refrele(ipif);
8216 		return (ENXIO);
8217 	}
8218 	/*
8219 	 * Mark the ioctl as coming from an IPv6 interface for
8220 	 * tun's convenience.
8221 	 */
8222 	if (ill->ill_isv6)
8223 		ta->ifta_flags |= 0x80000000;
8224 	ci->ci_ipif = ipif;
8225 	return (0);
8226 }
8227 
8228 /*
8229  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8230  * and return the associated ipif.
8231  * Return value:
8232  *	Non zero: An error has occurred. ci may not be filled out.
8233  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8234  *	a held ipif in ci.ci_ipif.
8235  */
8236 int
8237 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8238     cmd_info_t *ci, ipsq_func_t func)
8239 {
8240 	sin_t		*sin;
8241 	sin6_t		*sin6;
8242 	char		*name;
8243 	struct ifreq    *ifr;
8244 	struct lifreq    *lifr;
8245 	ipif_t		*ipif = NULL;
8246 	ill_t		*ill;
8247 	conn_t		*connp;
8248 	boolean_t	isv6;
8249 	boolean_t	exists;
8250 	int		err;
8251 	mblk_t		*mp1;
8252 	zoneid_t	zoneid;
8253 	ip_stack_t	*ipst;
8254 
8255 	if (q->q_next != NULL) {
8256 		ill = (ill_t *)q->q_ptr;
8257 		isv6 = ill->ill_isv6;
8258 		connp = NULL;
8259 		zoneid = ALL_ZONES;
8260 		ipst = ill->ill_ipst;
8261 	} else {
8262 		ill = NULL;
8263 		connp = Q_TO_CONN(q);
8264 		isv6 = connp->conn_af_isv6;
8265 		zoneid = connp->conn_zoneid;
8266 		if (zoneid == GLOBAL_ZONEID) {
8267 			/* global zone can access ipifs in all zones */
8268 			zoneid = ALL_ZONES;
8269 		}
8270 		ipst = connp->conn_netstack->netstack_ip;
8271 	}
8272 
8273 	/* Has been checked in ip_wput_nondata */
8274 	mp1 = mp->b_cont->b_cont;
8275 
8276 	if (ipip->ipi_cmd_type == IF_CMD) {
8277 		/* This a old style SIOC[GS]IF* command */
8278 		ifr = (struct ifreq *)mp1->b_rptr;
8279 		/*
8280 		 * Null terminate the string to protect against buffer
8281 		 * overrun. String was generated by user code and may not
8282 		 * be trusted.
8283 		 */
8284 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8285 		sin = (sin_t *)&ifr->ifr_addr;
8286 		name = ifr->ifr_name;
8287 		ci->ci_sin = sin;
8288 		ci->ci_sin6 = NULL;
8289 		ci->ci_lifr = (struct lifreq *)ifr;
8290 	} else {
8291 		/* This a new style SIOC[GS]LIF* command */
8292 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8293 		lifr = (struct lifreq *)mp1->b_rptr;
8294 		/*
8295 		 * Null terminate the string to protect against buffer
8296 		 * overrun. String was generated by user code and may not
8297 		 * be trusted.
8298 		 */
8299 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8300 		name = lifr->lifr_name;
8301 		sin = (sin_t *)&lifr->lifr_addr;
8302 		sin6 = (sin6_t *)&lifr->lifr_addr;
8303 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8304 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8305 			    LIFNAMSIZ);
8306 		}
8307 		ci->ci_sin = sin;
8308 		ci->ci_sin6 = sin6;
8309 		ci->ci_lifr = lifr;
8310 	}
8311 
8312 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8313 		/*
8314 		 * The ioctl will be failed if the ioctl comes down
8315 		 * an conn stream
8316 		 */
8317 		if (ill == NULL) {
8318 			/*
8319 			 * Not an ill queue, return EINVAL same as the
8320 			 * old error code.
8321 			 */
8322 			return (ENXIO);
8323 		}
8324 		ipif = ill->ill_ipif;
8325 		ipif_refhold(ipif);
8326 	} else {
8327 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8328 		    &exists, isv6, zoneid,
8329 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8330 		    ipst);
8331 		if (ipif == NULL) {
8332 			if (err == EINPROGRESS)
8333 				return (err);
8334 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8335 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8336 				/*
8337 				 * Need to try both v4 and v6 since this
8338 				 * ioctl can come down either v4 or v6
8339 				 * socket. The lifreq.lifr_family passed
8340 				 * down by this ioctl is AF_UNSPEC.
8341 				 */
8342 				ipif = ipif_lookup_on_name(name,
8343 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8344 				    zoneid, (connp == NULL) ? q :
8345 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8346 				if (err == EINPROGRESS)
8347 					return (err);
8348 			}
8349 			err = 0;	/* Ensure we don't use it below */
8350 		}
8351 	}
8352 
8353 	/*
8354 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8355 	 */
8356 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8357 		ipif_refrele(ipif);
8358 		return (ENXIO);
8359 	}
8360 
8361 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8362 	    name[0] == '\0') {
8363 		/*
8364 		 * Handle a or a SIOC?IF* with a null name
8365 		 * during plumb (on the ill queue before the I_PLINK).
8366 		 */
8367 		ipif = ill->ill_ipif;
8368 		ipif_refhold(ipif);
8369 	}
8370 
8371 	if (ipif == NULL)
8372 		return (ENXIO);
8373 
8374 	/*
8375 	 * Allow only GET operations if this ipif has been created
8376 	 * temporarily due to a MOVE operation.
8377 	 */
8378 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8379 		ipif_refrele(ipif);
8380 		return (EINVAL);
8381 	}
8382 
8383 	ci->ci_ipif = ipif;
8384 	return (0);
8385 }
8386 
8387 /*
8388  * Return the total number of ipifs.
8389  */
8390 static uint_t
8391 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8392 {
8393 	uint_t numifs = 0;
8394 	ill_t	*ill;
8395 	ill_walk_context_t	ctx;
8396 	ipif_t	*ipif;
8397 
8398 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8399 	ill = ILL_START_WALK_V4(&ctx, ipst);
8400 
8401 	while (ill != NULL) {
8402 		for (ipif = ill->ill_ipif; ipif != NULL;
8403 		    ipif = ipif->ipif_next) {
8404 			if (ipif->ipif_zoneid == zoneid ||
8405 			    ipif->ipif_zoneid == ALL_ZONES)
8406 				numifs++;
8407 		}
8408 		ill = ill_next(&ctx, ill);
8409 	}
8410 	rw_exit(&ipst->ips_ill_g_lock);
8411 	return (numifs);
8412 }
8413 
8414 /*
8415  * Return the total number of ipifs.
8416  */
8417 static uint_t
8418 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8419 {
8420 	uint_t numifs = 0;
8421 	ill_t	*ill;
8422 	ipif_t	*ipif;
8423 	ill_walk_context_t	ctx;
8424 
8425 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8426 
8427 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8428 	if (family == AF_INET)
8429 		ill = ILL_START_WALK_V4(&ctx, ipst);
8430 	else if (family == AF_INET6)
8431 		ill = ILL_START_WALK_V6(&ctx, ipst);
8432 	else
8433 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8434 
8435 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8436 		for (ipif = ill->ill_ipif; ipif != NULL;
8437 		    ipif = ipif->ipif_next) {
8438 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8439 			    !(lifn_flags & LIFC_NOXMIT))
8440 				continue;
8441 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8442 			    !(lifn_flags & LIFC_TEMPORARY))
8443 				continue;
8444 			if (((ipif->ipif_flags &
8445 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8446 			    IPIF_DEPRECATED)) ||
8447 			    IS_LOOPBACK(ill) ||
8448 			    !(ipif->ipif_flags & IPIF_UP)) &&
8449 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8450 				continue;
8451 
8452 			if (zoneid != ipif->ipif_zoneid &&
8453 			    ipif->ipif_zoneid != ALL_ZONES &&
8454 			    (zoneid != GLOBAL_ZONEID ||
8455 			    !(lifn_flags & LIFC_ALLZONES)))
8456 				continue;
8457 
8458 			numifs++;
8459 		}
8460 	}
8461 	rw_exit(&ipst->ips_ill_g_lock);
8462 	return (numifs);
8463 }
8464 
8465 uint_t
8466 ip_get_lifsrcofnum(ill_t *ill)
8467 {
8468 	uint_t numifs = 0;
8469 	ill_t	*ill_head = ill;
8470 	ip_stack_t	*ipst = ill->ill_ipst;
8471 
8472 	/*
8473 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8474 	 * other thread may be trying to relink the ILLs in this usesrc group
8475 	 * and adjusting the ill_usesrc_grp_next pointers
8476 	 */
8477 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8478 	if ((ill->ill_usesrc_ifindex == 0) &&
8479 	    (ill->ill_usesrc_grp_next != NULL)) {
8480 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8481 		    ill = ill->ill_usesrc_grp_next)
8482 			numifs++;
8483 	}
8484 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8485 
8486 	return (numifs);
8487 }
8488 
8489 /* Null values are passed in for ipif, sin, and ifreq */
8490 /* ARGSUSED */
8491 int
8492 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8493     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8494 {
8495 	int *nump;
8496 	conn_t *connp = Q_TO_CONN(q);
8497 
8498 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8499 
8500 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8501 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8502 
8503 	*nump = ip_get_numifs(connp->conn_zoneid,
8504 	    connp->conn_netstack->netstack_ip);
8505 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8506 	return (0);
8507 }
8508 
8509 /* Null values are passed in for ipif, sin, and ifreq */
8510 /* ARGSUSED */
8511 int
8512 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8513     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8514 {
8515 	struct lifnum *lifn;
8516 	mblk_t	*mp1;
8517 	conn_t *connp = Q_TO_CONN(q);
8518 
8519 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8520 
8521 	/* Existence checked in ip_wput_nondata */
8522 	mp1 = mp->b_cont->b_cont;
8523 
8524 	lifn = (struct lifnum *)mp1->b_rptr;
8525 	switch (lifn->lifn_family) {
8526 	case AF_UNSPEC:
8527 	case AF_INET:
8528 	case AF_INET6:
8529 		break;
8530 	default:
8531 		return (EAFNOSUPPORT);
8532 	}
8533 
8534 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8535 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8536 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8537 	return (0);
8538 }
8539 
8540 /* ARGSUSED */
8541 int
8542 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8543     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8544 {
8545 	STRUCT_HANDLE(ifconf, ifc);
8546 	mblk_t *mp1;
8547 	struct iocblk *iocp;
8548 	struct ifreq *ifr;
8549 	ill_walk_context_t	ctx;
8550 	ill_t	*ill;
8551 	ipif_t	*ipif;
8552 	struct sockaddr_in *sin;
8553 	int32_t	ifclen;
8554 	zoneid_t zoneid;
8555 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8556 
8557 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8558 
8559 	ip1dbg(("ip_sioctl_get_ifconf"));
8560 	/* Existence verified in ip_wput_nondata */
8561 	mp1 = mp->b_cont->b_cont;
8562 	iocp = (struct iocblk *)mp->b_rptr;
8563 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8564 
8565 	/*
8566 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8567 	 * the user buffer address and length into which the list of struct
8568 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8569 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8570 	 * the SIOCGIFCONF operation was redefined to simply provide
8571 	 * a large output buffer into which we are supposed to jam the ifreq
8572 	 * array.  The same ioctl command code was used, despite the fact that
8573 	 * both the applications and the kernel code had to change, thus making
8574 	 * it impossible to support both interfaces.
8575 	 *
8576 	 * For reasons not good enough to try to explain, the following
8577 	 * algorithm is used for deciding what to do with one of these:
8578 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8579 	 * form with the output buffer coming down as the continuation message.
8580 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8581 	 * and we have to copy in the ifconf structure to find out how big the
8582 	 * output buffer is and where to copy out to.  Sure no problem...
8583 	 *
8584 	 */
8585 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8586 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8587 		int numifs = 0;
8588 		size_t ifc_bufsize;
8589 
8590 		/*
8591 		 * Must be (better be!) continuation of a TRANSPARENT
8592 		 * IOCTL.  We just copied in the ifconf structure.
8593 		 */
8594 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8595 		    (struct ifconf *)mp1->b_rptr);
8596 
8597 		/*
8598 		 * Allocate a buffer to hold requested information.
8599 		 *
8600 		 * If ifc_len is larger than what is needed, we only
8601 		 * allocate what we will use.
8602 		 *
8603 		 * If ifc_len is smaller than what is needed, return
8604 		 * EINVAL.
8605 		 *
8606 		 * XXX: the ill_t structure can hava 2 counters, for
8607 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8608 		 * number of interfaces for a device, so we don't need
8609 		 * to count them here...
8610 		 */
8611 		numifs = ip_get_numifs(zoneid, ipst);
8612 
8613 		ifclen = STRUCT_FGET(ifc, ifc_len);
8614 		ifc_bufsize = numifs * sizeof (struct ifreq);
8615 		if (ifc_bufsize > ifclen) {
8616 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8617 				/* old behaviour */
8618 				return (EINVAL);
8619 			} else {
8620 				ifc_bufsize = ifclen;
8621 			}
8622 		}
8623 
8624 		mp1 = mi_copyout_alloc(q, mp,
8625 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8626 		if (mp1 == NULL)
8627 			return (ENOMEM);
8628 
8629 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8630 	}
8631 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8632 	/*
8633 	 * the SIOCGIFCONF ioctl only knows about
8634 	 * IPv4 addresses, so don't try to tell
8635 	 * it about interfaces with IPv6-only
8636 	 * addresses. (Last parm 'isv6' is B_FALSE)
8637 	 */
8638 
8639 	ifr = (struct ifreq *)mp1->b_rptr;
8640 
8641 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8642 	ill = ILL_START_WALK_V4(&ctx, ipst);
8643 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8644 		for (ipif = ill->ill_ipif; ipif != NULL;
8645 		    ipif = ipif->ipif_next) {
8646 			if (zoneid != ipif->ipif_zoneid &&
8647 			    ipif->ipif_zoneid != ALL_ZONES)
8648 				continue;
8649 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8650 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8651 					/* old behaviour */
8652 					rw_exit(&ipst->ips_ill_g_lock);
8653 					return (EINVAL);
8654 				} else {
8655 					goto if_copydone;
8656 				}
8657 			}
8658 			ipif_get_name(ipif, ifr->ifr_name,
8659 			    sizeof (ifr->ifr_name));
8660 			sin = (sin_t *)&ifr->ifr_addr;
8661 			*sin = sin_null;
8662 			sin->sin_family = AF_INET;
8663 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8664 			ifr++;
8665 		}
8666 	}
8667 if_copydone:
8668 	rw_exit(&ipst->ips_ill_g_lock);
8669 	mp1->b_wptr = (uchar_t *)ifr;
8670 
8671 	if (STRUCT_BUF(ifc) != NULL) {
8672 		STRUCT_FSET(ifc, ifc_len,
8673 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8674 	}
8675 	return (0);
8676 }
8677 
8678 /*
8679  * Get the interfaces using the address hosted on the interface passed in,
8680  * as a source adddress
8681  */
8682 /* ARGSUSED */
8683 int
8684 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8685     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8686 {
8687 	mblk_t *mp1;
8688 	ill_t	*ill, *ill_head;
8689 	ipif_t	*ipif, *orig_ipif;
8690 	int	numlifs = 0;
8691 	size_t	lifs_bufsize, lifsmaxlen;
8692 	struct	lifreq *lifr;
8693 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8694 	uint_t	ifindex;
8695 	zoneid_t zoneid;
8696 	int err = 0;
8697 	boolean_t isv6 = B_FALSE;
8698 	struct	sockaddr_in	*sin;
8699 	struct	sockaddr_in6	*sin6;
8700 	STRUCT_HANDLE(lifsrcof, lifs);
8701 	ip_stack_t		*ipst;
8702 
8703 	ipst = CONNQ_TO_IPST(q);
8704 
8705 	ASSERT(q->q_next == NULL);
8706 
8707 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8708 
8709 	/* Existence verified in ip_wput_nondata */
8710 	mp1 = mp->b_cont->b_cont;
8711 
8712 	/*
8713 	 * Must be (better be!) continuation of a TRANSPARENT
8714 	 * IOCTL.  We just copied in the lifsrcof structure.
8715 	 */
8716 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8717 	    (struct lifsrcof *)mp1->b_rptr);
8718 
8719 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8720 		return (EINVAL);
8721 
8722 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8723 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8724 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8725 	    ip_process_ioctl, &err, ipst);
8726 	if (ipif == NULL) {
8727 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8728 		    ifindex));
8729 		return (err);
8730 	}
8731 
8732 	/* Allocate a buffer to hold requested information */
8733 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8734 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8735 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8736 	/* The actual size needed is always returned in lifs_len */
8737 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8738 
8739 	/* If the amount we need is more than what is passed in, abort */
8740 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8741 		ipif_refrele(ipif);
8742 		return (0);
8743 	}
8744 
8745 	mp1 = mi_copyout_alloc(q, mp,
8746 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8747 	if (mp1 == NULL) {
8748 		ipif_refrele(ipif);
8749 		return (ENOMEM);
8750 	}
8751 
8752 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8753 	bzero(mp1->b_rptr, lifs_bufsize);
8754 
8755 	lifr = (struct lifreq *)mp1->b_rptr;
8756 
8757 	ill = ill_head = ipif->ipif_ill;
8758 	orig_ipif = ipif;
8759 
8760 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8761 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8762 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8763 
8764 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8765 	for (; (ill != NULL) && (ill != ill_head);
8766 	    ill = ill->ill_usesrc_grp_next) {
8767 
8768 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8769 			break;
8770 
8771 		ipif = ill->ill_ipif;
8772 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8773 		if (ipif->ipif_isv6) {
8774 			sin6 = (sin6_t *)&lifr->lifr_addr;
8775 			*sin6 = sin6_null;
8776 			sin6->sin6_family = AF_INET6;
8777 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8778 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8779 			    &ipif->ipif_v6net_mask);
8780 		} else {
8781 			sin = (sin_t *)&lifr->lifr_addr;
8782 			*sin = sin_null;
8783 			sin->sin_family = AF_INET;
8784 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8785 			lifr->lifr_addrlen = ip_mask_to_plen(
8786 			    ipif->ipif_net_mask);
8787 		}
8788 		lifr++;
8789 	}
8790 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8791 	rw_exit(&ipst->ips_ill_g_lock);
8792 	ipif_refrele(orig_ipif);
8793 	mp1->b_wptr = (uchar_t *)lifr;
8794 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8795 
8796 	return (0);
8797 }
8798 
8799 /* ARGSUSED */
8800 int
8801 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8802     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8803 {
8804 	mblk_t *mp1;
8805 	int	list;
8806 	ill_t	*ill;
8807 	ipif_t	*ipif;
8808 	int	flags;
8809 	int	numlifs = 0;
8810 	size_t	lifc_bufsize;
8811 	struct	lifreq *lifr;
8812 	sa_family_t	family;
8813 	struct	sockaddr_in	*sin;
8814 	struct	sockaddr_in6	*sin6;
8815 	ill_walk_context_t	ctx;
8816 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8817 	int32_t	lifclen;
8818 	zoneid_t zoneid;
8819 	STRUCT_HANDLE(lifconf, lifc);
8820 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8821 
8822 	ip1dbg(("ip_sioctl_get_lifconf"));
8823 
8824 	ASSERT(q->q_next == NULL);
8825 
8826 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8827 
8828 	/* Existence verified in ip_wput_nondata */
8829 	mp1 = mp->b_cont->b_cont;
8830 
8831 	/*
8832 	 * An extended version of SIOCGIFCONF that takes an
8833 	 * additional address family and flags field.
8834 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8835 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8836 	 * interfaces are omitted.
8837 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8838 	 * unless LIFC_TEMPORARY is specified.
8839 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8840 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8841 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8842 	 * has priority over LIFC_NOXMIT.
8843 	 */
8844 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8845 
8846 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8847 		return (EINVAL);
8848 
8849 	/*
8850 	 * Must be (better be!) continuation of a TRANSPARENT
8851 	 * IOCTL.  We just copied in the lifconf structure.
8852 	 */
8853 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8854 
8855 	family = STRUCT_FGET(lifc, lifc_family);
8856 	flags = STRUCT_FGET(lifc, lifc_flags);
8857 
8858 	switch (family) {
8859 	case AF_UNSPEC:
8860 		/*
8861 		 * walk all ILL's.
8862 		 */
8863 		list = MAX_G_HEADS;
8864 		break;
8865 	case AF_INET:
8866 		/*
8867 		 * walk only IPV4 ILL's.
8868 		 */
8869 		list = IP_V4_G_HEAD;
8870 		break;
8871 	case AF_INET6:
8872 		/*
8873 		 * walk only IPV6 ILL's.
8874 		 */
8875 		list = IP_V6_G_HEAD;
8876 		break;
8877 	default:
8878 		return (EAFNOSUPPORT);
8879 	}
8880 
8881 	/*
8882 	 * Allocate a buffer to hold requested information.
8883 	 *
8884 	 * If lifc_len is larger than what is needed, we only
8885 	 * allocate what we will use.
8886 	 *
8887 	 * If lifc_len is smaller than what is needed, return
8888 	 * EINVAL.
8889 	 */
8890 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8891 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8892 	lifclen = STRUCT_FGET(lifc, lifc_len);
8893 	if (lifc_bufsize > lifclen) {
8894 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8895 			return (EINVAL);
8896 		else
8897 			lifc_bufsize = lifclen;
8898 	}
8899 
8900 	mp1 = mi_copyout_alloc(q, mp,
8901 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8902 	if (mp1 == NULL)
8903 		return (ENOMEM);
8904 
8905 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8906 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8907 
8908 	lifr = (struct lifreq *)mp1->b_rptr;
8909 
8910 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8911 	ill = ill_first(list, list, &ctx, ipst);
8912 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8913 		for (ipif = ill->ill_ipif; ipif != NULL;
8914 		    ipif = ipif->ipif_next) {
8915 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8916 			    !(flags & LIFC_NOXMIT))
8917 				continue;
8918 
8919 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8920 			    !(flags & LIFC_TEMPORARY))
8921 				continue;
8922 
8923 			if (((ipif->ipif_flags &
8924 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8925 			    IPIF_DEPRECATED)) ||
8926 			    IS_LOOPBACK(ill) ||
8927 			    !(ipif->ipif_flags & IPIF_UP)) &&
8928 			    (flags & LIFC_EXTERNAL_SOURCE))
8929 				continue;
8930 
8931 			if (zoneid != ipif->ipif_zoneid &&
8932 			    ipif->ipif_zoneid != ALL_ZONES &&
8933 			    (zoneid != GLOBAL_ZONEID ||
8934 			    !(flags & LIFC_ALLZONES)))
8935 				continue;
8936 
8937 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8938 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8939 					rw_exit(&ipst->ips_ill_g_lock);
8940 					return (EINVAL);
8941 				} else {
8942 					goto lif_copydone;
8943 				}
8944 			}
8945 
8946 			ipif_get_name(ipif, lifr->lifr_name,
8947 			    sizeof (lifr->lifr_name));
8948 			if (ipif->ipif_isv6) {
8949 				sin6 = (sin6_t *)&lifr->lifr_addr;
8950 				*sin6 = sin6_null;
8951 				sin6->sin6_family = AF_INET6;
8952 				sin6->sin6_addr =
8953 				    ipif->ipif_v6lcl_addr;
8954 				lifr->lifr_addrlen =
8955 				    ip_mask_to_plen_v6(
8956 				    &ipif->ipif_v6net_mask);
8957 			} else {
8958 				sin = (sin_t *)&lifr->lifr_addr;
8959 				*sin = sin_null;
8960 				sin->sin_family = AF_INET;
8961 				sin->sin_addr.s_addr =
8962 				    ipif->ipif_lcl_addr;
8963 				lifr->lifr_addrlen =
8964 				    ip_mask_to_plen(
8965 				    ipif->ipif_net_mask);
8966 			}
8967 			lifr++;
8968 		}
8969 	}
8970 lif_copydone:
8971 	rw_exit(&ipst->ips_ill_g_lock);
8972 
8973 	mp1->b_wptr = (uchar_t *)lifr;
8974 	if (STRUCT_BUF(lifc) != NULL) {
8975 		STRUCT_FSET(lifc, lifc_len,
8976 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8977 	}
8978 	return (0);
8979 }
8980 
8981 /* ARGSUSED */
8982 int
8983 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8984     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8985 {
8986 	ip_stack_t	*ipst;
8987 
8988 	if (q->q_next == NULL)
8989 		ipst = CONNQ_TO_IPST(q);
8990 	else
8991 		ipst = ILLQ_TO_IPST(q);
8992 
8993 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8994 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8995 	return (0);
8996 }
8997 
8998 static void
8999 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9000 {
9001 	ip6_asp_t *table;
9002 	size_t table_size;
9003 	mblk_t *data_mp;
9004 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9005 	ip_stack_t	*ipst;
9006 
9007 	if (q->q_next == NULL)
9008 		ipst = CONNQ_TO_IPST(q);
9009 	else
9010 		ipst = ILLQ_TO_IPST(q);
9011 
9012 	/* These two ioctls are I_STR only */
9013 	if (iocp->ioc_count == TRANSPARENT) {
9014 		miocnak(q, mp, 0, EINVAL);
9015 		return;
9016 	}
9017 
9018 	data_mp = mp->b_cont;
9019 	if (data_mp == NULL) {
9020 		/* The user passed us a NULL argument */
9021 		table = NULL;
9022 		table_size = iocp->ioc_count;
9023 	} else {
9024 		/*
9025 		 * The user provided a table.  The stream head
9026 		 * may have copied in the user data in chunks,
9027 		 * so make sure everything is pulled up
9028 		 * properly.
9029 		 */
9030 		if (MBLKL(data_mp) < iocp->ioc_count) {
9031 			mblk_t *new_data_mp;
9032 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9033 			    NULL) {
9034 				miocnak(q, mp, 0, ENOMEM);
9035 				return;
9036 			}
9037 			freemsg(data_mp);
9038 			data_mp = new_data_mp;
9039 			mp->b_cont = data_mp;
9040 		}
9041 		table = (ip6_asp_t *)data_mp->b_rptr;
9042 		table_size = iocp->ioc_count;
9043 	}
9044 
9045 	switch (iocp->ioc_cmd) {
9046 	case SIOCGIP6ADDRPOLICY:
9047 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9048 		if (iocp->ioc_rval == -1)
9049 			iocp->ioc_error = EINVAL;
9050 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9051 		else if (table != NULL &&
9052 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9053 			ip6_asp_t *src = table;
9054 			ip6_asp32_t *dst = (void *)table;
9055 			int count = table_size / sizeof (ip6_asp_t);
9056 			int i;
9057 
9058 			/*
9059 			 * We need to do an in-place shrink of the array
9060 			 * to match the alignment attributes of the
9061 			 * 32-bit ABI looking at it.
9062 			 */
9063 			/* LINTED: logical expression always true: op "||" */
9064 			ASSERT(sizeof (*src) > sizeof (*dst));
9065 			for (i = 1; i < count; i++)
9066 				bcopy(src + i, dst + i, sizeof (*dst));
9067 		}
9068 #endif
9069 		break;
9070 
9071 	case SIOCSIP6ADDRPOLICY:
9072 		ASSERT(mp->b_prev == NULL);
9073 		mp->b_prev = (void *)q;
9074 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9075 		/*
9076 		 * We pass in the datamodel here so that the ip6_asp_replace()
9077 		 * routine can handle converting from 32-bit to native formats
9078 		 * where necessary.
9079 		 *
9080 		 * A better way to handle this might be to convert the inbound
9081 		 * data structure here, and hang it off a new 'mp'; thus the
9082 		 * ip6_asp_replace() logic would always be dealing with native
9083 		 * format data structures..
9084 		 *
9085 		 * (An even simpler way to handle these ioctls is to just
9086 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9087 		 * and just recompile everything that depends on it.)
9088 		 */
9089 #endif
9090 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9091 		    iocp->ioc_flag & IOC_MODELS);
9092 		return;
9093 	}
9094 
9095 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9096 	qreply(q, mp);
9097 }
9098 
9099 static void
9100 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9101 {
9102 	mblk_t 		*data_mp;
9103 	struct dstinforeq	*dir;
9104 	uint8_t		*end, *cur;
9105 	in6_addr_t	*daddr, *saddr;
9106 	ipaddr_t	v4daddr;
9107 	ire_t		*ire;
9108 	char		*slabel, *dlabel;
9109 	boolean_t	isipv4;
9110 	int		match_ire;
9111 	ill_t		*dst_ill;
9112 	ipif_t		*src_ipif, *ire_ipif;
9113 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9114 	zoneid_t	zoneid;
9115 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9116 
9117 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9118 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9119 
9120 	/*
9121 	 * This ioctl is I_STR only, and must have a
9122 	 * data mblk following the M_IOCTL mblk.
9123 	 */
9124 	data_mp = mp->b_cont;
9125 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9126 		miocnak(q, mp, 0, EINVAL);
9127 		return;
9128 	}
9129 
9130 	if (MBLKL(data_mp) < iocp->ioc_count) {
9131 		mblk_t *new_data_mp;
9132 
9133 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9134 			miocnak(q, mp, 0, ENOMEM);
9135 			return;
9136 		}
9137 		freemsg(data_mp);
9138 		data_mp = new_data_mp;
9139 		mp->b_cont = data_mp;
9140 	}
9141 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9142 
9143 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9144 	    end - cur >= sizeof (struct dstinforeq);
9145 	    cur += sizeof (struct dstinforeq)) {
9146 		dir = (struct dstinforeq *)cur;
9147 		daddr = &dir->dir_daddr;
9148 		saddr = &dir->dir_saddr;
9149 
9150 		/*
9151 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9152 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9153 		 * and ipif_select_source[_v6]() do not.
9154 		 */
9155 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9156 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9157 
9158 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9159 		if (isipv4) {
9160 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9161 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9162 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9163 		} else {
9164 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9165 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9166 		}
9167 		if (ire == NULL) {
9168 			dir->dir_dreachable = 0;
9169 
9170 			/* move on to next dst addr */
9171 			continue;
9172 		}
9173 		dir->dir_dreachable = 1;
9174 
9175 		ire_ipif = ire->ire_ipif;
9176 		if (ire_ipif == NULL)
9177 			goto next_dst;
9178 
9179 		/*
9180 		 * We expect to get back an interface ire or a
9181 		 * gateway ire cache entry.  For both types, the
9182 		 * output interface is ire_ipif->ipif_ill.
9183 		 */
9184 		dst_ill = ire_ipif->ipif_ill;
9185 		dir->dir_dmactype = dst_ill->ill_mactype;
9186 
9187 		if (isipv4) {
9188 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9189 		} else {
9190 			src_ipif = ipif_select_source_v6(dst_ill,
9191 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9192 			    zoneid);
9193 		}
9194 		if (src_ipif == NULL)
9195 			goto next_dst;
9196 
9197 		*saddr = src_ipif->ipif_v6lcl_addr;
9198 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9199 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9200 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9201 		dir->dir_sdeprecated =
9202 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9203 		ipif_refrele(src_ipif);
9204 next_dst:
9205 		ire_refrele(ire);
9206 	}
9207 	miocack(q, mp, iocp->ioc_count, 0);
9208 }
9209 
9210 /*
9211  * Check if this is an address assigned to this machine.
9212  * Skips interfaces that are down by using ire checks.
9213  * Translates mapped addresses to v4 addresses and then
9214  * treats them as such, returning true if the v4 address
9215  * associated with this mapped address is configured.
9216  * Note: Applications will have to be careful what they do
9217  * with the response; use of mapped addresses limits
9218  * what can be done with the socket, especially with
9219  * respect to socket options and ioctls - neither IPv4
9220  * options nor IPv6 sticky options/ancillary data options
9221  * may be used.
9222  */
9223 /* ARGSUSED */
9224 int
9225 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9226     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9227 {
9228 	struct sioc_addrreq *sia;
9229 	sin_t *sin;
9230 	ire_t *ire;
9231 	mblk_t *mp1;
9232 	zoneid_t zoneid;
9233 	ip_stack_t	*ipst;
9234 
9235 	ip1dbg(("ip_sioctl_tmyaddr"));
9236 
9237 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9238 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9239 	ipst = CONNQ_TO_IPST(q);
9240 
9241 	/* Existence verified in ip_wput_nondata */
9242 	mp1 = mp->b_cont->b_cont;
9243 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9244 	sin = (sin_t *)&sia->sa_addr;
9245 	switch (sin->sin_family) {
9246 	case AF_INET6: {
9247 		sin6_t *sin6 = (sin6_t *)sin;
9248 
9249 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9250 			ipaddr_t v4_addr;
9251 
9252 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9253 			    v4_addr);
9254 			ire = ire_ctable_lookup(v4_addr, 0,
9255 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9256 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9257 		} else {
9258 			in6_addr_t v6addr;
9259 
9260 			v6addr = sin6->sin6_addr;
9261 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9262 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9263 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9264 		}
9265 		break;
9266 	}
9267 	case AF_INET: {
9268 		ipaddr_t v4addr;
9269 
9270 		v4addr = sin->sin_addr.s_addr;
9271 		ire = ire_ctable_lookup(v4addr, 0,
9272 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9273 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9274 		break;
9275 	}
9276 	default:
9277 		return (EAFNOSUPPORT);
9278 	}
9279 	if (ire != NULL) {
9280 		sia->sa_res = 1;
9281 		ire_refrele(ire);
9282 	} else {
9283 		sia->sa_res = 0;
9284 	}
9285 	return (0);
9286 }
9287 
9288 /*
9289  * Check if this is an address assigned on-link i.e. neighbor,
9290  * and makes sure it's reachable from the current zone.
9291  * Returns true for my addresses as well.
9292  * Translates mapped addresses to v4 addresses and then
9293  * treats them as such, returning true if the v4 address
9294  * associated with this mapped address is configured.
9295  * Note: Applications will have to be careful what they do
9296  * with the response; use of mapped addresses limits
9297  * what can be done with the socket, especially with
9298  * respect to socket options and ioctls - neither IPv4
9299  * options nor IPv6 sticky options/ancillary data options
9300  * may be used.
9301  */
9302 /* ARGSUSED */
9303 int
9304 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9305     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9306 {
9307 	struct sioc_addrreq *sia;
9308 	sin_t *sin;
9309 	mblk_t	*mp1;
9310 	ire_t *ire = NULL;
9311 	zoneid_t zoneid;
9312 	ip_stack_t	*ipst;
9313 
9314 	ip1dbg(("ip_sioctl_tonlink"));
9315 
9316 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9317 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9318 	ipst = CONNQ_TO_IPST(q);
9319 
9320 	/* Existence verified in ip_wput_nondata */
9321 	mp1 = mp->b_cont->b_cont;
9322 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9323 	sin = (sin_t *)&sia->sa_addr;
9324 
9325 	/*
9326 	 * Match addresses with a zero gateway field to avoid
9327 	 * routes going through a router.
9328 	 * Exclude broadcast and multicast addresses.
9329 	 */
9330 	switch (sin->sin_family) {
9331 	case AF_INET6: {
9332 		sin6_t *sin6 = (sin6_t *)sin;
9333 
9334 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9335 			ipaddr_t v4_addr;
9336 
9337 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9338 			    v4_addr);
9339 			if (!CLASSD(v4_addr)) {
9340 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9341 				    NULL, NULL, zoneid, NULL,
9342 				    MATCH_IRE_GW, ipst);
9343 			}
9344 		} else {
9345 			in6_addr_t v6addr;
9346 			in6_addr_t v6gw;
9347 
9348 			v6addr = sin6->sin6_addr;
9349 			v6gw = ipv6_all_zeros;
9350 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9351 				ire = ire_route_lookup_v6(&v6addr, 0,
9352 				    &v6gw, 0, NULL, NULL, zoneid,
9353 				    NULL, MATCH_IRE_GW, ipst);
9354 			}
9355 		}
9356 		break;
9357 	}
9358 	case AF_INET: {
9359 		ipaddr_t v4addr;
9360 
9361 		v4addr = sin->sin_addr.s_addr;
9362 		if (!CLASSD(v4addr)) {
9363 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9364 			    NULL, NULL, zoneid, NULL,
9365 			    MATCH_IRE_GW, ipst);
9366 		}
9367 		break;
9368 	}
9369 	default:
9370 		return (EAFNOSUPPORT);
9371 	}
9372 	sia->sa_res = 0;
9373 	if (ire != NULL) {
9374 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9375 		    IRE_LOCAL|IRE_LOOPBACK)) {
9376 			sia->sa_res = 1;
9377 		}
9378 		ire_refrele(ire);
9379 	}
9380 	return (0);
9381 }
9382 
9383 /*
9384  * TBD: implement when kernel maintaines a list of site prefixes.
9385  */
9386 /* ARGSUSED */
9387 int
9388 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9389     ip_ioctl_cmd_t *ipip, void *ifreq)
9390 {
9391 	return (ENXIO);
9392 }
9393 
9394 /* ARGSUSED */
9395 int
9396 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9397     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9398 {
9399 	ill_t		*ill;
9400 	mblk_t		*mp1;
9401 	conn_t		*connp;
9402 	boolean_t	success;
9403 
9404 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9405 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9406 	/* ioctl comes down on an conn */
9407 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9408 	connp = Q_TO_CONN(q);
9409 
9410 	mp->b_datap->db_type = M_IOCTL;
9411 
9412 	/*
9413 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9414 	 * The original mp contains contaminated b_next values due to 'mi',
9415 	 * which is needed to do the mi_copy_done. Unfortunately if we
9416 	 * send down the original mblk itself and if we are popped due to an
9417 	 * an unplumb before the response comes back from tunnel,
9418 	 * the streamhead (which does a freemsg) will see this contaminated
9419 	 * message and the assertion in freemsg about non-null b_next/b_prev
9420 	 * will panic a DEBUG kernel.
9421 	 */
9422 	mp1 = copymsg(mp);
9423 	if (mp1 == NULL)
9424 		return (ENOMEM);
9425 
9426 	ill = ipif->ipif_ill;
9427 	mutex_enter(&connp->conn_lock);
9428 	mutex_enter(&ill->ill_lock);
9429 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9430 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9431 		    mp, 0);
9432 	} else {
9433 		success = ill_pending_mp_add(ill, connp, mp);
9434 	}
9435 	mutex_exit(&ill->ill_lock);
9436 	mutex_exit(&connp->conn_lock);
9437 
9438 	if (success) {
9439 		ip1dbg(("sending down tunparam request "));
9440 		putnext(ill->ill_wq, mp1);
9441 		return (EINPROGRESS);
9442 	} else {
9443 		/* The conn has started closing */
9444 		freemsg(mp1);
9445 		return (EINTR);
9446 	}
9447 }
9448 
9449 /*
9450  * ARP IOCTLs.
9451  * How does IP get in the business of fronting ARP configuration/queries?
9452  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9453  * are by tradition passed in through a datagram socket.  That lands in IP.
9454  * As it happens, this is just as well since the interface is quite crude in
9455  * that it passes in no information about protocol or hardware types, or
9456  * interface association.  After making the protocol assumption, IP is in
9457  * the position to look up the name of the ILL, which ARP will need, and
9458  * format a request that can be handled by ARP.  The request is passed up
9459  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9460  * back a response.  ARP supports its own set of more general IOCTLs, in
9461  * case anyone is interested.
9462  */
9463 /* ARGSUSED */
9464 int
9465 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9466     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9467 {
9468 	mblk_t *mp1;
9469 	mblk_t *mp2;
9470 	mblk_t *pending_mp;
9471 	ipaddr_t ipaddr;
9472 	area_t *area;
9473 	struct iocblk *iocp;
9474 	conn_t *connp;
9475 	struct arpreq *ar;
9476 	struct xarpreq *xar;
9477 	int flags, alength;
9478 	char *lladdr;
9479 	ip_stack_t	*ipst;
9480 	ill_t *ill = ipif->ipif_ill;
9481 	boolean_t if_arp_ioctl = B_FALSE;
9482 
9483 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9484 	connp = Q_TO_CONN(q);
9485 	ipst = connp->conn_netstack->netstack_ip;
9486 
9487 	if (ipip->ipi_cmd_type == XARP_CMD) {
9488 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9489 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9490 		ar = NULL;
9491 
9492 		flags = xar->xarp_flags;
9493 		lladdr = LLADDR(&xar->xarp_ha);
9494 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9495 		/*
9496 		 * Validate against user's link layer address length
9497 		 * input and name and addr length limits.
9498 		 */
9499 		alength = ill->ill_phys_addr_length;
9500 		if (ipip->ipi_cmd == SIOCSXARP) {
9501 			if (alength != xar->xarp_ha.sdl_alen ||
9502 			    (alength + xar->xarp_ha.sdl_nlen >
9503 			    sizeof (xar->xarp_ha.sdl_data)))
9504 				return (EINVAL);
9505 		}
9506 	} else {
9507 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9508 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9509 		xar = NULL;
9510 
9511 		flags = ar->arp_flags;
9512 		lladdr = ar->arp_ha.sa_data;
9513 		/*
9514 		 * Theoretically, the sa_family could tell us what link
9515 		 * layer type this operation is trying to deal with. By
9516 		 * common usage AF_UNSPEC means ethernet. We'll assume
9517 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9518 		 * for now. Our new SIOC*XARP ioctls can be used more
9519 		 * generally.
9520 		 *
9521 		 * If the underlying media happens to have a non 6 byte
9522 		 * address, arp module will fail set/get, but the del
9523 		 * operation will succeed.
9524 		 */
9525 		alength = 6;
9526 		if ((ipip->ipi_cmd != SIOCDARP) &&
9527 		    (alength != ill->ill_phys_addr_length)) {
9528 			return (EINVAL);
9529 		}
9530 	}
9531 
9532 	/*
9533 	 * We are going to pass up to ARP a packet chain that looks
9534 	 * like:
9535 	 *
9536 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9537 	 *
9538 	 * Get a copy of the original IOCTL mblk to head the chain,
9539 	 * to be sent up (in mp1). Also get another copy to store
9540 	 * in the ill_pending_mp list, for matching the response
9541 	 * when it comes back from ARP.
9542 	 */
9543 	mp1 = copyb(mp);
9544 	pending_mp = copymsg(mp);
9545 	if (mp1 == NULL || pending_mp == NULL) {
9546 		if (mp1 != NULL)
9547 			freeb(mp1);
9548 		if (pending_mp != NULL)
9549 			inet_freemsg(pending_mp);
9550 		return (ENOMEM);
9551 	}
9552 
9553 	ipaddr = sin->sin_addr.s_addr;
9554 
9555 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9556 	    (caddr_t)&ipaddr);
9557 	if (mp2 == NULL) {
9558 		freeb(mp1);
9559 		inet_freemsg(pending_mp);
9560 		return (ENOMEM);
9561 	}
9562 	/* Put together the chain. */
9563 	mp1->b_cont = mp2;
9564 	mp1->b_datap->db_type = M_IOCTL;
9565 	mp2->b_cont = mp;
9566 	mp2->b_datap->db_type = M_DATA;
9567 
9568 	iocp = (struct iocblk *)mp1->b_rptr;
9569 
9570 	/*
9571 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9572 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9573 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9574 	 * ioc_count field; set ioc_count to be correct.
9575 	 */
9576 	iocp->ioc_count = MBLKL(mp1->b_cont);
9577 
9578 	/*
9579 	 * Set the proper command in the ARP message.
9580 	 * Convert the SIOC{G|S|D}ARP calls into our
9581 	 * AR_ENTRY_xxx calls.
9582 	 */
9583 	area = (area_t *)mp2->b_rptr;
9584 	switch (iocp->ioc_cmd) {
9585 	case SIOCDARP:
9586 	case SIOCDXARP:
9587 		/*
9588 		 * We defer deleting the corresponding IRE until
9589 		 * we return from arp.
9590 		 */
9591 		area->area_cmd = AR_ENTRY_DELETE;
9592 		area->area_proto_mask_offset = 0;
9593 		break;
9594 	case SIOCGARP:
9595 	case SIOCGXARP:
9596 		area->area_cmd = AR_ENTRY_SQUERY;
9597 		area->area_proto_mask_offset = 0;
9598 		break;
9599 	case SIOCSARP:
9600 	case SIOCSXARP:
9601 		/*
9602 		 * Delete the corresponding ire to make sure IP will
9603 		 * pick up any change from arp.
9604 		 */
9605 		if (!if_arp_ioctl) {
9606 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9607 		} else {
9608 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9609 			if (ipif != NULL) {
9610 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9611 				    ipst);
9612 				ipif_refrele(ipif);
9613 			}
9614 		}
9615 		break;
9616 	}
9617 	iocp->ioc_cmd = area->area_cmd;
9618 
9619 	/*
9620 	 * Fill in the rest of the ARP operation fields.
9621 	 */
9622 	area->area_hw_addr_length = alength;
9623 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9624 
9625 	/* Translate the flags. */
9626 	if (flags & ATF_PERM)
9627 		area->area_flags |= ACE_F_PERMANENT;
9628 	if (flags & ATF_PUBL)
9629 		area->area_flags |= ACE_F_PUBLISH;
9630 	if (flags & ATF_AUTHORITY)
9631 		area->area_flags |= ACE_F_AUTHORITY;
9632 
9633 	/*
9634 	 * Before sending 'mp' to ARP, we have to clear the b_next
9635 	 * and b_prev. Otherwise if STREAMS encounters such a message
9636 	 * in freemsg(), (because ARP can close any time) it can cause
9637 	 * a panic. But mi code needs the b_next and b_prev values of
9638 	 * mp->b_cont, to complete the ioctl. So we store it here
9639 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9640 	 * when the response comes down from ARP.
9641 	 */
9642 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9643 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9644 	mp->b_cont->b_next = NULL;
9645 	mp->b_cont->b_prev = NULL;
9646 
9647 	mutex_enter(&connp->conn_lock);
9648 	mutex_enter(&ill->ill_lock);
9649 	/* conn has not yet started closing, hence this can't fail */
9650 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9651 	mutex_exit(&ill->ill_lock);
9652 	mutex_exit(&connp->conn_lock);
9653 
9654 	/*
9655 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9656 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9657 	 */
9658 	putnext(ill->ill_rq, mp1);
9659 	return (EINPROGRESS);
9660 }
9661 
9662 /*
9663  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9664  * the associated sin and refhold and return the associated ipif via `ci'.
9665  */
9666 int
9667 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9668     cmd_info_t *ci, ipsq_func_t func)
9669 {
9670 	mblk_t	*mp1;
9671 	int	err;
9672 	sin_t	*sin;
9673 	conn_t	*connp;
9674 	ipif_t	*ipif;
9675 	ire_t	*ire = NULL;
9676 	ill_t	*ill = NULL;
9677 	boolean_t exists;
9678 	ip_stack_t *ipst;
9679 	struct arpreq *ar;
9680 	struct xarpreq *xar;
9681 	struct sockaddr_dl *sdl;
9682 
9683 	/* ioctl comes down on a conn */
9684 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9685 	connp = Q_TO_CONN(q);
9686 	if (connp->conn_af_isv6)
9687 		return (ENXIO);
9688 
9689 	ipst = connp->conn_netstack->netstack_ip;
9690 
9691 	/* Verified in ip_wput_nondata */
9692 	mp1 = mp->b_cont->b_cont;
9693 
9694 	if (ipip->ipi_cmd_type == XARP_CMD) {
9695 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9696 		xar = (struct xarpreq *)mp1->b_rptr;
9697 		sin = (sin_t *)&xar->xarp_pa;
9698 		sdl = &xar->xarp_ha;
9699 
9700 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9701 			return (ENXIO);
9702 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9703 			return (EINVAL);
9704 	} else {
9705 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9706 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9707 		ar = (struct arpreq *)mp1->b_rptr;
9708 		sin = (sin_t *)&ar->arp_pa;
9709 	}
9710 
9711 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9712 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9713 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9714 		    mp, func, &err, ipst);
9715 		if (ipif == NULL)
9716 			return (err);
9717 		if (ipif->ipif_id != 0 ||
9718 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9719 			ipif_refrele(ipif);
9720 			return (ENXIO);
9721 		}
9722 	} else {
9723 		/*
9724 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9725 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9726 		 * case, a simple forwarding table lookup will return the
9727 		 * IRE_IF_RESOLVER for the first interface in the group, which
9728 		 * might not be the interface on which the requested IP
9729 		 * address was resolved due to the ill selection algorithm
9730 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9731 		 * lookup first: if the IRE cache entry for the IP address is
9732 		 * still there, it will contain the ill pointer for the right
9733 		 * interface, so we use that. If the cache entry has been
9734 		 * flushed, we fall back to the forwarding table lookup. This
9735 		 * should be rare enough since IRE cache entries have a longer
9736 		 * life expectancy than ARP cache entries.
9737 		 */
9738 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9739 		    ipst);
9740 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9741 		    ((ill = ire_to_ill(ire)) == NULL) ||
9742 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9743 			if (ire != NULL)
9744 				ire_refrele(ire);
9745 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9746 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9747 			    NULL, MATCH_IRE_TYPE, ipst);
9748 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9749 
9750 				if (ire != NULL)
9751 					ire_refrele(ire);
9752 				return (ENXIO);
9753 			}
9754 		}
9755 		ASSERT(ire != NULL && ill != NULL);
9756 		ipif = ill->ill_ipif;
9757 		ipif_refhold(ipif);
9758 		ire_refrele(ire);
9759 	}
9760 	ci->ci_sin = sin;
9761 	ci->ci_ipif = ipif;
9762 	return (0);
9763 }
9764 
9765 /*
9766  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9767  * atomically set/clear the muxids. Also complete the ioctl by acking or
9768  * naking it.  Note that the code is structured such that the link type,
9769  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9770  * its clones use the persistent link, while pppd(1M) and perhaps many
9771  * other daemons may use non-persistent link.  When combined with some
9772  * ill_t states, linking and unlinking lower streams may be used as
9773  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9774  */
9775 /* ARGSUSED */
9776 void
9777 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9778 {
9779 	mblk_t		*mp1, *mp2;
9780 	struct linkblk	*li;
9781 	struct ipmx_s	*ipmxp;
9782 	ill_t		*ill;
9783 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9784 	int		err = 0;
9785 	boolean_t	entered_ipsq = B_FALSE;
9786 	boolean_t	islink;
9787 	ip_stack_t	*ipst;
9788 
9789 	if (CONN_Q(q))
9790 		ipst = CONNQ_TO_IPST(q);
9791 	else
9792 		ipst = ILLQ_TO_IPST(q);
9793 
9794 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9795 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9796 
9797 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9798 
9799 	mp1 = mp->b_cont;	/* This is the linkblk info */
9800 	li = (struct linkblk *)mp1->b_rptr;
9801 
9802 	/*
9803 	 * ARP has added this special mblk, and the utility is asking us
9804 	 * to perform consistency checks, and also atomically set the
9805 	 * muxid. Ifconfig is an example.  It achieves this by using
9806 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9807 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9808 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9809 	 * and other comments in this routine for more details.
9810 	 */
9811 	mp2 = mp1->b_cont;	/* This is added by ARP */
9812 
9813 	/*
9814 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9815 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9816 	 * get the special mblk above.  For backward compatibility, we
9817 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9818 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9819 	 * not atomic, and can leave the streams unplumbable if the utility
9820 	 * is interrupted before it does the SIOCSLIFMUXID.
9821 	 */
9822 	if (mp2 == NULL) {
9823 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9824 		if (err == EINPROGRESS)
9825 			return;
9826 		goto done;
9827 	}
9828 
9829 	/*
9830 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9831 	 * ARP has appended this last mblk to tell us whether the lower stream
9832 	 * is an arp-dev stream or an IP module stream.
9833 	 */
9834 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9835 	if (ipmxp->ipmx_arpdev_stream) {
9836 		/*
9837 		 * The lower stream is the arp-dev stream.
9838 		 */
9839 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9840 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9841 		if (ill == NULL) {
9842 			if (err == EINPROGRESS)
9843 				return;
9844 			err = EINVAL;
9845 			goto done;
9846 		}
9847 
9848 		if (ipsq == NULL) {
9849 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9850 			    NEW_OP, B_TRUE);
9851 			if (ipsq == NULL) {
9852 				ill_refrele(ill);
9853 				return;
9854 			}
9855 			entered_ipsq = B_TRUE;
9856 		}
9857 		ASSERT(IAM_WRITER_ILL(ill));
9858 		ill_refrele(ill);
9859 
9860 		/*
9861 		 * To ensure consistency between IP and ARP, the following
9862 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9863 		 * This is because the muxid's are stored in the IP stream on
9864 		 * the ill.
9865 		 *
9866 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9867 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9868 		 * not yet plinked, and it also checks that the corresponding
9869 		 * IP stream is already plinked.
9870 		 *
9871 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9872 		 * punlinking the IP stream. IP does not allow punlink of the
9873 		 * IP stream unless the arp stream has been punlinked.
9874 		 */
9875 		if ((islink &&
9876 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9877 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9878 			err = EINVAL;
9879 			goto done;
9880 		}
9881 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9882 	} else {
9883 		/*
9884 		 * The lower stream is probably an IP module stream.  Do
9885 		 * consistency checking.
9886 		 */
9887 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9888 		if (err == EINPROGRESS)
9889 			return;
9890 	}
9891 done:
9892 	if (err == 0)
9893 		miocack(q, mp, 0, 0);
9894 	else
9895 		miocnak(q, mp, 0, err);
9896 
9897 	/* Conn was refheld in ip_sioctl_copyin_setup */
9898 	if (CONN_Q(q))
9899 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9900 	if (entered_ipsq)
9901 		ipsq_exit(ipsq);
9902 }
9903 
9904 /*
9905  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9906  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9907  * module stream).  If `doconsist' is set, then do the extended consistency
9908  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9909  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9910  * an error code on failure.
9911  */
9912 static int
9913 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9914     struct linkblk *li, boolean_t doconsist)
9915 {
9916 	ill_t  		*ill;
9917 	queue_t		*ipwq, *dwq;
9918 	const char	*name;
9919 	struct qinit	*qinfo;
9920 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9921 	boolean_t	entered_ipsq = B_FALSE;
9922 
9923 	/*
9924 	 * Walk the lower stream to verify it's the IP module stream.
9925 	 * The IP module is identified by its name, wput function,
9926 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9927 	 * (li->l_qbot) will not vanish until this ioctl completes.
9928 	 */
9929 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9930 		qinfo = ipwq->q_qinfo;
9931 		name = qinfo->qi_minfo->mi_idname;
9932 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9933 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9934 			break;
9935 		}
9936 	}
9937 
9938 	/*
9939 	 * If this isn't an IP module stream, bail.
9940 	 */
9941 	if (ipwq == NULL)
9942 		return (0);
9943 
9944 	ill = ipwq->q_ptr;
9945 	ASSERT(ill != NULL);
9946 
9947 	if (ipsq == NULL) {
9948 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9949 		    NEW_OP, B_TRUE);
9950 		if (ipsq == NULL)
9951 			return (EINPROGRESS);
9952 		entered_ipsq = B_TRUE;
9953 	}
9954 	ASSERT(IAM_WRITER_ILL(ill));
9955 
9956 	if (doconsist) {
9957 		/*
9958 		 * Consistency checking requires that I_{P}LINK occurs
9959 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9960 		 * occurs prior to clearing ill_arp_muxid.
9961 		 */
9962 		if ((islink && ill->ill_ip_muxid != 0) ||
9963 		    (!islink && ill->ill_arp_muxid != 0)) {
9964 			if (entered_ipsq)
9965 				ipsq_exit(ipsq);
9966 			return (EINVAL);
9967 		}
9968 	}
9969 
9970 	/*
9971 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9972 	 * the read queue of the module immediately below IP in the ill.
9973 	 * These are used during the capability negotiation below.
9974 	 */
9975 	ill->ill_lmod_rq = NULL;
9976 	ill->ill_lmod_cnt = 0;
9977 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9978 		ill->ill_lmod_rq = RD(dwq);
9979 		for (; dwq != NULL; dwq = dwq->q_next)
9980 			ill->ill_lmod_cnt++;
9981 	}
9982 
9983 	if (doconsist)
9984 		ill->ill_ip_muxid = islink ? li->l_index : 0;
9985 
9986 	/*
9987 	 * If there's at least one up ipif on this ill, then we're bound to
9988 	 * the underlying driver via DLPI.  In that case, renegotiate
9989 	 * capabilities to account for any possible change in modules
9990 	 * interposed between IP and the driver.
9991 	 */
9992 	if (ill->ill_ipif_up_count > 0) {
9993 		if (islink)
9994 			ill_capability_probe(ill);
9995 		else
9996 			ill_capability_reset(ill);
9997 	}
9998 
9999 	if (entered_ipsq)
10000 		ipsq_exit(ipsq);
10001 
10002 	return (0);
10003 }
10004 
10005 /*
10006  * Search the ioctl command in the ioctl tables and return a pointer
10007  * to the ioctl command information. The ioctl command tables are
10008  * static and fully populated at compile time.
10009  */
10010 ip_ioctl_cmd_t *
10011 ip_sioctl_lookup(int ioc_cmd)
10012 {
10013 	int index;
10014 	ip_ioctl_cmd_t *ipip;
10015 	ip_ioctl_cmd_t *ipip_end;
10016 
10017 	if (ioc_cmd == IPI_DONTCARE)
10018 		return (NULL);
10019 
10020 	/*
10021 	 * Do a 2 step search. First search the indexed table
10022 	 * based on the least significant byte of the ioctl cmd.
10023 	 * If we don't find a match, then search the misc table
10024 	 * serially.
10025 	 */
10026 	index = ioc_cmd & 0xFF;
10027 	if (index < ip_ndx_ioctl_count) {
10028 		ipip = &ip_ndx_ioctl_table[index];
10029 		if (ipip->ipi_cmd == ioc_cmd) {
10030 			/* Found a match in the ndx table */
10031 			return (ipip);
10032 		}
10033 	}
10034 
10035 	/* Search the misc table */
10036 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10037 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10038 		if (ipip->ipi_cmd == ioc_cmd)
10039 			/* Found a match in the misc table */
10040 			return (ipip);
10041 	}
10042 
10043 	return (NULL);
10044 }
10045 
10046 /*
10047  * Wrapper function for resuming deferred ioctl processing
10048  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10049  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10050  */
10051 /* ARGSUSED */
10052 void
10053 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10054     void *dummy_arg)
10055 {
10056 	ip_sioctl_copyin_setup(q, mp);
10057 }
10058 
10059 /*
10060  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10061  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10062  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10063  * We establish here the size of the block to be copied in.  mi_copyin
10064  * arranges for this to happen, an processing continues in ip_wput with
10065  * an M_IOCDATA message.
10066  */
10067 void
10068 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10069 {
10070 	int	copyin_size;
10071 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10072 	ip_ioctl_cmd_t *ipip;
10073 	cred_t *cr;
10074 	ip_stack_t	*ipst;
10075 
10076 	if (CONN_Q(q))
10077 		ipst = CONNQ_TO_IPST(q);
10078 	else
10079 		ipst = ILLQ_TO_IPST(q);
10080 
10081 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10082 	if (ipip == NULL) {
10083 		/*
10084 		 * The ioctl is not one we understand or own.
10085 		 * Pass it along to be processed down stream,
10086 		 * if this is a module instance of IP, else nak
10087 		 * the ioctl.
10088 		 */
10089 		if (q->q_next == NULL) {
10090 			goto nak;
10091 		} else {
10092 			putnext(q, mp);
10093 			return;
10094 		}
10095 	}
10096 
10097 	/*
10098 	 * If this is deferred, then we will do all the checks when we
10099 	 * come back.
10100 	 */
10101 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10102 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10103 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10104 		return;
10105 	}
10106 
10107 	/*
10108 	 * Only allow a very small subset of IP ioctls on this stream if
10109 	 * IP is a module and not a driver. Allowing ioctls to be processed
10110 	 * in this case may cause assert failures or data corruption.
10111 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10112 	 * ioctls allowed on an IP module stream, after which this stream
10113 	 * normally becomes a multiplexor (at which time the stream head
10114 	 * will fail all ioctls).
10115 	 */
10116 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10117 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10118 			/*
10119 			 * Pass common Streams ioctls which the IP
10120 			 * module does not own or consume along to
10121 			 * be processed down stream.
10122 			 */
10123 			putnext(q, mp);
10124 			return;
10125 		} else {
10126 			goto nak;
10127 		}
10128 	}
10129 
10130 	/* Make sure we have ioctl data to process. */
10131 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10132 		goto nak;
10133 
10134 	/*
10135 	 * Prefer dblk credential over ioctl credential; some synthesized
10136 	 * ioctls have kcred set because there's no way to crhold()
10137 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10138 	 * the framework; the caller of ioctl needs to hold the reference
10139 	 * for the duration of the call).
10140 	 */
10141 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10142 
10143 	/* Make sure normal users don't send down privileged ioctls */
10144 	if ((ipip->ipi_flags & IPI_PRIV) &&
10145 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10146 		/* We checked the privilege earlier but log it here */
10147 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10148 		return;
10149 	}
10150 
10151 	/*
10152 	 * The ioctl command tables can only encode fixed length
10153 	 * ioctl data. If the length is variable, the table will
10154 	 * encode the length as zero. Such special cases are handled
10155 	 * below in the switch.
10156 	 */
10157 	if (ipip->ipi_copyin_size != 0) {
10158 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10159 		return;
10160 	}
10161 
10162 	switch (iocp->ioc_cmd) {
10163 	case O_SIOCGIFCONF:
10164 	case SIOCGIFCONF:
10165 		/*
10166 		 * This IOCTL is hilarious.  See comments in
10167 		 * ip_sioctl_get_ifconf for the story.
10168 		 */
10169 		if (iocp->ioc_count == TRANSPARENT)
10170 			copyin_size = SIZEOF_STRUCT(ifconf,
10171 			    iocp->ioc_flag);
10172 		else
10173 			copyin_size = iocp->ioc_count;
10174 		mi_copyin(q, mp, NULL, copyin_size);
10175 		return;
10176 
10177 	case O_SIOCGLIFCONF:
10178 	case SIOCGLIFCONF:
10179 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10180 		mi_copyin(q, mp, NULL, copyin_size);
10181 		return;
10182 
10183 	case SIOCGLIFSRCOF:
10184 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10185 		mi_copyin(q, mp, NULL, copyin_size);
10186 		return;
10187 	case SIOCGIP6ADDRPOLICY:
10188 		ip_sioctl_ip6addrpolicy(q, mp);
10189 		ip6_asp_table_refrele(ipst);
10190 		return;
10191 
10192 	case SIOCSIP6ADDRPOLICY:
10193 		ip_sioctl_ip6addrpolicy(q, mp);
10194 		return;
10195 
10196 	case SIOCGDSTINFO:
10197 		ip_sioctl_dstinfo(q, mp);
10198 		ip6_asp_table_refrele(ipst);
10199 		return;
10200 
10201 	case I_PLINK:
10202 	case I_PUNLINK:
10203 	case I_LINK:
10204 	case I_UNLINK:
10205 		/*
10206 		 * We treat non-persistent link similarly as the persistent
10207 		 * link case, in terms of plumbing/unplumbing, as well as
10208 		 * dynamic re-plumbing events indicator.  See comments
10209 		 * in ip_sioctl_plink() for more.
10210 		 *
10211 		 * Request can be enqueued in the 'ipsq' while waiting
10212 		 * to become exclusive. So bump up the conn ref.
10213 		 */
10214 		if (CONN_Q(q))
10215 			CONN_INC_REF(Q_TO_CONN(q));
10216 		ip_sioctl_plink(NULL, q, mp, NULL);
10217 		return;
10218 
10219 	case ND_GET:
10220 	case ND_SET:
10221 		/*
10222 		 * Use of the nd table requires holding the reader lock.
10223 		 * Modifying the nd table thru nd_load/nd_unload requires
10224 		 * the writer lock.
10225 		 */
10226 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10227 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10228 			rw_exit(&ipst->ips_ip_g_nd_lock);
10229 
10230 			if (iocp->ioc_error)
10231 				iocp->ioc_count = 0;
10232 			mp->b_datap->db_type = M_IOCACK;
10233 			qreply(q, mp);
10234 			return;
10235 		}
10236 		rw_exit(&ipst->ips_ip_g_nd_lock);
10237 		/*
10238 		 * We don't understand this subioctl of ND_GET / ND_SET.
10239 		 * Maybe intended for some driver / module below us
10240 		 */
10241 		if (q->q_next) {
10242 			putnext(q, mp);
10243 		} else {
10244 			iocp->ioc_error = ENOENT;
10245 			mp->b_datap->db_type = M_IOCNAK;
10246 			iocp->ioc_count = 0;
10247 			qreply(q, mp);
10248 		}
10249 		return;
10250 
10251 	case IP_IOCTL:
10252 		ip_wput_ioctl(q, mp);
10253 		return;
10254 	default:
10255 		cmn_err(CE_PANIC, "should not happen ");
10256 	}
10257 nak:
10258 	if (mp->b_cont != NULL) {
10259 		freemsg(mp->b_cont);
10260 		mp->b_cont = NULL;
10261 	}
10262 	iocp->ioc_error = EINVAL;
10263 	mp->b_datap->db_type = M_IOCNAK;
10264 	iocp->ioc_count = 0;
10265 	qreply(q, mp);
10266 }
10267 
10268 /* ip_wput hands off ARP IOCTL responses to us */
10269 void
10270 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10271 {
10272 	struct arpreq *ar;
10273 	struct xarpreq *xar;
10274 	area_t	*area;
10275 	mblk_t	*area_mp;
10276 	struct iocblk *iocp;
10277 	mblk_t	*orig_ioc_mp, *tmp;
10278 	struct iocblk	*orig_iocp;
10279 	ill_t *ill;
10280 	conn_t *connp = NULL;
10281 	uint_t ioc_id;
10282 	mblk_t *pending_mp;
10283 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10284 	int *flagsp;
10285 	char *storage = NULL;
10286 	sin_t *sin;
10287 	ipaddr_t addr;
10288 	int err;
10289 	ip_stack_t *ipst;
10290 
10291 	ill = q->q_ptr;
10292 	ASSERT(ill != NULL);
10293 	ipst = ill->ill_ipst;
10294 
10295 	/*
10296 	 * We should get back from ARP a packet chain that looks like:
10297 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10298 	 */
10299 	if (!(area_mp = mp->b_cont) ||
10300 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10301 	    !(orig_ioc_mp = area_mp->b_cont) ||
10302 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10303 		freemsg(mp);
10304 		return;
10305 	}
10306 
10307 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10308 
10309 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10310 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10311 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10312 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10313 		x_arp_ioctl = B_TRUE;
10314 		xar = (struct xarpreq *)tmp->b_rptr;
10315 		sin = (sin_t *)&xar->xarp_pa;
10316 		flagsp = &xar->xarp_flags;
10317 		storage = xar->xarp_ha.sdl_data;
10318 		if (xar->xarp_ha.sdl_nlen != 0)
10319 			ifx_arp_ioctl = B_TRUE;
10320 	} else {
10321 		ar = (struct arpreq *)tmp->b_rptr;
10322 		sin = (sin_t *)&ar->arp_pa;
10323 		flagsp = &ar->arp_flags;
10324 		storage = ar->arp_ha.sa_data;
10325 	}
10326 
10327 	iocp = (struct iocblk *)mp->b_rptr;
10328 
10329 	/*
10330 	 * Pick out the originating queue based on the ioc_id.
10331 	 */
10332 	ioc_id = iocp->ioc_id;
10333 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10334 	if (pending_mp == NULL) {
10335 		ASSERT(connp == NULL);
10336 		inet_freemsg(mp);
10337 		return;
10338 	}
10339 	ASSERT(connp != NULL);
10340 	q = CONNP_TO_WQ(connp);
10341 
10342 	/* Uncouple the internally generated IOCTL from the original one */
10343 	area = (area_t *)area_mp->b_rptr;
10344 	area_mp->b_cont = NULL;
10345 
10346 	/*
10347 	 * Restore the b_next and b_prev used by mi code. This is needed
10348 	 * to complete the ioctl using mi* functions. We stored them in
10349 	 * the pending mp prior to sending the request to ARP.
10350 	 */
10351 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10352 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10353 	inet_freemsg(pending_mp);
10354 
10355 	/*
10356 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10357 	 * Catch the case where there is an IRE_CACHE by no entry in the
10358 	 * arp table.
10359 	 */
10360 	addr = sin->sin_addr.s_addr;
10361 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10362 		ire_t			*ire;
10363 		dl_unitdata_req_t	*dlup;
10364 		mblk_t			*llmp;
10365 		int			addr_len;
10366 		ill_t			*ipsqill = NULL;
10367 
10368 		if (ifx_arp_ioctl) {
10369 			/*
10370 			 * There's no need to lookup the ill, since
10371 			 * we've already done that when we started
10372 			 * processing the ioctl and sent the message
10373 			 * to ARP on that ill.  So use the ill that
10374 			 * is stored in q->q_ptr.
10375 			 */
10376 			ipsqill = ill;
10377 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10378 			    ipsqill->ill_ipif, ALL_ZONES,
10379 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10380 		} else {
10381 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10382 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10383 			if (ire != NULL)
10384 				ipsqill = ire_to_ill(ire);
10385 		}
10386 
10387 		if ((x_arp_ioctl) && (ipsqill != NULL))
10388 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10389 
10390 		if (ire != NULL) {
10391 			/*
10392 			 * Since the ire obtained from cachetable is used for
10393 			 * mac addr copying below, treat an incomplete ire as if
10394 			 * as if we never found it.
10395 			 */
10396 			if (ire->ire_nce != NULL &&
10397 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10398 				ire_refrele(ire);
10399 				ire = NULL;
10400 				ipsqill = NULL;
10401 				goto errack;
10402 			}
10403 			*flagsp = ATF_INUSE;
10404 			llmp = (ire->ire_nce != NULL ?
10405 			    ire->ire_nce->nce_res_mp : NULL);
10406 			if (llmp != NULL && ipsqill != NULL) {
10407 				uchar_t *macaddr;
10408 
10409 				addr_len = ipsqill->ill_phys_addr_length;
10410 				if (x_arp_ioctl && ((addr_len +
10411 				    ipsqill->ill_name_length) >
10412 				    sizeof (xar->xarp_ha.sdl_data))) {
10413 					ire_refrele(ire);
10414 					freemsg(mp);
10415 					ip_ioctl_finish(q, orig_ioc_mp,
10416 					    EINVAL, NO_COPYOUT, NULL);
10417 					return;
10418 				}
10419 				*flagsp |= ATF_COM;
10420 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10421 				if (ipsqill->ill_sap_length < 0)
10422 					macaddr = llmp->b_rptr +
10423 					    dlup->dl_dest_addr_offset;
10424 				else
10425 					macaddr = llmp->b_rptr +
10426 					    dlup->dl_dest_addr_offset +
10427 					    ipsqill->ill_sap_length;
10428 				/*
10429 				 * For SIOCGARP, MAC address length
10430 				 * validation has already been done
10431 				 * before the ioctl was issued to ARP to
10432 				 * allow it to progress only on 6 byte
10433 				 * addressable (ethernet like) media. Thus
10434 				 * the mac address copying can not overwrite
10435 				 * the sa_data area below.
10436 				 */
10437 				bcopy(macaddr, storage, addr_len);
10438 			}
10439 			/* Ditch the internal IOCTL. */
10440 			freemsg(mp);
10441 			ire_refrele(ire);
10442 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10443 			return;
10444 		}
10445 	}
10446 
10447 	/*
10448 	 * Delete the coresponding IRE_CACHE if any.
10449 	 * Reset the error if there was one (in case there was no entry
10450 	 * in arp.)
10451 	 */
10452 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10453 		ipif_t *ipintf = NULL;
10454 
10455 		if (ifx_arp_ioctl) {
10456 			/*
10457 			 * There's no need to lookup the ill, since
10458 			 * we've already done that when we started
10459 			 * processing the ioctl and sent the message
10460 			 * to ARP on that ill.  So use the ill that
10461 			 * is stored in q->q_ptr.
10462 			 */
10463 			ipintf = ill->ill_ipif;
10464 		}
10465 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10466 			/*
10467 			 * The address in "addr" may be an entry for a
10468 			 * router. If that's true, then any off-net
10469 			 * IRE_CACHE entries that go through the router
10470 			 * with address "addr" must be clobbered. Use
10471 			 * ire_walk to achieve this goal.
10472 			 */
10473 			if (ifx_arp_ioctl)
10474 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10475 				    ire_delete_cache_gw, (char *)&addr, ill);
10476 			else
10477 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10478 				    ALL_ZONES, ipst);
10479 			iocp->ioc_error = 0;
10480 		}
10481 	}
10482 errack:
10483 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10484 		err = iocp->ioc_error;
10485 		freemsg(mp);
10486 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10487 		return;
10488 	}
10489 
10490 	/*
10491 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10492 	 * the area_t into the struct {x}arpreq.
10493 	 */
10494 	if (x_arp_ioctl) {
10495 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10496 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10497 		    sizeof (xar->xarp_ha.sdl_data)) {
10498 			freemsg(mp);
10499 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10500 			    NULL);
10501 			return;
10502 		}
10503 	}
10504 	*flagsp = ATF_INUSE;
10505 	if (area->area_flags & ACE_F_PERMANENT)
10506 		*flagsp |= ATF_PERM;
10507 	if (area->area_flags & ACE_F_PUBLISH)
10508 		*flagsp |= ATF_PUBL;
10509 	if (area->area_flags & ACE_F_AUTHORITY)
10510 		*flagsp |= ATF_AUTHORITY;
10511 	if (area->area_hw_addr_length != 0) {
10512 		*flagsp |= ATF_COM;
10513 		/*
10514 		 * For SIOCGARP, MAC address length validation has
10515 		 * already been done before the ioctl was issued to ARP
10516 		 * to allow it to progress only on 6 byte addressable
10517 		 * (ethernet like) media. Thus the mac address copying
10518 		 * can not overwrite the sa_data area below.
10519 		 */
10520 		bcopy((char *)area + area->area_hw_addr_offset,
10521 		    storage, area->area_hw_addr_length);
10522 	}
10523 
10524 	/* Ditch the internal IOCTL. */
10525 	freemsg(mp);
10526 	/* Complete the original. */
10527 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10528 }
10529 
10530 /*
10531  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10532  * interface) create the next available logical interface for this
10533  * physical interface.
10534  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10535  * ipif with the specified name.
10536  *
10537  * If the address family is not AF_UNSPEC then set the address as well.
10538  *
10539  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10540  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10541  *
10542  * Executed as a writer on the ill or ill group.
10543  * So no lock is needed to traverse the ipif chain, or examine the
10544  * phyint flags.
10545  */
10546 /* ARGSUSED */
10547 int
10548 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10549     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10550 {
10551 	mblk_t	*mp1;
10552 	struct lifreq *lifr;
10553 	boolean_t	isv6;
10554 	boolean_t	exists;
10555 	char 	*name;
10556 	char	*endp;
10557 	char	*cp;
10558 	int	namelen;
10559 	ipif_t	*ipif;
10560 	long	id;
10561 	ipsq_t	*ipsq;
10562 	ill_t	*ill;
10563 	sin_t	*sin;
10564 	int	err = 0;
10565 	boolean_t found_sep = B_FALSE;
10566 	conn_t	*connp;
10567 	zoneid_t zoneid;
10568 	int	orig_ifindex = 0;
10569 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10570 
10571 	ASSERT(q->q_next == NULL);
10572 	ip1dbg(("ip_sioctl_addif\n"));
10573 	/* Existence of mp1 has been checked in ip_wput_nondata */
10574 	mp1 = mp->b_cont->b_cont;
10575 	/*
10576 	 * Null terminate the string to protect against buffer
10577 	 * overrun. String was generated by user code and may not
10578 	 * be trusted.
10579 	 */
10580 	lifr = (struct lifreq *)mp1->b_rptr;
10581 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10582 	name = lifr->lifr_name;
10583 	ASSERT(CONN_Q(q));
10584 	connp = Q_TO_CONN(q);
10585 	isv6 = connp->conn_af_isv6;
10586 	zoneid = connp->conn_zoneid;
10587 	namelen = mi_strlen(name);
10588 	if (namelen == 0)
10589 		return (EINVAL);
10590 
10591 	exists = B_FALSE;
10592 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10593 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10594 		/*
10595 		 * Allow creating lo0 using SIOCLIFADDIF.
10596 		 * can't be any other writer thread. So can pass null below
10597 		 * for the last 4 args to ipif_lookup_name.
10598 		 */
10599 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10600 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10601 		/* Prevent any further action */
10602 		if (ipif == NULL) {
10603 			return (ENOBUFS);
10604 		} else if (!exists) {
10605 			/* We created the ipif now and as writer */
10606 			ipif_refrele(ipif);
10607 			return (0);
10608 		} else {
10609 			ill = ipif->ipif_ill;
10610 			ill_refhold(ill);
10611 			ipif_refrele(ipif);
10612 		}
10613 	} else {
10614 		/* Look for a colon in the name. */
10615 		endp = &name[namelen];
10616 		for (cp = endp; --cp > name; ) {
10617 			if (*cp == IPIF_SEPARATOR_CHAR) {
10618 				found_sep = B_TRUE;
10619 				/*
10620 				 * Reject any non-decimal aliases for plumbing
10621 				 * of logical interfaces. Aliases with leading
10622 				 * zeroes are also rejected as they introduce
10623 				 * ambiguity in the naming of the interfaces.
10624 				 * Comparing with "0" takes care of all such
10625 				 * cases.
10626 				 */
10627 				if ((strncmp("0", cp+1, 1)) == 0)
10628 					return (EINVAL);
10629 
10630 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10631 				    id <= 0 || *endp != '\0') {
10632 					return (EINVAL);
10633 				}
10634 				*cp = '\0';
10635 				break;
10636 			}
10637 		}
10638 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10639 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10640 		if (found_sep)
10641 			*cp = IPIF_SEPARATOR_CHAR;
10642 		if (ill == NULL)
10643 			return (err);
10644 	}
10645 
10646 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10647 	    B_TRUE);
10648 
10649 	/*
10650 	 * Release the refhold due to the lookup, now that we are excl
10651 	 * or we are just returning
10652 	 */
10653 	ill_refrele(ill);
10654 
10655 	if (ipsq == NULL)
10656 		return (EINPROGRESS);
10657 
10658 	/*
10659 	 * If the interface is failed, inactive or offlined, look for a working
10660 	 * interface in the ill group and create the ipif there. If we can't
10661 	 * find a good interface, create the ipif anyway so that in.mpathd can
10662 	 * move it to the first repaired interface.
10663 	 */
10664 	if ((ill->ill_phyint->phyint_flags &
10665 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10666 	    ill->ill_phyint->phyint_groupname_len != 0) {
10667 		phyint_t *phyi;
10668 		char *groupname = ill->ill_phyint->phyint_groupname;
10669 
10670 		/*
10671 		 * We're looking for a working interface, but it doesn't matter
10672 		 * if it's up or down; so instead of following the group lists,
10673 		 * we look at each physical interface and compare the groupname.
10674 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10675 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10676 		 * Otherwise we create the ipif on the failed interface.
10677 		 */
10678 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10679 		phyi = avl_first(&ipst->ips_phyint_g_list->
10680 		    phyint_list_avl_by_index);
10681 		for (; phyi != NULL;
10682 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10683 		    phyint_list_avl_by_index,
10684 		    phyi, AVL_AFTER)) {
10685 			if (phyi->phyint_groupname_len == 0)
10686 				continue;
10687 			ASSERT(phyi->phyint_groupname != NULL);
10688 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10689 			    !(phyi->phyint_flags &
10690 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10691 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10692 			    (phyi->phyint_illv4 != NULL))) {
10693 				break;
10694 			}
10695 		}
10696 		rw_exit(&ipst->ips_ill_g_lock);
10697 
10698 		if (phyi != NULL) {
10699 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10700 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10701 			    phyi->phyint_illv4);
10702 		}
10703 	}
10704 
10705 	/*
10706 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10707 	 * before or after us.
10708 	 */
10709 	ASSERT(IAM_WRITER_ILL(ill));
10710 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10711 
10712 	if (found_sep && orig_ifindex == 0) {
10713 		/* Now see if there is an IPIF with this unit number. */
10714 		for (ipif = ill->ill_ipif; ipif != NULL;
10715 		    ipif = ipif->ipif_next) {
10716 			if (ipif->ipif_id == id) {
10717 				err = EEXIST;
10718 				goto done;
10719 			}
10720 		}
10721 	}
10722 
10723 	/*
10724 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10725 	 * of lo0. We never come here when we plumb lo0:0. It
10726 	 * happens in ipif_lookup_on_name.
10727 	 * The specified unit number is ignored when we create the ipif on a
10728 	 * different interface. However, we save it in ipif_orig_ipifid below so
10729 	 * that the ipif fails back to the right position.
10730 	 */
10731 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10732 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10733 		err = ENOBUFS;
10734 		goto done;
10735 	}
10736 
10737 	/* Return created name with ioctl */
10738 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10739 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10740 	ip1dbg(("created %s\n", lifr->lifr_name));
10741 
10742 	/* Set address */
10743 	sin = (sin_t *)&lifr->lifr_addr;
10744 	if (sin->sin_family != AF_UNSPEC) {
10745 		err = ip_sioctl_addr(ipif, sin, q, mp,
10746 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10747 	}
10748 
10749 	/* Set ifindex and unit number for failback */
10750 	if (err == 0 && orig_ifindex != 0) {
10751 		ipif->ipif_orig_ifindex = orig_ifindex;
10752 		if (found_sep) {
10753 			ipif->ipif_orig_ipifid = id;
10754 		}
10755 	}
10756 
10757 done:
10758 	ipsq_exit(ipsq);
10759 	return (err);
10760 }
10761 
10762 /*
10763  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10764  * interface) delete it based on the IP address (on this physical interface).
10765  * Otherwise delete it based on the ipif_id.
10766  * Also, special handling to allow a removeif of lo0.
10767  */
10768 /* ARGSUSED */
10769 int
10770 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10771     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10772 {
10773 	conn_t		*connp;
10774 	ill_t		*ill = ipif->ipif_ill;
10775 	boolean_t	 success;
10776 	ip_stack_t	*ipst;
10777 
10778 	ipst = CONNQ_TO_IPST(q);
10779 
10780 	ASSERT(q->q_next == NULL);
10781 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10782 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10783 	ASSERT(IAM_WRITER_IPIF(ipif));
10784 
10785 	connp = Q_TO_CONN(q);
10786 	/*
10787 	 * Special case for unplumbing lo0 (the loopback physical interface).
10788 	 * If unplumbing lo0, the incoming address structure has been
10789 	 * initialized to all zeros. When unplumbing lo0, all its logical
10790 	 * interfaces must be removed too.
10791 	 *
10792 	 * Note that this interface may be called to remove a specific
10793 	 * loopback logical interface (eg, lo0:1). But in that case
10794 	 * ipif->ipif_id != 0 so that the code path for that case is the
10795 	 * same as any other interface (meaning it skips the code directly
10796 	 * below).
10797 	 */
10798 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10799 		if (sin->sin_family == AF_UNSPEC &&
10800 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10801 			/*
10802 			 * Mark it condemned. No new ref. will be made to ill.
10803 			 */
10804 			mutex_enter(&ill->ill_lock);
10805 			ill->ill_state_flags |= ILL_CONDEMNED;
10806 			for (ipif = ill->ill_ipif; ipif != NULL;
10807 			    ipif = ipif->ipif_next) {
10808 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10809 			}
10810 			mutex_exit(&ill->ill_lock);
10811 
10812 			ipif = ill->ill_ipif;
10813 			/* unplumb the loopback interface */
10814 			ill_delete(ill);
10815 			mutex_enter(&connp->conn_lock);
10816 			mutex_enter(&ill->ill_lock);
10817 			ASSERT(ill->ill_group == NULL);
10818 
10819 			/* Are any references to this ill active */
10820 			if (ill_is_freeable(ill)) {
10821 				mutex_exit(&ill->ill_lock);
10822 				mutex_exit(&connp->conn_lock);
10823 				ill_delete_tail(ill);
10824 				mi_free(ill);
10825 				return (0);
10826 			}
10827 			success = ipsq_pending_mp_add(connp, ipif,
10828 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10829 			mutex_exit(&connp->conn_lock);
10830 			mutex_exit(&ill->ill_lock);
10831 			if (success)
10832 				return (EINPROGRESS);
10833 			else
10834 				return (EINTR);
10835 		}
10836 	}
10837 
10838 	/*
10839 	 * We are exclusive on the ipsq, so an ill move will be serialized
10840 	 * before or after us.
10841 	 */
10842 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10843 
10844 	if (ipif->ipif_id == 0) {
10845 
10846 		ipsq_t *ipsq;
10847 
10848 		/* Find based on address */
10849 		if (ipif->ipif_isv6) {
10850 			sin6_t *sin6;
10851 
10852 			if (sin->sin_family != AF_INET6)
10853 				return (EAFNOSUPPORT);
10854 
10855 			sin6 = (sin6_t *)sin;
10856 			/* We are a writer, so we should be able to lookup */
10857 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10858 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10859 			if (ipif == NULL) {
10860 				/*
10861 				 * Maybe the address in on another interface in
10862 				 * the same IPMP group? We check this below.
10863 				 */
10864 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10865 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10866 				    ipst);
10867 			}
10868 		} else {
10869 			ipaddr_t addr;
10870 
10871 			if (sin->sin_family != AF_INET)
10872 				return (EAFNOSUPPORT);
10873 
10874 			addr = sin->sin_addr.s_addr;
10875 			/* We are a writer, so we should be able to lookup */
10876 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10877 			    NULL, NULL, NULL, ipst);
10878 			if (ipif == NULL) {
10879 				/*
10880 				 * Maybe the address in on another interface in
10881 				 * the same IPMP group? We check this below.
10882 				 */
10883 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10884 				    NULL, NULL, NULL, NULL, ipst);
10885 			}
10886 		}
10887 		if (ipif == NULL) {
10888 			return (EADDRNOTAVAIL);
10889 		}
10890 
10891 		/*
10892 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10893 		 * lifr_name of the physical interface but with an ip address
10894 		 * lifr_addr of a logical interface plumbed over it.
10895 		 * So update ipsq_current_ipif once ipif points to the
10896 		 * correct interface after doing ipif_lookup_addr().
10897 		 */
10898 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10899 		ASSERT(ipsq != NULL);
10900 
10901 		mutex_enter(&ipsq->ipsq_lock);
10902 		ipsq->ipsq_current_ipif = ipif;
10903 		mutex_exit(&ipsq->ipsq_lock);
10904 
10905 		/*
10906 		 * When the address to be removed is hosted on a different
10907 		 * interface, we check if the interface is in the same IPMP
10908 		 * group as the specified one; if so we proceed with the
10909 		 * removal.
10910 		 * ill->ill_group is NULL when the ill is down, so we have to
10911 		 * compare the group names instead.
10912 		 */
10913 		if (ipif->ipif_ill != ill &&
10914 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10915 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10916 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10917 		    ill->ill_phyint->phyint_groupname) != 0)) {
10918 			ipif_refrele(ipif);
10919 			return (EADDRNOTAVAIL);
10920 		}
10921 
10922 		/* This is a writer */
10923 		ipif_refrele(ipif);
10924 	}
10925 
10926 	/*
10927 	 * Can not delete instance zero since it is tied to the ill.
10928 	 */
10929 	if (ipif->ipif_id == 0)
10930 		return (EBUSY);
10931 
10932 	mutex_enter(&ill->ill_lock);
10933 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10934 	mutex_exit(&ill->ill_lock);
10935 
10936 	ipif_free(ipif);
10937 
10938 	mutex_enter(&connp->conn_lock);
10939 	mutex_enter(&ill->ill_lock);
10940 
10941 
10942 	/* Are any references to this ipif active */
10943 	if (ipif_is_freeable(ipif)) {
10944 		mutex_exit(&ill->ill_lock);
10945 		mutex_exit(&connp->conn_lock);
10946 		ipif_non_duplicate(ipif);
10947 		ipif_down_tail(ipif);
10948 		ipif_free_tail(ipif); /* frees ipif */
10949 		return (0);
10950 	}
10951 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10952 	    IPIF_FREE);
10953 	mutex_exit(&ill->ill_lock);
10954 	mutex_exit(&connp->conn_lock);
10955 	if (success)
10956 		return (EINPROGRESS);
10957 	else
10958 		return (EINTR);
10959 }
10960 
10961 /*
10962  * Restart the removeif ioctl. The refcnt has gone down to 0.
10963  * The ipif is already condemned. So can't find it thru lookups.
10964  */
10965 /* ARGSUSED */
10966 int
10967 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10968     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10969 {
10970 	ill_t *ill = ipif->ipif_ill;
10971 
10972 	ASSERT(IAM_WRITER_IPIF(ipif));
10973 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10974 
10975 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10976 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10977 
10978 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10979 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10980 		ill_delete_tail(ill);
10981 		mi_free(ill);
10982 		return (0);
10983 	}
10984 
10985 	ipif_non_duplicate(ipif);
10986 	ipif_down_tail(ipif);
10987 	ipif_free_tail(ipif);
10988 
10989 	ILL_UNMARK_CHANGING(ill);
10990 	return (0);
10991 }
10992 
10993 /*
10994  * Set the local interface address.
10995  * Allow an address of all zero when the interface is down.
10996  */
10997 /* ARGSUSED */
10998 int
10999 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11000     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11001 {
11002 	int err = 0;
11003 	in6_addr_t v6addr;
11004 	boolean_t need_up = B_FALSE;
11005 
11006 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11007 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11008 
11009 	ASSERT(IAM_WRITER_IPIF(ipif));
11010 
11011 	if (ipif->ipif_isv6) {
11012 		sin6_t *sin6;
11013 		ill_t *ill;
11014 		phyint_t *phyi;
11015 
11016 		if (sin->sin_family != AF_INET6)
11017 			return (EAFNOSUPPORT);
11018 
11019 		sin6 = (sin6_t *)sin;
11020 		v6addr = sin6->sin6_addr;
11021 		ill = ipif->ipif_ill;
11022 		phyi = ill->ill_phyint;
11023 
11024 		/*
11025 		 * Enforce that true multicast interfaces have a link-local
11026 		 * address for logical unit 0.
11027 		 */
11028 		if (ipif->ipif_id == 0 &&
11029 		    (ill->ill_flags & ILLF_MULTICAST) &&
11030 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11031 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11032 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11033 			return (EADDRNOTAVAIL);
11034 		}
11035 
11036 		/*
11037 		 * up interfaces shouldn't have the unspecified address
11038 		 * unless they also have the IPIF_NOLOCAL flags set and
11039 		 * have a subnet assigned.
11040 		 */
11041 		if ((ipif->ipif_flags & IPIF_UP) &&
11042 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11043 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11044 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11045 			return (EADDRNOTAVAIL);
11046 		}
11047 
11048 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11049 			return (EADDRNOTAVAIL);
11050 	} else {
11051 		ipaddr_t addr;
11052 
11053 		if (sin->sin_family != AF_INET)
11054 			return (EAFNOSUPPORT);
11055 
11056 		addr = sin->sin_addr.s_addr;
11057 
11058 		/* Allow 0 as the local address. */
11059 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11060 			return (EADDRNOTAVAIL);
11061 
11062 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11063 	}
11064 
11065 	/*
11066 	 * Even if there is no change we redo things just to rerun
11067 	 * ipif_set_default.
11068 	 */
11069 	if (ipif->ipif_flags & IPIF_UP) {
11070 		/*
11071 		 * Setting a new local address, make sure
11072 		 * we have net and subnet bcast ire's for
11073 		 * the old address if we need them.
11074 		 */
11075 		if (!ipif->ipif_isv6)
11076 			ipif_check_bcast_ires(ipif);
11077 		/*
11078 		 * If the interface is already marked up,
11079 		 * we call ipif_down which will take care
11080 		 * of ditching any IREs that have been set
11081 		 * up based on the old interface address.
11082 		 */
11083 		err = ipif_logical_down(ipif, q, mp);
11084 		if (err == EINPROGRESS)
11085 			return (err);
11086 		ipif_down_tail(ipif);
11087 		need_up = 1;
11088 	}
11089 
11090 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11091 	return (err);
11092 }
11093 
11094 int
11095 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11096     boolean_t need_up)
11097 {
11098 	in6_addr_t v6addr;
11099 	in6_addr_t ov6addr;
11100 	ipaddr_t addr;
11101 	sin6_t	*sin6;
11102 	int	sinlen;
11103 	int	err = 0;
11104 	ill_t	*ill = ipif->ipif_ill;
11105 	boolean_t need_dl_down;
11106 	boolean_t need_arp_down;
11107 	struct iocblk *iocp;
11108 
11109 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11110 
11111 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11112 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11113 	ASSERT(IAM_WRITER_IPIF(ipif));
11114 
11115 	/* Must cancel any pending timer before taking the ill_lock */
11116 	if (ipif->ipif_recovery_id != 0)
11117 		(void) untimeout(ipif->ipif_recovery_id);
11118 	ipif->ipif_recovery_id = 0;
11119 
11120 	if (ipif->ipif_isv6) {
11121 		sin6 = (sin6_t *)sin;
11122 		v6addr = sin6->sin6_addr;
11123 		sinlen = sizeof (struct sockaddr_in6);
11124 	} else {
11125 		addr = sin->sin_addr.s_addr;
11126 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11127 		sinlen = sizeof (struct sockaddr_in);
11128 	}
11129 	mutex_enter(&ill->ill_lock);
11130 	ov6addr = ipif->ipif_v6lcl_addr;
11131 	ipif->ipif_v6lcl_addr = v6addr;
11132 	sctp_update_ipif_addr(ipif, ov6addr);
11133 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11134 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11135 	} else {
11136 		ipif->ipif_v6src_addr = v6addr;
11137 	}
11138 	ipif->ipif_addr_ready = 0;
11139 
11140 	/*
11141 	 * If the interface was previously marked as a duplicate, then since
11142 	 * we've now got a "new" address, it should no longer be considered a
11143 	 * duplicate -- even if the "new" address is the same as the old one.
11144 	 * Note that if all ipifs are down, we may have a pending ARP down
11145 	 * event to handle.  This is because we want to recover from duplicates
11146 	 * and thus delay tearing down ARP until the duplicates have been
11147 	 * removed or disabled.
11148 	 */
11149 	need_dl_down = need_arp_down = B_FALSE;
11150 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11151 		need_arp_down = !need_up;
11152 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11153 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11154 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11155 			need_dl_down = B_TRUE;
11156 		}
11157 	}
11158 
11159 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11160 	    !ill->ill_is_6to4tun) {
11161 		queue_t *wqp = ill->ill_wq;
11162 
11163 		/*
11164 		 * The local address of this interface is a 6to4 address,
11165 		 * check if this interface is in fact a 6to4 tunnel or just
11166 		 * an interface configured with a 6to4 address.  We are only
11167 		 * interested in the former.
11168 		 */
11169 		if (wqp != NULL) {
11170 			while ((wqp->q_next != NULL) &&
11171 			    (wqp->q_next->q_qinfo != NULL) &&
11172 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11173 
11174 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11175 				    == TUN6TO4_MODID) {
11176 					/* set for use in IP */
11177 					ill->ill_is_6to4tun = 1;
11178 					break;
11179 				}
11180 				wqp = wqp->q_next;
11181 			}
11182 		}
11183 	}
11184 
11185 	ipif_set_default(ipif);
11186 
11187 	/*
11188 	 * When publishing an interface address change event, we only notify
11189 	 * the event listeners of the new address.  It is assumed that if they
11190 	 * actively care about the addresses assigned that they will have
11191 	 * already discovered the previous address assigned (if there was one.)
11192 	 *
11193 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11194 	 */
11195 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11196 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11197 		    NE_ADDRESS_CHANGE, sin, sinlen);
11198 	}
11199 
11200 	mutex_exit(&ill->ill_lock);
11201 
11202 	if (need_up) {
11203 		/*
11204 		 * Now bring the interface back up.  If this
11205 		 * is the only IPIF for the ILL, ipif_up
11206 		 * will have to re-bind to the device, so
11207 		 * we may get back EINPROGRESS, in which
11208 		 * case, this IOCTL will get completed in
11209 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11210 		 */
11211 		err = ipif_up(ipif, q, mp);
11212 	}
11213 
11214 	if (need_dl_down)
11215 		ill_dl_down(ill);
11216 	if (need_arp_down)
11217 		ipif_arp_down(ipif);
11218 
11219 	return (err);
11220 }
11221 
11222 
11223 /*
11224  * Restart entry point to restart the address set operation after the
11225  * refcounts have dropped to zero.
11226  */
11227 /* ARGSUSED */
11228 int
11229 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11230     ip_ioctl_cmd_t *ipip, void *ifreq)
11231 {
11232 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11233 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11234 	ASSERT(IAM_WRITER_IPIF(ipif));
11235 	ipif_down_tail(ipif);
11236 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11237 }
11238 
11239 /* ARGSUSED */
11240 int
11241 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11242     ip_ioctl_cmd_t *ipip, void *if_req)
11243 {
11244 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11245 	struct lifreq *lifr = (struct lifreq *)if_req;
11246 
11247 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11248 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11249 	/*
11250 	 * The net mask and address can't change since we have a
11251 	 * reference to the ipif. So no lock is necessary.
11252 	 */
11253 	if (ipif->ipif_isv6) {
11254 		*sin6 = sin6_null;
11255 		sin6->sin6_family = AF_INET6;
11256 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11257 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11258 		lifr->lifr_addrlen =
11259 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11260 	} else {
11261 		*sin = sin_null;
11262 		sin->sin_family = AF_INET;
11263 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11264 		if (ipip->ipi_cmd_type == LIF_CMD) {
11265 			lifr->lifr_addrlen =
11266 			    ip_mask_to_plen(ipif->ipif_net_mask);
11267 		}
11268 	}
11269 	return (0);
11270 }
11271 
11272 /*
11273  * Set the destination address for a pt-pt interface.
11274  */
11275 /* ARGSUSED */
11276 int
11277 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11278     ip_ioctl_cmd_t *ipip, void *if_req)
11279 {
11280 	int err = 0;
11281 	in6_addr_t v6addr;
11282 	boolean_t need_up = B_FALSE;
11283 
11284 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11285 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11286 	ASSERT(IAM_WRITER_IPIF(ipif));
11287 
11288 	if (ipif->ipif_isv6) {
11289 		sin6_t *sin6;
11290 
11291 		if (sin->sin_family != AF_INET6)
11292 			return (EAFNOSUPPORT);
11293 
11294 		sin6 = (sin6_t *)sin;
11295 		v6addr = sin6->sin6_addr;
11296 
11297 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11298 			return (EADDRNOTAVAIL);
11299 	} else {
11300 		ipaddr_t addr;
11301 
11302 		if (sin->sin_family != AF_INET)
11303 			return (EAFNOSUPPORT);
11304 
11305 		addr = sin->sin_addr.s_addr;
11306 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11307 			return (EADDRNOTAVAIL);
11308 
11309 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11310 	}
11311 
11312 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11313 		return (0);	/* No change */
11314 
11315 	if (ipif->ipif_flags & IPIF_UP) {
11316 		/*
11317 		 * If the interface is already marked up,
11318 		 * we call ipif_down which will take care
11319 		 * of ditching any IREs that have been set
11320 		 * up based on the old pp dst address.
11321 		 */
11322 		err = ipif_logical_down(ipif, q, mp);
11323 		if (err == EINPROGRESS)
11324 			return (err);
11325 		ipif_down_tail(ipif);
11326 		need_up = B_TRUE;
11327 	}
11328 	/*
11329 	 * could return EINPROGRESS. If so ioctl will complete in
11330 	 * ip_rput_dlpi_writer
11331 	 */
11332 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11333 	return (err);
11334 }
11335 
11336 static int
11337 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11338     boolean_t need_up)
11339 {
11340 	in6_addr_t v6addr;
11341 	ill_t	*ill = ipif->ipif_ill;
11342 	int	err = 0;
11343 	boolean_t need_dl_down;
11344 	boolean_t need_arp_down;
11345 
11346 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11347 	    ipif->ipif_id, (void *)ipif));
11348 
11349 	/* Must cancel any pending timer before taking the ill_lock */
11350 	if (ipif->ipif_recovery_id != 0)
11351 		(void) untimeout(ipif->ipif_recovery_id);
11352 	ipif->ipif_recovery_id = 0;
11353 
11354 	if (ipif->ipif_isv6) {
11355 		sin6_t *sin6;
11356 
11357 		sin6 = (sin6_t *)sin;
11358 		v6addr = sin6->sin6_addr;
11359 	} else {
11360 		ipaddr_t addr;
11361 
11362 		addr = sin->sin_addr.s_addr;
11363 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11364 	}
11365 	mutex_enter(&ill->ill_lock);
11366 	/* Set point to point destination address. */
11367 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11368 		/*
11369 		 * Allow this as a means of creating logical
11370 		 * pt-pt interfaces on top of e.g. an Ethernet.
11371 		 * XXX Undocumented HACK for testing.
11372 		 * pt-pt interfaces are created with NUD disabled.
11373 		 */
11374 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11375 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11376 		if (ipif->ipif_isv6)
11377 			ill->ill_flags |= ILLF_NONUD;
11378 	}
11379 
11380 	/*
11381 	 * If the interface was previously marked as a duplicate, then since
11382 	 * we've now got a "new" address, it should no longer be considered a
11383 	 * duplicate -- even if the "new" address is the same as the old one.
11384 	 * Note that if all ipifs are down, we may have a pending ARP down
11385 	 * event to handle.
11386 	 */
11387 	need_dl_down = need_arp_down = B_FALSE;
11388 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11389 		need_arp_down = !need_up;
11390 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11391 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11392 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11393 			need_dl_down = B_TRUE;
11394 		}
11395 	}
11396 
11397 	/* Set the new address. */
11398 	ipif->ipif_v6pp_dst_addr = v6addr;
11399 	/* Make sure subnet tracks pp_dst */
11400 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11401 	mutex_exit(&ill->ill_lock);
11402 
11403 	if (need_up) {
11404 		/*
11405 		 * Now bring the interface back up.  If this
11406 		 * is the only IPIF for the ILL, ipif_up
11407 		 * will have to re-bind to the device, so
11408 		 * we may get back EINPROGRESS, in which
11409 		 * case, this IOCTL will get completed in
11410 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11411 		 */
11412 		err = ipif_up(ipif, q, mp);
11413 	}
11414 
11415 	if (need_dl_down)
11416 		ill_dl_down(ill);
11417 
11418 	if (need_arp_down)
11419 		ipif_arp_down(ipif);
11420 	return (err);
11421 }
11422 
11423 /*
11424  * Restart entry point to restart the dstaddress set operation after the
11425  * refcounts have dropped to zero.
11426  */
11427 /* ARGSUSED */
11428 int
11429 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11430     ip_ioctl_cmd_t *ipip, void *ifreq)
11431 {
11432 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11433 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11434 	ipif_down_tail(ipif);
11435 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11436 }
11437 
11438 /* ARGSUSED */
11439 int
11440 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11441     ip_ioctl_cmd_t *ipip, void *if_req)
11442 {
11443 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11444 
11445 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11446 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11447 	/*
11448 	 * Get point to point destination address. The addresses can't
11449 	 * change since we hold a reference to the ipif.
11450 	 */
11451 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11452 		return (EADDRNOTAVAIL);
11453 
11454 	if (ipif->ipif_isv6) {
11455 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11456 		*sin6 = sin6_null;
11457 		sin6->sin6_family = AF_INET6;
11458 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11459 	} else {
11460 		*sin = sin_null;
11461 		sin->sin_family = AF_INET;
11462 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11463 	}
11464 	return (0);
11465 }
11466 
11467 /*
11468  * part of ipmp, make this func return the active/inactive state and
11469  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11470  */
11471 /*
11472  * This function either sets or clears the IFF_INACTIVE flag.
11473  *
11474  * As long as there are some addresses or multicast memberships on the
11475  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11476  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11477  * will be used for outbound packets.
11478  *
11479  * Caller needs to verify the validity of setting IFF_INACTIVE.
11480  */
11481 static void
11482 phyint_inactive(phyint_t *phyi)
11483 {
11484 	ill_t *ill_v4;
11485 	ill_t *ill_v6;
11486 	ipif_t *ipif;
11487 	ilm_t *ilm;
11488 
11489 	ill_v4 = phyi->phyint_illv4;
11490 	ill_v6 = phyi->phyint_illv6;
11491 
11492 	/*
11493 	 * No need for a lock while traversing the list since iam
11494 	 * a writer
11495 	 */
11496 	if (ill_v4 != NULL) {
11497 		ASSERT(IAM_WRITER_ILL(ill_v4));
11498 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11499 		    ipif = ipif->ipif_next) {
11500 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11501 				mutex_enter(&phyi->phyint_lock);
11502 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11503 				mutex_exit(&phyi->phyint_lock);
11504 				return;
11505 			}
11506 		}
11507 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11508 		    ilm = ilm->ilm_next) {
11509 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11510 				mutex_enter(&phyi->phyint_lock);
11511 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11512 				mutex_exit(&phyi->phyint_lock);
11513 				return;
11514 			}
11515 		}
11516 	}
11517 	if (ill_v6 != NULL) {
11518 		ill_v6 = phyi->phyint_illv6;
11519 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11520 		    ipif = ipif->ipif_next) {
11521 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11522 				mutex_enter(&phyi->phyint_lock);
11523 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11524 				mutex_exit(&phyi->phyint_lock);
11525 				return;
11526 			}
11527 		}
11528 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11529 		    ilm = ilm->ilm_next) {
11530 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11531 				mutex_enter(&phyi->phyint_lock);
11532 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11533 				mutex_exit(&phyi->phyint_lock);
11534 				return;
11535 			}
11536 		}
11537 	}
11538 	mutex_enter(&phyi->phyint_lock);
11539 	phyi->phyint_flags |= PHYI_INACTIVE;
11540 	mutex_exit(&phyi->phyint_lock);
11541 }
11542 
11543 /*
11544  * This function is called only when the phyint flags change. Currently
11545  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11546  * that we can select a good ill.
11547  */
11548 static void
11549 ip_redo_nomination(phyint_t *phyi)
11550 {
11551 	ill_t *ill_v4;
11552 
11553 	ill_v4 = phyi->phyint_illv4;
11554 
11555 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11556 		ASSERT(IAM_WRITER_ILL(ill_v4));
11557 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11558 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11559 	}
11560 }
11561 
11562 /*
11563  * Heuristic to check if ill is INACTIVE.
11564  * Checks if ill has an ipif with an usable ip address.
11565  *
11566  * Return values:
11567  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11568  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11569  */
11570 static boolean_t
11571 ill_is_inactive(ill_t *ill)
11572 {
11573 	ipif_t *ipif;
11574 
11575 	/* Check whether it is in an IPMP group */
11576 	if (ill->ill_phyint->phyint_groupname == NULL)
11577 		return (B_FALSE);
11578 
11579 	if (ill->ill_ipif_up_count == 0)
11580 		return (B_TRUE);
11581 
11582 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11583 		uint64_t flags = ipif->ipif_flags;
11584 
11585 		/*
11586 		 * This ipif is usable if it is IPIF_UP and not a
11587 		 * dedicated test address.  A dedicated test address
11588 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11589 		 * (note in particular that V6 test addresses are
11590 		 * link-local data addresses and thus are marked
11591 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11592 		 */
11593 		if ((flags & IPIF_UP) &&
11594 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11595 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11596 			return (B_FALSE);
11597 	}
11598 	return (B_TRUE);
11599 }
11600 
11601 /*
11602  * Set interface flags.
11603  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11604  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11605  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11606  *
11607  * NOTE : We really don't enforce that ipif_id zero should be used
11608  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11609  *	  is because applications generally does SICGLIFFLAGS and
11610  *	  ORs in the new flags (that affects the logical) and does a
11611  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11612  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11613  *	  flags that will be turned on is correct with respect to
11614  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11615  */
11616 /* ARGSUSED */
11617 int
11618 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11619     ip_ioctl_cmd_t *ipip, void *if_req)
11620 {
11621 	uint64_t turn_on;
11622 	uint64_t turn_off;
11623 	int	err;
11624 	phyint_t *phyi;
11625 	ill_t *ill;
11626 	uint64_t intf_flags;
11627 	boolean_t phyint_flags_modified = B_FALSE;
11628 	uint64_t flags;
11629 	struct ifreq *ifr;
11630 	struct lifreq *lifr;
11631 	boolean_t set_linklocal = B_FALSE;
11632 	boolean_t zero_source = B_FALSE;
11633 	ip_stack_t *ipst;
11634 
11635 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11636 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11637 
11638 	ASSERT(IAM_WRITER_IPIF(ipif));
11639 
11640 	ill = ipif->ipif_ill;
11641 	phyi = ill->ill_phyint;
11642 	ipst = ill->ill_ipst;
11643 
11644 	if (ipip->ipi_cmd_type == IF_CMD) {
11645 		ifr = (struct ifreq *)if_req;
11646 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11647 	} else {
11648 		lifr = (struct lifreq *)if_req;
11649 		flags = lifr->lifr_flags;
11650 	}
11651 
11652 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11653 
11654 	/*
11655 	 * Have the flags been set correctly until now?
11656 	 */
11657 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11658 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11659 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11660 	/*
11661 	 * Compare the new flags to the old, and partition
11662 	 * into those coming on and those going off.
11663 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11664 	 */
11665 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11666 		flags |= intf_flags & ~0xFFFF;
11667 
11668 	/*
11669 	 * First check which bits will change and then which will
11670 	 * go on and off
11671 	 */
11672 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11673 	if (!turn_on)
11674 		return (0);	/* No change */
11675 
11676 	turn_off = intf_flags & turn_on;
11677 	turn_on ^= turn_off;
11678 	err = 0;
11679 
11680 	/*
11681 	 * Don't allow any bits belonging to the logical interface
11682 	 * to be set or cleared on the replacement ipif that was
11683 	 * created temporarily during a MOVE.
11684 	 */
11685 	if (ipif->ipif_replace_zero &&
11686 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11687 		return (EINVAL);
11688 	}
11689 
11690 	/*
11691 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11692 	 * IPv6 interfaces.
11693 	 */
11694 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11695 		return (EINVAL);
11696 
11697 	/*
11698 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11699 	 */
11700 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11701 		return (EINVAL);
11702 
11703 	/*
11704 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11705 	 * interfaces.  It makes no sense in that context.
11706 	 */
11707 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11708 		return (EINVAL);
11709 
11710 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11711 		zero_source = B_TRUE;
11712 
11713 	/*
11714 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11715 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11716 	 * If the link local address isn't set, and can be set, it will get
11717 	 * set later on in this function.
11718 	 */
11719 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11720 	    (flags & IFF_UP) && !zero_source &&
11721 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11722 		if (ipif_cant_setlinklocal(ipif))
11723 			return (EINVAL);
11724 		set_linklocal = B_TRUE;
11725 	}
11726 
11727 	/*
11728 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11729 	 * same time. No need to grab ill_g_usesrc_lock here, see
11730 	 * synchronization notes in ip.c
11731 	 */
11732 	if (turn_on & PHYI_STANDBY &&
11733 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11734 		return (EINVAL);
11735 	}
11736 
11737 	/*
11738 	 * If we modify physical interface flags, we'll potentially need to
11739 	 * send up two routing socket messages for the changes (one for the
11740 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11741 	 */
11742 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11743 		phyint_flags_modified = B_TRUE;
11744 
11745 	/*
11746 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11747 	 * we need to flush the IRE_CACHES belonging to this ill.
11748 	 * We handle this case here without doing the DOWN/UP dance
11749 	 * like it is done for other flags. If some other flags are
11750 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11751 	 * below will handle it by bringing it down and then
11752 	 * bringing it UP.
11753 	 */
11754 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11755 		ill_t *ill_v4, *ill_v6;
11756 
11757 		ill_v4 = phyi->phyint_illv4;
11758 		ill_v6 = phyi->phyint_illv6;
11759 
11760 		/*
11761 		 * First set the INACTIVE flag if needed. Then delete the ires.
11762 		 * ire_add will atomically prevent creating new IRE_CACHEs
11763 		 * unless hidden flag is set.
11764 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11765 		 */
11766 		if ((turn_on & PHYI_FAILED) &&
11767 		    ((intf_flags & PHYI_STANDBY) ||
11768 		    !ipst->ips_ipmp_enable_failback)) {
11769 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11770 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11771 		}
11772 		if ((turn_off & PHYI_FAILED) &&
11773 		    ((intf_flags & PHYI_STANDBY) ||
11774 		    (!ipst->ips_ipmp_enable_failback &&
11775 		    ill_is_inactive(ill)))) {
11776 			phyint_inactive(phyi);
11777 		}
11778 
11779 		if (turn_on & PHYI_STANDBY) {
11780 			/*
11781 			 * We implicitly set INACTIVE only when STANDBY is set.
11782 			 * INACTIVE is also set on non-STANDBY phyint when user
11783 			 * disables FAILBACK using configuration file.
11784 			 * Do not allow STANDBY to be set on such INACTIVE
11785 			 * phyint
11786 			 */
11787 			if (phyi->phyint_flags & PHYI_INACTIVE)
11788 				return (EINVAL);
11789 			if (!(phyi->phyint_flags & PHYI_FAILED))
11790 				phyint_inactive(phyi);
11791 		}
11792 		if (turn_off & PHYI_STANDBY) {
11793 			if (ipst->ips_ipmp_enable_failback) {
11794 				/*
11795 				 * Reset PHYI_INACTIVE.
11796 				 */
11797 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11798 			} else if (ill_is_inactive(ill) &&
11799 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11800 				/*
11801 				 * Need to set INACTIVE, when user sets
11802 				 * STANDBY on a non-STANDBY phyint and
11803 				 * later resets STANDBY
11804 				 */
11805 				phyint_inactive(phyi);
11806 			}
11807 		}
11808 		/*
11809 		 * We should always send up a message so that the
11810 		 * daemons come to know of it. Note that the zeroth
11811 		 * interface can be down and the check below for IPIF_UP
11812 		 * will not make sense as we are actually setting
11813 		 * a phyint flag here. We assume that the ipif used
11814 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11815 		 * send up any message for non-zero ipifs).
11816 		 */
11817 		phyint_flags_modified = B_TRUE;
11818 
11819 		if (ill_v4 != NULL) {
11820 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11821 			    IRE_CACHE, ill_stq_cache_delete,
11822 			    (char *)ill_v4, ill_v4);
11823 			illgrp_reset_schednext(ill_v4);
11824 		}
11825 		if (ill_v6 != NULL) {
11826 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11827 			    IRE_CACHE, ill_stq_cache_delete,
11828 			    (char *)ill_v6, ill_v6);
11829 			illgrp_reset_schednext(ill_v6);
11830 		}
11831 	}
11832 
11833 	/*
11834 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11835 	 * status of the interface and, if the interface is part of an IPMP
11836 	 * group, all other interfaces that are part of the same IPMP
11837 	 * group.
11838 	 */
11839 	if ((turn_on | turn_off) & ILLF_ROUTER)
11840 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11841 
11842 	/*
11843 	 * If the interface is not UP and we are not going to
11844 	 * bring it UP, record the flags and return. When the
11845 	 * interface comes UP later, the right actions will be
11846 	 * taken.
11847 	 */
11848 	if (!(ipif->ipif_flags & IPIF_UP) &&
11849 	    !(turn_on & IPIF_UP)) {
11850 		/* Record new flags in their respective places. */
11851 		mutex_enter(&ill->ill_lock);
11852 		mutex_enter(&ill->ill_phyint->phyint_lock);
11853 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11854 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11855 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11856 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11857 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11858 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11859 		mutex_exit(&ill->ill_lock);
11860 		mutex_exit(&ill->ill_phyint->phyint_lock);
11861 
11862 		/*
11863 		 * We do the broadcast and nomination here rather
11864 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11865 		 * the case of FAILBACK from INACTIVE standby to the
11866 		 * interface that has been repaired, PHYI_FAILED has not
11867 		 * been cleared yet. If there are only two interfaces in
11868 		 * that group, all we have is a FAILED and INACTIVE
11869 		 * interface. If we do the nomination soon after a failback,
11870 		 * the broadcast nomination code would select the
11871 		 * INACTIVE interface for receiving broadcasts as FAILED is
11872 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11873 		 * receive broadcast packets, we need to redo nomination
11874 		 * when the FAILED is cleared here. Thus, in general we
11875 		 * always do the nomination here for FAILED, STANDBY
11876 		 * and OFFLINE.
11877 		 */
11878 		if (((turn_on | turn_off) &
11879 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11880 			ip_redo_nomination(phyi);
11881 		}
11882 		if (phyint_flags_modified) {
11883 			if (phyi->phyint_illv4 != NULL) {
11884 				ip_rts_ifmsg(phyi->phyint_illv4->
11885 				    ill_ipif);
11886 			}
11887 			if (phyi->phyint_illv6 != NULL) {
11888 				ip_rts_ifmsg(phyi->phyint_illv6->
11889 				    ill_ipif);
11890 			}
11891 		}
11892 		return (0);
11893 	} else if (set_linklocal || zero_source) {
11894 		mutex_enter(&ill->ill_lock);
11895 		if (set_linklocal)
11896 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11897 		if (zero_source)
11898 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11899 		mutex_exit(&ill->ill_lock);
11900 	}
11901 
11902 	/*
11903 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11904 	 * or point-to-point interfaces with an unspecified destination. We do
11905 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11906 	 * have a subnet assigned, which is how in.ndpd currently manages its
11907 	 * onlink prefix list when no addresses are configured with those
11908 	 * prefixes.
11909 	 */
11910 	if (ipif->ipif_isv6 &&
11911 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11912 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11913 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11914 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11915 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11916 		return (EINVAL);
11917 	}
11918 
11919 	/*
11920 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11921 	 * from being brought up.
11922 	 */
11923 	if (!ipif->ipif_isv6 &&
11924 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11925 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11926 		return (EINVAL);
11927 	}
11928 
11929 	/*
11930 	 * The only flag changes that we currently take specific action on
11931 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11932 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11933 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11934 	 * the flags and bringing it back up again.
11935 	 */
11936 	if ((turn_on|turn_off) &
11937 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11938 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11939 		/*
11940 		 * Taking this ipif down, make sure we have
11941 		 * valid net and subnet bcast ire's for other
11942 		 * logical interfaces, if we need them.
11943 		 */
11944 		if (!ipif->ipif_isv6)
11945 			ipif_check_bcast_ires(ipif);
11946 
11947 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11948 		    !(turn_off & IPIF_UP)) {
11949 			if (ipif->ipif_flags & IPIF_UP)
11950 				ill->ill_logical_down = 1;
11951 			turn_on &= ~IPIF_UP;
11952 		}
11953 		err = ipif_down(ipif, q, mp);
11954 		ip1dbg(("ipif_down returns %d err ", err));
11955 		if (err == EINPROGRESS)
11956 			return (err);
11957 		ipif_down_tail(ipif);
11958 	}
11959 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11960 }
11961 
11962 static int
11963 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11964 {
11965 	ill_t	*ill;
11966 	phyint_t *phyi;
11967 	uint64_t turn_on;
11968 	uint64_t turn_off;
11969 	uint64_t intf_flags;
11970 	boolean_t phyint_flags_modified = B_FALSE;
11971 	int	err = 0;
11972 	boolean_t set_linklocal = B_FALSE;
11973 	boolean_t zero_source = B_FALSE;
11974 
11975 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11976 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11977 
11978 	ASSERT(IAM_WRITER_IPIF(ipif));
11979 
11980 	ill = ipif->ipif_ill;
11981 	phyi = ill->ill_phyint;
11982 
11983 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11984 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11985 
11986 	turn_off = intf_flags & turn_on;
11987 	turn_on ^= turn_off;
11988 
11989 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11990 		phyint_flags_modified = B_TRUE;
11991 
11992 	/*
11993 	 * Now we change the flags. Track current value of
11994 	 * other flags in their respective places.
11995 	 */
11996 	mutex_enter(&ill->ill_lock);
11997 	mutex_enter(&phyi->phyint_lock);
11998 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11999 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12000 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12001 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12002 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12003 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12004 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12005 		set_linklocal = B_TRUE;
12006 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12007 	}
12008 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12009 		zero_source = B_TRUE;
12010 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12011 	}
12012 	mutex_exit(&ill->ill_lock);
12013 	mutex_exit(&phyi->phyint_lock);
12014 
12015 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12016 		ip_redo_nomination(phyi);
12017 
12018 	if (set_linklocal)
12019 		(void) ipif_setlinklocal(ipif);
12020 
12021 	if (zero_source)
12022 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12023 	else
12024 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12025 
12026 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
12027 		/*
12028 		 * XXX ipif_up really does not know whether a phyint flags
12029 		 * was modified or not. So, it sends up information on
12030 		 * only one routing sockets message. As we don't bring up
12031 		 * the interface and also set STANDBY/FAILED simultaneously
12032 		 * it should be okay.
12033 		 */
12034 		err = ipif_up(ipif, q, mp);
12035 	} else {
12036 		/*
12037 		 * Make sure routing socket sees all changes to the flags.
12038 		 * ipif_up_done* handles this when we use ipif_up.
12039 		 */
12040 		if (phyint_flags_modified) {
12041 			if (phyi->phyint_illv4 != NULL) {
12042 				ip_rts_ifmsg(phyi->phyint_illv4->
12043 				    ill_ipif);
12044 			}
12045 			if (phyi->phyint_illv6 != NULL) {
12046 				ip_rts_ifmsg(phyi->phyint_illv6->
12047 				    ill_ipif);
12048 			}
12049 		} else {
12050 			ip_rts_ifmsg(ipif);
12051 		}
12052 		/*
12053 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12054 		 * this in need_up case.
12055 		 */
12056 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12057 	}
12058 	return (err);
12059 }
12060 
12061 /*
12062  * Restart the flags operation now that the refcounts have dropped to zero.
12063  */
12064 /* ARGSUSED */
12065 int
12066 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12067     ip_ioctl_cmd_t *ipip, void *if_req)
12068 {
12069 	uint64_t flags;
12070 	struct ifreq *ifr = if_req;
12071 	struct lifreq *lifr = if_req;
12072 
12073 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12074 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12075 
12076 	ipif_down_tail(ipif);
12077 	if (ipip->ipi_cmd_type == IF_CMD) {
12078 		/* cast to uint16_t prevents unwanted sign extension */
12079 		flags = (uint16_t)ifr->ifr_flags;
12080 	} else {
12081 		flags = lifr->lifr_flags;
12082 	}
12083 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
12084 }
12085 
12086 /*
12087  * Can operate on either a module or a driver queue.
12088  */
12089 /* ARGSUSED */
12090 int
12091 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12092     ip_ioctl_cmd_t *ipip, void *if_req)
12093 {
12094 	/*
12095 	 * Has the flags been set correctly till now ?
12096 	 */
12097 	ill_t *ill = ipif->ipif_ill;
12098 	phyint_t *phyi = ill->ill_phyint;
12099 
12100 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12101 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12102 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12103 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12104 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12105 
12106 	/*
12107 	 * Need a lock since some flags can be set even when there are
12108 	 * references to the ipif.
12109 	 */
12110 	mutex_enter(&ill->ill_lock);
12111 	if (ipip->ipi_cmd_type == IF_CMD) {
12112 		struct ifreq *ifr = (struct ifreq *)if_req;
12113 
12114 		/* Get interface flags (low 16 only). */
12115 		ifr->ifr_flags = ((ipif->ipif_flags |
12116 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12117 	} else {
12118 		struct lifreq *lifr = (struct lifreq *)if_req;
12119 
12120 		/* Get interface flags. */
12121 		lifr->lifr_flags = ipif->ipif_flags |
12122 		    ill->ill_flags | phyi->phyint_flags;
12123 	}
12124 	mutex_exit(&ill->ill_lock);
12125 	return (0);
12126 }
12127 
12128 /* ARGSUSED */
12129 int
12130 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12131     ip_ioctl_cmd_t *ipip, void *if_req)
12132 {
12133 	int mtu;
12134 	int ip_min_mtu;
12135 	struct ifreq	*ifr;
12136 	struct lifreq *lifr;
12137 	ire_t	*ire;
12138 	ip_stack_t *ipst;
12139 
12140 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12141 	    ipif->ipif_id, (void *)ipif));
12142 	if (ipip->ipi_cmd_type == IF_CMD) {
12143 		ifr = (struct ifreq *)if_req;
12144 		mtu = ifr->ifr_metric;
12145 	} else {
12146 		lifr = (struct lifreq *)if_req;
12147 		mtu = lifr->lifr_mtu;
12148 	}
12149 
12150 	if (ipif->ipif_isv6)
12151 		ip_min_mtu = IPV6_MIN_MTU;
12152 	else
12153 		ip_min_mtu = IP_MIN_MTU;
12154 
12155 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12156 		return (EINVAL);
12157 
12158 	/*
12159 	 * Change the MTU size in all relevant ire's.
12160 	 * Mtu change Vs. new ire creation - protocol below.
12161 	 * First change ipif_mtu and the ire_max_frag of the
12162 	 * interface ire. Then do an ire walk and change the
12163 	 * ire_max_frag of all affected ires. During ire_add
12164 	 * under the bucket lock, set the ire_max_frag of the
12165 	 * new ire being created from the ipif/ire from which
12166 	 * it is being derived. If an mtu change happens after
12167 	 * the ire is added, the new ire will be cleaned up.
12168 	 * Conversely if the mtu change happens before the ire
12169 	 * is added, ire_add will see the new value of the mtu.
12170 	 */
12171 	ipif->ipif_mtu = mtu;
12172 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12173 
12174 	if (ipif->ipif_isv6)
12175 		ire = ipif_to_ire_v6(ipif);
12176 	else
12177 		ire = ipif_to_ire(ipif);
12178 	if (ire != NULL) {
12179 		ire->ire_max_frag = ipif->ipif_mtu;
12180 		ire_refrele(ire);
12181 	}
12182 	ipst = ipif->ipif_ill->ill_ipst;
12183 	if (ipif->ipif_flags & IPIF_UP) {
12184 		if (ipif->ipif_isv6)
12185 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12186 			    ipst);
12187 		else
12188 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12189 			    ipst);
12190 	}
12191 	/* Update the MTU in SCTP's list */
12192 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12193 	return (0);
12194 }
12195 
12196 /* Get interface MTU. */
12197 /* ARGSUSED */
12198 int
12199 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12200 	ip_ioctl_cmd_t *ipip, void *if_req)
12201 {
12202 	struct ifreq	*ifr;
12203 	struct lifreq	*lifr;
12204 
12205 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12206 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12207 	if (ipip->ipi_cmd_type == IF_CMD) {
12208 		ifr = (struct ifreq *)if_req;
12209 		ifr->ifr_metric = ipif->ipif_mtu;
12210 	} else {
12211 		lifr = (struct lifreq *)if_req;
12212 		lifr->lifr_mtu = ipif->ipif_mtu;
12213 	}
12214 	return (0);
12215 }
12216 
12217 /* Set interface broadcast address. */
12218 /* ARGSUSED2 */
12219 int
12220 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12221 	ip_ioctl_cmd_t *ipip, void *if_req)
12222 {
12223 	ipaddr_t addr;
12224 	ire_t	*ire;
12225 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12226 
12227 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12228 	    ipif->ipif_id));
12229 
12230 	ASSERT(IAM_WRITER_IPIF(ipif));
12231 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12232 		return (EADDRNOTAVAIL);
12233 
12234 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12235 
12236 	if (sin->sin_family != AF_INET)
12237 		return (EAFNOSUPPORT);
12238 
12239 	addr = sin->sin_addr.s_addr;
12240 	if (ipif->ipif_flags & IPIF_UP) {
12241 		/*
12242 		 * If we are already up, make sure the new
12243 		 * broadcast address makes sense.  If it does,
12244 		 * there should be an IRE for it already.
12245 		 * Don't match on ipif, only on the ill
12246 		 * since we are sharing these now. Don't use
12247 		 * MATCH_IRE_ILL_GROUP as we are looking for
12248 		 * the broadcast ire on this ill and each ill
12249 		 * in the group has its own broadcast ire.
12250 		 */
12251 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12252 		    ipif, ALL_ZONES, NULL,
12253 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12254 		if (ire == NULL) {
12255 			return (EINVAL);
12256 		} else {
12257 			ire_refrele(ire);
12258 		}
12259 	}
12260 	/*
12261 	 * Changing the broadcast addr for this ipif.
12262 	 * Make sure we have valid net and subnet bcast
12263 	 * ire's for other logical interfaces, if needed.
12264 	 */
12265 	if (addr != ipif->ipif_brd_addr)
12266 		ipif_check_bcast_ires(ipif);
12267 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12268 	return (0);
12269 }
12270 
12271 /* Get interface broadcast address. */
12272 /* ARGSUSED */
12273 int
12274 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12275     ip_ioctl_cmd_t *ipip, void *if_req)
12276 {
12277 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12278 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12279 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12280 		return (EADDRNOTAVAIL);
12281 
12282 	/* IPIF_BROADCAST not possible with IPv6 */
12283 	ASSERT(!ipif->ipif_isv6);
12284 	*sin = sin_null;
12285 	sin->sin_family = AF_INET;
12286 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12287 	return (0);
12288 }
12289 
12290 /*
12291  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12292  */
12293 /* ARGSUSED */
12294 int
12295 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12296     ip_ioctl_cmd_t *ipip, void *if_req)
12297 {
12298 	int err = 0;
12299 	in6_addr_t v6mask;
12300 
12301 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12302 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12303 
12304 	ASSERT(IAM_WRITER_IPIF(ipif));
12305 
12306 	if (ipif->ipif_isv6) {
12307 		sin6_t *sin6;
12308 
12309 		if (sin->sin_family != AF_INET6)
12310 			return (EAFNOSUPPORT);
12311 
12312 		sin6 = (sin6_t *)sin;
12313 		v6mask = sin6->sin6_addr;
12314 	} else {
12315 		ipaddr_t mask;
12316 
12317 		if (sin->sin_family != AF_INET)
12318 			return (EAFNOSUPPORT);
12319 
12320 		mask = sin->sin_addr.s_addr;
12321 		V4MASK_TO_V6(mask, v6mask);
12322 	}
12323 
12324 	/*
12325 	 * No big deal if the interface isn't already up, or the mask
12326 	 * isn't really changing, or this is pt-pt.
12327 	 */
12328 	if (!(ipif->ipif_flags & IPIF_UP) ||
12329 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12330 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12331 		ipif->ipif_v6net_mask = v6mask;
12332 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12333 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12334 			    ipif->ipif_v6net_mask,
12335 			    ipif->ipif_v6subnet);
12336 		}
12337 		return (0);
12338 	}
12339 	/*
12340 	 * Make sure we have valid net and subnet broadcast ire's
12341 	 * for the old netmask, if needed by other logical interfaces.
12342 	 */
12343 	if (!ipif->ipif_isv6)
12344 		ipif_check_bcast_ires(ipif);
12345 
12346 	err = ipif_logical_down(ipif, q, mp);
12347 	if (err == EINPROGRESS)
12348 		return (err);
12349 	ipif_down_tail(ipif);
12350 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12351 	return (err);
12352 }
12353 
12354 static int
12355 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12356 {
12357 	in6_addr_t v6mask;
12358 	int err = 0;
12359 
12360 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12361 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12362 
12363 	if (ipif->ipif_isv6) {
12364 		sin6_t *sin6;
12365 
12366 		sin6 = (sin6_t *)sin;
12367 		v6mask = sin6->sin6_addr;
12368 	} else {
12369 		ipaddr_t mask;
12370 
12371 		mask = sin->sin_addr.s_addr;
12372 		V4MASK_TO_V6(mask, v6mask);
12373 	}
12374 
12375 	ipif->ipif_v6net_mask = v6mask;
12376 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12377 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12378 		    ipif->ipif_v6subnet);
12379 	}
12380 	err = ipif_up(ipif, q, mp);
12381 
12382 	if (err == 0 || err == EINPROGRESS) {
12383 		/*
12384 		 * The interface must be DL_BOUND if this packet has to
12385 		 * go out on the wire. Since we only go through a logical
12386 		 * down and are bound with the driver during an internal
12387 		 * down/up that is satisfied.
12388 		 */
12389 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12390 			/* Potentially broadcast an address mask reply. */
12391 			ipif_mask_reply(ipif);
12392 		}
12393 	}
12394 	return (err);
12395 }
12396 
12397 /* ARGSUSED */
12398 int
12399 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12400     ip_ioctl_cmd_t *ipip, void *if_req)
12401 {
12402 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12403 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12404 	ipif_down_tail(ipif);
12405 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12406 }
12407 
12408 /* Get interface net mask. */
12409 /* ARGSUSED */
12410 int
12411 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12412     ip_ioctl_cmd_t *ipip, void *if_req)
12413 {
12414 	struct lifreq *lifr = (struct lifreq *)if_req;
12415 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12416 
12417 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12418 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12419 
12420 	/*
12421 	 * net mask can't change since we have a reference to the ipif.
12422 	 */
12423 	if (ipif->ipif_isv6) {
12424 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12425 		*sin6 = sin6_null;
12426 		sin6->sin6_family = AF_INET6;
12427 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12428 		lifr->lifr_addrlen =
12429 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12430 	} else {
12431 		*sin = sin_null;
12432 		sin->sin_family = AF_INET;
12433 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12434 		if (ipip->ipi_cmd_type == LIF_CMD) {
12435 			lifr->lifr_addrlen =
12436 			    ip_mask_to_plen(ipif->ipif_net_mask);
12437 		}
12438 	}
12439 	return (0);
12440 }
12441 
12442 /* ARGSUSED */
12443 int
12444 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12445     ip_ioctl_cmd_t *ipip, void *if_req)
12446 {
12447 
12448 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12449 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12450 	/*
12451 	 * Set interface metric.  We don't use this for
12452 	 * anything but we keep track of it in case it is
12453 	 * important to routing applications or such.
12454 	 */
12455 	if (ipip->ipi_cmd_type == IF_CMD) {
12456 		struct ifreq    *ifr;
12457 
12458 		ifr = (struct ifreq *)if_req;
12459 		ipif->ipif_metric = ifr->ifr_metric;
12460 	} else {
12461 		struct lifreq   *lifr;
12462 
12463 		lifr = (struct lifreq *)if_req;
12464 		ipif->ipif_metric = lifr->lifr_metric;
12465 	}
12466 	return (0);
12467 }
12468 
12469 /* ARGSUSED */
12470 int
12471 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12472     ip_ioctl_cmd_t *ipip, void *if_req)
12473 {
12474 	/* Get interface metric. */
12475 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12476 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12477 	if (ipip->ipi_cmd_type == IF_CMD) {
12478 		struct ifreq    *ifr;
12479 
12480 		ifr = (struct ifreq *)if_req;
12481 		ifr->ifr_metric = ipif->ipif_metric;
12482 	} else {
12483 		struct lifreq   *lifr;
12484 
12485 		lifr = (struct lifreq *)if_req;
12486 		lifr->lifr_metric = ipif->ipif_metric;
12487 	}
12488 
12489 	return (0);
12490 }
12491 
12492 /* ARGSUSED */
12493 int
12494 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12495     ip_ioctl_cmd_t *ipip, void *if_req)
12496 {
12497 
12498 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12499 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12500 	/*
12501 	 * Set the muxid returned from I_PLINK.
12502 	 */
12503 	if (ipip->ipi_cmd_type == IF_CMD) {
12504 		struct ifreq *ifr = (struct ifreq *)if_req;
12505 
12506 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12507 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12508 	} else {
12509 		struct lifreq *lifr = (struct lifreq *)if_req;
12510 
12511 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12512 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12513 	}
12514 	return (0);
12515 }
12516 
12517 /* ARGSUSED */
12518 int
12519 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12520     ip_ioctl_cmd_t *ipip, void *if_req)
12521 {
12522 
12523 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12524 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12525 	/*
12526 	 * Get the muxid saved in ill for I_PUNLINK.
12527 	 */
12528 	if (ipip->ipi_cmd_type == IF_CMD) {
12529 		struct ifreq *ifr = (struct ifreq *)if_req;
12530 
12531 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12532 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12533 	} else {
12534 		struct lifreq *lifr = (struct lifreq *)if_req;
12535 
12536 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12537 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12538 	}
12539 	return (0);
12540 }
12541 
12542 /*
12543  * Set the subnet prefix. Does not modify the broadcast address.
12544  */
12545 /* ARGSUSED */
12546 int
12547 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12548     ip_ioctl_cmd_t *ipip, void *if_req)
12549 {
12550 	int err = 0;
12551 	in6_addr_t v6addr;
12552 	in6_addr_t v6mask;
12553 	boolean_t need_up = B_FALSE;
12554 	int addrlen;
12555 
12556 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12557 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12558 
12559 	ASSERT(IAM_WRITER_IPIF(ipif));
12560 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12561 
12562 	if (ipif->ipif_isv6) {
12563 		sin6_t *sin6;
12564 
12565 		if (sin->sin_family != AF_INET6)
12566 			return (EAFNOSUPPORT);
12567 
12568 		sin6 = (sin6_t *)sin;
12569 		v6addr = sin6->sin6_addr;
12570 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12571 			return (EADDRNOTAVAIL);
12572 	} else {
12573 		ipaddr_t addr;
12574 
12575 		if (sin->sin_family != AF_INET)
12576 			return (EAFNOSUPPORT);
12577 
12578 		addr = sin->sin_addr.s_addr;
12579 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12580 			return (EADDRNOTAVAIL);
12581 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12582 		/* Add 96 bits */
12583 		addrlen += IPV6_ABITS - IP_ABITS;
12584 	}
12585 
12586 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12587 		return (EINVAL);
12588 
12589 	/* Check if bits in the address is set past the mask */
12590 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12591 		return (EINVAL);
12592 
12593 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12594 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12595 		return (0);	/* No change */
12596 
12597 	if (ipif->ipif_flags & IPIF_UP) {
12598 		/*
12599 		 * If the interface is already marked up,
12600 		 * we call ipif_down which will take care
12601 		 * of ditching any IREs that have been set
12602 		 * up based on the old interface address.
12603 		 */
12604 		err = ipif_logical_down(ipif, q, mp);
12605 		if (err == EINPROGRESS)
12606 			return (err);
12607 		ipif_down_tail(ipif);
12608 		need_up = B_TRUE;
12609 	}
12610 
12611 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12612 	return (err);
12613 }
12614 
12615 static int
12616 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12617     queue_t *q, mblk_t *mp, boolean_t need_up)
12618 {
12619 	ill_t	*ill = ipif->ipif_ill;
12620 	int	err = 0;
12621 
12622 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12623 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12624 
12625 	/* Set the new address. */
12626 	mutex_enter(&ill->ill_lock);
12627 	ipif->ipif_v6net_mask = v6mask;
12628 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12629 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12630 		    ipif->ipif_v6subnet);
12631 	}
12632 	mutex_exit(&ill->ill_lock);
12633 
12634 	if (need_up) {
12635 		/*
12636 		 * Now bring the interface back up.  If this
12637 		 * is the only IPIF for the ILL, ipif_up
12638 		 * will have to re-bind to the device, so
12639 		 * we may get back EINPROGRESS, in which
12640 		 * case, this IOCTL will get completed in
12641 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12642 		 */
12643 		err = ipif_up(ipif, q, mp);
12644 		if (err == EINPROGRESS)
12645 			return (err);
12646 	}
12647 	return (err);
12648 }
12649 
12650 /* ARGSUSED */
12651 int
12652 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12653     ip_ioctl_cmd_t *ipip, void *if_req)
12654 {
12655 	int	addrlen;
12656 	in6_addr_t v6addr;
12657 	in6_addr_t v6mask;
12658 	struct lifreq *lifr = (struct lifreq *)if_req;
12659 
12660 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12661 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12662 	ipif_down_tail(ipif);
12663 
12664 	addrlen = lifr->lifr_addrlen;
12665 	if (ipif->ipif_isv6) {
12666 		sin6_t *sin6;
12667 
12668 		sin6 = (sin6_t *)sin;
12669 		v6addr = sin6->sin6_addr;
12670 	} else {
12671 		ipaddr_t addr;
12672 
12673 		addr = sin->sin_addr.s_addr;
12674 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12675 		addrlen += IPV6_ABITS - IP_ABITS;
12676 	}
12677 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12678 
12679 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12680 }
12681 
12682 /* ARGSUSED */
12683 int
12684 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12685     ip_ioctl_cmd_t *ipip, void *if_req)
12686 {
12687 	struct lifreq *lifr = (struct lifreq *)if_req;
12688 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12689 
12690 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12691 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12692 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12693 
12694 	if (ipif->ipif_isv6) {
12695 		*sin6 = sin6_null;
12696 		sin6->sin6_family = AF_INET6;
12697 		sin6->sin6_addr = ipif->ipif_v6subnet;
12698 		lifr->lifr_addrlen =
12699 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12700 	} else {
12701 		*sin = sin_null;
12702 		sin->sin_family = AF_INET;
12703 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12704 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12705 	}
12706 	return (0);
12707 }
12708 
12709 /*
12710  * Set the IPv6 address token.
12711  */
12712 /* ARGSUSED */
12713 int
12714 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12715     ip_ioctl_cmd_t *ipi, void *if_req)
12716 {
12717 	ill_t *ill = ipif->ipif_ill;
12718 	int err;
12719 	in6_addr_t v6addr;
12720 	in6_addr_t v6mask;
12721 	boolean_t need_up = B_FALSE;
12722 	int i;
12723 	sin6_t *sin6 = (sin6_t *)sin;
12724 	struct lifreq *lifr = (struct lifreq *)if_req;
12725 	int addrlen;
12726 
12727 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12728 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12729 	ASSERT(IAM_WRITER_IPIF(ipif));
12730 
12731 	addrlen = lifr->lifr_addrlen;
12732 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12733 	if (ipif->ipif_id != 0)
12734 		return (EINVAL);
12735 
12736 	if (!ipif->ipif_isv6)
12737 		return (EINVAL);
12738 
12739 	if (addrlen > IPV6_ABITS)
12740 		return (EINVAL);
12741 
12742 	v6addr = sin6->sin6_addr;
12743 
12744 	/*
12745 	 * The length of the token is the length from the end.  To get
12746 	 * the proper mask for this, compute the mask of the bits not
12747 	 * in the token; ie. the prefix, and then xor to get the mask.
12748 	 */
12749 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12750 		return (EINVAL);
12751 	for (i = 0; i < 4; i++) {
12752 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12753 	}
12754 
12755 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12756 	    ill->ill_token_length == addrlen)
12757 		return (0);	/* No change */
12758 
12759 	if (ipif->ipif_flags & IPIF_UP) {
12760 		err = ipif_logical_down(ipif, q, mp);
12761 		if (err == EINPROGRESS)
12762 			return (err);
12763 		ipif_down_tail(ipif);
12764 		need_up = B_TRUE;
12765 	}
12766 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12767 	return (err);
12768 }
12769 
12770 static int
12771 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12772     mblk_t *mp, boolean_t need_up)
12773 {
12774 	in6_addr_t v6addr;
12775 	in6_addr_t v6mask;
12776 	ill_t	*ill = ipif->ipif_ill;
12777 	int	i;
12778 	int	err = 0;
12779 
12780 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12781 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12782 	v6addr = sin6->sin6_addr;
12783 	/*
12784 	 * The length of the token is the length from the end.  To get
12785 	 * the proper mask for this, compute the mask of the bits not
12786 	 * in the token; ie. the prefix, and then xor to get the mask.
12787 	 */
12788 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12789 	for (i = 0; i < 4; i++)
12790 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12791 
12792 	mutex_enter(&ill->ill_lock);
12793 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12794 	ill->ill_token_length = addrlen;
12795 	mutex_exit(&ill->ill_lock);
12796 
12797 	if (need_up) {
12798 		/*
12799 		 * Now bring the interface back up.  If this
12800 		 * is the only IPIF for the ILL, ipif_up
12801 		 * will have to re-bind to the device, so
12802 		 * we may get back EINPROGRESS, in which
12803 		 * case, this IOCTL will get completed in
12804 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12805 		 */
12806 		err = ipif_up(ipif, q, mp);
12807 		if (err == EINPROGRESS)
12808 			return (err);
12809 	}
12810 	return (err);
12811 }
12812 
12813 /* ARGSUSED */
12814 int
12815 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12816     ip_ioctl_cmd_t *ipi, void *if_req)
12817 {
12818 	ill_t *ill;
12819 	sin6_t *sin6 = (sin6_t *)sin;
12820 	struct lifreq *lifr = (struct lifreq *)if_req;
12821 
12822 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12823 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12824 	if (ipif->ipif_id != 0)
12825 		return (EINVAL);
12826 
12827 	ill = ipif->ipif_ill;
12828 	if (!ill->ill_isv6)
12829 		return (ENXIO);
12830 
12831 	*sin6 = sin6_null;
12832 	sin6->sin6_family = AF_INET6;
12833 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12834 	sin6->sin6_addr = ill->ill_token;
12835 	lifr->lifr_addrlen = ill->ill_token_length;
12836 	return (0);
12837 }
12838 
12839 /*
12840  * Set (hardware) link specific information that might override
12841  * what was acquired through the DL_INFO_ACK.
12842  * The logic is as follows.
12843  *
12844  * become exclusive
12845  * set CHANGING flag
12846  * change mtu on affected IREs
12847  * clear CHANGING flag
12848  *
12849  * An ire add that occurs before the CHANGING flag is set will have its mtu
12850  * changed by the ip_sioctl_lnkinfo.
12851  *
12852  * During the time the CHANGING flag is set, no new ires will be added to the
12853  * bucket, and ire add will fail (due the CHANGING flag).
12854  *
12855  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12856  * before it is added to the bucket.
12857  *
12858  * Obviously only 1 thread can set the CHANGING flag and we need to become
12859  * exclusive to set the flag.
12860  */
12861 /* ARGSUSED */
12862 int
12863 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12864     ip_ioctl_cmd_t *ipi, void *if_req)
12865 {
12866 	ill_t		*ill = ipif->ipif_ill;
12867 	ipif_t		*nipif;
12868 	int		ip_min_mtu;
12869 	boolean_t	mtu_walk = B_FALSE;
12870 	struct lifreq	*lifr = (struct lifreq *)if_req;
12871 	lif_ifinfo_req_t *lir;
12872 	ire_t		*ire;
12873 
12874 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12875 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12876 	lir = &lifr->lifr_ifinfo;
12877 	ASSERT(IAM_WRITER_IPIF(ipif));
12878 
12879 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12880 	if (ipif->ipif_id != 0)
12881 		return (EINVAL);
12882 
12883 	/* Set interface MTU. */
12884 	if (ipif->ipif_isv6)
12885 		ip_min_mtu = IPV6_MIN_MTU;
12886 	else
12887 		ip_min_mtu = IP_MIN_MTU;
12888 
12889 	/*
12890 	 * Verify values before we set anything. Allow zero to
12891 	 * mean unspecified.
12892 	 */
12893 	if (lir->lir_maxmtu != 0 &&
12894 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12895 	    lir->lir_maxmtu < ip_min_mtu))
12896 		return (EINVAL);
12897 	if (lir->lir_reachtime != 0 &&
12898 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12899 		return (EINVAL);
12900 	if (lir->lir_reachretrans != 0 &&
12901 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12902 		return (EINVAL);
12903 
12904 	mutex_enter(&ill->ill_lock);
12905 	ill->ill_state_flags |= ILL_CHANGING;
12906 	for (nipif = ill->ill_ipif; nipif != NULL;
12907 	    nipif = nipif->ipif_next) {
12908 		nipif->ipif_state_flags |= IPIF_CHANGING;
12909 	}
12910 
12911 	mutex_exit(&ill->ill_lock);
12912 
12913 	if (lir->lir_maxmtu != 0) {
12914 		ill->ill_max_mtu = lir->lir_maxmtu;
12915 		ill->ill_mtu_userspecified = 1;
12916 		mtu_walk = B_TRUE;
12917 	}
12918 
12919 	if (lir->lir_reachtime != 0)
12920 		ill->ill_reachable_time = lir->lir_reachtime;
12921 
12922 	if (lir->lir_reachretrans != 0)
12923 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12924 
12925 	ill->ill_max_hops = lir->lir_maxhops;
12926 
12927 	ill->ill_max_buf = ND_MAX_Q;
12928 
12929 	if (mtu_walk) {
12930 		/*
12931 		 * Set the MTU on all ipifs associated with this ill except
12932 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12933 		 */
12934 		for (nipif = ill->ill_ipif; nipif != NULL;
12935 		    nipif = nipif->ipif_next) {
12936 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12937 				continue;
12938 
12939 			nipif->ipif_mtu = ill->ill_max_mtu;
12940 
12941 			if (!(nipif->ipif_flags & IPIF_UP))
12942 				continue;
12943 
12944 			if (nipif->ipif_isv6)
12945 				ire = ipif_to_ire_v6(nipif);
12946 			else
12947 				ire = ipif_to_ire(nipif);
12948 			if (ire != NULL) {
12949 				ire->ire_max_frag = ipif->ipif_mtu;
12950 				ire_refrele(ire);
12951 			}
12952 
12953 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12954 			    nipif, ill);
12955 		}
12956 	}
12957 
12958 	mutex_enter(&ill->ill_lock);
12959 	for (nipif = ill->ill_ipif; nipif != NULL;
12960 	    nipif = nipif->ipif_next) {
12961 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12962 	}
12963 	ILL_UNMARK_CHANGING(ill);
12964 	mutex_exit(&ill->ill_lock);
12965 
12966 	return (0);
12967 }
12968 
12969 /* ARGSUSED */
12970 int
12971 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12972     ip_ioctl_cmd_t *ipi, void *if_req)
12973 {
12974 	struct lif_ifinfo_req *lir;
12975 	ill_t *ill = ipif->ipif_ill;
12976 
12977 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12978 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12979 	if (ipif->ipif_id != 0)
12980 		return (EINVAL);
12981 
12982 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12983 	lir->lir_maxhops = ill->ill_max_hops;
12984 	lir->lir_reachtime = ill->ill_reachable_time;
12985 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12986 	lir->lir_maxmtu = ill->ill_max_mtu;
12987 
12988 	return (0);
12989 }
12990 
12991 /*
12992  * Return best guess as to the subnet mask for the specified address.
12993  * Based on the subnet masks for all the configured interfaces.
12994  *
12995  * We end up returning a zero mask in the case of default, multicast or
12996  * experimental.
12997  */
12998 static ipaddr_t
12999 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13000 {
13001 	ipaddr_t net_mask;
13002 	ill_t	*ill;
13003 	ipif_t	*ipif;
13004 	ill_walk_context_t ctx;
13005 	ipif_t	*fallback_ipif = NULL;
13006 
13007 	net_mask = ip_net_mask(addr);
13008 	if (net_mask == 0) {
13009 		*ipifp = NULL;
13010 		return (0);
13011 	}
13012 
13013 	/* Let's check to see if this is maybe a local subnet route. */
13014 	/* this function only applies to IPv4 interfaces */
13015 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13016 	ill = ILL_START_WALK_V4(&ctx, ipst);
13017 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13018 		mutex_enter(&ill->ill_lock);
13019 		for (ipif = ill->ill_ipif; ipif != NULL;
13020 		    ipif = ipif->ipif_next) {
13021 			if (!IPIF_CAN_LOOKUP(ipif))
13022 				continue;
13023 			if (!(ipif->ipif_flags & IPIF_UP))
13024 				continue;
13025 			if ((ipif->ipif_subnet & net_mask) ==
13026 			    (addr & net_mask)) {
13027 				/*
13028 				 * Don't trust pt-pt interfaces if there are
13029 				 * other interfaces.
13030 				 */
13031 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13032 					if (fallback_ipif == NULL) {
13033 						ipif_refhold_locked(ipif);
13034 						fallback_ipif = ipif;
13035 					}
13036 					continue;
13037 				}
13038 
13039 				/*
13040 				 * Fine. Just assume the same net mask as the
13041 				 * directly attached subnet interface is using.
13042 				 */
13043 				ipif_refhold_locked(ipif);
13044 				mutex_exit(&ill->ill_lock);
13045 				rw_exit(&ipst->ips_ill_g_lock);
13046 				if (fallback_ipif != NULL)
13047 					ipif_refrele(fallback_ipif);
13048 				*ipifp = ipif;
13049 				return (ipif->ipif_net_mask);
13050 			}
13051 		}
13052 		mutex_exit(&ill->ill_lock);
13053 	}
13054 	rw_exit(&ipst->ips_ill_g_lock);
13055 
13056 	*ipifp = fallback_ipif;
13057 	return ((fallback_ipif != NULL) ?
13058 	    fallback_ipif->ipif_net_mask : net_mask);
13059 }
13060 
13061 /*
13062  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13063  */
13064 static void
13065 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13066 {
13067 	IOCP	iocp;
13068 	ipft_t	*ipft;
13069 	ipllc_t	*ipllc;
13070 	mblk_t	*mp1;
13071 	cred_t	*cr;
13072 	int	error = 0;
13073 	conn_t	*connp;
13074 
13075 	ip1dbg(("ip_wput_ioctl"));
13076 	iocp = (IOCP)mp->b_rptr;
13077 	mp1 = mp->b_cont;
13078 	if (mp1 == NULL) {
13079 		iocp->ioc_error = EINVAL;
13080 		mp->b_datap->db_type = M_IOCNAK;
13081 		iocp->ioc_count = 0;
13082 		qreply(q, mp);
13083 		return;
13084 	}
13085 
13086 	/*
13087 	 * These IOCTLs provide various control capabilities to
13088 	 * upstream agents such as ULPs and processes.	There
13089 	 * are currently two such IOCTLs implemented.  They
13090 	 * are used by TCP to provide update information for
13091 	 * existing IREs and to forcibly delete an IRE for a
13092 	 * host that is not responding, thereby forcing an
13093 	 * attempt at a new route.
13094 	 */
13095 	iocp->ioc_error = EINVAL;
13096 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13097 		goto done;
13098 
13099 	ipllc = (ipllc_t *)mp1->b_rptr;
13100 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13101 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13102 			break;
13103 	}
13104 	/*
13105 	 * prefer credential from mblk over ioctl;
13106 	 * see ip_sioctl_copyin_setup
13107 	 */
13108 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13109 
13110 	/*
13111 	 * Refhold the conn in case the request gets queued up in some lookup
13112 	 */
13113 	ASSERT(CONN_Q(q));
13114 	connp = Q_TO_CONN(q);
13115 	CONN_INC_REF(connp);
13116 	if (ipft->ipft_pfi &&
13117 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13118 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13119 		error = (*ipft->ipft_pfi)(q,
13120 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13121 	}
13122 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13123 		/*
13124 		 * CONN_OPER_PENDING_DONE happens in the function called
13125 		 * through ipft_pfi above.
13126 		 */
13127 		return;
13128 	}
13129 
13130 	CONN_OPER_PENDING_DONE(connp);
13131 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13132 		freemsg(mp);
13133 		return;
13134 	}
13135 	iocp->ioc_error = error;
13136 
13137 done:
13138 	mp->b_datap->db_type = M_IOCACK;
13139 	if (iocp->ioc_error)
13140 		iocp->ioc_count = 0;
13141 	qreply(q, mp);
13142 }
13143 
13144 /*
13145  * Lookup an ipif using the sequence id (ipif_seqid)
13146  */
13147 ipif_t *
13148 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13149 {
13150 	ipif_t *ipif;
13151 
13152 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13153 
13154 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13155 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13156 			return (ipif);
13157 	}
13158 	return (NULL);
13159 }
13160 
13161 /*
13162  * Assign a unique id for the ipif. This is used later when we send
13163  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13164  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13165  * IRE is added, we verify that ipif has not disappeared.
13166  */
13167 
13168 static void
13169 ipif_assign_seqid(ipif_t *ipif)
13170 {
13171 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13172 
13173 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13174 }
13175 
13176 /*
13177  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13178  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13179  * be inserted into the first space available in the list. The value of
13180  * ipif_id will then be set to the appropriate value for its position.
13181  */
13182 static int
13183 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13184 {
13185 	ill_t *ill;
13186 	ipif_t *tipif;
13187 	ipif_t **tipifp;
13188 	int id;
13189 	ip_stack_t	*ipst;
13190 
13191 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13192 	    IAM_WRITER_IPIF(ipif));
13193 
13194 	ill = ipif->ipif_ill;
13195 	ASSERT(ill != NULL);
13196 	ipst = ill->ill_ipst;
13197 
13198 	/*
13199 	 * In the case of lo0:0 we already hold the ill_g_lock.
13200 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13201 	 * ipif_insert. Another such caller is ipif_move.
13202 	 */
13203 	if (acquire_g_lock)
13204 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13205 	if (acquire_ill_lock)
13206 		mutex_enter(&ill->ill_lock);
13207 	id = ipif->ipif_id;
13208 	tipifp = &(ill->ill_ipif);
13209 	if (id == -1) {	/* need to find a real id */
13210 		id = 0;
13211 		while ((tipif = *tipifp) != NULL) {
13212 			ASSERT(tipif->ipif_id >= id);
13213 			if (tipif->ipif_id != id)
13214 				break; /* non-consecutive id */
13215 			id++;
13216 			tipifp = &(tipif->ipif_next);
13217 		}
13218 		/* limit number of logical interfaces */
13219 		if (id >= ipst->ips_ip_addrs_per_if) {
13220 			if (acquire_ill_lock)
13221 				mutex_exit(&ill->ill_lock);
13222 			if (acquire_g_lock)
13223 				rw_exit(&ipst->ips_ill_g_lock);
13224 			return (-1);
13225 		}
13226 		ipif->ipif_id = id; /* assign new id */
13227 	} else if (id < ipst->ips_ip_addrs_per_if) {
13228 		/* we have a real id; insert ipif in the right place */
13229 		while ((tipif = *tipifp) != NULL) {
13230 			ASSERT(tipif->ipif_id != id);
13231 			if (tipif->ipif_id > id)
13232 				break; /* found correct location */
13233 			tipifp = &(tipif->ipif_next);
13234 		}
13235 	} else {
13236 		if (acquire_ill_lock)
13237 			mutex_exit(&ill->ill_lock);
13238 		if (acquire_g_lock)
13239 			rw_exit(&ipst->ips_ill_g_lock);
13240 		return (-1);
13241 	}
13242 
13243 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13244 
13245 	ipif->ipif_next = tipif;
13246 	*tipifp = ipif;
13247 	if (acquire_ill_lock)
13248 		mutex_exit(&ill->ill_lock);
13249 	if (acquire_g_lock)
13250 		rw_exit(&ipst->ips_ill_g_lock);
13251 	return (0);
13252 }
13253 
13254 static void
13255 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13256 {
13257 	ipif_t	**ipifp;
13258 	ill_t	*ill = ipif->ipif_ill;
13259 
13260 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13261 	if (acquire_ill_lock)
13262 		mutex_enter(&ill->ill_lock);
13263 	else
13264 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13265 
13266 	ipifp = &ill->ill_ipif;
13267 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13268 		if (*ipifp == ipif) {
13269 			*ipifp = ipif->ipif_next;
13270 			break;
13271 		}
13272 	}
13273 
13274 	if (acquire_ill_lock)
13275 		mutex_exit(&ill->ill_lock);
13276 }
13277 
13278 /*
13279  * Allocate and initialize a new interface control structure.  (Always
13280  * called as writer.)
13281  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13282  * is not part of the global linked list of ills. ipif_seqid is unique
13283  * in the system and to preserve the uniqueness, it is assigned only
13284  * when ill becomes part of the global list. At that point ill will
13285  * have a name. If it doesn't get assigned here, it will get assigned
13286  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13287  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13288  * the interface flags or any other information from the DL_INFO_ACK for
13289  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13290  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13291  * second DL_INFO_ACK comes in from the driver.
13292  */
13293 static ipif_t *
13294 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13295 {
13296 	ipif_t	*ipif;
13297 	phyint_t *phyi;
13298 
13299 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13300 	    ill->ill_name, id, (void *)ill));
13301 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13302 
13303 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13304 		return (NULL);
13305 	*ipif = ipif_zero;	/* start clean */
13306 
13307 	ipif->ipif_ill = ill;
13308 	ipif->ipif_id = id;	/* could be -1 */
13309 	/*
13310 	 * Inherit the zoneid from the ill; for the shared stack instance
13311 	 * this is always the global zone
13312 	 */
13313 	ipif->ipif_zoneid = ill->ill_zoneid;
13314 
13315 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13316 
13317 	ipif->ipif_refcnt = 0;
13318 	ipif->ipif_saved_ire_cnt = 0;
13319 
13320 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13321 		mi_free(ipif);
13322 		return (NULL);
13323 	}
13324 	/* -1 id should have been replaced by real id */
13325 	id = ipif->ipif_id;
13326 	ASSERT(id >= 0);
13327 
13328 	if (ill->ill_name[0] != '\0')
13329 		ipif_assign_seqid(ipif);
13330 
13331 	/*
13332 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13333 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13334 	 * ioctl sets ipif_orig_ipifid to zero.
13335 	 */
13336 	ipif->ipif_orig_ipifid = id;
13337 
13338 	/*
13339 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13340 	 * The ipif is still not up and can't be looked up until the
13341 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13342 	 */
13343 	mutex_enter(&ill->ill_lock);
13344 	mutex_enter(&ill->ill_phyint->phyint_lock);
13345 	/*
13346 	 * Set the running flag when logical interface zero is created.
13347 	 * For subsequent logical interfaces, a DLPI link down
13348 	 * notification message may have cleared the running flag to
13349 	 * indicate the link is down, so we shouldn't just blindly set it.
13350 	 */
13351 	if (id == 0)
13352 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13353 	ipif->ipif_ire_type = ire_type;
13354 	phyi = ill->ill_phyint;
13355 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13356 
13357 	if (ipif->ipif_isv6) {
13358 		ill->ill_flags |= ILLF_IPV6;
13359 	} else {
13360 		ipaddr_t inaddr_any = INADDR_ANY;
13361 
13362 		ill->ill_flags |= ILLF_IPV4;
13363 
13364 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13365 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13366 		    &ipif->ipif_v6lcl_addr);
13367 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13368 		    &ipif->ipif_v6src_addr);
13369 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13370 		    &ipif->ipif_v6subnet);
13371 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13372 		    &ipif->ipif_v6net_mask);
13373 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13374 		    &ipif->ipif_v6brd_addr);
13375 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13376 		    &ipif->ipif_v6pp_dst_addr);
13377 	}
13378 
13379 	/*
13380 	 * Don't set the interface flags etc. now, will do it in
13381 	 * ip_ll_subnet_defaults.
13382 	 */
13383 	if (!initialize) {
13384 		mutex_exit(&ill->ill_lock);
13385 		mutex_exit(&ill->ill_phyint->phyint_lock);
13386 		return (ipif);
13387 	}
13388 	ipif->ipif_mtu = ill->ill_max_mtu;
13389 
13390 	if (ill->ill_bcast_addr_length != 0) {
13391 		/*
13392 		 * Later detect lack of DLPI driver multicast
13393 		 * capability by catching DL_ENABMULTI errors in
13394 		 * ip_rput_dlpi.
13395 		 */
13396 		ill->ill_flags |= ILLF_MULTICAST;
13397 		if (!ipif->ipif_isv6)
13398 			ipif->ipif_flags |= IPIF_BROADCAST;
13399 	} else {
13400 		if (ill->ill_net_type != IRE_LOOPBACK) {
13401 			if (ipif->ipif_isv6)
13402 				/*
13403 				 * Note: xresolv interfaces will eventually need
13404 				 * NOARP set here as well, but that will require
13405 				 * those external resolvers to have some
13406 				 * knowledge of that flag and act appropriately.
13407 				 * Not to be changed at present.
13408 				 */
13409 				ill->ill_flags |= ILLF_NONUD;
13410 			else
13411 				ill->ill_flags |= ILLF_NOARP;
13412 		}
13413 		if (ill->ill_phys_addr_length == 0) {
13414 			if (ill->ill_media &&
13415 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13416 				ipif->ipif_flags |= IPIF_NOXMIT;
13417 				phyi->phyint_flags |= PHYI_VIRTUAL;
13418 			} else {
13419 				/* pt-pt supports multicast. */
13420 				ill->ill_flags |= ILLF_MULTICAST;
13421 				if (ill->ill_net_type == IRE_LOOPBACK) {
13422 					phyi->phyint_flags |=
13423 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13424 				} else {
13425 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13426 				}
13427 			}
13428 		}
13429 	}
13430 	mutex_exit(&ill->ill_lock);
13431 	mutex_exit(&ill->ill_phyint->phyint_lock);
13432 	return (ipif);
13433 }
13434 
13435 /*
13436  * If appropriate, send a message up to the resolver delete the entry
13437  * for the address of this interface which is going out of business.
13438  * (Always called as writer).
13439  *
13440  * NOTE : We need to check for NULL mps as some of the fields are
13441  *	  initialized only for some interface types. See ipif_resolver_up()
13442  *	  for details.
13443  */
13444 void
13445 ipif_arp_down(ipif_t *ipif)
13446 {
13447 	mblk_t	*mp;
13448 	ill_t	*ill = ipif->ipif_ill;
13449 
13450 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13451 	ASSERT(IAM_WRITER_IPIF(ipif));
13452 
13453 	/* Delete the mapping for the local address */
13454 	mp = ipif->ipif_arp_del_mp;
13455 	if (mp != NULL) {
13456 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13457 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13458 		putnext(ill->ill_rq, mp);
13459 		ipif->ipif_arp_del_mp = NULL;
13460 	}
13461 
13462 	/*
13463 	 * If this is the last ipif that is going down and there are no
13464 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13465 	 * clean up ARP completely.
13466 	 */
13467 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13468 
13469 		/* Send up AR_INTERFACE_DOWN message */
13470 		mp = ill->ill_arp_down_mp;
13471 		if (mp != NULL) {
13472 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13473 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13474 			    ipif->ipif_id));
13475 			putnext(ill->ill_rq, mp);
13476 			ill->ill_arp_down_mp = NULL;
13477 		}
13478 
13479 		/* Tell ARP to delete the multicast mappings */
13480 		mp = ill->ill_arp_del_mapping_mp;
13481 		if (mp != NULL) {
13482 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13483 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13484 			    ipif->ipif_id));
13485 			putnext(ill->ill_rq, mp);
13486 			ill->ill_arp_del_mapping_mp = NULL;
13487 		}
13488 	}
13489 }
13490 
13491 /*
13492  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13493  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13494  * that it wants the add_mp allocated in this function to be returned
13495  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13496  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13497  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13498  * as it does a ipif_arp_down after calling this function - which will
13499  * remove what we add here.
13500  *
13501  * Returns -1 on failures and 0 on success.
13502  */
13503 int
13504 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13505 {
13506 	mblk_t	*del_mp = NULL;
13507 	mblk_t *add_mp = NULL;
13508 	mblk_t *mp;
13509 	ill_t	*ill = ipif->ipif_ill;
13510 	phyint_t *phyi = ill->ill_phyint;
13511 	ipaddr_t addr, mask, extract_mask = 0;
13512 	arma_t	*arma;
13513 	uint8_t *maddr, *bphys_addr;
13514 	uint32_t hw_start;
13515 	dl_unitdata_req_t *dlur;
13516 
13517 	ASSERT(IAM_WRITER_IPIF(ipif));
13518 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13519 		return (0);
13520 
13521 	/*
13522 	 * Delete the existing mapping from ARP. Normally ipif_down
13523 	 * -> ipif_arp_down should send this up to ARP. The only
13524 	 * reason we would find this when we are switching from
13525 	 * Multicast to Broadcast where we did not do a down.
13526 	 */
13527 	mp = ill->ill_arp_del_mapping_mp;
13528 	if (mp != NULL) {
13529 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13530 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13531 		putnext(ill->ill_rq, mp);
13532 		ill->ill_arp_del_mapping_mp = NULL;
13533 	}
13534 
13535 	if (arp_add_mapping_mp != NULL)
13536 		*arp_add_mapping_mp = NULL;
13537 
13538 	/*
13539 	 * Check that the address is not to long for the constant
13540 	 * length reserved in the template arma_t.
13541 	 */
13542 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13543 		return (-1);
13544 
13545 	/* Add mapping mblk */
13546 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13547 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13548 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13549 	    (caddr_t)&addr);
13550 	if (add_mp == NULL)
13551 		return (-1);
13552 	arma = (arma_t *)add_mp->b_rptr;
13553 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13554 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13555 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13556 
13557 	/*
13558 	 * Determine the broadcast address.
13559 	 */
13560 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13561 	if (ill->ill_sap_length < 0)
13562 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13563 	else
13564 		bphys_addr = (uchar_t *)dlur +
13565 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13566 	/*
13567 	 * Check PHYI_MULTI_BCAST and length of physical
13568 	 * address to determine if we use the mapping or the
13569 	 * broadcast address.
13570 	 */
13571 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13572 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13573 		    bphys_addr, maddr, &hw_start, &extract_mask))
13574 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13575 
13576 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13577 	    (ill->ill_flags & ILLF_MULTICAST)) {
13578 		/* Make sure this will not match the "exact" entry. */
13579 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13580 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13581 		    (caddr_t)&addr);
13582 		if (del_mp == NULL) {
13583 			freemsg(add_mp);
13584 			return (-1);
13585 		}
13586 		bcopy(&extract_mask, (char *)arma +
13587 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13588 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13589 			/* Use link-layer broadcast address for MULTI_BCAST */
13590 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13591 			ip2dbg(("ipif_arp_setup_multicast: adding"
13592 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13593 		} else {
13594 			arma->arma_hw_mapping_start = hw_start;
13595 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13596 			    " ARP setup for %s\n", ill->ill_name));
13597 		}
13598 	} else {
13599 		freemsg(add_mp);
13600 		ASSERT(del_mp == NULL);
13601 		/* It is neither MULTICAST nor MULTI_BCAST */
13602 		return (0);
13603 	}
13604 	ASSERT(add_mp != NULL && del_mp != NULL);
13605 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13606 	ill->ill_arp_del_mapping_mp = del_mp;
13607 	if (arp_add_mapping_mp != NULL) {
13608 		/* The caller just wants the mblks allocated */
13609 		*arp_add_mapping_mp = add_mp;
13610 	} else {
13611 		/* The caller wants us to send it to arp */
13612 		putnext(ill->ill_rq, add_mp);
13613 	}
13614 	return (0);
13615 }
13616 
13617 /*
13618  * Get the resolver set up for a new interface address.
13619  * (Always called as writer.)
13620  * Called both for IPv4 and IPv6 interfaces,
13621  * though it only sets up the resolver for v6
13622  * if it's an xresolv interface (one using an external resolver).
13623  * Honors ILLF_NOARP.
13624  * The enumerated value res_act is used to tune the behavior.
13625  * If set to Res_act_initial, then we set up all the resolver
13626  * structures for a new interface.  If set to Res_act_move, then
13627  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13628  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13629  * asynchronous hardware address change notification.  If set to
13630  * Res_act_defend, then we tell ARP that it needs to send a single
13631  * gratuitous message in defense of the address.
13632  * Returns error on failure.
13633  */
13634 int
13635 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13636 {
13637 	caddr_t	addr;
13638 	mblk_t	*arp_up_mp = NULL;
13639 	mblk_t	*arp_down_mp = NULL;
13640 	mblk_t	*arp_add_mp = NULL;
13641 	mblk_t	*arp_del_mp = NULL;
13642 	mblk_t	*arp_add_mapping_mp = NULL;
13643 	mblk_t	*arp_del_mapping_mp = NULL;
13644 	ill_t	*ill = ipif->ipif_ill;
13645 	uchar_t	*area_p = NULL;
13646 	uchar_t	*ared_p = NULL;
13647 	int	err = ENOMEM;
13648 	boolean_t was_dup;
13649 
13650 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13651 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13652 	ASSERT(IAM_WRITER_IPIF(ipif));
13653 
13654 	was_dup = B_FALSE;
13655 	if (res_act == Res_act_initial) {
13656 		ipif->ipif_addr_ready = 0;
13657 		/*
13658 		 * We're bringing an interface up here.  There's no way that we
13659 		 * should need to shut down ARP now.
13660 		 */
13661 		mutex_enter(&ill->ill_lock);
13662 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13663 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13664 			ill->ill_ipif_dup_count--;
13665 			was_dup = B_TRUE;
13666 		}
13667 		mutex_exit(&ill->ill_lock);
13668 	}
13669 	if (ipif->ipif_recovery_id != 0)
13670 		(void) untimeout(ipif->ipif_recovery_id);
13671 	ipif->ipif_recovery_id = 0;
13672 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13673 		ipif->ipif_addr_ready = 1;
13674 		return (0);
13675 	}
13676 	/* NDP will set the ipif_addr_ready flag when it's ready */
13677 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13678 		return (0);
13679 
13680 	if (ill->ill_isv6) {
13681 		/*
13682 		 * External resolver for IPv6
13683 		 */
13684 		ASSERT(res_act == Res_act_initial);
13685 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13686 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13687 			area_p = (uchar_t *)&ip6_area_template;
13688 			ared_p = (uchar_t *)&ip6_ared_template;
13689 		}
13690 	} else {
13691 		/*
13692 		 * IPv4 arp case. If the ARP stream has already started
13693 		 * closing, fail this request for ARP bringup. Else
13694 		 * record the fact that an ARP bringup is pending.
13695 		 */
13696 		mutex_enter(&ill->ill_lock);
13697 		if (ill->ill_arp_closing) {
13698 			mutex_exit(&ill->ill_lock);
13699 			err = EINVAL;
13700 			goto failed;
13701 		} else {
13702 			if (ill->ill_ipif_up_count == 0 &&
13703 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13704 				ill->ill_arp_bringup_pending = 1;
13705 			mutex_exit(&ill->ill_lock);
13706 		}
13707 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13708 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13709 			area_p = (uchar_t *)&ip_area_template;
13710 			ared_p = (uchar_t *)&ip_ared_template;
13711 		}
13712 	}
13713 
13714 	/*
13715 	 * Add an entry for the local address in ARP only if it
13716 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13717 	 */
13718 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13719 		area_t *area;
13720 
13721 		/* Now ask ARP to publish our address. */
13722 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13723 		if (arp_add_mp == NULL)
13724 			goto failed;
13725 		area = (area_t *)arp_add_mp->b_rptr;
13726 		if (res_act != Res_act_initial) {
13727 			/*
13728 			 * Copy the new hardware address and length into
13729 			 * arp_add_mp to be sent to ARP.
13730 			 */
13731 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13732 			bcopy(ill->ill_phys_addr,
13733 			    ((char *)area + area->area_hw_addr_offset),
13734 			    area->area_hw_addr_length);
13735 		}
13736 
13737 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13738 		    ACE_F_MYADDR;
13739 
13740 		if (res_act == Res_act_defend) {
13741 			area->area_flags |= ACE_F_DEFEND;
13742 			/*
13743 			 * If we're just defending our address now, then
13744 			 * there's no need to set up ARP multicast mappings.
13745 			 * The publish command is enough.
13746 			 */
13747 			goto done;
13748 		}
13749 
13750 		if (res_act != Res_act_initial)
13751 			goto arp_setup_multicast;
13752 
13753 		/*
13754 		 * Allocate an ARP deletion message so we know we can tell ARP
13755 		 * when the interface goes down.
13756 		 */
13757 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13758 		if (arp_del_mp == NULL)
13759 			goto failed;
13760 
13761 	} else {
13762 		if (res_act != Res_act_initial)
13763 			goto done;
13764 	}
13765 	/*
13766 	 * Need to bring up ARP or setup multicast mapping only
13767 	 * when the first interface is coming UP.
13768 	 */
13769 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13770 	    was_dup) {
13771 		goto done;
13772 	}
13773 
13774 	/*
13775 	 * Allocate an ARP down message (to be saved) and an ARP up
13776 	 * message.
13777 	 */
13778 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13779 	if (arp_down_mp == NULL)
13780 		goto failed;
13781 
13782 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13783 	if (arp_up_mp == NULL)
13784 		goto failed;
13785 
13786 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13787 		goto done;
13788 
13789 arp_setup_multicast:
13790 	/*
13791 	 * Setup the multicast mappings. This function initializes
13792 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13793 	 * IPv6.
13794 	 */
13795 	if (!ill->ill_isv6) {
13796 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13797 		if (err != 0)
13798 			goto failed;
13799 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13800 		ASSERT(arp_add_mapping_mp != NULL);
13801 	}
13802 
13803 done:
13804 	if (arp_del_mp != NULL) {
13805 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13806 		ipif->ipif_arp_del_mp = arp_del_mp;
13807 	}
13808 	if (arp_down_mp != NULL) {
13809 		ASSERT(ill->ill_arp_down_mp == NULL);
13810 		ill->ill_arp_down_mp = arp_down_mp;
13811 	}
13812 	if (arp_del_mapping_mp != NULL) {
13813 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13814 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13815 	}
13816 	if (arp_up_mp != NULL) {
13817 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13818 		    ill->ill_name, ipif->ipif_id));
13819 		putnext(ill->ill_rq, arp_up_mp);
13820 	}
13821 	if (arp_add_mp != NULL) {
13822 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13823 		    ill->ill_name, ipif->ipif_id));
13824 		/*
13825 		 * If it's an extended ARP implementation, then we'll wait to
13826 		 * hear that DAD has finished before using the interface.
13827 		 */
13828 		if (!ill->ill_arp_extend)
13829 			ipif->ipif_addr_ready = 1;
13830 		putnext(ill->ill_rq, arp_add_mp);
13831 	} else {
13832 		ipif->ipif_addr_ready = 1;
13833 	}
13834 	if (arp_add_mapping_mp != NULL) {
13835 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13836 		    ill->ill_name, ipif->ipif_id));
13837 		putnext(ill->ill_rq, arp_add_mapping_mp);
13838 	}
13839 	if (res_act != Res_act_initial)
13840 		return (0);
13841 
13842 	if (ill->ill_flags & ILLF_NOARP)
13843 		err = ill_arp_off(ill);
13844 	else
13845 		err = ill_arp_on(ill);
13846 	if (err != 0) {
13847 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13848 		freemsg(ipif->ipif_arp_del_mp);
13849 		freemsg(ill->ill_arp_down_mp);
13850 		freemsg(ill->ill_arp_del_mapping_mp);
13851 		ipif->ipif_arp_del_mp = NULL;
13852 		ill->ill_arp_down_mp = NULL;
13853 		ill->ill_arp_del_mapping_mp = NULL;
13854 		return (err);
13855 	}
13856 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13857 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13858 
13859 failed:
13860 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13861 	freemsg(arp_add_mp);
13862 	freemsg(arp_del_mp);
13863 	freemsg(arp_add_mapping_mp);
13864 	freemsg(arp_up_mp);
13865 	freemsg(arp_down_mp);
13866 	ill->ill_arp_bringup_pending = 0;
13867 	return (err);
13868 }
13869 
13870 /*
13871  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13872  * just gone back up.
13873  */
13874 static void
13875 ipif_arp_start_dad(ipif_t *ipif)
13876 {
13877 	ill_t *ill = ipif->ipif_ill;
13878 	mblk_t *arp_add_mp;
13879 	area_t *area;
13880 
13881 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13882 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13883 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13884 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13885 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13886 		/*
13887 		 * If we can't contact ARP for some reason, that's not really a
13888 		 * problem.  Just send out the routing socket notification that
13889 		 * DAD completion would have done, and continue.
13890 		 */
13891 		ipif_mask_reply(ipif);
13892 		ipif_up_notify(ipif);
13893 		ipif->ipif_addr_ready = 1;
13894 		return;
13895 	}
13896 
13897 	/* Setting the 'unverified' flag restarts DAD */
13898 	area = (area_t *)arp_add_mp->b_rptr;
13899 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13900 	    ACE_F_UNVERIFIED;
13901 	putnext(ill->ill_rq, arp_add_mp);
13902 }
13903 
13904 static void
13905 ipif_ndp_start_dad(ipif_t *ipif)
13906 {
13907 	nce_t *nce;
13908 
13909 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13910 	if (nce == NULL)
13911 		return;
13912 
13913 	if (!ndp_restart_dad(nce)) {
13914 		/*
13915 		 * If we can't restart DAD for some reason, that's not really a
13916 		 * problem.  Just send out the routing socket notification that
13917 		 * DAD completion would have done, and continue.
13918 		 */
13919 		ipif_up_notify(ipif);
13920 		ipif->ipif_addr_ready = 1;
13921 	}
13922 	NCE_REFRELE(nce);
13923 }
13924 
13925 /*
13926  * Restart duplicate address detection on all interfaces on the given ill.
13927  *
13928  * This is called when an interface transitions from down to up
13929  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13930  *
13931  * Note that since the underlying physical link has transitioned, we must cause
13932  * at least one routing socket message to be sent here, either via DAD
13933  * completion or just by default on the first ipif.  (If we don't do this, then
13934  * in.mpathd will see long delays when doing link-based failure recovery.)
13935  */
13936 void
13937 ill_restart_dad(ill_t *ill, boolean_t went_up)
13938 {
13939 	ipif_t *ipif;
13940 
13941 	if (ill == NULL)
13942 		return;
13943 
13944 	/*
13945 	 * If layer two doesn't support duplicate address detection, then just
13946 	 * send the routing socket message now and be done with it.
13947 	 */
13948 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13949 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13950 		ip_rts_ifmsg(ill->ill_ipif);
13951 		return;
13952 	}
13953 
13954 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13955 		if (went_up) {
13956 			if (ipif->ipif_flags & IPIF_UP) {
13957 				if (ill->ill_isv6)
13958 					ipif_ndp_start_dad(ipif);
13959 				else
13960 					ipif_arp_start_dad(ipif);
13961 			} else if (ill->ill_isv6 &&
13962 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13963 				/*
13964 				 * For IPv4, the ARP module itself will
13965 				 * automatically start the DAD process when it
13966 				 * sees DL_NOTE_LINK_UP.  We respond to the
13967 				 * AR_CN_READY at the completion of that task.
13968 				 * For IPv6, we must kick off the bring-up
13969 				 * process now.
13970 				 */
13971 				ndp_do_recovery(ipif);
13972 			} else {
13973 				/*
13974 				 * Unfortunately, the first ipif is "special"
13975 				 * and represents the underlying ill in the
13976 				 * routing socket messages.  Thus, when this
13977 				 * one ipif is down, we must still notify so
13978 				 * that the user knows the IFF_RUNNING status
13979 				 * change.  (If the first ipif is up, then
13980 				 * we'll handle eventual routing socket
13981 				 * notification via DAD completion.)
13982 				 */
13983 				if (ipif == ill->ill_ipif)
13984 					ip_rts_ifmsg(ill->ill_ipif);
13985 			}
13986 		} else {
13987 			/*
13988 			 * After link down, we'll need to send a new routing
13989 			 * message when the link comes back, so clear
13990 			 * ipif_addr_ready.
13991 			 */
13992 			ipif->ipif_addr_ready = 0;
13993 		}
13994 	}
13995 
13996 	/*
13997 	 * If we've torn down links, then notify the user right away.
13998 	 */
13999 	if (!went_up)
14000 		ip_rts_ifmsg(ill->ill_ipif);
14001 }
14002 
14003 /*
14004  * Wakeup all threads waiting to enter the ipsq, and sleeping
14005  * on any of the ills in this ipsq. The ill_lock of the ill
14006  * must be held so that waiters don't miss wakeups
14007  */
14008 static void
14009 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14010 {
14011 	phyint_t *phyint;
14012 
14013 	phyint = ipsq->ipsq_phyint_list;
14014 	while (phyint != NULL) {
14015 		if (phyint->phyint_illv4) {
14016 			if (!caller_holds_lock)
14017 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14018 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14019 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14020 			if (!caller_holds_lock)
14021 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14022 		}
14023 		if (phyint->phyint_illv6) {
14024 			if (!caller_holds_lock)
14025 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14026 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14027 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14028 			if (!caller_holds_lock)
14029 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14030 		}
14031 		phyint = phyint->phyint_ipsq_next;
14032 	}
14033 }
14034 
14035 static ipsq_t *
14036 ipsq_create(char *groupname, ip_stack_t *ipst)
14037 {
14038 	ipsq_t	*ipsq;
14039 
14040 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14041 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14042 	if (ipsq == NULL) {
14043 		return (NULL);
14044 	}
14045 
14046 	if (groupname != NULL)
14047 		(void) strcpy(ipsq->ipsq_name, groupname);
14048 	else
14049 		ipsq->ipsq_name[0] = '\0';
14050 
14051 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14052 	ipsq->ipsq_flags |= IPSQ_GROUP;
14053 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14054 	ipst->ips_ipsq_g_head = ipsq;
14055 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14056 	return (ipsq);
14057 }
14058 
14059 /*
14060  * Return an ipsq correspoding to the groupname. If 'create' is true
14061  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14062  * uniquely with an IPMP group. However during IPMP groupname operations,
14063  * multiple IPMP groups may be associated with a single ipsq. But no
14064  * IPMP group can be associated with more than 1 ipsq at any time.
14065  * For example
14066  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14067  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14068  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14069  *
14070  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14071  * status shown below during the execution of the above command.
14072  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14073  *
14074  * After the completion of the above groupname command we return to the stable
14075  * state shown below.
14076  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14077  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14078  *
14079  * Because of the above, we don't search based on the ipsq_name since that
14080  * would miss the correct ipsq during certain windows as shown above.
14081  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14082  * natural state.
14083  */
14084 static ipsq_t *
14085 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14086     ip_stack_t *ipst)
14087 {
14088 	ipsq_t	*ipsq;
14089 	int	group_len;
14090 	phyint_t *phyint;
14091 
14092 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14093 
14094 	group_len = strlen(groupname);
14095 	ASSERT(group_len != 0);
14096 	group_len++;
14097 
14098 	for (ipsq = ipst->ips_ipsq_g_head;
14099 	    ipsq != NULL;
14100 	    ipsq = ipsq->ipsq_next) {
14101 		/*
14102 		 * When an ipsq is being split, and ill_split_ipsq
14103 		 * calls this function, we exclude it from being considered.
14104 		 */
14105 		if (ipsq == exclude_ipsq)
14106 			continue;
14107 
14108 		/*
14109 		 * Compare against the ipsq_name. The groupname change happens
14110 		 * in 2 phases. The 1st phase merges the from group into
14111 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14112 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14113 		 * ipsq_name. At this point the phyint_groupname has not been
14114 		 * updated.
14115 		 */
14116 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14117 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14118 			/*
14119 			 * Verify that an ipmp groupname is exactly
14120 			 * part of 1 ipsq and is not found in any other
14121 			 * ipsq.
14122 			 */
14123 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14124 			    NULL);
14125 			return (ipsq);
14126 		}
14127 
14128 		/*
14129 		 * Comparison against ipsq_name alone is not sufficient.
14130 		 * In the case when groups are currently being
14131 		 * merged, the ipsq could hold other IPMP groups temporarily.
14132 		 * so we walk the phyint list and compare against the
14133 		 * phyint_groupname as well.
14134 		 */
14135 		phyint = ipsq->ipsq_phyint_list;
14136 		while (phyint != NULL) {
14137 			if ((group_len == phyint->phyint_groupname_len) &&
14138 			    (bcmp(phyint->phyint_groupname, groupname,
14139 			    group_len) == 0)) {
14140 				/*
14141 				 * Verify that an ipmp groupname is exactly
14142 				 * part of 1 ipsq and is not found in any other
14143 				 * ipsq.
14144 				 */
14145 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14146 				    ipst) == NULL);
14147 				return (ipsq);
14148 			}
14149 			phyint = phyint->phyint_ipsq_next;
14150 		}
14151 	}
14152 	if (create)
14153 		ipsq = ipsq_create(groupname, ipst);
14154 	return (ipsq);
14155 }
14156 
14157 static void
14158 ipsq_delete(ipsq_t *ipsq)
14159 {
14160 	ipsq_t *nipsq;
14161 	ipsq_t *pipsq = NULL;
14162 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14163 
14164 	/*
14165 	 * We don't hold the ipsq lock, but we are sure no new
14166 	 * messages can land up, since the ipsq_refs is zero.
14167 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14168 	 * is associated with this ipsq. (Lookups are based on ill_name
14169 	 * or phyint_groupname)
14170 	 */
14171 	ASSERT(ipsq->ipsq_refs == 0);
14172 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14173 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14174 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14175 		/*
14176 		 * This is not the ipsq of an IPMP group.
14177 		 */
14178 		ipsq->ipsq_ipst = NULL;
14179 		kmem_free(ipsq, sizeof (ipsq_t));
14180 		return;
14181 	}
14182 
14183 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14184 
14185 	/*
14186 	 * Locate the ipsq  before we can remove it from
14187 	 * the singly linked list of ipsq's.
14188 	 */
14189 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14190 	    nipsq = nipsq->ipsq_next) {
14191 		if (nipsq == ipsq) {
14192 			break;
14193 		}
14194 		pipsq = nipsq;
14195 	}
14196 
14197 	ASSERT(nipsq == ipsq);
14198 
14199 	/* unlink ipsq from the list */
14200 	if (pipsq != NULL)
14201 		pipsq->ipsq_next = ipsq->ipsq_next;
14202 	else
14203 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14204 	ipsq->ipsq_ipst = NULL;
14205 	kmem_free(ipsq, sizeof (ipsq_t));
14206 	rw_exit(&ipst->ips_ill_g_lock);
14207 }
14208 
14209 static void
14210 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14211     queue_t *q)
14212 {
14213 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14214 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14215 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14216 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14217 	ASSERT(current_mp != NULL);
14218 
14219 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14220 	    NEW_OP, NULL);
14221 
14222 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14223 	    new_ipsq->ipsq_xopq_mphead != NULL);
14224 
14225 	/*
14226 	 * move from old ipsq to the new ipsq.
14227 	 */
14228 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14229 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14230 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14231 
14232 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14233 }
14234 
14235 void
14236 ill_group_cleanup(ill_t *ill)
14237 {
14238 	ill_t *ill_v4;
14239 	ill_t *ill_v6;
14240 	ipif_t *ipif;
14241 
14242 	ill_v4 = ill->ill_phyint->phyint_illv4;
14243 	ill_v6 = ill->ill_phyint->phyint_illv6;
14244 
14245 	if (ill_v4 != NULL) {
14246 		mutex_enter(&ill_v4->ill_lock);
14247 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14248 		    ipif = ipif->ipif_next) {
14249 			IPIF_UNMARK_MOVING(ipif);
14250 		}
14251 		ill_v4->ill_up_ipifs = B_FALSE;
14252 		mutex_exit(&ill_v4->ill_lock);
14253 	}
14254 
14255 	if (ill_v6 != NULL) {
14256 		mutex_enter(&ill_v6->ill_lock);
14257 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14258 		    ipif = ipif->ipif_next) {
14259 			IPIF_UNMARK_MOVING(ipif);
14260 		}
14261 		ill_v6->ill_up_ipifs = B_FALSE;
14262 		mutex_exit(&ill_v6->ill_lock);
14263 	}
14264 }
14265 /*
14266  * This function is called when an ill has had a change in its group status
14267  * to bring up all the ipifs that were up before the change.
14268  */
14269 int
14270 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14271 {
14272 	ipif_t *ipif;
14273 	ill_t *ill_v4;
14274 	ill_t *ill_v6;
14275 	ill_t *from_ill;
14276 	int err = 0;
14277 
14278 	ASSERT(IAM_WRITER_ILL(ill));
14279 
14280 	/*
14281 	 * Except for ipif_state_flags and ill_state_flags the other
14282 	 * fields of the ipif/ill that are modified below are protected
14283 	 * implicitly since we are a writer. We would have tried to down
14284 	 * even an ipif that was already down, in ill_down_ipifs. So we
14285 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14286 	 */
14287 	ill_v4 = ill->ill_phyint->phyint_illv4;
14288 	ill_v6 = ill->ill_phyint->phyint_illv6;
14289 	if (ill_v4 != NULL) {
14290 		ill_v4->ill_up_ipifs = B_TRUE;
14291 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14292 		    ipif = ipif->ipif_next) {
14293 			mutex_enter(&ill_v4->ill_lock);
14294 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14295 			IPIF_UNMARK_MOVING(ipif);
14296 			mutex_exit(&ill_v4->ill_lock);
14297 			if (ipif->ipif_was_up) {
14298 				if (!(ipif->ipif_flags & IPIF_UP))
14299 					err = ipif_up(ipif, q, mp);
14300 				ipif->ipif_was_up = B_FALSE;
14301 				if (err != 0) {
14302 					/*
14303 					 * Can there be any other error ?
14304 					 */
14305 					ASSERT(err == EINPROGRESS);
14306 					return (err);
14307 				}
14308 			}
14309 		}
14310 		mutex_enter(&ill_v4->ill_lock);
14311 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14312 		mutex_exit(&ill_v4->ill_lock);
14313 		ill_v4->ill_up_ipifs = B_FALSE;
14314 		if (ill_v4->ill_move_in_progress) {
14315 			ASSERT(ill_v4->ill_move_peer != NULL);
14316 			ill_v4->ill_move_in_progress = B_FALSE;
14317 			from_ill = ill_v4->ill_move_peer;
14318 			from_ill->ill_move_in_progress = B_FALSE;
14319 			from_ill->ill_move_peer = NULL;
14320 			mutex_enter(&from_ill->ill_lock);
14321 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14322 			mutex_exit(&from_ill->ill_lock);
14323 			if (ill_v6 == NULL) {
14324 				if (from_ill->ill_phyint->phyint_flags &
14325 				    PHYI_STANDBY) {
14326 					phyint_inactive(from_ill->ill_phyint);
14327 				}
14328 				if (ill_v4->ill_phyint->phyint_flags &
14329 				    PHYI_STANDBY) {
14330 					phyint_inactive(ill_v4->ill_phyint);
14331 				}
14332 			}
14333 			ill_v4->ill_move_peer = NULL;
14334 		}
14335 	}
14336 
14337 	if (ill_v6 != NULL) {
14338 		ill_v6->ill_up_ipifs = B_TRUE;
14339 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14340 		    ipif = ipif->ipif_next) {
14341 			mutex_enter(&ill_v6->ill_lock);
14342 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14343 			IPIF_UNMARK_MOVING(ipif);
14344 			mutex_exit(&ill_v6->ill_lock);
14345 			if (ipif->ipif_was_up) {
14346 				if (!(ipif->ipif_flags & IPIF_UP))
14347 					err = ipif_up(ipif, q, mp);
14348 				ipif->ipif_was_up = B_FALSE;
14349 				if (err != 0) {
14350 					/*
14351 					 * Can there be any other error ?
14352 					 */
14353 					ASSERT(err == EINPROGRESS);
14354 					return (err);
14355 				}
14356 			}
14357 		}
14358 		mutex_enter(&ill_v6->ill_lock);
14359 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14360 		mutex_exit(&ill_v6->ill_lock);
14361 		ill_v6->ill_up_ipifs = B_FALSE;
14362 		if (ill_v6->ill_move_in_progress) {
14363 			ASSERT(ill_v6->ill_move_peer != NULL);
14364 			ill_v6->ill_move_in_progress = B_FALSE;
14365 			from_ill = ill_v6->ill_move_peer;
14366 			from_ill->ill_move_in_progress = B_FALSE;
14367 			from_ill->ill_move_peer = NULL;
14368 			mutex_enter(&from_ill->ill_lock);
14369 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14370 			mutex_exit(&from_ill->ill_lock);
14371 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14372 				phyint_inactive(from_ill->ill_phyint);
14373 			}
14374 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14375 				phyint_inactive(ill_v6->ill_phyint);
14376 			}
14377 			ill_v6->ill_move_peer = NULL;
14378 		}
14379 	}
14380 	return (0);
14381 }
14382 
14383 /*
14384  * bring down all the approriate ipifs.
14385  */
14386 /* ARGSUSED */
14387 static void
14388 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14389 {
14390 	ipif_t *ipif;
14391 
14392 	ASSERT(IAM_WRITER_ILL(ill));
14393 
14394 	/*
14395 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14396 	 * are modified below are protected implicitly since we are a writer
14397 	 */
14398 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14399 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14400 			continue;
14401 		/*
14402 		 * Don't bring down the LINK LOCAL addresses as they are tied
14403 		 * to physical interface and they don't move. Treat them as
14404 		 * IPIF_NOFAILOVER.
14405 		 */
14406 		if (chk_nofailover && ill->ill_isv6 &&
14407 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))
14408 			continue;
14409 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14410 			/*
14411 			 * We go through the ipif_down logic even if the ipif
14412 			 * is already down, since routes can be added based
14413 			 * on down ipifs. Going through ipif_down once again
14414 			 * will delete any IREs created based on these routes.
14415 			 */
14416 			if (ipif->ipif_flags & IPIF_UP)
14417 				ipif->ipif_was_up = B_TRUE;
14418 			/*
14419 			 * If called with chk_nofailover true ipif is moving.
14420 			 */
14421 			mutex_enter(&ill->ill_lock);
14422 			if (chk_nofailover) {
14423 				ipif->ipif_state_flags |=
14424 				    IPIF_MOVING | IPIF_CHANGING;
14425 			} else {
14426 				ipif->ipif_state_flags |= IPIF_CHANGING;
14427 			}
14428 			mutex_exit(&ill->ill_lock);
14429 			/*
14430 			 * Need to re-create net/subnet bcast ires if
14431 			 * they are dependent on ipif.
14432 			 */
14433 			if (!ipif->ipif_isv6)
14434 				ipif_check_bcast_ires(ipif);
14435 			(void) ipif_logical_down(ipif, NULL, NULL);
14436 			ipif_non_duplicate(ipif);
14437 			ipif_down_tail(ipif);
14438 		}
14439 	}
14440 }
14441 
14442 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14443 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14444 	(ipsq)->ipsq_refs++;				\
14445 }
14446 
14447 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14448 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14449 	(ipsq)->ipsq_refs--;				\
14450 	if ((ipsq)->ipsq_refs == 0)				\
14451 		(ipsq)->ipsq_name[0] = '\0'; 		\
14452 }
14453 
14454 /*
14455  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14456  * new_ipsq.
14457  */
14458 static void
14459 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14460 {
14461 	phyint_t *phyint;
14462 	phyint_t *next_phyint;
14463 
14464 	/*
14465 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14466 	 * writer and the ill_lock of the ill in question. Also the dest
14467 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14468 	 */
14469 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14470 
14471 	phyint = cur_ipsq->ipsq_phyint_list;
14472 	cur_ipsq->ipsq_phyint_list = NULL;
14473 	while (phyint != NULL) {
14474 		next_phyint = phyint->phyint_ipsq_next;
14475 		IPSQ_DEC_REF(cur_ipsq, ipst);
14476 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14477 		new_ipsq->ipsq_phyint_list = phyint;
14478 		IPSQ_INC_REF(new_ipsq, ipst);
14479 		phyint->phyint_ipsq = new_ipsq;
14480 		phyint = next_phyint;
14481 	}
14482 }
14483 
14484 #define	SPLIT_SUCCESS		0
14485 #define	SPLIT_NOT_NEEDED	1
14486 #define	SPLIT_FAILED		2
14487 
14488 int
14489 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14490     ip_stack_t *ipst)
14491 {
14492 	ipsq_t *newipsq = NULL;
14493 
14494 	/*
14495 	 * Assertions denote pre-requisites for changing the ipsq of
14496 	 * a phyint
14497 	 */
14498 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14499 	/*
14500 	 * <ill-phyint> assocs can't change while ill_g_lock
14501 	 * is held as writer. See ill_phyint_reinit()
14502 	 */
14503 	ASSERT(phyint->phyint_illv4 == NULL ||
14504 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14505 	ASSERT(phyint->phyint_illv6 == NULL ||
14506 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14507 
14508 	if ((phyint->phyint_groupname_len !=
14509 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14510 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14511 	    phyint->phyint_groupname_len) != 0)) {
14512 		/*
14513 		 * Once we fail in creating a new ipsq due to memory shortage,
14514 		 * don't attempt to create new ipsq again, based on another
14515 		 * phyint, since we want all phyints belonging to an IPMP group
14516 		 * to be in the same ipsq even in the event of mem alloc fails.
14517 		 */
14518 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14519 		    cur_ipsq, ipst);
14520 		if (newipsq == NULL) {
14521 			/* Memory allocation failure */
14522 			return (SPLIT_FAILED);
14523 		} else {
14524 			/* ipsq_refs protected by ill_g_lock (writer) */
14525 			IPSQ_DEC_REF(cur_ipsq, ipst);
14526 			phyint->phyint_ipsq = newipsq;
14527 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14528 			newipsq->ipsq_phyint_list = phyint;
14529 			IPSQ_INC_REF(newipsq, ipst);
14530 			return (SPLIT_SUCCESS);
14531 		}
14532 	}
14533 	return (SPLIT_NOT_NEEDED);
14534 }
14535 
14536 /*
14537  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14538  * to do this split
14539  */
14540 static int
14541 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14542 {
14543 	ipsq_t *newipsq;
14544 
14545 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14546 	/*
14547 	 * <ill-phyint> assocs can't change while ill_g_lock
14548 	 * is held as writer. See ill_phyint_reinit()
14549 	 */
14550 
14551 	ASSERT(phyint->phyint_illv4 == NULL ||
14552 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14553 	ASSERT(phyint->phyint_illv6 == NULL ||
14554 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14555 
14556 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14557 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14558 		/*
14559 		 * ipsq_init failed due to no memory
14560 		 * caller will use the same ipsq
14561 		 */
14562 		return (SPLIT_FAILED);
14563 	}
14564 
14565 	/* ipsq_ref is protected by ill_g_lock (writer) */
14566 	IPSQ_DEC_REF(cur_ipsq, ipst);
14567 
14568 	/*
14569 	 * This is a new ipsq that is unknown to the world.
14570 	 * So we don't need to hold ipsq_lock,
14571 	 */
14572 	newipsq = phyint->phyint_ipsq;
14573 	newipsq->ipsq_writer = NULL;
14574 	newipsq->ipsq_reentry_cnt--;
14575 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14576 #ifdef DEBUG
14577 	newipsq->ipsq_depth = 0;
14578 #endif
14579 
14580 	return (SPLIT_SUCCESS);
14581 }
14582 
14583 /*
14584  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14585  * ipsq's representing their individual groups or themselves. Return
14586  * whether split needs to be retried again later.
14587  */
14588 static boolean_t
14589 ill_split_ipsq(ipsq_t *cur_ipsq)
14590 {
14591 	phyint_t *phyint;
14592 	phyint_t *next_phyint;
14593 	int	error;
14594 	boolean_t need_retry = B_FALSE;
14595 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14596 
14597 	phyint = cur_ipsq->ipsq_phyint_list;
14598 	cur_ipsq->ipsq_phyint_list = NULL;
14599 	while (phyint != NULL) {
14600 		next_phyint = phyint->phyint_ipsq_next;
14601 		/*
14602 		 * 'created' will tell us whether the callee actually
14603 		 * created an ipsq. Lack of memory may force the callee
14604 		 * to return without creating an ipsq.
14605 		 */
14606 		if (phyint->phyint_groupname == NULL) {
14607 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14608 		} else {
14609 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14610 			    need_retry, ipst);
14611 		}
14612 
14613 		switch (error) {
14614 		case SPLIT_FAILED:
14615 			need_retry = B_TRUE;
14616 			/* FALLTHRU */
14617 		case SPLIT_NOT_NEEDED:
14618 			/*
14619 			 * Keep it on the list.
14620 			 */
14621 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14622 			cur_ipsq->ipsq_phyint_list = phyint;
14623 			break;
14624 		case SPLIT_SUCCESS:
14625 			break;
14626 		default:
14627 			ASSERT(0);
14628 		}
14629 
14630 		phyint = next_phyint;
14631 	}
14632 	return (need_retry);
14633 }
14634 
14635 /*
14636  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14637  * and return the ills in the list. This list will be
14638  * needed to unlock all the ills later on by the caller.
14639  * The <ill-ipsq> associations could change between the
14640  * lock and unlock. Hence the unlock can't traverse the
14641  * ipsq to get the list of ills.
14642  */
14643 static int
14644 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14645 {
14646 	int	cnt = 0;
14647 	phyint_t	*phyint;
14648 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14649 
14650 	/*
14651 	 * The caller holds ill_g_lock to ensure that the ill memberships
14652 	 * of the ipsq don't change
14653 	 */
14654 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14655 
14656 	phyint = ipsq->ipsq_phyint_list;
14657 	while (phyint != NULL) {
14658 		if (phyint->phyint_illv4 != NULL) {
14659 			ASSERT(cnt < list_max);
14660 			list[cnt++] = phyint->phyint_illv4;
14661 		}
14662 		if (phyint->phyint_illv6 != NULL) {
14663 			ASSERT(cnt < list_max);
14664 			list[cnt++] = phyint->phyint_illv6;
14665 		}
14666 		phyint = phyint->phyint_ipsq_next;
14667 	}
14668 	ill_lock_ills(list, cnt);
14669 	return (cnt);
14670 }
14671 
14672 void
14673 ill_lock_ills(ill_t **list, int cnt)
14674 {
14675 	int	i;
14676 
14677 	if (cnt > 1) {
14678 		boolean_t try_again;
14679 		do {
14680 			try_again = B_FALSE;
14681 			for (i = 0; i < cnt - 1; i++) {
14682 				if (list[i] < list[i + 1]) {
14683 					ill_t	*tmp;
14684 
14685 					/* swap the elements */
14686 					tmp = list[i];
14687 					list[i] = list[i + 1];
14688 					list[i + 1] = tmp;
14689 					try_again = B_TRUE;
14690 				}
14691 			}
14692 		} while (try_again);
14693 	}
14694 
14695 	for (i = 0; i < cnt; i++) {
14696 		if (i == 0) {
14697 			if (list[i] != NULL)
14698 				mutex_enter(&list[i]->ill_lock);
14699 			else
14700 				return;
14701 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14702 			mutex_enter(&list[i]->ill_lock);
14703 		}
14704 	}
14705 }
14706 
14707 void
14708 ill_unlock_ills(ill_t **list, int cnt)
14709 {
14710 	int	i;
14711 
14712 	for (i = 0; i < cnt; i++) {
14713 		if ((i == 0) && (list[i] != NULL)) {
14714 			mutex_exit(&list[i]->ill_lock);
14715 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14716 			mutex_exit(&list[i]->ill_lock);
14717 		}
14718 	}
14719 }
14720 
14721 /*
14722  * Merge all the ills from 1 ipsq group into another ipsq group.
14723  * The source ipsq group is specified by the ipsq associated with
14724  * 'from_ill'. The destination ipsq group is specified by the ipsq
14725  * associated with 'to_ill' or 'groupname' respectively.
14726  * Note that ipsq itself does not have a reference count mechanism
14727  * and functions don't look up an ipsq and pass it around. Instead
14728  * functions pass around an ill or groupname, and the ipsq is looked
14729  * up from the ill or groupname and the required operation performed
14730  * atomically with the lookup on the ipsq.
14731  */
14732 static int
14733 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14734     queue_t *q)
14735 {
14736 	ipsq_t *old_ipsq;
14737 	ipsq_t *new_ipsq;
14738 	ill_t	**ill_list;
14739 	int	cnt;
14740 	size_t	ill_list_size;
14741 	boolean_t became_writer_on_new_sq = B_FALSE;
14742 	ip_stack_t	*ipst = from_ill->ill_ipst;
14743 
14744 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14745 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14746 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14747 
14748 	/*
14749 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14750 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14751 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14752 	 */
14753 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14754 
14755 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14756 	if (groupname != NULL)
14757 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14758 	else {
14759 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14760 	}
14761 
14762 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14763 
14764 	/*
14765 	 * both groups are on the same ipsq.
14766 	 */
14767 	if (old_ipsq == new_ipsq) {
14768 		rw_exit(&ipst->ips_ill_g_lock);
14769 		return (0);
14770 	}
14771 
14772 	cnt = old_ipsq->ipsq_refs << 1;
14773 	ill_list_size = cnt * sizeof (ill_t *);
14774 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14775 	if (ill_list == NULL) {
14776 		rw_exit(&ipst->ips_ill_g_lock);
14777 		return (ENOMEM);
14778 	}
14779 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14780 
14781 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14782 	mutex_enter(&new_ipsq->ipsq_lock);
14783 	if ((new_ipsq->ipsq_writer == NULL &&
14784 	    new_ipsq->ipsq_current_ipif == NULL) ||
14785 	    (new_ipsq->ipsq_writer == curthread)) {
14786 		new_ipsq->ipsq_writer = curthread;
14787 		new_ipsq->ipsq_reentry_cnt++;
14788 		became_writer_on_new_sq = B_TRUE;
14789 	}
14790 
14791 	/*
14792 	 * We are holding ill_g_lock as writer and all the ill locks of
14793 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14794 	 * message can land up on the old ipsq even though we don't hold the
14795 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14796 	 */
14797 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14798 
14799 	/*
14800 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14801 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14802 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14803 	 */
14804 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14805 
14806 	/*
14807 	 * Mark the new ipsq as needing a split since it is currently
14808 	 * being shared by more than 1 IPMP group. The split will
14809 	 * occur at the end of ipsq_exit
14810 	 */
14811 	new_ipsq->ipsq_split = B_TRUE;
14812 
14813 	/* Now release all the locks */
14814 	mutex_exit(&new_ipsq->ipsq_lock);
14815 	ill_unlock_ills(ill_list, cnt);
14816 	rw_exit(&ipst->ips_ill_g_lock);
14817 
14818 	kmem_free(ill_list, ill_list_size);
14819 
14820 	/*
14821 	 * If we succeeded in becoming writer on the new ipsq, then
14822 	 * drain the new ipsq and start processing  all enqueued messages
14823 	 * including the current ioctl we are processing which is either
14824 	 * a set groupname or failover/failback.
14825 	 */
14826 	if (became_writer_on_new_sq)
14827 		ipsq_exit(new_ipsq);
14828 
14829 	/*
14830 	 * syncq has been changed and all the messages have been moved.
14831 	 */
14832 	mutex_enter(&old_ipsq->ipsq_lock);
14833 	old_ipsq->ipsq_current_ipif = NULL;
14834 	old_ipsq->ipsq_current_ioctl = 0;
14835 	old_ipsq->ipsq_current_done = B_TRUE;
14836 	mutex_exit(&old_ipsq->ipsq_lock);
14837 	return (EINPROGRESS);
14838 }
14839 
14840 /*
14841  * Delete and add the loopback copy and non-loopback copy of
14842  * the BROADCAST ire corresponding to ill and addr. Used to
14843  * group broadcast ires together when ill becomes part of
14844  * a group.
14845  *
14846  * This function is also called when ill is leaving the group
14847  * so that the ires belonging to the group gets re-grouped.
14848  */
14849 static void
14850 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14851 {
14852 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14853 	ire_t **ire_ptpn = &ire_head;
14854 	ip_stack_t	*ipst = ill->ill_ipst;
14855 
14856 	/*
14857 	 * The loopback and non-loopback IREs are inserted in the order in which
14858 	 * they're found, on the basis that they are correctly ordered (loopback
14859 	 * first).
14860 	 */
14861 	for (;;) {
14862 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14863 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14864 		if (ire == NULL)
14865 			break;
14866 
14867 		/*
14868 		 * we are passing in KM_SLEEP because it is not easy to
14869 		 * go back to a sane state in case of memory failure.
14870 		 */
14871 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14872 		ASSERT(nire != NULL);
14873 		bzero(nire, sizeof (ire_t));
14874 		/*
14875 		 * Don't use ire_max_frag directly since we don't
14876 		 * hold on to 'ire' until we add the new ire 'nire' and
14877 		 * we don't want the new ire to have a dangling reference
14878 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14879 		 * be in sync with the ipif_mtu of the associate ipif.
14880 		 * For eg. this happens as a result of SIOCSLIFNAME,
14881 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14882 		 * the driver. A change in ire_max_frag triggered as
14883 		 * as a result of path mtu discovery, or due to an
14884 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14885 		 * route change -mtu command does not apply to broadcast ires.
14886 		 *
14887 		 * XXX We need a recovery strategy here if ire_init fails
14888 		 */
14889 		if (ire_init(nire,
14890 		    (uchar_t *)&ire->ire_addr,
14891 		    (uchar_t *)&ire->ire_mask,
14892 		    (uchar_t *)&ire->ire_src_addr,
14893 		    (uchar_t *)&ire->ire_gateway_addr,
14894 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14895 		    &ire->ire_ipif->ipif_mtu,
14896 		    ire->ire_nce,
14897 		    ire->ire_rfq,
14898 		    ire->ire_stq,
14899 		    ire->ire_type,
14900 		    ire->ire_ipif,
14901 		    ire->ire_cmask,
14902 		    ire->ire_phandle,
14903 		    ire->ire_ihandle,
14904 		    ire->ire_flags,
14905 		    &ire->ire_uinfo,
14906 		    NULL,
14907 		    NULL,
14908 		    ipst) == NULL) {
14909 			cmn_err(CE_PANIC, "ire_init() failed");
14910 		}
14911 		ire_delete(ire);
14912 		ire_refrele(ire);
14913 
14914 		/*
14915 		 * The newly created IREs are inserted at the tail of the list
14916 		 * starting with ire_head. As we've just allocated them no one
14917 		 * knows about them so it's safe.
14918 		 */
14919 		*ire_ptpn = nire;
14920 		ire_ptpn = &nire->ire_next;
14921 	}
14922 
14923 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14924 		int error;
14925 		ire_t *oire;
14926 		/* unlink the IRE from our list before calling ire_add() */
14927 		nire_next = nire->ire_next;
14928 		nire->ire_next = NULL;
14929 
14930 		/* ire_add adds the ire at the right place in the list */
14931 		oire = nire;
14932 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14933 		ASSERT(error == 0);
14934 		ASSERT(oire == nire);
14935 		ire_refrele(nire);	/* Held in ire_add */
14936 	}
14937 }
14938 
14939 /*
14940  * This function is usually called when an ill is inserted in
14941  * a group and all the ipifs are already UP. As all the ipifs
14942  * are already UP, the broadcast ires have already been created
14943  * and been inserted. But, ire_add_v4 would not have grouped properly.
14944  * We need to re-group for the benefit of ip_wput_ire which
14945  * expects BROADCAST ires to be grouped properly to avoid sending
14946  * more than one copy of the broadcast packet per group.
14947  *
14948  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14949  *	  because when ipif_up_done ends up calling this, ires have
14950  *        already been added before illgrp_insert i.e before ill_group
14951  *	  has been initialized.
14952  */
14953 static void
14954 ill_group_bcast_for_xmit(ill_t *ill)
14955 {
14956 	ill_group_t *illgrp;
14957 	ipif_t *ipif;
14958 	ipaddr_t addr;
14959 	ipaddr_t net_mask;
14960 	ipaddr_t subnet_netmask;
14961 
14962 	illgrp = ill->ill_group;
14963 
14964 	/*
14965 	 * This function is called even when an ill is deleted from
14966 	 * the group. Hence, illgrp could be null.
14967 	 */
14968 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14969 		return;
14970 
14971 	/*
14972 	 * Delete all the BROADCAST ires matching this ill and add
14973 	 * them back. This time, ire_add_v4 should take care of
14974 	 * grouping them with others because ill is part of the
14975 	 * group.
14976 	 */
14977 	ill_bcast_delete_and_add(ill, 0);
14978 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
14979 
14980 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14981 
14982 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14983 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14984 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14985 		} else {
14986 			net_mask = htonl(IN_CLASSA_NET);
14987 		}
14988 		addr = net_mask & ipif->ipif_subnet;
14989 		ill_bcast_delete_and_add(ill, addr);
14990 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
14991 
14992 		subnet_netmask = ipif->ipif_net_mask;
14993 		addr = ipif->ipif_subnet;
14994 		ill_bcast_delete_and_add(ill, addr);
14995 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
14996 	}
14997 }
14998 
14999 /*
15000  * This function is called from illgrp_delete when ill is being deleted
15001  * from the group.
15002  *
15003  * As ill is not there in the group anymore, any address belonging
15004  * to this ill should be cleared of IRE_MARK_NORECV.
15005  */
15006 static void
15007 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15008 {
15009 	ire_t *ire;
15010 	irb_t *irb;
15011 	ip_stack_t	*ipst = ill->ill_ipst;
15012 
15013 	ASSERT(ill->ill_group == NULL);
15014 
15015 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15016 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15017 
15018 	if (ire != NULL) {
15019 		/*
15020 		 * IPMP and plumbing operations are serialized on the ipsq, so
15021 		 * no one will insert or delete a broadcast ire under our feet.
15022 		 */
15023 		irb = ire->ire_bucket;
15024 		rw_enter(&irb->irb_lock, RW_READER);
15025 		ire_refrele(ire);
15026 
15027 		for (; ire != NULL; ire = ire->ire_next) {
15028 			if (ire->ire_addr != addr)
15029 				break;
15030 			if (ire_to_ill(ire) != ill)
15031 				continue;
15032 
15033 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15034 			ire->ire_marks &= ~IRE_MARK_NORECV;
15035 		}
15036 		rw_exit(&irb->irb_lock);
15037 	}
15038 }
15039 
15040 ire_t *
15041 irep_insert(ill_group_t *illgrp, ipaddr_t addr, ire_t *ire, ire_t ***pirep)
15042 {
15043 	boolean_t first = B_TRUE;
15044 	ire_t *clear_ire = NULL;
15045 	ire_t *start_ire = NULL;
15046 	uint64_t match_flags;
15047 	uint64_t phyi_flags;
15048 	boolean_t fallback = B_FALSE;
15049 
15050 	/*
15051 	 * irb_lock must be held by the caller.
15052 	 * Get to the first ire matching the address and the
15053 	 * group. If the address does not match we are done
15054 	 * as we could not find the IRE. If the address matches
15055 	 * we should get to the first one matching the group.
15056 	 */
15057 	while (ire != NULL) {
15058 		if (ire->ire_addr != addr ||
15059 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15060 			break;
15061 		}
15062 		ire = ire->ire_next;
15063 	}
15064 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15065 	start_ire = ire;
15066 redo:
15067 	while (ire != NULL && ire->ire_addr == addr &&
15068 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15069 		/*
15070 		 * The first ire for any address within a group
15071 		 * should always be the one with IRE_MARK_NORECV cleared
15072 		 * so that ip_wput_ire can avoid searching for one.
15073 		 * Note down the insertion point which will be used
15074 		 * later.
15075 		 */
15076 		if (first && (*pirep == NULL))
15077 			*pirep = ire->ire_ptpn;
15078 		/*
15079 		 * PHYI_FAILED is set when the interface fails.
15080 		 * This interface might have become good, but the
15081 		 * daemon has not yet detected. We should still
15082 		 * not receive on this. PHYI_OFFLINE should never
15083 		 * be picked as this has been offlined and soon
15084 		 * be removed.
15085 		 */
15086 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15087 		if (phyi_flags & PHYI_OFFLINE) {
15088 			ire->ire_marks |= IRE_MARK_NORECV;
15089 			ire = ire->ire_next;
15090 			continue;
15091 		}
15092 		if (phyi_flags & match_flags) {
15093 			ire->ire_marks |= IRE_MARK_NORECV;
15094 			ire = ire->ire_next;
15095 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15096 			    PHYI_INACTIVE) {
15097 				fallback = B_TRUE;
15098 			}
15099 			continue;
15100 		}
15101 		if (first) {
15102 			/*
15103 			 * We will move this to the front of the list later
15104 			 * on.
15105 			 */
15106 			clear_ire = ire;
15107 			ire->ire_marks &= ~IRE_MARK_NORECV;
15108 		} else {
15109 			ire->ire_marks |= IRE_MARK_NORECV;
15110 		}
15111 		first = B_FALSE;
15112 		ire = ire->ire_next;
15113 	}
15114 	/*
15115 	 * If we never nominated anybody, try nominating at least
15116 	 * an INACTIVE, if we found one. Do it only once though.
15117 	 */
15118 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15119 	    fallback) {
15120 		match_flags = PHYI_FAILED;
15121 		ire = start_ire;
15122 		*pirep = NULL;
15123 		goto redo;
15124 	}
15125 	return (clear_ire);
15126 }
15127 
15128 /*
15129  * This function must be called only after the broadcast ires
15130  * have been grouped together. For a given address addr, nominate
15131  * only one of the ires whose interface is not FAILED or OFFLINE.
15132  *
15133  * This is also called when an ipif goes down, so that we can nominate
15134  * a different ire with the same address for receiving.
15135  */
15136 static void
15137 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15138 {
15139 	irb_t *irb;
15140 	ire_t *ire;
15141 	ire_t *ire1;
15142 	ire_t *save_ire;
15143 	ire_t **irep = NULL;
15144 	ire_t *clear_ire = NULL;
15145 	ire_t	*new_lb_ire;
15146 	ire_t	*new_nlb_ire;
15147 	boolean_t new_lb_ire_used = B_FALSE;
15148 	boolean_t new_nlb_ire_used = B_FALSE;
15149 	boolean_t refrele_lb_ire = B_FALSE;
15150 	boolean_t refrele_nlb_ire = B_FALSE;
15151 	uint_t	max_frag;
15152 
15153 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15154 	    NULL, MATCH_IRE_TYPE, ipst);
15155 	/*
15156 	 * We may not be able to find some ires if a previous
15157 	 * ire_create failed. This happens when an ipif goes
15158 	 * down and we are unable to create BROADCAST ires due
15159 	 * to memory failure. Thus, we have to check for NULL
15160 	 * below. This should handle the case for LOOPBACK,
15161 	 * POINTOPOINT and interfaces with some POINTOPOINT
15162 	 * logicals for which there are no BROADCAST ires.
15163 	 */
15164 	if (ire == NULL)
15165 		return;
15166 	/*
15167 	 * Currently IRE_BROADCASTS are deleted when an ipif
15168 	 * goes down which runs exclusively. Thus, setting
15169 	 * IRE_MARK_RCVD should not race with ire_delete marking
15170 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15171 	 * be consistent with other parts of the code that walks
15172 	 * a given bucket.
15173 	 */
15174 	save_ire = ire;
15175 	irb = ire->ire_bucket;
15176 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15177 	if (new_lb_ire == NULL) {
15178 		ire_refrele(ire);
15179 		return;
15180 	}
15181 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15182 	if (new_nlb_ire == NULL) {
15183 		ire_refrele(ire);
15184 		kmem_cache_free(ire_cache, new_lb_ire);
15185 		return;
15186 	}
15187 	IRB_REFHOLD(irb);
15188 	rw_enter(&irb->irb_lock, RW_WRITER);
15189 	clear_ire = irep_insert(illgrp, addr, ire, &irep);
15190 
15191 	/*
15192 	 * irep non-NULL indicates that we entered the while loop
15193 	 * above. If clear_ire is at the insertion point, we don't
15194 	 * have to do anything. clear_ire will be NULL if all the
15195 	 * interfaces are failed.
15196 	 *
15197 	 * We cannot unlink and reinsert the ire at the right place
15198 	 * in the list since there can be other walkers of this bucket.
15199 	 * Instead we delete and recreate the ire
15200 	 */
15201 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15202 		ire_t *clear_ire_stq = NULL;
15203 		ire_t *clr_ire = NULL;
15204 		ire_t *ire_next = NULL;
15205 
15206 		if (clear_ire->ire_stq == NULL)
15207 			ire_next = clear_ire->ire_next;
15208 
15209 		rw_exit(&irb->irb_lock);
15210 
15211 		bzero(new_lb_ire, sizeof (ire_t));
15212 		/* XXX We need a recovery strategy here. */
15213 		if (ire_init(new_lb_ire,
15214 		    (uchar_t *)&clear_ire->ire_addr,
15215 		    (uchar_t *)&clear_ire->ire_mask,
15216 		    (uchar_t *)&clear_ire->ire_src_addr,
15217 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15218 		    &clear_ire->ire_max_frag,
15219 		    NULL, /* let ire_nce_init derive the resolver info */
15220 		    clear_ire->ire_rfq,
15221 		    clear_ire->ire_stq,
15222 		    clear_ire->ire_type,
15223 		    clear_ire->ire_ipif,
15224 		    clear_ire->ire_cmask,
15225 		    clear_ire->ire_phandle,
15226 		    clear_ire->ire_ihandle,
15227 		    clear_ire->ire_flags,
15228 		    &clear_ire->ire_uinfo,
15229 		    NULL,
15230 		    NULL,
15231 		    ipst) == NULL)
15232 			cmn_err(CE_PANIC, "ire_init() failed");
15233 
15234 		refrele_lb_ire = B_TRUE;
15235 
15236 		if (ire_next != NULL &&
15237 		    ire_next->ire_stq != NULL &&
15238 		    ire_next->ire_addr == clear_ire->ire_addr &&
15239 		    ire_next->ire_ipif->ipif_ill ==
15240 		    clear_ire->ire_ipif->ipif_ill) {
15241 			clear_ire_stq = ire_next;
15242 
15243 			bzero(new_nlb_ire, sizeof (ire_t));
15244 			/* XXX We need a recovery strategy here. */
15245 			if (ire_init(new_nlb_ire,
15246 			    (uchar_t *)&clear_ire_stq->ire_addr,
15247 			    (uchar_t *)&clear_ire_stq->ire_mask,
15248 			    (uchar_t *)&clear_ire_stq->ire_src_addr,
15249 			    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15250 			    &clear_ire_stq->ire_max_frag,
15251 			    NULL,
15252 			    clear_ire_stq->ire_rfq,
15253 			    clear_ire_stq->ire_stq,
15254 			    clear_ire_stq->ire_type,
15255 			    clear_ire_stq->ire_ipif,
15256 			    clear_ire_stq->ire_cmask,
15257 			    clear_ire_stq->ire_phandle,
15258 			    clear_ire_stq->ire_ihandle,
15259 			    clear_ire_stq->ire_flags,
15260 			    &clear_ire_stq->ire_uinfo,
15261 			    NULL,
15262 			    NULL,
15263 			    ipst) == NULL)
15264 				cmn_err(CE_PANIC, "ire_init() failed");
15265 
15266 				refrele_nlb_ire = B_TRUE;
15267 			}
15268 
15269 		rw_enter(&irb->irb_lock, RW_WRITER);
15270 		/*
15271 		 * irb_lock was dropped across call to ire_init() due to
15272 		 * lock ordering issue with ipst->ips_ndp{4,6}->ndp_g_lock
15273 		 * mutex lock. Therefore irep could have changed. call
15274 		 * irep_insert() to get the new insertion point (irep) and
15275 		 * recheck all known conditions.
15276 		 */
15277 		irep = NULL;
15278 		clr_ire = irep_insert(illgrp, addr, save_ire, &irep);
15279 		if ((irep != NULL) && (*irep != clear_ire) &&
15280 		    (clr_ire == clear_ire)) {
15281 			if ((clear_ire_stq != NULL) &&
15282 			    (clr_ire->ire_next != clear_ire_stq))
15283 				clear_ire_stq = NULL;
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.
15288 			 * So just mark it CONDEMNED.
15289 			 * The IRB_REFRELE will delete the ire from the list
15290 			 * and do the refrele.
15291 			 */
15292 			clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15293 			irb->irb_marks |= IRB_MARK_CONDEMNED;
15294 
15295 			if (clear_ire_stq != NULL &&
15296 			    clear_ire_stq->ire_nce != NULL) {
15297 				nce_fastpath_list_delete(
15298 				    clear_ire_stq->ire_nce);
15299 				clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15300 			}
15301 
15302 			/*
15303 			 * Also take care of otherfields like ib/ob pkt count
15304 			 * etc. Need to dup them.
15305 			 * ditto in ill_bcast_delete_and_add
15306 			 */
15307 
15308 			/* Set the max_frag before adding the ire */
15309 			max_frag = *new_lb_ire->ire_max_fragp;
15310 			new_lb_ire->ire_max_fragp = NULL;
15311 			new_lb_ire->ire_max_frag = max_frag;
15312 
15313 			/* Add the new ire's. Insert at *irep */
15314 			new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15315 			ire1 = *irep;
15316 			if (ire1 != NULL)
15317 				ire1->ire_ptpn = &new_lb_ire->ire_next;
15318 			new_lb_ire->ire_next = ire1;
15319 			/* Link the new one in. */
15320 			new_lb_ire->ire_ptpn = irep;
15321 			membar_producer();
15322 			*irep = new_lb_ire;
15323 			new_lb_ire_used = B_TRUE;
15324 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15325 			    ire_stats_inserted);
15326 			new_lb_ire->ire_bucket->irb_ire_cnt++;
15327 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *),
15328 			    new_lb_ire->ire_ipif,
15329 			    (char *), "ire", (void *), new_lb_ire);
15330 			new_lb_ire->ire_ipif->ipif_ire_cnt++;
15331 
15332 			if (clear_ire_stq != NULL) {
15333 				ill_t	*ire_ill;
15334 				/* Set the max_frag before adding the ire */
15335 				max_frag = *new_nlb_ire->ire_max_fragp;
15336 				new_nlb_ire->ire_max_fragp = NULL;
15337 				new_nlb_ire->ire_max_frag = max_frag;
15338 
15339 				new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15340 				irep = &new_lb_ire->ire_next;
15341 				/* Add the new ire. Insert at *irep */
15342 				ire1 = *irep;
15343 				if (ire1 != NULL)
15344 					ire1->ire_ptpn = &new_nlb_ire->ire_next;
15345 				new_nlb_ire->ire_next = ire1;
15346 				/* Link the new one in. */
15347 				new_nlb_ire->ire_ptpn = irep;
15348 				membar_producer();
15349 				*irep = new_nlb_ire;
15350 				new_nlb_ire_used = B_TRUE;
15351 				BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15352 				    ire_stats_inserted);
15353 				new_nlb_ire->ire_bucket->irb_ire_cnt++;
15354 				DTRACE_PROBE3(ipif__incr__cnt,
15355 				    (ipif_t *), new_nlb_ire->ire_ipif,
15356 				    (char *), "ire", (void *), new_nlb_ire);
15357 				new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15358 				DTRACE_PROBE3(ill__incr__cnt,
15359 				    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15360 				    (char *), "ire", (void *), new_nlb_ire);
15361 				ire_ill = (ill_t *)new_nlb_ire->ire_stq->q_ptr;
15362 				ire_ill->ill_ire_cnt++;
15363 			}
15364 		}
15365 	}
15366 	ire_refrele(save_ire);
15367 	rw_exit(&irb->irb_lock);
15368 	/*
15369 	 * Since we dropped the irb_lock across call to ire_init()
15370 	 * and rechecking known conditions, it is possible that
15371 	 * the checks might fail, therefore undo the work done by
15372 	 * ire_init() by calling ire_refrele() on the newly created ire.
15373 	 */
15374 	if (!new_lb_ire_used) {
15375 		if (refrele_lb_ire) {
15376 			ire_refrele(new_lb_ire);
15377 		} else {
15378 			kmem_cache_free(ire_cache, new_lb_ire);
15379 		}
15380 	}
15381 	if (!new_nlb_ire_used) {
15382 		if (refrele_nlb_ire) {
15383 			ire_refrele(new_nlb_ire);
15384 		} else {
15385 			kmem_cache_free(ire_cache, new_nlb_ire);
15386 		}
15387 	}
15388 	IRB_REFRELE(irb);
15389 }
15390 
15391 /*
15392  * Whenever an ipif goes down we have to renominate a different
15393  * broadcast ire to receive. Whenever an ipif comes up, we need
15394  * to make sure that we have only one nominated to receive.
15395  */
15396 static void
15397 ipif_renominate_bcast(ipif_t *ipif)
15398 {
15399 	ill_t *ill = ipif->ipif_ill;
15400 	ipaddr_t subnet_addr;
15401 	ipaddr_t net_addr;
15402 	ipaddr_t net_mask = 0;
15403 	ipaddr_t subnet_netmask;
15404 	ipaddr_t addr;
15405 	ill_group_t *illgrp;
15406 	ip_stack_t	*ipst = ill->ill_ipst;
15407 
15408 	illgrp = ill->ill_group;
15409 	/*
15410 	 * If this is the last ipif going down, it might take
15411 	 * the ill out of the group. In that case ipif_down ->
15412 	 * illgrp_delete takes care of doing the nomination.
15413 	 * ipif_down does not call for this case.
15414 	 */
15415 	ASSERT(illgrp != NULL);
15416 
15417 	/* There could not have been any ires associated with this */
15418 	if (ipif->ipif_subnet == 0)
15419 		return;
15420 
15421 	ill_mark_bcast(illgrp, 0, ipst);
15422 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15423 
15424 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15425 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15426 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15427 	} else {
15428 		net_mask = htonl(IN_CLASSA_NET);
15429 	}
15430 	addr = net_mask & ipif->ipif_subnet;
15431 	ill_mark_bcast(illgrp, addr, ipst);
15432 
15433 	net_addr = ~net_mask | addr;
15434 	ill_mark_bcast(illgrp, net_addr, ipst);
15435 
15436 	subnet_netmask = ipif->ipif_net_mask;
15437 	addr = ipif->ipif_subnet;
15438 	ill_mark_bcast(illgrp, addr, ipst);
15439 
15440 	subnet_addr = ~subnet_netmask | addr;
15441 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15442 }
15443 
15444 /*
15445  * Whenever we form or delete ill groups, we need to nominate one set of
15446  * BROADCAST ires for receiving in the group.
15447  *
15448  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15449  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15450  *    for ill_ipif_up_count to be non-zero. This is the only case where
15451  *    ill_ipif_up_count is zero and we would still find the ires.
15452  *
15453  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15454  *    ipif is UP and we just have to do the nomination.
15455  *
15456  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15457  *    from the group. So, we have to do the nomination.
15458  *
15459  * Because of (3), there could be just one ill in the group. But we have
15460  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15461  * Thus, this function does not optimize when there is only one ill as
15462  * it is not correct for (3).
15463  */
15464 static void
15465 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15466 {
15467 	ill_t *ill;
15468 	ipif_t *ipif;
15469 	ipaddr_t subnet_addr;
15470 	ipaddr_t prev_subnet_addr = 0;
15471 	ipaddr_t net_addr;
15472 	ipaddr_t prev_net_addr = 0;
15473 	ipaddr_t net_mask = 0;
15474 	ipaddr_t subnet_netmask;
15475 	ipaddr_t addr;
15476 	ip_stack_t	*ipst;
15477 
15478 	/*
15479 	 * When the last memeber is leaving, there is nothing to
15480 	 * nominate.
15481 	 */
15482 	if (illgrp->illgrp_ill_count == 0) {
15483 		ASSERT(illgrp->illgrp_ill == NULL);
15484 		return;
15485 	}
15486 
15487 	ill = illgrp->illgrp_ill;
15488 	ASSERT(!ill->ill_isv6);
15489 	ipst = ill->ill_ipst;
15490 	/*
15491 	 * We assume that ires with same address and belonging to the
15492 	 * same group, has been grouped together. Nominating a *single*
15493 	 * ill in the group for sending and receiving broadcast is done
15494 	 * by making sure that the first BROADCAST ire (which will be
15495 	 * the one returned by ire_ctable_lookup for ip_rput and the
15496 	 * one that will be used in ip_wput_ire) will be the one that
15497 	 * will not have IRE_MARK_NORECV set.
15498 	 *
15499 	 * 1) ip_rput checks and discards packets received on ires marked
15500 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15501 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15502 	 *    first ire in the group for every broadcast address in the group.
15503 	 *    ip_rput will accept packets only on the first ire i.e only
15504 	 *    one copy of the ill.
15505 	 *
15506 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15507 	 *    packet for the whole group. It needs to send out on the ill
15508 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15509 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15510 	 *    the copy echoed back on other port where the ire is not marked
15511 	 *    with IRE_MARK_NORECV.
15512 	 *
15513 	 * Note that we just need to have the first IRE either loopback or
15514 	 * non-loopback (either of them may not exist if ire_create failed
15515 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15516 	 * always hit the first one and hence will always accept one copy.
15517 	 *
15518 	 * We have a broadcast ire per ill for all the unique prefixes
15519 	 * hosted on that ill. As we don't have a way of knowing the
15520 	 * unique prefixes on a given ill and hence in the whole group,
15521 	 * we just call ill_mark_bcast on all the prefixes that exist
15522 	 * in the group. For the common case of one prefix, the code
15523 	 * below optimizes by remebering the last address used for
15524 	 * markng. In the case of multiple prefixes, this will still
15525 	 * optimize depending the order of prefixes.
15526 	 *
15527 	 * The only unique address across the whole group is 0.0.0.0 and
15528 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15529 	 * the first ire in the bucket for receiving and disables the
15530 	 * others.
15531 	 */
15532 	ill_mark_bcast(illgrp, 0, ipst);
15533 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15534 	for (; ill != NULL; ill = ill->ill_group_next) {
15535 
15536 		for (ipif = ill->ill_ipif; ipif != NULL;
15537 		    ipif = ipif->ipif_next) {
15538 
15539 			if (!(ipif->ipif_flags & IPIF_UP) ||
15540 			    ipif->ipif_subnet == 0) {
15541 				continue;
15542 			}
15543 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15544 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15545 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15546 			} else {
15547 				net_mask = htonl(IN_CLASSA_NET);
15548 			}
15549 			addr = net_mask & ipif->ipif_subnet;
15550 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15551 				ill_mark_bcast(illgrp, addr, ipst);
15552 				net_addr = ~net_mask | addr;
15553 				ill_mark_bcast(illgrp, net_addr, ipst);
15554 			}
15555 			prev_net_addr = addr;
15556 
15557 			subnet_netmask = ipif->ipif_net_mask;
15558 			addr = ipif->ipif_subnet;
15559 			if (prev_subnet_addr == 0 ||
15560 			    prev_subnet_addr != addr) {
15561 				ill_mark_bcast(illgrp, addr, ipst);
15562 				subnet_addr = ~subnet_netmask | addr;
15563 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15564 			}
15565 			prev_subnet_addr = addr;
15566 		}
15567 	}
15568 }
15569 
15570 /*
15571  * This function is called while forming ill groups.
15572  *
15573  * Currently, we handle only allmulti groups. We want to join
15574  * allmulti on only one of the ills in the groups. In future,
15575  * when we have link aggregation, we may have to join normal
15576  * multicast groups on multiple ills as switch does inbound load
15577  * balancing. Following are the functions that calls this
15578  * function :
15579  *
15580  * 1) ill_recover_multicast : Interface is coming back UP.
15581  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15582  *    will call ill_recover_multicast to recover all the multicast
15583  *    groups. We need to make sure that only one member is joined
15584  *    in the ill group.
15585  *
15586  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15587  *    Somebody is joining allmulti. We need to make sure that only one
15588  *    member is joined in the group.
15589  *
15590  * 3) illgrp_insert : If allmulti has already joined, we need to make
15591  *    sure that only one member is joined in the group.
15592  *
15593  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15594  *    allmulti who we have nominated. We need to pick someother ill.
15595  *
15596  * 5) illgrp_delete : The ill we nominated is leaving the group,
15597  *    we need to pick a new ill to join the group.
15598  *
15599  * For (1), (2), (5) - we just have to check whether there is
15600  * a good ill joined in the group. If we could not find any ills
15601  * joined the group, we should join.
15602  *
15603  * For (4), the one that was nominated to receive, left the group.
15604  * There could be nobody joined in the group when this function is
15605  * called.
15606  *
15607  * For (3) - we need to explicitly check whether there are multiple
15608  * ills joined in the group.
15609  *
15610  * For simplicity, we don't differentiate any of the above cases. We
15611  * just leave the group if it is joined on any of them and join on
15612  * the first good ill.
15613  */
15614 int
15615 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15616 {
15617 	ilm_t *ilm;
15618 	ill_t *ill;
15619 	ill_t *fallback_inactive_ill = NULL;
15620 	ill_t *fallback_failed_ill = NULL;
15621 	int ret = 0;
15622 
15623 	/*
15624 	 * Leave the allmulti on all the ills and start fresh.
15625 	 */
15626 	for (ill = illgrp->illgrp_ill; ill != NULL;
15627 	    ill = ill->ill_group_next) {
15628 		if (ill->ill_join_allmulti)
15629 			ill_leave_allmulti(ill);
15630 	}
15631 
15632 	/*
15633 	 * Choose a good ill. Fallback to inactive or failed if
15634 	 * none available. We need to fallback to FAILED in the
15635 	 * case where we have 2 interfaces in a group - where
15636 	 * one of them is failed and another is a good one and
15637 	 * the good one (not marked inactive) is leaving the group.
15638 	 */
15639 	for (ill = illgrp->illgrp_ill; ill != NULL; ill = ill->ill_group_next) {
15640 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15641 			continue;
15642 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15643 			fallback_failed_ill = ill;
15644 			continue;
15645 		}
15646 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15647 			fallback_inactive_ill = ill;
15648 			continue;
15649 		}
15650 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15651 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15652 				ret = ill_join_allmulti(ill);
15653 				/*
15654 				 * ill_join_allmulti() can fail because of
15655 				 * memory failures so make sure we join at
15656 				 * least on one ill.
15657 				 */
15658 				if (ill->ill_join_allmulti)
15659 					return (0);
15660 			}
15661 		}
15662 	}
15663 	if (ret != 0) {
15664 		/*
15665 		 * If we tried nominating above and failed to do so,
15666 		 * return error. We might have tried multiple times.
15667 		 * But, return the latest error.
15668 		 */
15669 		return (ret);
15670 	}
15671 	if ((ill = fallback_inactive_ill) != NULL) {
15672 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15673 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr))
15674 				return (ill_join_allmulti(ill));
15675 		}
15676 	} else if ((ill = fallback_failed_ill) != NULL) {
15677 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15678 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr))
15679 				return (ill_join_allmulti(ill));
15680 		}
15681 	}
15682 	return (0);
15683 }
15684 
15685 /*
15686  * This function is called from illgrp_delete after it is
15687  * deleted from the group to reschedule responsibilities
15688  * to a different ill.
15689  */
15690 static void
15691 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15692 {
15693 	ilm_t	*ilm;
15694 	ipif_t	*ipif;
15695 	ipaddr_t subnet_addr;
15696 	ipaddr_t net_addr;
15697 	ipaddr_t net_mask = 0;
15698 	ipaddr_t subnet_netmask;
15699 	ipaddr_t addr;
15700 	ip_stack_t *ipst = ill->ill_ipst;
15701 
15702 	ASSERT(ill->ill_group == NULL);
15703 	/*
15704 	 * Broadcast Responsibility:
15705 	 *
15706 	 * 1. If this ill has been nominated for receiving broadcast
15707 	 * packets, we need to find a new one. Before we find a new
15708 	 * one, we need to re-group the ires that are part of this new
15709 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15710 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15711 	 * thing for us.
15712 	 *
15713 	 * 2. If this ill was not nominated for receiving broadcast
15714 	 * packets, we need to clear the IRE_MARK_NORECV flag
15715 	 * so that we continue to send up broadcast packets.
15716 	 */
15717 	if (!ill->ill_isv6) {
15718 		/*
15719 		 * Case 1 above : No optimization here. Just redo the
15720 		 * nomination.
15721 		 */
15722 		ill_group_bcast_for_xmit(ill);
15723 		ill_nominate_bcast_rcv(illgrp);
15724 
15725 		/*
15726 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15727 		 */
15728 		ill_clear_bcast_mark(ill, 0);
15729 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15730 
15731 		for (ipif = ill->ill_ipif; ipif != NULL;
15732 		    ipif = ipif->ipif_next) {
15733 
15734 			if (!(ipif->ipif_flags & IPIF_UP) ||
15735 			    ipif->ipif_subnet == 0) {
15736 				continue;
15737 			}
15738 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15739 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15740 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15741 			} else {
15742 				net_mask = htonl(IN_CLASSA_NET);
15743 			}
15744 			addr = net_mask & ipif->ipif_subnet;
15745 			ill_clear_bcast_mark(ill, addr);
15746 
15747 			net_addr = ~net_mask | addr;
15748 			ill_clear_bcast_mark(ill, net_addr);
15749 
15750 			subnet_netmask = ipif->ipif_net_mask;
15751 			addr = ipif->ipif_subnet;
15752 			ill_clear_bcast_mark(ill, addr);
15753 
15754 			subnet_addr = ~subnet_netmask | addr;
15755 			ill_clear_bcast_mark(ill, subnet_addr);
15756 		}
15757 	}
15758 
15759 	/*
15760 	 * Multicast Responsibility.
15761 	 *
15762 	 * If we have joined allmulti on this one, find a new member
15763 	 * in the group to join allmulti. As this ill is already part
15764 	 * of allmulti, we don't have to join on this one.
15765 	 *
15766 	 * If we have not joined allmulti on this one, there is no
15767 	 * responsibility to handoff. But we need to take new
15768 	 * responsibility i.e, join allmulti on this one if we need
15769 	 * to.
15770 	 */
15771 	if (ill->ill_join_allmulti) {
15772 		(void) ill_nominate_mcast_rcv(illgrp);
15773 	} else {
15774 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15775 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15776 				(void) ill_join_allmulti(ill);
15777 				break;
15778 			}
15779 		}
15780 	}
15781 
15782 	/*
15783 	 * We intentionally do the flushing of IRE_CACHES only matching
15784 	 * on the ill and not on groups. Note that we are already deleted
15785 	 * from the group.
15786 	 *
15787 	 * This will make sure that all IRE_CACHES whose stq is pointing
15788 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15789 	 * deleted and IRE_CACHES that are not pointing at this ill will
15790 	 * be left alone.
15791 	 */
15792 	ire_walk_ill(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_CACHE,
15793 	    illgrp_cache_delete, ill, ill);
15794 
15795 	/*
15796 	 * Some conn may have cached one of the IREs deleted above. By removing
15797 	 * the ire reference, we clean up the extra reference to the ill held in
15798 	 * ire->ire_stq.
15799 	 */
15800 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15801 
15802 	/*
15803 	 * Re-do source address selection for all the members in the
15804 	 * group, if they borrowed source address from one of the ipifs
15805 	 * in this ill.
15806 	 */
15807 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15808 		if (ill->ill_isv6) {
15809 			ipif_update_other_ipifs_v6(ipif, illgrp);
15810 		} else {
15811 			ipif_update_other_ipifs(ipif, illgrp);
15812 		}
15813 	}
15814 }
15815 
15816 /*
15817  * Delete the ill from the group. The caller makes sure that it is
15818  * in a group and it okay to delete from the group. So, we always
15819  * delete here.
15820  */
15821 static void
15822 illgrp_delete(ill_t *ill)
15823 {
15824 	ill_group_t *illgrp;
15825 	ill_group_t *tmpg;
15826 	ill_t *tmp_ill;
15827 	ip_stack_t	*ipst = ill->ill_ipst;
15828 
15829 	/*
15830 	 * Reset illgrp_ill_schednext if it was pointing at us.
15831 	 * We need to do this before we set ill_group to NULL.
15832 	 */
15833 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15834 	mutex_enter(&ill->ill_lock);
15835 
15836 	illgrp_reset_schednext(ill);
15837 
15838 	illgrp = ill->ill_group;
15839 
15840 	/* Delete the ill from illgrp. */
15841 	if (illgrp->illgrp_ill == ill) {
15842 		illgrp->illgrp_ill = ill->ill_group_next;
15843 	} else {
15844 		tmp_ill = illgrp->illgrp_ill;
15845 		while (tmp_ill->ill_group_next != ill) {
15846 			tmp_ill = tmp_ill->ill_group_next;
15847 			ASSERT(tmp_ill != NULL);
15848 		}
15849 		tmp_ill->ill_group_next = ill->ill_group_next;
15850 	}
15851 	ill->ill_group = NULL;
15852 	ill->ill_group_next = NULL;
15853 
15854 	illgrp->illgrp_ill_count--;
15855 	mutex_exit(&ill->ill_lock);
15856 	rw_exit(&ipst->ips_ill_g_lock);
15857 
15858 	/*
15859 	 * As this ill is leaving the group, we need to hand off
15860 	 * the responsibilities to the other ills in the group, if
15861 	 * this ill had some responsibilities.
15862 	 */
15863 
15864 	ill_handoff_responsibility(ill, illgrp);
15865 
15866 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15867 
15868 	if (illgrp->illgrp_ill_count == 0) {
15869 
15870 		ASSERT(illgrp->illgrp_ill == NULL);
15871 		if (ill->ill_isv6) {
15872 			if (illgrp == ipst->ips_illgrp_head_v6) {
15873 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15874 			} else {
15875 				tmpg = ipst->ips_illgrp_head_v6;
15876 				while (tmpg->illgrp_next != illgrp) {
15877 					tmpg = tmpg->illgrp_next;
15878 					ASSERT(tmpg != NULL);
15879 				}
15880 				tmpg->illgrp_next = illgrp->illgrp_next;
15881 			}
15882 		} else {
15883 			if (illgrp == ipst->ips_illgrp_head_v4) {
15884 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15885 			} else {
15886 				tmpg = ipst->ips_illgrp_head_v4;
15887 				while (tmpg->illgrp_next != illgrp) {
15888 					tmpg = tmpg->illgrp_next;
15889 					ASSERT(tmpg != NULL);
15890 				}
15891 				tmpg->illgrp_next = illgrp->illgrp_next;
15892 			}
15893 		}
15894 		mutex_destroy(&illgrp->illgrp_lock);
15895 		mi_free(illgrp);
15896 	}
15897 	rw_exit(&ipst->ips_ill_g_lock);
15898 
15899 	/*
15900 	 * Even though the ill is out of the group its not necessary
15901 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15902 	 * We will split the ipsq when phyint_groupname is set to NULL.
15903 	 */
15904 
15905 	/*
15906 	 * Send a routing sockets message if we are deleting from
15907 	 * groups with names.
15908 	 */
15909 	if (ill->ill_phyint->phyint_groupname_len != 0)
15910 		ip_rts_ifmsg(ill->ill_ipif);
15911 }
15912 
15913 /*
15914  * Re-do source address selection. This is normally called when
15915  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15916  * ipif comes up.
15917  */
15918 void
15919 ill_update_source_selection(ill_t *ill)
15920 {
15921 	ipif_t *ipif;
15922 
15923 	ASSERT(IAM_WRITER_ILL(ill));
15924 
15925 	if (ill->ill_group != NULL)
15926 		ill = ill->ill_group->illgrp_ill;
15927 
15928 	for (; ill != NULL; ill = ill->ill_group_next) {
15929 		for (ipif = ill->ill_ipif; ipif != NULL;
15930 		    ipif = ipif->ipif_next) {
15931 			if (ill->ill_isv6)
15932 				ipif_recreate_interface_routes_v6(NULL, ipif);
15933 			else
15934 				ipif_recreate_interface_routes(NULL, ipif);
15935 		}
15936 	}
15937 }
15938 
15939 /*
15940  * Insert ill in a group headed by illgrp_head. The caller can either
15941  * pass a groupname in which case we search for a group with the
15942  * same name to insert in or pass a group to insert in. This function
15943  * would only search groups with names.
15944  *
15945  * NOTE : The caller should make sure that there is at least one ipif
15946  *	  UP on this ill so that illgrp_scheduler can pick this ill
15947  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15948  *	  already sent a DL_UNBIND to the driver and we don't want to
15949  *	  send anymore packets. We don't assert for ipif_up_count
15950  *	  to be greater than zero, because ipif_up_done wants to call
15951  *	  this function before bumping up the ipif_up_count. See
15952  *	  ipif_up_done() for details.
15953  */
15954 int
15955 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15956     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15957 {
15958 	ill_group_t *illgrp;
15959 	ill_t *prev_ill;
15960 	phyint_t *phyi;
15961 	ip_stack_t	*ipst = ill->ill_ipst;
15962 
15963 	ASSERT(ill->ill_group == NULL);
15964 
15965 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15966 	mutex_enter(&ill->ill_lock);
15967 
15968 	if (groupname != NULL) {
15969 		/*
15970 		 * Look for a group with a matching groupname to insert.
15971 		 */
15972 		for (illgrp = *illgrp_head; illgrp != NULL;
15973 		    illgrp = illgrp->illgrp_next) {
15974 
15975 			ill_t *tmp_ill;
15976 
15977 			/*
15978 			 * If we have an ill_group_t in the list which has
15979 			 * no ill_t assigned then we must be in the process of
15980 			 * removing this group. We skip this as illgrp_delete()
15981 			 * will remove it from the list.
15982 			 */
15983 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15984 				ASSERT(illgrp->illgrp_ill_count == 0);
15985 				continue;
15986 			}
15987 
15988 			ASSERT(tmp_ill->ill_phyint != NULL);
15989 			phyi = tmp_ill->ill_phyint;
15990 			/*
15991 			 * Look at groups which has names only.
15992 			 */
15993 			if (phyi->phyint_groupname_len == 0)
15994 				continue;
15995 			/*
15996 			 * Names are stored in the phyint common to both
15997 			 * IPv4 and IPv6.
15998 			 */
15999 			if (mi_strcmp(phyi->phyint_groupname,
16000 			    groupname) == 0) {
16001 				break;
16002 			}
16003 		}
16004 	} else {
16005 		/*
16006 		 * If the caller passes in a NULL "grp_to_insert", we
16007 		 * allocate one below and insert this singleton.
16008 		 */
16009 		illgrp = grp_to_insert;
16010 	}
16011 
16012 	ill->ill_group_next = NULL;
16013 
16014 	if (illgrp == NULL) {
16015 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16016 		if (illgrp == NULL) {
16017 			return (ENOMEM);
16018 		}
16019 		illgrp->illgrp_next = *illgrp_head;
16020 		*illgrp_head = illgrp;
16021 		illgrp->illgrp_ill = ill;
16022 		illgrp->illgrp_ill_count = 1;
16023 		ill->ill_group = illgrp;
16024 		/*
16025 		 * Used in illgrp_scheduler to protect multiple threads
16026 		 * from traversing the list.
16027 		 */
16028 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16029 	} else {
16030 		ASSERT(ill->ill_net_type ==
16031 		    illgrp->illgrp_ill->ill_net_type);
16032 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16033 
16034 		/* Insert ill at tail of this group */
16035 		prev_ill = illgrp->illgrp_ill;
16036 		while (prev_ill->ill_group_next != NULL)
16037 			prev_ill = prev_ill->ill_group_next;
16038 		prev_ill->ill_group_next = ill;
16039 		ill->ill_group = illgrp;
16040 		illgrp->illgrp_ill_count++;
16041 		/*
16042 		 * Inherit group properties. Currently only forwarding
16043 		 * is the property we try to keep the same with all the
16044 		 * ills. When there are more, we will abstract this into
16045 		 * a function.
16046 		 */
16047 		ill->ill_flags &= ~ILLF_ROUTER;
16048 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16049 	}
16050 	mutex_exit(&ill->ill_lock);
16051 	rw_exit(&ipst->ips_ill_g_lock);
16052 
16053 	/*
16054 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16055 	 *    may be zero as it has not yet been bumped. But the ires
16056 	 *    have already been added. So, we do the nomination here
16057 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16058 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16059 	 *    ill_ipif_up_count here while nominating broadcast ires for
16060 	 *    receive.
16061 	 *
16062 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16063 	 *    to group them properly as ire_add() has already happened
16064 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16065 	 *    case, we need to do it here anyway.
16066 	 */
16067 	if (!ill->ill_isv6) {
16068 		ill_group_bcast_for_xmit(ill);
16069 		ill_nominate_bcast_rcv(illgrp);
16070 	}
16071 
16072 	if (!ipif_is_coming_up) {
16073 		/*
16074 		 * When ipif_up_done() calls this function, the multicast
16075 		 * groups have not been joined yet. So, there is no point in
16076 		 * nomination. ill_join_allmulti() will handle groups when
16077 		 * ill_recover_multicast() is called from ipif_up_done() later.
16078 		 */
16079 		(void) ill_nominate_mcast_rcv(illgrp);
16080 		/*
16081 		 * ipif_up_done calls ill_update_source_selection
16082 		 * anyway. Moreover, we don't want to re-create
16083 		 * interface routes while ipif_up_done() still has reference
16084 		 * to them. Refer to ipif_up_done() for more details.
16085 		 */
16086 		ill_update_source_selection(ill);
16087 	}
16088 
16089 	/*
16090 	 * Send a routing sockets message if we are inserting into
16091 	 * groups with names.
16092 	 */
16093 	if (groupname != NULL)
16094 		ip_rts_ifmsg(ill->ill_ipif);
16095 	return (0);
16096 }
16097 
16098 /*
16099  * Return the first phyint matching the groupname. There could
16100  * be more than one when there are ill groups.
16101  *
16102  * If 'usable' is set, then we exclude ones that are marked with any of
16103  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16104  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16105  * emulation of ipmp.
16106  */
16107 phyint_t *
16108 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16109 {
16110 	phyint_t *phyi;
16111 
16112 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16113 	/*
16114 	 * Group names are stored in the phyint - a common structure
16115 	 * to both IPv4 and IPv6.
16116 	 */
16117 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16118 	for (; phyi != NULL;
16119 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16120 	    phyi, AVL_AFTER)) {
16121 		if (phyi->phyint_groupname_len == 0)
16122 			continue;
16123 		/*
16124 		 * Skip the ones that should not be used since the callers
16125 		 * sometime use this for sending packets.
16126 		 */
16127 		if (usable && (phyi->phyint_flags &
16128 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16129 			continue;
16130 
16131 		ASSERT(phyi->phyint_groupname != NULL);
16132 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16133 			return (phyi);
16134 	}
16135 	return (NULL);
16136 }
16137 
16138 
16139 /*
16140  * Return the first usable phyint matching the group index. By 'usable'
16141  * we exclude ones that are marked ununsable with any of
16142  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16143  *
16144  * Used only for the ipmp/netinfo emulation of ipmp.
16145  */
16146 phyint_t *
16147 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16148 {
16149 	phyint_t *phyi;
16150 
16151 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16152 
16153 	if (!ipst->ips_ipmp_hook_emulation)
16154 		return (NULL);
16155 
16156 	/*
16157 	 * Group indicies are stored in the phyint - a common structure
16158 	 * to both IPv4 and IPv6.
16159 	 */
16160 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16161 	for (; phyi != NULL;
16162 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16163 	    phyi, AVL_AFTER)) {
16164 		/* Ignore the ones that do not have a group */
16165 		if (phyi->phyint_groupname_len == 0)
16166 			continue;
16167 
16168 		ASSERT(phyi->phyint_group_ifindex != 0);
16169 		/*
16170 		 * Skip the ones that should not be used since the callers
16171 		 * sometime use this for sending packets.
16172 		 */
16173 		if (phyi->phyint_flags &
16174 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16175 			continue;
16176 		if (phyi->phyint_group_ifindex == group_ifindex)
16177 			return (phyi);
16178 	}
16179 	return (NULL);
16180 }
16181 
16182 /*
16183  * MT notes on creation and deletion of IPMP groups
16184  *
16185  * Creation and deletion of IPMP groups introduce the need to merge or
16186  * split the associated serialization objects i.e the ipsq's. Normally all
16187  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16188  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16189  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16190  * is a need to change the <ill-ipsq> association and we have to operate on both
16191  * the source and destination IPMP groups. For eg. attempting to set the
16192  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16193  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16194  * source or destination IPMP group are mapped to a single ipsq for executing
16195  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16196  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16197  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16198  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16199  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16200  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16201  *
16202  * In the above example the ioctl handling code locates the current ipsq of hme0
16203  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16204  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16205  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16206  * the destination ipsq. If the destination ipsq is not busy, it also enters
16207  * the destination ipsq exclusively. Now the actual groupname setting operation
16208  * can proceed. If the destination ipsq is busy, the operation is enqueued
16209  * on the destination (merged) ipsq and will be handled in the unwind from
16210  * ipsq_exit.
16211  *
16212  * To prevent other threads accessing the ill while the group name change is
16213  * in progres, we bring down the ipifs which also removes the ill from the
16214  * group. The group is changed in phyint and when the first ipif on the ill
16215  * is brought up, the ill is inserted into the right IPMP group by
16216  * illgrp_insert.
16217  */
16218 /* ARGSUSED */
16219 int
16220 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16221     ip_ioctl_cmd_t *ipip, void *ifreq)
16222 {
16223 	int i;
16224 	char *tmp;
16225 	int namelen;
16226 	ill_t *ill = ipif->ipif_ill;
16227 	ill_t *ill_v4, *ill_v6;
16228 	int err = 0;
16229 	phyint_t *phyi;
16230 	phyint_t *phyi_tmp;
16231 	struct lifreq *lifr;
16232 	mblk_t	*mp1;
16233 	char *groupname;
16234 	ipsq_t *ipsq;
16235 	ip_stack_t	*ipst = ill->ill_ipst;
16236 
16237 	ASSERT(IAM_WRITER_IPIF(ipif));
16238 
16239 	/* Existance verified in ip_wput_nondata */
16240 	mp1 = mp->b_cont->b_cont;
16241 	lifr = (struct lifreq *)mp1->b_rptr;
16242 	groupname = lifr->lifr_groupname;
16243 
16244 	if (ipif->ipif_id != 0)
16245 		return (EINVAL);
16246 
16247 	phyi = ill->ill_phyint;
16248 	ASSERT(phyi != NULL);
16249 
16250 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16251 		return (EINVAL);
16252 
16253 	tmp = groupname;
16254 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16255 		;
16256 
16257 	if (i == LIFNAMSIZ) {
16258 		/* no null termination */
16259 		return (EINVAL);
16260 	}
16261 
16262 	/*
16263 	 * Calculate the namelen exclusive of the null
16264 	 * termination character.
16265 	 */
16266 	namelen = tmp - groupname;
16267 
16268 	ill_v4 = phyi->phyint_illv4;
16269 	ill_v6 = phyi->phyint_illv6;
16270 
16271 	/*
16272 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16273 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16274 	 * synchronization notes in ip.c
16275 	 */
16276 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16277 		return (EINVAL);
16278 	}
16279 
16280 	/*
16281 	 * mark the ill as changing.
16282 	 * this should queue all new requests on the syncq.
16283 	 */
16284 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16285 
16286 	if (ill_v4 != NULL)
16287 		ill_v4->ill_state_flags |= ILL_CHANGING;
16288 	if (ill_v6 != NULL)
16289 		ill_v6->ill_state_flags |= ILL_CHANGING;
16290 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16291 
16292 	if (namelen == 0) {
16293 		/*
16294 		 * Null string means remove this interface from the
16295 		 * existing group.
16296 		 */
16297 		if (phyi->phyint_groupname_len == 0) {
16298 			/*
16299 			 * Never was in a group.
16300 			 */
16301 			err = 0;
16302 			goto done;
16303 		}
16304 
16305 		/*
16306 		 * IPv4 or IPv6 may be temporarily out of the group when all
16307 		 * the ipifs are down. Thus, we need to check for ill_group to
16308 		 * be non-NULL.
16309 		 */
16310 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16311 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16312 			mutex_enter(&ill_v4->ill_lock);
16313 			if (!ill_is_quiescent(ill_v4)) {
16314 				/*
16315 				 * ipsq_pending_mp_add will not fail since
16316 				 * connp is NULL
16317 				 */
16318 				(void) ipsq_pending_mp_add(NULL,
16319 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16320 				mutex_exit(&ill_v4->ill_lock);
16321 				err = EINPROGRESS;
16322 				goto done;
16323 			}
16324 			mutex_exit(&ill_v4->ill_lock);
16325 		}
16326 
16327 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16328 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16329 			mutex_enter(&ill_v6->ill_lock);
16330 			if (!ill_is_quiescent(ill_v6)) {
16331 				(void) ipsq_pending_mp_add(NULL,
16332 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16333 				mutex_exit(&ill_v6->ill_lock);
16334 				err = EINPROGRESS;
16335 				goto done;
16336 			}
16337 			mutex_exit(&ill_v6->ill_lock);
16338 		}
16339 
16340 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16341 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16342 		mutex_enter(&phyi->phyint_lock);
16343 		ASSERT(phyi->phyint_groupname != NULL);
16344 		mi_free(phyi->phyint_groupname);
16345 		phyi->phyint_groupname = NULL;
16346 		phyi->phyint_groupname_len = 0;
16347 
16348 		/* Restore the ifindex used to be the per interface one */
16349 		phyi->phyint_group_ifindex = 0;
16350 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16351 		mutex_exit(&phyi->phyint_lock);
16352 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16353 		rw_exit(&ipst->ips_ill_g_lock);
16354 		err = ill_up_ipifs(ill, q, mp);
16355 
16356 		/*
16357 		 * set the split flag so that the ipsq can be split
16358 		 */
16359 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16360 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16361 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16362 
16363 	} else {
16364 		if (phyi->phyint_groupname_len != 0) {
16365 			ASSERT(phyi->phyint_groupname != NULL);
16366 			/* Are we inserting in the same group ? */
16367 			if (mi_strcmp(groupname,
16368 			    phyi->phyint_groupname) == 0) {
16369 				err = 0;
16370 				goto done;
16371 			}
16372 		}
16373 
16374 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16375 		/*
16376 		 * Merge ipsq for the group's.
16377 		 * This check is here as multiple groups/ills might be
16378 		 * sharing the same ipsq.
16379 		 * If we have to merege than the operation is restarted
16380 		 * on the new ipsq.
16381 		 */
16382 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16383 		if (phyi->phyint_ipsq != ipsq) {
16384 			rw_exit(&ipst->ips_ill_g_lock);
16385 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16386 			goto done;
16387 		}
16388 		/*
16389 		 * Running exclusive on new ipsq.
16390 		 */
16391 
16392 		ASSERT(ipsq != NULL);
16393 		ASSERT(ipsq->ipsq_writer == curthread);
16394 
16395 		/*
16396 		 * Check whether the ill_type and ill_net_type matches before
16397 		 * we allocate any memory so that the cleanup is easier.
16398 		 *
16399 		 * We can't group dissimilar ones as we can't load spread
16400 		 * packets across the group because of potential link-level
16401 		 * header differences.
16402 		 */
16403 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16404 		if (phyi_tmp != NULL) {
16405 			if ((ill_v4 != NULL &&
16406 			    phyi_tmp->phyint_illv4 != NULL) &&
16407 			    ((ill_v4->ill_net_type !=
16408 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16409 			    (ill_v4->ill_type !=
16410 			    phyi_tmp->phyint_illv4->ill_type))) {
16411 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16412 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16413 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16414 				rw_exit(&ipst->ips_ill_g_lock);
16415 				return (EINVAL);
16416 			}
16417 			if ((ill_v6 != NULL &&
16418 			    phyi_tmp->phyint_illv6 != NULL) &&
16419 			    ((ill_v6->ill_net_type !=
16420 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16421 			    (ill_v6->ill_type !=
16422 			    phyi_tmp->phyint_illv6->ill_type))) {
16423 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16424 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16425 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16426 				rw_exit(&ipst->ips_ill_g_lock);
16427 				return (EINVAL);
16428 			}
16429 		}
16430 
16431 		rw_exit(&ipst->ips_ill_g_lock);
16432 
16433 		/*
16434 		 * bring down all v4 ipifs.
16435 		 */
16436 		if (ill_v4 != NULL) {
16437 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16438 		}
16439 
16440 		/*
16441 		 * bring down all v6 ipifs.
16442 		 */
16443 		if (ill_v6 != NULL) {
16444 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16445 		}
16446 
16447 		/*
16448 		 * make sure all ipifs are down and there are no active
16449 		 * references. Call to ipsq_pending_mp_add will not fail
16450 		 * since connp is NULL.
16451 		 */
16452 		if (ill_v4 != NULL) {
16453 			mutex_enter(&ill_v4->ill_lock);
16454 			if (!ill_is_quiescent(ill_v4)) {
16455 				(void) ipsq_pending_mp_add(NULL,
16456 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16457 				mutex_exit(&ill_v4->ill_lock);
16458 				err = EINPROGRESS;
16459 				goto done;
16460 			}
16461 			mutex_exit(&ill_v4->ill_lock);
16462 		}
16463 
16464 		if (ill_v6 != NULL) {
16465 			mutex_enter(&ill_v6->ill_lock);
16466 			if (!ill_is_quiescent(ill_v6)) {
16467 				(void) ipsq_pending_mp_add(NULL,
16468 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16469 				mutex_exit(&ill_v6->ill_lock);
16470 				err = EINPROGRESS;
16471 				goto done;
16472 			}
16473 			mutex_exit(&ill_v6->ill_lock);
16474 		}
16475 
16476 		/*
16477 		 * allocate including space for null terminator
16478 		 * before we insert.
16479 		 */
16480 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16481 		if (tmp == NULL)
16482 			return (ENOMEM);
16483 
16484 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16485 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16486 		mutex_enter(&phyi->phyint_lock);
16487 		if (phyi->phyint_groupname_len != 0) {
16488 			ASSERT(phyi->phyint_groupname != NULL);
16489 			mi_free(phyi->phyint_groupname);
16490 		}
16491 
16492 		/*
16493 		 * setup the new group name.
16494 		 */
16495 		phyi->phyint_groupname = tmp;
16496 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16497 		phyi->phyint_groupname_len = namelen + 1;
16498 
16499 		if (ipst->ips_ipmp_hook_emulation) {
16500 			/*
16501 			 * If the group already exists we use the existing
16502 			 * group_ifindex, otherwise we pick a new index here.
16503 			 */
16504 			if (phyi_tmp != NULL) {
16505 				phyi->phyint_group_ifindex =
16506 				    phyi_tmp->phyint_group_ifindex;
16507 			} else {
16508 				/* XXX We need a recovery strategy here. */
16509 				if (!ip_assign_ifindex(
16510 				    &phyi->phyint_group_ifindex, ipst))
16511 					cmn_err(CE_PANIC,
16512 					    "ip_assign_ifindex() failed");
16513 			}
16514 		}
16515 		/*
16516 		 * Select whether the netinfo and hook use the per-interface
16517 		 * or per-group ifindex.
16518 		 */
16519 		if (ipst->ips_ipmp_hook_emulation)
16520 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16521 		else
16522 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16523 
16524 		if (ipst->ips_ipmp_hook_emulation &&
16525 		    phyi_tmp != NULL) {
16526 			/* First phyint in group - group PLUMB event */
16527 			ill_nic_event_plumb(ill, B_TRUE);
16528 		}
16529 		mutex_exit(&phyi->phyint_lock);
16530 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16531 		rw_exit(&ipst->ips_ill_g_lock);
16532 
16533 		err = ill_up_ipifs(ill, q, mp);
16534 	}
16535 
16536 done:
16537 	/*
16538 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16539 	 */
16540 	if (err != EINPROGRESS) {
16541 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16542 		if (ill_v4 != NULL)
16543 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16544 		if (ill_v6 != NULL)
16545 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16546 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16547 	}
16548 	return (err);
16549 }
16550 
16551 /* ARGSUSED */
16552 int
16553 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16554     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16555 {
16556 	ill_t *ill;
16557 	phyint_t *phyi;
16558 	struct lifreq *lifr;
16559 	mblk_t	*mp1;
16560 
16561 	/* Existence verified in ip_wput_nondata */
16562 	mp1 = mp->b_cont->b_cont;
16563 	lifr = (struct lifreq *)mp1->b_rptr;
16564 	ill = ipif->ipif_ill;
16565 	phyi = ill->ill_phyint;
16566 
16567 	lifr->lifr_groupname[0] = '\0';
16568 	/*
16569 	 * ill_group may be null if all the interfaces
16570 	 * are down. But still, the phyint should always
16571 	 * hold the name.
16572 	 */
16573 	if (phyi->phyint_groupname_len != 0) {
16574 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16575 		    phyi->phyint_groupname_len);
16576 	}
16577 
16578 	return (0);
16579 }
16580 
16581 
16582 typedef struct conn_move_s {
16583 	ill_t	*cm_from_ill;
16584 	ill_t	*cm_to_ill;
16585 	int	cm_ifindex;
16586 } conn_move_t;
16587 
16588 /*
16589  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16590  */
16591 static void
16592 conn_move(conn_t *connp, caddr_t arg)
16593 {
16594 	conn_move_t *connm;
16595 	int ifindex;
16596 	int i;
16597 	ill_t *from_ill;
16598 	ill_t *to_ill;
16599 	ilg_t *ilg;
16600 	ilm_t *ret_ilm;
16601 
16602 	connm = (conn_move_t *)arg;
16603 	ifindex = connm->cm_ifindex;
16604 	from_ill = connm->cm_from_ill;
16605 	to_ill = connm->cm_to_ill;
16606 
16607 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16608 
16609 	/* All multicast fields protected by conn_lock */
16610 	mutex_enter(&connp->conn_lock);
16611 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16612 	if ((connp->conn_outgoing_ill == from_ill) &&
16613 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16614 		connp->conn_outgoing_ill = to_ill;
16615 		connp->conn_incoming_ill = to_ill;
16616 	}
16617 
16618 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16619 
16620 	if ((connp->conn_multicast_ill == from_ill) &&
16621 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16622 		connp->conn_multicast_ill = connm->cm_to_ill;
16623 	}
16624 
16625 	/*
16626 	 * Change the ilg_ill to point to the new one. This assumes
16627 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16628 	 * has been told to receive packets on this interface.
16629 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16630 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16631 	 * some ilms may not have moved. We check to see whether
16632 	 * the ilms have moved to to_ill. We can't check on from_ill
16633 	 * as in the process of moving, we could have split an ilm
16634 	 * in to two - which has the same orig_ifindex and v6group.
16635 	 *
16636 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16637 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16638 	 */
16639 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16640 		ilg = &connp->conn_ilg[i];
16641 		if ((ilg->ilg_ill == from_ill) &&
16642 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16643 			/* ifindex != 0 indicates failback */
16644 			if (ifindex != 0) {
16645 				connp->conn_ilg[i].ilg_ill = to_ill;
16646 				continue;
16647 			}
16648 
16649 			mutex_enter(&to_ill->ill_lock);
16650 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16651 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16652 			    connp->conn_zoneid);
16653 			mutex_exit(&to_ill->ill_lock);
16654 
16655 			if (ret_ilm != NULL)
16656 				connp->conn_ilg[i].ilg_ill = to_ill;
16657 		}
16658 	}
16659 	mutex_exit(&connp->conn_lock);
16660 }
16661 
16662 static void
16663 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16664 {
16665 	conn_move_t connm;
16666 	ip_stack_t	*ipst = from_ill->ill_ipst;
16667 
16668 	connm.cm_from_ill = from_ill;
16669 	connm.cm_to_ill = to_ill;
16670 	connm.cm_ifindex = ifindex;
16671 
16672 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16673 }
16674 
16675 /*
16676  * ilm has been moved from from_ill to to_ill.
16677  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16678  * appropriately.
16679  *
16680  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16681  *	  the code there de-references ipif_ill to get the ill to
16682  *	  send multicast requests. It does not work as ipif is on its
16683  *	  move and already moved when this function is called.
16684  *	  Thus, we need to use from_ill and to_ill send down multicast
16685  *	  requests.
16686  */
16687 static void
16688 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16689 {
16690 	ipif_t *ipif;
16691 	ilm_t *ilm;
16692 
16693 	/*
16694 	 * See whether we need to send down DL_ENABMULTI_REQ on
16695 	 * to_ill as ilm has just been added.
16696 	 */
16697 	ASSERT(IAM_WRITER_ILL(to_ill));
16698 	ASSERT(IAM_WRITER_ILL(from_ill));
16699 
16700 	ILM_WALKER_HOLD(to_ill);
16701 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16702 
16703 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16704 			continue;
16705 		/*
16706 		 * no locks held, ill/ipif cannot dissappear as long
16707 		 * as we are writer.
16708 		 */
16709 		ipif = to_ill->ill_ipif;
16710 		/*
16711 		 * No need to hold any lock as we are the writer and this
16712 		 * can only be changed by a writer.
16713 		 */
16714 		ilm->ilm_is_new = B_FALSE;
16715 
16716 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16717 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16718 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16719 			    "resolver\n"));
16720 			continue;		/* Must be IRE_IF_NORESOLVER */
16721 		}
16722 
16723 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16724 			ip1dbg(("ilm_send_multicast_reqs: "
16725 			    "to_ill MULTI_BCAST\n"));
16726 			goto from;
16727 		}
16728 
16729 		if (to_ill->ill_isv6)
16730 			mld_joingroup(ilm);
16731 		else
16732 			igmp_joingroup(ilm);
16733 
16734 		if (to_ill->ill_ipif_up_count == 0) {
16735 			/*
16736 			 * Nobody there. All multicast addresses will be
16737 			 * re-joined when we get the DL_BIND_ACK bringing the
16738 			 * interface up.
16739 			 */
16740 			ilm->ilm_notify_driver = B_FALSE;
16741 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16742 			goto from;
16743 		}
16744 
16745 		/*
16746 		 * For allmulti address, we want to join on only one interface.
16747 		 * Checking for ilm_numentries_v6 is not correct as you may
16748 		 * find an ilm with zero address on to_ill, but we may not
16749 		 * have nominated to_ill for receiving. Thus, if we have
16750 		 * nominated from_ill (ill_join_allmulti is set), nominate
16751 		 * only if to_ill is not already nominated (to_ill normally
16752 		 * should not have been nominated if "from_ill" has already
16753 		 * been nominated. As we don't prevent failovers from happening
16754 		 * across groups, we don't assert).
16755 		 */
16756 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16757 			/*
16758 			 * There is no need to hold ill locks as we are
16759 			 * writer on both ills and when ill_join_allmulti()
16760 			 * is called the thread is always a writer.
16761 			 */
16762 			if (from_ill->ill_join_allmulti &&
16763 			    !to_ill->ill_join_allmulti) {
16764 				(void) ill_join_allmulti(to_ill);
16765 			}
16766 		} else if (ilm->ilm_notify_driver) {
16767 
16768 			/*
16769 			 * This is a newly moved ilm so we need to tell the
16770 			 * driver about the new group. There can be more than
16771 			 * one ilm's for the same group in the list each with a
16772 			 * different orig_ifindex. We have to inform the driver
16773 			 * once. In ilm_move_v[4,6] we only set the flag
16774 			 * ilm_notify_driver for the first ilm.
16775 			 */
16776 
16777 			(void) ip_ll_send_enabmulti_req(to_ill,
16778 			    &ilm->ilm_v6addr);
16779 		}
16780 
16781 		ilm->ilm_notify_driver = B_FALSE;
16782 
16783 		/*
16784 		 * See whether we need to send down DL_DISABMULTI_REQ on
16785 		 * from_ill as ilm has just been removed.
16786 		 */
16787 from:
16788 		ipif = from_ill->ill_ipif;
16789 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16790 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16791 			ip1dbg(("ilm_send_multicast_reqs: "
16792 			    "from_ill not resolver\n"));
16793 			continue;		/* Must be IRE_IF_NORESOLVER */
16794 		}
16795 
16796 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16797 			ip1dbg(("ilm_send_multicast_reqs: "
16798 			    "from_ill MULTI_BCAST\n"));
16799 			continue;
16800 		}
16801 
16802 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16803 			if (from_ill->ill_join_allmulti)
16804 				ill_leave_allmulti(from_ill);
16805 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16806 			(void) ip_ll_send_disabmulti_req(from_ill,
16807 			    &ilm->ilm_v6addr);
16808 		}
16809 	}
16810 	ILM_WALKER_RELE(to_ill);
16811 }
16812 
16813 /*
16814  * This function is called when all multicast memberships needs
16815  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16816  * called only once unlike the IPv4 counterpart where it is called after
16817  * every logical interface is moved. The reason is due to multicast
16818  * memberships are joined using an interface address in IPv4 while in
16819  * IPv6, interface index is used.
16820  */
16821 static void
16822 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16823 {
16824 	ilm_t	*ilm;
16825 	ilm_t	*ilm_next;
16826 	ilm_t	*new_ilm;
16827 	ilm_t	**ilmp;
16828 	int	count;
16829 	char buf[INET6_ADDRSTRLEN];
16830 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16831 	ip_stack_t	*ipst = from_ill->ill_ipst;
16832 
16833 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16834 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16835 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16836 
16837 	if (ifindex == 0) {
16838 		/*
16839 		 * Form the solicited node mcast address which is used later.
16840 		 */
16841 		ipif_t *ipif;
16842 
16843 		ipif = from_ill->ill_ipif;
16844 		ASSERT(ipif->ipif_id == 0);
16845 
16846 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16847 	}
16848 
16849 	ilmp = &from_ill->ill_ilm;
16850 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16851 		ilm_next = ilm->ilm_next;
16852 
16853 		if (ilm->ilm_flags & ILM_DELETED) {
16854 			ilmp = &ilm->ilm_next;
16855 			continue;
16856 		}
16857 
16858 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16859 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16860 		ASSERT(ilm->ilm_orig_ifindex != 0);
16861 		if (ilm->ilm_orig_ifindex == ifindex) {
16862 			/*
16863 			 * We are failing back multicast memberships.
16864 			 * If the same ilm exists in to_ill, it means somebody
16865 			 * has joined the same group there e.g. ff02::1
16866 			 * is joined within the kernel when the interfaces
16867 			 * came UP.
16868 			 */
16869 			ASSERT(ilm->ilm_ipif == NULL);
16870 			if (new_ilm != NULL) {
16871 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16872 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16873 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16874 					new_ilm->ilm_is_new = B_TRUE;
16875 				}
16876 			} else {
16877 				/*
16878 				 * check if we can just move the ilm
16879 				 */
16880 				if (from_ill->ill_ilm_walker_cnt != 0) {
16881 					/*
16882 					 * We have walkers we cannot move
16883 					 * the ilm, so allocate a new ilm,
16884 					 * this (old) ilm will be marked
16885 					 * ILM_DELETED at the end of the loop
16886 					 * and will be freed when the
16887 					 * last walker exits.
16888 					 */
16889 					new_ilm = (ilm_t *)mi_zalloc
16890 					    (sizeof (ilm_t));
16891 					if (new_ilm == NULL) {
16892 						ip0dbg(("ilm_move_v6: "
16893 						    "FAILBACK of IPv6"
16894 						    " multicast address %s : "
16895 						    "from %s to"
16896 						    " %s failed : ENOMEM \n",
16897 						    inet_ntop(AF_INET6,
16898 						    &ilm->ilm_v6addr, buf,
16899 						    sizeof (buf)),
16900 						    from_ill->ill_name,
16901 						    to_ill->ill_name));
16902 
16903 							ilmp = &ilm->ilm_next;
16904 							continue;
16905 					}
16906 					*new_ilm = *ilm;
16907 					/*
16908 					 * we don't want new_ilm linked to
16909 					 * ilm's filter list.
16910 					 */
16911 					new_ilm->ilm_filter = NULL;
16912 				} else {
16913 					/*
16914 					 * No walkers we can move the ilm.
16915 					 * lets take it out of the list.
16916 					 */
16917 					*ilmp = ilm->ilm_next;
16918 					ilm->ilm_next = NULL;
16919 					DTRACE_PROBE3(ill__decr__cnt,
16920 					    (ill_t *), from_ill,
16921 					    (char *), "ilm", (void *), ilm);
16922 					ASSERT(from_ill->ill_ilm_cnt > 0);
16923 					from_ill->ill_ilm_cnt--;
16924 
16925 					new_ilm = ilm;
16926 				}
16927 
16928 				/*
16929 				 * if this is the first ilm for the group
16930 				 * set ilm_notify_driver so that we notify the
16931 				 * driver in ilm_send_multicast_reqs.
16932 				 */
16933 				if (ilm_lookup_ill_v6(to_ill,
16934 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16935 					new_ilm->ilm_notify_driver = B_TRUE;
16936 
16937 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16938 				    (char *), "ilm", (void *), new_ilm);
16939 				new_ilm->ilm_ill = to_ill;
16940 				to_ill->ill_ilm_cnt++;
16941 
16942 				/* Add to the to_ill's list */
16943 				new_ilm->ilm_next = to_ill->ill_ilm;
16944 				to_ill->ill_ilm = new_ilm;
16945 				/*
16946 				 * set the flag so that mld_joingroup is
16947 				 * called in ilm_send_multicast_reqs().
16948 				 */
16949 				new_ilm->ilm_is_new = B_TRUE;
16950 			}
16951 			goto bottom;
16952 		} else if (ifindex != 0) {
16953 			/*
16954 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16955 			 * has not matched above, look at the next ilm.
16956 			 */
16957 			ilmp = &ilm->ilm_next;
16958 			continue;
16959 		}
16960 		/*
16961 		 * If we are here, it means ifindex is 0. Failover
16962 		 * everything.
16963 		 *
16964 		 * We need to handle solicited node mcast address
16965 		 * and all_nodes mcast address differently as they
16966 		 * are joined witin the kenrel (ipif_multicast_up)
16967 		 * and potentially from the userland. We are called
16968 		 * after the ipifs of from_ill has been moved.
16969 		 * If we still find ilms on ill with solicited node
16970 		 * mcast address or all_nodes mcast address, it must
16971 		 * belong to the UP interface that has not moved e.g.
16972 		 * ipif_id 0 with the link local prefix does not move.
16973 		 * We join this on the new ill accounting for all the
16974 		 * userland memberships so that applications don't
16975 		 * see any failure.
16976 		 *
16977 		 * We need to make sure that we account only for the
16978 		 * solicited node and all node multicast addresses
16979 		 * that was brought UP on these. In the case of
16980 		 * a failover from A to B, we might have ilms belonging
16981 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16982 		 * for the membership from the userland. If we are failing
16983 		 * over from B to C now, we will find the ones belonging
16984 		 * to A on B. These don't account for the ill_ipif_up_count.
16985 		 * They just move from B to C. The check below on
16986 		 * ilm_orig_ifindex ensures that.
16987 		 */
16988 		if ((ilm->ilm_orig_ifindex ==
16989 		    from_ill->ill_phyint->phyint_ifindex) &&
16990 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16991 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16992 		    &ilm->ilm_v6addr))) {
16993 			ASSERT(ilm->ilm_refcnt > 0);
16994 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16995 			/*
16996 			 * For indentation reasons, we are not using a
16997 			 * "else" here.
16998 			 */
16999 			if (count == 0) {
17000 				ilmp = &ilm->ilm_next;
17001 				continue;
17002 			}
17003 			ilm->ilm_refcnt -= count;
17004 			if (new_ilm != NULL) {
17005 				/*
17006 				 * Can find one with the same
17007 				 * ilm_orig_ifindex, if we are failing
17008 				 * over to a STANDBY. This happens
17009 				 * when somebody wants to join a group
17010 				 * on a STANDBY interface and we
17011 				 * internally join on a different one.
17012 				 * If we had joined on from_ill then, a
17013 				 * failover now will find a new ilm
17014 				 * with this index.
17015 				 */
17016 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17017 				    " new ilm on %s, group address %s\n",
17018 				    to_ill->ill_name,
17019 				    inet_ntop(AF_INET6,
17020 				    &ilm->ilm_v6addr, buf,
17021 				    sizeof (buf))));
17022 				new_ilm->ilm_refcnt += count;
17023 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17024 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17025 					new_ilm->ilm_is_new = B_TRUE;
17026 				}
17027 			} else {
17028 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17029 				if (new_ilm == NULL) {
17030 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17031 					    " multicast address %s : from %s to"
17032 					    " %s failed : ENOMEM \n",
17033 					    inet_ntop(AF_INET6,
17034 					    &ilm->ilm_v6addr, buf,
17035 					    sizeof (buf)), from_ill->ill_name,
17036 					    to_ill->ill_name));
17037 					ilmp = &ilm->ilm_next;
17038 					continue;
17039 				}
17040 				*new_ilm = *ilm;
17041 				new_ilm->ilm_filter = NULL;
17042 				new_ilm->ilm_refcnt = count;
17043 				new_ilm->ilm_timer = INFINITY;
17044 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17045 				new_ilm->ilm_is_new = B_TRUE;
17046 				/*
17047 				 * If the to_ill has not joined this
17048 				 * group we need to tell the driver in
17049 				 * ill_send_multicast_reqs.
17050 				 */
17051 				if (ilm_lookup_ill_v6(to_ill,
17052 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17053 					new_ilm->ilm_notify_driver = B_TRUE;
17054 
17055 				new_ilm->ilm_ill = to_ill;
17056 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17057 				    (char *), "ilm", (void *), new_ilm);
17058 				to_ill->ill_ilm_cnt++;
17059 
17060 				/* Add to the to_ill's list */
17061 				new_ilm->ilm_next = to_ill->ill_ilm;
17062 				to_ill->ill_ilm = new_ilm;
17063 				ASSERT(new_ilm->ilm_ipif == NULL);
17064 			}
17065 			if (ilm->ilm_refcnt == 0) {
17066 				goto bottom;
17067 			} else {
17068 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17069 				CLEAR_SLIST(new_ilm->ilm_filter);
17070 				ilmp = &ilm->ilm_next;
17071 			}
17072 			continue;
17073 		} else {
17074 			/*
17075 			 * ifindex = 0 means, move everything pointing at
17076 			 * from_ill. We are doing this becuase ill has
17077 			 * either FAILED or became INACTIVE.
17078 			 *
17079 			 * As we would like to move things later back to
17080 			 * from_ill, we want to retain the identity of this
17081 			 * ilm. Thus, we don't blindly increment the reference
17082 			 * count on the ilms matching the address alone. We
17083 			 * need to match on the ilm_orig_index also. new_ilm
17084 			 * was obtained by matching ilm_orig_index also.
17085 			 */
17086 			if (new_ilm != NULL) {
17087 				/*
17088 				 * This is possible only if a previous restore
17089 				 * was incomplete i.e restore to
17090 				 * ilm_orig_ifindex left some ilms because
17091 				 * of some failures. Thus when we are failing
17092 				 * again, we might find our old friends there.
17093 				 */
17094 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17095 				    " on %s, group address %s\n",
17096 				    to_ill->ill_name,
17097 				    inet_ntop(AF_INET6,
17098 				    &ilm->ilm_v6addr, buf,
17099 				    sizeof (buf))));
17100 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17101 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17102 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17103 					new_ilm->ilm_is_new = B_TRUE;
17104 				}
17105 			} else {
17106 				if (from_ill->ill_ilm_walker_cnt != 0) {
17107 					new_ilm = (ilm_t *)
17108 					    mi_zalloc(sizeof (ilm_t));
17109 					if (new_ilm == NULL) {
17110 						ip0dbg(("ilm_move_v6: "
17111 						    "FAILOVER of IPv6"
17112 						    " multicast address %s : "
17113 						    "from %s to"
17114 						    " %s failed : ENOMEM \n",
17115 						    inet_ntop(AF_INET6,
17116 						    &ilm->ilm_v6addr, buf,
17117 						    sizeof (buf)),
17118 						    from_ill->ill_name,
17119 						    to_ill->ill_name));
17120 
17121 							ilmp = &ilm->ilm_next;
17122 							continue;
17123 					}
17124 					*new_ilm = *ilm;
17125 					new_ilm->ilm_filter = NULL;
17126 				} else {
17127 					*ilmp = ilm->ilm_next;
17128 					DTRACE_PROBE3(ill__decr__cnt,
17129 					    (ill_t *), from_ill,
17130 					    (char *), "ilm", (void *), ilm);
17131 					ASSERT(from_ill->ill_ilm_cnt > 0);
17132 					from_ill->ill_ilm_cnt--;
17133 
17134 					new_ilm = ilm;
17135 				}
17136 				/*
17137 				 * If the to_ill has not joined this
17138 				 * group we need to tell the driver in
17139 				 * ill_send_multicast_reqs.
17140 				 */
17141 				if (ilm_lookup_ill_v6(to_ill,
17142 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17143 					new_ilm->ilm_notify_driver = B_TRUE;
17144 
17145 				/* Add to the to_ill's list */
17146 				new_ilm->ilm_next = to_ill->ill_ilm;
17147 				to_ill->ill_ilm = new_ilm;
17148 				ASSERT(ilm->ilm_ipif == NULL);
17149 				new_ilm->ilm_ill = to_ill;
17150 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17151 				    (char *), "ilm", (void *), new_ilm);
17152 				to_ill->ill_ilm_cnt++;
17153 				new_ilm->ilm_is_new = B_TRUE;
17154 			}
17155 
17156 		}
17157 
17158 bottom:
17159 		/*
17160 		 * Revert multicast filter state to (EXCLUDE, NULL).
17161 		 * new_ilm->ilm_is_new should already be set if needed.
17162 		 */
17163 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17164 		CLEAR_SLIST(new_ilm->ilm_filter);
17165 		/*
17166 		 * We allocated/got a new ilm, free the old one.
17167 		 */
17168 		if (new_ilm != ilm) {
17169 			if (from_ill->ill_ilm_walker_cnt == 0) {
17170 				*ilmp = ilm->ilm_next;
17171 
17172 				ASSERT(ilm->ilm_ipif == NULL); /* ipv6 */
17173 				DTRACE_PROBE3(ill__decr__cnt, (ill_t *),
17174 				    from_ill, (char *), "ilm", (void *), ilm);
17175 				ASSERT(from_ill->ill_ilm_cnt > 0);
17176 				from_ill->ill_ilm_cnt--;
17177 
17178 				ilm_inactive(ilm); /* frees this ilm */
17179 
17180 			} else {
17181 				ilm->ilm_flags |= ILM_DELETED;
17182 				from_ill->ill_ilm_cleanup_reqd = 1;
17183 				ilmp = &ilm->ilm_next;
17184 			}
17185 		}
17186 	}
17187 }
17188 
17189 /*
17190  * Move all the multicast memberships to to_ill. Called when
17191  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17192  * different from IPv6 counterpart as multicast memberships are associated
17193  * with ills in IPv6. This function is called after every ipif is moved
17194  * unlike IPv6, where it is moved only once.
17195  */
17196 static void
17197 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17198 {
17199 	ilm_t	*ilm;
17200 	ilm_t	*ilm_next;
17201 	ilm_t	*new_ilm;
17202 	ilm_t	**ilmp;
17203 	ip_stack_t	*ipst = from_ill->ill_ipst;
17204 
17205 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17206 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17207 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17208 
17209 	ilmp = &from_ill->ill_ilm;
17210 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17211 		ilm_next = ilm->ilm_next;
17212 
17213 		if (ilm->ilm_flags & ILM_DELETED) {
17214 			ilmp = &ilm->ilm_next;
17215 			continue;
17216 		}
17217 
17218 		ASSERT(ilm->ilm_ipif != NULL);
17219 
17220 		if (ilm->ilm_ipif != ipif) {
17221 			ilmp = &ilm->ilm_next;
17222 			continue;
17223 		}
17224 
17225 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17226 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17227 			new_ilm = ilm_lookup_ipif(ipif,
17228 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17229 			if (new_ilm != NULL) {
17230 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17231 				/*
17232 				 * We still need to deal with the from_ill.
17233 				 */
17234 				new_ilm->ilm_is_new = B_TRUE;
17235 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17236 				CLEAR_SLIST(new_ilm->ilm_filter);
17237 				ASSERT(ilm->ilm_ipif == ipif);
17238 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17239 				if (from_ill->ill_ilm_walker_cnt == 0) {
17240 					DTRACE_PROBE3(ill__decr__cnt,
17241 					    (ill_t *), from_ill,
17242 					    (char *), "ilm", (void *), ilm);
17243 					ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17244 				}
17245 				goto delete_ilm;
17246 			}
17247 			/*
17248 			 * If we could not find one e.g. ipif is
17249 			 * still down on to_ill, we add this ilm
17250 			 * on ill_new to preserve the reference
17251 			 * count.
17252 			 */
17253 		}
17254 		/*
17255 		 * When ipifs move, ilms always move with it
17256 		 * to the NEW ill. Thus we should never be
17257 		 * able to find ilm till we really move it here.
17258 		 */
17259 		ASSERT(ilm_lookup_ipif(ipif,
17260 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17261 
17262 		if (from_ill->ill_ilm_walker_cnt != 0) {
17263 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17264 			if (new_ilm == NULL) {
17265 				char buf[INET6_ADDRSTRLEN];
17266 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17267 				    " multicast address %s : "
17268 				    "from %s to"
17269 				    " %s failed : ENOMEM \n",
17270 				    inet_ntop(AF_INET,
17271 				    &ilm->ilm_v6addr, buf,
17272 				    sizeof (buf)),
17273 				    from_ill->ill_name,
17274 				    to_ill->ill_name));
17275 
17276 				ilmp = &ilm->ilm_next;
17277 				continue;
17278 			}
17279 			*new_ilm = *ilm;
17280 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
17281 			    (char *), "ilm", (void *), ilm);
17282 			new_ilm->ilm_ipif->ipif_ilm_cnt++;
17283 			/* We don't want new_ilm linked to ilm's filter list */
17284 			new_ilm->ilm_filter = NULL;
17285 		} else {
17286 			/* Remove from the list */
17287 			*ilmp = ilm->ilm_next;
17288 			new_ilm = ilm;
17289 		}
17290 
17291 		/*
17292 		 * If we have never joined this group on the to_ill
17293 		 * make sure we tell the driver.
17294 		 */
17295 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17296 		    ALL_ZONES) == NULL)
17297 			new_ilm->ilm_notify_driver = B_TRUE;
17298 
17299 		/* Add to the to_ill's list */
17300 		new_ilm->ilm_next = to_ill->ill_ilm;
17301 		to_ill->ill_ilm = new_ilm;
17302 		new_ilm->ilm_is_new = B_TRUE;
17303 
17304 		/*
17305 		 * Revert multicast filter state to (EXCLUDE, NULL)
17306 		 */
17307 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17308 		CLEAR_SLIST(new_ilm->ilm_filter);
17309 
17310 		/*
17311 		 * Delete only if we have allocated a new ilm.
17312 		 */
17313 		if (new_ilm != ilm) {
17314 delete_ilm:
17315 			if (from_ill->ill_ilm_walker_cnt == 0) {
17316 				/* Remove from the list */
17317 				*ilmp = ilm->ilm_next;
17318 				ilm->ilm_next = NULL;
17319 				DTRACE_PROBE3(ipif__decr__cnt,
17320 				    (ipif_t *), ilm->ilm_ipif,
17321 				    (char *), "ilm", (void *), ilm);
17322 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17323 				ilm->ilm_ipif->ipif_ilm_cnt--;
17324 				ilm_inactive(ilm);
17325 			} else {
17326 				ilm->ilm_flags |= ILM_DELETED;
17327 				from_ill->ill_ilm_cleanup_reqd = 1;
17328 				ilmp = &ilm->ilm_next;
17329 			}
17330 		}
17331 	}
17332 }
17333 
17334 static uint_t
17335 ipif_get_id(ill_t *ill, uint_t id)
17336 {
17337 	uint_t	unit;
17338 	ipif_t	*tipif;
17339 	boolean_t found = B_FALSE;
17340 	ip_stack_t	*ipst = ill->ill_ipst;
17341 
17342 	/*
17343 	 * During failback, we want to go back to the same id
17344 	 * instead of the smallest id so that the original
17345 	 * configuration is maintained. id is non-zero in that
17346 	 * case.
17347 	 */
17348 	if (id != 0) {
17349 		/*
17350 		 * While failing back, if we still have an ipif with
17351 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17352 		 * as soon as we return from this function. It was
17353 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17354 		 * we can choose the smallest id. Thus we return zero
17355 		 * in that case ignoring the hint.
17356 		 */
17357 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17358 			return (0);
17359 		for (tipif = ill->ill_ipif; tipif != NULL;
17360 		    tipif = tipif->ipif_next) {
17361 			if (tipif->ipif_id == id) {
17362 				found = B_TRUE;
17363 				break;
17364 			}
17365 		}
17366 		/*
17367 		 * If somebody already plumbed another logical
17368 		 * with the same id, we won't be able to find it.
17369 		 */
17370 		if (!found)
17371 			return (id);
17372 	}
17373 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17374 		found = B_FALSE;
17375 		for (tipif = ill->ill_ipif; tipif != NULL;
17376 		    tipif = tipif->ipif_next) {
17377 			if (tipif->ipif_id == unit) {
17378 				found = B_TRUE;
17379 				break;
17380 			}
17381 		}
17382 		if (!found)
17383 			break;
17384 	}
17385 	return (unit);
17386 }
17387 
17388 /* ARGSUSED */
17389 static int
17390 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17391     ipif_t **rep_ipif_ptr)
17392 {
17393 	ill_t	*from_ill;
17394 	ipif_t	*rep_ipif;
17395 	uint_t	unit;
17396 	int err = 0;
17397 	ipif_t	*to_ipif;
17398 	struct iocblk	*iocp;
17399 	boolean_t failback_cmd;
17400 	boolean_t remove_ipif;
17401 	int	rc;
17402 	ip_stack_t	*ipst;
17403 
17404 	ASSERT(IAM_WRITER_ILL(to_ill));
17405 	ASSERT(IAM_WRITER_IPIF(ipif));
17406 
17407 	iocp = (struct iocblk *)mp->b_rptr;
17408 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17409 	remove_ipif = B_FALSE;
17410 
17411 	from_ill = ipif->ipif_ill;
17412 	ipst = from_ill->ill_ipst;
17413 
17414 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17415 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17416 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17417 
17418 	/*
17419 	 * Don't move LINK LOCAL addresses as they are tied to
17420 	 * physical interface.
17421 	 */
17422 	if (from_ill->ill_isv6 &&
17423 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17424 		ipif->ipif_was_up = B_FALSE;
17425 		IPIF_UNMARK_MOVING(ipif);
17426 		return (0);
17427 	}
17428 
17429 	/*
17430 	 * We set the ipif_id to maximum so that the search for
17431 	 * ipif_id will pick the lowest number i.e 0 in the
17432 	 * following 2 cases :
17433 	 *
17434 	 * 1) We have a replacement ipif at the head of to_ill.
17435 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17436 	 *    on to_ill and hence the MOVE might fail. We want to
17437 	 *    remove it only if we could move the ipif. Thus, by
17438 	 *    setting it to the MAX value, we make the search in
17439 	 *    ipif_get_id return the zeroth id.
17440 	 *
17441 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17442 	 *    we might just have a zero address plumbed on the ipif
17443 	 *    with zero id in the case of IPv4. We remove that while
17444 	 *    doing the failback. We want to remove it only if we
17445 	 *    could move the ipif. Thus, by setting it to the MAX
17446 	 *    value, we make the search in ipif_get_id return the
17447 	 *    zeroth id.
17448 	 *
17449 	 * Both (1) and (2) are done only when when we are moving
17450 	 * an ipif (either due to failover/failback) which originally
17451 	 * belonged to this interface i.e the ipif_orig_ifindex is
17452 	 * the same as to_ill's ifindex. This is needed so that
17453 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17454 	 * from B -> A (B is being removed from the group) and
17455 	 * FAILBACK from A -> B restores the original configuration.
17456 	 * Without the check for orig_ifindex, the second FAILOVER
17457 	 * could make the ipif belonging to B replace the A's zeroth
17458 	 * ipif and the subsequent failback re-creating the replacement
17459 	 * ipif again.
17460 	 *
17461 	 * NOTE : We created the replacement ipif when we did a
17462 	 * FAILOVER (See below). We could check for FAILBACK and
17463 	 * then look for replacement ipif to be removed. But we don't
17464 	 * want to do that because we wan't to allow the possibility
17465 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17466 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17467 	 * from B -> A.
17468 	 */
17469 	to_ipif = to_ill->ill_ipif;
17470 	if ((to_ill->ill_phyint->phyint_ifindex ==
17471 	    ipif->ipif_orig_ifindex) &&
17472 	    to_ipif->ipif_replace_zero) {
17473 		ASSERT(to_ipif->ipif_id == 0);
17474 		remove_ipif = B_TRUE;
17475 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17476 	}
17477 	/*
17478 	 * Find the lowest logical unit number on the to_ill.
17479 	 * If we are failing back, try to get the original id
17480 	 * rather than the lowest one so that the original
17481 	 * configuration is maintained.
17482 	 *
17483 	 * XXX need a better scheme for this.
17484 	 */
17485 	if (failback_cmd) {
17486 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17487 	} else {
17488 		unit = ipif_get_id(to_ill, 0);
17489 	}
17490 
17491 	/* Reset back to zero in case we fail below */
17492 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17493 		to_ipif->ipif_id = 0;
17494 
17495 	if (unit == ipst->ips_ip_addrs_per_if) {
17496 		ipif->ipif_was_up = B_FALSE;
17497 		IPIF_UNMARK_MOVING(ipif);
17498 		return (EINVAL);
17499 	}
17500 
17501 	/*
17502 	 * ipif is ready to move from "from_ill" to "to_ill".
17503 	 *
17504 	 * 1) If we are moving ipif with id zero, create a
17505 	 *    replacement ipif for this ipif on from_ill. If this fails
17506 	 *    fail the MOVE operation.
17507 	 *
17508 	 * 2) Remove the replacement ipif on to_ill if any.
17509 	 *    We could remove the replacement ipif when we are moving
17510 	 *    the ipif with id zero. But what if somebody already
17511 	 *    unplumbed it ? Thus we always remove it if it is present.
17512 	 *    We want to do it only if we are sure we are going to
17513 	 *    move the ipif to to_ill which is why there are no
17514 	 *    returns due to error till ipif is linked to to_ill.
17515 	 *    Note that the first ipif that we failback will always
17516 	 *    be zero if it is present.
17517 	 */
17518 	if (ipif->ipif_id == 0) {
17519 		ipaddr_t inaddr_any = INADDR_ANY;
17520 
17521 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17522 		if (rep_ipif == NULL) {
17523 			ipif->ipif_was_up = B_FALSE;
17524 			IPIF_UNMARK_MOVING(ipif);
17525 			return (ENOMEM);
17526 		}
17527 		*rep_ipif = ipif_zero;
17528 		/*
17529 		 * Before we put the ipif on the list, store the addresses
17530 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17531 		 * assumes so. This logic is not any different from what
17532 		 * ipif_allocate does.
17533 		 */
17534 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17535 		    &rep_ipif->ipif_v6lcl_addr);
17536 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17537 		    &rep_ipif->ipif_v6src_addr);
17538 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17539 		    &rep_ipif->ipif_v6subnet);
17540 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17541 		    &rep_ipif->ipif_v6net_mask);
17542 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17543 		    &rep_ipif->ipif_v6brd_addr);
17544 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17545 		    &rep_ipif->ipif_v6pp_dst_addr);
17546 		/*
17547 		 * We mark IPIF_NOFAILOVER so that this can never
17548 		 * move.
17549 		 */
17550 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17551 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17552 		rep_ipif->ipif_replace_zero = B_TRUE;
17553 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17554 		    MUTEX_DEFAULT, NULL);
17555 		rep_ipif->ipif_id = 0;
17556 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17557 		rep_ipif->ipif_ill = from_ill;
17558 		rep_ipif->ipif_orig_ifindex =
17559 		    from_ill->ill_phyint->phyint_ifindex;
17560 		/* Insert at head */
17561 		rep_ipif->ipif_next = from_ill->ill_ipif;
17562 		from_ill->ill_ipif = rep_ipif;
17563 		/*
17564 		 * We don't really care to let apps know about
17565 		 * this interface.
17566 		 */
17567 	}
17568 
17569 	if (remove_ipif) {
17570 		/*
17571 		 * We set to a max value above for this case to get
17572 		 * id zero. ASSERT that we did get one.
17573 		 */
17574 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17575 		rep_ipif = to_ipif;
17576 		to_ill->ill_ipif = rep_ipif->ipif_next;
17577 		rep_ipif->ipif_next = NULL;
17578 		/*
17579 		 * If some apps scanned and find this interface,
17580 		 * it is time to let them know, so that they can
17581 		 * delete it.
17582 		 */
17583 
17584 		*rep_ipif_ptr = rep_ipif;
17585 	}
17586 
17587 	/* Get it out of the ILL interface list. */
17588 	ipif_remove(ipif, B_FALSE);
17589 
17590 	/* Assign the new ill */
17591 	ipif->ipif_ill = to_ill;
17592 	ipif->ipif_id = unit;
17593 	/* id has already been checked */
17594 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17595 	ASSERT(rc == 0);
17596 	/* Let SCTP update its list */
17597 	sctp_move_ipif(ipif, from_ill, to_ill);
17598 	/*
17599 	 * Handle the failover and failback of ipif_t between
17600 	 * ill_t that have differing maximum mtu values.
17601 	 */
17602 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17603 		if (ipif->ipif_saved_mtu == 0) {
17604 			/*
17605 			 * As this ipif_t is moving to an ill_t
17606 			 * that has a lower ill_max_mtu, its
17607 			 * ipif_mtu needs to be saved so it can
17608 			 * be restored during failback or during
17609 			 * failover to an ill_t which has a
17610 			 * higher ill_max_mtu.
17611 			 */
17612 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17613 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17614 		} else {
17615 			/*
17616 			 * The ipif_t is, once again, moving to
17617 			 * an ill_t that has a lower maximum mtu
17618 			 * value.
17619 			 */
17620 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17621 		}
17622 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17623 	    ipif->ipif_saved_mtu != 0) {
17624 		/*
17625 		 * The mtu of this ipif_t had to be reduced
17626 		 * during an earlier failover; this is an
17627 		 * opportunity for it to be increased (either as
17628 		 * part of another failover or a failback).
17629 		 */
17630 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17631 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17632 			ipif->ipif_saved_mtu = 0;
17633 		} else {
17634 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17635 		}
17636 	}
17637 
17638 	/*
17639 	 * We preserve all the other fields of the ipif including
17640 	 * ipif_saved_ire_mp. The routes that are saved here will
17641 	 * be recreated on the new interface and back on the old
17642 	 * interface when we move back.
17643 	 */
17644 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17645 
17646 	return (err);
17647 }
17648 
17649 static int
17650 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17651     int ifindex, ipif_t **rep_ipif_ptr)
17652 {
17653 	ipif_t *mipif;
17654 	ipif_t *ipif_next;
17655 	int err;
17656 
17657 	/*
17658 	 * We don't really try to MOVE back things if some of the
17659 	 * operations fail. The daemon will take care of moving again
17660 	 * later on.
17661 	 */
17662 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17663 		ipif_next = mipif->ipif_next;
17664 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17665 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17666 
17667 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17668 
17669 			/*
17670 			 * When the MOVE fails, it is the job of the
17671 			 * application to take care of this properly
17672 			 * i.e try again if it is ENOMEM.
17673 			 */
17674 			if (mipif->ipif_ill != from_ill) {
17675 				/*
17676 				 * ipif has moved.
17677 				 *
17678 				 * Move the multicast memberships associated
17679 				 * with this ipif to the new ill. For IPv6, we
17680 				 * do it once after all the ipifs are moved
17681 				 * (in ill_move) as they are not associated
17682 				 * with ipifs.
17683 				 *
17684 				 * We need to move the ilms as the ipif has
17685 				 * already been moved to a new ill even
17686 				 * in the case of errors. Neither
17687 				 * ilm_free(ipif) will find the ilm
17688 				 * when somebody unplumbs this ipif nor
17689 				 * ilm_delete(ilm) will be able to find the
17690 				 * ilm, if we don't move now.
17691 				 */
17692 				if (!from_ill->ill_isv6)
17693 					ilm_move_v4(from_ill, to_ill, mipif);
17694 			}
17695 
17696 			if (err != 0)
17697 				return (err);
17698 		}
17699 	}
17700 	return (0);
17701 }
17702 
17703 static int
17704 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17705 {
17706 	int ifindex;
17707 	int err;
17708 	struct iocblk	*iocp;
17709 	ipif_t	*ipif;
17710 	ipif_t *rep_ipif_ptr = NULL;
17711 	ipif_t	*from_ipif = NULL;
17712 	boolean_t check_rep_if = B_FALSE;
17713 	ip_stack_t	*ipst = from_ill->ill_ipst;
17714 
17715 	iocp = (struct iocblk *)mp->b_rptr;
17716 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17717 		/*
17718 		 * Move everything pointing at from_ill to to_ill.
17719 		 * We acheive this by passing in 0 as ifindex.
17720 		 */
17721 		ifindex = 0;
17722 	} else {
17723 		/*
17724 		 * Move everything pointing at from_ill whose original
17725 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17726 		 * We acheive this by passing in ifindex rather than 0.
17727 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17728 		 */
17729 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17730 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17731 	}
17732 
17733 	/*
17734 	 * Determine if there is at least one ipif that would move from
17735 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17736 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17737 	 * the move, in which case we need to quiesce the replacement ipif also.
17738 	 */
17739 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17740 	    from_ipif = from_ipif->ipif_next) {
17741 		if (((ifindex == 0) ||
17742 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17743 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17744 			check_rep_if = B_TRUE;
17745 			break;
17746 		}
17747 	}
17748 
17749 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17750 
17751 	GRAB_ILL_LOCKS(from_ill, to_ill);
17752 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17753 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17754 		    mp, ILL_MOVE_OK);
17755 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17756 		return (EINPROGRESS);
17757 	}
17758 
17759 	/* Check if the replacement ipif is quiescent to delete */
17760 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17761 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17762 		to_ill->ill_ipif->ipif_state_flags |=
17763 		    IPIF_MOVING | IPIF_CHANGING;
17764 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17765 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17766 			    mp, ILL_MOVE_OK);
17767 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17768 			return (EINPROGRESS);
17769 		}
17770 	}
17771 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17772 
17773 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17774 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17775 	GRAB_ILL_LOCKS(from_ill, to_ill);
17776 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17777 
17778 	/* ilm_move is done inside ipif_move for IPv4 */
17779 	if (err == 0 && from_ill->ill_isv6)
17780 		ilm_move_v6(from_ill, to_ill, ifindex);
17781 
17782 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17783 	rw_exit(&ipst->ips_ill_g_lock);
17784 
17785 	/*
17786 	 * send rts messages and multicast messages.
17787 	 */
17788 	if (rep_ipif_ptr != NULL) {
17789 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17790 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17791 			rep_ipif_ptr->ipif_recovery_id = 0;
17792 		}
17793 		ip_rts_ifmsg(rep_ipif_ptr);
17794 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17795 #ifdef DEBUG
17796 		ipif_trace_cleanup(rep_ipif_ptr);
17797 #endif
17798 		mi_free(rep_ipif_ptr);
17799 	}
17800 
17801 	conn_move_ill(from_ill, to_ill, ifindex);
17802 
17803 	return (err);
17804 }
17805 
17806 /*
17807  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17808  * Also checks for the validity of the arguments.
17809  * Note: We are already exclusive inside the from group.
17810  * It is upto the caller to release refcnt on the to_ill's.
17811  */
17812 static int
17813 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17814     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17815 {
17816 	int dst_index;
17817 	ipif_t *ipif_v4, *ipif_v6;
17818 	struct lifreq *lifr;
17819 	mblk_t *mp1;
17820 	boolean_t exists;
17821 	sin_t	*sin;
17822 	int	err = 0;
17823 	ip_stack_t	*ipst;
17824 
17825 	if (CONN_Q(q))
17826 		ipst = CONNQ_TO_IPST(q);
17827 	else
17828 		ipst = ILLQ_TO_IPST(q);
17829 
17830 	if ((mp1 = mp->b_cont) == NULL)
17831 		return (EPROTO);
17832 
17833 	if ((mp1 = mp1->b_cont) == NULL)
17834 		return (EPROTO);
17835 
17836 	lifr = (struct lifreq *)mp1->b_rptr;
17837 	sin = (sin_t *)&lifr->lifr_addr;
17838 
17839 	/*
17840 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17841 	 * specific operations.
17842 	 */
17843 	if (sin->sin_family != AF_UNSPEC)
17844 		return (EINVAL);
17845 
17846 	/*
17847 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17848 	 * NULLs for the last 4 args and we know the lookup won't fail
17849 	 * with EINPROGRESS.
17850 	 */
17851 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17852 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17853 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17854 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17855 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17856 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17857 
17858 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17859 		return (ENXIO);
17860 
17861 	if (ipif_v4 != NULL) {
17862 		ASSERT(ipif_v4->ipif_refcnt != 0);
17863 		if (ipif_v4->ipif_id != 0) {
17864 			err = EINVAL;
17865 			goto done;
17866 		}
17867 
17868 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17869 		*ill_from_v4 = ipif_v4->ipif_ill;
17870 	}
17871 
17872 	if (ipif_v6 != NULL) {
17873 		ASSERT(ipif_v6->ipif_refcnt != 0);
17874 		if (ipif_v6->ipif_id != 0) {
17875 			err = EINVAL;
17876 			goto done;
17877 		}
17878 
17879 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17880 		*ill_from_v6 = ipif_v6->ipif_ill;
17881 	}
17882 
17883 	err = 0;
17884 	dst_index = lifr->lifr_movetoindex;
17885 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17886 	    q, mp, ip_process_ioctl, &err, ipst);
17887 	if (err != 0) {
17888 		/*
17889 		 * A move may be in progress, EINPROGRESS looking up the "to"
17890 		 * ill means changes already done to the "from" ipsq need to
17891 		 * be undone to avoid potential deadlocks.
17892 		 *
17893 		 * ENXIO will usually be because there is only v6 on the ill,
17894 		 * that's not treated as an error unless an ENXIO is also
17895 		 * seen when looking up the v6 "to" ill.
17896 		 *
17897 		 * If EINPROGRESS, the mp has been enqueued and can not be
17898 		 * used to look up the v6 "to" ill, but a preemptive clean
17899 		 * up of changes to the v6 "from" ipsq is done.
17900 		 */
17901 		if (err == EINPROGRESS) {
17902 			if (*ill_from_v4 != NULL) {
17903 				ill_t   *from_ill;
17904 				ipsq_t  *from_ipsq;
17905 
17906 				from_ill = ipif_v4->ipif_ill;
17907 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17908 
17909 				mutex_enter(&from_ipsq->ipsq_lock);
17910 				from_ipsq->ipsq_current_ipif = NULL;
17911 				mutex_exit(&from_ipsq->ipsq_lock);
17912 			}
17913 			if (*ill_from_v6 != NULL) {
17914 				ill_t   *from_ill;
17915 				ipsq_t  *from_ipsq;
17916 
17917 				from_ill = ipif_v6->ipif_ill;
17918 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17919 
17920 				mutex_enter(&from_ipsq->ipsq_lock);
17921 				from_ipsq->ipsq_current_ipif = NULL;
17922 				mutex_exit(&from_ipsq->ipsq_lock);
17923 			}
17924 			goto done;
17925 		}
17926 		ASSERT(err == ENXIO);
17927 		err = 0;
17928 	}
17929 
17930 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17931 	    q, mp, ip_process_ioctl, &err, ipst);
17932 	if (err != 0) {
17933 		/*
17934 		 * A move may be in progress, EINPROGRESS looking up the "to"
17935 		 * ill means changes already done to the "from" ipsq need to
17936 		 * be undone to avoid potential deadlocks.
17937 		 */
17938 		if (err == EINPROGRESS) {
17939 			if (*ill_from_v6 != NULL) {
17940 				ill_t   *from_ill;
17941 				ipsq_t  *from_ipsq;
17942 
17943 				from_ill = ipif_v6->ipif_ill;
17944 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17945 
17946 				mutex_enter(&from_ipsq->ipsq_lock);
17947 				from_ipsq->ipsq_current_ipif = NULL;
17948 				mutex_exit(&from_ipsq->ipsq_lock);
17949 			}
17950 			goto done;
17951 		}
17952 		ASSERT(err == ENXIO);
17953 
17954 		/* Both v4 and v6 lookup failed */
17955 		if (*ill_to_v4 == NULL) {
17956 			err = ENXIO;
17957 			goto done;
17958 		}
17959 		err = 0;
17960 	}
17961 
17962 	/*
17963 	 * If we have something to MOVE i.e "from" not NULL,
17964 	 * "to" should be non-NULL.
17965 	 */
17966 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17967 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17968 		err = EINVAL;
17969 	}
17970 
17971 done:
17972 	if (ipif_v4 != NULL)
17973 		ipif_refrele(ipif_v4);
17974 	if (ipif_v6 != NULL)
17975 		ipif_refrele(ipif_v6);
17976 	return (err);
17977 }
17978 
17979 /*
17980  * FAILOVER and FAILBACK are modelled as MOVE operations.
17981  *
17982  * We don't check whether the MOVE is within the same group or
17983  * not, because this ioctl can be used as a generic mechanism
17984  * to failover from interface A to B, though things will function
17985  * only if they are really part of the same group. Moreover,
17986  * all ipifs may be down and hence temporarily out of the group.
17987  *
17988  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17989  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17990  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17991  * have been deleted and there are no active references. Once quiescent the
17992  * ipif's are moved and brought up on the new ill.
17993  *
17994  * Normally the source ill and destination ill belong to the same IPMP group
17995  * and hence the same ipsq_t. In the event they don't belong to the same
17996  * same group the two ipsq's are first merged into one ipsq - that of the
17997  * to_ill. The multicast memberships on the source and destination ill cannot
17998  * change during the move operation since multicast joins/leaves also have to
17999  * execute on the same ipsq and are hence serialized.
18000  */
18001 /* ARGSUSED */
18002 int
18003 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18004     ip_ioctl_cmd_t *ipip, void *ifreq)
18005 {
18006 	ill_t *ill_to_v4 = NULL;
18007 	ill_t *ill_to_v6 = NULL;
18008 	ill_t *ill_from_v4 = NULL;
18009 	ill_t *ill_from_v6 = NULL;
18010 	int err = 0;
18011 
18012 	/*
18013 	 * setup from and to ill's, we can get EINPROGRESS only for
18014 	 * to_ill's.
18015 	 */
18016 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18017 	    &ill_to_v4, &ill_to_v6);
18018 
18019 	if (err != 0) {
18020 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18021 		goto done;
18022 	}
18023 
18024 	/*
18025 	 * nothing to do.
18026 	 */
18027 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18028 		goto done;
18029 	}
18030 
18031 	/*
18032 	 * nothing to do.
18033 	 */
18034 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18035 		goto done;
18036 	}
18037 
18038 	/*
18039 	 * Mark the ill as changing.
18040 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18041 	 * in ill_up_ipifs in case of error they are cleared below.
18042 	 */
18043 
18044 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18045 	if (ill_from_v4 != NULL)
18046 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18047 	if (ill_from_v6 != NULL)
18048 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18049 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18050 
18051 	/*
18052 	 * Make sure that both src and dst are
18053 	 * in the same syncq group. If not make it happen.
18054 	 * We are not holding any locks because we are the writer
18055 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18056 	 * to protect to_ipsq against changing.
18057 	 */
18058 	if (ill_from_v4 != NULL) {
18059 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18060 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18061 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18062 			    NULL, mp, q);
18063 			goto err_ret;
18064 
18065 		}
18066 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18067 	} else {
18068 
18069 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18070 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18071 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18072 			    NULL, mp, q);
18073 			goto err_ret;
18074 
18075 		}
18076 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18077 	}
18078 
18079 	/*
18080 	 * Now that the ipsq's have been merged and we are the writer
18081 	 * lets mark to_ill as changing as well.
18082 	 */
18083 
18084 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18085 	if (ill_to_v4 != NULL)
18086 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18087 	if (ill_to_v6 != NULL)
18088 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18089 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18090 
18091 	/*
18092 	 * Its ok for us to proceed with the move even if
18093 	 * ill_pending_mp is non null on one of the from ill's as the reply
18094 	 * should not be looking at the ipif, it should only care about the
18095 	 * ill itself.
18096 	 */
18097 
18098 	/*
18099 	 * lets move ipv4 first.
18100 	 */
18101 	if (ill_from_v4 != NULL) {
18102 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18103 		ill_from_v4->ill_move_in_progress = B_TRUE;
18104 		ill_to_v4->ill_move_in_progress = B_TRUE;
18105 		ill_to_v4->ill_move_peer = ill_from_v4;
18106 		ill_from_v4->ill_move_peer = ill_to_v4;
18107 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18108 	}
18109 
18110 	/*
18111 	 * Now lets move ipv6.
18112 	 */
18113 	if (err == 0 && ill_from_v6 != NULL) {
18114 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18115 		ill_from_v6->ill_move_in_progress = B_TRUE;
18116 		ill_to_v6->ill_move_in_progress = B_TRUE;
18117 		ill_to_v6->ill_move_peer = ill_from_v6;
18118 		ill_from_v6->ill_move_peer = ill_to_v6;
18119 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18120 	}
18121 
18122 err_ret:
18123 	/*
18124 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18125 	 * moved to become quiescent.
18126 	 */
18127 	if (err == EINPROGRESS) {
18128 		goto done;
18129 	}
18130 
18131 	/*
18132 	 * if err is set ill_up_ipifs will not be called
18133 	 * lets clear the flags.
18134 	 */
18135 
18136 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18137 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18138 	/*
18139 	 * Some of the clearing may be redundant. But it is simple
18140 	 * not making any extra checks.
18141 	 */
18142 	if (ill_from_v6 != NULL) {
18143 		ill_from_v6->ill_move_in_progress = B_FALSE;
18144 		ill_from_v6->ill_move_peer = NULL;
18145 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18146 	}
18147 	if (ill_from_v4 != NULL) {
18148 		ill_from_v4->ill_move_in_progress = B_FALSE;
18149 		ill_from_v4->ill_move_peer = NULL;
18150 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18151 	}
18152 	if (ill_to_v6 != NULL) {
18153 		ill_to_v6->ill_move_in_progress = B_FALSE;
18154 		ill_to_v6->ill_move_peer = NULL;
18155 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18156 	}
18157 	if (ill_to_v4 != NULL) {
18158 		ill_to_v4->ill_move_in_progress = B_FALSE;
18159 		ill_to_v4->ill_move_peer = NULL;
18160 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18161 	}
18162 
18163 	/*
18164 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18165 	 * Do this always to maintain proper state i.e even in case of errors.
18166 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18167 	 * we need not call on both v4 and v6 interfaces.
18168 	 */
18169 	if (ill_from_v4 != NULL) {
18170 		if ((ill_from_v4->ill_phyint->phyint_flags &
18171 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18172 			phyint_inactive(ill_from_v4->ill_phyint);
18173 		}
18174 	} else if (ill_from_v6 != NULL) {
18175 		if ((ill_from_v6->ill_phyint->phyint_flags &
18176 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18177 			phyint_inactive(ill_from_v6->ill_phyint);
18178 		}
18179 	}
18180 
18181 	if (ill_to_v4 != NULL) {
18182 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18183 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18184 		}
18185 	} else if (ill_to_v6 != NULL) {
18186 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18187 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18188 		}
18189 	}
18190 
18191 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18192 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18193 
18194 no_err:
18195 	/*
18196 	 * lets bring the interfaces up on the to_ill.
18197 	 */
18198 	if (err == 0) {
18199 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18200 		    q, mp);
18201 	}
18202 
18203 	if (err == 0) {
18204 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18205 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18206 
18207 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18208 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18209 	}
18210 done:
18211 
18212 	if (ill_to_v4 != NULL) {
18213 		ill_refrele(ill_to_v4);
18214 	}
18215 	if (ill_to_v6 != NULL) {
18216 		ill_refrele(ill_to_v6);
18217 	}
18218 
18219 	return (err);
18220 }
18221 
18222 static void
18223 ill_dl_down(ill_t *ill)
18224 {
18225 	/*
18226 	 * The ill is down; unbind but stay attached since we're still
18227 	 * associated with a PPA. If we have negotiated DLPI capabilites
18228 	 * with the data link service provider (IDS_OK) then reset them.
18229 	 * The interval between unbinding and rebinding is potentially
18230 	 * unbounded hence we cannot assume things will be the same.
18231 	 * The DLPI capabilities will be probed again when the data link
18232 	 * is brought up.
18233 	 */
18234 	mblk_t	*mp = ill->ill_unbind_mp;
18235 
18236 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18237 
18238 	ill->ill_unbind_mp = NULL;
18239 	if (mp != NULL) {
18240 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18241 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18242 		    ill->ill_name));
18243 		mutex_enter(&ill->ill_lock);
18244 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18245 		mutex_exit(&ill->ill_lock);
18246 		/*
18247 		 * Reset the capabilities if the negotiation is done or is
18248 		 * still in progress. Note that ill_capability_reset() will
18249 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18250 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18251 		 *
18252 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18253 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18254 		 * the capabilities renegotiation from happening.
18255 		 */
18256 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18257 			ill_capability_reset(ill);
18258 		ill->ill_capab_reneg = B_FALSE;
18259 
18260 		ill_dlpi_send(ill, mp);
18261 	}
18262 
18263 	/*
18264 	 * Toss all of our multicast memberships.  We could keep them, but
18265 	 * then we'd have to do bookkeeping of any joins and leaves performed
18266 	 * by the application while the the interface is down (we can't just
18267 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18268 	 * on a downed interface).
18269 	 */
18270 	ill_leave_multicast(ill);
18271 
18272 	mutex_enter(&ill->ill_lock);
18273 	ill->ill_dl_up = 0;
18274 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
18275 	mutex_exit(&ill->ill_lock);
18276 }
18277 
18278 static void
18279 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18280 {
18281 	union DL_primitives *dlp;
18282 	t_uscalar_t prim;
18283 
18284 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18285 
18286 	dlp = (union DL_primitives *)mp->b_rptr;
18287 	prim = dlp->dl_primitive;
18288 
18289 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18290 	    dl_primstr(prim), prim, ill->ill_name));
18291 
18292 	switch (prim) {
18293 	case DL_PHYS_ADDR_REQ:
18294 	{
18295 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18296 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18297 		break;
18298 	}
18299 	case DL_BIND_REQ:
18300 		mutex_enter(&ill->ill_lock);
18301 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18302 		mutex_exit(&ill->ill_lock);
18303 		break;
18304 	}
18305 
18306 	/*
18307 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18308 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18309 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18310 	 */
18311 	mutex_enter(&ill->ill_lock);
18312 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18313 	    (prim == DL_UNBIND_REQ)) {
18314 		ill->ill_dlpi_pending = prim;
18315 	}
18316 	mutex_exit(&ill->ill_lock);
18317 
18318 	putnext(ill->ill_wq, mp);
18319 }
18320 
18321 /*
18322  * Helper function for ill_dlpi_send().
18323  */
18324 /* ARGSUSED */
18325 static void
18326 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18327 {
18328 	ill_dlpi_send(q->q_ptr, mp);
18329 }
18330 
18331 /*
18332  * Send a DLPI control message to the driver but make sure there
18333  * is only one outstanding message. Uses ill_dlpi_pending to tell
18334  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18335  * when an ACK or a NAK is received to process the next queued message.
18336  */
18337 void
18338 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18339 {
18340 	mblk_t **mpp;
18341 
18342 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18343 
18344 	/*
18345 	 * To ensure that any DLPI requests for current exclusive operation
18346 	 * are always completely sent before any DLPI messages for other
18347 	 * operations, require writer access before enqueuing.
18348 	 */
18349 	if (!IAM_WRITER_ILL(ill)) {
18350 		ill_refhold(ill);
18351 		/* qwriter_ip() does the ill_refrele() */
18352 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18353 		    NEW_OP, B_TRUE);
18354 		return;
18355 	}
18356 
18357 	mutex_enter(&ill->ill_lock);
18358 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18359 		/* Must queue message. Tail insertion */
18360 		mpp = &ill->ill_dlpi_deferred;
18361 		while (*mpp != NULL)
18362 			mpp = &((*mpp)->b_next);
18363 
18364 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18365 		    ill->ill_name));
18366 
18367 		*mpp = mp;
18368 		mutex_exit(&ill->ill_lock);
18369 		return;
18370 	}
18371 	mutex_exit(&ill->ill_lock);
18372 	ill_dlpi_dispatch(ill, mp);
18373 }
18374 
18375 /*
18376  * Send all deferred DLPI messages without waiting for their ACKs.
18377  */
18378 void
18379 ill_dlpi_send_deferred(ill_t *ill)
18380 {
18381 	mblk_t *mp, *nextmp;
18382 
18383 	/*
18384 	 * Clear ill_dlpi_pending so that the message is not queued in
18385 	 * ill_dlpi_send().
18386 	 */
18387 	mutex_enter(&ill->ill_lock);
18388 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18389 	mp = ill->ill_dlpi_deferred;
18390 	ill->ill_dlpi_deferred = NULL;
18391 	mutex_exit(&ill->ill_lock);
18392 
18393 	for (; mp != NULL; mp = nextmp) {
18394 		nextmp = mp->b_next;
18395 		mp->b_next = NULL;
18396 		ill_dlpi_send(ill, mp);
18397 	}
18398 }
18399 
18400 /*
18401  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18402  */
18403 boolean_t
18404 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18405 {
18406 	t_uscalar_t pending;
18407 
18408 	mutex_enter(&ill->ill_lock);
18409 	if (ill->ill_dlpi_pending == prim) {
18410 		mutex_exit(&ill->ill_lock);
18411 		return (B_TRUE);
18412 	}
18413 
18414 	/*
18415 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18416 	 * without waiting, so don't print any warnings in that case.
18417 	 */
18418 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18419 		mutex_exit(&ill->ill_lock);
18420 		return (B_FALSE);
18421 	}
18422 	pending = ill->ill_dlpi_pending;
18423 	mutex_exit(&ill->ill_lock);
18424 
18425 	if (pending == DL_PRIM_INVAL) {
18426 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18427 		    "received unsolicited ack for %s on %s\n",
18428 		    dl_primstr(prim), ill->ill_name);
18429 	} else {
18430 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18431 		    "received unexpected ack for %s on %s (expecting %s)\n",
18432 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18433 	}
18434 	return (B_FALSE);
18435 }
18436 
18437 /*
18438  * Complete the current DLPI operation associated with `prim' on `ill' and
18439  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18440  * operations and the ill's current exclusive IPSQ operation has finished
18441  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18442  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18443  * the comments above ipsq_current_finish() for details.
18444  */
18445 void
18446 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18447 {
18448 	mblk_t *mp;
18449 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18450 
18451 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18452 	mutex_enter(&ill->ill_lock);
18453 
18454 	ASSERT(prim != DL_PRIM_INVAL);
18455 	ASSERT(ill->ill_dlpi_pending == prim);
18456 
18457 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18458 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18459 
18460 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18461 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18462 
18463 		mutex_enter(&ipsq->ipsq_lock);
18464 		if (ipsq->ipsq_current_done)
18465 			ipsq->ipsq_current_ipif = NULL;
18466 		mutex_exit(&ipsq->ipsq_lock);
18467 
18468 		cv_signal(&ill->ill_cv);
18469 		mutex_exit(&ill->ill_lock);
18470 		return;
18471 	}
18472 
18473 	ill->ill_dlpi_deferred = mp->b_next;
18474 	mp->b_next = NULL;
18475 	mutex_exit(&ill->ill_lock);
18476 
18477 	ill_dlpi_dispatch(ill, mp);
18478 }
18479 
18480 void
18481 conn_delete_ire(conn_t *connp, caddr_t arg)
18482 {
18483 	ipif_t	*ipif = (ipif_t *)arg;
18484 	ire_t	*ire;
18485 
18486 	/*
18487 	 * Look at the cached ires on conns which has pointers to ipifs.
18488 	 * We just call ire_refrele which clears up the reference
18489 	 * to ire. Called when a conn closes. Also called from ipif_free
18490 	 * to cleanup indirect references to the stale ipif via the cached ire.
18491 	 */
18492 	mutex_enter(&connp->conn_lock);
18493 	ire = connp->conn_ire_cache;
18494 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18495 		connp->conn_ire_cache = NULL;
18496 		mutex_exit(&connp->conn_lock);
18497 		IRE_REFRELE_NOTR(ire);
18498 		return;
18499 	}
18500 	mutex_exit(&connp->conn_lock);
18501 
18502 }
18503 
18504 /*
18505  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18506  * of IREs. Those IREs may have been previously cached in the conn structure.
18507  * This ipcl_walk() walker function releases all references to such IREs based
18508  * on the condemned flag.
18509  */
18510 /* ARGSUSED */
18511 void
18512 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18513 {
18514 	ire_t	*ire;
18515 
18516 	mutex_enter(&connp->conn_lock);
18517 	ire = connp->conn_ire_cache;
18518 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18519 		connp->conn_ire_cache = NULL;
18520 		mutex_exit(&connp->conn_lock);
18521 		IRE_REFRELE_NOTR(ire);
18522 		return;
18523 	}
18524 	mutex_exit(&connp->conn_lock);
18525 }
18526 
18527 /*
18528  * Take down a specific interface, but don't lose any information about it.
18529  * Also delete interface from its interface group (ifgrp).
18530  * (Always called as writer.)
18531  * This function goes through the down sequence even if the interface is
18532  * already down. There are 2 reasons.
18533  * a. Currently we permit interface routes that depend on down interfaces
18534  *    to be added. This behaviour itself is questionable. However it appears
18535  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18536  *    time. We go thru the cleanup in order to remove these routes.
18537  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18538  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18539  *    down, but we need to cleanup i.e. do ill_dl_down and
18540  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18541  *
18542  * IP-MT notes:
18543  *
18544  * Model of reference to interfaces.
18545  *
18546  * The following members in ipif_t track references to the ipif.
18547  *	int     ipif_refcnt;    Active reference count
18548  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18549  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18550  *
18551  * The following members in ill_t track references to the ill.
18552  *	int             ill_refcnt;     active refcnt
18553  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18554  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18555  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18556  *
18557  * Reference to an ipif or ill can be obtained in any of the following ways.
18558  *
18559  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18560  * Pointers to ipif / ill from other data structures viz ire and conn.
18561  * Implicit reference to the ipif / ill by holding a reference to the ire.
18562  *
18563  * The ipif/ill lookup functions return a reference held ipif / ill.
18564  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18565  * This is a purely dynamic reference count associated with threads holding
18566  * references to the ipif / ill. Pointers from other structures do not
18567  * count towards this reference count.
18568  *
18569  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18570  * associated with the ipif/ill. This is incremented whenever a new
18571  * ire is created referencing the ipif/ill. This is done atomically inside
18572  * ire_add_v[46] where the ire is actually added to the ire hash table.
18573  * The count is decremented in ire_inactive where the ire is destroyed.
18574  *
18575  * nce's reference ill's thru nce_ill and the count of nce's associated with
18576  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18577  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18578  * table. Similarly it is decremented in ndp_inactive() where the nce
18579  * is destroyed.
18580  *
18581  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18582  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18583  * in ilm_walker_cleanup() or ilm_delete().
18584  *
18585  * Flow of ioctls involving interface down/up
18586  *
18587  * The following is the sequence of an attempt to set some critical flags on an
18588  * up interface.
18589  * ip_sioctl_flags
18590  * ipif_down
18591  * wait for ipif to be quiescent
18592  * ipif_down_tail
18593  * ip_sioctl_flags_tail
18594  *
18595  * All set ioctls that involve down/up sequence would have a skeleton similar
18596  * to the above. All the *tail functions are called after the refcounts have
18597  * dropped to the appropriate values.
18598  *
18599  * The mechanism to quiesce an ipif is as follows.
18600  *
18601  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18602  * on the ipif. Callers either pass a flag requesting wait or the lookup
18603  *  functions will return NULL.
18604  *
18605  * Delete all ires referencing this ipif
18606  *
18607  * Any thread attempting to do an ipif_refhold on an ipif that has been
18608  * obtained thru a cached pointer will first make sure that
18609  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18610  * increment the refcount.
18611  *
18612  * The above guarantees that the ipif refcount will eventually come down to
18613  * zero and the ipif will quiesce, once all threads that currently hold a
18614  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18615  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18616  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18617  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18618  * in ip.h
18619  *
18620  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18621  *
18622  * Threads trying to lookup an ipif or ill can pass a flag requesting
18623  * wait and restart if the ipif / ill cannot be looked up currently.
18624  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18625  * failure if the ipif is currently undergoing an exclusive operation, and
18626  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18627  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18628  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18629  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18630  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18631  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18632  * until we release the ipsq_lock, even though the the ill/ipif state flags
18633  * can change after we drop the ill_lock.
18634  *
18635  * An attempt to send out a packet using an ipif that is currently
18636  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18637  * operation and restart it later when the exclusive condition on the ipif ends.
18638  * This is an example of not passing the wait flag to the lookup functions. For
18639  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18640  * out a multicast packet on that ipif will fail while the ipif is
18641  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18642  * currently IPIF_CHANGING will also fail.
18643  */
18644 int
18645 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18646 {
18647 	ill_t		*ill = ipif->ipif_ill;
18648 	phyint_t	*phyi;
18649 	conn_t		*connp;
18650 	boolean_t	success;
18651 	boolean_t	ipif_was_up = B_FALSE;
18652 	ip_stack_t	*ipst = ill->ill_ipst;
18653 
18654 	ASSERT(IAM_WRITER_IPIF(ipif));
18655 
18656 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18657 
18658 	if (ipif->ipif_flags & IPIF_UP) {
18659 		mutex_enter(&ill->ill_lock);
18660 		ipif->ipif_flags &= ~IPIF_UP;
18661 		ASSERT(ill->ill_ipif_up_count > 0);
18662 		--ill->ill_ipif_up_count;
18663 		mutex_exit(&ill->ill_lock);
18664 		ipif_was_up = B_TRUE;
18665 		/* Update status in SCTP's list */
18666 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18667 		ill_nic_event_dispatch(ipif->ipif_ill,
18668 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
18669 	}
18670 
18671 	/*
18672 	 * Blow away memberships we established in ipif_multicast_up().
18673 	 */
18674 	ipif_multicast_down(ipif);
18675 
18676 	/*
18677 	 * Remove from the mapping for __sin6_src_id. We insert only
18678 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18679 	 * stored as mapped addresses, we need to check for mapped
18680 	 * INADDR_ANY also.
18681 	 */
18682 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18683 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18684 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18685 		int err;
18686 
18687 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18688 		    ipif->ipif_zoneid, ipst);
18689 		if (err != 0) {
18690 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18691 		}
18692 	}
18693 
18694 	/*
18695 	 * Before we delete the ill from the group (if any), we need
18696 	 * to make sure that we delete all the routes dependent on
18697 	 * this and also any ipifs dependent on this ipif for
18698 	 * source address. We need to do before we delete from
18699 	 * the group because
18700 	 *
18701 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18702 	 *
18703 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18704 	 *    for re-doing source address selection. Note that
18705 	 *    ipif_select_source[_v6] called from
18706 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18707 	 *    because we have already marked down here i.e cleared
18708 	 *    IPIF_UP.
18709 	 */
18710 	if (ipif->ipif_isv6) {
18711 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18712 		    ipst);
18713 	} else {
18714 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18715 		    ipst);
18716 	}
18717 
18718 	/*
18719 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18720 	 * ires have been deleted above. Otherwise a thread could end up
18721 	 * caching an ire in a conn after we have finished the cleanup of the
18722 	 * conn. The caching is done after making sure that the ire is not yet
18723 	 * condemned. Also documented in the block comment above ip_output
18724 	 */
18725 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18726 	/* Also, delete the ires cached in SCTP */
18727 	sctp_ire_cache_flush(ipif);
18728 
18729 	/*
18730 	 * Update any other ipifs which have used "our" local address as
18731 	 * a source address. This entails removing and recreating IRE_INTERFACE
18732 	 * entries for such ipifs.
18733 	 */
18734 	if (ipif->ipif_isv6)
18735 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18736 	else
18737 		ipif_update_other_ipifs(ipif, ill->ill_group);
18738 
18739 	if (ipif_was_up) {
18740 		/*
18741 		 * Check whether it is last ipif to leave this group.
18742 		 * If this is the last ipif to leave, we should remove
18743 		 * this ill from the group as ipif_select_source will not
18744 		 * be able to find any useful ipifs if this ill is selected
18745 		 * for load balancing.
18746 		 *
18747 		 * For nameless groups, we should call ifgrp_delete if this
18748 		 * belongs to some group. As this ipif is going down, we may
18749 		 * need to reconstruct groups.
18750 		 */
18751 		phyi = ill->ill_phyint;
18752 		/*
18753 		 * If the phyint_groupname_len is 0, it may or may not
18754 		 * be in the nameless group. If the phyint_groupname_len is
18755 		 * not 0, then this ill should be part of some group.
18756 		 * As we always insert this ill in the group if
18757 		 * phyint_groupname_len is not zero when the first ipif
18758 		 * comes up (in ipif_up_done), it should be in a group
18759 		 * when the namelen is not 0.
18760 		 *
18761 		 * NOTE : When we delete the ill from the group,it will
18762 		 * blow away all the IRE_CACHES pointing either at this ipif or
18763 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18764 		 * should be pointing at this ill.
18765 		 */
18766 		ASSERT(phyi->phyint_groupname_len == 0 ||
18767 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18768 
18769 		if (phyi->phyint_groupname_len != 0) {
18770 			if (ill->ill_ipif_up_count == 0)
18771 				illgrp_delete(ill);
18772 		}
18773 
18774 		/*
18775 		 * If we have deleted some of the broadcast ires associated
18776 		 * with this ipif, we need to re-nominate somebody else if
18777 		 * the ires that we deleted were the nominated ones.
18778 		 */
18779 		if (ill->ill_group != NULL && !ill->ill_isv6)
18780 			ipif_renominate_bcast(ipif);
18781 	}
18782 
18783 	/*
18784 	 * neighbor-discovery or arp entries for this interface.
18785 	 */
18786 	ipif_ndp_down(ipif);
18787 
18788 	/*
18789 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18790 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18791 	 * and ill_delete -> ipif_free -> ipif_down
18792 	 */
18793 	if (mp == NULL) {
18794 		ASSERT(q == NULL);
18795 		return (0);
18796 	}
18797 
18798 	if (CONN_Q(q)) {
18799 		connp = Q_TO_CONN(q);
18800 		mutex_enter(&connp->conn_lock);
18801 	} else {
18802 		connp = NULL;
18803 	}
18804 	mutex_enter(&ill->ill_lock);
18805 	/*
18806 	 * Are there any ire's pointing to this ipif that are still active ?
18807 	 * If this is the last ipif going down, are there any ire's pointing
18808 	 * to this ill that are still active ?
18809 	 */
18810 	if (ipif_is_quiescent(ipif)) {
18811 		mutex_exit(&ill->ill_lock);
18812 		if (connp != NULL)
18813 			mutex_exit(&connp->conn_lock);
18814 		return (0);
18815 	}
18816 
18817 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18818 	    ill->ill_name, (void *)ill));
18819 	/*
18820 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18821 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18822 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18823 	 */
18824 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18825 	if (!success) {
18826 		/* The conn is closing. So just return */
18827 		ASSERT(connp != NULL);
18828 		mutex_exit(&ill->ill_lock);
18829 		mutex_exit(&connp->conn_lock);
18830 		return (EINTR);
18831 	}
18832 
18833 	mutex_exit(&ill->ill_lock);
18834 	if (connp != NULL)
18835 		mutex_exit(&connp->conn_lock);
18836 	return (EINPROGRESS);
18837 }
18838 
18839 void
18840 ipif_down_tail(ipif_t *ipif)
18841 {
18842 	ill_t	*ill = ipif->ipif_ill;
18843 
18844 	/*
18845 	 * Skip any loopback interface (null wq).
18846 	 * If this is the last logical interface on the ill
18847 	 * have ill_dl_down tell the driver we are gone (unbind)
18848 	 * Note that lun 0 can ipif_down even though
18849 	 * there are other logical units that are up.
18850 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18851 	 */
18852 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18853 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18854 	    ill->ill_dl_up) {
18855 		ill_dl_down(ill);
18856 	}
18857 	ill->ill_logical_down = 0;
18858 
18859 	/*
18860 	 * Have to be after removing the routes in ipif_down_delete_ire.
18861 	 */
18862 	if (ipif->ipif_isv6) {
18863 		if (ill->ill_flags & ILLF_XRESOLV)
18864 			ipif_arp_down(ipif);
18865 	} else {
18866 		ipif_arp_down(ipif);
18867 	}
18868 
18869 	ip_rts_ifmsg(ipif);
18870 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18871 }
18872 
18873 /*
18874  * Bring interface logically down without bringing the physical interface
18875  * down e.g. when the netmask is changed. This avoids long lasting link
18876  * negotiations between an ethernet interface and a certain switches.
18877  */
18878 static int
18879 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18880 {
18881 	/*
18882 	 * The ill_logical_down flag is a transient flag. It is set here
18883 	 * and is cleared once the down has completed in ipif_down_tail.
18884 	 * This flag does not indicate whether the ill stream is in the
18885 	 * DL_BOUND state with the driver. Instead this flag is used by
18886 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18887 	 * the driver. The state of the ill stream i.e. whether it is
18888 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18889 	 */
18890 	ipif->ipif_ill->ill_logical_down = 1;
18891 	return (ipif_down(ipif, q, mp));
18892 }
18893 
18894 /*
18895  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18896  * If the usesrc client ILL is already part of a usesrc group or not,
18897  * in either case a ire_stq with the matching usesrc client ILL will
18898  * locate the IRE's that need to be deleted. We want IREs to be created
18899  * with the new source address.
18900  */
18901 static void
18902 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18903 {
18904 	ill_t	*ucill = (ill_t *)ill_arg;
18905 
18906 	ASSERT(IAM_WRITER_ILL(ucill));
18907 
18908 	if (ire->ire_stq == NULL)
18909 		return;
18910 
18911 	if ((ire->ire_type == IRE_CACHE) &&
18912 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18913 		ire_delete(ire);
18914 }
18915 
18916 /*
18917  * ire_walk routine to delete every IRE dependent on the interface
18918  * address that is going down.	(Always called as writer.)
18919  * Works for both v4 and v6.
18920  * In addition for checking for ire_ipif matches it also checks for
18921  * IRE_CACHE entries which have the same source address as the
18922  * disappearing ipif since ipif_select_source might have picked
18923  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18924  * care of any IRE_INTERFACE with the disappearing source address.
18925  */
18926 static void
18927 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18928 {
18929 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18930 	ill_t *ire_ill;
18931 	ill_t *ipif_ill;
18932 
18933 	ASSERT(IAM_WRITER_IPIF(ipif));
18934 	if (ire->ire_ipif == NULL)
18935 		return;
18936 
18937 	/*
18938 	 * For IPv4, we derive source addresses for an IRE from ipif's
18939 	 * belonging to the same IPMP group as the IRE's outgoing
18940 	 * interface.  If an IRE's outgoing interface isn't in the
18941 	 * same IPMP group as a particular ipif, then that ipif
18942 	 * couldn't have been used as a source address for this IRE.
18943 	 *
18944 	 * For IPv6, source addresses are only restricted to the IPMP group
18945 	 * if the IRE is for a link-local address or a multicast address.
18946 	 * Otherwise, source addresses for an IRE can be chosen from
18947 	 * interfaces other than the the outgoing interface for that IRE.
18948 	 *
18949 	 * For source address selection details, see ipif_select_source()
18950 	 * and ipif_select_source_v6().
18951 	 */
18952 	if (ire->ire_ipversion == IPV4_VERSION ||
18953 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18954 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18955 		ire_ill = ire->ire_ipif->ipif_ill;
18956 		ipif_ill = ipif->ipif_ill;
18957 
18958 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18959 			return;
18960 		}
18961 	}
18962 
18963 	if (ire->ire_ipif != ipif) {
18964 		/*
18965 		 * Look for a matching source address.
18966 		 */
18967 		if (ire->ire_type != IRE_CACHE)
18968 			return;
18969 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18970 			return;
18971 
18972 		if (ire->ire_ipversion == IPV4_VERSION) {
18973 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18974 				return;
18975 		} else {
18976 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18977 			    &ipif->ipif_v6lcl_addr))
18978 				return;
18979 		}
18980 		ire_delete(ire);
18981 		return;
18982 	}
18983 	/*
18984 	 * ire_delete() will do an ire_flush_cache which will delete
18985 	 * all ire_ipif matches
18986 	 */
18987 	ire_delete(ire);
18988 }
18989 
18990 /*
18991  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18992  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18993  * 2) when an interface is brought up or down (on that ill).
18994  * This ensures that the IRE_CACHE entries don't retain stale source
18995  * address selection results.
18996  */
18997 void
18998 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18999 {
19000 	ill_t	*ill = (ill_t *)ill_arg;
19001 	ill_t	*ipif_ill;
19002 
19003 	ASSERT(IAM_WRITER_ILL(ill));
19004 	/*
19005 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19006 	 * Hence this should be IRE_CACHE.
19007 	 */
19008 	ASSERT(ire->ire_type == IRE_CACHE);
19009 
19010 	/*
19011 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19012 	 * We are only interested in IRE_CACHES that has borrowed
19013 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19014 	 * for which we need to look at ire_ipif->ipif_ill match
19015 	 * with ill.
19016 	 */
19017 	ASSERT(ire->ire_ipif != NULL);
19018 	ipif_ill = ire->ire_ipif->ipif_ill;
19019 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19020 	    ipif_ill->ill_group == ill->ill_group)) {
19021 		ire_delete(ire);
19022 	}
19023 }
19024 
19025 /*
19026  * Delete all the ire whose stq references ill_arg.
19027  */
19028 static void
19029 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19030 {
19031 	ill_t	*ill = (ill_t *)ill_arg;
19032 	ill_t	*ire_ill;
19033 
19034 	ASSERT(IAM_WRITER_ILL(ill));
19035 	/*
19036 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19037 	 * Hence this should be IRE_CACHE.
19038 	 */
19039 	ASSERT(ire->ire_type == IRE_CACHE);
19040 
19041 	/*
19042 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19043 	 * matches ill. We are only interested in IRE_CACHES that
19044 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19045 	 * filtering here.
19046 	 */
19047 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19048 
19049 	if (ire_ill == ill)
19050 		ire_delete(ire);
19051 }
19052 
19053 /*
19054  * This is called when an ill leaves the group. We want to delete
19055  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19056  * pointing at ill.
19057  */
19058 static void
19059 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19060 {
19061 	ill_t	*ill = (ill_t *)ill_arg;
19062 
19063 	ASSERT(IAM_WRITER_ILL(ill));
19064 	ASSERT(ill->ill_group == NULL);
19065 	/*
19066 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19067 	 * Hence this should be IRE_CACHE.
19068 	 */
19069 	ASSERT(ire->ire_type == IRE_CACHE);
19070 	/*
19071 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19072 	 * matches ill. We are interested in both.
19073 	 */
19074 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19075 	    (ire->ire_ipif->ipif_ill == ill));
19076 
19077 	ire_delete(ire);
19078 }
19079 
19080 /*
19081  * Initiate deallocate of an IPIF. Always called as writer. Called by
19082  * ill_delete or ip_sioctl_removeif.
19083  */
19084 static void
19085 ipif_free(ipif_t *ipif)
19086 {
19087 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19088 
19089 	ASSERT(IAM_WRITER_IPIF(ipif));
19090 
19091 	if (ipif->ipif_recovery_id != 0)
19092 		(void) untimeout(ipif->ipif_recovery_id);
19093 	ipif->ipif_recovery_id = 0;
19094 
19095 	/* Remove conn references */
19096 	reset_conn_ipif(ipif);
19097 
19098 	/*
19099 	 * Make sure we have valid net and subnet broadcast ire's for the
19100 	 * other ipif's which share them with this ipif.
19101 	 */
19102 	if (!ipif->ipif_isv6)
19103 		ipif_check_bcast_ires(ipif);
19104 
19105 	/*
19106 	 * Take down the interface. We can be called either from ill_delete
19107 	 * or from ip_sioctl_removeif.
19108 	 */
19109 	(void) ipif_down(ipif, NULL, NULL);
19110 
19111 	/*
19112 	 * Now that the interface is down, there's no chance it can still
19113 	 * become a duplicate.  Cancel any timer that may have been set while
19114 	 * tearing down.
19115 	 */
19116 	if (ipif->ipif_recovery_id != 0)
19117 		(void) untimeout(ipif->ipif_recovery_id);
19118 	ipif->ipif_recovery_id = 0;
19119 
19120 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19121 	/* Remove pointers to this ill in the multicast routing tables */
19122 	reset_mrt_vif_ipif(ipif);
19123 	rw_exit(&ipst->ips_ill_g_lock);
19124 }
19125 
19126 /*
19127  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19128  * also ill_move().
19129  */
19130 static void
19131 ipif_free_tail(ipif_t *ipif)
19132 {
19133 	mblk_t	*mp;
19134 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19135 
19136 	/*
19137 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19138 	 */
19139 	mutex_enter(&ipif->ipif_saved_ire_lock);
19140 	mp = ipif->ipif_saved_ire_mp;
19141 	ipif->ipif_saved_ire_mp = NULL;
19142 	mutex_exit(&ipif->ipif_saved_ire_lock);
19143 	freemsg(mp);
19144 
19145 	/*
19146 	 * Need to hold both ill_g_lock and ill_lock while
19147 	 * inserting or removing an ipif from the linked list
19148 	 * of ipifs hanging off the ill.
19149 	 */
19150 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19151 
19152 	ASSERT(ilm_walk_ipif(ipif) == 0);
19153 
19154 #ifdef DEBUG
19155 	ipif_trace_cleanup(ipif);
19156 #endif
19157 
19158 	/* Ask SCTP to take it out of it list */
19159 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19160 
19161 	/* Get it out of the ILL interface list. */
19162 	ipif_remove(ipif, B_TRUE);
19163 	rw_exit(&ipst->ips_ill_g_lock);
19164 
19165 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19166 
19167 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19168 	ASSERT(ipif->ipif_recovery_id == 0);
19169 
19170 	/* Free the memory. */
19171 	mi_free(ipif);
19172 }
19173 
19174 /*
19175  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19176  * is zero.
19177  */
19178 void
19179 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19180 {
19181 	char	lbuf[LIFNAMSIZ];
19182 	char	*name;
19183 	size_t	name_len;
19184 
19185 	buf[0] = '\0';
19186 	name = ipif->ipif_ill->ill_name;
19187 	name_len = ipif->ipif_ill->ill_name_length;
19188 	if (ipif->ipif_id != 0) {
19189 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19190 		    ipif->ipif_id);
19191 		name = lbuf;
19192 		name_len = mi_strlen(name) + 1;
19193 	}
19194 	len -= 1;
19195 	buf[len] = '\0';
19196 	len = MIN(len, name_len);
19197 	bcopy(name, buf, len);
19198 }
19199 
19200 /*
19201  * Find an IPIF based on the name passed in.  Names can be of the
19202  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19203  * The <phys> string can have forms like <dev><#> (e.g., le0),
19204  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19205  * When there is no colon, the implied unit id is zero. <phys> must
19206  * correspond to the name of an ILL.  (May be called as writer.)
19207  */
19208 static ipif_t *
19209 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19210     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19211     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19212 {
19213 	char	*cp;
19214 	char	*endp;
19215 	long	id;
19216 	ill_t	*ill;
19217 	ipif_t	*ipif;
19218 	uint_t	ire_type;
19219 	boolean_t did_alloc = B_FALSE;
19220 	ipsq_t	*ipsq;
19221 
19222 	if (error != NULL)
19223 		*error = 0;
19224 
19225 	/*
19226 	 * If the caller wants to us to create the ipif, make sure we have a
19227 	 * valid zoneid
19228 	 */
19229 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19230 
19231 	if (namelen == 0) {
19232 		if (error != NULL)
19233 			*error = ENXIO;
19234 		return (NULL);
19235 	}
19236 
19237 	*exists = B_FALSE;
19238 	/* Look for a colon in the name. */
19239 	endp = &name[namelen];
19240 	for (cp = endp; --cp > name; ) {
19241 		if (*cp == IPIF_SEPARATOR_CHAR)
19242 			break;
19243 	}
19244 
19245 	if (*cp == IPIF_SEPARATOR_CHAR) {
19246 		/*
19247 		 * Reject any non-decimal aliases for logical
19248 		 * interfaces. Aliases with leading zeroes
19249 		 * are also rejected as they introduce ambiguity
19250 		 * in the naming of the interfaces.
19251 		 * In order to confirm with existing semantics,
19252 		 * and to not break any programs/script relying
19253 		 * on that behaviour, if<0>:0 is considered to be
19254 		 * a valid interface.
19255 		 *
19256 		 * If alias has two or more digits and the first
19257 		 * is zero, fail.
19258 		 */
19259 		if (&cp[2] < endp && cp[1] == '0') {
19260 			if (error != NULL)
19261 				*error = EINVAL;
19262 			return (NULL);
19263 		}
19264 	}
19265 
19266 	if (cp <= name) {
19267 		cp = endp;
19268 	} else {
19269 		*cp = '\0';
19270 	}
19271 
19272 	/*
19273 	 * Look up the ILL, based on the portion of the name
19274 	 * before the slash. ill_lookup_on_name returns a held ill.
19275 	 * Temporary to check whether ill exists already. If so
19276 	 * ill_lookup_on_name will clear it.
19277 	 */
19278 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19279 	    q, mp, func, error, &did_alloc, ipst);
19280 	if (cp != endp)
19281 		*cp = IPIF_SEPARATOR_CHAR;
19282 	if (ill == NULL)
19283 		return (NULL);
19284 
19285 	/* Establish the unit number in the name. */
19286 	id = 0;
19287 	if (cp < endp && *endp == '\0') {
19288 		/* If there was a colon, the unit number follows. */
19289 		cp++;
19290 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19291 			ill_refrele(ill);
19292 			if (error != NULL)
19293 				*error = ENXIO;
19294 			return (NULL);
19295 		}
19296 	}
19297 
19298 	GRAB_CONN_LOCK(q);
19299 	mutex_enter(&ill->ill_lock);
19300 	/* Now see if there is an IPIF with this unit number. */
19301 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19302 		if (ipif->ipif_id == id) {
19303 			if (zoneid != ALL_ZONES &&
19304 			    zoneid != ipif->ipif_zoneid &&
19305 			    ipif->ipif_zoneid != ALL_ZONES) {
19306 				mutex_exit(&ill->ill_lock);
19307 				RELEASE_CONN_LOCK(q);
19308 				ill_refrele(ill);
19309 				if (error != NULL)
19310 					*error = ENXIO;
19311 				return (NULL);
19312 			}
19313 			/*
19314 			 * The block comment at the start of ipif_down
19315 			 * explains the use of the macros used below
19316 			 */
19317 			if (IPIF_CAN_LOOKUP(ipif)) {
19318 				ipif_refhold_locked(ipif);
19319 				mutex_exit(&ill->ill_lock);
19320 				if (!did_alloc)
19321 					*exists = B_TRUE;
19322 				/*
19323 				 * Drop locks before calling ill_refrele
19324 				 * since it can potentially call into
19325 				 * ipif_ill_refrele_tail which can end up
19326 				 * in trying to acquire any lock.
19327 				 */
19328 				RELEASE_CONN_LOCK(q);
19329 				ill_refrele(ill);
19330 				return (ipif);
19331 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19332 				ipsq = ill->ill_phyint->phyint_ipsq;
19333 				mutex_enter(&ipsq->ipsq_lock);
19334 				mutex_exit(&ill->ill_lock);
19335 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19336 				mutex_exit(&ipsq->ipsq_lock);
19337 				RELEASE_CONN_LOCK(q);
19338 				ill_refrele(ill);
19339 				if (error != NULL)
19340 					*error = EINPROGRESS;
19341 				return (NULL);
19342 			}
19343 		}
19344 	}
19345 	RELEASE_CONN_LOCK(q);
19346 
19347 	if (!do_alloc) {
19348 		mutex_exit(&ill->ill_lock);
19349 		ill_refrele(ill);
19350 		if (error != NULL)
19351 			*error = ENXIO;
19352 		return (NULL);
19353 	}
19354 
19355 	/*
19356 	 * If none found, atomically allocate and return a new one.
19357 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19358 	 * to support "receive only" use of lo0:1 etc. as is still done
19359 	 * below as an initial guess.
19360 	 * However, this is now likely to be overriden later in ipif_up_done()
19361 	 * when we know for sure what address has been configured on the
19362 	 * interface, since we might have more than one loopback interface
19363 	 * with a loopback address, e.g. in the case of zones, and all the
19364 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19365 	 */
19366 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19367 		ire_type = IRE_LOOPBACK;
19368 	else
19369 		ire_type = IRE_LOCAL;
19370 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19371 	if (ipif != NULL)
19372 		ipif_refhold_locked(ipif);
19373 	else if (error != NULL)
19374 		*error = ENOMEM;
19375 	mutex_exit(&ill->ill_lock);
19376 	ill_refrele(ill);
19377 	return (ipif);
19378 }
19379 
19380 /*
19381  * This routine is called whenever a new address comes up on an ipif.  If
19382  * we are configured to respond to address mask requests, then we are supposed
19383  * to broadcast an address mask reply at this time.  This routine is also
19384  * called if we are already up, but a netmask change is made.  This is legal
19385  * but might not make the system manager very popular.	(May be called
19386  * as writer.)
19387  */
19388 void
19389 ipif_mask_reply(ipif_t *ipif)
19390 {
19391 	icmph_t	*icmph;
19392 	ipha_t	*ipha;
19393 	mblk_t	*mp;
19394 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19395 
19396 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19397 
19398 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19399 		return;
19400 
19401 	/* ICMP mask reply is IPv4 only */
19402 	ASSERT(!ipif->ipif_isv6);
19403 	/* ICMP mask reply is not for a loopback interface */
19404 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19405 
19406 	mp = allocb(REPLY_LEN, BPRI_HI);
19407 	if (mp == NULL)
19408 		return;
19409 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19410 
19411 	ipha = (ipha_t *)mp->b_rptr;
19412 	bzero(ipha, REPLY_LEN);
19413 	*ipha = icmp_ipha;
19414 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19415 	ipha->ipha_src = ipif->ipif_src_addr;
19416 	ipha->ipha_dst = ipif->ipif_brd_addr;
19417 	ipha->ipha_length = htons(REPLY_LEN);
19418 	ipha->ipha_ident = 0;
19419 
19420 	icmph = (icmph_t *)&ipha[1];
19421 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19422 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19423 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19424 
19425 	put(ipif->ipif_wq, mp);
19426 
19427 #undef	REPLY_LEN
19428 }
19429 
19430 /*
19431  * When the mtu in the ipif changes, we call this routine through ire_walk
19432  * to update all the relevant IREs.
19433  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19434  */
19435 static void
19436 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19437 {
19438 	ipif_t *ipif = (ipif_t *)ipif_arg;
19439 
19440 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19441 		return;
19442 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19443 }
19444 
19445 /*
19446  * When the mtu in the ill changes, we call this routine through ire_walk
19447  * to update all the relevant IREs.
19448  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19449  */
19450 void
19451 ill_mtu_change(ire_t *ire, char *ill_arg)
19452 {
19453 	ill_t	*ill = (ill_t *)ill_arg;
19454 
19455 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19456 		return;
19457 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19458 }
19459 
19460 /*
19461  * Join the ipif specific multicast groups.
19462  * Must be called after a mapping has been set up in the resolver.  (Always
19463  * called as writer.)
19464  */
19465 void
19466 ipif_multicast_up(ipif_t *ipif)
19467 {
19468 	int err, index;
19469 	ill_t *ill;
19470 
19471 	ASSERT(IAM_WRITER_IPIF(ipif));
19472 
19473 	ill = ipif->ipif_ill;
19474 	index = ill->ill_phyint->phyint_ifindex;
19475 
19476 	ip1dbg(("ipif_multicast_up\n"));
19477 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19478 		return;
19479 
19480 	if (ipif->ipif_isv6) {
19481 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19482 			return;
19483 
19484 		/* Join the all hosts multicast address */
19485 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19486 		/*
19487 		 * Passing B_TRUE means we have to join the multicast
19488 		 * membership on this interface even though this is
19489 		 * FAILED. If we join on a different one in the group,
19490 		 * we will not be able to delete the membership later
19491 		 * as we currently don't track where we join when we
19492 		 * join within the kernel unlike applications where
19493 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19494 		 * for more on this.
19495 		 */
19496 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19497 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19498 		if (err != 0) {
19499 			ip0dbg(("ipif_multicast_up: "
19500 			    "all_hosts_mcast failed %d\n",
19501 			    err));
19502 			return;
19503 		}
19504 		/*
19505 		 * Enable multicast for the solicited node multicast address
19506 		 */
19507 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19508 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19509 
19510 			ipv6_multi.s6_addr32[3] |=
19511 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19512 
19513 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19514 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19515 			    NULL);
19516 			if (err != 0) {
19517 				ip0dbg(("ipif_multicast_up: solicited MC"
19518 				    " failed %d\n", err));
19519 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19520 				    ill, ill->ill_phyint->phyint_ifindex,
19521 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19522 				return;
19523 			}
19524 		}
19525 	} else {
19526 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19527 			return;
19528 
19529 		/* Join the all hosts multicast address */
19530 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19531 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19532 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19533 		if (err) {
19534 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19535 			return;
19536 		}
19537 	}
19538 	ipif->ipif_multicast_up = 1;
19539 }
19540 
19541 /*
19542  * Blow away any multicast groups that we joined in ipif_multicast_up().
19543  * (Explicit memberships are blown away in ill_leave_multicast() when the
19544  * ill is brought down.)
19545  */
19546 static void
19547 ipif_multicast_down(ipif_t *ipif)
19548 {
19549 	int err;
19550 
19551 	ASSERT(IAM_WRITER_IPIF(ipif));
19552 
19553 	ip1dbg(("ipif_multicast_down\n"));
19554 	if (!ipif->ipif_multicast_up)
19555 		return;
19556 
19557 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19558 
19559 	if (!ipif->ipif_isv6) {
19560 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19561 		    B_TRUE);
19562 		if (err != 0)
19563 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19564 
19565 		ipif->ipif_multicast_up = 0;
19566 		return;
19567 	}
19568 
19569 	/*
19570 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19571 	 * we should look for ilms on this ill rather than the ones that have
19572 	 * been failed over here.  They are here temporarily. As
19573 	 * ipif_multicast_up has joined on this ill, we should delete only
19574 	 * from this ill.
19575 	 */
19576 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19577 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19578 	    B_TRUE, B_TRUE);
19579 	if (err != 0) {
19580 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19581 		    err));
19582 	}
19583 	/*
19584 	 * Disable multicast for the solicited node multicast address
19585 	 */
19586 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19587 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19588 
19589 		ipv6_multi.s6_addr32[3] |=
19590 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19591 
19592 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19593 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19594 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19595 
19596 		if (err != 0) {
19597 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19598 			    err));
19599 		}
19600 	}
19601 
19602 	ipif->ipif_multicast_up = 0;
19603 }
19604 
19605 /*
19606  * Used when an interface comes up to recreate any extra routes on this
19607  * interface.
19608  */
19609 static ire_t **
19610 ipif_recover_ire(ipif_t *ipif)
19611 {
19612 	mblk_t	*mp;
19613 	ire_t	**ipif_saved_irep;
19614 	ire_t	**irep;
19615 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19616 
19617 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19618 	    ipif->ipif_id));
19619 
19620 	mutex_enter(&ipif->ipif_saved_ire_lock);
19621 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19622 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19623 	if (ipif_saved_irep == NULL) {
19624 		mutex_exit(&ipif->ipif_saved_ire_lock);
19625 		return (NULL);
19626 	}
19627 
19628 	irep = ipif_saved_irep;
19629 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19630 		ire_t		*ire;
19631 		queue_t		*rfq;
19632 		queue_t		*stq;
19633 		ifrt_t		*ifrt;
19634 		uchar_t		*src_addr;
19635 		uchar_t		*gateway_addr;
19636 		ushort_t	type;
19637 
19638 		/*
19639 		 * When the ire was initially created and then added in
19640 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19641 		 * in the case of a traditional interface route, or as one of
19642 		 * the IRE_OFFSUBNET types (with the exception of
19643 		 * IRE_HOST types ire which is created by icmp_redirect() and
19644 		 * which we don't need to save or recover).  In the case where
19645 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19646 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19647 		 * to satisfy software like GateD and Sun Cluster which creates
19648 		 * routes using the the loopback interface's address as a
19649 		 * gateway.
19650 		 *
19651 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19652 		 * ire_create() will be called in the same way here as
19653 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19654 		 * the route looks like a traditional interface route (where
19655 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19656 		 * the saved ifrt->ifrt_type.  This means that in the case where
19657 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19658 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19659 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19660 		 */
19661 		ifrt = (ifrt_t *)mp->b_rptr;
19662 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19663 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19664 			rfq = NULL;
19665 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19666 			    ? ipif->ipif_rq : ipif->ipif_wq;
19667 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19668 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19669 			    : (uint8_t *)&ipif->ipif_src_addr;
19670 			gateway_addr = NULL;
19671 			type = ipif->ipif_net_type;
19672 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19673 			/* Recover multiroute broadcast IRE. */
19674 			rfq = ipif->ipif_rq;
19675 			stq = ipif->ipif_wq;
19676 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19677 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19678 			    : (uint8_t *)&ipif->ipif_src_addr;
19679 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19680 			type = ifrt->ifrt_type;
19681 		} else {
19682 			rfq = NULL;
19683 			stq = NULL;
19684 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19685 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19686 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19687 			type = ifrt->ifrt_type;
19688 		}
19689 
19690 		/*
19691 		 * Create a copy of the IRE with the saved address and netmask.
19692 		 */
19693 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19694 		    "0x%x/0x%x\n",
19695 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19696 		    ntohl(ifrt->ifrt_addr),
19697 		    ntohl(ifrt->ifrt_mask)));
19698 		ire = ire_create(
19699 		    (uint8_t *)&ifrt->ifrt_addr,
19700 		    (uint8_t *)&ifrt->ifrt_mask,
19701 		    src_addr,
19702 		    gateway_addr,
19703 		    &ifrt->ifrt_max_frag,
19704 		    NULL,
19705 		    rfq,
19706 		    stq,
19707 		    type,
19708 		    ipif,
19709 		    0,
19710 		    0,
19711 		    0,
19712 		    ifrt->ifrt_flags,
19713 		    &ifrt->ifrt_iulp_info,
19714 		    NULL,
19715 		    NULL,
19716 		    ipst);
19717 
19718 		if (ire == NULL) {
19719 			mutex_exit(&ipif->ipif_saved_ire_lock);
19720 			kmem_free(ipif_saved_irep,
19721 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19722 			return (NULL);
19723 		}
19724 
19725 		/*
19726 		 * Some software (for example, GateD and Sun Cluster) attempts
19727 		 * to create (what amount to) IRE_PREFIX routes with the
19728 		 * loopback address as the gateway.  This is primarily done to
19729 		 * set up prefixes with the RTF_REJECT flag set (for example,
19730 		 * when generating aggregate routes.)
19731 		 *
19732 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19733 		 * IRE_LOOPBACK, then we map the request into a
19734 		 * IRE_IF_NORESOLVER.
19735 		 */
19736 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19737 			ire->ire_type = IRE_IF_NORESOLVER;
19738 		/*
19739 		 * ire held by ire_add, will be refreled' towards the
19740 		 * the end of ipif_up_done
19741 		 */
19742 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19743 		*irep = ire;
19744 		irep++;
19745 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19746 	}
19747 	mutex_exit(&ipif->ipif_saved_ire_lock);
19748 	return (ipif_saved_irep);
19749 }
19750 
19751 /*
19752  * Used to set the netmask and broadcast address to default values when the
19753  * interface is brought up.  (Always called as writer.)
19754  */
19755 static void
19756 ipif_set_default(ipif_t *ipif)
19757 {
19758 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19759 
19760 	if (!ipif->ipif_isv6) {
19761 		/*
19762 		 * Interface holds an IPv4 address. Default
19763 		 * mask is the natural netmask.
19764 		 */
19765 		if (!ipif->ipif_net_mask) {
19766 			ipaddr_t	v4mask;
19767 
19768 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19769 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19770 		}
19771 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19772 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19773 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19774 		} else {
19775 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19776 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19777 		}
19778 		/*
19779 		 * NOTE: SunOS 4.X does this even if the broadcast address
19780 		 * has been already set thus we do the same here.
19781 		 */
19782 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19783 			ipaddr_t	v4addr;
19784 
19785 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19786 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19787 		}
19788 	} else {
19789 		/*
19790 		 * Interface holds an IPv6-only address.  Default
19791 		 * mask is all-ones.
19792 		 */
19793 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19794 			ipif->ipif_v6net_mask = ipv6_all_ones;
19795 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19796 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19797 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19798 		} else {
19799 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19800 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19801 		}
19802 	}
19803 }
19804 
19805 /*
19806  * Return 0 if this address can be used as local address without causing
19807  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19808  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19809  * Special checks are needed to allow the same IPv6 link-local address
19810  * on different ills.
19811  * TODO: allowing the same site-local address on different ill's.
19812  */
19813 int
19814 ip_addr_availability_check(ipif_t *new_ipif)
19815 {
19816 	in6_addr_t our_v6addr;
19817 	ill_t *ill;
19818 	ipif_t *ipif;
19819 	ill_walk_context_t ctx;
19820 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19821 
19822 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19823 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19824 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19825 
19826 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19827 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19828 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19829 		return (0);
19830 
19831 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19832 
19833 	if (new_ipif->ipif_isv6)
19834 		ill = ILL_START_WALK_V6(&ctx, ipst);
19835 	else
19836 		ill = ILL_START_WALK_V4(&ctx, ipst);
19837 
19838 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19839 		for (ipif = ill->ill_ipif; ipif != NULL;
19840 		    ipif = ipif->ipif_next) {
19841 			if ((ipif == new_ipif) ||
19842 			    !(ipif->ipif_flags & IPIF_UP) ||
19843 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19844 				continue;
19845 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19846 			    &our_v6addr)) {
19847 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19848 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19849 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19850 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19851 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19852 				    new_ipif->ipif_ill != ill)
19853 					continue;
19854 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19855 				    new_ipif->ipif_ill != ill)
19856 					continue;
19857 				else if (new_ipif->ipif_zoneid !=
19858 				    ipif->ipif_zoneid &&
19859 				    ipif->ipif_zoneid != ALL_ZONES &&
19860 				    IS_LOOPBACK(ill))
19861 					continue;
19862 				else if (new_ipif->ipif_ill == ill)
19863 					return (EADDRINUSE);
19864 				else
19865 					return (EADDRNOTAVAIL);
19866 			}
19867 		}
19868 	}
19869 
19870 	return (0);
19871 }
19872 
19873 /*
19874  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19875  * IREs for the ipif.
19876  * When the routine returns EINPROGRESS then mp has been consumed and
19877  * the ioctl will be acked from ip_rput_dlpi.
19878  */
19879 static int
19880 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19881 {
19882 	ill_t	*ill = ipif->ipif_ill;
19883 	boolean_t isv6 = ipif->ipif_isv6;
19884 	int	err = 0;
19885 	boolean_t success;
19886 
19887 	ASSERT(IAM_WRITER_IPIF(ipif));
19888 
19889 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19890 
19891 	/* Shouldn't get here if it is already up. */
19892 	if (ipif->ipif_flags & IPIF_UP)
19893 		return (EALREADY);
19894 
19895 	/* Skip arp/ndp for any loopback interface. */
19896 	if (ill->ill_wq != NULL) {
19897 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19898 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19899 
19900 		if (!ill->ill_dl_up) {
19901 			/*
19902 			 * ill_dl_up is not yet set. i.e. we are yet to
19903 			 * DL_BIND with the driver and this is the first
19904 			 * logical interface on the ill to become "up".
19905 			 * Tell the driver to get going (via DL_BIND_REQ).
19906 			 * Note that changing "significant" IFF_ flags
19907 			 * address/netmask etc cause a down/up dance, but
19908 			 * does not cause an unbind (DL_UNBIND) with the driver
19909 			 */
19910 			return (ill_dl_up(ill, ipif, mp, q));
19911 		}
19912 
19913 		/*
19914 		 * ipif_resolver_up may end up sending an
19915 		 * AR_INTERFACE_UP message to ARP, which would, in
19916 		 * turn send a DLPI message to the driver. ioctls are
19917 		 * serialized and so we cannot send more than one
19918 		 * interface up message at a time. If ipif_resolver_up
19919 		 * does send an interface up message to ARP, we get
19920 		 * EINPROGRESS and we will complete in ip_arp_done.
19921 		 */
19922 
19923 		ASSERT(connp != NULL || !CONN_Q(q));
19924 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19925 		if (connp != NULL)
19926 			mutex_enter(&connp->conn_lock);
19927 		mutex_enter(&ill->ill_lock);
19928 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19929 		mutex_exit(&ill->ill_lock);
19930 		if (connp != NULL)
19931 			mutex_exit(&connp->conn_lock);
19932 		if (!success)
19933 			return (EINTR);
19934 
19935 		/*
19936 		 * Crank up IPv6 neighbor discovery
19937 		 * Unlike ARP, this should complete when
19938 		 * ipif_ndp_up returns. However, for
19939 		 * ILLF_XRESOLV interfaces we also send a
19940 		 * AR_INTERFACE_UP to the external resolver.
19941 		 * That ioctl will complete in ip_rput.
19942 		 */
19943 		if (isv6) {
19944 			err = ipif_ndp_up(ipif);
19945 			if (err != 0) {
19946 				if (err != EINPROGRESS)
19947 					mp = ipsq_pending_mp_get(ipsq, &connp);
19948 				return (err);
19949 			}
19950 		}
19951 		/* Now, ARP */
19952 		err = ipif_resolver_up(ipif, Res_act_initial);
19953 		if (err == EINPROGRESS) {
19954 			/* We will complete it in ip_arp_done */
19955 			return (err);
19956 		}
19957 		mp = ipsq_pending_mp_get(ipsq, &connp);
19958 		ASSERT(mp != NULL);
19959 		if (err != 0)
19960 			return (err);
19961 	} else {
19962 		/*
19963 		 * Interfaces without underlying hardware don't do duplicate
19964 		 * address detection.
19965 		 */
19966 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19967 		ipif->ipif_addr_ready = 1;
19968 	}
19969 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19970 }
19971 
19972 /*
19973  * Perform a bind for the physical device.
19974  * When the routine returns EINPROGRESS then mp has been consumed and
19975  * the ioctl will be acked from ip_rput_dlpi.
19976  * Allocate an unbind message and save it until ipif_down.
19977  */
19978 static int
19979 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19980 {
19981 	areq_t	*areq;
19982 	mblk_t	*areq_mp = NULL;
19983 	mblk_t	*bind_mp = NULL;
19984 	mblk_t	*unbind_mp = NULL;
19985 	conn_t	*connp;
19986 	boolean_t success;
19987 	uint16_t sap_addr;
19988 
19989 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19990 	ASSERT(IAM_WRITER_ILL(ill));
19991 	ASSERT(mp != NULL);
19992 
19993 	/* Create a resolver cookie for ARP */
19994 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19995 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19996 		if (areq_mp == NULL)
19997 			return (ENOMEM);
19998 
19999 		freemsg(ill->ill_resolver_mp);
20000 		ill->ill_resolver_mp = areq_mp;
20001 		areq = (areq_t *)areq_mp->b_rptr;
20002 		sap_addr = ill->ill_sap;
20003 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20004 	}
20005 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20006 	    DL_BIND_REQ);
20007 	if (bind_mp == NULL)
20008 		goto bad;
20009 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20010 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20011 
20012 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20013 	if (unbind_mp == NULL)
20014 		goto bad;
20015 
20016 	/*
20017 	 * Record state needed to complete this operation when the
20018 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20019 	 */
20020 	ASSERT(WR(q)->q_next == NULL);
20021 	connp = Q_TO_CONN(q);
20022 
20023 	mutex_enter(&connp->conn_lock);
20024 	mutex_enter(&ipif->ipif_ill->ill_lock);
20025 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20026 	mutex_exit(&ipif->ipif_ill->ill_lock);
20027 	mutex_exit(&connp->conn_lock);
20028 	if (!success)
20029 		goto bad;
20030 
20031 	/*
20032 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20033 	 * the interface goes down.
20034 	 */
20035 	ASSERT(ill->ill_unbind_mp == NULL);
20036 	ill->ill_unbind_mp = unbind_mp;
20037 
20038 	ill_dlpi_send(ill, bind_mp);
20039 	/* Send down link-layer capabilities probe if not already done. */
20040 	ill_capability_probe(ill);
20041 
20042 	/*
20043 	 * Sysid used to rely on the fact that netboots set domainname
20044 	 * and the like. Now that miniroot boots aren't strictly netboots
20045 	 * and miniroot network configuration is driven from userland
20046 	 * these things still need to be set. This situation can be detected
20047 	 * by comparing the interface being configured here to the one
20048 	 * dhcifname was set to reference by the boot loader. Once sysid is
20049 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20050 	 */
20051 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
20052 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
20053 	    (strlen(srpc_domain) == 0)) {
20054 		if (dhcpinit() != 0)
20055 			cmn_err(CE_WARN, "no cached dhcp response");
20056 	}
20057 
20058 	/*
20059 	 * This operation will complete in ip_rput_dlpi with either
20060 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20061 	 */
20062 	return (EINPROGRESS);
20063 bad:
20064 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20065 	/*
20066 	 * We don't have to check for possible removal from illgrp
20067 	 * as we have not yet inserted in illgrp. For groups
20068 	 * without names, this ipif is still not UP and hence
20069 	 * this could not have possibly had any influence in forming
20070 	 * groups.
20071 	 */
20072 
20073 	freemsg(bind_mp);
20074 	freemsg(unbind_mp);
20075 	return (ENOMEM);
20076 }
20077 
20078 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20079 
20080 /*
20081  * DLPI and ARP is up.
20082  * Create all the IREs associated with an interface bring up multicast.
20083  * Set the interface flag and finish other initialization
20084  * that potentially had to be differed to after DL_BIND_ACK.
20085  */
20086 int
20087 ipif_up_done(ipif_t *ipif)
20088 {
20089 	ire_t	*ire_array[20];
20090 	ire_t	**irep = ire_array;
20091 	ire_t	**irep1;
20092 	ipaddr_t net_mask = 0;
20093 	ipaddr_t subnet_mask, route_mask;
20094 	ill_t	*ill = ipif->ipif_ill;
20095 	queue_t	*stq;
20096 	ipif_t	 *src_ipif;
20097 	ipif_t   *tmp_ipif;
20098 	boolean_t	flush_ire_cache = B_TRUE;
20099 	int	err = 0;
20100 	phyint_t *phyi;
20101 	ire_t	**ipif_saved_irep = NULL;
20102 	int ipif_saved_ire_cnt;
20103 	int	cnt;
20104 	boolean_t	src_ipif_held = B_FALSE;
20105 	boolean_t	ire_added = B_FALSE;
20106 	boolean_t	loopback = B_FALSE;
20107 	ip_stack_t	*ipst = ill->ill_ipst;
20108 
20109 	ip1dbg(("ipif_up_done(%s:%u)\n",
20110 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20111 	/* Check if this is a loopback interface */
20112 	if (ipif->ipif_ill->ill_wq == NULL)
20113 		loopback = B_TRUE;
20114 
20115 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20116 	/*
20117 	 * If all other interfaces for this ill are down or DEPRECATED,
20118 	 * or otherwise unsuitable for source address selection, remove
20119 	 * any IRE_CACHE entries for this ill to make sure source
20120 	 * address selection gets to take this new ipif into account.
20121 	 * No need to hold ill_lock while traversing the ipif list since
20122 	 * we are writer
20123 	 */
20124 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20125 	    tmp_ipif = tmp_ipif->ipif_next) {
20126 		if (((tmp_ipif->ipif_flags &
20127 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20128 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20129 		    (tmp_ipif == ipif))
20130 			continue;
20131 		/* first useable pre-existing interface */
20132 		flush_ire_cache = B_FALSE;
20133 		break;
20134 	}
20135 	if (flush_ire_cache)
20136 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20137 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20138 
20139 	/*
20140 	 * Figure out which way the send-to queue should go.  Only
20141 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20142 	 * should show up here.
20143 	 */
20144 	switch (ill->ill_net_type) {
20145 	case IRE_IF_RESOLVER:
20146 		stq = ill->ill_rq;
20147 		break;
20148 	case IRE_IF_NORESOLVER:
20149 	case IRE_LOOPBACK:
20150 		stq = ill->ill_wq;
20151 		break;
20152 	default:
20153 		return (EINVAL);
20154 	}
20155 
20156 	if (IS_LOOPBACK(ill)) {
20157 		/*
20158 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20159 		 * ipif_lookup_on_name(), but in the case of zones we can have
20160 		 * several loopback addresses on lo0. So all the interfaces with
20161 		 * loopback addresses need to be marked IRE_LOOPBACK.
20162 		 */
20163 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20164 		    htonl(INADDR_LOOPBACK))
20165 			ipif->ipif_ire_type = IRE_LOOPBACK;
20166 		else
20167 			ipif->ipif_ire_type = IRE_LOCAL;
20168 	}
20169 
20170 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20171 		/*
20172 		 * Can't use our source address. Select a different
20173 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20174 		 */
20175 		src_ipif = ipif_select_source(ipif->ipif_ill,
20176 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20177 		if (src_ipif == NULL)
20178 			src_ipif = ipif;	/* Last resort */
20179 		else
20180 			src_ipif_held = B_TRUE;
20181 	} else {
20182 		src_ipif = ipif;
20183 	}
20184 
20185 	/* Create all the IREs associated with this interface */
20186 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20187 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20188 
20189 		/*
20190 		 * If we're on a labeled system then make sure that zone-
20191 		 * private addresses have proper remote host database entries.
20192 		 */
20193 		if (is_system_labeled() &&
20194 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20195 		    !tsol_check_interface_address(ipif))
20196 			return (EINVAL);
20197 
20198 		/* Register the source address for __sin6_src_id */
20199 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20200 		    ipif->ipif_zoneid, ipst);
20201 		if (err != 0) {
20202 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20203 			return (err);
20204 		}
20205 
20206 		/* If the interface address is set, create the local IRE. */
20207 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20208 		    (void *)ipif,
20209 		    ipif->ipif_ire_type,
20210 		    ntohl(ipif->ipif_lcl_addr)));
20211 		*irep++ = ire_create(
20212 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20213 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20214 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20215 		    NULL,				/* no gateway */
20216 		    &ip_loopback_mtuplus,		/* max frag size */
20217 		    NULL,
20218 		    ipif->ipif_rq,			/* recv-from queue */
20219 		    NULL,				/* no send-to queue */
20220 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20221 		    ipif,
20222 		    0,
20223 		    0,
20224 		    0,
20225 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20226 		    RTF_PRIVATE : 0,
20227 		    &ire_uinfo_null,
20228 		    NULL,
20229 		    NULL,
20230 		    ipst);
20231 	} else {
20232 		ip1dbg((
20233 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20234 		    ipif->ipif_ire_type,
20235 		    ntohl(ipif->ipif_lcl_addr),
20236 		    (uint_t)ipif->ipif_flags));
20237 	}
20238 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20239 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20240 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20241 	} else {
20242 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20243 	}
20244 
20245 	subnet_mask = ipif->ipif_net_mask;
20246 
20247 	/*
20248 	 * If mask was not specified, use natural netmask of
20249 	 * interface address. Also, store this mask back into the
20250 	 * ipif struct.
20251 	 */
20252 	if (subnet_mask == 0) {
20253 		subnet_mask = net_mask;
20254 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20255 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20256 		    ipif->ipif_v6subnet);
20257 	}
20258 
20259 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20260 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20261 	    ipif->ipif_subnet != INADDR_ANY) {
20262 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20263 
20264 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20265 			route_mask = IP_HOST_MASK;
20266 		} else {
20267 			route_mask = subnet_mask;
20268 		}
20269 
20270 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20271 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20272 		    (void *)ipif, (void *)ill,
20273 		    ill->ill_net_type,
20274 		    ntohl(ipif->ipif_subnet)));
20275 		*irep++ = ire_create(
20276 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20277 		    (uchar_t *)&route_mask,		/* mask */
20278 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20279 		    NULL,				/* no gateway */
20280 		    &ipif->ipif_mtu,			/* max frag */
20281 		    NULL,
20282 		    NULL,				/* no recv queue */
20283 		    stq,				/* send-to queue */
20284 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20285 		    ipif,
20286 		    0,
20287 		    0,
20288 		    0,
20289 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20290 		    &ire_uinfo_null,
20291 		    NULL,
20292 		    NULL,
20293 		    ipst);
20294 	}
20295 
20296 	/*
20297 	 * Create any necessary broadcast IREs.
20298 	 */
20299 	if (ipif->ipif_flags & IPIF_BROADCAST)
20300 		irep = ipif_create_bcast_ires(ipif, irep);
20301 
20302 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20303 
20304 	/* If an earlier ire_create failed, get out now */
20305 	for (irep1 = irep; irep1 > ire_array; ) {
20306 		irep1--;
20307 		if (*irep1 == NULL) {
20308 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20309 			err = ENOMEM;
20310 			goto bad;
20311 		}
20312 	}
20313 
20314 	/*
20315 	 * Need to atomically check for ip_addr_availablity_check
20316 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20317 	 * from group also.The ill_g_lock is grabbed as reader
20318 	 * just to make sure no new ills or new ipifs are being added
20319 	 * to the system while we are checking the uniqueness of addresses.
20320 	 */
20321 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20322 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20323 	/* Mark it up, and increment counters. */
20324 	ipif->ipif_flags |= IPIF_UP;
20325 	ill->ill_ipif_up_count++;
20326 	err = ip_addr_availability_check(ipif);
20327 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20328 	rw_exit(&ipst->ips_ill_g_lock);
20329 
20330 	if (err != 0) {
20331 		/*
20332 		 * Our address may already be up on the same ill. In this case,
20333 		 * the ARP entry for our ipif replaced the one for the other
20334 		 * ipif. So we don't want to delete it (otherwise the other ipif
20335 		 * would be unable to send packets).
20336 		 * ip_addr_availability_check() identifies this case for us and
20337 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20338 		 * which is the expected error code.
20339 		 */
20340 		if (err == EADDRINUSE) {
20341 			freemsg(ipif->ipif_arp_del_mp);
20342 			ipif->ipif_arp_del_mp = NULL;
20343 			err = EADDRNOTAVAIL;
20344 		}
20345 		ill->ill_ipif_up_count--;
20346 		ipif->ipif_flags &= ~IPIF_UP;
20347 		goto bad;
20348 	}
20349 
20350 	/*
20351 	 * Add in all newly created IREs.  ire_create_bcast() has
20352 	 * already checked for duplicates of the IRE_BROADCAST type.
20353 	 * We want to add before we call ifgrp_insert which wants
20354 	 * to know whether IRE_IF_RESOLVER exists or not.
20355 	 *
20356 	 * NOTE : We refrele the ire though we may branch to "bad"
20357 	 *	  later on where we do ire_delete. This is okay
20358 	 *	  because nobody can delete it as we are running
20359 	 *	  exclusively.
20360 	 */
20361 	for (irep1 = irep; irep1 > ire_array; ) {
20362 		irep1--;
20363 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20364 		/*
20365 		 * refheld by ire_add. refele towards the end of the func
20366 		 */
20367 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20368 	}
20369 	ire_added = B_TRUE;
20370 	/*
20371 	 * Form groups if possible.
20372 	 *
20373 	 * If we are supposed to be in a ill_group with a name, insert it
20374 	 * now as we know that at least one ipif is UP. Otherwise form
20375 	 * nameless groups.
20376 	 *
20377 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20378 	 * this ipif into the appropriate interface group, or create a
20379 	 * new one. If this is already in a nameless group, we try to form
20380 	 * a bigger group looking at other ills potentially sharing this
20381 	 * ipif's prefix.
20382 	 */
20383 	phyi = ill->ill_phyint;
20384 	if (phyi->phyint_groupname_len != 0) {
20385 		ASSERT(phyi->phyint_groupname != NULL);
20386 		if (ill->ill_ipif_up_count == 1) {
20387 			ASSERT(ill->ill_group == NULL);
20388 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20389 			    phyi->phyint_groupname, NULL, B_TRUE);
20390 			if (err != 0) {
20391 				ip1dbg(("ipif_up_done: illgrp allocation "
20392 				    "failed, error %d\n", err));
20393 				goto bad;
20394 			}
20395 		}
20396 		ASSERT(ill->ill_group != NULL);
20397 	}
20398 
20399 	/*
20400 	 * When this is part of group, we need to make sure that
20401 	 * any broadcast ires created because of this ipif coming
20402 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20403 	 * so that we don't receive duplicate broadcast packets.
20404 	 */
20405 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20406 		ipif_renominate_bcast(ipif);
20407 
20408 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20409 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20410 	ipif_saved_irep = ipif_recover_ire(ipif);
20411 
20412 	if (!loopback) {
20413 		/*
20414 		 * If the broadcast address has been set, make sure it makes
20415 		 * sense based on the interface address.
20416 		 * Only match on ill since we are sharing broadcast addresses.
20417 		 */
20418 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20419 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20420 			ire_t	*ire;
20421 
20422 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20423 			    IRE_BROADCAST, ipif, ALL_ZONES,
20424 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20425 
20426 			if (ire == NULL) {
20427 				/*
20428 				 * If there isn't a matching broadcast IRE,
20429 				 * revert to the default for this netmask.
20430 				 */
20431 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20432 				mutex_enter(&ipif->ipif_ill->ill_lock);
20433 				ipif_set_default(ipif);
20434 				mutex_exit(&ipif->ipif_ill->ill_lock);
20435 			} else {
20436 				ire_refrele(ire);
20437 			}
20438 		}
20439 
20440 	}
20441 
20442 	if (ill->ill_need_recover_multicast) {
20443 		/*
20444 		 * Need to recover all multicast memberships in the driver.
20445 		 * This had to be deferred until we had attached.  The same
20446 		 * code exists in ipif_up_done_v6() to recover IPv6
20447 		 * memberships.
20448 		 *
20449 		 * Note that it would be preferable to unconditionally do the
20450 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
20451 		 * that since ill_join_allmulti() depends on ill_dl_up being
20452 		 * set, and it is not set until we receive a DL_BIND_ACK after
20453 		 * having called ill_dl_up().
20454 		 */
20455 		ill_recover_multicast(ill);
20456 	}
20457 	/* Join the allhosts multicast address */
20458 	ipif_multicast_up(ipif);
20459 
20460 	if (!loopback) {
20461 		/*
20462 		 * See whether anybody else would benefit from the
20463 		 * new ipif that we added. We call this always rather
20464 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20465 		 * ipif is for the benefit of illgrp_insert (done above)
20466 		 * which does not do source address selection as it does
20467 		 * not want to re-create interface routes that we are
20468 		 * having reference to it here.
20469 		 */
20470 		ill_update_source_selection(ill);
20471 	}
20472 
20473 	for (irep1 = irep; irep1 > ire_array; ) {
20474 		irep1--;
20475 		if (*irep1 != NULL) {
20476 			/* was held in ire_add */
20477 			ire_refrele(*irep1);
20478 		}
20479 	}
20480 
20481 	cnt = ipif_saved_ire_cnt;
20482 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20483 		if (*irep1 != NULL) {
20484 			/* was held in ire_add */
20485 			ire_refrele(*irep1);
20486 		}
20487 	}
20488 
20489 	if (!loopback && ipif->ipif_addr_ready) {
20490 		/* Broadcast an address mask reply. */
20491 		ipif_mask_reply(ipif);
20492 	}
20493 	if (ipif_saved_irep != NULL) {
20494 		kmem_free(ipif_saved_irep,
20495 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20496 	}
20497 	if (src_ipif_held)
20498 		ipif_refrele(src_ipif);
20499 
20500 	/*
20501 	 * This had to be deferred until we had bound.  Tell routing sockets and
20502 	 * others that this interface is up if it looks like the address has
20503 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20504 	 * duplicate address detection to do its thing.
20505 	 */
20506 	if (ipif->ipif_addr_ready)
20507 		ipif_up_notify(ipif);
20508 	return (0);
20509 
20510 bad:
20511 	ip1dbg(("ipif_up_done: FAILED \n"));
20512 	/*
20513 	 * We don't have to bother removing from ill groups because
20514 	 *
20515 	 * 1) For groups with names, we insert only when the first ipif
20516 	 *    comes up. In that case if it fails, it will not be in any
20517 	 *    group. So, we need not try to remove for that case.
20518 	 *
20519 	 * 2) For groups without names, either we tried to insert ipif_ill
20520 	 *    in a group as singleton or found some other group to become
20521 	 *    a bigger group. For the former, if it fails we don't have
20522 	 *    anything to do as ipif_ill is not in the group and for the
20523 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20524 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20525 	 */
20526 	while (irep > ire_array) {
20527 		irep--;
20528 		if (*irep != NULL) {
20529 			ire_delete(*irep);
20530 			if (ire_added)
20531 				ire_refrele(*irep);
20532 		}
20533 	}
20534 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20535 
20536 	if (ipif_saved_irep != NULL) {
20537 		kmem_free(ipif_saved_irep,
20538 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20539 	}
20540 	if (src_ipif_held)
20541 		ipif_refrele(src_ipif);
20542 
20543 	ipif_arp_down(ipif);
20544 	return (err);
20545 }
20546 
20547 /*
20548  * Turn off the ARP with the ILLF_NOARP flag.
20549  */
20550 static int
20551 ill_arp_off(ill_t *ill)
20552 {
20553 	mblk_t	*arp_off_mp = NULL;
20554 	mblk_t	*arp_on_mp = NULL;
20555 
20556 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20557 
20558 	ASSERT(IAM_WRITER_ILL(ill));
20559 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20560 
20561 	/*
20562 	 * If the on message is still around we've already done
20563 	 * an arp_off without doing an arp_on thus there is no
20564 	 * work needed.
20565 	 */
20566 	if (ill->ill_arp_on_mp != NULL)
20567 		return (0);
20568 
20569 	/*
20570 	 * Allocate an ARP on message (to be saved) and an ARP off message
20571 	 */
20572 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20573 	if (!arp_off_mp)
20574 		return (ENOMEM);
20575 
20576 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20577 	if (!arp_on_mp)
20578 		goto failed;
20579 
20580 	ASSERT(ill->ill_arp_on_mp == NULL);
20581 	ill->ill_arp_on_mp = arp_on_mp;
20582 
20583 	/* Send an AR_INTERFACE_OFF request */
20584 	putnext(ill->ill_rq, arp_off_mp);
20585 	return (0);
20586 failed:
20587 
20588 	if (arp_off_mp)
20589 		freemsg(arp_off_mp);
20590 	return (ENOMEM);
20591 }
20592 
20593 /*
20594  * Turn on ARP by turning off the ILLF_NOARP flag.
20595  */
20596 static int
20597 ill_arp_on(ill_t *ill)
20598 {
20599 	mblk_t	*mp;
20600 
20601 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20602 
20603 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20604 
20605 	ASSERT(IAM_WRITER_ILL(ill));
20606 	/*
20607 	 * Send an AR_INTERFACE_ON request if we have already done
20608 	 * an arp_off (which allocated the message).
20609 	 */
20610 	if (ill->ill_arp_on_mp != NULL) {
20611 		mp = ill->ill_arp_on_mp;
20612 		ill->ill_arp_on_mp = NULL;
20613 		putnext(ill->ill_rq, mp);
20614 	}
20615 	return (0);
20616 }
20617 
20618 /*
20619  * Called after either deleting ill from the group or when setting
20620  * FAILED or STANDBY on the interface.
20621  */
20622 static void
20623 illgrp_reset_schednext(ill_t *ill)
20624 {
20625 	ill_group_t *illgrp;
20626 	ill_t *save_ill;
20627 
20628 	ASSERT(IAM_WRITER_ILL(ill));
20629 	/*
20630 	 * When called from illgrp_delete, ill_group will be non-NULL.
20631 	 * But when called from ip_sioctl_flags, it could be NULL if
20632 	 * somebody is setting FAILED/INACTIVE on some interface which
20633 	 * is not part of a group.
20634 	 */
20635 	illgrp = ill->ill_group;
20636 	if (illgrp == NULL)
20637 		return;
20638 	if (illgrp->illgrp_ill_schednext != ill)
20639 		return;
20640 
20641 	illgrp->illgrp_ill_schednext = NULL;
20642 	save_ill = ill;
20643 	/*
20644 	 * Choose a good ill to be the next one for
20645 	 * outbound traffic. As the flags FAILED/STANDBY is
20646 	 * not yet marked when called from ip_sioctl_flags,
20647 	 * we check for ill separately.
20648 	 */
20649 	for (ill = illgrp->illgrp_ill; ill != NULL;
20650 	    ill = ill->ill_group_next) {
20651 		if ((ill != save_ill) &&
20652 		    !(ill->ill_phyint->phyint_flags &
20653 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20654 			illgrp->illgrp_ill_schednext = ill;
20655 			return;
20656 		}
20657 	}
20658 }
20659 
20660 /*
20661  * Given an ill, find the next ill in the group to be scheduled.
20662  * (This should be called by ip_newroute() before ire_create().)
20663  * The passed in ill may be pulled out of the group, after we have picked
20664  * up a different outgoing ill from the same group. However ire add will
20665  * atomically check this.
20666  */
20667 ill_t *
20668 illgrp_scheduler(ill_t *ill)
20669 {
20670 	ill_t *retill;
20671 	ill_group_t *illgrp;
20672 	int illcnt;
20673 	int i;
20674 	uint64_t flags;
20675 	ip_stack_t	*ipst = ill->ill_ipst;
20676 
20677 	/*
20678 	 * We don't use a lock to check for the ill_group. If this ill
20679 	 * is currently being inserted we may end up just returning this
20680 	 * ill itself. That is ok.
20681 	 */
20682 	if (ill->ill_group == NULL) {
20683 		ill_refhold(ill);
20684 		return (ill);
20685 	}
20686 
20687 	/*
20688 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20689 	 * a set of stable ills. No ill can be added or deleted or change
20690 	 * group while we hold the reader lock.
20691 	 */
20692 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20693 	if ((illgrp = ill->ill_group) == NULL) {
20694 		rw_exit(&ipst->ips_ill_g_lock);
20695 		ill_refhold(ill);
20696 		return (ill);
20697 	}
20698 
20699 	illcnt = illgrp->illgrp_ill_count;
20700 	mutex_enter(&illgrp->illgrp_lock);
20701 	retill = illgrp->illgrp_ill_schednext;
20702 
20703 	if (retill == NULL)
20704 		retill = illgrp->illgrp_ill;
20705 
20706 	/*
20707 	 * We do a circular search beginning at illgrp_ill_schednext
20708 	 * or illgrp_ill. We don't check the flags against the ill lock
20709 	 * since it can change anytime. The ire creation will be atomic
20710 	 * and will fail if the ill is FAILED or OFFLINE.
20711 	 */
20712 	for (i = 0; i < illcnt; i++) {
20713 		flags = retill->ill_phyint->phyint_flags;
20714 
20715 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20716 		    ILL_CAN_LOOKUP(retill)) {
20717 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20718 			ill_refhold(retill);
20719 			break;
20720 		}
20721 		retill = retill->ill_group_next;
20722 		if (retill == NULL)
20723 			retill = illgrp->illgrp_ill;
20724 	}
20725 	mutex_exit(&illgrp->illgrp_lock);
20726 	rw_exit(&ipst->ips_ill_g_lock);
20727 
20728 	return (i == illcnt ? NULL : retill);
20729 }
20730 
20731 /*
20732  * Checks for availbility of a usable source address (if there is one) when the
20733  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20734  * this selection is done regardless of the destination.
20735  */
20736 boolean_t
20737 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20738 {
20739 	uint_t	ifindex;
20740 	ipif_t	*ipif = NULL;
20741 	ill_t	*uill;
20742 	boolean_t isv6;
20743 	ip_stack_t	*ipst = ill->ill_ipst;
20744 
20745 	ASSERT(ill != NULL);
20746 
20747 	isv6 = ill->ill_isv6;
20748 	ifindex = ill->ill_usesrc_ifindex;
20749 	if (ifindex != 0) {
20750 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20751 		    NULL, ipst);
20752 		if (uill == NULL)
20753 			return (NULL);
20754 		mutex_enter(&uill->ill_lock);
20755 		for (ipif = uill->ill_ipif; ipif != NULL;
20756 		    ipif = ipif->ipif_next) {
20757 			if (!IPIF_CAN_LOOKUP(ipif))
20758 				continue;
20759 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20760 				continue;
20761 			if (!(ipif->ipif_flags & IPIF_UP))
20762 				continue;
20763 			if (ipif->ipif_zoneid != zoneid)
20764 				continue;
20765 			if ((isv6 &&
20766 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20767 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20768 				continue;
20769 			mutex_exit(&uill->ill_lock);
20770 			ill_refrele(uill);
20771 			return (B_TRUE);
20772 		}
20773 		mutex_exit(&uill->ill_lock);
20774 		ill_refrele(uill);
20775 	}
20776 	return (B_FALSE);
20777 }
20778 
20779 /*
20780  * Determine the best source address given a destination address and an ill.
20781  * Prefers non-deprecated over deprecated but will return a deprecated
20782  * address if there is no other choice. If there is a usable source address
20783  * on the interface pointed to by ill_usesrc_ifindex then that is given
20784  * first preference.
20785  *
20786  * Returns NULL if there is no suitable source address for the ill.
20787  * This only occurs when there is no valid source address for the ill.
20788  */
20789 ipif_t *
20790 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20791 {
20792 	ipif_t *ipif;
20793 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20794 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20795 	int index = 0;
20796 	boolean_t wrapped = B_FALSE;
20797 	boolean_t same_subnet_only = B_FALSE;
20798 	boolean_t ipif_same_found, ipif_other_found;
20799 	boolean_t specific_found;
20800 	ill_t	*till, *usill = NULL;
20801 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20802 	ip_stack_t	*ipst = ill->ill_ipst;
20803 
20804 	if (ill->ill_usesrc_ifindex != 0) {
20805 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20806 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20807 		if (usill != NULL)
20808 			ill = usill;	/* Select source from usesrc ILL */
20809 		else
20810 			return (NULL);
20811 	}
20812 
20813 	/*
20814 	 * If we're dealing with an unlabeled destination on a labeled system,
20815 	 * make sure that we ignore source addresses that are incompatible with
20816 	 * the destination's default label.  That destination's default label
20817 	 * must dominate the minimum label on the source address.
20818 	 */
20819 	dst_rhtp = NULL;
20820 	if (is_system_labeled()) {
20821 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20822 		if (dst_rhtp == NULL)
20823 			return (NULL);
20824 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20825 			TPC_RELE(dst_rhtp);
20826 			dst_rhtp = NULL;
20827 		}
20828 	}
20829 
20830 	/*
20831 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20832 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20833 	 * After selecting the right ipif, under ill_lock make sure ipif is
20834 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20835 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20836 	 * but not under a lock.
20837 	 */
20838 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20839 
20840 retry:
20841 	till = ill;
20842 	ipif_arr[0] = NULL;
20843 
20844 	if (till->ill_group != NULL)
20845 		till = till->ill_group->illgrp_ill;
20846 
20847 	/*
20848 	 * Choose one good source address from each ill across the group.
20849 	 * If possible choose a source address in the same subnet as
20850 	 * the destination address.
20851 	 *
20852 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20853 	 * This is okay because of the following.
20854 	 *
20855 	 *    If PHYI_FAILED is set and we still have non-deprecated
20856 	 *    addresses, it means the addresses have not yet been
20857 	 *    failed over to a different interface. We potentially
20858 	 *    select them to create IRE_CACHES, which will be later
20859 	 *    flushed when the addresses move over.
20860 	 *
20861 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20862 	 *    addresses, it means either the user has configured them
20863 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20864 	 *    been moved over. For the former, in.mpathd does a failover
20865 	 *    when the interface becomes INACTIVE and hence we should
20866 	 *    not find them. Once INACTIVE is set, we don't allow them
20867 	 *    to create logical interfaces anymore. For the latter, a
20868 	 *    flush will happen when INACTIVE is cleared which will
20869 	 *    flush the IRE_CACHES.
20870 	 *
20871 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20872 	 *    over soon. We potentially select them to create IRE_CACHEs,
20873 	 *    which will be later flushed when the addresses move over.
20874 	 *
20875 	 * NOTE : As ipif_select_source is called to borrow source address
20876 	 * for an ipif that is part of a group, source address selection
20877 	 * will be re-done whenever the group changes i.e either an
20878 	 * insertion/deletion in the group.
20879 	 *
20880 	 * Fill ipif_arr[] with source addresses, using these rules:
20881 	 *
20882 	 *	1. At most one source address from a given ill ends up
20883 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20884 	 *	   associated with a given ill ends up in ipif_arr[].
20885 	 *
20886 	 *	2. If there is at least one non-deprecated ipif in the
20887 	 *	   IPMP group with a source address on the same subnet as
20888 	 *	   our destination, then fill ipif_arr[] only with
20889 	 *	   source addresses on the same subnet as our destination.
20890 	 *	   Note that because of (1), only the first
20891 	 *	   non-deprecated ipif found with a source address
20892 	 *	   matching the destination ends up in ipif_arr[].
20893 	 *
20894 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20895 	 *	   addresses not in the same subnet as our destination.
20896 	 *	   Again, because of (1), only the first off-subnet source
20897 	 *	   address will be chosen.
20898 	 *
20899 	 *	4. If there are no non-deprecated ipifs, then just use
20900 	 *	   the source address associated with the last deprecated
20901 	 *	   one we find that happens to be on the same subnet,
20902 	 *	   otherwise the first one not in the same subnet.
20903 	 */
20904 	specific_found = B_FALSE;
20905 	for (; till != NULL; till = till->ill_group_next) {
20906 		ipif_same_found = B_FALSE;
20907 		ipif_other_found = B_FALSE;
20908 		for (ipif = till->ill_ipif; ipif != NULL;
20909 		    ipif = ipif->ipif_next) {
20910 			if (!IPIF_CAN_LOOKUP(ipif))
20911 				continue;
20912 			/* Always skip NOLOCAL and ANYCAST interfaces */
20913 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20914 				continue;
20915 			if (!(ipif->ipif_flags & IPIF_UP) ||
20916 			    !ipif->ipif_addr_ready)
20917 				continue;
20918 			if (ipif->ipif_zoneid != zoneid &&
20919 			    ipif->ipif_zoneid != ALL_ZONES)
20920 				continue;
20921 			/*
20922 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20923 			 * but are not valid as source addresses.
20924 			 */
20925 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20926 				continue;
20927 
20928 			/*
20929 			 * Check compatibility of local address for
20930 			 * destination's default label if we're on a labeled
20931 			 * system.  Incompatible addresses can't be used at
20932 			 * all.
20933 			 */
20934 			if (dst_rhtp != NULL) {
20935 				boolean_t incompat;
20936 
20937 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20938 				    IPV4_VERSION, B_FALSE);
20939 				if (src_rhtp == NULL)
20940 					continue;
20941 				incompat =
20942 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20943 				    src_rhtp->tpc_tp.tp_doi !=
20944 				    dst_rhtp->tpc_tp.tp_doi ||
20945 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20946 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20947 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20948 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20949 				TPC_RELE(src_rhtp);
20950 				if (incompat)
20951 					continue;
20952 			}
20953 
20954 			/*
20955 			 * We prefer not to use all all-zones addresses, if we
20956 			 * can avoid it, as they pose problems with unlabeled
20957 			 * destinations.
20958 			 */
20959 			if (ipif->ipif_zoneid != ALL_ZONES) {
20960 				if (!specific_found &&
20961 				    (!same_subnet_only ||
20962 				    (ipif->ipif_net_mask & dst) ==
20963 				    ipif->ipif_subnet)) {
20964 					index = 0;
20965 					specific_found = B_TRUE;
20966 					ipif_other_found = B_FALSE;
20967 				}
20968 			} else {
20969 				if (specific_found)
20970 					continue;
20971 			}
20972 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20973 				if (ipif_dep == NULL ||
20974 				    (ipif->ipif_net_mask & dst) ==
20975 				    ipif->ipif_subnet)
20976 					ipif_dep = ipif;
20977 				continue;
20978 			}
20979 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20980 				/* found a source address in the same subnet */
20981 				if (!same_subnet_only) {
20982 					same_subnet_only = B_TRUE;
20983 					index = 0;
20984 				}
20985 				ipif_same_found = B_TRUE;
20986 			} else {
20987 				if (same_subnet_only || ipif_other_found)
20988 					continue;
20989 				ipif_other_found = B_TRUE;
20990 			}
20991 			ipif_arr[index++] = ipif;
20992 			if (index == MAX_IPIF_SELECT_SOURCE) {
20993 				wrapped = B_TRUE;
20994 				index = 0;
20995 			}
20996 			if (ipif_same_found)
20997 				break;
20998 		}
20999 	}
21000 
21001 	if (ipif_arr[0] == NULL) {
21002 		ipif = ipif_dep;
21003 	} else {
21004 		if (wrapped)
21005 			index = MAX_IPIF_SELECT_SOURCE;
21006 		ipif = ipif_arr[ipif_rand(ipst) % index];
21007 		ASSERT(ipif != NULL);
21008 	}
21009 
21010 	if (ipif != NULL) {
21011 		mutex_enter(&ipif->ipif_ill->ill_lock);
21012 		if (!IPIF_CAN_LOOKUP(ipif)) {
21013 			mutex_exit(&ipif->ipif_ill->ill_lock);
21014 			goto retry;
21015 		}
21016 		ipif_refhold_locked(ipif);
21017 		mutex_exit(&ipif->ipif_ill->ill_lock);
21018 	}
21019 
21020 	rw_exit(&ipst->ips_ill_g_lock);
21021 	if (usill != NULL)
21022 		ill_refrele(usill);
21023 	if (dst_rhtp != NULL)
21024 		TPC_RELE(dst_rhtp);
21025 
21026 #ifdef DEBUG
21027 	if (ipif == NULL) {
21028 		char buf1[INET6_ADDRSTRLEN];
21029 
21030 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21031 		    ill->ill_name,
21032 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21033 	} else {
21034 		char buf1[INET6_ADDRSTRLEN];
21035 		char buf2[INET6_ADDRSTRLEN];
21036 
21037 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21038 		    ipif->ipif_ill->ill_name,
21039 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21040 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21041 		    buf2, sizeof (buf2))));
21042 	}
21043 #endif /* DEBUG */
21044 	return (ipif);
21045 }
21046 
21047 
21048 /*
21049  * If old_ipif is not NULL, see if ipif was derived from old
21050  * ipif and if so, recreate the interface route by re-doing
21051  * source address selection. This happens when ipif_down ->
21052  * ipif_update_other_ipifs calls us.
21053  *
21054  * If old_ipif is NULL, just redo the source address selection
21055  * if needed. This happens when illgrp_insert or ipif_up_done
21056  * calls us.
21057  */
21058 static void
21059 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21060 {
21061 	ire_t *ire;
21062 	ire_t *ipif_ire;
21063 	queue_t *stq;
21064 	ipif_t *nipif;
21065 	ill_t *ill;
21066 	boolean_t need_rele = B_FALSE;
21067 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21068 
21069 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21070 	ASSERT(IAM_WRITER_IPIF(ipif));
21071 
21072 	ill = ipif->ipif_ill;
21073 	if (!(ipif->ipif_flags &
21074 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21075 		/*
21076 		 * Can't possibly have borrowed the source
21077 		 * from old_ipif.
21078 		 */
21079 		return;
21080 	}
21081 
21082 	/*
21083 	 * Is there any work to be done? No work if the address
21084 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21085 	 * ipif_select_source() does not borrow addresses from
21086 	 * NOLOCAL and ANYCAST interfaces).
21087 	 */
21088 	if ((old_ipif != NULL) &&
21089 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21090 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21091 	    (old_ipif->ipif_flags &
21092 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21093 		return;
21094 	}
21095 
21096 	/*
21097 	 * Perform the same checks as when creating the
21098 	 * IRE_INTERFACE in ipif_up_done.
21099 	 */
21100 	if (!(ipif->ipif_flags & IPIF_UP))
21101 		return;
21102 
21103 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21104 	    (ipif->ipif_subnet == INADDR_ANY))
21105 		return;
21106 
21107 	ipif_ire = ipif_to_ire(ipif);
21108 	if (ipif_ire == NULL)
21109 		return;
21110 
21111 	/*
21112 	 * We know that ipif uses some other source for its
21113 	 * IRE_INTERFACE. Is it using the source of this
21114 	 * old_ipif?
21115 	 */
21116 	if (old_ipif != NULL &&
21117 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21118 		ire_refrele(ipif_ire);
21119 		return;
21120 	}
21121 	if (ip_debug > 2) {
21122 		/* ip1dbg */
21123 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21124 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21125 	}
21126 
21127 	stq = ipif_ire->ire_stq;
21128 
21129 	/*
21130 	 * Can't use our source address. Select a different
21131 	 * source address for the IRE_INTERFACE.
21132 	 */
21133 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21134 	if (nipif == NULL) {
21135 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21136 		nipif = ipif;
21137 	} else {
21138 		need_rele = B_TRUE;
21139 	}
21140 
21141 	ire = ire_create(
21142 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21143 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21144 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21145 	    NULL,				/* no gateway */
21146 	    &ipif->ipif_mtu,			/* max frag */
21147 	    NULL,				/* no src nce */
21148 	    NULL,				/* no recv from queue */
21149 	    stq,				/* send-to queue */
21150 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21151 	    ipif,
21152 	    0,
21153 	    0,
21154 	    0,
21155 	    0,
21156 	    &ire_uinfo_null,
21157 	    NULL,
21158 	    NULL,
21159 	    ipst);
21160 
21161 	if (ire != NULL) {
21162 		ire_t *ret_ire;
21163 		int error;
21164 
21165 		/*
21166 		 * We don't need ipif_ire anymore. We need to delete
21167 		 * before we add so that ire_add does not detect
21168 		 * duplicates.
21169 		 */
21170 		ire_delete(ipif_ire);
21171 		ret_ire = ire;
21172 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21173 		ASSERT(error == 0);
21174 		ASSERT(ire == ret_ire);
21175 		/* Held in ire_add */
21176 		ire_refrele(ret_ire);
21177 	}
21178 	/*
21179 	 * Either we are falling through from above or could not
21180 	 * allocate a replacement.
21181 	 */
21182 	ire_refrele(ipif_ire);
21183 	if (need_rele)
21184 		ipif_refrele(nipif);
21185 }
21186 
21187 /*
21188  * This old_ipif is going away.
21189  *
21190  * Determine if any other ipif's is using our address as
21191  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21192  * IPIF_DEPRECATED).
21193  * Find the IRE_INTERFACE for such ipifs and recreate them
21194  * to use an different source address following the rules in
21195  * ipif_up_done.
21196  *
21197  * This function takes an illgrp as an argument so that illgrp_delete
21198  * can call this to update source address even after deleting the
21199  * old_ipif->ipif_ill from the ill group.
21200  */
21201 static void
21202 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21203 {
21204 	ipif_t *ipif;
21205 	ill_t *ill;
21206 	char	buf[INET6_ADDRSTRLEN];
21207 
21208 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21209 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21210 
21211 	ill = old_ipif->ipif_ill;
21212 
21213 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21214 	    ill->ill_name,
21215 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21216 	    buf, sizeof (buf))));
21217 	/*
21218 	 * If this part of a group, look at all ills as ipif_select_source
21219 	 * borrows source address across all the ills in the group.
21220 	 */
21221 	if (illgrp != NULL)
21222 		ill = illgrp->illgrp_ill;
21223 
21224 	for (; ill != NULL; ill = ill->ill_group_next) {
21225 		for (ipif = ill->ill_ipif; ipif != NULL;
21226 		    ipif = ipif->ipif_next) {
21227 
21228 			if (ipif == old_ipif)
21229 				continue;
21230 
21231 			ipif_recreate_interface_routes(old_ipif, ipif);
21232 		}
21233 	}
21234 }
21235 
21236 /* ARGSUSED */
21237 int
21238 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21239 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21240 {
21241 	/*
21242 	 * ill_phyint_reinit merged the v4 and v6 into a single
21243 	 * ipsq. Could also have become part of a ipmp group in the
21244 	 * process, and we might not have been able to complete the
21245 	 * operation in ipif_set_values, if we could not become
21246 	 * exclusive.  If so restart it here.
21247 	 */
21248 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21249 }
21250 
21251 /*
21252  * Can operate on either a module or a driver queue.
21253  * Returns an error if not a module queue.
21254  */
21255 /* ARGSUSED */
21256 int
21257 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21258     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21259 {
21260 	queue_t		*q1 = q;
21261 	char 		*cp;
21262 	char		interf_name[LIFNAMSIZ];
21263 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21264 
21265 	if (q->q_next == NULL) {
21266 		ip1dbg((
21267 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21268 		return (EINVAL);
21269 	}
21270 
21271 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21272 		return (EALREADY);
21273 
21274 	do {
21275 		q1 = q1->q_next;
21276 	} while (q1->q_next);
21277 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21278 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21279 
21280 	/*
21281 	 * Here we are not going to delay the ioack until after
21282 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21283 	 * original ioctl message before sending the requests.
21284 	 */
21285 	return (ipif_set_values(q, mp, interf_name, &ppa));
21286 }
21287 
21288 /* ARGSUSED */
21289 int
21290 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21291     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21292 {
21293 	return (ENXIO);
21294 }
21295 
21296 /*
21297  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21298  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21299  * ire_check_and_create_bcast()).
21300  */
21301 static ire_t **
21302 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21303 {
21304 	ipaddr_t addr;
21305 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21306 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21307 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21308 
21309 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21310 
21311 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21312 
21313 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21314 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21315 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21316 
21317 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21318 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21319 
21320 	/*
21321 	 * For backward compatibility, we create net broadcast IREs based on
21322 	 * the old "IP address class system", since some old machines only
21323 	 * respond to these class derived net broadcast.  However, we must not
21324 	 * create these net broadcast IREs if the subnetmask is shorter than
21325 	 * the IP address class based derived netmask.  Otherwise, we may
21326 	 * create a net broadcast address which is the same as an IP address
21327 	 * on the subnet -- and then TCP will refuse to talk to that address.
21328 	 */
21329 	if (netmask < subnetmask) {
21330 		addr = netmask & ipif->ipif_subnet;
21331 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21332 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21333 		    flags);
21334 	}
21335 
21336 	/*
21337 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21338 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21339 	 * created.  Creating these broadcast IREs will only create confusion
21340 	 * as `addr' will be the same as the IP address.
21341 	 */
21342 	if (subnetmask != 0xFFFFFFFF) {
21343 		addr = ipif->ipif_subnet;
21344 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21345 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21346 		    irep, flags);
21347 	}
21348 
21349 	return (irep);
21350 }
21351 
21352 /*
21353  * Broadcast IRE info structure used in the functions below.  Since we
21354  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21355  */
21356 typedef struct bcast_ireinfo {
21357 	uchar_t		bi_type;	/* BCAST_* value from below */
21358 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21359 			bi_needrep:1,	/* do we need to replace it? */
21360 			bi_haverep:1,	/* have we replaced it? */
21361 			bi_pad:5;
21362 	ipaddr_t	bi_addr;	/* IRE address */
21363 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21364 } bcast_ireinfo_t;
21365 
21366 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21367 
21368 /*
21369  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21370  * return B_TRUE if it should immediately be used to recreate the IRE.
21371  */
21372 static boolean_t
21373 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21374 {
21375 	ipaddr_t addr;
21376 
21377 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21378 
21379 	switch (bireinfop->bi_type) {
21380 	case BCAST_NET:
21381 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21382 		if (addr != bireinfop->bi_addr)
21383 			return (B_FALSE);
21384 		break;
21385 	case BCAST_SUBNET:
21386 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21387 			return (B_FALSE);
21388 		break;
21389 	}
21390 
21391 	bireinfop->bi_needrep = 1;
21392 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21393 		if (bireinfop->bi_backup == NULL)
21394 			bireinfop->bi_backup = ipif;
21395 		return (B_FALSE);
21396 	}
21397 	return (B_TRUE);
21398 }
21399 
21400 /*
21401  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21402  * them ala ire_check_and_create_bcast().
21403  */
21404 static ire_t **
21405 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21406 {
21407 	ipaddr_t mask, addr;
21408 
21409 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21410 
21411 	addr = bireinfop->bi_addr;
21412 	irep = ire_create_bcast(ipif, addr, irep);
21413 
21414 	switch (bireinfop->bi_type) {
21415 	case BCAST_NET:
21416 		mask = ip_net_mask(ipif->ipif_subnet);
21417 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21418 		break;
21419 	case BCAST_SUBNET:
21420 		mask = ipif->ipif_net_mask;
21421 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21422 		break;
21423 	}
21424 
21425 	bireinfop->bi_haverep = 1;
21426 	return (irep);
21427 }
21428 
21429 /*
21430  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21431  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21432  * that are going away are still needed.  If so, have ipif_create_bcast()
21433  * recreate them (except for the deprecated case, as explained below).
21434  */
21435 static ire_t **
21436 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21437     ire_t **irep)
21438 {
21439 	int i;
21440 	ipif_t *ipif;
21441 
21442 	ASSERT(!ill->ill_isv6);
21443 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21444 		/*
21445 		 * Skip this ipif if it's (a) the one being taken down, (b)
21446 		 * not in the same zone, or (c) has no valid local address.
21447 		 */
21448 		if (ipif == test_ipif ||
21449 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21450 		    ipif->ipif_subnet == 0 ||
21451 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21452 		    (IPIF_UP|IPIF_BROADCAST))
21453 			continue;
21454 
21455 		/*
21456 		 * For each dying IRE that hasn't yet been replaced, see if
21457 		 * `ipif' needs it and whether the IRE should be recreated on
21458 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21459 		 * will return B_FALSE even if `ipif' needs the IRE on the
21460 		 * hopes that we'll later find a needy non-deprecated ipif.
21461 		 * However, the ipif is recorded in bi_backup for possible
21462 		 * subsequent use by ipif_check_bcast_ires().
21463 		 */
21464 		for (i = 0; i < BCAST_COUNT; i++) {
21465 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21466 				continue;
21467 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21468 				continue;
21469 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21470 		}
21471 
21472 		/*
21473 		 * If we've replaced all of the broadcast IREs that are going
21474 		 * to be taken down, we know we're done.
21475 		 */
21476 		for (i = 0; i < BCAST_COUNT; i++) {
21477 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21478 				break;
21479 		}
21480 		if (i == BCAST_COUNT)
21481 			break;
21482 	}
21483 	return (irep);
21484 }
21485 
21486 /*
21487  * Check if `test_ipif' (which is going away) is associated with any existing
21488  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21489  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21490  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21491  *
21492  * This is necessary because broadcast IREs are shared.  In particular, a
21493  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21494  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21495  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21496  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21497  * same zone, they will share the same set of broadcast IREs.
21498  *
21499  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21500  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21501  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21502  */
21503 static void
21504 ipif_check_bcast_ires(ipif_t *test_ipif)
21505 {
21506 	ill_t		*ill = test_ipif->ipif_ill;
21507 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21508 	ire_t		**irep1, **irep = &ire_array[0];
21509 	uint_t 		i, willdie;
21510 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21511 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21512 
21513 	ASSERT(!test_ipif->ipif_isv6);
21514 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21515 
21516 	/*
21517 	 * No broadcast IREs for the LOOPBACK interface
21518 	 * or others such as point to point and IPIF_NOXMIT.
21519 	 */
21520 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21521 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21522 		return;
21523 
21524 	bzero(bireinfo, sizeof (bireinfo));
21525 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21526 	bireinfo[0].bi_addr = 0;
21527 
21528 	bireinfo[1].bi_type = BCAST_ALLONES;
21529 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21530 
21531 	bireinfo[2].bi_type = BCAST_NET;
21532 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21533 
21534 	if (test_ipif->ipif_net_mask != 0)
21535 		mask = test_ipif->ipif_net_mask;
21536 	bireinfo[3].bi_type = BCAST_SUBNET;
21537 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21538 
21539 	/*
21540 	 * Figure out what (if any) broadcast IREs will die as a result of
21541 	 * `test_ipif' going away.  If none will die, we're done.
21542 	 */
21543 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21544 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21545 		    test_ipif, ALL_ZONES, NULL,
21546 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21547 		if (ire != NULL) {
21548 			willdie++;
21549 			bireinfo[i].bi_willdie = 1;
21550 			ire_refrele(ire);
21551 		}
21552 	}
21553 
21554 	if (willdie == 0)
21555 		return;
21556 
21557 	/*
21558 	 * Walk through all the ipifs that will be affected by the dying IREs,
21559 	 * and recreate the IREs as necessary.
21560 	 */
21561 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21562 
21563 	/*
21564 	 * Scan through the set of broadcast IREs and see if there are any
21565 	 * that we need to replace that have not yet been replaced.  If so,
21566 	 * replace them using the appropriate backup ipif.
21567 	 */
21568 	for (i = 0; i < BCAST_COUNT; i++) {
21569 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21570 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21571 			    &bireinfo[i], irep);
21572 	}
21573 
21574 	/*
21575 	 * If we can't create all of them, don't add any of them.  (Code in
21576 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21577 	 * non-loopback copy and loopback copy for a given address.)
21578 	 */
21579 	for (irep1 = irep; irep1 > ire_array; ) {
21580 		irep1--;
21581 		if (*irep1 == NULL) {
21582 			ip0dbg(("ipif_check_bcast_ires: can't create "
21583 			    "IRE_BROADCAST, memory allocation failure\n"));
21584 			while (irep > ire_array) {
21585 				irep--;
21586 				if (*irep != NULL)
21587 					ire_delete(*irep);
21588 			}
21589 			return;
21590 		}
21591 	}
21592 
21593 	for (irep1 = irep; irep1 > ire_array; ) {
21594 		irep1--;
21595 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21596 			ire_refrele(*irep1);		/* Held in ire_add */
21597 	}
21598 }
21599 
21600 /*
21601  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21602  * from lifr_flags and the name from lifr_name.
21603  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21604  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21605  * Returns EINPROGRESS when mp has been consumed by queueing it on
21606  * ill_pending_mp and the ioctl will complete in ip_rput.
21607  *
21608  * Can operate on either a module or a driver queue.
21609  * Returns an error if not a module queue.
21610  */
21611 /* ARGSUSED */
21612 int
21613 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21614     ip_ioctl_cmd_t *ipip, void *if_req)
21615 {
21616 	ill_t	*ill = q->q_ptr;
21617 	phyint_t *phyi;
21618 	ip_stack_t *ipst;
21619 	struct lifreq *lifr = if_req;
21620 
21621 	ASSERT(ipif != NULL);
21622 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21623 
21624 	if (q->q_next == NULL) {
21625 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21626 		return (EINVAL);
21627 	}
21628 
21629 	/*
21630 	 * If we are not writer on 'q' then this interface exists already
21631 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21632 	 * so return EALREADY.
21633 	 */
21634 	if (ill != ipif->ipif_ill)
21635 		return (EALREADY);
21636 
21637 	if (ill->ill_name[0] != '\0')
21638 		return (EALREADY);
21639 
21640 	/*
21641 	 * Set all the flags. Allows all kinds of override. Provide some
21642 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21643 	 * unless there is either multicast/broadcast support in the driver
21644 	 * or it is a pt-pt link.
21645 	 */
21646 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21647 		/* Meaningless to IP thus don't allow them to be set. */
21648 		ip1dbg(("ip_setname: EINVAL 1\n"));
21649 		return (EINVAL);
21650 	}
21651 
21652 	/*
21653 	 * If there's another ill already with the requested name, ensure
21654 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21655 	 * fuse together two unrelated ills, which will cause chaos.
21656 	 */
21657 	ipst = ill->ill_ipst;
21658 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21659 	    lifr->lifr_name, NULL);
21660 	if (phyi != NULL) {
21661 		ill_t *ill_mate = phyi->phyint_illv4;
21662 
21663 		if (ill_mate == NULL)
21664 			ill_mate = phyi->phyint_illv6;
21665 		ASSERT(ill_mate != NULL);
21666 
21667 		if (ill_mate->ill_media->ip_m_mac_type !=
21668 		    ill->ill_media->ip_m_mac_type) {
21669 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21670 			    "use the same ill name on differing media\n"));
21671 			return (EINVAL);
21672 		}
21673 	}
21674 
21675 	/*
21676 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21677 	 * ill_bcast_addr_length info.
21678 	 */
21679 	if (!ill->ill_needs_attach &&
21680 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21681 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21682 	    ill->ill_bcast_addr_length == 0)) {
21683 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21684 		ip1dbg(("ip_setname: EINVAL 2\n"));
21685 		return (EINVAL);
21686 	}
21687 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21688 	    ((lifr->lifr_flags & IFF_IPV6) ||
21689 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21690 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21691 		ip1dbg(("ip_setname: EINVAL 3\n"));
21692 		return (EINVAL);
21693 	}
21694 	if (lifr->lifr_flags & IFF_UP) {
21695 		/* Can only be set with SIOCSLIFFLAGS */
21696 		ip1dbg(("ip_setname: EINVAL 4\n"));
21697 		return (EINVAL);
21698 	}
21699 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21700 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21701 		ip1dbg(("ip_setname: EINVAL 5\n"));
21702 		return (EINVAL);
21703 	}
21704 	/*
21705 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21706 	 */
21707 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21708 	    !(lifr->lifr_flags & IFF_IPV6) &&
21709 	    !(ipif->ipif_isv6)) {
21710 		ip1dbg(("ip_setname: EINVAL 6\n"));
21711 		return (EINVAL);
21712 	}
21713 
21714 	/*
21715 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21716 	 * we have all the flags here. So, we assign rather than we OR.
21717 	 * We can't OR the flags here because we don't want to set
21718 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21719 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21720 	 * on lifr_flags value here.
21721 	 */
21722 	/*
21723 	 * This ill has not been inserted into the global list.
21724 	 * So we are still single threaded and don't need any lock
21725 	 */
21726 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21727 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21728 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21729 
21730 	/* We started off as V4. */
21731 	if (ill->ill_flags & ILLF_IPV6) {
21732 		ill->ill_phyint->phyint_illv6 = ill;
21733 		ill->ill_phyint->phyint_illv4 = NULL;
21734 	}
21735 
21736 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21737 }
21738 
21739 /* ARGSUSED */
21740 int
21741 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21742     ip_ioctl_cmd_t *ipip, void *if_req)
21743 {
21744 	/*
21745 	 * ill_phyint_reinit merged the v4 and v6 into a single
21746 	 * ipsq. Could also have become part of a ipmp group in the
21747 	 * process, and we might not have been able to complete the
21748 	 * slifname in ipif_set_values, if we could not become
21749 	 * exclusive.  If so restart it here
21750 	 */
21751 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21752 }
21753 
21754 /*
21755  * Return a pointer to the ipif which matches the index, IP version type and
21756  * zoneid.
21757  */
21758 ipif_t *
21759 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21760     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21761 {
21762 	ill_t	*ill;
21763 	ipif_t	*ipif = NULL;
21764 
21765 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21766 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21767 
21768 	if (err != NULL)
21769 		*err = 0;
21770 
21771 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21772 	if (ill != NULL) {
21773 		mutex_enter(&ill->ill_lock);
21774 		for (ipif = ill->ill_ipif; ipif != NULL;
21775 		    ipif = ipif->ipif_next) {
21776 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21777 			    zoneid == ipif->ipif_zoneid ||
21778 			    ipif->ipif_zoneid == ALL_ZONES)) {
21779 				ipif_refhold_locked(ipif);
21780 				break;
21781 			}
21782 		}
21783 		mutex_exit(&ill->ill_lock);
21784 		ill_refrele(ill);
21785 		if (ipif == NULL && err != NULL)
21786 			*err = ENXIO;
21787 	}
21788 	return (ipif);
21789 }
21790 
21791 typedef struct conn_change_s {
21792 	uint_t cc_old_ifindex;
21793 	uint_t cc_new_ifindex;
21794 } conn_change_t;
21795 
21796 /*
21797  * ipcl_walk function for changing interface index.
21798  */
21799 static void
21800 conn_change_ifindex(conn_t *connp, caddr_t arg)
21801 {
21802 	conn_change_t *connc;
21803 	uint_t old_ifindex;
21804 	uint_t new_ifindex;
21805 	int i;
21806 	ilg_t *ilg;
21807 
21808 	connc = (conn_change_t *)arg;
21809 	old_ifindex = connc->cc_old_ifindex;
21810 	new_ifindex = connc->cc_new_ifindex;
21811 
21812 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21813 		connp->conn_orig_bound_ifindex = new_ifindex;
21814 
21815 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21816 		connp->conn_orig_multicast_ifindex = new_ifindex;
21817 
21818 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21819 		ilg = &connp->conn_ilg[i];
21820 		if (ilg->ilg_orig_ifindex == old_ifindex)
21821 			ilg->ilg_orig_ifindex = new_ifindex;
21822 	}
21823 }
21824 
21825 /*
21826  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21827  * to new_index if it matches the old_index.
21828  *
21829  * Failovers typically happen within a group of ills. But somebody
21830  * can remove an ill from the group after a failover happened. If
21831  * we are setting the ifindex after this, we potentially need to
21832  * look at all the ills rather than just the ones in the group.
21833  * We cut down the work by looking at matching ill_net_types
21834  * and ill_types as we could not possibly grouped them together.
21835  */
21836 static void
21837 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21838 {
21839 	ill_t *ill;
21840 	ipif_t *ipif;
21841 	uint_t old_ifindex;
21842 	uint_t new_ifindex;
21843 	ilm_t *ilm;
21844 	ill_walk_context_t ctx;
21845 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21846 
21847 	old_ifindex = connc->cc_old_ifindex;
21848 	new_ifindex = connc->cc_new_ifindex;
21849 
21850 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21851 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21852 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21853 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21854 		    (ill_orig->ill_type != ill->ill_type)) {
21855 			continue;
21856 		}
21857 		for (ipif = ill->ill_ipif; ipif != NULL;
21858 		    ipif = ipif->ipif_next) {
21859 			if (ipif->ipif_orig_ifindex == old_ifindex)
21860 				ipif->ipif_orig_ifindex = new_ifindex;
21861 		}
21862 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21863 			if (ilm->ilm_orig_ifindex == old_ifindex)
21864 				ilm->ilm_orig_ifindex = new_ifindex;
21865 		}
21866 	}
21867 	rw_exit(&ipst->ips_ill_g_lock);
21868 }
21869 
21870 /*
21871  * We first need to ensure that the new index is unique, and
21872  * then carry the change across both v4 and v6 ill representation
21873  * of the physical interface.
21874  */
21875 /* ARGSUSED */
21876 int
21877 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21878     ip_ioctl_cmd_t *ipip, void *ifreq)
21879 {
21880 	ill_t		*ill;
21881 	ill_t		*ill_other;
21882 	phyint_t	*phyi;
21883 	int		old_index;
21884 	conn_change_t	connc;
21885 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21886 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21887 	uint_t	index;
21888 	ill_t	*ill_v4;
21889 	ill_t	*ill_v6;
21890 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21891 
21892 	if (ipip->ipi_cmd_type == IF_CMD)
21893 		index = ifr->ifr_index;
21894 	else
21895 		index = lifr->lifr_index;
21896 
21897 	/*
21898 	 * Only allow on physical interface. Also, index zero is illegal.
21899 	 *
21900 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21901 	 *
21902 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21903 	 *    implies a possible failback might have to happen. As failback
21904 	 *    depends on the old index, we should fail setting the index.
21905 	 *
21906 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21907 	 *    any addresses or multicast memberships are failed over to
21908 	 *    a non-STANDBY interface. As failback depends on the old
21909 	 *    index, we should fail setting the index for this case also.
21910 	 *
21911 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21912 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21913 	 */
21914 	ill = ipif->ipif_ill;
21915 	phyi = ill->ill_phyint;
21916 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21917 	    ipif->ipif_id != 0 || index == 0) {
21918 		return (EINVAL);
21919 	}
21920 	old_index = phyi->phyint_ifindex;
21921 
21922 	/* If the index is not changing, no work to do */
21923 	if (old_index == index)
21924 		return (0);
21925 
21926 	/*
21927 	 * Use ill_lookup_on_ifindex to determine if the
21928 	 * new index is unused and if so allow the change.
21929 	 */
21930 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21931 	    ipst);
21932 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21933 	    ipst);
21934 	if (ill_v6 != NULL || ill_v4 != NULL) {
21935 		if (ill_v4 != NULL)
21936 			ill_refrele(ill_v4);
21937 		if (ill_v6 != NULL)
21938 			ill_refrele(ill_v6);
21939 		return (EBUSY);
21940 	}
21941 
21942 	/*
21943 	 * The new index is unused. Set it in the phyint.
21944 	 * Locate the other ill so that we can send a routing
21945 	 * sockets message.
21946 	 */
21947 	if (ill->ill_isv6) {
21948 		ill_other = phyi->phyint_illv4;
21949 	} else {
21950 		ill_other = phyi->phyint_illv6;
21951 	}
21952 
21953 	phyi->phyint_ifindex = index;
21954 
21955 	/* Update SCTP's ILL list */
21956 	sctp_ill_reindex(ill, old_index);
21957 
21958 	connc.cc_old_ifindex = old_index;
21959 	connc.cc_new_ifindex = index;
21960 	ip_change_ifindex(ill, &connc);
21961 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21962 
21963 	/* Send the routing sockets message */
21964 	ip_rts_ifmsg(ipif);
21965 	if (ill_other != NULL)
21966 		ip_rts_ifmsg(ill_other->ill_ipif);
21967 
21968 	return (0);
21969 }
21970 
21971 /* ARGSUSED */
21972 int
21973 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21974     ip_ioctl_cmd_t *ipip, void *ifreq)
21975 {
21976 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21977 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21978 
21979 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21980 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21981 	/* Get the interface index */
21982 	if (ipip->ipi_cmd_type == IF_CMD) {
21983 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21984 	} else {
21985 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21986 	}
21987 	return (0);
21988 }
21989 
21990 /* ARGSUSED */
21991 int
21992 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21993     ip_ioctl_cmd_t *ipip, void *ifreq)
21994 {
21995 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21996 
21997 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21998 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21999 	/* Get the interface zone */
22000 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22001 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22002 	return (0);
22003 }
22004 
22005 /*
22006  * Set the zoneid of an interface.
22007  */
22008 /* ARGSUSED */
22009 int
22010 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22011     ip_ioctl_cmd_t *ipip, void *ifreq)
22012 {
22013 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22014 	int err = 0;
22015 	boolean_t need_up = B_FALSE;
22016 	zone_t *zptr;
22017 	zone_status_t status;
22018 	zoneid_t zoneid;
22019 
22020 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22021 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22022 		if (!is_system_labeled())
22023 			return (ENOTSUP);
22024 		zoneid = GLOBAL_ZONEID;
22025 	}
22026 
22027 	/* cannot assign instance zero to a non-global zone */
22028 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22029 		return (ENOTSUP);
22030 
22031 	/*
22032 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22033 	 * the event of a race with the zone shutdown processing, since IP
22034 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22035 	 * interface will be cleaned up even if the zone is shut down
22036 	 * immediately after the status check. If the interface can't be brought
22037 	 * down right away, and the zone is shut down before the restart
22038 	 * function is called, we resolve the possible races by rechecking the
22039 	 * zone status in the restart function.
22040 	 */
22041 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22042 		return (EINVAL);
22043 	status = zone_status_get(zptr);
22044 	zone_rele(zptr);
22045 
22046 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22047 		return (EINVAL);
22048 
22049 	if (ipif->ipif_flags & IPIF_UP) {
22050 		/*
22051 		 * If the interface is already marked up,
22052 		 * we call ipif_down which will take care
22053 		 * of ditching any IREs that have been set
22054 		 * up based on the old interface address.
22055 		 */
22056 		err = ipif_logical_down(ipif, q, mp);
22057 		if (err == EINPROGRESS)
22058 			return (err);
22059 		ipif_down_tail(ipif);
22060 		need_up = B_TRUE;
22061 	}
22062 
22063 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22064 	return (err);
22065 }
22066 
22067 static int
22068 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22069     queue_t *q, mblk_t *mp, boolean_t need_up)
22070 {
22071 	int	err = 0;
22072 	ip_stack_t	*ipst;
22073 
22074 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22075 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22076 
22077 	if (CONN_Q(q))
22078 		ipst = CONNQ_TO_IPST(q);
22079 	else
22080 		ipst = ILLQ_TO_IPST(q);
22081 
22082 	/*
22083 	 * For exclusive stacks we don't allow a different zoneid than
22084 	 * global.
22085 	 */
22086 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22087 	    zoneid != GLOBAL_ZONEID)
22088 		return (EINVAL);
22089 
22090 	/* Set the new zone id. */
22091 	ipif->ipif_zoneid = zoneid;
22092 
22093 	/* Update sctp list */
22094 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22095 
22096 	if (need_up) {
22097 		/*
22098 		 * Now bring the interface back up.  If this
22099 		 * is the only IPIF for the ILL, ipif_up
22100 		 * will have to re-bind to the device, so
22101 		 * we may get back EINPROGRESS, in which
22102 		 * case, this IOCTL will get completed in
22103 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22104 		 */
22105 		err = ipif_up(ipif, q, mp);
22106 	}
22107 	return (err);
22108 }
22109 
22110 /* ARGSUSED */
22111 int
22112 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22113     ip_ioctl_cmd_t *ipip, void *if_req)
22114 {
22115 	struct lifreq *lifr = (struct lifreq *)if_req;
22116 	zoneid_t zoneid;
22117 	zone_t *zptr;
22118 	zone_status_t status;
22119 
22120 	ASSERT(ipif->ipif_id != 0);
22121 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22122 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22123 		zoneid = GLOBAL_ZONEID;
22124 
22125 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22126 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22127 
22128 	/*
22129 	 * We recheck the zone status to resolve the following race condition:
22130 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22131 	 * 2) hme0:1 is up and can't be brought down right away;
22132 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22133 	 * 3) zone "myzone" is halted; the zone status switches to
22134 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22135 	 * the interfaces to remove - hme0:1 is not returned because it's not
22136 	 * yet in "myzone", so it won't be removed;
22137 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22138 	 * status check here, we would have hme0:1 in "myzone" after it's been
22139 	 * destroyed.
22140 	 * Note that if the status check fails, we need to bring the interface
22141 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22142 	 * ipif_up_done[_v6]().
22143 	 */
22144 	status = ZONE_IS_UNINITIALIZED;
22145 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22146 		status = zone_status_get(zptr);
22147 		zone_rele(zptr);
22148 	}
22149 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22150 		if (ipif->ipif_isv6) {
22151 			(void) ipif_up_done_v6(ipif);
22152 		} else {
22153 			(void) ipif_up_done(ipif);
22154 		}
22155 		return (EINVAL);
22156 	}
22157 
22158 	ipif_down_tail(ipif);
22159 
22160 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22161 	    B_TRUE));
22162 }
22163 
22164 /* ARGSUSED */
22165 int
22166 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22167 	ip_ioctl_cmd_t *ipip, void *ifreq)
22168 {
22169 	struct lifreq	*lifr = ifreq;
22170 
22171 	ASSERT(q->q_next == NULL);
22172 	ASSERT(CONN_Q(q));
22173 
22174 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22175 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22176 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22177 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22178 
22179 	return (0);
22180 }
22181 
22182 /* Find the previous ILL in this usesrc group */
22183 static ill_t *
22184 ill_prev_usesrc(ill_t *uill)
22185 {
22186 	ill_t *ill;
22187 
22188 	for (ill = uill->ill_usesrc_grp_next;
22189 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22190 	    ill = ill->ill_usesrc_grp_next)
22191 		/* do nothing */;
22192 	return (ill);
22193 }
22194 
22195 /*
22196  * Release all members of the usesrc group. This routine is called
22197  * from ill_delete when the interface being unplumbed is the
22198  * group head.
22199  */
22200 static void
22201 ill_disband_usesrc_group(ill_t *uill)
22202 {
22203 	ill_t *next_ill, *tmp_ill;
22204 	ip_stack_t	*ipst = uill->ill_ipst;
22205 
22206 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22207 	next_ill = uill->ill_usesrc_grp_next;
22208 
22209 	do {
22210 		ASSERT(next_ill != NULL);
22211 		tmp_ill = next_ill->ill_usesrc_grp_next;
22212 		ASSERT(tmp_ill != NULL);
22213 		next_ill->ill_usesrc_grp_next = NULL;
22214 		next_ill->ill_usesrc_ifindex = 0;
22215 		next_ill = tmp_ill;
22216 	} while (next_ill->ill_usesrc_ifindex != 0);
22217 	uill->ill_usesrc_grp_next = NULL;
22218 }
22219 
22220 /*
22221  * Remove the client usesrc ILL from the list and relink to a new list
22222  */
22223 int
22224 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22225 {
22226 	ill_t *ill, *tmp_ill;
22227 	ip_stack_t	*ipst = ucill->ill_ipst;
22228 
22229 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22230 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22231 
22232 	/*
22233 	 * Check if the usesrc client ILL passed in is not already
22234 	 * in use as a usesrc ILL i.e one whose source address is
22235 	 * in use OR a usesrc ILL is not already in use as a usesrc
22236 	 * client ILL
22237 	 */
22238 	if ((ucill->ill_usesrc_ifindex == 0) ||
22239 	    (uill->ill_usesrc_ifindex != 0)) {
22240 		return (-1);
22241 	}
22242 
22243 	ill = ill_prev_usesrc(ucill);
22244 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22245 
22246 	/* Remove from the current list */
22247 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22248 		/* Only two elements in the list */
22249 		ASSERT(ill->ill_usesrc_ifindex == 0);
22250 		ill->ill_usesrc_grp_next = NULL;
22251 	} else {
22252 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22253 	}
22254 
22255 	if (ifindex == 0) {
22256 		ucill->ill_usesrc_ifindex = 0;
22257 		ucill->ill_usesrc_grp_next = NULL;
22258 		return (0);
22259 	}
22260 
22261 	ucill->ill_usesrc_ifindex = ifindex;
22262 	tmp_ill = uill->ill_usesrc_grp_next;
22263 	uill->ill_usesrc_grp_next = ucill;
22264 	ucill->ill_usesrc_grp_next =
22265 	    (tmp_ill != NULL) ? tmp_ill : uill;
22266 	return (0);
22267 }
22268 
22269 /*
22270  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22271  * ip.c for locking details.
22272  */
22273 /* ARGSUSED */
22274 int
22275 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22276     ip_ioctl_cmd_t *ipip, void *ifreq)
22277 {
22278 	struct lifreq *lifr = (struct lifreq *)ifreq;
22279 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22280 	    ill_flag_changed = B_FALSE;
22281 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22282 	int err = 0, ret;
22283 	uint_t ifindex;
22284 	phyint_t *us_phyint, *us_cli_phyint;
22285 	ipsq_t *ipsq = NULL;
22286 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22287 
22288 	ASSERT(IAM_WRITER_IPIF(ipif));
22289 	ASSERT(q->q_next == NULL);
22290 	ASSERT(CONN_Q(q));
22291 
22292 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22293 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22294 
22295 	ASSERT(us_cli_phyint != NULL);
22296 
22297 	/*
22298 	 * If the client ILL is being used for IPMP, abort.
22299 	 * Note, this can be done before ipsq_try_enter since we are already
22300 	 * exclusive on this ILL
22301 	 */
22302 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22303 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22304 		return (EINVAL);
22305 	}
22306 
22307 	ifindex = lifr->lifr_index;
22308 	if (ifindex == 0) {
22309 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22310 			/* non usesrc group interface, nothing to reset */
22311 			return (0);
22312 		}
22313 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22314 		/* valid reset request */
22315 		reset_flg = B_TRUE;
22316 	}
22317 
22318 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22319 	    ip_process_ioctl, &err, ipst);
22320 
22321 	if (usesrc_ill == NULL) {
22322 		return (err);
22323 	}
22324 
22325 	/*
22326 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22327 	 * group nor can either of the interfaces be used for standy. So
22328 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22329 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22330 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22331 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22332 	 * the usesrc_cli_ill
22333 	 */
22334 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22335 	    NEW_OP, B_TRUE);
22336 	if (ipsq == NULL) {
22337 		err = EINPROGRESS;
22338 		/* Operation enqueued on the ipsq of the usesrc ILL */
22339 		goto done;
22340 	}
22341 
22342 	/* Check if the usesrc_ill is used for IPMP */
22343 	us_phyint = usesrc_ill->ill_phyint;
22344 	if ((us_phyint->phyint_groupname != NULL) ||
22345 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22346 		err = EINVAL;
22347 		goto done;
22348 	}
22349 
22350 	/*
22351 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22352 	 * already a client then return EINVAL
22353 	 */
22354 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22355 		err = EINVAL;
22356 		goto done;
22357 	}
22358 
22359 	/*
22360 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22361 	 * be then this is a duplicate operation.
22362 	 */
22363 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22364 		err = 0;
22365 		goto done;
22366 	}
22367 
22368 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22369 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22370 	    usesrc_ill->ill_isv6));
22371 
22372 	/*
22373 	 * The next step ensures that no new ires will be created referencing
22374 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22375 	 * we go through an ire walk deleting all ire caches that reference
22376 	 * the client ill. New ires referencing the client ill that are added
22377 	 * to the ire table before the ILL_CHANGING flag is set, will be
22378 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22379 	 * the client ill while the ILL_CHANGING flag is set will be failed
22380 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22381 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22382 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22383 	 * belong to the same usesrc group.
22384 	 */
22385 	mutex_enter(&usesrc_cli_ill->ill_lock);
22386 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22387 	mutex_exit(&usesrc_cli_ill->ill_lock);
22388 	ill_flag_changed = B_TRUE;
22389 
22390 	if (ipif->ipif_isv6)
22391 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22392 		    ALL_ZONES, ipst);
22393 	else
22394 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22395 		    ALL_ZONES, ipst);
22396 
22397 	/*
22398 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22399 	 * and the ill_usesrc_ifindex fields
22400 	 */
22401 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22402 
22403 	if (reset_flg) {
22404 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22405 		if (ret != 0) {
22406 			err = EINVAL;
22407 		}
22408 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22409 		goto done;
22410 	}
22411 
22412 	/*
22413 	 * Four possibilities to consider:
22414 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22415 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22416 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22417 	 * 4. Both are part of their respective usesrc groups
22418 	 */
22419 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22420 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22421 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22422 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22423 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22424 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22425 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22426 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22427 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22428 		/* Insert at head of list */
22429 		usesrc_cli_ill->ill_usesrc_grp_next =
22430 		    usesrc_ill->ill_usesrc_grp_next;
22431 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22432 	} else {
22433 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22434 		    ifindex);
22435 		if (ret != 0)
22436 			err = EINVAL;
22437 	}
22438 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22439 
22440 done:
22441 	if (ill_flag_changed) {
22442 		mutex_enter(&usesrc_cli_ill->ill_lock);
22443 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22444 		mutex_exit(&usesrc_cli_ill->ill_lock);
22445 	}
22446 	if (ipsq != NULL)
22447 		ipsq_exit(ipsq);
22448 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22449 	ill_refrele(usesrc_ill);
22450 	return (err);
22451 }
22452 
22453 /*
22454  * comparison function used by avl.
22455  */
22456 static int
22457 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22458 {
22459 
22460 	uint_t index;
22461 
22462 	ASSERT(phyip != NULL && index_ptr != NULL);
22463 
22464 	index = *((uint_t *)index_ptr);
22465 	/*
22466 	 * let the phyint with the lowest index be on top.
22467 	 */
22468 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22469 		return (1);
22470 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22471 		return (-1);
22472 	return (0);
22473 }
22474 
22475 /*
22476  * comparison function used by avl.
22477  */
22478 static int
22479 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22480 {
22481 	ill_t *ill;
22482 	int res = 0;
22483 
22484 	ASSERT(phyip != NULL && name_ptr != NULL);
22485 
22486 	if (((phyint_t *)phyip)->phyint_illv4)
22487 		ill = ((phyint_t *)phyip)->phyint_illv4;
22488 	else
22489 		ill = ((phyint_t *)phyip)->phyint_illv6;
22490 	ASSERT(ill != NULL);
22491 
22492 	res = strcmp(ill->ill_name, (char *)name_ptr);
22493 	if (res > 0)
22494 		return (1);
22495 	else if (res < 0)
22496 		return (-1);
22497 	return (0);
22498 }
22499 /*
22500  * This function is called from ill_delete when the ill is being
22501  * unplumbed. We remove the reference from the phyint and we also
22502  * free the phyint when there are no more references to it.
22503  */
22504 static void
22505 ill_phyint_free(ill_t *ill)
22506 {
22507 	phyint_t *phyi;
22508 	phyint_t *next_phyint;
22509 	ipsq_t *cur_ipsq;
22510 	ip_stack_t	*ipst = ill->ill_ipst;
22511 
22512 	ASSERT(ill->ill_phyint != NULL);
22513 
22514 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22515 	phyi = ill->ill_phyint;
22516 	ill->ill_phyint = NULL;
22517 	/*
22518 	 * ill_init allocates a phyint always to store the copy
22519 	 * of flags relevant to phyint. At that point in time, we could
22520 	 * not assign the name and hence phyint_illv4/v6 could not be
22521 	 * initialized. Later in ipif_set_values, we assign the name to
22522 	 * the ill, at which point in time we assign phyint_illv4/v6.
22523 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22524 	 */
22525 	if (ill->ill_flags & ILLF_IPV6) {
22526 		phyi->phyint_illv6 = NULL;
22527 	} else {
22528 		phyi->phyint_illv4 = NULL;
22529 	}
22530 	/*
22531 	 * ipif_down removes it from the group when the last ipif goes
22532 	 * down.
22533 	 */
22534 	ASSERT(ill->ill_group == NULL);
22535 
22536 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22537 		return;
22538 
22539 	/*
22540 	 * Make sure this phyint was put in the list.
22541 	 */
22542 	if (phyi->phyint_ifindex > 0) {
22543 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22544 		    phyi);
22545 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22546 		    phyi);
22547 	}
22548 	/*
22549 	 * remove phyint from the ipsq list.
22550 	 */
22551 	cur_ipsq = phyi->phyint_ipsq;
22552 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22553 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22554 	} else {
22555 		next_phyint = cur_ipsq->ipsq_phyint_list;
22556 		while (next_phyint != NULL) {
22557 			if (next_phyint->phyint_ipsq_next == phyi) {
22558 				next_phyint->phyint_ipsq_next =
22559 				    phyi->phyint_ipsq_next;
22560 				break;
22561 			}
22562 			next_phyint = next_phyint->phyint_ipsq_next;
22563 		}
22564 		ASSERT(next_phyint != NULL);
22565 	}
22566 	IPSQ_DEC_REF(cur_ipsq, ipst);
22567 
22568 	if (phyi->phyint_groupname_len != 0) {
22569 		ASSERT(phyi->phyint_groupname != NULL);
22570 		mi_free(phyi->phyint_groupname);
22571 	}
22572 	mi_free(phyi);
22573 }
22574 
22575 /*
22576  * Attach the ill to the phyint structure which can be shared by both
22577  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22578  * function is called from ipif_set_values and ill_lookup_on_name (for
22579  * loopback) where we know the name of the ill. We lookup the ill and if
22580  * there is one present already with the name use that phyint. Otherwise
22581  * reuse the one allocated by ill_init.
22582  */
22583 static void
22584 ill_phyint_reinit(ill_t *ill)
22585 {
22586 	boolean_t isv6 = ill->ill_isv6;
22587 	phyint_t *phyi_old;
22588 	phyint_t *phyi;
22589 	avl_index_t where = 0;
22590 	ill_t	*ill_other = NULL;
22591 	ipsq_t	*ipsq;
22592 	ip_stack_t	*ipst = ill->ill_ipst;
22593 
22594 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22595 
22596 	phyi_old = ill->ill_phyint;
22597 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22598 	    phyi_old->phyint_illv6 == NULL));
22599 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22600 	    phyi_old->phyint_illv4 == NULL));
22601 	ASSERT(phyi_old->phyint_ifindex == 0);
22602 
22603 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22604 	    ill->ill_name, &where);
22605 
22606 	/*
22607 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22608 	 *    the global list of ills. So no other thread could have located
22609 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22610 	 * 2. Now locate the other protocol instance of this ill.
22611 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22612 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22613 	 *    of neither ill can change.
22614 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22615 	 *    other ill.
22616 	 * 5. Release all locks.
22617 	 */
22618 
22619 	/*
22620 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22621 	 * we are initializing IPv4.
22622 	 */
22623 	if (phyi != NULL) {
22624 		ill_other = (isv6) ? phyi->phyint_illv4 :
22625 		    phyi->phyint_illv6;
22626 		ASSERT(ill_other->ill_phyint != NULL);
22627 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22628 		    (!isv6 && ill_other->ill_isv6));
22629 		GRAB_ILL_LOCKS(ill, ill_other);
22630 		/*
22631 		 * We are potentially throwing away phyint_flags which
22632 		 * could be different from the one that we obtain from
22633 		 * ill_other->ill_phyint. But it is okay as we are assuming
22634 		 * that the state maintained within IP is correct.
22635 		 */
22636 		mutex_enter(&phyi->phyint_lock);
22637 		if (isv6) {
22638 			ASSERT(phyi->phyint_illv6 == NULL);
22639 			phyi->phyint_illv6 = ill;
22640 		} else {
22641 			ASSERT(phyi->phyint_illv4 == NULL);
22642 			phyi->phyint_illv4 = ill;
22643 		}
22644 		/*
22645 		 * This is a new ill, currently undergoing SLIFNAME
22646 		 * So we could not have joined an IPMP group until now.
22647 		 */
22648 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22649 		    phyi_old->phyint_groupname == NULL);
22650 
22651 		/*
22652 		 * This phyi_old is going away. Decref ipsq_refs and
22653 		 * assert it is zero. The ipsq itself will be freed in
22654 		 * ipsq_exit
22655 		 */
22656 		ipsq = phyi_old->phyint_ipsq;
22657 		IPSQ_DEC_REF(ipsq, ipst);
22658 		ASSERT(ipsq->ipsq_refs == 0);
22659 		/* Get the singleton phyint out of the ipsq list */
22660 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22661 		ipsq->ipsq_phyint_list = NULL;
22662 		phyi_old->phyint_illv4 = NULL;
22663 		phyi_old->phyint_illv6 = NULL;
22664 		mi_free(phyi_old);
22665 	} else {
22666 		mutex_enter(&ill->ill_lock);
22667 		/*
22668 		 * We don't need to acquire any lock, since
22669 		 * the ill is not yet visible globally  and we
22670 		 * have not yet released the ill_g_lock.
22671 		 */
22672 		phyi = phyi_old;
22673 		mutex_enter(&phyi->phyint_lock);
22674 		/* XXX We need a recovery strategy here. */
22675 		if (!phyint_assign_ifindex(phyi, ipst))
22676 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22677 
22678 		/* No IPMP group yet, thus the hook uses the ifindex */
22679 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22680 
22681 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22682 		    (void *)phyi, where);
22683 
22684 		(void) avl_find(&ipst->ips_phyint_g_list->
22685 		    phyint_list_avl_by_index,
22686 		    &phyi->phyint_ifindex, &where);
22687 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22688 		    (void *)phyi, where);
22689 	}
22690 
22691 	/*
22692 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22693 	 * pending mp is not affected because that is per ill basis.
22694 	 */
22695 	ill->ill_phyint = phyi;
22696 
22697 	/*
22698 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22699 	 * We do this here as when the first ipif was allocated,
22700 	 * ipif_allocate does not know the right interface index.
22701 	 */
22702 
22703 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22704 	/*
22705 	 * Now that the phyint's ifindex has been assigned, complete the
22706 	 * remaining
22707 	 */
22708 
22709 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22710 	if (ill->ill_isv6) {
22711 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22712 		    ill->ill_phyint->phyint_ifindex;
22713 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22714 	} else {
22715 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22716 	}
22717 
22718 	/*
22719 	 * Generate an event within the hooks framework to indicate that
22720 	 * a new interface has just been added to IP.  For this event to
22721 	 * be generated, the network interface must, at least, have an
22722 	 * ifindex assigned to it.
22723 	 *
22724 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22725 	 * that the ordering of delivered events to listeners matches the
22726 	 * order of them in the kernel.
22727 	 *
22728 	 * This function could be called from ill_lookup_on_name. In that case
22729 	 * the interface is loopback "lo", which will not generate a NIC event.
22730 	 */
22731 	if (ill->ill_name_length <= 2 ||
22732 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22733 		/*
22734 		 * Generate nic plumb event for ill_name even if
22735 		 * ipmp_hook_emulation is set. That avoids generating events
22736 		 * for the ill_names should ipmp_hook_emulation be turned on
22737 		 * later.
22738 		 */
22739 		ill_nic_event_plumb(ill, B_FALSE);
22740 	}
22741 	RELEASE_ILL_LOCKS(ill, ill_other);
22742 	mutex_exit(&phyi->phyint_lock);
22743 }
22744 
22745 /*
22746  * Allocate a NE_PLUMB nic info event and store in the ill.
22747  * If 'group' is set we do it for the group name, otherwise the ill name.
22748  * It will be sent when we leave the ipsq.
22749  */
22750 void
22751 ill_nic_event_plumb(ill_t *ill, boolean_t group)
22752 {
22753 	phyint_t	*phyi = ill->ill_phyint;
22754 	char		*name;
22755 	int		namelen;
22756 
22757 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22758 
22759 	if (group) {
22760 		ASSERT(phyi->phyint_groupname_len != 0);
22761 		namelen = phyi->phyint_groupname_len;
22762 		name = phyi->phyint_groupname;
22763 	} else {
22764 		namelen = ill->ill_name_length;
22765 		name = ill->ill_name;
22766 	}
22767 
22768 	ill_nic_event_dispatch(ill, 0, NE_PLUMB, name, namelen);
22769 }
22770 
22771 /*
22772  * Notify any downstream modules of the name of this interface.
22773  * An M_IOCTL is used even though we don't expect a successful reply.
22774  * Any reply message from the driver (presumably an M_IOCNAK) will
22775  * eventually get discarded somewhere upstream.  The message format is
22776  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22777  * to IP.
22778  */
22779 static void
22780 ip_ifname_notify(ill_t *ill, queue_t *q)
22781 {
22782 	mblk_t *mp1, *mp2;
22783 	struct iocblk *iocp;
22784 	struct lifreq *lifr;
22785 
22786 	mp1 = mkiocb(SIOCSLIFNAME);
22787 	if (mp1 == NULL)
22788 		return;
22789 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22790 	if (mp2 == NULL) {
22791 		freeb(mp1);
22792 		return;
22793 	}
22794 
22795 	mp1->b_cont = mp2;
22796 	iocp = (struct iocblk *)mp1->b_rptr;
22797 	iocp->ioc_count = sizeof (struct lifreq);
22798 
22799 	lifr = (struct lifreq *)mp2->b_rptr;
22800 	mp2->b_wptr += sizeof (struct lifreq);
22801 	bzero(lifr, sizeof (struct lifreq));
22802 
22803 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22804 	lifr->lifr_ppa = ill->ill_ppa;
22805 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22806 
22807 	putnext(q, mp1);
22808 }
22809 
22810 static int
22811 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22812 {
22813 	int err;
22814 	ip_stack_t	*ipst = ill->ill_ipst;
22815 
22816 	/* Set the obsolete NDD per-interface forwarding name. */
22817 	err = ill_set_ndd_name(ill);
22818 	if (err != 0) {
22819 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22820 		    err);
22821 	}
22822 
22823 	/* Tell downstream modules where they are. */
22824 	ip_ifname_notify(ill, q);
22825 
22826 	/*
22827 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22828 	 * Error cases are ENOMEM ...
22829 	 */
22830 	err = ill_dl_phys(ill, ipif, mp, q);
22831 
22832 	/*
22833 	 * If there is no IRE expiration timer running, get one started.
22834 	 * igmp and mld timers will be triggered by the first multicast
22835 	 */
22836 	if (ipst->ips_ip_ire_expire_id == 0) {
22837 		/*
22838 		 * acquire the lock and check again.
22839 		 */
22840 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22841 		if (ipst->ips_ip_ire_expire_id == 0) {
22842 			ipst->ips_ip_ire_expire_id = timeout(
22843 			    ip_trash_timer_expire, ipst,
22844 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22845 		}
22846 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22847 	}
22848 
22849 	if (ill->ill_isv6) {
22850 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22851 		if (ipst->ips_mld_slowtimeout_id == 0) {
22852 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22853 			    (void *)ipst,
22854 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22855 		}
22856 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22857 	} else {
22858 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22859 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22860 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22861 			    (void *)ipst,
22862 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22863 		}
22864 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22865 	}
22866 
22867 	return (err);
22868 }
22869 
22870 /*
22871  * Common routine for ppa and ifname setting. Should be called exclusive.
22872  *
22873  * Returns EINPROGRESS when mp has been consumed by queueing it on
22874  * ill_pending_mp and the ioctl will complete in ip_rput.
22875  *
22876  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22877  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22878  * For SLIFNAME, we pass these values back to the userland.
22879  */
22880 static int
22881 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22882 {
22883 	ill_t	*ill;
22884 	ipif_t	*ipif;
22885 	ipsq_t	*ipsq;
22886 	char	*ppa_ptr;
22887 	char	*old_ptr;
22888 	char	old_char;
22889 	int	error;
22890 	ip_stack_t	*ipst;
22891 
22892 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22893 	ASSERT(q->q_next != NULL);
22894 	ASSERT(interf_name != NULL);
22895 
22896 	ill = (ill_t *)q->q_ptr;
22897 	ipst = ill->ill_ipst;
22898 
22899 	ASSERT(ill->ill_ipst != NULL);
22900 	ASSERT(ill->ill_name[0] == '\0');
22901 	ASSERT(IAM_WRITER_ILL(ill));
22902 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22903 	ASSERT(ill->ill_ppa == UINT_MAX);
22904 
22905 	/* The ppa is sent down by ifconfig or is chosen */
22906 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22907 		return (EINVAL);
22908 	}
22909 
22910 	/*
22911 	 * make sure ppa passed in is same as ppa in the name.
22912 	 * This check is not made when ppa == UINT_MAX in that case ppa
22913 	 * in the name could be anything. System will choose a ppa and
22914 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22915 	 */
22916 	if (*new_ppa_ptr != UINT_MAX) {
22917 		/* stoi changes the pointer */
22918 		old_ptr = ppa_ptr;
22919 		/*
22920 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22921 		 * (they don't have an externally visible ppa).  We assign one
22922 		 * here so that we can manage the interface.  Note that in
22923 		 * the past this value was always 0 for DLPI 1 drivers.
22924 		 */
22925 		if (*new_ppa_ptr == 0)
22926 			*new_ppa_ptr = stoi(&old_ptr);
22927 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22928 			return (EINVAL);
22929 	}
22930 	/*
22931 	 * terminate string before ppa
22932 	 * save char at that location.
22933 	 */
22934 	old_char = ppa_ptr[0];
22935 	ppa_ptr[0] = '\0';
22936 
22937 	ill->ill_ppa = *new_ppa_ptr;
22938 	/*
22939 	 * Finish as much work now as possible before calling ill_glist_insert
22940 	 * which makes the ill globally visible and also merges it with the
22941 	 * other protocol instance of this phyint. The remaining work is
22942 	 * done after entering the ipsq which may happen sometime later.
22943 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22944 	 */
22945 	ipif = ill->ill_ipif;
22946 
22947 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22948 	ipif_assign_seqid(ipif);
22949 
22950 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22951 		ill->ill_flags |= ILLF_IPV4;
22952 
22953 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22954 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22955 
22956 	if (ill->ill_flags & ILLF_IPV6) {
22957 
22958 		ill->ill_isv6 = B_TRUE;
22959 		if (ill->ill_rq != NULL) {
22960 			ill->ill_rq->q_qinfo = &iprinitv6;
22961 			ill->ill_wq->q_qinfo = &ipwinitv6;
22962 		}
22963 
22964 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22965 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22966 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22967 		ipif->ipif_v6subnet = ipv6_all_zeros;
22968 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22969 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22970 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22971 		/*
22972 		 * point-to-point or Non-mulicast capable
22973 		 * interfaces won't do NUD unless explicitly
22974 		 * configured to do so.
22975 		 */
22976 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22977 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22978 			ill->ill_flags |= ILLF_NONUD;
22979 		}
22980 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22981 		if (ill->ill_flags & ILLF_NOARP) {
22982 			/*
22983 			 * Note: xresolv interfaces will eventually need
22984 			 * NOARP set here as well, but that will require
22985 			 * those external resolvers to have some
22986 			 * knowledge of that flag and act appropriately.
22987 			 * Not to be changed at present.
22988 			 */
22989 			ill->ill_flags &= ~ILLF_NOARP;
22990 		}
22991 		/*
22992 		 * Set the ILLF_ROUTER flag according to the global
22993 		 * IPv6 forwarding policy.
22994 		 */
22995 		if (ipst->ips_ipv6_forward != 0)
22996 			ill->ill_flags |= ILLF_ROUTER;
22997 	} else if (ill->ill_flags & ILLF_IPV4) {
22998 		ill->ill_isv6 = B_FALSE;
22999 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23000 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23001 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23002 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23003 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23004 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23005 		/*
23006 		 * Set the ILLF_ROUTER flag according to the global
23007 		 * IPv4 forwarding policy.
23008 		 */
23009 		if (ipst->ips_ip_g_forward != 0)
23010 			ill->ill_flags |= ILLF_ROUTER;
23011 	}
23012 
23013 	ASSERT(ill->ill_phyint != NULL);
23014 
23015 	/*
23016 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23017 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23018 	 */
23019 	if (!ill_allocate_mibs(ill))
23020 		return (ENOMEM);
23021 
23022 	/*
23023 	 * Pick a default sap until we get the DL_INFO_ACK back from
23024 	 * the driver.
23025 	 */
23026 	if (ill->ill_sap == 0) {
23027 		if (ill->ill_isv6)
23028 			ill->ill_sap = IP6_DL_SAP;
23029 		else
23030 			ill->ill_sap = IP_DL_SAP;
23031 	}
23032 
23033 	ill->ill_ifname_pending = 1;
23034 	ill->ill_ifname_pending_err = 0;
23035 
23036 	/*
23037 	 * When the first ipif comes up in ipif_up_done(), multicast groups
23038 	 * that were joined while this ill was not bound to the DLPI link need
23039 	 * to be recovered by ill_recover_multicast().
23040 	 */
23041 	ill->ill_need_recover_multicast = 1;
23042 
23043 	ill_refhold(ill);
23044 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23045 	if ((error = ill_glist_insert(ill, interf_name,
23046 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23047 		ill->ill_ppa = UINT_MAX;
23048 		ill->ill_name[0] = '\0';
23049 		/*
23050 		 * undo null termination done above.
23051 		 */
23052 		ppa_ptr[0] = old_char;
23053 		rw_exit(&ipst->ips_ill_g_lock);
23054 		ill_refrele(ill);
23055 		return (error);
23056 	}
23057 
23058 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23059 
23060 	/*
23061 	 * When we return the buffer pointed to by interf_name should contain
23062 	 * the same name as in ill_name.
23063 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23064 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23065 	 * so copy full name and update the ppa ptr.
23066 	 * When ppa passed in != UINT_MAX all values are correct just undo
23067 	 * null termination, this saves a bcopy.
23068 	 */
23069 	if (*new_ppa_ptr == UINT_MAX) {
23070 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23071 		*new_ppa_ptr = ill->ill_ppa;
23072 	} else {
23073 		/*
23074 		 * undo null termination done above.
23075 		 */
23076 		ppa_ptr[0] = old_char;
23077 	}
23078 
23079 	/* Let SCTP know about this ILL */
23080 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23081 
23082 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23083 	    B_TRUE);
23084 
23085 	rw_exit(&ipst->ips_ill_g_lock);
23086 	ill_refrele(ill);
23087 	if (ipsq == NULL)
23088 		return (EINPROGRESS);
23089 
23090 	/*
23091 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23092 	 */
23093 	if (ipsq->ipsq_current_ipif == NULL)
23094 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23095 	else
23096 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23097 
23098 	error = ipif_set_values_tail(ill, ipif, mp, q);
23099 	ipsq_exit(ipsq);
23100 	if (error != 0 && error != EINPROGRESS) {
23101 		/*
23102 		 * restore previous values
23103 		 */
23104 		ill->ill_isv6 = B_FALSE;
23105 	}
23106 	return (error);
23107 }
23108 
23109 
23110 void
23111 ipif_init(ip_stack_t *ipst)
23112 {
23113 	hrtime_t hrt;
23114 	int i;
23115 
23116 	/*
23117 	 * Can't call drv_getparm here as it is too early in the boot.
23118 	 * As we use ipif_src_random just for picking a different
23119 	 * source address everytime, this need not be really random.
23120 	 */
23121 	hrt = gethrtime();
23122 	ipst->ips_ipif_src_random =
23123 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23124 
23125 	for (i = 0; i < MAX_G_HEADS; i++) {
23126 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23127 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23128 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23129 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23130 	}
23131 
23132 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23133 	    ill_phyint_compare_index,
23134 	    sizeof (phyint_t),
23135 	    offsetof(struct phyint, phyint_avl_by_index));
23136 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23137 	    ill_phyint_compare_name,
23138 	    sizeof (phyint_t),
23139 	    offsetof(struct phyint, phyint_avl_by_name));
23140 }
23141 
23142 /*
23143  * Lookup the ipif corresponding to the onlink destination address. For
23144  * point-to-point interfaces, it matches with remote endpoint destination
23145  * address. For point-to-multipoint interfaces it only tries to match the
23146  * destination with the interface's subnet address. The longest, most specific
23147  * match is found to take care of such rare network configurations like -
23148  * le0: 129.146.1.1/16
23149  * le1: 129.146.2.2/24
23150  * It is used only by SO_DONTROUTE at the moment.
23151  */
23152 ipif_t *
23153 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23154 {
23155 	ipif_t	*ipif, *best_ipif;
23156 	ill_t	*ill;
23157 	ill_walk_context_t ctx;
23158 
23159 	ASSERT(zoneid != ALL_ZONES);
23160 	best_ipif = NULL;
23161 
23162 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23163 	ill = ILL_START_WALK_V4(&ctx, ipst);
23164 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23165 		mutex_enter(&ill->ill_lock);
23166 		for (ipif = ill->ill_ipif; ipif != NULL;
23167 		    ipif = ipif->ipif_next) {
23168 			if (!IPIF_CAN_LOOKUP(ipif))
23169 				continue;
23170 			if (ipif->ipif_zoneid != zoneid &&
23171 			    ipif->ipif_zoneid != ALL_ZONES)
23172 				continue;
23173 			/*
23174 			 * Point-to-point case. Look for exact match with
23175 			 * destination address.
23176 			 */
23177 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23178 				if (ipif->ipif_pp_dst_addr == addr) {
23179 					ipif_refhold_locked(ipif);
23180 					mutex_exit(&ill->ill_lock);
23181 					rw_exit(&ipst->ips_ill_g_lock);
23182 					if (best_ipif != NULL)
23183 						ipif_refrele(best_ipif);
23184 					return (ipif);
23185 				}
23186 			} else if (ipif->ipif_subnet == (addr &
23187 			    ipif->ipif_net_mask)) {
23188 				/*
23189 				 * Point-to-multipoint case. Looping through to
23190 				 * find the most specific match. If there are
23191 				 * multiple best match ipif's then prefer ipif's
23192 				 * that are UP. If there is only one best match
23193 				 * ipif and it is DOWN we must still return it.
23194 				 */
23195 				if ((best_ipif == NULL) ||
23196 				    (ipif->ipif_net_mask >
23197 				    best_ipif->ipif_net_mask) ||
23198 				    ((ipif->ipif_net_mask ==
23199 				    best_ipif->ipif_net_mask) &&
23200 				    ((ipif->ipif_flags & IPIF_UP) &&
23201 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23202 					ipif_refhold_locked(ipif);
23203 					mutex_exit(&ill->ill_lock);
23204 					rw_exit(&ipst->ips_ill_g_lock);
23205 					if (best_ipif != NULL)
23206 						ipif_refrele(best_ipif);
23207 					best_ipif = ipif;
23208 					rw_enter(&ipst->ips_ill_g_lock,
23209 					    RW_READER);
23210 					mutex_enter(&ill->ill_lock);
23211 				}
23212 			}
23213 		}
23214 		mutex_exit(&ill->ill_lock);
23215 	}
23216 	rw_exit(&ipst->ips_ill_g_lock);
23217 	return (best_ipif);
23218 }
23219 
23220 /*
23221  * Save enough information so that we can recreate the IRE if
23222  * the interface goes down and then up.
23223  */
23224 static void
23225 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23226 {
23227 	mblk_t	*save_mp;
23228 
23229 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23230 	if (save_mp != NULL) {
23231 		ifrt_t	*ifrt;
23232 
23233 		save_mp->b_wptr += sizeof (ifrt_t);
23234 		ifrt = (ifrt_t *)save_mp->b_rptr;
23235 		bzero(ifrt, sizeof (ifrt_t));
23236 		ifrt->ifrt_type = ire->ire_type;
23237 		ifrt->ifrt_addr = ire->ire_addr;
23238 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23239 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23240 		ifrt->ifrt_mask = ire->ire_mask;
23241 		ifrt->ifrt_flags = ire->ire_flags;
23242 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23243 		mutex_enter(&ipif->ipif_saved_ire_lock);
23244 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23245 		ipif->ipif_saved_ire_mp = save_mp;
23246 		ipif->ipif_saved_ire_cnt++;
23247 		mutex_exit(&ipif->ipif_saved_ire_lock);
23248 	}
23249 }
23250 
23251 static void
23252 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23253 {
23254 	mblk_t	**mpp;
23255 	mblk_t	*mp;
23256 	ifrt_t	*ifrt;
23257 
23258 	/* Remove from ipif_saved_ire_mp list if it is there */
23259 	mutex_enter(&ipif->ipif_saved_ire_lock);
23260 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23261 	    mpp = &(*mpp)->b_cont) {
23262 		/*
23263 		 * On a given ipif, the triple of address, gateway and
23264 		 * mask is unique for each saved IRE (in the case of
23265 		 * ordinary interface routes, the gateway address is
23266 		 * all-zeroes).
23267 		 */
23268 		mp = *mpp;
23269 		ifrt = (ifrt_t *)mp->b_rptr;
23270 		if (ifrt->ifrt_addr == ire->ire_addr &&
23271 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23272 		    ifrt->ifrt_mask == ire->ire_mask) {
23273 			*mpp = mp->b_cont;
23274 			ipif->ipif_saved_ire_cnt--;
23275 			freeb(mp);
23276 			break;
23277 		}
23278 	}
23279 	mutex_exit(&ipif->ipif_saved_ire_lock);
23280 }
23281 
23282 /*
23283  * IP multirouting broadcast routes handling
23284  * Append CGTP broadcast IREs to regular ones created
23285  * at ifconfig time.
23286  */
23287 static void
23288 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23289 {
23290 	ire_t *ire_prim;
23291 
23292 	ASSERT(ire != NULL);
23293 	ASSERT(ire_dst != NULL);
23294 
23295 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23296 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23297 	if (ire_prim != NULL) {
23298 		/*
23299 		 * We are in the special case of broadcasts for
23300 		 * CGTP. We add an IRE_BROADCAST that holds
23301 		 * the RTF_MULTIRT flag, the destination
23302 		 * address of ire_dst and the low level
23303 		 * info of ire_prim. In other words, CGTP
23304 		 * broadcast is added to the redundant ipif.
23305 		 */
23306 		ipif_t *ipif_prim;
23307 		ire_t  *bcast_ire;
23308 
23309 		ipif_prim = ire_prim->ire_ipif;
23310 
23311 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23312 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23313 		    (void *)ire_dst, (void *)ire_prim,
23314 		    (void *)ipif_prim));
23315 
23316 		bcast_ire = ire_create(
23317 		    (uchar_t *)&ire->ire_addr,
23318 		    (uchar_t *)&ip_g_all_ones,
23319 		    (uchar_t *)&ire_dst->ire_src_addr,
23320 		    (uchar_t *)&ire->ire_gateway_addr,
23321 		    &ipif_prim->ipif_mtu,
23322 		    NULL,
23323 		    ipif_prim->ipif_rq,
23324 		    ipif_prim->ipif_wq,
23325 		    IRE_BROADCAST,
23326 		    ipif_prim,
23327 		    0,
23328 		    0,
23329 		    0,
23330 		    ire->ire_flags,
23331 		    &ire_uinfo_null,
23332 		    NULL,
23333 		    NULL,
23334 		    ipst);
23335 
23336 		if (bcast_ire != NULL) {
23337 
23338 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23339 			    B_FALSE) == 0) {
23340 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23341 				    "added bcast_ire %p\n",
23342 				    (void *)bcast_ire));
23343 
23344 				ipif_save_ire(bcast_ire->ire_ipif,
23345 				    bcast_ire);
23346 				ire_refrele(bcast_ire);
23347 			}
23348 		}
23349 		ire_refrele(ire_prim);
23350 	}
23351 }
23352 
23353 
23354 /*
23355  * IP multirouting broadcast routes handling
23356  * Remove the broadcast ire
23357  */
23358 static void
23359 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23360 {
23361 	ire_t *ire_dst;
23362 
23363 	ASSERT(ire != NULL);
23364 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23365 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23366 	if (ire_dst != NULL) {
23367 		ire_t *ire_prim;
23368 
23369 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23370 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23371 		if (ire_prim != NULL) {
23372 			ipif_t *ipif_prim;
23373 			ire_t  *bcast_ire;
23374 
23375 			ipif_prim = ire_prim->ire_ipif;
23376 
23377 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23378 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23379 			    (void *)ire_dst, (void *)ire_prim,
23380 			    (void *)ipif_prim));
23381 
23382 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23383 			    ire->ire_gateway_addr,
23384 			    IRE_BROADCAST,
23385 			    ipif_prim, ALL_ZONES,
23386 			    NULL,
23387 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23388 			    MATCH_IRE_MASK, ipst);
23389 
23390 			if (bcast_ire != NULL) {
23391 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23392 				    "looked up bcast_ire %p\n",
23393 				    (void *)bcast_ire));
23394 				ipif_remove_ire(bcast_ire->ire_ipif,
23395 				    bcast_ire);
23396 				ire_delete(bcast_ire);
23397 				ire_refrele(bcast_ire);
23398 			}
23399 			ire_refrele(ire_prim);
23400 		}
23401 		ire_refrele(ire_dst);
23402 	}
23403 }
23404 
23405 /*
23406  * IPsec hardware acceleration capabilities related functions.
23407  */
23408 
23409 /*
23410  * Free a per-ill IPsec capabilities structure.
23411  */
23412 static void
23413 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23414 {
23415 	if (capab->auth_hw_algs != NULL)
23416 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23417 	if (capab->encr_hw_algs != NULL)
23418 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23419 	if (capab->encr_algparm != NULL)
23420 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23421 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23422 }
23423 
23424 /*
23425  * Allocate a new per-ill IPsec capabilities structure. This structure
23426  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23427  * an array which specifies, for each algorithm, whether this algorithm
23428  * is supported by the ill or not.
23429  */
23430 static ill_ipsec_capab_t *
23431 ill_ipsec_capab_alloc(void)
23432 {
23433 	ill_ipsec_capab_t *capab;
23434 	uint_t nelems;
23435 
23436 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23437 	if (capab == NULL)
23438 		return (NULL);
23439 
23440 	/* we need one bit per algorithm */
23441 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23442 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23443 
23444 	/* allocate memory to store algorithm flags */
23445 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23446 	if (capab->encr_hw_algs == NULL)
23447 		goto nomem;
23448 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23449 	if (capab->auth_hw_algs == NULL)
23450 		goto nomem;
23451 	/*
23452 	 * Leave encr_algparm NULL for now since we won't need it half
23453 	 * the time
23454 	 */
23455 	return (capab);
23456 
23457 nomem:
23458 	ill_ipsec_capab_free(capab);
23459 	return (NULL);
23460 }
23461 
23462 /*
23463  * Resize capability array.  Since we're exclusive, this is OK.
23464  */
23465 static boolean_t
23466 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23467 {
23468 	ipsec_capab_algparm_t *nalp, *oalp;
23469 	uint32_t olen, nlen;
23470 
23471 	oalp = capab->encr_algparm;
23472 	olen = capab->encr_algparm_size;
23473 
23474 	if (oalp != NULL) {
23475 		if (algid < capab->encr_algparm_end)
23476 			return (B_TRUE);
23477 	}
23478 
23479 	nlen = (algid + 1) * sizeof (*nalp);
23480 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23481 	if (nalp == NULL)
23482 		return (B_FALSE);
23483 
23484 	if (oalp != NULL) {
23485 		bcopy(oalp, nalp, olen);
23486 		kmem_free(oalp, olen);
23487 	}
23488 	capab->encr_algparm = nalp;
23489 	capab->encr_algparm_size = nlen;
23490 	capab->encr_algparm_end = algid + 1;
23491 
23492 	return (B_TRUE);
23493 }
23494 
23495 /*
23496  * Compare the capabilities of the specified ill with the protocol
23497  * and algorithms specified by the SA passed as argument.
23498  * If they match, returns B_TRUE, B_FALSE if they do not match.
23499  *
23500  * The ill can be passed as a pointer to it, or by specifying its index
23501  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23502  *
23503  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23504  * packet is eligible for hardware acceleration, and by
23505  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23506  * to a particular ill.
23507  */
23508 boolean_t
23509 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23510     ipsa_t *sa, netstack_t *ns)
23511 {
23512 	boolean_t sa_isv6;
23513 	uint_t algid;
23514 	struct ill_ipsec_capab_s *cpp;
23515 	boolean_t need_refrele = B_FALSE;
23516 	ip_stack_t	*ipst = ns->netstack_ip;
23517 
23518 	if (ill == NULL) {
23519 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23520 		    NULL, NULL, NULL, ipst);
23521 		if (ill == NULL) {
23522 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23523 			return (B_FALSE);
23524 		}
23525 		need_refrele = B_TRUE;
23526 	}
23527 
23528 	/*
23529 	 * Use the address length specified by the SA to determine
23530 	 * if it corresponds to a IPv6 address, and fail the matching
23531 	 * if the isv6 flag passed as argument does not match.
23532 	 * Note: this check is used for SADB capability checking before
23533 	 * sending SA information to an ill.
23534 	 */
23535 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23536 	if (sa_isv6 != ill_isv6)
23537 		/* protocol mismatch */
23538 		goto done;
23539 
23540 	/*
23541 	 * Check if the ill supports the protocol, algorithm(s) and
23542 	 * key size(s) specified by the SA, and get the pointers to
23543 	 * the algorithms supported by the ill.
23544 	 */
23545 	switch (sa->ipsa_type) {
23546 
23547 	case SADB_SATYPE_ESP:
23548 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23549 			/* ill does not support ESP acceleration */
23550 			goto done;
23551 		cpp = ill->ill_ipsec_capab_esp;
23552 		algid = sa->ipsa_auth_alg;
23553 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23554 			goto done;
23555 		algid = sa->ipsa_encr_alg;
23556 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23557 			goto done;
23558 		if (algid < cpp->encr_algparm_end) {
23559 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23560 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23561 				goto done;
23562 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23563 				goto done;
23564 		}
23565 		break;
23566 
23567 	case SADB_SATYPE_AH:
23568 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23569 			/* ill does not support AH acceleration */
23570 			goto done;
23571 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23572 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23573 			goto done;
23574 		break;
23575 	}
23576 
23577 	if (need_refrele)
23578 		ill_refrele(ill);
23579 	return (B_TRUE);
23580 done:
23581 	if (need_refrele)
23582 		ill_refrele(ill);
23583 	return (B_FALSE);
23584 }
23585 
23586 /*
23587  * Add a new ill to the list of IPsec capable ills.
23588  * Called from ill_capability_ipsec_ack() when an ACK was received
23589  * indicating that IPsec hardware processing was enabled for an ill.
23590  *
23591  * ill must point to the ill for which acceleration was enabled.
23592  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23593  */
23594 static void
23595 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23596 {
23597 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23598 	uint_t sa_type;
23599 	uint_t ipproto;
23600 	ip_stack_t	*ipst = ill->ill_ipst;
23601 
23602 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23603 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23604 
23605 	switch (dl_cap) {
23606 	case DL_CAPAB_IPSEC_AH:
23607 		sa_type = SADB_SATYPE_AH;
23608 		ills = &ipst->ips_ipsec_capab_ills_ah;
23609 		ipproto = IPPROTO_AH;
23610 		break;
23611 	case DL_CAPAB_IPSEC_ESP:
23612 		sa_type = SADB_SATYPE_ESP;
23613 		ills = &ipst->ips_ipsec_capab_ills_esp;
23614 		ipproto = IPPROTO_ESP;
23615 		break;
23616 	}
23617 
23618 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23619 
23620 	/*
23621 	 * Add ill index to list of hardware accelerators. If
23622 	 * already in list, do nothing.
23623 	 */
23624 	for (cur_ill = *ills; cur_ill != NULL &&
23625 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23626 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23627 		;
23628 
23629 	if (cur_ill == NULL) {
23630 		/* if this is a new entry for this ill */
23631 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23632 		if (new_ill == NULL) {
23633 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23634 			return;
23635 		}
23636 
23637 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23638 		new_ill->ill_isv6 = ill->ill_isv6;
23639 		new_ill->next = *ills;
23640 		*ills = new_ill;
23641 	} else if (!sadb_resync) {
23642 		/* not resync'ing SADB and an entry exists for this ill */
23643 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23644 		return;
23645 	}
23646 
23647 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23648 
23649 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23650 		/*
23651 		 * IPsec module for protocol loaded, initiate dump
23652 		 * of the SADB to this ill.
23653 		 */
23654 		sadb_ill_download(ill, sa_type);
23655 }
23656 
23657 /*
23658  * Remove an ill from the list of IPsec capable ills.
23659  */
23660 static void
23661 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23662 {
23663 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23664 	ip_stack_t	*ipst = ill->ill_ipst;
23665 
23666 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23667 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23668 
23669 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23670 	    &ipst->ips_ipsec_capab_ills_esp;
23671 
23672 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23673 
23674 	prev_ill = NULL;
23675 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23676 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23677 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23678 		;
23679 	if (cur_ill == NULL) {
23680 		/* entry not found */
23681 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23682 		return;
23683 	}
23684 	if (prev_ill == NULL) {
23685 		/* entry at front of list */
23686 		*ills = NULL;
23687 	} else {
23688 		prev_ill->next = cur_ill->next;
23689 	}
23690 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23691 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23692 }
23693 
23694 /*
23695  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23696  * supporting the specified IPsec protocol acceleration.
23697  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23698  * We free the mblk and, if sa is non-null, release the held referece.
23699  */
23700 void
23701 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23702     netstack_t *ns)
23703 {
23704 	ipsec_capab_ill_t *ici, *cur_ici;
23705 	ill_t *ill;
23706 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23707 	ip_stack_t	*ipst = ns->netstack_ip;
23708 
23709 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23710 	    ipst->ips_ipsec_capab_ills_esp;
23711 
23712 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23713 
23714 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23715 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23716 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23717 
23718 		/*
23719 		 * Handle the case where the ill goes away while the SADB is
23720 		 * attempting to send messages.  If it's going away, it's
23721 		 * nuking its shadow SADB, so we don't care..
23722 		 */
23723 
23724 		if (ill == NULL)
23725 			continue;
23726 
23727 		if (sa != NULL) {
23728 			/*
23729 			 * Make sure capabilities match before
23730 			 * sending SA to ill.
23731 			 */
23732 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23733 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23734 				ill_refrele(ill);
23735 				continue;
23736 			}
23737 
23738 			mutex_enter(&sa->ipsa_lock);
23739 			sa->ipsa_flags |= IPSA_F_HW;
23740 			mutex_exit(&sa->ipsa_lock);
23741 		}
23742 
23743 		/*
23744 		 * Copy template message, and add it to the front
23745 		 * of the mblk ship list. We want to avoid holding
23746 		 * the ipsec_capab_ills_lock while sending the
23747 		 * message to the ills.
23748 		 *
23749 		 * The b_next and b_prev are temporarily used
23750 		 * to build a list of mblks to be sent down, and to
23751 		 * save the ill to which they must be sent.
23752 		 */
23753 		nmp = copymsg(mp);
23754 		if (nmp == NULL) {
23755 			ill_refrele(ill);
23756 			continue;
23757 		}
23758 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23759 		nmp->b_next = mp_ship_list;
23760 		mp_ship_list = nmp;
23761 		nmp->b_prev = (mblk_t *)ill;
23762 	}
23763 
23764 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23765 
23766 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23767 		/* restore the mblk to a sane state */
23768 		next_mp = nmp->b_next;
23769 		nmp->b_next = NULL;
23770 		ill = (ill_t *)nmp->b_prev;
23771 		nmp->b_prev = NULL;
23772 
23773 		ill_dlpi_send(ill, nmp);
23774 		ill_refrele(ill);
23775 	}
23776 
23777 	if (sa != NULL)
23778 		IPSA_REFRELE(sa);
23779 	freemsg(mp);
23780 }
23781 
23782 /*
23783  * Derive an interface id from the link layer address.
23784  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23785  */
23786 static boolean_t
23787 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23788 {
23789 	char		*addr;
23790 
23791 	if (phys_length != ETHERADDRL)
23792 		return (B_FALSE);
23793 
23794 	/* Form EUI-64 like address */
23795 	addr = (char *)&v6addr->s6_addr32[2];
23796 	bcopy((char *)phys_addr, addr, 3);
23797 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23798 	addr[3] = (char)0xff;
23799 	addr[4] = (char)0xfe;
23800 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23801 	return (B_TRUE);
23802 }
23803 
23804 /* ARGSUSED */
23805 static boolean_t
23806 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23807 {
23808 	return (B_FALSE);
23809 }
23810 
23811 /* ARGSUSED */
23812 static boolean_t
23813 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23814     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23815 {
23816 	/*
23817 	 * Multicast address mappings used over Ethernet/802.X.
23818 	 * This address is used as a base for mappings.
23819 	 */
23820 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23821 	    0x00, 0x00, 0x00};
23822 
23823 	/*
23824 	 * Extract low order 32 bits from IPv6 multicast address.
23825 	 * Or that into the link layer address, starting from the
23826 	 * second byte.
23827 	 */
23828 	*hw_start = 2;
23829 	v6_extract_mask->s6_addr32[0] = 0;
23830 	v6_extract_mask->s6_addr32[1] = 0;
23831 	v6_extract_mask->s6_addr32[2] = 0;
23832 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23833 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23834 	return (B_TRUE);
23835 }
23836 
23837 /*
23838  * Indicate by return value whether multicast is supported. If not,
23839  * this code should not touch/change any parameters.
23840  */
23841 /* ARGSUSED */
23842 static boolean_t
23843 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23844     uint32_t *hw_start, ipaddr_t *extract_mask)
23845 {
23846 	/*
23847 	 * Multicast address mappings used over Ethernet/802.X.
23848 	 * This address is used as a base for mappings.
23849 	 */
23850 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23851 	    0x00, 0x00, 0x00 };
23852 
23853 	if (phys_length != ETHERADDRL)
23854 		return (B_FALSE);
23855 
23856 	*extract_mask = htonl(0x007fffff);
23857 	*hw_start = 2;
23858 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23859 	return (B_TRUE);
23860 }
23861 
23862 /*
23863  * Derive IPoIB interface id from the link layer address.
23864  */
23865 static boolean_t
23866 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23867 {
23868 	char		*addr;
23869 
23870 	if (phys_length != 20)
23871 		return (B_FALSE);
23872 	addr = (char *)&v6addr->s6_addr32[2];
23873 	bcopy(phys_addr + 12, addr, 8);
23874 	/*
23875 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23876 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23877 	 * rules. In these cases, the IBA considers these GUIDs to be in
23878 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23879 	 * required; vendors are required not to assign global EUI-64's
23880 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23881 	 * of the interface identifier. Whether the GUID is in modified
23882 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23883 	 * bit set to 1.
23884 	 */
23885 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23886 	return (B_TRUE);
23887 }
23888 
23889 /*
23890  * Note on mapping from multicast IP addresses to IPoIB multicast link
23891  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23892  * The format of an IPoIB multicast address is:
23893  *
23894  *  4 byte QPN      Scope Sign.  Pkey
23895  * +--------------------------------------------+
23896  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23897  * +--------------------------------------------+
23898  *
23899  * The Scope and Pkey components are properties of the IBA port and
23900  * network interface. They can be ascertained from the broadcast address.
23901  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23902  */
23903 
23904 static boolean_t
23905 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23906     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23907 {
23908 	/*
23909 	 * Base IPoIB IPv6 multicast address used for mappings.
23910 	 * Does not contain the IBA scope/Pkey values.
23911 	 */
23912 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23913 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23914 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23915 
23916 	/*
23917 	 * Extract low order 80 bits from IPv6 multicast address.
23918 	 * Or that into the link layer address, starting from the
23919 	 * sixth byte.
23920 	 */
23921 	*hw_start = 6;
23922 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23923 
23924 	/*
23925 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23926 	 */
23927 	*(maddr + 5) = *(bphys_addr + 5);
23928 	*(maddr + 8) = *(bphys_addr + 8);
23929 	*(maddr + 9) = *(bphys_addr + 9);
23930 
23931 	v6_extract_mask->s6_addr32[0] = 0;
23932 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23933 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23934 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23935 	return (B_TRUE);
23936 }
23937 
23938 static boolean_t
23939 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23940     uint32_t *hw_start, ipaddr_t *extract_mask)
23941 {
23942 	/*
23943 	 * Base IPoIB IPv4 multicast address used for mappings.
23944 	 * Does not contain the IBA scope/Pkey values.
23945 	 */
23946 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23947 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23948 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23949 
23950 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23951 		return (B_FALSE);
23952 
23953 	/*
23954 	 * Extract low order 28 bits from IPv4 multicast address.
23955 	 * Or that into the link layer address, starting from the
23956 	 * sixteenth byte.
23957 	 */
23958 	*extract_mask = htonl(0x0fffffff);
23959 	*hw_start = 16;
23960 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23961 
23962 	/*
23963 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23964 	 */
23965 	*(maddr + 5) = *(bphys_addr + 5);
23966 	*(maddr + 8) = *(bphys_addr + 8);
23967 	*(maddr + 9) = *(bphys_addr + 9);
23968 	return (B_TRUE);
23969 }
23970 
23971 /*
23972  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23973  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23974  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23975  * the link-local address is preferred.
23976  */
23977 boolean_t
23978 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23979 {
23980 	ipif_t	*ipif;
23981 	ipif_t	*maybe_ipif = NULL;
23982 
23983 	mutex_enter(&ill->ill_lock);
23984 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23985 		mutex_exit(&ill->ill_lock);
23986 		if (ipifp != NULL)
23987 			*ipifp = NULL;
23988 		return (B_FALSE);
23989 	}
23990 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23991 		if (!IPIF_CAN_LOOKUP(ipif))
23992 			continue;
23993 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23994 		    ipif->ipif_zoneid != ALL_ZONES)
23995 			continue;
23996 		if ((ipif->ipif_flags & flags) != flags)
23997 			continue;
23998 
23999 		if (ipifp == NULL) {
24000 			mutex_exit(&ill->ill_lock);
24001 			ASSERT(maybe_ipif == NULL);
24002 			return (B_TRUE);
24003 		}
24004 		if (!ill->ill_isv6 ||
24005 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24006 			ipif_refhold_locked(ipif);
24007 			mutex_exit(&ill->ill_lock);
24008 			*ipifp = ipif;
24009 			return (B_TRUE);
24010 		}
24011 		if (maybe_ipif == NULL)
24012 			maybe_ipif = ipif;
24013 	}
24014 	if (ipifp != NULL) {
24015 		if (maybe_ipif != NULL)
24016 			ipif_refhold_locked(maybe_ipif);
24017 		*ipifp = maybe_ipif;
24018 	}
24019 	mutex_exit(&ill->ill_lock);
24020 	return (maybe_ipif != NULL);
24021 }
24022 
24023 /*
24024  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24025  */
24026 boolean_t
24027 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24028 {
24029 	ill_t *illg;
24030 	ip_stack_t	*ipst = ill->ill_ipst;
24031 
24032 	/*
24033 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24034 	 */
24035 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24036 		return (B_TRUE);
24037 	}
24038 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24039 	if (ill->ill_group == NULL) {
24040 		/* ill not in a group */
24041 		rw_exit(&ipst->ips_ill_g_lock);
24042 		return (B_FALSE);
24043 	}
24044 
24045 	/*
24046 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24047 	 * group. We need to look for an ipif in the zone on all the ills in the
24048 	 * group.
24049 	 */
24050 	illg = ill->ill_group->illgrp_ill;
24051 	do {
24052 		/*
24053 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24054 		 * that it's not there.
24055 		 */
24056 		if (illg != ill &&
24057 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24058 			break;
24059 		}
24060 	} while ((illg = illg->ill_group_next) != NULL);
24061 	rw_exit(&ipst->ips_ill_g_lock);
24062 	return (illg != NULL);
24063 }
24064 
24065 /*
24066  * Check if this ill is only being used to send ICMP probes for IPMP
24067  */
24068 boolean_t
24069 ill_is_probeonly(ill_t *ill)
24070 {
24071 	/*
24072 	 * Check if the interface is FAILED, or INACTIVE
24073 	 */
24074 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24075 		return (B_TRUE);
24076 
24077 	return (B_FALSE);
24078 }
24079 
24080 /*
24081  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24082  * If a pointer to an ipif_t is returned then the caller will need to do
24083  * an ill_refrele().
24084  *
24085  * If there is no real interface which matches the ifindex, then it looks
24086  * for a group that has a matching index. In the case of a group match the
24087  * lifidx must be zero. We don't need emulate the logical interfaces
24088  * since IP Filter's use of netinfo doesn't use that.
24089  */
24090 ipif_t *
24091 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24092     ip_stack_t *ipst)
24093 {
24094 	ipif_t *ipif;
24095 	ill_t *ill;
24096 
24097 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24098 	    ipst);
24099 
24100 	if (ill == NULL) {
24101 		/* Fallback to group names only if hook_emulation set */
24102 		if (!ipst->ips_ipmp_hook_emulation)
24103 			return (NULL);
24104 
24105 		if (lifidx != 0)
24106 			return (NULL);
24107 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24108 		if (ill == NULL)
24109 			return (NULL);
24110 	}
24111 
24112 	mutex_enter(&ill->ill_lock);
24113 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24114 		mutex_exit(&ill->ill_lock);
24115 		ill_refrele(ill);
24116 		return (NULL);
24117 	}
24118 
24119 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24120 		if (!IPIF_CAN_LOOKUP(ipif))
24121 			continue;
24122 		if (lifidx == ipif->ipif_id) {
24123 			ipif_refhold_locked(ipif);
24124 			break;
24125 		}
24126 	}
24127 
24128 	mutex_exit(&ill->ill_lock);
24129 	ill_refrele(ill);
24130 	return (ipif);
24131 }
24132 
24133 /*
24134  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24135  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24136  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24137  * for details.
24138  */
24139 void
24140 ill_fastpath_flush(ill_t *ill)
24141 {
24142 	ip_stack_t *ipst = ill->ill_ipst;
24143 
24144 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24145 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24146 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24147 }
24148 
24149 /*
24150  * Set the physical address information for `ill' to the contents of the
24151  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24152  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24153  * EINPROGRESS will be returned.
24154  */
24155 int
24156 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24157 {
24158 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24159 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24160 
24161 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24162 
24163 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24164 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24165 		/* Changing DL_IPV6_TOKEN is not yet supported */
24166 		return (0);
24167 	}
24168 
24169 	/*
24170 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24171 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24172 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24173 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24174 	 */
24175 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24176 		freemsg(mp);
24177 		return (ENOMEM);
24178 	}
24179 
24180 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24181 
24182 	/*
24183 	 * If we can quiesce the ill, then set the address.  If not, then
24184 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24185 	 */
24186 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24187 	mutex_enter(&ill->ill_lock);
24188 	if (!ill_is_quiescent(ill)) {
24189 		/* call cannot fail since `conn_t *' argument is NULL */
24190 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24191 		    mp, ILL_DOWN);
24192 		mutex_exit(&ill->ill_lock);
24193 		return (EINPROGRESS);
24194 	}
24195 	mutex_exit(&ill->ill_lock);
24196 
24197 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24198 	return (0);
24199 }
24200 
24201 /*
24202  * Once the ill associated with `q' has quiesced, set its physical address
24203  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24204  * are passed (linked by b_cont), since we sometimes need to save two distinct
24205  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24206  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24207  * is quiesced, we know any stale IREs with the old address information have
24208  * already been removed, so we don't need to call ill_fastpath_flush().
24209  */
24210 /* ARGSUSED */
24211 static void
24212 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24213 {
24214 	ill_t		*ill = q->q_ptr;
24215 	mblk_t		*addrmp2 = unlinkb(addrmp);
24216 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24217 	uint_t		addrlen, addroff;
24218 
24219 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24220 
24221 	addroff	= dlindp->dl_addr_offset;
24222 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24223 
24224 	switch (dlindp->dl_data) {
24225 	case DL_IPV6_LINK_LAYER_ADDR:
24226 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24227 		freemsg(addrmp2);
24228 		break;
24229 
24230 	case DL_CURR_PHYS_ADDR:
24231 		freemsg(ill->ill_phys_addr_mp);
24232 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24233 		ill->ill_phys_addr_mp = addrmp;
24234 		ill->ill_phys_addr_length = addrlen;
24235 
24236 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24237 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24238 		else
24239 			freemsg(addrmp2);
24240 		break;
24241 	default:
24242 		ASSERT(0);
24243 	}
24244 
24245 	/*
24246 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24247 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24248 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24249 	 * brought up.
24250 	 */
24251 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24252 		ipsq_current_finish(ipsq);
24253 }
24254 
24255 /*
24256  * Helper routine for setting the ill_nd_lla fields.
24257  */
24258 void
24259 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24260 {
24261 	freemsg(ill->ill_nd_lla_mp);
24262 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24263 	ill->ill_nd_lla_mp = ndmp;
24264 	ill->ill_nd_lla_len = addrlen;
24265 }
24266 
24267 major_t IP_MAJ;
24268 #define	IP	"ip"
24269 
24270 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24271 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24272 
24273 /*
24274  * Issue REMOVEIF ioctls to have the loopback interfaces
24275  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24276  * the former going away when the user-level processes in the zone
24277  * are killed  * and the latter are cleaned up by the stream head
24278  * str_stack_shutdown callback that undoes all I_PLINKs.
24279  */
24280 void
24281 ip_loopback_cleanup(ip_stack_t *ipst)
24282 {
24283 	int error;
24284 	ldi_handle_t	lh = NULL;
24285 	ldi_ident_t	li = NULL;
24286 	int		rval;
24287 	cred_t		*cr;
24288 	struct strioctl iocb;
24289 	struct lifreq	lifreq;
24290 
24291 	IP_MAJ = ddi_name_to_major(IP);
24292 
24293 #ifdef NS_DEBUG
24294 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24295 	    ipst->ips_netstack->netstack_stackid);
24296 #endif
24297 
24298 	bzero(&lifreq, sizeof (lifreq));
24299 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24300 
24301 	error = ldi_ident_from_major(IP_MAJ, &li);
24302 	if (error) {
24303 #ifdef DEBUG
24304 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24305 		    error);
24306 #endif
24307 		return;
24308 	}
24309 
24310 	cr = zone_get_kcred(netstackid_to_zoneid(
24311 	    ipst->ips_netstack->netstack_stackid));
24312 	ASSERT(cr != NULL);
24313 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24314 	if (error) {
24315 #ifdef DEBUG
24316 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24317 		    error);
24318 #endif
24319 		goto out;
24320 	}
24321 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24322 	iocb.ic_timout = 15;
24323 	iocb.ic_len = sizeof (lifreq);
24324 	iocb.ic_dp = (char *)&lifreq;
24325 
24326 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24327 	/* LINTED - statement has no consequent */
24328 	if (error) {
24329 #ifdef NS_DEBUG
24330 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24331 		    "UDP6 error %d\n", error);
24332 #endif
24333 	}
24334 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24335 	lh = NULL;
24336 
24337 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24338 	if (error) {
24339 #ifdef NS_DEBUG
24340 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24341 		    error);
24342 #endif
24343 		goto out;
24344 	}
24345 
24346 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24347 	iocb.ic_timout = 15;
24348 	iocb.ic_len = sizeof (lifreq);
24349 	iocb.ic_dp = (char *)&lifreq;
24350 
24351 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24352 	/* LINTED - statement has no consequent */
24353 	if (error) {
24354 #ifdef NS_DEBUG
24355 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24356 		    "UDP error %d\n", error);
24357 #endif
24358 	}
24359 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24360 	lh = NULL;
24361 
24362 out:
24363 	/* Close layered handles */
24364 	if (lh)
24365 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24366 	if (li)
24367 		ldi_ident_release(li);
24368 
24369 	crfree(cr);
24370 }
24371 
24372 /*
24373  * This needs to be in-sync with nic_event_t definition
24374  */
24375 static const char *
24376 ill_hook_event2str(nic_event_t event)
24377 {
24378 	switch (event) {
24379 	case NE_PLUMB:
24380 		return ("PLUMB");
24381 	case NE_UNPLUMB:
24382 		return ("UNPLUMB");
24383 	case NE_UP:
24384 		return ("UP");
24385 	case NE_DOWN:
24386 		return ("DOWN");
24387 	case NE_ADDRESS_CHANGE:
24388 		return ("ADDRESS_CHANGE");
24389 	case NE_LIF_UP:
24390 		return ("LIF_UP");
24391 	case NE_LIF_DOWN:
24392 		return ("LIF_DOWN");
24393 	default:
24394 		return ("UNKNOWN");
24395 	}
24396 }
24397 
24398 void
24399 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
24400     nic_event_data_t data, size_t datalen)
24401 {
24402 	ip_stack_t		*ipst = ill->ill_ipst;
24403 	hook_nic_event_int_t	*info;
24404 	const char		*str = NULL;
24405 
24406 	/* create a new nic event info */
24407 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
24408 		goto fail;
24409 
24410 	if (event == NE_UNPLUMB)
24411 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
24412 	else
24413 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24414 	info->hnei_event.hne_lif = lif;
24415 	info->hnei_event.hne_event = event;
24416 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
24417 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24418 	info->hnei_event.hne_data = NULL;
24419 	info->hnei_event.hne_datalen = 0;
24420 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
24421 
24422 	if (data != NULL && datalen != 0) {
24423 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24424 		if (info->hnei_event.hne_data == NULL)
24425 			goto fail;
24426 		bcopy(data, info->hnei_event.hne_data, datalen);
24427 		info->hnei_event.hne_datalen = datalen;
24428 	}
24429 
24430 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
24431 	    DDI_NOSLEEP) == DDI_SUCCESS)
24432 		return;
24433 
24434 fail:
24435 	if (info != NULL) {
24436 		if (info->hnei_event.hne_data != NULL) {
24437 			kmem_free(info->hnei_event.hne_data,
24438 			    info->hnei_event.hne_datalen);
24439 		}
24440 		kmem_free(info, sizeof (hook_nic_event_t));
24441 	}
24442 	str = ill_hook_event2str(event);
24443 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
24444 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24445 }
24446 
24447 void
24448 ipif_up_notify(ipif_t *ipif)
24449 {
24450 	ip_rts_ifmsg(ipif);
24451 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
24452 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
24453 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
24454 	    NE_LIF_UP, NULL, 0);
24455 }
24456