xref: /titanic_41/usr/src/uts/common/inet/ip/ip_if.c (revision c48ac12eb805b96b1b8d7582c4168b4e4fa56865)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 #include <sys/bitmap.h>
49 
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/strsun.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_ftable.h>
81 #include <inet/ip_rts.h>
82 #include <inet/ip_ndp.h>
83 #include <inet/ip_if.h>
84 #include <inet/ip_impl.h>
85 #include <inet/tun.h>
86 #include <inet/sctp_ip.h>
87 #include <inet/ip_netinfo.h>
88 #include <inet/mib2.h>
89 
90 #include <net/pfkeyv2.h>
91 #include <inet/ipsec_info.h>
92 #include <inet/sadb.h>
93 #include <inet/ipsec_impl.h>
94 #include <sys/iphada.h>
95 
96 
97 #include <netinet/igmp.h>
98 #include <inet/ip_listutils.h>
99 #include <inet/ipclassifier.h>
100 #include <sys/mac.h>
101 
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
104 
105 #include <sys/tsol/tndb.h>
106 #include <sys/tsol/tnet.h>
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 typedef struct ip_sock_ar_s {
122 	union {
123 		area_t	ip_sock_area;
124 		ared_t	ip_sock_ared;
125 		areq_t	ip_sock_areq;
126 	} ip_sock_ar_u;
127 	queue_t	*ip_sock_ar_q;
128 } ip_sock_ar_t;
129 
130 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
131 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
132 		    char *value, caddr_t cp, cred_t *ioc_cr);
133 
134 static boolean_t ill_is_quiescent(ill_t *);
135 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
136 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
137 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
140     mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
142     queue_t *q, mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
144     mblk_t *mp, boolean_t need_up);
145 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
146     mblk_t *mp);
147 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
148     queue_t *q, mblk_t *mp, boolean_t need_up);
149 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160 		    boolean_t initialize);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_multicast_down(ipif_t *ipif);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
182 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
183 
184 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
185 static int	ill_arp_off(ill_t *ill);
186 static int	ill_arp_on(ill_t *ill);
187 static void	ill_delete_interface_type(ill_if_t *);
188 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
189 static void	ill_dl_down(ill_t *ill);
190 static void	ill_down(ill_t *ill);
191 static void	ill_downi(ire_t *ire, char *ill_arg);
192 static void	ill_free_mib(ill_t *ill);
193 static void	ill_glist_delete(ill_t *);
194 static boolean_t ill_has_usable_ipif(ill_t *);
195 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
196 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
197 static void	ill_phyint_free(ill_t *ill);
198 static void	ill_phyint_reinit(ill_t *ill);
199 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
200 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
201 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
202 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
203 static void	ill_stq_cache_delete(ire_t *, char *);
204 
205 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     in6_addr_t *);
209 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     ipaddr_t *);
211 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
212 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     in6_addr_t *);
214 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
215     ipaddr_t *);
216 
217 static void	ipif_save_ire(ipif_t *, ire_t *);
218 static void	ipif_remove_ire(ipif_t *, ire_t *);
219 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
220 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
221 
222 /*
223  * Per-ill IPsec capabilities management.
224  */
225 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
226 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
227 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
228 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
229 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
230 static void ill_capability_proto(ill_t *, int, mblk_t *);
231 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
232     boolean_t);
233 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
236 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
238 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
240 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
241     dl_capability_sub_t *);
242 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
243 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static void ill_capability_lso_reset(ill_t *, mblk_t **);
245 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
246 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
247 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
248 static void	ill_capability_dls_disable(ill_t *);
249 
250 static void	illgrp_cache_delete(ire_t *, char *);
251 static void	illgrp_delete(ill_t *ill);
252 static void	illgrp_reset_schednext(ill_t *ill);
253 
254 static ill_t	*ill_prev_usesrc(ill_t *);
255 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
256 static void	ill_disband_usesrc_group(ill_t *);
257 
258 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
259 
260 #ifdef DEBUG
261 static	void	ill_trace_cleanup(const ill_t *);
262 static	void	ipif_trace_cleanup(const ipif_t *);
263 #endif
264 
265 /*
266  * if we go over the memory footprint limit more than once in this msec
267  * interval, we'll start pruning aggressively.
268  */
269 int ip_min_frag_prune_time = 0;
270 
271 /*
272  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
273  * and the IPsec DOI
274  */
275 #define	MAX_IPSEC_ALGS	256
276 
277 #define	BITSPERBYTE	8
278 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
279 
280 #define	IPSEC_ALG_ENABLE(algs, algid) \
281 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
282 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
283 
284 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
285 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
286 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
287 
288 typedef uint8_t ipsec_capab_elem_t;
289 
290 /*
291  * Per-algorithm parameters.  Note that at present, only encryption
292  * algorithms have variable keysize (IKE does not provide a way to negotiate
293  * auth algorithm keysize).
294  *
295  * All sizes here are in bits.
296  */
297 typedef struct
298 {
299 	uint16_t	minkeylen;
300 	uint16_t	maxkeylen;
301 } ipsec_capab_algparm_t;
302 
303 /*
304  * Per-ill capabilities.
305  */
306 struct ill_ipsec_capab_s {
307 	ipsec_capab_elem_t *encr_hw_algs;
308 	ipsec_capab_elem_t *auth_hw_algs;
309 	uint32_t algs_size;	/* size of _hw_algs in bytes */
310 	/* algorithm key lengths */
311 	ipsec_capab_algparm_t *encr_algparm;
312 	uint32_t encr_algparm_size;
313 	uint32_t encr_algparm_end;
314 };
315 
316 /*
317  * The field values are larger than strictly necessary for simple
318  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
319  */
320 static area_t	ip_area_template = {
321 	AR_ENTRY_ADD,			/* area_cmd */
322 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
323 					/* area_name_offset */
324 	/* area_name_length temporarily holds this structure length */
325 	sizeof (area_t),			/* area_name_length */
326 	IP_ARP_PROTO_TYPE,		/* area_proto */
327 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
328 	IP_ADDR_LEN,			/* area_proto_addr_length */
329 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
330 					/* area_proto_mask_offset */
331 	0,				/* area_flags */
332 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
333 					/* area_hw_addr_offset */
334 	/* Zero length hw_addr_length means 'use your idea of the address' */
335 	0				/* area_hw_addr_length */
336 };
337 
338 /*
339  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
340  * support
341  */
342 static area_t	ip6_area_template = {
343 	AR_ENTRY_ADD,			/* area_cmd */
344 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
345 					/* area_name_offset */
346 	/* area_name_length temporarily holds this structure length */
347 	sizeof (area_t),			/* area_name_length */
348 	IP_ARP_PROTO_TYPE,		/* area_proto */
349 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
350 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
351 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
352 					/* area_proto_mask_offset */
353 	0,				/* area_flags */
354 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
355 					/* area_hw_addr_offset */
356 	/* Zero length hw_addr_length means 'use your idea of the address' */
357 	0				/* area_hw_addr_length */
358 };
359 
360 static ared_t	ip_ared_template = {
361 	AR_ENTRY_DELETE,
362 	sizeof (ared_t) + IP_ADDR_LEN,
363 	sizeof (ared_t),
364 	IP_ARP_PROTO_TYPE,
365 	sizeof (ared_t),
366 	IP_ADDR_LEN
367 };
368 
369 static ared_t	ip6_ared_template = {
370 	AR_ENTRY_DELETE,
371 	sizeof (ared_t) + IPV6_ADDR_LEN,
372 	sizeof (ared_t),
373 	IP_ARP_PROTO_TYPE,
374 	sizeof (ared_t),
375 	IPV6_ADDR_LEN
376 };
377 
378 /*
379  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
380  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
381  * areq is used).
382  */
383 static areq_t	ip_areq_template = {
384 	AR_ENTRY_QUERY,			/* cmd */
385 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
386 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
387 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
388 	sizeof (areq_t),			/* target addr offset */
389 	IP_ADDR_LEN,			/* target addr_length */
390 	0,				/* flags */
391 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
392 	IP_ADDR_LEN,			/* sender addr length */
393 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
394 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
395 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
396 	/* anything else filled in by the code */
397 };
398 
399 static arc_t	ip_aru_template = {
400 	AR_INTERFACE_UP,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_ard_template = {
406 	AR_INTERFACE_DOWN,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aron_template = {
412 	AR_INTERFACE_ON,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 static arc_t	ip_aroff_template = {
418 	AR_INTERFACE_OFF,
419 	sizeof (arc_t),		/* Name offset */
420 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
421 };
422 
423 
424 static arma_t	ip_arma_multi_template = {
425 	AR_MAPPING_ADD,
426 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
427 				/* Name offset */
428 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
429 	IP_ARP_PROTO_TYPE,
430 	sizeof (arma_t),			/* proto_addr_offset */
431 	IP_ADDR_LEN,				/* proto_addr_length */
432 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
433 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
434 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
435 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
436 	IP_MAX_HW_LEN,				/* hw_addr_length */
437 	0,					/* hw_mapping_start */
438 };
439 
440 static ipft_t	ip_ioctl_ftbl[] = {
441 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
442 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
443 		IPFT_F_NO_REPLY },
444 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
445 		IPFT_F_NO_REPLY },
446 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
447 	{ 0 }
448 };
449 
450 /* Simple ICMP IP Header Template */
451 static ipha_t icmp_ipha = {
452 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
453 };
454 
455 /* Flag descriptors for ip_ipif_report */
456 static nv_t	ipif_nv_tbl[] = {
457 	{ IPIF_UP,		"UP" },
458 	{ IPIF_BROADCAST,	"BROADCAST" },
459 	{ ILLF_DEBUG,		"DEBUG" },
460 	{ PHYI_LOOPBACK,	"LOOPBACK" },
461 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
462 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
463 	{ PHYI_RUNNING,		"RUNNING" },
464 	{ ILLF_NOARP,		"NOARP" },
465 	{ PHYI_PROMISC,		"PROMISC" },
466 	{ PHYI_ALLMULTI,	"ALLMULTI" },
467 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
468 	{ ILLF_MULTICAST,	"MULTICAST" },
469 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
470 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
471 	{ IPIF_DHCPRUNNING,	"DHCP" },
472 	{ IPIF_PRIVATE,		"PRIVATE" },
473 	{ IPIF_NOXMIT,		"NOXMIT" },
474 	{ IPIF_NOLOCAL,		"NOLOCAL" },
475 	{ IPIF_DEPRECATED,	"DEPRECATED" },
476 	{ IPIF_PREFERRED,	"PREFERRED" },
477 	{ IPIF_TEMPORARY,	"TEMPORARY" },
478 	{ IPIF_ADDRCONF,	"ADDRCONF" },
479 	{ PHYI_VIRTUAL,		"VIRTUAL" },
480 	{ ILLF_ROUTER,		"ROUTER" },
481 	{ ILLF_NONUD,		"NONUD" },
482 	{ IPIF_ANYCAST,		"ANYCAST" },
483 	{ ILLF_NORTEXCH,	"NORTEXCH" },
484 	{ ILLF_IPV4,		"IPV4" },
485 	{ ILLF_IPV6,		"IPV6" },
486 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
487 	{ PHYI_FAILED,		"FAILED" },
488 	{ PHYI_STANDBY,		"STANDBY" },
489 	{ PHYI_INACTIVE,	"INACTIVE" },
490 	{ PHYI_OFFLINE,		"OFFLINE" },
491 };
492 
493 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
494 
495 static ip_m_t	ip_m_tbl[] = {
496 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
497 	    ip_ether_v6intfid },
498 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
499 	    ip_nodef_v6intfid },
500 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
501 	    ip_nodef_v6intfid },
502 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
503 	    ip_nodef_v6intfid },
504 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
505 	    ip_ether_v6intfid },
506 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
507 	    ip_ib_v6intfid },
508 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
509 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
510 	    ip_nodef_v6intfid }
511 };
512 
513 static ill_t	ill_null;		/* Empty ILL for init. */
514 char	ipif_loopback_name[] = "lo0";
515 static char *ipv4_forward_suffix = ":ip_forwarding";
516 static char *ipv6_forward_suffix = ":ip6_forwarding";
517 static	sin6_t	sin6_null;	/* Zero address for quick clears */
518 static	sin_t	sin_null;	/* Zero address for quick clears */
519 
520 /* When set search for unused ipif_seqid */
521 static ipif_t	ipif_zero;
522 
523 /*
524  * ppa arena is created after these many
525  * interfaces have been plumbed.
526  */
527 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
528 
529 /*
530  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
531  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
532  * set through platform specific code (Niagara/Ontario).
533  */
534 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
535 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
536 
537 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
538 
539 static uint_t
540 ipif_rand(ip_stack_t *ipst)
541 {
542 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
543 	    12345;
544 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
545 }
546 
547 /*
548  * Allocate per-interface mibs.
549  * Returns true if ok. False otherwise.
550  *  ipsq  may not yet be allocated (loopback case ).
551  */
552 static boolean_t
553 ill_allocate_mibs(ill_t *ill)
554 {
555 	/* Already allocated? */
556 	if (ill->ill_ip_mib != NULL) {
557 		if (ill->ill_isv6)
558 			ASSERT(ill->ill_icmp6_mib != NULL);
559 		return (B_TRUE);
560 	}
561 
562 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
563 	    KM_NOSLEEP);
564 	if (ill->ill_ip_mib == NULL) {
565 		return (B_FALSE);
566 	}
567 
568 	/* Setup static information */
569 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
570 	    sizeof (mib2_ipIfStatsEntry_t));
571 	if (ill->ill_isv6) {
572 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
573 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
574 		    sizeof (mib2_ipv6AddrEntry_t));
575 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
576 		    sizeof (mib2_ipv6RouteEntry_t));
577 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
578 		    sizeof (mib2_ipv6NetToMediaEntry_t));
579 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
580 		    sizeof (ipv6_member_t));
581 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
582 		    sizeof (ipv6_grpsrc_t));
583 	} else {
584 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
585 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
586 		    sizeof (mib2_ipAddrEntry_t));
587 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
588 		    sizeof (mib2_ipRouteEntry_t));
589 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
590 		    sizeof (mib2_ipNetToMediaEntry_t));
591 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
592 		    sizeof (ip_member_t));
593 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
594 		    sizeof (ip_grpsrc_t));
595 
596 		/*
597 		 * For a v4 ill, we are done at this point, because per ill
598 		 * icmp mibs are only used for v6.
599 		 */
600 		return (B_TRUE);
601 	}
602 
603 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
604 	    KM_NOSLEEP);
605 	if (ill->ill_icmp6_mib == NULL) {
606 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
607 		ill->ill_ip_mib = NULL;
608 		return (B_FALSE);
609 	}
610 	/* static icmp info */
611 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
612 	    sizeof (mib2_ipv6IfIcmpEntry_t);
613 	/*
614 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
615 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
616 	 * -> ill_phyint_reinit
617 	 */
618 	return (B_TRUE);
619 }
620 
621 /*
622  * Common code for preparation of ARP commands.  Two points to remember:
623  * 	1) The ill_name is tacked on at the end of the allocated space so
624  *	   the templates name_offset field must contain the total space
625  *	   to allocate less the name length.
626  *
627  *	2) The templates name_length field should contain the *template*
628  *	   length.  We use it as a parameter to bcopy() and then write
629  *	   the real ill_name_length into the name_length field of the copy.
630  * (Always called as writer.)
631  */
632 mblk_t *
633 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
634 {
635 	arc_t	*arc = (arc_t *)template;
636 	char	*cp;
637 	int	len;
638 	mblk_t	*mp;
639 	uint_t	name_length = ill->ill_name_length;
640 	uint_t	template_len = arc->arc_name_length;
641 
642 	len = arc->arc_name_offset + name_length;
643 	mp = allocb(len, BPRI_HI);
644 	if (mp == NULL)
645 		return (NULL);
646 	cp = (char *)mp->b_rptr;
647 	mp->b_wptr = (uchar_t *)&cp[len];
648 	if (template_len)
649 		bcopy(template, cp, template_len);
650 	if (len > template_len)
651 		bzero(&cp[template_len], len - template_len);
652 	mp->b_datap->db_type = M_PROTO;
653 
654 	arc = (arc_t *)cp;
655 	arc->arc_name_length = name_length;
656 	cp = (char *)arc + arc->arc_name_offset;
657 	bcopy(ill->ill_name, cp, name_length);
658 
659 	if (addr) {
660 		area_t	*area = (area_t *)mp->b_rptr;
661 
662 		cp = (char *)area + area->area_proto_addr_offset;
663 		bcopy(addr, cp, area->area_proto_addr_length);
664 		if (area->area_cmd == AR_ENTRY_ADD) {
665 			cp = (char *)area;
666 			len = area->area_proto_addr_length;
667 			if (area->area_proto_mask_offset)
668 				cp += area->area_proto_mask_offset;
669 			else
670 				cp += area->area_proto_addr_offset + len;
671 			while (len-- > 0)
672 				*cp++ = (char)~0;
673 		}
674 	}
675 	return (mp);
676 }
677 
678 mblk_t *
679 ipif_area_alloc(ipif_t *ipif)
680 {
681 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
682 	    (char *)&ipif->ipif_lcl_addr));
683 }
684 
685 mblk_t *
686 ipif_ared_alloc(ipif_t *ipif)
687 {
688 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
689 	    (char *)&ipif->ipif_lcl_addr));
690 }
691 
692 mblk_t *
693 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
694 {
695 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
696 	    (char *)&addr));
697 }
698 
699 /*
700  * Completely vaporize a lower level tap and all associated interfaces.
701  * ill_delete is called only out of ip_close when the device control
702  * stream is being closed.
703  */
704 void
705 ill_delete(ill_t *ill)
706 {
707 	ipif_t	*ipif;
708 	ill_t	*prev_ill;
709 	ip_stack_t	*ipst = ill->ill_ipst;
710 
711 	/*
712 	 * ill_delete may be forcibly entering the ipsq. The previous
713 	 * ioctl may not have completed and may need to be aborted.
714 	 * ipsq_flush takes care of it. If we don't need to enter the
715 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
716 	 * ill_delete_tail is sufficient.
717 	 */
718 	ipsq_flush(ill);
719 
720 	/*
721 	 * Nuke all interfaces.  ipif_free will take down the interface,
722 	 * remove it from the list, and free the data structure.
723 	 * Walk down the ipif list and remove the logical interfaces
724 	 * first before removing the main ipif. We can't unplumb
725 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
726 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
727 	 * POINTOPOINT.
728 	 *
729 	 * If ill_ipif was not properly initialized (i.e low on memory),
730 	 * then no interfaces to clean up. In this case just clean up the
731 	 * ill.
732 	 */
733 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
734 		ipif_free(ipif);
735 
736 	/*
737 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
738 	 * So nobody can be using this mp now. Free the mp allocated for
739 	 * honoring ILLF_NOARP
740 	 */
741 	freemsg(ill->ill_arp_on_mp);
742 	ill->ill_arp_on_mp = NULL;
743 
744 	/* Clean up msgs on pending upcalls for mrouted */
745 	reset_mrt_ill(ill);
746 
747 	/*
748 	 * ipif_free -> reset_conn_ipif will remove all multicast
749 	 * references for IPv4. For IPv6, we need to do it here as
750 	 * it points only at ills.
751 	 */
752 	reset_conn_ill(ill);
753 
754 	/*
755 	 * ill_down will arrange to blow off any IRE's dependent on this
756 	 * ILL, and shut down fragmentation reassembly.
757 	 */
758 	ill_down(ill);
759 
760 	/* Let SCTP know, so that it can remove this from its list. */
761 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
762 
763 	/*
764 	 * If an address on this ILL is being used as a source address then
765 	 * clear out the pointers in other ILLs that point to this ILL.
766 	 */
767 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
768 	if (ill->ill_usesrc_grp_next != NULL) {
769 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
770 			ill_disband_usesrc_group(ill);
771 		} else {	/* consumer of the usesrc ILL */
772 			prev_ill = ill_prev_usesrc(ill);
773 			prev_ill->ill_usesrc_grp_next =
774 			    ill->ill_usesrc_grp_next;
775 		}
776 	}
777 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
778 }
779 
780 static void
781 ipif_non_duplicate(ipif_t *ipif)
782 {
783 	ill_t *ill = ipif->ipif_ill;
784 	mutex_enter(&ill->ill_lock);
785 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
786 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
787 		ASSERT(ill->ill_ipif_dup_count > 0);
788 		ill->ill_ipif_dup_count--;
789 	}
790 	mutex_exit(&ill->ill_lock);
791 }
792 
793 /*
794  * ill_delete_tail is called from ip_modclose after all references
795  * to the closing ill are gone. The wait is done in ip_modclose
796  */
797 void
798 ill_delete_tail(ill_t *ill)
799 {
800 	mblk_t	**mpp;
801 	ipif_t	*ipif;
802 	ip_stack_t	*ipst = ill->ill_ipst;
803 
804 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
805 		ipif_non_duplicate(ipif);
806 		ipif_down_tail(ipif);
807 	}
808 
809 	ASSERT(ill->ill_ipif_dup_count == 0 &&
810 	    ill->ill_arp_down_mp == NULL &&
811 	    ill->ill_arp_del_mapping_mp == NULL);
812 
813 	/*
814 	 * If polling capability is enabled (which signifies direct
815 	 * upcall into IP and driver has ill saved as a handle),
816 	 * we need to make sure that unbind has completed before we
817 	 * let the ill disappear and driver no longer has any reference
818 	 * to this ill.
819 	 */
820 	mutex_enter(&ill->ill_lock);
821 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
822 		cv_wait(&ill->ill_cv, &ill->ill_lock);
823 	mutex_exit(&ill->ill_lock);
824 
825 	/*
826 	 * Clean up polling and soft ring capabilities
827 	 */
828 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
829 		ill_capability_dls_disable(ill);
830 
831 	if (ill->ill_net_type != IRE_LOOPBACK)
832 		qprocsoff(ill->ill_rq);
833 
834 	/*
835 	 * We do an ipsq_flush once again now. New messages could have
836 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
837 	 * could also have landed up if an ioctl thread had looked up
838 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
839 	 * enqueued the ioctl when we did the ipsq_flush last time.
840 	 */
841 	ipsq_flush(ill);
842 
843 	/*
844 	 * Free capabilities.
845 	 */
846 	if (ill->ill_ipsec_capab_ah != NULL) {
847 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
848 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
849 		ill->ill_ipsec_capab_ah = NULL;
850 	}
851 
852 	if (ill->ill_ipsec_capab_esp != NULL) {
853 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
854 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
855 		ill->ill_ipsec_capab_esp = NULL;
856 	}
857 
858 	if (ill->ill_mdt_capab != NULL) {
859 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
860 		ill->ill_mdt_capab = NULL;
861 	}
862 
863 	if (ill->ill_hcksum_capab != NULL) {
864 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
865 		ill->ill_hcksum_capab = NULL;
866 	}
867 
868 	if (ill->ill_zerocopy_capab != NULL) {
869 		kmem_free(ill->ill_zerocopy_capab,
870 		    sizeof (ill_zerocopy_capab_t));
871 		ill->ill_zerocopy_capab = NULL;
872 	}
873 
874 	if (ill->ill_lso_capab != NULL) {
875 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
876 		ill->ill_lso_capab = NULL;
877 	}
878 
879 	if (ill->ill_dls_capab != NULL) {
880 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
881 		ill->ill_dls_capab->ill_unbind_conn = NULL;
882 		kmem_free(ill->ill_dls_capab,
883 		    sizeof (ill_dls_capab_t) +
884 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
885 		ill->ill_dls_capab = NULL;
886 	}
887 
888 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
889 
890 	while (ill->ill_ipif != NULL)
891 		ipif_free_tail(ill->ill_ipif);
892 
893 	/*
894 	 * We have removed all references to ilm from conn and the ones joined
895 	 * within the kernel.
896 	 *
897 	 * We don't walk conns, mrts and ires because
898 	 *
899 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
900 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
901 	 *    ill references.
902 	 */
903 	ASSERT(ilm_walk_ill(ill) == 0);
904 	/*
905 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
906 	 * could free the phyint. No more reference to the phyint after this
907 	 * point.
908 	 */
909 	(void) ill_glist_delete(ill);
910 
911 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
912 	if (ill->ill_ndd_name != NULL)
913 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
914 	rw_exit(&ipst->ips_ip_g_nd_lock);
915 
916 
917 	if (ill->ill_frag_ptr != NULL) {
918 		uint_t count;
919 
920 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
921 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
922 		}
923 		mi_free(ill->ill_frag_ptr);
924 		ill->ill_frag_ptr = NULL;
925 		ill->ill_frag_hash_tbl = NULL;
926 	}
927 
928 	freemsg(ill->ill_nd_lla_mp);
929 	/* Free all retained control messages. */
930 	mpp = &ill->ill_first_mp_to_free;
931 	do {
932 		while (mpp[0]) {
933 			mblk_t  *mp;
934 			mblk_t  *mp1;
935 
936 			mp = mpp[0];
937 			mpp[0] = mp->b_next;
938 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
939 				mp1->b_next = NULL;
940 				mp1->b_prev = NULL;
941 			}
942 			freemsg(mp);
943 		}
944 	} while (mpp++ != &ill->ill_last_mp_to_free);
945 
946 	ill_free_mib(ill);
947 
948 #ifdef DEBUG
949 	ill_trace_cleanup(ill);
950 #endif
951 
952 	/* Drop refcnt here */
953 	netstack_rele(ill->ill_ipst->ips_netstack);
954 	ill->ill_ipst = NULL;
955 }
956 
957 static void
958 ill_free_mib(ill_t *ill)
959 {
960 	ip_stack_t *ipst = ill->ill_ipst;
961 
962 	/*
963 	 * MIB statistics must not be lost, so when an interface
964 	 * goes away the counter values will be added to the global
965 	 * MIBs.
966 	 */
967 	if (ill->ill_ip_mib != NULL) {
968 		if (ill->ill_isv6) {
969 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
970 			    ill->ill_ip_mib);
971 		} else {
972 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
973 			    ill->ill_ip_mib);
974 		}
975 
976 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
977 		ill->ill_ip_mib = NULL;
978 	}
979 	if (ill->ill_icmp6_mib != NULL) {
980 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
981 		    ill->ill_icmp6_mib);
982 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
983 		ill->ill_icmp6_mib = NULL;
984 	}
985 }
986 
987 /*
988  * Concatenate together a physical address and a sap.
989  *
990  * Sap_lengths are interpreted as follows:
991  *   sap_length == 0	==>	no sap
992  *   sap_length > 0	==>	sap is at the head of the dlpi address
993  *   sap_length < 0	==>	sap is at the tail of the dlpi address
994  */
995 static void
996 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
997     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
998 {
999 	uint16_t sap_addr = (uint16_t)sap_src;
1000 
1001 	if (sap_length == 0) {
1002 		if (phys_src == NULL)
1003 			bzero(dst, phys_length);
1004 		else
1005 			bcopy(phys_src, dst, phys_length);
1006 	} else if (sap_length < 0) {
1007 		if (phys_src == NULL)
1008 			bzero(dst, phys_length);
1009 		else
1010 			bcopy(phys_src, dst, phys_length);
1011 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1012 	} else {
1013 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1014 		if (phys_src == NULL)
1015 			bzero((char *)dst + sap_length, phys_length);
1016 		else
1017 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1018 	}
1019 }
1020 
1021 /*
1022  * Generate a dl_unitdata_req mblk for the device and address given.
1023  * addr_length is the length of the physical portion of the address.
1024  * If addr is NULL include an all zero address of the specified length.
1025  * TRUE? In any case, addr_length is taken to be the entire length of the
1026  * dlpi address, including the absolute value of sap_length.
1027  */
1028 mblk_t *
1029 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1030 		t_scalar_t sap_length)
1031 {
1032 	dl_unitdata_req_t *dlur;
1033 	mblk_t	*mp;
1034 	t_scalar_t	abs_sap_length;		/* absolute value */
1035 
1036 	abs_sap_length = ABS(sap_length);
1037 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1038 	    DL_UNITDATA_REQ);
1039 	if (mp == NULL)
1040 		return (NULL);
1041 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1042 	/* HACK: accomodate incompatible DLPI drivers */
1043 	if (addr_length == 8)
1044 		addr_length = 6;
1045 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1046 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1047 	dlur->dl_priority.dl_min = 0;
1048 	dlur->dl_priority.dl_max = 0;
1049 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1050 	    (uchar_t *)&dlur[1]);
1051 	return (mp);
1052 }
1053 
1054 /*
1055  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1056  * Return an error if we already have 1 or more ioctls in progress.
1057  * This is used only for non-exclusive ioctls. Currently this is used
1058  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1059  * and thus need to use ipsq_pending_mp_add.
1060  */
1061 boolean_t
1062 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1063 {
1064 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1065 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1066 	/*
1067 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1068 	 */
1069 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1070 	    (add_mp->b_datap->db_type == M_IOCTL));
1071 
1072 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1073 	/*
1074 	 * Return error if the conn has started closing. The conn
1075 	 * could have finished cleaning up the pending mp list,
1076 	 * If so we should not add another mp to the list negating
1077 	 * the cleanup.
1078 	 */
1079 	if (connp->conn_state_flags & CONN_CLOSING)
1080 		return (B_FALSE);
1081 	/*
1082 	 * Add the pending mp to the head of the list, chained by b_next.
1083 	 * Note down the conn on which the ioctl request came, in b_prev.
1084 	 * This will be used to later get the conn, when we get a response
1085 	 * on the ill queue, from some other module (typically arp)
1086 	 */
1087 	add_mp->b_next = (void *)ill->ill_pending_mp;
1088 	add_mp->b_queue = CONNP_TO_WQ(connp);
1089 	ill->ill_pending_mp = add_mp;
1090 	if (connp != NULL)
1091 		connp->conn_oper_pending_ill = ill;
1092 	return (B_TRUE);
1093 }
1094 
1095 /*
1096  * Retrieve the ill_pending_mp and return it. We have to walk the list
1097  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1098  */
1099 mblk_t *
1100 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1101 {
1102 	mblk_t	*prev = NULL;
1103 	mblk_t	*curr = NULL;
1104 	uint_t	id;
1105 	conn_t	*connp;
1106 
1107 	/*
1108 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1109 	 * up the pending mp, but it does not know the ioc_id and
1110 	 * passes in a zero for it.
1111 	 */
1112 	mutex_enter(&ill->ill_lock);
1113 	if (ioc_id != 0)
1114 		*connpp = NULL;
1115 
1116 	/* Search the list for the appropriate ioctl based on ioc_id */
1117 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1118 	    prev = curr, curr = curr->b_next) {
1119 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1120 		connp = Q_TO_CONN(curr->b_queue);
1121 		/* Match based on the ioc_id or based on the conn */
1122 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1123 			break;
1124 	}
1125 
1126 	if (curr != NULL) {
1127 		/* Unlink the mblk from the pending mp list */
1128 		if (prev != NULL) {
1129 			prev->b_next = curr->b_next;
1130 		} else {
1131 			ASSERT(ill->ill_pending_mp == curr);
1132 			ill->ill_pending_mp = curr->b_next;
1133 		}
1134 
1135 		/*
1136 		 * conn refcnt must have been bumped up at the start of
1137 		 * the ioctl. So we can safely access the conn.
1138 		 */
1139 		ASSERT(CONN_Q(curr->b_queue));
1140 		*connpp = Q_TO_CONN(curr->b_queue);
1141 		curr->b_next = NULL;
1142 		curr->b_queue = NULL;
1143 	}
1144 
1145 	mutex_exit(&ill->ill_lock);
1146 
1147 	return (curr);
1148 }
1149 
1150 /*
1151  * Add the pending mp to the list. There can be only 1 pending mp
1152  * in the list. Any exclusive ioctl that needs to wait for a response
1153  * from another module or driver needs to use this function to set
1154  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1155  * the other module/driver. This is also used while waiting for the
1156  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1157  */
1158 boolean_t
1159 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1160     int waitfor)
1161 {
1162 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1163 
1164 	ASSERT(IAM_WRITER_IPIF(ipif));
1165 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1166 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1167 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1168 	/*
1169 	 * The caller may be using a different ipif than the one passed into
1170 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1171 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1172 	 * that `ipsq_current_ipif == ipif'.
1173 	 */
1174 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1175 
1176 	/*
1177 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1178 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1179 	 */
1180 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1181 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1182 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1183 
1184 	if (connp != NULL) {
1185 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1186 		/*
1187 		 * Return error if the conn has started closing. The conn
1188 		 * could have finished cleaning up the pending mp list,
1189 		 * If so we should not add another mp to the list negating
1190 		 * the cleanup.
1191 		 */
1192 		if (connp->conn_state_flags & CONN_CLOSING)
1193 			return (B_FALSE);
1194 	}
1195 	mutex_enter(&ipsq->ipsq_lock);
1196 	ipsq->ipsq_pending_ipif = ipif;
1197 	/*
1198 	 * Note down the queue in b_queue. This will be returned by
1199 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1200 	 * the processing
1201 	 */
1202 	add_mp->b_next = NULL;
1203 	add_mp->b_queue = q;
1204 	ipsq->ipsq_pending_mp = add_mp;
1205 	ipsq->ipsq_waitfor = waitfor;
1206 
1207 	if (connp != NULL)
1208 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1209 	mutex_exit(&ipsq->ipsq_lock);
1210 	return (B_TRUE);
1211 }
1212 
1213 /*
1214  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1215  * queued in the list.
1216  */
1217 mblk_t *
1218 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1219 {
1220 	mblk_t	*curr = NULL;
1221 
1222 	mutex_enter(&ipsq->ipsq_lock);
1223 	*connpp = NULL;
1224 	if (ipsq->ipsq_pending_mp == NULL) {
1225 		mutex_exit(&ipsq->ipsq_lock);
1226 		return (NULL);
1227 	}
1228 
1229 	/* There can be only 1 such excl message */
1230 	curr = ipsq->ipsq_pending_mp;
1231 	ASSERT(curr != NULL && curr->b_next == NULL);
1232 	ipsq->ipsq_pending_ipif = NULL;
1233 	ipsq->ipsq_pending_mp = NULL;
1234 	ipsq->ipsq_waitfor = 0;
1235 	mutex_exit(&ipsq->ipsq_lock);
1236 
1237 	if (CONN_Q(curr->b_queue)) {
1238 		/*
1239 		 * This mp did a refhold on the conn, at the start of the ioctl.
1240 		 * So we can safely return a pointer to the conn to the caller.
1241 		 */
1242 		*connpp = Q_TO_CONN(curr->b_queue);
1243 	} else {
1244 		*connpp = NULL;
1245 	}
1246 	curr->b_next = NULL;
1247 	curr->b_prev = NULL;
1248 	return (curr);
1249 }
1250 
1251 /*
1252  * Cleanup the ioctl mp queued in ipsq_pending_mp
1253  * - Called in the ill_delete path
1254  * - Called in the M_ERROR or M_HANGUP path on the ill.
1255  * - Called in the conn close path.
1256  */
1257 boolean_t
1258 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1259 {
1260 	mblk_t	*mp;
1261 	ipsq_t	*ipsq;
1262 	queue_t	*q;
1263 	ipif_t	*ipif;
1264 
1265 	ASSERT(IAM_WRITER_ILL(ill));
1266 	ipsq = ill->ill_phyint->phyint_ipsq;
1267 	mutex_enter(&ipsq->ipsq_lock);
1268 	/*
1269 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1270 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1271 	 * even if it is meant for another ill, since we have to enqueue
1272 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1273 	 * If connp is non-null we are called from the conn close path.
1274 	 */
1275 	mp = ipsq->ipsq_pending_mp;
1276 	if (mp == NULL || (connp != NULL &&
1277 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1278 		mutex_exit(&ipsq->ipsq_lock);
1279 		return (B_FALSE);
1280 	}
1281 	/* Now remove from the ipsq_pending_mp */
1282 	ipsq->ipsq_pending_mp = NULL;
1283 	q = mp->b_queue;
1284 	mp->b_next = NULL;
1285 	mp->b_prev = NULL;
1286 	mp->b_queue = NULL;
1287 
1288 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1289 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1290 	if (ill->ill_move_in_progress) {
1291 		ILL_CLEAR_MOVE(ill);
1292 	} else if (ill->ill_up_ipifs) {
1293 		ill_group_cleanup(ill);
1294 	}
1295 
1296 	ipif = ipsq->ipsq_pending_ipif;
1297 	ipsq->ipsq_pending_ipif = NULL;
1298 	ipsq->ipsq_waitfor = 0;
1299 	ipsq->ipsq_current_ipif = NULL;
1300 	ipsq->ipsq_current_ioctl = 0;
1301 	mutex_exit(&ipsq->ipsq_lock);
1302 
1303 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1304 		if (connp == NULL) {
1305 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1306 		} else {
1307 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1308 			mutex_enter(&ipif->ipif_ill->ill_lock);
1309 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1310 			mutex_exit(&ipif->ipif_ill->ill_lock);
1311 		}
1312 	} else {
1313 		/*
1314 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1315 		 * be just inet_freemsg. we have to restart it
1316 		 * otherwise the thread will be stuck.
1317 		 */
1318 		inet_freemsg(mp);
1319 	}
1320 	return (B_TRUE);
1321 }
1322 
1323 /*
1324  * The ill is closing. Cleanup all the pending mps. Called exclusively
1325  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1326  * knows this ill, and hence nobody can add an mp to this list
1327  */
1328 static void
1329 ill_pending_mp_cleanup(ill_t *ill)
1330 {
1331 	mblk_t	*mp;
1332 	queue_t	*q;
1333 
1334 	ASSERT(IAM_WRITER_ILL(ill));
1335 
1336 	mutex_enter(&ill->ill_lock);
1337 	/*
1338 	 * Every mp on the pending mp list originating from an ioctl
1339 	 * added 1 to the conn refcnt, at the start of the ioctl.
1340 	 * So bump it down now.  See comments in ip_wput_nondata()
1341 	 */
1342 	while (ill->ill_pending_mp != NULL) {
1343 		mp = ill->ill_pending_mp;
1344 		ill->ill_pending_mp = mp->b_next;
1345 		mutex_exit(&ill->ill_lock);
1346 
1347 		q = mp->b_queue;
1348 		ASSERT(CONN_Q(q));
1349 		mp->b_next = NULL;
1350 		mp->b_prev = NULL;
1351 		mp->b_queue = NULL;
1352 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1353 		mutex_enter(&ill->ill_lock);
1354 	}
1355 	ill->ill_pending_ipif = NULL;
1356 
1357 	mutex_exit(&ill->ill_lock);
1358 }
1359 
1360 /*
1361  * Called in the conn close path and ill delete path
1362  */
1363 static void
1364 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1365 {
1366 	ipsq_t	*ipsq;
1367 	mblk_t	*prev;
1368 	mblk_t	*curr;
1369 	mblk_t	*next;
1370 	queue_t	*q;
1371 	mblk_t	*tmp_list = NULL;
1372 
1373 	ASSERT(IAM_WRITER_ILL(ill));
1374 	if (connp != NULL)
1375 		q = CONNP_TO_WQ(connp);
1376 	else
1377 		q = ill->ill_wq;
1378 
1379 	ipsq = ill->ill_phyint->phyint_ipsq;
1380 	/*
1381 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1382 	 * In the case of ioctl from a conn, there can be only 1 mp
1383 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1384 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1385 	 * ioctls meant for this ill form conn's are not flushed. They will
1386 	 * be processed during ipsq_exit and will not find the ill and will
1387 	 * return error.
1388 	 */
1389 	mutex_enter(&ipsq->ipsq_lock);
1390 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1391 	    curr = next) {
1392 		next = curr->b_next;
1393 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1394 			/* Unlink the mblk from the pending mp list */
1395 			if (prev != NULL) {
1396 				prev->b_next = curr->b_next;
1397 			} else {
1398 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1399 				ipsq->ipsq_xopq_mphead = curr->b_next;
1400 			}
1401 			if (ipsq->ipsq_xopq_mptail == curr)
1402 				ipsq->ipsq_xopq_mptail = prev;
1403 			/*
1404 			 * Create a temporary list and release the ipsq lock
1405 			 * New elements are added to the head of the tmp_list
1406 			 */
1407 			curr->b_next = tmp_list;
1408 			tmp_list = curr;
1409 		} else {
1410 			prev = curr;
1411 		}
1412 	}
1413 	mutex_exit(&ipsq->ipsq_lock);
1414 
1415 	while (tmp_list != NULL) {
1416 		curr = tmp_list;
1417 		tmp_list = curr->b_next;
1418 		curr->b_next = NULL;
1419 		curr->b_prev = NULL;
1420 		curr->b_queue = NULL;
1421 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1422 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1423 			    CONN_CLOSE : NO_COPYOUT, NULL);
1424 		} else {
1425 			/*
1426 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1427 			 * this can't be just inet_freemsg. we have to
1428 			 * restart it otherwise the thread will be stuck.
1429 			 */
1430 			inet_freemsg(curr);
1431 		}
1432 	}
1433 }
1434 
1435 /*
1436  * This conn has started closing. Cleanup any pending ioctl from this conn.
1437  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1438  */
1439 void
1440 conn_ioctl_cleanup(conn_t *connp)
1441 {
1442 	mblk_t *curr;
1443 	ipsq_t	*ipsq;
1444 	ill_t	*ill;
1445 	boolean_t refheld;
1446 
1447 	/*
1448 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1449 	 * ioctl has not yet started, the mp is pending in the list headed by
1450 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1451 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1452 	 * is currently executing now the mp is not queued anywhere but
1453 	 * conn_oper_pending_ill is null. The conn close will wait
1454 	 * till the conn_ref drops to zero.
1455 	 */
1456 	mutex_enter(&connp->conn_lock);
1457 	ill = connp->conn_oper_pending_ill;
1458 	if (ill == NULL) {
1459 		mutex_exit(&connp->conn_lock);
1460 		return;
1461 	}
1462 
1463 	curr = ill_pending_mp_get(ill, &connp, 0);
1464 	if (curr != NULL) {
1465 		mutex_exit(&connp->conn_lock);
1466 		CONN_DEC_REF(connp);
1467 		inet_freemsg(curr);
1468 		return;
1469 	}
1470 	/*
1471 	 * We may not be able to refhold the ill if the ill/ipif
1472 	 * is changing. But we need to make sure that the ill will
1473 	 * not vanish. So we just bump up the ill_waiter count.
1474 	 */
1475 	refheld = ill_waiter_inc(ill);
1476 	mutex_exit(&connp->conn_lock);
1477 	if (refheld) {
1478 		if (ipsq_enter(ill, B_TRUE)) {
1479 			ill_waiter_dcr(ill);
1480 			/*
1481 			 * Check whether this ioctl has started and is
1482 			 * pending now in ipsq_pending_mp. If it is not
1483 			 * found there then check whether this ioctl has
1484 			 * not even started and is in the ipsq_xopq list.
1485 			 */
1486 			if (!ipsq_pending_mp_cleanup(ill, connp))
1487 				ipsq_xopq_mp_cleanup(ill, connp);
1488 			ipsq = ill->ill_phyint->phyint_ipsq;
1489 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1490 			return;
1491 		}
1492 	}
1493 
1494 	/*
1495 	 * The ill is also closing and we could not bump up the
1496 	 * ill_waiter_count or we could not enter the ipsq. Leave
1497 	 * the cleanup to ill_delete
1498 	 */
1499 	mutex_enter(&connp->conn_lock);
1500 	while (connp->conn_oper_pending_ill != NULL)
1501 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1502 	mutex_exit(&connp->conn_lock);
1503 	if (refheld)
1504 		ill_waiter_dcr(ill);
1505 }
1506 
1507 /*
1508  * ipcl_walk function for cleaning up conn_*_ill fields.
1509  */
1510 static void
1511 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1512 {
1513 	ill_t	*ill = (ill_t *)arg;
1514 	ire_t	*ire;
1515 
1516 	mutex_enter(&connp->conn_lock);
1517 	if (connp->conn_multicast_ill == ill) {
1518 		/* Revert to late binding */
1519 		connp->conn_multicast_ill = NULL;
1520 		connp->conn_orig_multicast_ifindex = 0;
1521 	}
1522 	if (connp->conn_incoming_ill == ill)
1523 		connp->conn_incoming_ill = NULL;
1524 	if (connp->conn_outgoing_ill == ill)
1525 		connp->conn_outgoing_ill = NULL;
1526 	if (connp->conn_outgoing_pill == ill)
1527 		connp->conn_outgoing_pill = NULL;
1528 	if (connp->conn_nofailover_ill == ill)
1529 		connp->conn_nofailover_ill = NULL;
1530 	if (connp->conn_dhcpinit_ill == ill) {
1531 		connp->conn_dhcpinit_ill = NULL;
1532 		ASSERT(ill->ill_dhcpinit != 0);
1533 		atomic_dec_32(&ill->ill_dhcpinit);
1534 	}
1535 	if (connp->conn_ire_cache != NULL) {
1536 		ire = connp->conn_ire_cache;
1537 		/*
1538 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1539 		 * interface X and ipif coming from interface Y, if interface
1540 		 * X and Y are part of the same IPMPgroup. Thus whenever
1541 		 * interface X goes down, remove all references to it by
1542 		 * checking both on ire_ipif and ire_stq.
1543 		 */
1544 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1545 		    (ire->ire_type == IRE_CACHE &&
1546 		    ire->ire_stq == ill->ill_wq)) {
1547 			connp->conn_ire_cache = NULL;
1548 			mutex_exit(&connp->conn_lock);
1549 			ire_refrele_notr(ire);
1550 			return;
1551 		}
1552 	}
1553 	mutex_exit(&connp->conn_lock);
1554 
1555 }
1556 
1557 /* ARGSUSED */
1558 void
1559 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1560 {
1561 	ill_t	*ill = q->q_ptr;
1562 	ipif_t	*ipif;
1563 
1564 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1565 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1566 		ipif_non_duplicate(ipif);
1567 		ipif_down_tail(ipif);
1568 	}
1569 	freemsg(mp);
1570 	ipsq_current_finish(ipsq);
1571 }
1572 
1573 /*
1574  * ill_down_start is called when we want to down this ill and bring it up again
1575  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1576  * all interfaces, but don't tear down any plumbing.
1577  */
1578 boolean_t
1579 ill_down_start(queue_t *q, mblk_t *mp)
1580 {
1581 	ill_t	*ill = q->q_ptr;
1582 	ipif_t	*ipif;
1583 
1584 	ASSERT(IAM_WRITER_ILL(ill));
1585 
1586 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1587 		(void) ipif_down(ipif, NULL, NULL);
1588 
1589 	ill_down(ill);
1590 
1591 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1592 
1593 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1594 
1595 	/*
1596 	 * Atomically test and add the pending mp if references are active.
1597 	 */
1598 	mutex_enter(&ill->ill_lock);
1599 	if (!ill_is_quiescent(ill)) {
1600 		/* call cannot fail since `conn_t *' argument is NULL */
1601 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1602 		    mp, ILL_DOWN);
1603 		mutex_exit(&ill->ill_lock);
1604 		return (B_FALSE);
1605 	}
1606 	mutex_exit(&ill->ill_lock);
1607 	return (B_TRUE);
1608 }
1609 
1610 static void
1611 ill_down(ill_t *ill)
1612 {
1613 	ip_stack_t	*ipst = ill->ill_ipst;
1614 
1615 	/* Blow off any IREs dependent on this ILL. */
1616 	ire_walk(ill_downi, (char *)ill, ipst);
1617 
1618 	/* Remove any conn_*_ill depending on this ill */
1619 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1620 
1621 	if (ill->ill_group != NULL) {
1622 		illgrp_delete(ill);
1623 	}
1624 }
1625 
1626 /*
1627  * ire_walk routine used to delete every IRE that depends on queues
1628  * associated with 'ill'.  (Always called as writer.)
1629  */
1630 static void
1631 ill_downi(ire_t *ire, char *ill_arg)
1632 {
1633 	ill_t	*ill = (ill_t *)ill_arg;
1634 
1635 	/*
1636 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1637 	 * interface X and ipif coming from interface Y, if interface
1638 	 * X and Y are part of the same IPMP group. Thus whenever interface
1639 	 * X goes down, remove all references to it by checking both
1640 	 * on ire_ipif and ire_stq.
1641 	 */
1642 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1643 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1644 		ire_delete(ire);
1645 	}
1646 }
1647 
1648 /*
1649  * Remove ire/nce from the fastpath list.
1650  */
1651 void
1652 ill_fastpath_nack(ill_t *ill)
1653 {
1654 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1655 }
1656 
1657 /* Consume an M_IOCACK of the fastpath probe. */
1658 void
1659 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1660 {
1661 	mblk_t	*mp1 = mp;
1662 
1663 	/*
1664 	 * If this was the first attempt turn on the fastpath probing.
1665 	 */
1666 	mutex_enter(&ill->ill_lock);
1667 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1668 		ill->ill_dlpi_fastpath_state = IDS_OK;
1669 	mutex_exit(&ill->ill_lock);
1670 
1671 	/* Free the M_IOCACK mblk, hold on to the data */
1672 	mp = mp->b_cont;
1673 	freeb(mp1);
1674 	if (mp == NULL)
1675 		return;
1676 	if (mp->b_cont != NULL) {
1677 		/*
1678 		 * Update all IRE's or NCE's that are waiting for
1679 		 * fastpath update.
1680 		 */
1681 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1682 		mp1 = mp->b_cont;
1683 		freeb(mp);
1684 		mp = mp1;
1685 	} else {
1686 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1687 	}
1688 
1689 	freeb(mp);
1690 }
1691 
1692 /*
1693  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1694  * The data portion of the request is a dl_unitdata_req_t template for
1695  * what we would send downstream in the absence of a fastpath confirmation.
1696  */
1697 int
1698 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1699 {
1700 	struct iocblk	*ioc;
1701 	mblk_t	*mp;
1702 
1703 	if (dlur_mp == NULL)
1704 		return (EINVAL);
1705 
1706 	mutex_enter(&ill->ill_lock);
1707 	switch (ill->ill_dlpi_fastpath_state) {
1708 	case IDS_FAILED:
1709 		/*
1710 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1711 		 * support it.
1712 		 */
1713 		mutex_exit(&ill->ill_lock);
1714 		return (ENOTSUP);
1715 	case IDS_UNKNOWN:
1716 		/* This is the first probe */
1717 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1718 		break;
1719 	default:
1720 		break;
1721 	}
1722 	mutex_exit(&ill->ill_lock);
1723 
1724 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1725 		return (EAGAIN);
1726 
1727 	mp->b_cont = copyb(dlur_mp);
1728 	if (mp->b_cont == NULL) {
1729 		freeb(mp);
1730 		return (EAGAIN);
1731 	}
1732 
1733 	ioc = (struct iocblk *)mp->b_rptr;
1734 	ioc->ioc_count = msgdsize(mp->b_cont);
1735 
1736 	putnext(ill->ill_wq, mp);
1737 	return (0);
1738 }
1739 
1740 void
1741 ill_capability_probe(ill_t *ill)
1742 {
1743 	/*
1744 	 * Do so only if capabilities are still unknown.
1745 	 */
1746 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1747 		return;
1748 
1749 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1750 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1751 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1752 }
1753 
1754 void
1755 ill_capability_reset(ill_t *ill)
1756 {
1757 	mblk_t *sc_mp = NULL;
1758 	mblk_t *tmp;
1759 
1760 	/*
1761 	 * Note here that we reset the state to UNKNOWN, and later send
1762 	 * down the DL_CAPABILITY_REQ without first setting the state to
1763 	 * INPROGRESS.  We do this in order to distinguish the
1764 	 * DL_CAPABILITY_ACK response which may come back in response to
1765 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1766 	 * also handle the case where the driver doesn't send us back
1767 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1768 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1769 	 * features are turned off until the state reaches IDS_OK.
1770 	 */
1771 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1772 	ill->ill_capab_reneg = B_FALSE;
1773 
1774 	/*
1775 	 * Disable sub-capabilities and request a list of sub-capability
1776 	 * messages which will be sent down to the driver.  Each handler
1777 	 * allocates the corresponding dl_capability_sub_t inside an
1778 	 * mblk, and links it to the existing sc_mp mblk, or return it
1779 	 * as sc_mp if it's the first sub-capability (the passed in
1780 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1781 	 * sc_mp will be pulled-up, before passing it downstream.
1782 	 */
1783 	ill_capability_mdt_reset(ill, &sc_mp);
1784 	ill_capability_hcksum_reset(ill, &sc_mp);
1785 	ill_capability_zerocopy_reset(ill, &sc_mp);
1786 	ill_capability_ipsec_reset(ill, &sc_mp);
1787 	ill_capability_dls_reset(ill, &sc_mp);
1788 	ill_capability_lso_reset(ill, &sc_mp);
1789 
1790 	/* Nothing to send down in order to disable the capabilities? */
1791 	if (sc_mp == NULL)
1792 		return;
1793 
1794 	tmp = msgpullup(sc_mp, -1);
1795 	freemsg(sc_mp);
1796 	if ((sc_mp = tmp) == NULL) {
1797 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1798 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1799 		return;
1800 	}
1801 
1802 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1803 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1804 }
1805 
1806 /*
1807  * Request or set new-style hardware capabilities supported by DLS provider.
1808  */
1809 static void
1810 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1811 {
1812 	mblk_t *mp;
1813 	dl_capability_req_t *capb;
1814 	size_t size = 0;
1815 	uint8_t *ptr;
1816 
1817 	if (reqp != NULL)
1818 		size = MBLKL(reqp);
1819 
1820 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1821 	if (mp == NULL) {
1822 		freemsg(reqp);
1823 		return;
1824 	}
1825 	ptr = mp->b_rptr;
1826 
1827 	capb = (dl_capability_req_t *)ptr;
1828 	ptr += sizeof (dl_capability_req_t);
1829 
1830 	if (reqp != NULL) {
1831 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1832 		capb->dl_sub_length = size;
1833 		bcopy(reqp->b_rptr, ptr, size);
1834 		ptr += size;
1835 		mp->b_cont = reqp->b_cont;
1836 		freeb(reqp);
1837 	}
1838 	ASSERT(ptr == mp->b_wptr);
1839 
1840 	ill_dlpi_send(ill, mp);
1841 }
1842 
1843 static void
1844 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1845 {
1846 	dl_capab_id_t *id_ic;
1847 	uint_t sub_dl_cap = outers->dl_cap;
1848 	dl_capability_sub_t *inners;
1849 	uint8_t *capend;
1850 
1851 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1852 
1853 	/*
1854 	 * Note: range checks here are not absolutely sufficient to
1855 	 * make us robust against malformed messages sent by drivers;
1856 	 * this is in keeping with the rest of IP's dlpi handling.
1857 	 * (Remember, it's coming from something else in the kernel
1858 	 * address space)
1859 	 */
1860 
1861 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1862 	if (capend > mp->b_wptr) {
1863 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1864 		    "malformed sub-capability too long for mblk");
1865 		return;
1866 	}
1867 
1868 	id_ic = (dl_capab_id_t *)(outers + 1);
1869 
1870 	if (outers->dl_length < sizeof (*id_ic) ||
1871 	    (inners = &id_ic->id_subcap,
1872 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1873 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1874 		    "encapsulated capab type %d too long for mblk",
1875 		    inners->dl_cap);
1876 		return;
1877 	}
1878 
1879 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1880 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1881 		    "isn't as expected; pass-thru module(s) detected, "
1882 		    "discarding capability\n", inners->dl_cap));
1883 		return;
1884 	}
1885 
1886 	/* Process the encapsulated sub-capability */
1887 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1888 }
1889 
1890 /*
1891  * Process Multidata Transmit capability negotiation ack received from a
1892  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1893  * DL_CAPABILITY_ACK message.
1894  */
1895 static void
1896 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1897 {
1898 	mblk_t *nmp = NULL;
1899 	dl_capability_req_t *oc;
1900 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1901 	ill_mdt_capab_t **ill_mdt_capab;
1902 	uint_t sub_dl_cap = isub->dl_cap;
1903 	uint8_t *capend;
1904 
1905 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1906 
1907 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1908 
1909 	/*
1910 	 * Note: range checks here are not absolutely sufficient to
1911 	 * make us robust against malformed messages sent by drivers;
1912 	 * this is in keeping with the rest of IP's dlpi handling.
1913 	 * (Remember, it's coming from something else in the kernel
1914 	 * address space)
1915 	 */
1916 
1917 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1918 	if (capend > mp->b_wptr) {
1919 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1920 		    "malformed sub-capability too long for mblk");
1921 		return;
1922 	}
1923 
1924 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1925 
1926 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1927 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1928 		    "unsupported MDT sub-capability (version %d, expected %d)",
1929 		    mdt_ic->mdt_version, MDT_VERSION_2);
1930 		return;
1931 	}
1932 
1933 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1934 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1935 		    "capability isn't as expected; pass-thru module(s) "
1936 		    "detected, discarding capability\n"));
1937 		return;
1938 	}
1939 
1940 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1941 
1942 		if (*ill_mdt_capab == NULL) {
1943 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1944 			    KM_NOSLEEP);
1945 
1946 			if (*ill_mdt_capab == NULL) {
1947 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1948 				    "could not enable MDT version %d "
1949 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1950 				    ill->ill_name);
1951 				return;
1952 			}
1953 		}
1954 
1955 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1956 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1957 		    "header spaces, %d max pld bufs, %d span limit)\n",
1958 		    ill->ill_name, MDT_VERSION_2,
1959 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1960 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1961 
1962 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1963 		(*ill_mdt_capab)->ill_mdt_on = 1;
1964 		/*
1965 		 * Round the following values to the nearest 32-bit; ULP
1966 		 * may further adjust them to accomodate for additional
1967 		 * protocol headers.  We pass these values to ULP during
1968 		 * bind time.
1969 		 */
1970 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1971 		    roundup(mdt_ic->mdt_hdr_head, 4);
1972 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1973 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1974 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1975 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1976 
1977 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1978 	} else {
1979 		uint_t size;
1980 		uchar_t *rptr;
1981 
1982 		size = sizeof (dl_capability_req_t) +
1983 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1984 
1985 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1986 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1987 			    "could not enable MDT for %s (ENOMEM)\n",
1988 			    ill->ill_name);
1989 			return;
1990 		}
1991 
1992 		rptr = nmp->b_rptr;
1993 		/* initialize dl_capability_req_t */
1994 		oc = (dl_capability_req_t *)nmp->b_rptr;
1995 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1996 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1997 		    sizeof (dl_capab_mdt_t);
1998 		nmp->b_rptr += sizeof (dl_capability_req_t);
1999 
2000 		/* initialize dl_capability_sub_t */
2001 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2002 		nmp->b_rptr += sizeof (*isub);
2003 
2004 		/* initialize dl_capab_mdt_t */
2005 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2006 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2007 
2008 		nmp->b_rptr = rptr;
2009 
2010 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2011 		    "to enable MDT version %d\n", ill->ill_name,
2012 		    MDT_VERSION_2));
2013 
2014 		/* set ENABLE flag */
2015 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2016 
2017 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2018 		ill_dlpi_send(ill, nmp);
2019 	}
2020 }
2021 
2022 static void
2023 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2024 {
2025 	mblk_t *mp;
2026 	dl_capab_mdt_t *mdt_subcap;
2027 	dl_capability_sub_t *dl_subcap;
2028 	int size;
2029 
2030 	if (!ILL_MDT_CAPABLE(ill))
2031 		return;
2032 
2033 	ASSERT(ill->ill_mdt_capab != NULL);
2034 	/*
2035 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2036 	 * structure since it's possible that another thread is still
2037 	 * referring to it.  The structure only gets deallocated when
2038 	 * we destroy the ill.
2039 	 */
2040 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2041 
2042 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2043 
2044 	mp = allocb(size, BPRI_HI);
2045 	if (mp == NULL) {
2046 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2047 		    "request to disable MDT\n"));
2048 		return;
2049 	}
2050 
2051 	mp->b_wptr = mp->b_rptr + size;
2052 
2053 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2054 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2055 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2056 
2057 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2058 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2059 	mdt_subcap->mdt_flags = 0;
2060 	mdt_subcap->mdt_hdr_head = 0;
2061 	mdt_subcap->mdt_hdr_tail = 0;
2062 
2063 	if (*sc_mp != NULL)
2064 		linkb(*sc_mp, mp);
2065 	else
2066 		*sc_mp = mp;
2067 }
2068 
2069 /*
2070  * Send a DL_NOTIFY_REQ to the specified ill to enable
2071  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2072  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2073  * acceleration.
2074  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2075  */
2076 static boolean_t
2077 ill_enable_promisc_notify(ill_t *ill)
2078 {
2079 	mblk_t *mp;
2080 	dl_notify_req_t *req;
2081 
2082 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2083 
2084 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2085 	if (mp == NULL)
2086 		return (B_FALSE);
2087 
2088 	req = (dl_notify_req_t *)mp->b_rptr;
2089 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2090 	    DL_NOTE_PROMISC_OFF_PHYS;
2091 
2092 	ill_dlpi_send(ill, mp);
2093 
2094 	return (B_TRUE);
2095 }
2096 
2097 
2098 /*
2099  * Allocate an IPsec capability request which will be filled by our
2100  * caller to turn on support for one or more algorithms.
2101  */
2102 static mblk_t *
2103 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2104 {
2105 	mblk_t *nmp;
2106 	dl_capability_req_t	*ocap;
2107 	dl_capab_ipsec_t	*ocip;
2108 	dl_capab_ipsec_t	*icip;
2109 	uint8_t			*ptr;
2110 	icip = (dl_capab_ipsec_t *)(isub + 1);
2111 
2112 	/*
2113 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2114 	 * PROMISC_ON/OFF notification from the provider. We need to
2115 	 * do this before enabling the algorithms to avoid leakage of
2116 	 * cleartext packets.
2117 	 */
2118 
2119 	if (!ill_enable_promisc_notify(ill))
2120 		return (NULL);
2121 
2122 	/*
2123 	 * Allocate new mblk which will contain a new capability
2124 	 * request to enable the capabilities.
2125 	 */
2126 
2127 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2128 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2129 	if (nmp == NULL)
2130 		return (NULL);
2131 
2132 	ptr = nmp->b_rptr;
2133 
2134 	/* initialize dl_capability_req_t */
2135 	ocap = (dl_capability_req_t *)ptr;
2136 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2137 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2138 	ptr += sizeof (dl_capability_req_t);
2139 
2140 	/* initialize dl_capability_sub_t */
2141 	bcopy(isub, ptr, sizeof (*isub));
2142 	ptr += sizeof (*isub);
2143 
2144 	/* initialize dl_capab_ipsec_t */
2145 	ocip = (dl_capab_ipsec_t *)ptr;
2146 	bcopy(icip, ocip, sizeof (*icip));
2147 
2148 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2149 	return (nmp);
2150 }
2151 
2152 /*
2153  * Process an IPsec capability negotiation ack received from a DLS Provider.
2154  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2155  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2156  */
2157 static void
2158 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2159 {
2160 	dl_capab_ipsec_t	*icip;
2161 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2162 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2163 	uint_t cipher, nciphers;
2164 	mblk_t *nmp;
2165 	uint_t alg_len;
2166 	boolean_t need_sadb_dump;
2167 	uint_t sub_dl_cap = isub->dl_cap;
2168 	ill_ipsec_capab_t **ill_capab;
2169 	uint64_t ill_capab_flag;
2170 	uint8_t *capend, *ciphend;
2171 	boolean_t sadb_resync;
2172 
2173 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2174 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2175 
2176 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2177 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2178 		ill_capab_flag = ILL_CAPAB_AH;
2179 	} else {
2180 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2181 		ill_capab_flag = ILL_CAPAB_ESP;
2182 	}
2183 
2184 	/*
2185 	 * If the ill capability structure exists, then this incoming
2186 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2187 	 * If this is so, then we'd need to resynchronize the SADB
2188 	 * after re-enabling the offloaded ciphers.
2189 	 */
2190 	sadb_resync = (*ill_capab != NULL);
2191 
2192 	/*
2193 	 * Note: range checks here are not absolutely sufficient to
2194 	 * make us robust against malformed messages sent by drivers;
2195 	 * this is in keeping with the rest of IP's dlpi handling.
2196 	 * (Remember, it's coming from something else in the kernel
2197 	 * address space)
2198 	 */
2199 
2200 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2201 	if (capend > mp->b_wptr) {
2202 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2203 		    "malformed sub-capability too long for mblk");
2204 		return;
2205 	}
2206 
2207 	/*
2208 	 * There are two types of acks we process here:
2209 	 * 1. acks in reply to a (first form) generic capability req
2210 	 *    (no ENABLE flag set)
2211 	 * 2. acks in reply to a ENABLE capability req.
2212 	 *    (ENABLE flag set)
2213 	 *
2214 	 * We process the subcapability passed as argument as follows:
2215 	 * 1 do initializations
2216 	 *   1.1 initialize nmp = NULL
2217 	 *   1.2 set need_sadb_dump to B_FALSE
2218 	 * 2 for each cipher in subcapability:
2219 	 *   2.1 if ENABLE flag is set:
2220 	 *	2.1.1 update per-ill ipsec capabilities info
2221 	 *	2.1.2 set need_sadb_dump to B_TRUE
2222 	 *   2.2 if ENABLE flag is not set:
2223 	 *	2.2.1 if nmp is NULL:
2224 	 *		2.2.1.1 allocate and initialize nmp
2225 	 *		2.2.1.2 init current pos in nmp
2226 	 *	2.2.2 copy current cipher to current pos in nmp
2227 	 *	2.2.3 set ENABLE flag in nmp
2228 	 *	2.2.4 update current pos
2229 	 * 3 if nmp is not equal to NULL, send enable request
2230 	 *   3.1 send capability request
2231 	 * 4 if need_sadb_dump is B_TRUE
2232 	 *   4.1 enable promiscuous on/off notifications
2233 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2234 	 *	AH or ESP SA's to interface.
2235 	 */
2236 
2237 	nmp = NULL;
2238 	oalg = NULL;
2239 	need_sadb_dump = B_FALSE;
2240 	icip = (dl_capab_ipsec_t *)(isub + 1);
2241 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2242 
2243 	nciphers = icip->cip_nciphers;
2244 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2245 
2246 	if (ciphend > capend) {
2247 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2248 		    "too many ciphers for sub-capability len");
2249 		return;
2250 	}
2251 
2252 	for (cipher = 0; cipher < nciphers; cipher++) {
2253 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2254 
2255 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2256 			/*
2257 			 * TBD: when we provide a way to disable capabilities
2258 			 * from above, need to manage the request-pending state
2259 			 * and fail if we were not expecting this ACK.
2260 			 */
2261 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2262 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2263 
2264 			/*
2265 			 * Update IPsec capabilities for this ill
2266 			 */
2267 
2268 			if (*ill_capab == NULL) {
2269 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2270 				    ("ill_capability_ipsec_ack: "
2271 				    "allocating ipsec_capab for ill\n"));
2272 				*ill_capab = ill_ipsec_capab_alloc();
2273 
2274 				if (*ill_capab == NULL) {
2275 					cmn_err(CE_WARN,
2276 					    "ill_capability_ipsec_ack: "
2277 					    "could not enable IPsec Hardware "
2278 					    "acceleration for %s (ENOMEM)\n",
2279 					    ill->ill_name);
2280 					return;
2281 				}
2282 			}
2283 
2284 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2285 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2286 
2287 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2288 				cmn_err(CE_WARN,
2289 				    "ill_capability_ipsec_ack: "
2290 				    "malformed IPsec algorithm id %d",
2291 				    ialg->alg_prim);
2292 				continue;
2293 			}
2294 
2295 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2296 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2297 				    ialg->alg_prim);
2298 			} else {
2299 				ipsec_capab_algparm_t *alp;
2300 
2301 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2302 				    ialg->alg_prim);
2303 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2304 				    ialg->alg_prim)) {
2305 					cmn_err(CE_WARN,
2306 					    "ill_capability_ipsec_ack: "
2307 					    "no space for IPsec alg id %d",
2308 					    ialg->alg_prim);
2309 					continue;
2310 				}
2311 				alp = &((*ill_capab)->encr_algparm[
2312 				    ialg->alg_prim]);
2313 				alp->minkeylen = ialg->alg_minbits;
2314 				alp->maxkeylen = ialg->alg_maxbits;
2315 			}
2316 			ill->ill_capabilities |= ill_capab_flag;
2317 			/*
2318 			 * indicate that a capability was enabled, which
2319 			 * will be used below to kick off a SADB dump
2320 			 * to the ill.
2321 			 */
2322 			need_sadb_dump = B_TRUE;
2323 		} else {
2324 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2325 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2326 			    ialg->alg_prim));
2327 
2328 			if (nmp == NULL) {
2329 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2330 				if (nmp == NULL) {
2331 					/*
2332 					 * Sending the PROMISC_ON/OFF
2333 					 * notification request failed.
2334 					 * We cannot enable the algorithms
2335 					 * since the Provider will not
2336 					 * notify IP of promiscous mode
2337 					 * changes, which could lead
2338 					 * to leakage of packets.
2339 					 */
2340 					cmn_err(CE_WARN,
2341 					    "ill_capability_ipsec_ack: "
2342 					    "could not enable IPsec Hardware "
2343 					    "acceleration for %s (ENOMEM)\n",
2344 					    ill->ill_name);
2345 					return;
2346 				}
2347 				/* ptr to current output alg specifier */
2348 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2349 			}
2350 
2351 			/*
2352 			 * Copy current alg specifier, set ENABLE
2353 			 * flag, and advance to next output alg.
2354 			 * For now we enable all IPsec capabilities.
2355 			 */
2356 			ASSERT(oalg != NULL);
2357 			bcopy(ialg, oalg, alg_len);
2358 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2359 			nmp->b_wptr += alg_len;
2360 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2361 		}
2362 
2363 		/* move to next input algorithm specifier */
2364 		ialg = (dl_capab_ipsec_alg_t *)
2365 		    ((char *)ialg + alg_len);
2366 	}
2367 
2368 	if (nmp != NULL)
2369 		/*
2370 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2371 		 * IPsec hardware acceleration.
2372 		 */
2373 		ill_dlpi_send(ill, nmp);
2374 
2375 	if (need_sadb_dump)
2376 		/*
2377 		 * An acknowledgement corresponding to a request to
2378 		 * enable acceleration was received, notify SADB.
2379 		 */
2380 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2381 }
2382 
2383 /*
2384  * Given an mblk with enough space in it, create sub-capability entries for
2385  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2386  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2387  * in preparation for the reset the DL_CAPABILITY_REQ message.
2388  */
2389 static void
2390 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2391     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2392 {
2393 	dl_capab_ipsec_t *oipsec;
2394 	dl_capab_ipsec_alg_t *oalg;
2395 	dl_capability_sub_t *dl_subcap;
2396 	int i, k;
2397 
2398 	ASSERT(nciphers > 0);
2399 	ASSERT(ill_cap != NULL);
2400 	ASSERT(mp != NULL);
2401 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2402 
2403 	/* dl_capability_sub_t for "stype" */
2404 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2405 	dl_subcap->dl_cap = stype;
2406 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2407 	mp->b_wptr += sizeof (dl_capability_sub_t);
2408 
2409 	/* dl_capab_ipsec_t for "stype" */
2410 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2411 	oipsec->cip_version = 1;
2412 	oipsec->cip_nciphers = nciphers;
2413 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2414 
2415 	/* create entries for "stype" AUTH ciphers */
2416 	for (i = 0; i < ill_cap->algs_size; i++) {
2417 		for (k = 0; k < BITSPERBYTE; k++) {
2418 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2419 				continue;
2420 
2421 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2422 			bzero((void *)oalg, sizeof (*oalg));
2423 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2424 			oalg->alg_prim = k + (BITSPERBYTE * i);
2425 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2426 		}
2427 	}
2428 	/* create entries for "stype" ENCR ciphers */
2429 	for (i = 0; i < ill_cap->algs_size; i++) {
2430 		for (k = 0; k < BITSPERBYTE; k++) {
2431 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2432 				continue;
2433 
2434 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2435 			bzero((void *)oalg, sizeof (*oalg));
2436 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2437 			oalg->alg_prim = k + (BITSPERBYTE * i);
2438 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2439 		}
2440 	}
2441 }
2442 
2443 /*
2444  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2445  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2446  * POPC instruction, but our macro is more flexible for an arbitrary length
2447  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2448  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2449  * stays that way, we can reduce the number of iterations required.
2450  */
2451 #define	COUNT_1S(val, sum) {					\
2452 	uint8_t x = val & 0xff;					\
2453 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2454 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2455 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2456 }
2457 
2458 /* ARGSUSED */
2459 static void
2460 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2461 {
2462 	mblk_t *mp;
2463 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2464 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2465 	uint64_t ill_capabilities = ill->ill_capabilities;
2466 	int ah_cnt = 0, esp_cnt = 0;
2467 	int ah_len = 0, esp_len = 0;
2468 	int i, size = 0;
2469 
2470 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2471 		return;
2472 
2473 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2474 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2475 
2476 	/* Find out the number of ciphers for AH */
2477 	if (cap_ah != NULL) {
2478 		for (i = 0; i < cap_ah->algs_size; i++) {
2479 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2480 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2481 		}
2482 		if (ah_cnt > 0) {
2483 			size += sizeof (dl_capability_sub_t) +
2484 			    sizeof (dl_capab_ipsec_t);
2485 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2486 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2487 			size += ah_len;
2488 		}
2489 	}
2490 
2491 	/* Find out the number of ciphers for ESP */
2492 	if (cap_esp != NULL) {
2493 		for (i = 0; i < cap_esp->algs_size; i++) {
2494 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2495 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2496 		}
2497 		if (esp_cnt > 0) {
2498 			size += sizeof (dl_capability_sub_t) +
2499 			    sizeof (dl_capab_ipsec_t);
2500 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2501 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2502 			size += esp_len;
2503 		}
2504 	}
2505 
2506 	if (size == 0) {
2507 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2508 		    "there's nothing to reset\n"));
2509 		return;
2510 	}
2511 
2512 	mp = allocb(size, BPRI_HI);
2513 	if (mp == NULL) {
2514 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2515 		    "request to disable IPSEC Hardware Acceleration\n"));
2516 		return;
2517 	}
2518 
2519 	/*
2520 	 * Clear the capability flags for IPsec HA but retain the ill
2521 	 * capability structures since it's possible that another thread
2522 	 * is still referring to them.  The structures only get deallocated
2523 	 * when we destroy the ill.
2524 	 *
2525 	 * Various places check the flags to see if the ill is capable of
2526 	 * hardware acceleration, and by clearing them we ensure that new
2527 	 * outbound IPsec packets are sent down encrypted.
2528 	 */
2529 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2530 
2531 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2532 	if (ah_cnt > 0) {
2533 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2534 		    cap_ah, mp);
2535 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2536 	}
2537 
2538 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2539 	if (esp_cnt > 0) {
2540 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2541 		    cap_esp, mp);
2542 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2543 	}
2544 
2545 	/*
2546 	 * At this point we've composed a bunch of sub-capabilities to be
2547 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2548 	 * by the caller.  Upon receiving this reset message, the driver
2549 	 * must stop inbound decryption (by destroying all inbound SAs)
2550 	 * and let the corresponding packets come in encrypted.
2551 	 */
2552 
2553 	if (*sc_mp != NULL)
2554 		linkb(*sc_mp, mp);
2555 	else
2556 		*sc_mp = mp;
2557 }
2558 
2559 static void
2560 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2561     boolean_t encapsulated)
2562 {
2563 	boolean_t legacy = B_FALSE;
2564 
2565 	/*
2566 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2567 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2568 	 * instructed the driver to disable its advertised capabilities,
2569 	 * so there's no point in accepting any response at this moment.
2570 	 */
2571 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2572 		return;
2573 
2574 	/*
2575 	 * Note that only the following two sub-capabilities may be
2576 	 * considered as "legacy", since their original definitions
2577 	 * do not incorporate the dl_mid_t module ID token, and hence
2578 	 * may require the use of the wrapper sub-capability.
2579 	 */
2580 	switch (subp->dl_cap) {
2581 	case DL_CAPAB_IPSEC_AH:
2582 	case DL_CAPAB_IPSEC_ESP:
2583 		legacy = B_TRUE;
2584 		break;
2585 	}
2586 
2587 	/*
2588 	 * For legacy sub-capabilities which don't incorporate a queue_t
2589 	 * pointer in their structures, discard them if we detect that
2590 	 * there are intermediate modules in between IP and the driver.
2591 	 */
2592 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2593 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2594 		    "%d discarded; %d module(s) present below IP\n",
2595 		    subp->dl_cap, ill->ill_lmod_cnt));
2596 		return;
2597 	}
2598 
2599 	switch (subp->dl_cap) {
2600 	case DL_CAPAB_IPSEC_AH:
2601 	case DL_CAPAB_IPSEC_ESP:
2602 		ill_capability_ipsec_ack(ill, mp, subp);
2603 		break;
2604 	case DL_CAPAB_MDT:
2605 		ill_capability_mdt_ack(ill, mp, subp);
2606 		break;
2607 	case DL_CAPAB_HCKSUM:
2608 		ill_capability_hcksum_ack(ill, mp, subp);
2609 		break;
2610 	case DL_CAPAB_ZEROCOPY:
2611 		ill_capability_zerocopy_ack(ill, mp, subp);
2612 		break;
2613 	case DL_CAPAB_POLL:
2614 		if (!SOFT_RINGS_ENABLED())
2615 			ill_capability_dls_ack(ill, mp, subp);
2616 		break;
2617 	case DL_CAPAB_SOFT_RING:
2618 		if (SOFT_RINGS_ENABLED())
2619 			ill_capability_dls_ack(ill, mp, subp);
2620 		break;
2621 	case DL_CAPAB_LSO:
2622 		ill_capability_lso_ack(ill, mp, subp);
2623 		break;
2624 	default:
2625 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2626 		    subp->dl_cap));
2627 	}
2628 }
2629 
2630 /*
2631  * As part of negotiating polling capability, the driver tells us
2632  * the default (or normal) blanking interval and packet threshold
2633  * (the receive timer fires if blanking interval is reached or
2634  * the packet threshold is reached).
2635  *
2636  * As part of manipulating the polling interval, we always use our
2637  * estimated interval (avg service time * number of packets queued
2638  * on the squeue) but we try to blank for a minimum of
2639  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2640  * packet threshold during this time. When we are not in polling mode
2641  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2642  * rr_min_blank_ratio but up the packet cnt by a ratio of
2643  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2644  * possible although for a shorter interval.
2645  */
2646 #define	RR_MAX_BLANK_RATIO	20
2647 #define	RR_MIN_BLANK_RATIO	10
2648 #define	RR_MAX_PKT_CNT_RATIO	3
2649 #define	RR_MIN_PKT_CNT_RATIO	3
2650 
2651 /*
2652  * These can be tuned via /etc/system.
2653  */
2654 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2655 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2656 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2657 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2658 
2659 static mac_resource_handle_t
2660 ill_ring_add(void *arg, mac_resource_t *mrp)
2661 {
2662 	ill_t			*ill = (ill_t *)arg;
2663 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2664 	ill_rx_ring_t		*rx_ring;
2665 	int			ip_rx_index;
2666 
2667 	ASSERT(mrp != NULL);
2668 	if (mrp->mr_type != MAC_RX_FIFO) {
2669 		return (NULL);
2670 	}
2671 	ASSERT(ill != NULL);
2672 	ASSERT(ill->ill_dls_capab != NULL);
2673 
2674 	mutex_enter(&ill->ill_lock);
2675 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2676 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2677 		ASSERT(rx_ring != NULL);
2678 
2679 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2680 			time_t normal_blank_time =
2681 			    mrfp->mrf_normal_blank_time;
2682 			uint_t normal_pkt_cnt =
2683 			    mrfp->mrf_normal_pkt_count;
2684 
2685 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2686 
2687 	rx_ring->rr_blank = mrfp->mrf_blank;
2688 	rx_ring->rr_handle = mrfp->mrf_arg;
2689 	rx_ring->rr_ill = ill;
2690 	rx_ring->rr_normal_blank_time = normal_blank_time;
2691 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2692 
2693 			rx_ring->rr_max_blank_time =
2694 			    normal_blank_time * rr_max_blank_ratio;
2695 			rx_ring->rr_min_blank_time =
2696 			    normal_blank_time * rr_min_blank_ratio;
2697 			rx_ring->rr_max_pkt_cnt =
2698 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2699 			rx_ring->rr_min_pkt_cnt =
2700 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2701 
2702 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2703 			mutex_exit(&ill->ill_lock);
2704 
2705 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2706 			    (int), ip_rx_index);
2707 			return ((mac_resource_handle_t)rx_ring);
2708 		}
2709 	}
2710 
2711 	/*
2712 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2713 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2714 	 * should be made configurable. Meanwhile it cause no panic because
2715 	 * driver will pass ip_input a NULL handle which will make
2716 	 * IP allocate the default squeue and Polling mode will not
2717 	 * be used for this ring.
2718 	 */
2719 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2720 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2721 
2722 	mutex_exit(&ill->ill_lock);
2723 	return (NULL);
2724 }
2725 
2726 static boolean_t
2727 ill_capability_dls_init(ill_t *ill)
2728 {
2729 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2730 	conn_t 			*connp;
2731 	size_t			sz;
2732 	ip_stack_t *ipst = ill->ill_ipst;
2733 
2734 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2735 		if (ill_dls == NULL) {
2736 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2737 			    "soft_ring enabled for ill=%s (%p) but data "
2738 			    "structs uninitialized\n", ill->ill_name,
2739 			    (void *)ill);
2740 		}
2741 		return (B_TRUE);
2742 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2743 		if (ill_dls == NULL) {
2744 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2745 			    "polling enabled for ill=%s (%p) but data "
2746 			    "structs uninitialized\n", ill->ill_name,
2747 			    (void *)ill);
2748 		}
2749 		return (B_TRUE);
2750 	}
2751 
2752 	if (ill_dls != NULL) {
2753 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2754 		/* Soft_Ring or polling is being re-enabled */
2755 
2756 		connp = ill_dls->ill_unbind_conn;
2757 		ASSERT(rx_ring != NULL);
2758 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2759 		bzero((void *)rx_ring,
2760 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2761 		ill_dls->ill_ring_tbl = rx_ring;
2762 		ill_dls->ill_unbind_conn = connp;
2763 		return (B_TRUE);
2764 	}
2765 
2766 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2767 	    ipst->ips_netstack)) == NULL)
2768 		return (B_FALSE);
2769 
2770 	sz = sizeof (ill_dls_capab_t);
2771 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2772 
2773 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2774 	if (ill_dls == NULL) {
2775 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2776 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2777 		    (void *)ill);
2778 		CONN_DEC_REF(connp);
2779 		return (B_FALSE);
2780 	}
2781 
2782 	/* Allocate space to hold ring table */
2783 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2784 	ill->ill_dls_capab = ill_dls;
2785 	ill_dls->ill_unbind_conn = connp;
2786 	return (B_TRUE);
2787 }
2788 
2789 /*
2790  * ill_capability_dls_disable: disable soft_ring and/or polling
2791  * capability. Since any of the rings might already be in use, need
2792  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2793  * direct calls if necessary.
2794  */
2795 static void
2796 ill_capability_dls_disable(ill_t *ill)
2797 {
2798 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2799 
2800 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2801 		ip_squeue_clean_all(ill);
2802 		ill_dls->ill_tx = NULL;
2803 		ill_dls->ill_tx_handle = NULL;
2804 		ill_dls->ill_dls_change_status = NULL;
2805 		ill_dls->ill_dls_bind = NULL;
2806 		ill_dls->ill_dls_unbind = NULL;
2807 	}
2808 
2809 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2810 }
2811 
2812 static void
2813 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2814     dl_capability_sub_t *isub)
2815 {
2816 	uint_t			size;
2817 	uchar_t			*rptr;
2818 	dl_capab_dls_t	dls, *odls;
2819 	ill_dls_capab_t	*ill_dls;
2820 	mblk_t			*nmp = NULL;
2821 	dl_capability_req_t	*ocap;
2822 	uint_t			sub_dl_cap = isub->dl_cap;
2823 
2824 	if (!ill_capability_dls_init(ill))
2825 		return;
2826 	ill_dls = ill->ill_dls_capab;
2827 
2828 	/* Copy locally to get the members aligned */
2829 	bcopy((void *)idls, (void *)&dls,
2830 	    sizeof (dl_capab_dls_t));
2831 
2832 	/* Get the tx function and handle from dld */
2833 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2834 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2835 
2836 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2837 		ill_dls->ill_dls_change_status =
2838 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2839 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2840 		ill_dls->ill_dls_unbind =
2841 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2842 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2843 	}
2844 
2845 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2846 	    isub->dl_length;
2847 
2848 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2849 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2850 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2851 		    ill->ill_name, (void *)ill);
2852 		return;
2853 	}
2854 
2855 	/* initialize dl_capability_req_t */
2856 	rptr = nmp->b_rptr;
2857 	ocap = (dl_capability_req_t *)rptr;
2858 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2859 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2860 	rptr += sizeof (dl_capability_req_t);
2861 
2862 	/* initialize dl_capability_sub_t */
2863 	bcopy(isub, rptr, sizeof (*isub));
2864 	rptr += sizeof (*isub);
2865 
2866 	odls = (dl_capab_dls_t *)rptr;
2867 	rptr += sizeof (dl_capab_dls_t);
2868 
2869 	/* initialize dl_capab_dls_t to be sent down */
2870 	dls.dls_rx_handle = (uintptr_t)ill;
2871 	dls.dls_rx = (uintptr_t)ip_input;
2872 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2873 
2874 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2875 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2876 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2877 		dls.dls_flags = SOFT_RING_ENABLE;
2878 	} else {
2879 		dls.dls_flags = POLL_ENABLE;
2880 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2881 		    "to enable polling\n", ill->ill_name));
2882 	}
2883 	bcopy((void *)&dls, (void *)odls,
2884 	    sizeof (dl_capab_dls_t));
2885 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2886 	/*
2887 	 * nmp points to a DL_CAPABILITY_REQ message to
2888 	 * enable either soft_ring or polling
2889 	 */
2890 	ill_dlpi_send(ill, nmp);
2891 }
2892 
2893 static void
2894 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2895 {
2896 	mblk_t *mp;
2897 	dl_capab_dls_t *idls;
2898 	dl_capability_sub_t *dl_subcap;
2899 	int size;
2900 
2901 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2902 		return;
2903 
2904 	ASSERT(ill->ill_dls_capab != NULL);
2905 
2906 	size = sizeof (*dl_subcap) + sizeof (*idls);
2907 
2908 	mp = allocb(size, BPRI_HI);
2909 	if (mp == NULL) {
2910 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2911 		    "request to disable soft_ring\n"));
2912 		return;
2913 	}
2914 
2915 	mp->b_wptr = mp->b_rptr + size;
2916 
2917 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2918 	dl_subcap->dl_length = sizeof (*idls);
2919 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2920 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2921 	else
2922 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2923 
2924 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2925 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2926 		idls->dls_flags = SOFT_RING_DISABLE;
2927 	else
2928 		idls->dls_flags = POLL_DISABLE;
2929 
2930 	if (*sc_mp != NULL)
2931 		linkb(*sc_mp, mp);
2932 	else
2933 		*sc_mp = mp;
2934 }
2935 
2936 /*
2937  * Process a soft_ring/poll capability negotiation ack received
2938  * from a DLS Provider.isub must point to the sub-capability
2939  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2940  */
2941 static void
2942 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2943 {
2944 	dl_capab_dls_t		*idls;
2945 	uint_t			sub_dl_cap = isub->dl_cap;
2946 	uint8_t			*capend;
2947 
2948 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2949 	    sub_dl_cap == DL_CAPAB_POLL);
2950 
2951 	if (ill->ill_isv6)
2952 		return;
2953 
2954 	/*
2955 	 * Note: range checks here are not absolutely sufficient to
2956 	 * make us robust against malformed messages sent by drivers;
2957 	 * this is in keeping with the rest of IP's dlpi handling.
2958 	 * (Remember, it's coming from something else in the kernel
2959 	 * address space)
2960 	 */
2961 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2962 	if (capend > mp->b_wptr) {
2963 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2964 		    "malformed sub-capability too long for mblk");
2965 		return;
2966 	}
2967 
2968 	/*
2969 	 * There are two types of acks we process here:
2970 	 * 1. acks in reply to a (first form) generic capability req
2971 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2972 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2973 	 *    capability req.
2974 	 */
2975 	idls = (dl_capab_dls_t *)(isub + 1);
2976 
2977 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2978 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2979 		    "capability isn't as expected; pass-thru "
2980 		    "module(s) detected, discarding capability\n"));
2981 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2982 			/*
2983 			 * This is a capability renegotitation case.
2984 			 * The interface better be unusable at this
2985 			 * point other wise bad things will happen
2986 			 * if we disable direct calls on a running
2987 			 * and up interface.
2988 			 */
2989 			ill_capability_dls_disable(ill);
2990 		}
2991 		return;
2992 	}
2993 
2994 	switch (idls->dls_flags) {
2995 	default:
2996 		/* Disable if unknown flag */
2997 	case SOFT_RING_DISABLE:
2998 	case POLL_DISABLE:
2999 		ill_capability_dls_disable(ill);
3000 		break;
3001 	case SOFT_RING_CAPABLE:
3002 	case POLL_CAPABLE:
3003 		/*
3004 		 * If the capability was already enabled, its safe
3005 		 * to disable it first to get rid of stale information
3006 		 * and then start enabling it again.
3007 		 */
3008 		ill_capability_dls_disable(ill);
3009 		ill_capability_dls_capable(ill, idls, isub);
3010 		break;
3011 	case SOFT_RING_ENABLE:
3012 	case POLL_ENABLE:
3013 		mutex_enter(&ill->ill_lock);
3014 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3015 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3016 			ASSERT(ill->ill_dls_capab != NULL);
3017 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3018 		}
3019 		if (sub_dl_cap == DL_CAPAB_POLL &&
3020 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3021 			ASSERT(ill->ill_dls_capab != NULL);
3022 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3023 			ip1dbg(("ill_capability_dls_ack: interface %s "
3024 			    "has enabled polling\n", ill->ill_name));
3025 		}
3026 		mutex_exit(&ill->ill_lock);
3027 		break;
3028 	}
3029 }
3030 
3031 /*
3032  * Process a hardware checksum offload capability negotiation ack received
3033  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3034  * of a DL_CAPABILITY_ACK message.
3035  */
3036 static void
3037 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3038 {
3039 	dl_capability_req_t	*ocap;
3040 	dl_capab_hcksum_t	*ihck, *ohck;
3041 	ill_hcksum_capab_t	**ill_hcksum;
3042 	mblk_t			*nmp = NULL;
3043 	uint_t			sub_dl_cap = isub->dl_cap;
3044 	uint8_t			*capend;
3045 
3046 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3047 
3048 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3049 
3050 	/*
3051 	 * Note: range checks here are not absolutely sufficient to
3052 	 * make us robust against malformed messages sent by drivers;
3053 	 * this is in keeping with the rest of IP's dlpi handling.
3054 	 * (Remember, it's coming from something else in the kernel
3055 	 * address space)
3056 	 */
3057 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3058 	if (capend > mp->b_wptr) {
3059 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3060 		    "malformed sub-capability too long for mblk");
3061 		return;
3062 	}
3063 
3064 	/*
3065 	 * There are two types of acks we process here:
3066 	 * 1. acks in reply to a (first form) generic capability req
3067 	 *    (no ENABLE flag set)
3068 	 * 2. acks in reply to a ENABLE capability req.
3069 	 *    (ENABLE flag set)
3070 	 */
3071 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3072 
3073 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3074 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3075 		    "unsupported hardware checksum "
3076 		    "sub-capability (version %d, expected %d)",
3077 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3078 		return;
3079 	}
3080 
3081 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3082 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3083 		    "checksum capability isn't as expected; pass-thru "
3084 		    "module(s) detected, discarding capability\n"));
3085 		return;
3086 	}
3087 
3088 #define	CURR_HCKSUM_CAPAB				\
3089 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3090 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3091 
3092 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3093 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3094 		/* do ENABLE processing */
3095 		if (*ill_hcksum == NULL) {
3096 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3097 			    KM_NOSLEEP);
3098 
3099 			if (*ill_hcksum == NULL) {
3100 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3101 				    "could not enable hcksum version %d "
3102 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3103 				    ill->ill_name);
3104 				return;
3105 			}
3106 		}
3107 
3108 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3109 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3110 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3111 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3112 		    "has enabled hardware checksumming\n ",
3113 		    ill->ill_name));
3114 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3115 		/*
3116 		 * Enabling hardware checksum offload
3117 		 * Currently IP supports {TCP,UDP}/IPv4
3118 		 * partial and full cksum offload and
3119 		 * IPv4 header checksum offload.
3120 		 * Allocate new mblk which will
3121 		 * contain a new capability request
3122 		 * to enable hardware checksum offload.
3123 		 */
3124 		uint_t	size;
3125 		uchar_t	*rptr;
3126 
3127 		size = sizeof (dl_capability_req_t) +
3128 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3129 
3130 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3131 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3132 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3133 			    ill->ill_name);
3134 			return;
3135 		}
3136 
3137 		rptr = nmp->b_rptr;
3138 		/* initialize dl_capability_req_t */
3139 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3140 		ocap->dl_sub_offset =
3141 		    sizeof (dl_capability_req_t);
3142 		ocap->dl_sub_length =
3143 		    sizeof (dl_capability_sub_t) +
3144 		    isub->dl_length;
3145 		nmp->b_rptr += sizeof (dl_capability_req_t);
3146 
3147 		/* initialize dl_capability_sub_t */
3148 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3149 		nmp->b_rptr += sizeof (*isub);
3150 
3151 		/* initialize dl_capab_hcksum_t */
3152 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3153 		bcopy(ihck, ohck, sizeof (*ihck));
3154 
3155 		nmp->b_rptr = rptr;
3156 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3157 
3158 		/* Set ENABLE flag */
3159 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3160 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3161 
3162 		/*
3163 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3164 		 * hardware checksum acceleration.
3165 		 */
3166 		ill_dlpi_send(ill, nmp);
3167 	} else {
3168 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3169 		    "advertised %x hardware checksum capability flags\n",
3170 		    ill->ill_name, ihck->hcksum_txflags));
3171 	}
3172 }
3173 
3174 static void
3175 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3176 {
3177 	mblk_t *mp;
3178 	dl_capab_hcksum_t *hck_subcap;
3179 	dl_capability_sub_t *dl_subcap;
3180 	int size;
3181 
3182 	if (!ILL_HCKSUM_CAPABLE(ill))
3183 		return;
3184 
3185 	ASSERT(ill->ill_hcksum_capab != NULL);
3186 	/*
3187 	 * Clear the capability flag for hardware checksum offload but
3188 	 * retain the ill_hcksum_capab structure since it's possible that
3189 	 * another thread is still referring to it.  The structure only
3190 	 * gets deallocated when we destroy the ill.
3191 	 */
3192 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3193 
3194 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3195 
3196 	mp = allocb(size, BPRI_HI);
3197 	if (mp == NULL) {
3198 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3199 		    "request to disable hardware checksum offload\n"));
3200 		return;
3201 	}
3202 
3203 	mp->b_wptr = mp->b_rptr + size;
3204 
3205 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3206 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3207 	dl_subcap->dl_length = sizeof (*hck_subcap);
3208 
3209 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3210 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3211 	hck_subcap->hcksum_txflags = 0;
3212 
3213 	if (*sc_mp != NULL)
3214 		linkb(*sc_mp, mp);
3215 	else
3216 		*sc_mp = mp;
3217 }
3218 
3219 static void
3220 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3221 {
3222 	mblk_t *nmp = NULL;
3223 	dl_capability_req_t *oc;
3224 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3225 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3226 	uint_t sub_dl_cap = isub->dl_cap;
3227 	uint8_t *capend;
3228 
3229 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3230 
3231 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3232 
3233 	/*
3234 	 * Note: range checks here are not absolutely sufficient to
3235 	 * make us robust against malformed messages sent by drivers;
3236 	 * this is in keeping with the rest of IP's dlpi handling.
3237 	 * (Remember, it's coming from something else in the kernel
3238 	 * address space)
3239 	 */
3240 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3241 	if (capend > mp->b_wptr) {
3242 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3243 		    "malformed sub-capability too long for mblk");
3244 		return;
3245 	}
3246 
3247 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3248 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3249 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3250 		    "unsupported ZEROCOPY sub-capability (version %d, "
3251 		    "expected %d)", zc_ic->zerocopy_version,
3252 		    ZEROCOPY_VERSION_1);
3253 		return;
3254 	}
3255 
3256 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3257 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3258 		    "capability isn't as expected; pass-thru module(s) "
3259 		    "detected, discarding capability\n"));
3260 		return;
3261 	}
3262 
3263 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3264 		if (*ill_zerocopy_capab == NULL) {
3265 			*ill_zerocopy_capab =
3266 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3267 			    KM_NOSLEEP);
3268 
3269 			if (*ill_zerocopy_capab == NULL) {
3270 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3271 				    "could not enable Zero-copy version %d "
3272 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3273 				    ill->ill_name);
3274 				return;
3275 			}
3276 		}
3277 
3278 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3279 		    "supports Zero-copy version %d\n", ill->ill_name,
3280 		    ZEROCOPY_VERSION_1));
3281 
3282 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3283 		    zc_ic->zerocopy_version;
3284 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3285 		    zc_ic->zerocopy_flags;
3286 
3287 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3288 	} else {
3289 		uint_t size;
3290 		uchar_t *rptr;
3291 
3292 		size = sizeof (dl_capability_req_t) +
3293 		    sizeof (dl_capability_sub_t) +
3294 		    sizeof (dl_capab_zerocopy_t);
3295 
3296 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3297 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3298 			    "could not enable zerocopy for %s (ENOMEM)\n",
3299 			    ill->ill_name);
3300 			return;
3301 		}
3302 
3303 		rptr = nmp->b_rptr;
3304 		/* initialize dl_capability_req_t */
3305 		oc = (dl_capability_req_t *)rptr;
3306 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3307 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3308 		    sizeof (dl_capab_zerocopy_t);
3309 		rptr += sizeof (dl_capability_req_t);
3310 
3311 		/* initialize dl_capability_sub_t */
3312 		bcopy(isub, rptr, sizeof (*isub));
3313 		rptr += sizeof (*isub);
3314 
3315 		/* initialize dl_capab_zerocopy_t */
3316 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3317 		*zc_oc = *zc_ic;
3318 
3319 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3320 		    "to enable zero-copy version %d\n", ill->ill_name,
3321 		    ZEROCOPY_VERSION_1));
3322 
3323 		/* set VMSAFE_MEM flag */
3324 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3325 
3326 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3327 		ill_dlpi_send(ill, nmp);
3328 	}
3329 }
3330 
3331 static void
3332 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3333 {
3334 	mblk_t *mp;
3335 	dl_capab_zerocopy_t *zerocopy_subcap;
3336 	dl_capability_sub_t *dl_subcap;
3337 	int size;
3338 
3339 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3340 		return;
3341 
3342 	ASSERT(ill->ill_zerocopy_capab != NULL);
3343 	/*
3344 	 * Clear the capability flag for Zero-copy but retain the
3345 	 * ill_zerocopy_capab structure since it's possible that another
3346 	 * thread is still referring to it.  The structure only gets
3347 	 * deallocated when we destroy the ill.
3348 	 */
3349 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3350 
3351 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3352 
3353 	mp = allocb(size, BPRI_HI);
3354 	if (mp == NULL) {
3355 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3356 		    "request to disable Zero-copy\n"));
3357 		return;
3358 	}
3359 
3360 	mp->b_wptr = mp->b_rptr + size;
3361 
3362 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3363 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3364 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3365 
3366 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3367 	zerocopy_subcap->zerocopy_version =
3368 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3369 	zerocopy_subcap->zerocopy_flags = 0;
3370 
3371 	if (*sc_mp != NULL)
3372 		linkb(*sc_mp, mp);
3373 	else
3374 		*sc_mp = mp;
3375 }
3376 
3377 /*
3378  * Process Large Segment Offload capability negotiation ack received from a
3379  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3380  * DL_CAPABILITY_ACK message.
3381  */
3382 static void
3383 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3384 {
3385 	mblk_t *nmp = NULL;
3386 	dl_capability_req_t *oc;
3387 	dl_capab_lso_t *lso_ic, *lso_oc;
3388 	ill_lso_capab_t **ill_lso_capab;
3389 	uint_t sub_dl_cap = isub->dl_cap;
3390 	uint8_t *capend;
3391 
3392 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3393 
3394 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3395 
3396 	/*
3397 	 * Note: range checks here are not absolutely sufficient to
3398 	 * make us robust against malformed messages sent by drivers;
3399 	 * this is in keeping with the rest of IP's dlpi handling.
3400 	 * (Remember, it's coming from something else in the kernel
3401 	 * address space)
3402 	 */
3403 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3404 	if (capend > mp->b_wptr) {
3405 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3406 		    "malformed sub-capability too long for mblk");
3407 		return;
3408 	}
3409 
3410 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3411 
3412 	if (lso_ic->lso_version != LSO_VERSION_1) {
3413 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3414 		    "unsupported LSO sub-capability (version %d, expected %d)",
3415 		    lso_ic->lso_version, LSO_VERSION_1);
3416 		return;
3417 	}
3418 
3419 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3420 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3421 		    "capability isn't as expected; pass-thru module(s) "
3422 		    "detected, discarding capability\n"));
3423 		return;
3424 	}
3425 
3426 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3427 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3428 		if (*ill_lso_capab == NULL) {
3429 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3430 			    KM_NOSLEEP);
3431 
3432 			if (*ill_lso_capab == NULL) {
3433 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3434 				    "could not enable LSO version %d "
3435 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3436 				    ill->ill_name);
3437 				return;
3438 			}
3439 		}
3440 
3441 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3442 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3443 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3444 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3445 
3446 		ip1dbg(("ill_capability_lso_ack: interface %s "
3447 		    "has enabled LSO\n ", ill->ill_name));
3448 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3449 		uint_t size;
3450 		uchar_t *rptr;
3451 
3452 		size = sizeof (dl_capability_req_t) +
3453 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3454 
3455 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3456 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3457 			    "could not enable LSO for %s (ENOMEM)\n",
3458 			    ill->ill_name);
3459 			return;
3460 		}
3461 
3462 		rptr = nmp->b_rptr;
3463 		/* initialize dl_capability_req_t */
3464 		oc = (dl_capability_req_t *)nmp->b_rptr;
3465 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3466 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3467 		    sizeof (dl_capab_lso_t);
3468 		nmp->b_rptr += sizeof (dl_capability_req_t);
3469 
3470 		/* initialize dl_capability_sub_t */
3471 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3472 		nmp->b_rptr += sizeof (*isub);
3473 
3474 		/* initialize dl_capab_lso_t */
3475 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3476 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3477 
3478 		nmp->b_rptr = rptr;
3479 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3480 
3481 		/* set ENABLE flag */
3482 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3483 
3484 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3485 		ill_dlpi_send(ill, nmp);
3486 	} else {
3487 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3488 		    "advertised %x LSO capability flags\n",
3489 		    ill->ill_name, lso_ic->lso_flags));
3490 	}
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 			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.  Note that the per ILL count is
3795 	 * approximate, while the per frag hash bucket counts are accurate.
3796 	 */
3797 	while (ill->ill_frag_count > max_count) {
3798 		int	ix;
3799 		ipfb_t	*oipfb = NULL;
3800 		uint_t	oldest = UINT_MAX;
3801 
3802 		count = 0;
3803 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3804 			ipfb = &ill->ill_frag_hash_tbl[ix];
3805 			mutex_enter(&ipfb->ipfb_lock);
3806 			ipf = ipfb->ipfb_ipf;
3807 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3808 				oldest = ipf->ipf_gen;
3809 				oipfb = ipfb;
3810 			}
3811 			count += ipfb->ipfb_count;
3812 			mutex_exit(&ipfb->ipfb_lock);
3813 		}
3814 		/* Refresh the per ILL count */
3815 		ill->ill_frag_count = count;
3816 		if (oipfb == NULL) {
3817 			ill->ill_frag_count = 0;
3818 			break;
3819 		}
3820 		if (count <= max_count)
3821 			return;	/* Somebody beat us to it, nothing to do */
3822 		mutex_enter(&oipfb->ipfb_lock);
3823 		ipf = oipfb->ipfb_ipf;
3824 		if (ipf != NULL) {
3825 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3826 		}
3827 		mutex_exit(&oipfb->ipfb_lock);
3828 	}
3829 }
3830 
3831 /*
3832  * free 'free_cnt' fragmented packets starting at ipf.
3833  */
3834 void
3835 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3836 {
3837 	size_t	count;
3838 	mblk_t	*mp;
3839 	mblk_t	*tmp;
3840 	ipf_t **ipfp = ipf->ipf_ptphn;
3841 
3842 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3843 	ASSERT(ipfp != NULL);
3844 	ASSERT(ipf != NULL);
3845 
3846 	while (ipf != NULL && free_cnt-- > 0) {
3847 		count = ipf->ipf_count;
3848 		mp = ipf->ipf_mp;
3849 		ipf = ipf->ipf_hash_next;
3850 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3851 			IP_REASS_SET_START(tmp, 0);
3852 			IP_REASS_SET_END(tmp, 0);
3853 		}
3854 		ill->ill_frag_count -= count;
3855 		ASSERT(ipfb->ipfb_count >= count);
3856 		ipfb->ipfb_count -= count;
3857 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3858 		ipfb->ipfb_frag_pkts--;
3859 		freemsg(mp);
3860 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3861 	}
3862 
3863 	if (ipf)
3864 		ipf->ipf_ptphn = ipfp;
3865 	ipfp[0] = ipf;
3866 }
3867 
3868 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3869 	"obsolete and may be removed in a future release of Solaris.  Use " \
3870 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3871 
3872 /*
3873  * For obsolete per-interface forwarding configuration;
3874  * called in response to ND_GET.
3875  */
3876 /* ARGSUSED */
3877 static int
3878 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3879 {
3880 	ill_t *ill = (ill_t *)cp;
3881 
3882 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3883 
3884 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3885 	return (0);
3886 }
3887 
3888 /*
3889  * For obsolete per-interface forwarding configuration;
3890  * called in response to ND_SET.
3891  */
3892 /* ARGSUSED */
3893 static int
3894 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3895     cred_t *ioc_cr)
3896 {
3897 	long value;
3898 	int retval;
3899 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3900 
3901 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3902 
3903 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3904 	    value < 0 || value > 1) {
3905 		return (EINVAL);
3906 	}
3907 
3908 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3909 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3910 	rw_exit(&ipst->ips_ill_g_lock);
3911 	return (retval);
3912 }
3913 
3914 /*
3915  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3916  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3917  * up RTS_IFINFO routing socket messages for each interface whose flags we
3918  * change.
3919  */
3920 int
3921 ill_forward_set(ill_t *ill, boolean_t enable)
3922 {
3923 	ill_group_t *illgrp;
3924 	ip_stack_t	*ipst = ill->ill_ipst;
3925 
3926 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3927 
3928 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3929 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3930 		return (0);
3931 
3932 	if (IS_LOOPBACK(ill))
3933 		return (EINVAL);
3934 
3935 	/*
3936 	 * If the ill is in an IPMP group, set the forwarding policy on all
3937 	 * members of the group to the same value.
3938 	 */
3939 	illgrp = ill->ill_group;
3940 	if (illgrp != NULL) {
3941 		ill_t *tmp_ill;
3942 
3943 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3944 		    tmp_ill = tmp_ill->ill_group_next) {
3945 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3946 			    (enable ? "Enabling" : "Disabling"),
3947 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3948 			    tmp_ill->ill_name));
3949 			mutex_enter(&tmp_ill->ill_lock);
3950 			if (enable)
3951 				tmp_ill->ill_flags |= ILLF_ROUTER;
3952 			else
3953 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3954 			mutex_exit(&tmp_ill->ill_lock);
3955 			if (tmp_ill->ill_isv6)
3956 				ill_set_nce_router_flags(tmp_ill, enable);
3957 			/* Notify routing socket listeners of this change. */
3958 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3959 		}
3960 	} else {
3961 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3962 		    (enable ? "Enabling" : "Disabling"),
3963 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3964 		mutex_enter(&ill->ill_lock);
3965 		if (enable)
3966 			ill->ill_flags |= ILLF_ROUTER;
3967 		else
3968 			ill->ill_flags &= ~ILLF_ROUTER;
3969 		mutex_exit(&ill->ill_lock);
3970 		if (ill->ill_isv6)
3971 			ill_set_nce_router_flags(ill, enable);
3972 		/* Notify routing socket listeners of this change. */
3973 		ip_rts_ifmsg(ill->ill_ipif);
3974 	}
3975 
3976 	return (0);
3977 }
3978 
3979 /*
3980  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3981  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3982  * set or clear.
3983  */
3984 static void
3985 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3986 {
3987 	ipif_t *ipif;
3988 	nce_t *nce;
3989 
3990 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3991 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3992 		if (nce != NULL) {
3993 			mutex_enter(&nce->nce_lock);
3994 			if (enable)
3995 				nce->nce_flags |= NCE_F_ISROUTER;
3996 			else
3997 				nce->nce_flags &= ~NCE_F_ISROUTER;
3998 			mutex_exit(&nce->nce_lock);
3999 			NCE_REFRELE(nce);
4000 		}
4001 	}
4002 }
4003 
4004 /*
4005  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4006  * for this ill.  Make sure the v6/v4 question has been answered about this
4007  * ill.  The creation of this ndd variable is only for backwards compatibility.
4008  * The preferred way to control per-interface IP forwarding is through the
4009  * ILLF_ROUTER interface flag.
4010  */
4011 static int
4012 ill_set_ndd_name(ill_t *ill)
4013 {
4014 	char *suffix;
4015 	ip_stack_t	*ipst = ill->ill_ipst;
4016 
4017 	ASSERT(IAM_WRITER_ILL(ill));
4018 
4019 	if (ill->ill_isv6)
4020 		suffix = ipv6_forward_suffix;
4021 	else
4022 		suffix = ipv4_forward_suffix;
4023 
4024 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4025 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4026 	/*
4027 	 * Copies over the '\0'.
4028 	 * Note that strlen(suffix) is always bounded.
4029 	 */
4030 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4031 	    strlen(suffix) + 1);
4032 
4033 	/*
4034 	 * Use of the nd table requires holding the reader lock.
4035 	 * Modifying the nd table thru nd_load/nd_unload requires
4036 	 * the writer lock.
4037 	 */
4038 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4039 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4040 	    nd_ill_forward_set, (caddr_t)ill)) {
4041 		/*
4042 		 * If the nd_load failed, it only meant that it could not
4043 		 * allocate a new bunch of room for further NDD expansion.
4044 		 * Because of that, the ill_ndd_name will be set to 0, and
4045 		 * this interface is at the mercy of the global ip_forwarding
4046 		 * variable.
4047 		 */
4048 		rw_exit(&ipst->ips_ip_g_nd_lock);
4049 		ill->ill_ndd_name = NULL;
4050 		return (ENOMEM);
4051 	}
4052 	rw_exit(&ipst->ips_ip_g_nd_lock);
4053 	return (0);
4054 }
4055 
4056 /*
4057  * Intializes the context structure and returns the first ill in the list
4058  * cuurently start_list and end_list can have values:
4059  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4060  * IP_V4_G_HEAD		Traverse IPV4 list only.
4061  * IP_V6_G_HEAD		Traverse IPV6 list only.
4062  */
4063 
4064 /*
4065  * We don't check for CONDEMNED ills here. Caller must do that if
4066  * necessary under the ill lock.
4067  */
4068 ill_t *
4069 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4070     ip_stack_t *ipst)
4071 {
4072 	ill_if_t *ifp;
4073 	ill_t *ill;
4074 	avl_tree_t *avl_tree;
4075 
4076 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4077 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4078 
4079 	/*
4080 	 * setup the lists to search
4081 	 */
4082 	if (end_list != MAX_G_HEADS) {
4083 		ctx->ctx_current_list = start_list;
4084 		ctx->ctx_last_list = end_list;
4085 	} else {
4086 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4087 		ctx->ctx_current_list = 0;
4088 	}
4089 
4090 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4091 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4092 		if (ifp != (ill_if_t *)
4093 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4094 			avl_tree = &ifp->illif_avl_by_ppa;
4095 			ill = avl_first(avl_tree);
4096 			/*
4097 			 * ill is guaranteed to be non NULL or ifp should have
4098 			 * not existed.
4099 			 */
4100 			ASSERT(ill != NULL);
4101 			return (ill);
4102 		}
4103 		ctx->ctx_current_list++;
4104 	}
4105 
4106 	return (NULL);
4107 }
4108 
4109 /*
4110  * returns the next ill in the list. ill_first() must have been called
4111  * before calling ill_next() or bad things will happen.
4112  */
4113 
4114 /*
4115  * We don't check for CONDEMNED ills here. Caller must do that if
4116  * necessary under the ill lock.
4117  */
4118 ill_t *
4119 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4120 {
4121 	ill_if_t *ifp;
4122 	ill_t *ill;
4123 	ip_stack_t	*ipst = lastill->ill_ipst;
4124 
4125 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4126 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4127 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4128 	    AVL_AFTER)) != NULL) {
4129 		return (ill);
4130 	}
4131 
4132 	/* goto next ill_ifp in the list. */
4133 	ifp = lastill->ill_ifptr->illif_next;
4134 
4135 	/* make sure not at end of circular list */
4136 	while (ifp ==
4137 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4138 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4139 			return (NULL);
4140 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4141 	}
4142 
4143 	return (avl_first(&ifp->illif_avl_by_ppa));
4144 }
4145 
4146 /*
4147  * Check interface name for correct format which is name+ppa.
4148  * name can contain characters and digits, the right most digits
4149  * make up the ppa number. use of octal is not allowed, name must contain
4150  * a ppa, return pointer to the start of ppa.
4151  * In case of error return NULL.
4152  */
4153 static char *
4154 ill_get_ppa_ptr(char *name)
4155 {
4156 	int namelen = mi_strlen(name);
4157 
4158 	int len = namelen;
4159 
4160 	name += len;
4161 	while (len > 0) {
4162 		name--;
4163 		if (*name < '0' || *name > '9')
4164 			break;
4165 		len--;
4166 	}
4167 
4168 	/* empty string, all digits, or no trailing digits */
4169 	if (len == 0 || len == (int)namelen)
4170 		return (NULL);
4171 
4172 	name++;
4173 	/* check for attempted use of octal */
4174 	if (*name == '0' && len != (int)namelen - 1)
4175 		return (NULL);
4176 	return (name);
4177 }
4178 
4179 /*
4180  * use avl tree to locate the ill.
4181  */
4182 static ill_t *
4183 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4184     ipsq_func_t func, int *error, ip_stack_t *ipst)
4185 {
4186 	char *ppa_ptr = NULL;
4187 	int len;
4188 	uint_t ppa;
4189 	ill_t *ill = NULL;
4190 	ill_if_t *ifp;
4191 	int list;
4192 	ipsq_t *ipsq;
4193 
4194 	if (error != NULL)
4195 		*error = 0;
4196 
4197 	/*
4198 	 * get ppa ptr
4199 	 */
4200 	if (isv6)
4201 		list = IP_V6_G_HEAD;
4202 	else
4203 		list = IP_V4_G_HEAD;
4204 
4205 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4206 		if (error != NULL)
4207 			*error = ENXIO;
4208 		return (NULL);
4209 	}
4210 
4211 	len = ppa_ptr - name + 1;
4212 
4213 	ppa = stoi(&ppa_ptr);
4214 
4215 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4216 
4217 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4218 		/*
4219 		 * match is done on len - 1 as the name is not null
4220 		 * terminated it contains ppa in addition to the interface
4221 		 * name.
4222 		 */
4223 		if ((ifp->illif_name_len == len) &&
4224 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4225 			break;
4226 		} else {
4227 			ifp = ifp->illif_next;
4228 		}
4229 	}
4230 
4231 
4232 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4233 		/*
4234 		 * Even the interface type does not exist.
4235 		 */
4236 		if (error != NULL)
4237 			*error = ENXIO;
4238 		return (NULL);
4239 	}
4240 
4241 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4242 	if (ill != NULL) {
4243 		/*
4244 		 * The block comment at the start of ipif_down
4245 		 * explains the use of the macros used below
4246 		 */
4247 		GRAB_CONN_LOCK(q);
4248 		mutex_enter(&ill->ill_lock);
4249 		if (ILL_CAN_LOOKUP(ill)) {
4250 			ill_refhold_locked(ill);
4251 			mutex_exit(&ill->ill_lock);
4252 			RELEASE_CONN_LOCK(q);
4253 			return (ill);
4254 		} else if (ILL_CAN_WAIT(ill, q)) {
4255 			ipsq = ill->ill_phyint->phyint_ipsq;
4256 			mutex_enter(&ipsq->ipsq_lock);
4257 			mutex_exit(&ill->ill_lock);
4258 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4259 			mutex_exit(&ipsq->ipsq_lock);
4260 			RELEASE_CONN_LOCK(q);
4261 			if (error != NULL)
4262 				*error = EINPROGRESS;
4263 			return (NULL);
4264 		}
4265 		mutex_exit(&ill->ill_lock);
4266 		RELEASE_CONN_LOCK(q);
4267 	}
4268 	if (error != NULL)
4269 		*error = ENXIO;
4270 	return (NULL);
4271 }
4272 
4273 /*
4274  * comparison function for use with avl.
4275  */
4276 static int
4277 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4278 {
4279 	uint_t ppa;
4280 	uint_t ill_ppa;
4281 
4282 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4283 
4284 	ppa = *((uint_t *)ppa_ptr);
4285 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4286 	/*
4287 	 * We want the ill with the lowest ppa to be on the
4288 	 * top.
4289 	 */
4290 	if (ill_ppa < ppa)
4291 		return (1);
4292 	if (ill_ppa > ppa)
4293 		return (-1);
4294 	return (0);
4295 }
4296 
4297 /*
4298  * remove an interface type from the global list.
4299  */
4300 static void
4301 ill_delete_interface_type(ill_if_t *interface)
4302 {
4303 	ASSERT(interface != NULL);
4304 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4305 
4306 	avl_destroy(&interface->illif_avl_by_ppa);
4307 	if (interface->illif_ppa_arena != NULL)
4308 		vmem_destroy(interface->illif_ppa_arena);
4309 
4310 	remque(interface);
4311 
4312 	mi_free(interface);
4313 }
4314 
4315 /*
4316  * remove ill from the global list.
4317  */
4318 static void
4319 ill_glist_delete(ill_t *ill)
4320 {
4321 	hook_nic_event_t *info;
4322 	ip_stack_t	*ipst;
4323 
4324 	if (ill == NULL)
4325 		return;
4326 	ipst = ill->ill_ipst;
4327 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4328 
4329 	/*
4330 	 * If the ill was never inserted into the AVL tree
4331 	 * we skip the if branch.
4332 	 */
4333 	if (ill->ill_ifptr != NULL) {
4334 		/*
4335 		 * remove from AVL tree and free ppa number
4336 		 */
4337 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4338 
4339 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4340 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4341 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4342 		}
4343 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4344 			ill_delete_interface_type(ill->ill_ifptr);
4345 		}
4346 
4347 		/*
4348 		 * Indicate ill is no longer in the list.
4349 		 */
4350 		ill->ill_ifptr = NULL;
4351 		ill->ill_name_length = 0;
4352 		ill->ill_name[0] = '\0';
4353 		ill->ill_ppa = UINT_MAX;
4354 	}
4355 
4356 	/*
4357 	 * Run the unplumb hook after the NIC has disappeared from being
4358 	 * visible so that attempts to revalidate its existance will fail.
4359 	 *
4360 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4361 	 * that the ordering of delivered events to listeners matches the
4362 	 * order of them in the kernel.
4363 	 */
4364 	info = ill->ill_nic_event_info;
4365 	if (info != NULL && info->hne_event == NE_DOWN) {
4366 		mutex_enter(&ill->ill_lock);
4367 		ill_nic_info_dispatch(ill);
4368 		mutex_exit(&ill->ill_lock);
4369 	}
4370 
4371 	/* Generate NE_UNPLUMB event for ill_name. */
4372 	(void) ill_hook_event_create(ill, 0, NE_UNPLUMB, ill->ill_name,
4373 	    ill->ill_name_length);
4374 
4375 	ill_phyint_free(ill);
4376 	rw_exit(&ipst->ips_ill_g_lock);
4377 }
4378 
4379 /*
4380  * allocate a ppa, if the number of plumbed interfaces of this type are
4381  * less than ill_no_arena do a linear search to find a unused ppa.
4382  * When the number goes beyond ill_no_arena switch to using an arena.
4383  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4384  * is the return value for an error condition, so allocation starts at one
4385  * and is decremented by one.
4386  */
4387 static int
4388 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4389 {
4390 	ill_t *tmp_ill;
4391 	uint_t start, end;
4392 	int ppa;
4393 
4394 	if (ifp->illif_ppa_arena == NULL &&
4395 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4396 		/*
4397 		 * Create an arena.
4398 		 */
4399 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4400 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4401 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4402 			/* allocate what has already been assigned */
4403 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4404 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4405 		    tmp_ill, AVL_AFTER)) {
4406 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4407 			    1,		/* size */
4408 			    1,		/* align/quantum */
4409 			    0,		/* phase */
4410 			    0,		/* nocross */
4411 			    /* minaddr */
4412 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4413 			    /* maxaddr */
4414 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4415 			    VM_NOSLEEP|VM_FIRSTFIT);
4416 			if (ppa == 0) {
4417 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4418 				    " failed while switching"));
4419 				vmem_destroy(ifp->illif_ppa_arena);
4420 				ifp->illif_ppa_arena = NULL;
4421 				break;
4422 			}
4423 		}
4424 	}
4425 
4426 	if (ifp->illif_ppa_arena != NULL) {
4427 		if (ill->ill_ppa == UINT_MAX) {
4428 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4429 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4430 			if (ppa == 0)
4431 				return (EAGAIN);
4432 			ill->ill_ppa = --ppa;
4433 		} else {
4434 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4435 			    1, 		/* size */
4436 			    1, 		/* align/quantum */
4437 			    0, 		/* phase */
4438 			    0, 		/* nocross */
4439 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4440 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4441 			    VM_NOSLEEP|VM_FIRSTFIT);
4442 			/*
4443 			 * Most likely the allocation failed because
4444 			 * the requested ppa was in use.
4445 			 */
4446 			if (ppa == 0)
4447 				return (EEXIST);
4448 		}
4449 		return (0);
4450 	}
4451 
4452 	/*
4453 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4454 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4455 	 */
4456 	if (ill->ill_ppa == UINT_MAX) {
4457 		end = UINT_MAX - 1;
4458 		start = 0;
4459 	} else {
4460 		end = start = ill->ill_ppa;
4461 	}
4462 
4463 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4464 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4465 		if (start++ >= end) {
4466 			if (ill->ill_ppa == UINT_MAX)
4467 				return (EAGAIN);
4468 			else
4469 				return (EEXIST);
4470 		}
4471 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4472 	}
4473 	ill->ill_ppa = start;
4474 	return (0);
4475 }
4476 
4477 /*
4478  * Insert ill into the list of configured ill's. Once this function completes,
4479  * the ill is globally visible and is available through lookups. More precisely
4480  * this happens after the caller drops the ill_g_lock.
4481  */
4482 static int
4483 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4484 {
4485 	ill_if_t *ill_interface;
4486 	avl_index_t where = 0;
4487 	int error;
4488 	int name_length;
4489 	int index;
4490 	boolean_t check_length = B_FALSE;
4491 	ip_stack_t	*ipst = ill->ill_ipst;
4492 
4493 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4494 
4495 	name_length = mi_strlen(name) + 1;
4496 
4497 	if (isv6)
4498 		index = IP_V6_G_HEAD;
4499 	else
4500 		index = IP_V4_G_HEAD;
4501 
4502 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4503 	/*
4504 	 * Search for interface type based on name
4505 	 */
4506 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4507 		if ((ill_interface->illif_name_len == name_length) &&
4508 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4509 			break;
4510 		}
4511 		ill_interface = ill_interface->illif_next;
4512 	}
4513 
4514 	/*
4515 	 * Interface type not found, create one.
4516 	 */
4517 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4518 
4519 		ill_g_head_t ghead;
4520 
4521 		/*
4522 		 * allocate ill_if_t structure
4523 		 */
4524 
4525 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4526 		if (ill_interface == NULL) {
4527 			return (ENOMEM);
4528 		}
4529 
4530 
4531 
4532 		(void) strcpy(ill_interface->illif_name, name);
4533 		ill_interface->illif_name_len = name_length;
4534 
4535 		avl_create(&ill_interface->illif_avl_by_ppa,
4536 		    ill_compare_ppa, sizeof (ill_t),
4537 		    offsetof(struct ill_s, ill_avl_byppa));
4538 
4539 		/*
4540 		 * link the structure in the back to maintain order
4541 		 * of configuration for ifconfig output.
4542 		 */
4543 		ghead = ipst->ips_ill_g_heads[index];
4544 		insque(ill_interface, ghead.ill_g_list_tail);
4545 
4546 	}
4547 
4548 	if (ill->ill_ppa == UINT_MAX)
4549 		check_length = B_TRUE;
4550 
4551 	error = ill_alloc_ppa(ill_interface, ill);
4552 	if (error != 0) {
4553 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4554 			ill_delete_interface_type(ill->ill_ifptr);
4555 		return (error);
4556 	}
4557 
4558 	/*
4559 	 * When the ppa is choosen by the system, check that there is
4560 	 * enough space to insert ppa. if a specific ppa was passed in this
4561 	 * check is not required as the interface name passed in will have
4562 	 * the right ppa in it.
4563 	 */
4564 	if (check_length) {
4565 		/*
4566 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4567 		 */
4568 		char buf[sizeof (uint_t) * 3];
4569 
4570 		/*
4571 		 * convert ppa to string to calculate the amount of space
4572 		 * required for it in the name.
4573 		 */
4574 		numtos(ill->ill_ppa, buf);
4575 
4576 		/* Do we have enough space to insert ppa ? */
4577 
4578 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4579 			/* Free ppa and interface type struct */
4580 			if (ill_interface->illif_ppa_arena != NULL) {
4581 				vmem_free(ill_interface->illif_ppa_arena,
4582 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4583 			}
4584 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4585 			    0) {
4586 				ill_delete_interface_type(ill->ill_ifptr);
4587 			}
4588 
4589 			return (EINVAL);
4590 		}
4591 	}
4592 
4593 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4594 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4595 
4596 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4597 	    &where);
4598 	ill->ill_ifptr = ill_interface;
4599 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4600 
4601 	ill_phyint_reinit(ill);
4602 	return (0);
4603 }
4604 
4605 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4606 static boolean_t
4607 ipsq_init(ill_t *ill)
4608 {
4609 	ipsq_t  *ipsq;
4610 
4611 	/* Init the ipsq and impicitly enter as writer */
4612 	ill->ill_phyint->phyint_ipsq =
4613 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4614 	if (ill->ill_phyint->phyint_ipsq == NULL)
4615 		return (B_FALSE);
4616 	ipsq = ill->ill_phyint->phyint_ipsq;
4617 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4618 	ill->ill_phyint->phyint_ipsq_next = NULL;
4619 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4620 	ipsq->ipsq_refs = 1;
4621 	ipsq->ipsq_writer = curthread;
4622 	ipsq->ipsq_reentry_cnt = 1;
4623 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4624 #ifdef DEBUG
4625 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4626 	    IPSQ_STACK_DEPTH);
4627 #endif
4628 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4629 	return (B_TRUE);
4630 }
4631 
4632 /*
4633  * ill_init is called by ip_open when a device control stream is opened.
4634  * It does a few initializations, and shoots a DL_INFO_REQ message down
4635  * to the driver.  The response is later picked up in ip_rput_dlpi and
4636  * used to set up default mechanisms for talking to the driver.  (Always
4637  * called as writer.)
4638  *
4639  * If this function returns error, ip_open will call ip_close which in
4640  * turn will call ill_delete to clean up any memory allocated here that
4641  * is not yet freed.
4642  */
4643 int
4644 ill_init(queue_t *q, ill_t *ill)
4645 {
4646 	int	count;
4647 	dl_info_req_t	*dlir;
4648 	mblk_t	*info_mp;
4649 	uchar_t *frag_ptr;
4650 
4651 	/*
4652 	 * The ill is initialized to zero by mi_alloc*(). In addition
4653 	 * some fields already contain valid values, initialized in
4654 	 * ip_open(), before we reach here.
4655 	 */
4656 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4657 
4658 	ill->ill_rq = q;
4659 	ill->ill_wq = WR(q);
4660 
4661 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4662 	    BPRI_HI);
4663 	if (info_mp == NULL)
4664 		return (ENOMEM);
4665 
4666 	/*
4667 	 * Allocate sufficient space to contain our fragment hash table and
4668 	 * the device name.
4669 	 */
4670 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4671 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4672 	if (frag_ptr == NULL) {
4673 		freemsg(info_mp);
4674 		return (ENOMEM);
4675 	}
4676 	ill->ill_frag_ptr = frag_ptr;
4677 	ill->ill_frag_free_num_pkts = 0;
4678 	ill->ill_last_frag_clean_time = 0;
4679 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4680 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4681 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4682 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4683 		    NULL, MUTEX_DEFAULT, NULL);
4684 	}
4685 
4686 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4687 	if (ill->ill_phyint == NULL) {
4688 		freemsg(info_mp);
4689 		mi_free(frag_ptr);
4690 		return (ENOMEM);
4691 	}
4692 
4693 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4694 	/*
4695 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4696 	 * at this point because of the following reason. If we can't
4697 	 * enter the ipsq at some point and cv_wait, the writer that
4698 	 * wakes us up tries to locate us using the list of all phyints
4699 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4700 	 * If we don't set it now, we risk a missed wakeup.
4701 	 */
4702 	ill->ill_phyint->phyint_illv4 = ill;
4703 	ill->ill_ppa = UINT_MAX;
4704 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4705 
4706 	if (!ipsq_init(ill)) {
4707 		freemsg(info_mp);
4708 		mi_free(frag_ptr);
4709 		mi_free(ill->ill_phyint);
4710 		return (ENOMEM);
4711 	}
4712 
4713 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4714 
4715 
4716 	/* Frag queue limit stuff */
4717 	ill->ill_frag_count = 0;
4718 	ill->ill_ipf_gen = 0;
4719 
4720 	ill->ill_global_timer = INFINITY;
4721 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4722 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4723 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4724 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4725 
4726 	/*
4727 	 * Initialize IPv6 configuration variables.  The IP module is always
4728 	 * opened as an IPv4 module.  Instead tracking down the cases where
4729 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4730 	 * here for convenience, this has no effect until the ill is set to do
4731 	 * IPv6.
4732 	 */
4733 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4734 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4735 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4736 	ill->ill_max_buf = ND_MAX_Q;
4737 	ill->ill_refcnt = 0;
4738 
4739 	/* Send down the Info Request to the driver. */
4740 	info_mp->b_datap->db_type = M_PCPROTO;
4741 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4742 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4743 	dlir->dl_primitive = DL_INFO_REQ;
4744 
4745 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4746 
4747 	qprocson(q);
4748 	ill_dlpi_send(ill, info_mp);
4749 
4750 	return (0);
4751 }
4752 
4753 /*
4754  * ill_dls_info
4755  * creates datalink socket info from the device.
4756  */
4757 int
4758 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4759 {
4760 	size_t	len;
4761 	ill_t	*ill = ipif->ipif_ill;
4762 
4763 	sdl->sdl_family = AF_LINK;
4764 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4765 	sdl->sdl_type = ill->ill_type;
4766 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4767 	len = strlen(sdl->sdl_data);
4768 	ASSERT(len < 256);
4769 	sdl->sdl_nlen = (uchar_t)len;
4770 	sdl->sdl_alen = ill->ill_phys_addr_length;
4771 	sdl->sdl_slen = 0;
4772 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4773 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4774 
4775 	return (sizeof (struct sockaddr_dl));
4776 }
4777 
4778 /*
4779  * ill_xarp_info
4780  * creates xarp info from the device.
4781  */
4782 static int
4783 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4784 {
4785 	sdl->sdl_family = AF_LINK;
4786 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4787 	sdl->sdl_type = ill->ill_type;
4788 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4789 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4790 	sdl->sdl_alen = ill->ill_phys_addr_length;
4791 	sdl->sdl_slen = 0;
4792 	return (sdl->sdl_nlen);
4793 }
4794 
4795 static int
4796 loopback_kstat_update(kstat_t *ksp, int rw)
4797 {
4798 	kstat_named_t *kn;
4799 	netstackid_t	stackid;
4800 	netstack_t	*ns;
4801 	ip_stack_t	*ipst;
4802 
4803 	if (ksp == NULL || ksp->ks_data == NULL)
4804 		return (EIO);
4805 
4806 	if (rw == KSTAT_WRITE)
4807 		return (EACCES);
4808 
4809 	kn = KSTAT_NAMED_PTR(ksp);
4810 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4811 
4812 	ns = netstack_find_by_stackid(stackid);
4813 	if (ns == NULL)
4814 		return (-1);
4815 
4816 	ipst = ns->netstack_ip;
4817 	if (ipst == NULL) {
4818 		netstack_rele(ns);
4819 		return (-1);
4820 	}
4821 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4822 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4823 	netstack_rele(ns);
4824 	return (0);
4825 }
4826 
4827 
4828 /*
4829  * Has ifindex been plumbed already.
4830  * Compares both phyint_ifindex and phyint_group_ifindex.
4831  */
4832 static boolean_t
4833 phyint_exists(uint_t index, ip_stack_t *ipst)
4834 {
4835 	phyint_t *phyi;
4836 
4837 	ASSERT(index != 0);
4838 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4839 	/*
4840 	 * Indexes are stored in the phyint - a common structure
4841 	 * to both IPv4 and IPv6.
4842 	 */
4843 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4844 	for (; phyi != NULL;
4845 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4846 	    phyi, AVL_AFTER)) {
4847 		if (phyi->phyint_ifindex == index ||
4848 		    phyi->phyint_group_ifindex == index)
4849 			return (B_TRUE);
4850 	}
4851 	return (B_FALSE);
4852 }
4853 
4854 /* Pick a unique ifindex */
4855 boolean_t
4856 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4857 {
4858 	uint_t starting_index;
4859 
4860 	if (!ipst->ips_ill_index_wrap) {
4861 		*indexp = ipst->ips_ill_index++;
4862 		if (ipst->ips_ill_index == 0) {
4863 			/* Reached the uint_t limit Next time wrap  */
4864 			ipst->ips_ill_index_wrap = B_TRUE;
4865 		}
4866 		return (B_TRUE);
4867 	}
4868 
4869 	/*
4870 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4871 	 * at this point and don't want to call any function that attempts
4872 	 * to get the lock again.
4873 	 */
4874 	starting_index = ipst->ips_ill_index++;
4875 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4876 		if (ipst->ips_ill_index != 0 &&
4877 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4878 			/* found unused index - use it */
4879 			*indexp = ipst->ips_ill_index;
4880 			return (B_TRUE);
4881 		}
4882 	}
4883 
4884 	/*
4885 	 * all interface indicies are inuse.
4886 	 */
4887 	return (B_FALSE);
4888 }
4889 
4890 /*
4891  * Assign a unique interface index for the phyint.
4892  */
4893 static boolean_t
4894 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4895 {
4896 	ASSERT(phyi->phyint_ifindex == 0);
4897 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4898 }
4899 
4900 /*
4901  * Return a pointer to the ill which matches the supplied name.  Note that
4902  * the ill name length includes the null termination character.  (May be
4903  * called as writer.)
4904  * If do_alloc and the interface is "lo0" it will be automatically created.
4905  * Cannot bump up reference on condemned ills. So dup detect can't be done
4906  * using this func.
4907  */
4908 ill_t *
4909 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4910     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4911     ip_stack_t *ipst)
4912 {
4913 	ill_t	*ill;
4914 	ipif_t	*ipif;
4915 	kstat_named_t	*kn;
4916 	boolean_t isloopback;
4917 	ipsq_t *old_ipsq;
4918 	in6_addr_t ov6addr;
4919 
4920 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4921 
4922 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4923 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4924 	rw_exit(&ipst->ips_ill_g_lock);
4925 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4926 		return (ill);
4927 
4928 	/*
4929 	 * Couldn't find it.  Does this happen to be a lookup for the
4930 	 * loopback device and are we allowed to allocate it?
4931 	 */
4932 	if (!isloopback || !do_alloc)
4933 		return (NULL);
4934 
4935 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4936 
4937 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4938 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4939 		rw_exit(&ipst->ips_ill_g_lock);
4940 		return (ill);
4941 	}
4942 
4943 	/* Create the loopback device on demand */
4944 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4945 	    sizeof (ipif_loopback_name), BPRI_MED));
4946 	if (ill == NULL)
4947 		goto done;
4948 
4949 	*ill = ill_null;
4950 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4951 	ill->ill_ipst = ipst;
4952 	netstack_hold(ipst->ips_netstack);
4953 	/*
4954 	 * For exclusive stacks we set the zoneid to zero
4955 	 * to make IP operate as if in the global zone.
4956 	 */
4957 	ill->ill_zoneid = GLOBAL_ZONEID;
4958 
4959 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4960 	if (ill->ill_phyint == NULL)
4961 		goto done;
4962 
4963 	if (isv6)
4964 		ill->ill_phyint->phyint_illv6 = ill;
4965 	else
4966 		ill->ill_phyint->phyint_illv4 = ill;
4967 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4968 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4969 	/* Add room for tcp+ip headers */
4970 	if (isv6) {
4971 		ill->ill_isv6 = B_TRUE;
4972 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4973 	} else {
4974 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4975 	}
4976 	if (!ill_allocate_mibs(ill))
4977 		goto done;
4978 	ill->ill_max_mtu = ill->ill_max_frag;
4979 	/*
4980 	 * ipif_loopback_name can't be pointed at directly because its used
4981 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4982 	 * from the glist, ill_glist_delete() sets the first character of
4983 	 * ill_name to '\0'.
4984 	 */
4985 	ill->ill_name = (char *)ill + sizeof (*ill);
4986 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4987 	ill->ill_name_length = sizeof (ipif_loopback_name);
4988 	/* Set ill_name_set for ill_phyint_reinit to work properly */
4989 
4990 	ill->ill_global_timer = INFINITY;
4991 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4992 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4993 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4994 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4995 
4996 	/* No resolver here. */
4997 	ill->ill_net_type = IRE_LOOPBACK;
4998 
4999 	/* Initialize the ipsq */
5000 	if (!ipsq_init(ill))
5001 		goto done;
5002 
5003 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5004 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5005 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5006 #ifdef DEBUG
5007 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5008 #endif
5009 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5010 	if (ipif == NULL)
5011 		goto done;
5012 
5013 	ill->ill_flags = ILLF_MULTICAST;
5014 
5015 	ov6addr = ipif->ipif_v6lcl_addr;
5016 	/* Set up default loopback address and mask. */
5017 	if (!isv6) {
5018 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5019 
5020 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5021 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5022 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5023 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5024 		    ipif->ipif_v6subnet);
5025 		ill->ill_flags |= ILLF_IPV4;
5026 	} else {
5027 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5028 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5029 		ipif->ipif_v6net_mask = ipv6_all_ones;
5030 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5031 		    ipif->ipif_v6subnet);
5032 		ill->ill_flags |= ILLF_IPV6;
5033 	}
5034 
5035 	/*
5036 	 * Chain us in at the end of the ill list. hold the ill
5037 	 * before we make it globally visible. 1 for the lookup.
5038 	 */
5039 	ill->ill_refcnt = 0;
5040 	ill_refhold(ill);
5041 
5042 	ill->ill_frag_count = 0;
5043 	ill->ill_frag_free_num_pkts = 0;
5044 	ill->ill_last_frag_clean_time = 0;
5045 
5046 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5047 
5048 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5049 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5050 
5051 	/* Let SCTP know so that it can add this to its list */
5052 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5053 
5054 	/*
5055 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5056 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5057 	 * requires to be after ill_glist_insert() since we need the
5058 	 * ill_index set. Pass on ipv6_loopback as the old address.
5059 	 */
5060 	sctp_update_ipif_addr(ipif, ov6addr);
5061 
5062 	/*
5063 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5064 	 */
5065 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5066 		/* Loopback ills aren't in any IPMP group */
5067 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5068 		ipsq_delete(old_ipsq);
5069 	}
5070 
5071 	/*
5072 	 * Delay this till the ipif is allocated as ipif_allocate
5073 	 * de-references ill_phyint for getting the ifindex. We
5074 	 * can't do this before ipif_allocate because ill_phyint_reinit
5075 	 * -> phyint_assign_ifindex expects ipif to be present.
5076 	 */
5077 	mutex_enter(&ill->ill_phyint->phyint_lock);
5078 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5079 	mutex_exit(&ill->ill_phyint->phyint_lock);
5080 
5081 	if (ipst->ips_loopback_ksp == NULL) {
5082 		/* Export loopback interface statistics */
5083 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5084 		    ipif_loopback_name, "net",
5085 		    KSTAT_TYPE_NAMED, 2, 0,
5086 		    ipst->ips_netstack->netstack_stackid);
5087 		if (ipst->ips_loopback_ksp != NULL) {
5088 			ipst->ips_loopback_ksp->ks_update =
5089 			    loopback_kstat_update;
5090 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5091 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5092 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5093 			ipst->ips_loopback_ksp->ks_private =
5094 			    (void *)(uintptr_t)ipst->ips_netstack->
5095 			    netstack_stackid;
5096 			kstat_install(ipst->ips_loopback_ksp);
5097 		}
5098 	}
5099 
5100 	if (error != NULL)
5101 		*error = 0;
5102 	*did_alloc = B_TRUE;
5103 	rw_exit(&ipst->ips_ill_g_lock);
5104 	return (ill);
5105 done:
5106 	if (ill != NULL) {
5107 		if (ill->ill_phyint != NULL) {
5108 			ipsq_t	*ipsq;
5109 
5110 			ipsq = ill->ill_phyint->phyint_ipsq;
5111 			if (ipsq != NULL) {
5112 				ipsq->ipsq_ipst = NULL;
5113 				kmem_free(ipsq, sizeof (ipsq_t));
5114 			}
5115 			mi_free(ill->ill_phyint);
5116 		}
5117 		ill_free_mib(ill);
5118 		if (ill->ill_ipst != NULL)
5119 			netstack_rele(ill->ill_ipst->ips_netstack);
5120 		mi_free(ill);
5121 	}
5122 	rw_exit(&ipst->ips_ill_g_lock);
5123 	if (error != NULL)
5124 		*error = ENOMEM;
5125 	return (NULL);
5126 }
5127 
5128 /*
5129  * For IPP calls - use the ip_stack_t for global stack.
5130  */
5131 ill_t *
5132 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5133     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5134 {
5135 	ip_stack_t	*ipst;
5136 	ill_t		*ill;
5137 
5138 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5139 	if (ipst == NULL) {
5140 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5141 		return (NULL);
5142 	}
5143 
5144 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5145 	netstack_rele(ipst->ips_netstack);
5146 	return (ill);
5147 }
5148 
5149 /*
5150  * Return a pointer to the ill which matches the index and IP version type.
5151  */
5152 ill_t *
5153 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5154     ipsq_func_t func, int *err, ip_stack_t *ipst)
5155 {
5156 	ill_t	*ill;
5157 	ipsq_t  *ipsq;
5158 	phyint_t *phyi;
5159 
5160 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5161 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5162 
5163 	if (err != NULL)
5164 		*err = 0;
5165 
5166 	/*
5167 	 * Indexes are stored in the phyint - a common structure
5168 	 * to both IPv4 and IPv6.
5169 	 */
5170 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5171 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5172 	    (void *) &index, NULL);
5173 	if (phyi != NULL) {
5174 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5175 		if (ill != NULL) {
5176 			/*
5177 			 * The block comment at the start of ipif_down
5178 			 * explains the use of the macros used below
5179 			 */
5180 			GRAB_CONN_LOCK(q);
5181 			mutex_enter(&ill->ill_lock);
5182 			if (ILL_CAN_LOOKUP(ill)) {
5183 				ill_refhold_locked(ill);
5184 				mutex_exit(&ill->ill_lock);
5185 				RELEASE_CONN_LOCK(q);
5186 				rw_exit(&ipst->ips_ill_g_lock);
5187 				return (ill);
5188 			} else if (ILL_CAN_WAIT(ill, q)) {
5189 				ipsq = ill->ill_phyint->phyint_ipsq;
5190 				mutex_enter(&ipsq->ipsq_lock);
5191 				rw_exit(&ipst->ips_ill_g_lock);
5192 				mutex_exit(&ill->ill_lock);
5193 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5194 				mutex_exit(&ipsq->ipsq_lock);
5195 				RELEASE_CONN_LOCK(q);
5196 				if (err != NULL)
5197 					*err = EINPROGRESS;
5198 				return (NULL);
5199 			}
5200 			RELEASE_CONN_LOCK(q);
5201 			mutex_exit(&ill->ill_lock);
5202 		}
5203 	}
5204 	rw_exit(&ipst->ips_ill_g_lock);
5205 	if (err != NULL)
5206 		*err = ENXIO;
5207 	return (NULL);
5208 }
5209 
5210 /*
5211  * Return the ifindex next in sequence after the passed in ifindex.
5212  * If there is no next ifindex for the given protocol, return 0.
5213  */
5214 uint_t
5215 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5216 {
5217 	phyint_t *phyi;
5218 	phyint_t *phyi_initial;
5219 	uint_t   ifindex;
5220 
5221 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5222 
5223 	if (index == 0) {
5224 		phyi = avl_first(
5225 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5226 	} else {
5227 		phyi = phyi_initial = avl_find(
5228 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5229 		    (void *) &index, NULL);
5230 	}
5231 
5232 	for (; phyi != NULL;
5233 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5234 	    phyi, AVL_AFTER)) {
5235 		/*
5236 		 * If we're not returning the first interface in the tree
5237 		 * and we still haven't moved past the phyint_t that
5238 		 * corresponds to index, avl_walk needs to be called again
5239 		 */
5240 		if (!((index != 0) && (phyi == phyi_initial))) {
5241 			if (isv6) {
5242 				if ((phyi->phyint_illv6) &&
5243 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5244 				    (phyi->phyint_illv6->ill_isv6 == 1))
5245 					break;
5246 			} else {
5247 				if ((phyi->phyint_illv4) &&
5248 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5249 				    (phyi->phyint_illv4->ill_isv6 == 0))
5250 					break;
5251 			}
5252 		}
5253 	}
5254 
5255 	rw_exit(&ipst->ips_ill_g_lock);
5256 
5257 	if (phyi != NULL)
5258 		ifindex = phyi->phyint_ifindex;
5259 	else
5260 		ifindex = 0;
5261 
5262 	return (ifindex);
5263 }
5264 
5265 
5266 /*
5267  * Return the ifindex for the named interface.
5268  * If there is no next ifindex for the interface, return 0.
5269  */
5270 uint_t
5271 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5272 {
5273 	phyint_t	*phyi;
5274 	avl_index_t	where = 0;
5275 	uint_t		ifindex;
5276 
5277 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5278 
5279 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5280 	    name, &where)) == NULL) {
5281 		rw_exit(&ipst->ips_ill_g_lock);
5282 		return (0);
5283 	}
5284 
5285 	ifindex = phyi->phyint_ifindex;
5286 
5287 	rw_exit(&ipst->ips_ill_g_lock);
5288 
5289 	return (ifindex);
5290 }
5291 
5292 
5293 /*
5294  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5295  * that gives a running thread a reference to the ill. This reference must be
5296  * released by the thread when it is done accessing the ill and related
5297  * objects. ill_refcnt can not be used to account for static references
5298  * such as other structures pointing to an ill. Callers must generally
5299  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5300  * or be sure that the ill is not being deleted or changing state before
5301  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5302  * ill won't change any of its critical state such as address, netmask etc.
5303  */
5304 void
5305 ill_refhold(ill_t *ill)
5306 {
5307 	mutex_enter(&ill->ill_lock);
5308 	ill->ill_refcnt++;
5309 	ILL_TRACE_REF(ill);
5310 	mutex_exit(&ill->ill_lock);
5311 }
5312 
5313 void
5314 ill_refhold_locked(ill_t *ill)
5315 {
5316 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5317 	ill->ill_refcnt++;
5318 	ILL_TRACE_REF(ill);
5319 }
5320 
5321 int
5322 ill_check_and_refhold(ill_t *ill)
5323 {
5324 	mutex_enter(&ill->ill_lock);
5325 	if (ILL_CAN_LOOKUP(ill)) {
5326 		ill_refhold_locked(ill);
5327 		mutex_exit(&ill->ill_lock);
5328 		return (0);
5329 	}
5330 	mutex_exit(&ill->ill_lock);
5331 	return (ILL_LOOKUP_FAILED);
5332 }
5333 
5334 /*
5335  * Must not be called while holding any locks. Otherwise if this is
5336  * the last reference to be released, there is a chance of recursive mutex
5337  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5338  * to restart an ioctl.
5339  */
5340 void
5341 ill_refrele(ill_t *ill)
5342 {
5343 	mutex_enter(&ill->ill_lock);
5344 	ASSERT(ill->ill_refcnt != 0);
5345 	ill->ill_refcnt--;
5346 	ILL_UNTRACE_REF(ill);
5347 	if (ill->ill_refcnt != 0) {
5348 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5349 		mutex_exit(&ill->ill_lock);
5350 		return;
5351 	}
5352 
5353 	/* Drops the ill_lock */
5354 	ipif_ill_refrele_tail(ill);
5355 }
5356 
5357 /*
5358  * Obtain a weak reference count on the ill. This reference ensures the
5359  * ill won't be freed, but the ill may change any of its critical state
5360  * such as netmask, address etc. Returns an error if the ill has started
5361  * closing.
5362  */
5363 boolean_t
5364 ill_waiter_inc(ill_t *ill)
5365 {
5366 	mutex_enter(&ill->ill_lock);
5367 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5368 		mutex_exit(&ill->ill_lock);
5369 		return (B_FALSE);
5370 	}
5371 	ill->ill_waiters++;
5372 	mutex_exit(&ill->ill_lock);
5373 	return (B_TRUE);
5374 }
5375 
5376 void
5377 ill_waiter_dcr(ill_t *ill)
5378 {
5379 	mutex_enter(&ill->ill_lock);
5380 	ill->ill_waiters--;
5381 	if (ill->ill_waiters == 0)
5382 		cv_broadcast(&ill->ill_cv);
5383 	mutex_exit(&ill->ill_lock);
5384 }
5385 
5386 /*
5387  * Named Dispatch routine to produce a formatted report on all ILLs.
5388  * This report is accessed by using the ndd utility to "get" ND variable
5389  * "ip_ill_status".
5390  */
5391 /* ARGSUSED */
5392 int
5393 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5394 {
5395 	ill_t		*ill;
5396 	ill_walk_context_t ctx;
5397 	ip_stack_t	*ipst;
5398 
5399 	ipst = CONNQ_TO_IPST(q);
5400 
5401 	(void) mi_mpprintf(mp,
5402 	    "ILL      " MI_COL_HDRPAD_STR
5403 	/*   01234567[89ABCDEF] */
5404 	    "rq       " MI_COL_HDRPAD_STR
5405 	/*   01234567[89ABCDEF] */
5406 	    "wq       " MI_COL_HDRPAD_STR
5407 	/*   01234567[89ABCDEF] */
5408 	    "upcnt mxfrg err name");
5409 	/*   12345 12345 123 xxxxxxxx  */
5410 
5411 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5412 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5413 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5414 		(void) mi_mpprintf(mp,
5415 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5416 		    "%05u %05u %03d %s",
5417 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5418 		    ill->ill_ipif_up_count,
5419 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5420 	}
5421 	rw_exit(&ipst->ips_ill_g_lock);
5422 
5423 	return (0);
5424 }
5425 
5426 /*
5427  * Named Dispatch routine to produce a formatted report on all IPIFs.
5428  * This report is accessed by using the ndd utility to "get" ND variable
5429  * "ip_ipif_status".
5430  */
5431 /* ARGSUSED */
5432 int
5433 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5434 {
5435 	char	buf1[INET6_ADDRSTRLEN];
5436 	char	buf2[INET6_ADDRSTRLEN];
5437 	char	buf3[INET6_ADDRSTRLEN];
5438 	char	buf4[INET6_ADDRSTRLEN];
5439 	char	buf5[INET6_ADDRSTRLEN];
5440 	char	buf6[INET6_ADDRSTRLEN];
5441 	char	buf[LIFNAMSIZ];
5442 	ill_t	*ill;
5443 	ipif_t	*ipif;
5444 	nv_t	*nvp;
5445 	uint64_t flags;
5446 	zoneid_t zoneid;
5447 	ill_walk_context_t ctx;
5448 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5449 
5450 	(void) mi_mpprintf(mp,
5451 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5452 	    "\tlocal address\n"
5453 	    "\tsrc address\n"
5454 	    "\tsubnet\n"
5455 	    "\tmask\n"
5456 	    "\tbroadcast\n"
5457 	    "\tp-p-dst");
5458 
5459 	ASSERT(q->q_next == NULL);
5460 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5461 
5462 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5463 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5464 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5465 		for (ipif = ill->ill_ipif; ipif != NULL;
5466 		    ipif = ipif->ipif_next) {
5467 			if (zoneid != GLOBAL_ZONEID &&
5468 			    zoneid != ipif->ipif_zoneid &&
5469 			    ipif->ipif_zoneid != ALL_ZONES)
5470 				continue;
5471 
5472 			ipif_get_name(ipif, buf, sizeof (buf));
5473 			(void) mi_mpprintf(mp,
5474 			    MI_COL_PTRFMT_STR
5475 			    "%04u %05u %u/%u/%u %s %d",
5476 			    (void *)ipif,
5477 			    ipif->ipif_metric, ipif->ipif_mtu,
5478 			    ipif->ipif_ib_pkt_count,
5479 			    ipif->ipif_ob_pkt_count,
5480 			    ipif->ipif_fo_pkt_count,
5481 			    buf,
5482 			    ipif->ipif_zoneid);
5483 
5484 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5485 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5486 
5487 		/* Tack on text strings for any flags. */
5488 		nvp = ipif_nv_tbl;
5489 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5490 			if (nvp->nv_value & flags)
5491 				(void) mi_mpprintf_nr(mp, " %s",
5492 				    nvp->nv_name);
5493 		}
5494 		(void) mi_mpprintf(mp,
5495 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5496 		    inet_ntop(AF_INET6,
5497 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5498 		    inet_ntop(AF_INET6,
5499 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5500 		    inet_ntop(AF_INET6,
5501 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5502 		    inet_ntop(AF_INET6,
5503 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5504 		    inet_ntop(AF_INET6,
5505 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5506 		    inet_ntop(AF_INET6,
5507 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5508 		}
5509 	}
5510 	rw_exit(&ipst->ips_ill_g_lock);
5511 	return (0);
5512 }
5513 
5514 /*
5515  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5516  * driver.  We construct best guess defaults for lower level information that
5517  * we need.  If an interface is brought up without injection of any overriding
5518  * information from outside, we have to be ready to go with these defaults.
5519  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5520  * we primarely want the dl_provider_style.
5521  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5522  * at which point we assume the other part of the information is valid.
5523  */
5524 void
5525 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5526 {
5527 	uchar_t		*brdcst_addr;
5528 	uint_t		brdcst_addr_length, phys_addr_length;
5529 	t_scalar_t	sap_length;
5530 	dl_info_ack_t	*dlia;
5531 	ip_m_t		*ipm;
5532 	dl_qos_cl_sel1_t *sel1;
5533 
5534 	ASSERT(IAM_WRITER_ILL(ill));
5535 
5536 	/*
5537 	 * Till the ill is fully up ILL_CHANGING will be set and
5538 	 * the ill is not globally visible. So no need for a lock.
5539 	 */
5540 	dlia = (dl_info_ack_t *)mp->b_rptr;
5541 	ill->ill_mactype = dlia->dl_mac_type;
5542 
5543 	ipm = ip_m_lookup(dlia->dl_mac_type);
5544 	if (ipm == NULL) {
5545 		ipm = ip_m_lookup(DL_OTHER);
5546 		ASSERT(ipm != NULL);
5547 	}
5548 	ill->ill_media = ipm;
5549 
5550 	/*
5551 	 * When the new DLPI stuff is ready we'll pull lengths
5552 	 * from dlia.
5553 	 */
5554 	if (dlia->dl_version == DL_VERSION_2) {
5555 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5556 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5557 		    brdcst_addr_length);
5558 		if (brdcst_addr == NULL) {
5559 			brdcst_addr_length = 0;
5560 		}
5561 		sap_length = dlia->dl_sap_length;
5562 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5563 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5564 		    brdcst_addr_length, sap_length, phys_addr_length));
5565 	} else {
5566 		brdcst_addr_length = 6;
5567 		brdcst_addr = ip_six_byte_all_ones;
5568 		sap_length = -2;
5569 		phys_addr_length = brdcst_addr_length;
5570 	}
5571 
5572 	ill->ill_bcast_addr_length = brdcst_addr_length;
5573 	ill->ill_phys_addr_length = phys_addr_length;
5574 	ill->ill_sap_length = sap_length;
5575 	ill->ill_max_frag = dlia->dl_max_sdu;
5576 	ill->ill_max_mtu = ill->ill_max_frag;
5577 
5578 	ill->ill_type = ipm->ip_m_type;
5579 
5580 	if (!ill->ill_dlpi_style_set) {
5581 		if (dlia->dl_provider_style == DL_STYLE2)
5582 			ill->ill_needs_attach = 1;
5583 
5584 		/*
5585 		 * Allocate the first ipif on this ill. We don't delay it
5586 		 * further as ioctl handling assumes atleast one ipif to
5587 		 * be present.
5588 		 *
5589 		 * At this point we don't know whether the ill is v4 or v6.
5590 		 * We will know this whan the SIOCSLIFNAME happens and
5591 		 * the correct value for ill_isv6 will be assigned in
5592 		 * ipif_set_values(). We need to hold the ill lock and
5593 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5594 		 * the wakeup.
5595 		 */
5596 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5597 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5598 		mutex_enter(&ill->ill_lock);
5599 		ASSERT(ill->ill_dlpi_style_set == 0);
5600 		ill->ill_dlpi_style_set = 1;
5601 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5602 		cv_broadcast(&ill->ill_cv);
5603 		mutex_exit(&ill->ill_lock);
5604 		freemsg(mp);
5605 		return;
5606 	}
5607 	ASSERT(ill->ill_ipif != NULL);
5608 	/*
5609 	 * We know whether it is IPv4 or IPv6 now, as this is the
5610 	 * second DL_INFO_ACK we are recieving in response to the
5611 	 * DL_INFO_REQ sent in ipif_set_values.
5612 	 */
5613 	if (ill->ill_isv6)
5614 		ill->ill_sap = IP6_DL_SAP;
5615 	else
5616 		ill->ill_sap = IP_DL_SAP;
5617 	/*
5618 	 * Set ipif_mtu which is used to set the IRE's
5619 	 * ire_max_frag value. The driver could have sent
5620 	 * a different mtu from what it sent last time. No
5621 	 * need to call ipif_mtu_change because IREs have
5622 	 * not yet been created.
5623 	 */
5624 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5625 	/*
5626 	 * Clear all the flags that were set based on ill_bcast_addr_length
5627 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5628 	 * changed now and we need to re-evaluate.
5629 	 */
5630 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5631 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5632 
5633 	/*
5634 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5635 	 * changed now.
5636 	 */
5637 	if (ill->ill_bcast_addr_length == 0) {
5638 		if (ill->ill_resolver_mp != NULL)
5639 			freemsg(ill->ill_resolver_mp);
5640 		if (ill->ill_bcast_mp != NULL)
5641 			freemsg(ill->ill_bcast_mp);
5642 		if (ill->ill_flags & ILLF_XRESOLV)
5643 			ill->ill_net_type = IRE_IF_RESOLVER;
5644 		else
5645 			ill->ill_net_type = IRE_IF_NORESOLVER;
5646 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5647 		    ill->ill_phys_addr_length,
5648 		    ill->ill_sap,
5649 		    ill->ill_sap_length);
5650 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5651 
5652 		if (ill->ill_isv6)
5653 			/*
5654 			 * Note: xresolv interfaces will eventually need NOARP
5655 			 * set here as well, but that will require those
5656 			 * external resolvers to have some knowledge of
5657 			 * that flag and act appropriately. Not to be changed
5658 			 * at present.
5659 			 */
5660 			ill->ill_flags |= ILLF_NONUD;
5661 		else
5662 			ill->ill_flags |= ILLF_NOARP;
5663 
5664 		if (ill->ill_phys_addr_length == 0) {
5665 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5666 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5667 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5668 			} else {
5669 				/* pt-pt supports multicast. */
5670 				ill->ill_flags |= ILLF_MULTICAST;
5671 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5672 			}
5673 		}
5674 	} else {
5675 		ill->ill_net_type = IRE_IF_RESOLVER;
5676 		if (ill->ill_bcast_mp != NULL)
5677 			freemsg(ill->ill_bcast_mp);
5678 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5679 		    ill->ill_bcast_addr_length, ill->ill_sap,
5680 		    ill->ill_sap_length);
5681 		/*
5682 		 * Later detect lack of DLPI driver multicast
5683 		 * capability by catching DL_ENABMULTI errors in
5684 		 * ip_rput_dlpi.
5685 		 */
5686 		ill->ill_flags |= ILLF_MULTICAST;
5687 		if (!ill->ill_isv6)
5688 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5689 	}
5690 	/* By default an interface does not support any CoS marking */
5691 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5692 
5693 	/*
5694 	 * If we get QoS information in DL_INFO_ACK, the device supports
5695 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5696 	 */
5697 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5698 	    dlia->dl_qos_length);
5699 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5700 		ill->ill_flags |= ILLF_COS_ENABLED;
5701 	}
5702 
5703 	/* Clear any previous error indication. */
5704 	ill->ill_error = 0;
5705 	freemsg(mp);
5706 }
5707 
5708 /*
5709  * Perform various checks to verify that an address would make sense as a
5710  * local, remote, or subnet interface address.
5711  */
5712 static boolean_t
5713 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5714 {
5715 	ipaddr_t	net_mask;
5716 
5717 	/*
5718 	 * Don't allow all zeroes, or all ones, but allow
5719 	 * all ones netmask.
5720 	 */
5721 	if ((net_mask = ip_net_mask(addr)) == 0)
5722 		return (B_FALSE);
5723 	/* A given netmask overrides the "guess" netmask */
5724 	if (subnet_mask != 0)
5725 		net_mask = subnet_mask;
5726 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5727 	    (addr == (addr | ~net_mask)))) {
5728 		return (B_FALSE);
5729 	}
5730 
5731 	/*
5732 	 * Even if the netmask is all ones, we do not allow address to be
5733 	 * 255.255.255.255
5734 	 */
5735 	if (addr == INADDR_BROADCAST)
5736 		return (B_FALSE);
5737 
5738 	if (CLASSD(addr))
5739 		return (B_FALSE);
5740 
5741 	return (B_TRUE);
5742 }
5743 
5744 #define	V6_IPIF_LINKLOCAL(p)	\
5745 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5746 
5747 /*
5748  * Compare two given ipifs and check if the second one is better than
5749  * the first one using the order of preference (not taking deprecated
5750  * into acount) specified in ipif_lookup_multicast().
5751  */
5752 static boolean_t
5753 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5754 {
5755 	/* Check the least preferred first. */
5756 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5757 		/* If both ipifs are the same, use the first one. */
5758 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5759 			return (B_FALSE);
5760 		else
5761 			return (B_TRUE);
5762 	}
5763 
5764 	/* For IPv6, check for link local address. */
5765 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5766 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5767 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5768 			/* The second one is equal or less preferred. */
5769 			return (B_FALSE);
5770 		} else {
5771 			return (B_TRUE);
5772 		}
5773 	}
5774 
5775 	/* Then check for point to point interface. */
5776 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5777 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5778 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5779 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5780 			return (B_FALSE);
5781 		} else {
5782 			return (B_TRUE);
5783 		}
5784 	}
5785 
5786 	/* old_ipif is a normal interface, so no need to use the new one. */
5787 	return (B_FALSE);
5788 }
5789 
5790 /*
5791  * Find any non-virtual, not condemned, and up multicast capable interface
5792  * given an IP instance and zoneid.  Order of preference is:
5793  *
5794  * 1. normal
5795  * 1.1 normal, but deprecated
5796  * 2. point to point
5797  * 2.1 point to point, but deprecated
5798  * 3. link local
5799  * 3.1 link local, but deprecated
5800  * 4. loopback.
5801  */
5802 ipif_t *
5803 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5804 {
5805 	ill_t			*ill;
5806 	ill_walk_context_t	ctx;
5807 	ipif_t			*ipif;
5808 	ipif_t			*saved_ipif = NULL;
5809 	ipif_t			*dep_ipif = NULL;
5810 
5811 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5812 	if (isv6)
5813 		ill = ILL_START_WALK_V6(&ctx, ipst);
5814 	else
5815 		ill = ILL_START_WALK_V4(&ctx, ipst);
5816 
5817 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5818 		mutex_enter(&ill->ill_lock);
5819 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5820 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5821 			mutex_exit(&ill->ill_lock);
5822 			continue;
5823 		}
5824 		for (ipif = ill->ill_ipif; ipif != NULL;
5825 		    ipif = ipif->ipif_next) {
5826 			if (zoneid != ipif->ipif_zoneid &&
5827 			    zoneid != ALL_ZONES &&
5828 			    ipif->ipif_zoneid != ALL_ZONES) {
5829 				continue;
5830 			}
5831 			if (!(ipif->ipif_flags & IPIF_UP) ||
5832 			    !IPIF_CAN_LOOKUP(ipif)) {
5833 				continue;
5834 			}
5835 
5836 			/*
5837 			 * Found one candidate.  If it is deprecated,
5838 			 * remember it in dep_ipif.  If it is not deprecated,
5839 			 * remember it in saved_ipif.
5840 			 */
5841 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5842 				if (dep_ipif == NULL) {
5843 					dep_ipif = ipif;
5844 				} else if (ipif_comp_multi(dep_ipif, ipif,
5845 				    isv6)) {
5846 					/*
5847 					 * If the previous dep_ipif does not
5848 					 * belong to the same ill, we've done
5849 					 * a ipif_refhold() on it.  So we need
5850 					 * to release it.
5851 					 */
5852 					if (dep_ipif->ipif_ill != ill)
5853 						ipif_refrele(dep_ipif);
5854 					dep_ipif = ipif;
5855 				}
5856 				continue;
5857 			}
5858 			if (saved_ipif == NULL) {
5859 				saved_ipif = ipif;
5860 			} else {
5861 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5862 					if (saved_ipif->ipif_ill != ill)
5863 						ipif_refrele(saved_ipif);
5864 					saved_ipif = ipif;
5865 				}
5866 			}
5867 		}
5868 		/*
5869 		 * Before going to the next ill, do a ipif_refhold() on the
5870 		 * saved ones.
5871 		 */
5872 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5873 			ipif_refhold_locked(saved_ipif);
5874 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5875 			ipif_refhold_locked(dep_ipif);
5876 		mutex_exit(&ill->ill_lock);
5877 	}
5878 	rw_exit(&ipst->ips_ill_g_lock);
5879 
5880 	/*
5881 	 * If we have only the saved_ipif, return it.  But if we have both
5882 	 * saved_ipif and dep_ipif, check to see which one is better.
5883 	 */
5884 	if (saved_ipif != NULL) {
5885 		if (dep_ipif != NULL) {
5886 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5887 				ipif_refrele(saved_ipif);
5888 				return (dep_ipif);
5889 			} else {
5890 				ipif_refrele(dep_ipif);
5891 				return (saved_ipif);
5892 			}
5893 		}
5894 		return (saved_ipif);
5895 	} else {
5896 		return (dep_ipif);
5897 	}
5898 }
5899 
5900 /*
5901  * This function is called when an application does not specify an interface
5902  * to be used for multicast traffic (joining a group/sending data).  It
5903  * calls ire_lookup_multi() to look for an interface route for the
5904  * specified multicast group.  Doing this allows the administrator to add
5905  * prefix routes for multicast to indicate which interface to be used for
5906  * multicast traffic in the above scenario.  The route could be for all
5907  * multicast (224.0/4), for a single multicast group (a /32 route) or
5908  * anything in between.  If there is no such multicast route, we just find
5909  * any multicast capable interface and return it.  The returned ipif
5910  * is refhold'ed.
5911  */
5912 ipif_t *
5913 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5914 {
5915 	ire_t			*ire;
5916 	ipif_t			*ipif;
5917 
5918 	ire = ire_lookup_multi(group, zoneid, ipst);
5919 	if (ire != NULL) {
5920 		ipif = ire->ire_ipif;
5921 		ipif_refhold(ipif);
5922 		ire_refrele(ire);
5923 		return (ipif);
5924 	}
5925 
5926 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5927 }
5928 
5929 /*
5930  * Look for an ipif with the specified interface address and destination.
5931  * The destination address is used only for matching point-to-point interfaces.
5932  */
5933 ipif_t *
5934 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5935     ipsq_func_t func, int *error, ip_stack_t *ipst)
5936 {
5937 	ipif_t	*ipif;
5938 	ill_t	*ill;
5939 	ill_walk_context_t ctx;
5940 	ipsq_t	*ipsq;
5941 
5942 	if (error != NULL)
5943 		*error = 0;
5944 
5945 	/*
5946 	 * First match all the point-to-point interfaces
5947 	 * before looking at non-point-to-point interfaces.
5948 	 * This is done to avoid returning non-point-to-point
5949 	 * ipif instead of unnumbered point-to-point ipif.
5950 	 */
5951 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5952 	ill = ILL_START_WALK_V4(&ctx, ipst);
5953 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5954 		GRAB_CONN_LOCK(q);
5955 		mutex_enter(&ill->ill_lock);
5956 		for (ipif = ill->ill_ipif; ipif != NULL;
5957 		    ipif = ipif->ipif_next) {
5958 			/* Allow the ipif to be down */
5959 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5960 			    (ipif->ipif_lcl_addr == if_addr) &&
5961 			    (ipif->ipif_pp_dst_addr == dst)) {
5962 				/*
5963 				 * The block comment at the start of ipif_down
5964 				 * explains the use of the macros used below
5965 				 */
5966 				if (IPIF_CAN_LOOKUP(ipif)) {
5967 					ipif_refhold_locked(ipif);
5968 					mutex_exit(&ill->ill_lock);
5969 					RELEASE_CONN_LOCK(q);
5970 					rw_exit(&ipst->ips_ill_g_lock);
5971 					return (ipif);
5972 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5973 					ipsq = ill->ill_phyint->phyint_ipsq;
5974 					mutex_enter(&ipsq->ipsq_lock);
5975 					mutex_exit(&ill->ill_lock);
5976 					rw_exit(&ipst->ips_ill_g_lock);
5977 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5978 					    ill);
5979 					mutex_exit(&ipsq->ipsq_lock);
5980 					RELEASE_CONN_LOCK(q);
5981 					if (error != NULL)
5982 						*error = EINPROGRESS;
5983 					return (NULL);
5984 				}
5985 			}
5986 		}
5987 		mutex_exit(&ill->ill_lock);
5988 		RELEASE_CONN_LOCK(q);
5989 	}
5990 	rw_exit(&ipst->ips_ill_g_lock);
5991 
5992 	/* lookup the ipif based on interface address */
5993 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5994 	    ipst);
5995 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5996 	return (ipif);
5997 }
5998 
5999 /*
6000  * Look for an ipif with the specified address. For point-point links
6001  * we look for matches on either the destination address and the local
6002  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6003  * is set.
6004  * Matches on a specific ill if match_ill is set.
6005  */
6006 ipif_t *
6007 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6008     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6009 {
6010 	ipif_t  *ipif;
6011 	ill_t   *ill;
6012 	boolean_t ptp = B_FALSE;
6013 	ipsq_t	*ipsq;
6014 	ill_walk_context_t	ctx;
6015 
6016 	if (error != NULL)
6017 		*error = 0;
6018 
6019 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6020 	/*
6021 	 * Repeat twice, first based on local addresses and
6022 	 * next time for pointopoint.
6023 	 */
6024 repeat:
6025 	ill = ILL_START_WALK_V4(&ctx, ipst);
6026 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6027 		if (match_ill != NULL && ill != match_ill) {
6028 			continue;
6029 		}
6030 		GRAB_CONN_LOCK(q);
6031 		mutex_enter(&ill->ill_lock);
6032 		for (ipif = ill->ill_ipif; ipif != NULL;
6033 		    ipif = ipif->ipif_next) {
6034 			if (zoneid != ALL_ZONES &&
6035 			    zoneid != ipif->ipif_zoneid &&
6036 			    ipif->ipif_zoneid != ALL_ZONES)
6037 				continue;
6038 			/* Allow the ipif to be down */
6039 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6040 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6041 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6042 			    (ipif->ipif_pp_dst_addr == addr))) {
6043 				/*
6044 				 * The block comment at the start of ipif_down
6045 				 * explains the use of the macros used below
6046 				 */
6047 				if (IPIF_CAN_LOOKUP(ipif)) {
6048 					ipif_refhold_locked(ipif);
6049 					mutex_exit(&ill->ill_lock);
6050 					RELEASE_CONN_LOCK(q);
6051 					rw_exit(&ipst->ips_ill_g_lock);
6052 					return (ipif);
6053 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6054 					ipsq = ill->ill_phyint->phyint_ipsq;
6055 					mutex_enter(&ipsq->ipsq_lock);
6056 					mutex_exit(&ill->ill_lock);
6057 					rw_exit(&ipst->ips_ill_g_lock);
6058 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6059 					    ill);
6060 					mutex_exit(&ipsq->ipsq_lock);
6061 					RELEASE_CONN_LOCK(q);
6062 					if (error != NULL)
6063 						*error = EINPROGRESS;
6064 					return (NULL);
6065 				}
6066 			}
6067 		}
6068 		mutex_exit(&ill->ill_lock);
6069 		RELEASE_CONN_LOCK(q);
6070 	}
6071 
6072 	/* If we already did the ptp case, then we are done */
6073 	if (ptp) {
6074 		rw_exit(&ipst->ips_ill_g_lock);
6075 		if (error != NULL)
6076 			*error = ENXIO;
6077 		return (NULL);
6078 	}
6079 	ptp = B_TRUE;
6080 	goto repeat;
6081 }
6082 
6083 /*
6084  * Look for an ipif with the specified address. For point-point links
6085  * we look for matches on either the destination address and the local
6086  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6087  * is set.
6088  * Matches on a specific ill if match_ill is set.
6089  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6090  */
6091 zoneid_t
6092 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6093 {
6094 	zoneid_t zoneid;
6095 	ipif_t  *ipif;
6096 	ill_t   *ill;
6097 	boolean_t ptp = B_FALSE;
6098 	ill_walk_context_t	ctx;
6099 
6100 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6101 	/*
6102 	 * Repeat twice, first based on local addresses and
6103 	 * next time for pointopoint.
6104 	 */
6105 repeat:
6106 	ill = ILL_START_WALK_V4(&ctx, ipst);
6107 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6108 		if (match_ill != NULL && ill != match_ill) {
6109 			continue;
6110 		}
6111 		mutex_enter(&ill->ill_lock);
6112 		for (ipif = ill->ill_ipif; ipif != NULL;
6113 		    ipif = ipif->ipif_next) {
6114 			/* Allow the ipif to be down */
6115 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6116 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6117 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6118 			    (ipif->ipif_pp_dst_addr == addr)) &&
6119 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6120 				zoneid = ipif->ipif_zoneid;
6121 				mutex_exit(&ill->ill_lock);
6122 				rw_exit(&ipst->ips_ill_g_lock);
6123 				/*
6124 				 * If ipif_zoneid was ALL_ZONES then we have
6125 				 * a trusted extensions shared IP address.
6126 				 * In that case GLOBAL_ZONEID works to send.
6127 				 */
6128 				if (zoneid == ALL_ZONES)
6129 					zoneid = GLOBAL_ZONEID;
6130 				return (zoneid);
6131 			}
6132 		}
6133 		mutex_exit(&ill->ill_lock);
6134 	}
6135 
6136 	/* If we already did the ptp case, then we are done */
6137 	if (ptp) {
6138 		rw_exit(&ipst->ips_ill_g_lock);
6139 		return (ALL_ZONES);
6140 	}
6141 	ptp = B_TRUE;
6142 	goto repeat;
6143 }
6144 
6145 /*
6146  * Look for an ipif that matches the specified remote address i.e. the
6147  * ipif that would receive the specified packet.
6148  * First look for directly connected interfaces and then do a recursive
6149  * IRE lookup and pick the first ipif corresponding to the source address in the
6150  * ire.
6151  * Returns: held ipif
6152  */
6153 ipif_t *
6154 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6155 {
6156 	ipif_t	*ipif;
6157 	ire_t	*ire;
6158 	ip_stack_t	*ipst = ill->ill_ipst;
6159 
6160 	ASSERT(!ill->ill_isv6);
6161 
6162 	/*
6163 	 * Someone could be changing this ipif currently or change it
6164 	 * after we return this. Thus  a few packets could use the old
6165 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6166 	 * will atomically be updated or cleaned up with the new value
6167 	 * Thus we don't need a lock to check the flags or other attrs below.
6168 	 */
6169 	mutex_enter(&ill->ill_lock);
6170 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6171 		if (!IPIF_CAN_LOOKUP(ipif))
6172 			continue;
6173 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6174 		    ipif->ipif_zoneid != ALL_ZONES)
6175 			continue;
6176 		/* Allow the ipif to be down */
6177 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6178 			if ((ipif->ipif_pp_dst_addr == addr) ||
6179 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6180 			    ipif->ipif_lcl_addr == addr)) {
6181 				ipif_refhold_locked(ipif);
6182 				mutex_exit(&ill->ill_lock);
6183 				return (ipif);
6184 			}
6185 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6186 			ipif_refhold_locked(ipif);
6187 			mutex_exit(&ill->ill_lock);
6188 			return (ipif);
6189 		}
6190 	}
6191 	mutex_exit(&ill->ill_lock);
6192 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6193 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6194 	if (ire != NULL) {
6195 		/*
6196 		 * The callers of this function wants to know the
6197 		 * interface on which they have to send the replies
6198 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6199 		 * derived from different ills, we really don't care
6200 		 * what we return here.
6201 		 */
6202 		ipif = ire->ire_ipif;
6203 		if (ipif != NULL) {
6204 			ipif_refhold(ipif);
6205 			ire_refrele(ire);
6206 			return (ipif);
6207 		}
6208 		ire_refrele(ire);
6209 	}
6210 	/* Pick the first interface */
6211 	ipif = ipif_get_next_ipif(NULL, ill);
6212 	return (ipif);
6213 }
6214 
6215 /*
6216  * This func does not prevent refcnt from increasing. But if
6217  * the caller has taken steps to that effect, then this func
6218  * can be used to determine whether the ill has become quiescent
6219  */
6220 static boolean_t
6221 ill_is_quiescent(ill_t *ill)
6222 {
6223 	ipif_t	*ipif;
6224 
6225 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6226 
6227 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6228 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6229 			return (B_FALSE);
6230 		}
6231 	}
6232 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6233 		return (B_FALSE);
6234 	}
6235 	return (B_TRUE);
6236 }
6237 
6238 boolean_t
6239 ill_is_freeable(ill_t *ill)
6240 {
6241 	ipif_t	*ipif;
6242 
6243 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6244 
6245 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6246 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6247 			return (B_FALSE);
6248 		}
6249 	}
6250 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6251 		return (B_FALSE);
6252 	}
6253 	return (B_TRUE);
6254 }
6255 
6256 /*
6257  * This func does not prevent refcnt from increasing. But if
6258  * the caller has taken steps to that effect, then this func
6259  * can be used to determine whether the ipif has become quiescent
6260  */
6261 static boolean_t
6262 ipif_is_quiescent(ipif_t *ipif)
6263 {
6264 	ill_t *ill;
6265 
6266 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6267 
6268 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6269 		return (B_FALSE);
6270 	}
6271 
6272 	ill = ipif->ipif_ill;
6273 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6274 	    ill->ill_logical_down) {
6275 		return (B_TRUE);
6276 	}
6277 
6278 	/* This is the last ipif going down or being deleted on this ill */
6279 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6280 		return (B_FALSE);
6281 	}
6282 
6283 	return (B_TRUE);
6284 }
6285 
6286 /*
6287  * return true if the ipif can be destroyed: the ipif has to be quiescent
6288  * with zero references from ire/nce/ilm to it.
6289  */
6290 static boolean_t
6291 ipif_is_freeable(ipif_t *ipif)
6292 {
6293 
6294 	ill_t *ill;
6295 
6296 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6297 
6298 	if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6299 		return (B_FALSE);
6300 	}
6301 
6302 	ill = ipif->ipif_ill;
6303 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6304 	    ill->ill_logical_down) {
6305 		return (B_TRUE);
6306 	}
6307 
6308 	/* This is the last ipif going down or being deleted on this ill */
6309 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6310 		return (B_FALSE);
6311 	}
6312 
6313 	return (B_TRUE);
6314 }
6315 
6316 /*
6317  * This func does not prevent refcnt from increasing. But if
6318  * the caller has taken steps to that effect, then this func
6319  * can be used to determine whether the ipifs marked with IPIF_MOVING
6320  * have become quiescent and can be moved in a failover/failback.
6321  */
6322 static ipif_t *
6323 ill_quiescent_to_move(ill_t *ill)
6324 {
6325 	ipif_t  *ipif;
6326 
6327 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6328 
6329 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6330 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6331 			if (ipif->ipif_refcnt != 0 ||
6332 			    !IPIF_DOWN_OK(ipif)) {
6333 				return (ipif);
6334 			}
6335 		}
6336 	}
6337 	return (NULL);
6338 }
6339 
6340 /*
6341  * The ipif/ill/ire has been refreled. Do the tail processing.
6342  * Determine if the ipif or ill in question has become quiescent and if so
6343  * wakeup close and/or restart any queued pending ioctl that is waiting
6344  * for the ipif_down (or ill_down)
6345  */
6346 void
6347 ipif_ill_refrele_tail(ill_t *ill)
6348 {
6349 	mblk_t	*mp;
6350 	conn_t	*connp;
6351 	ipsq_t	*ipsq;
6352 	ipif_t	*ipif;
6353 	dl_notify_ind_t *dlindp;
6354 
6355 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6356 
6357 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6358 	    ill_is_freeable(ill)) {
6359 		/* ill_close may be waiting */
6360 		cv_broadcast(&ill->ill_cv);
6361 	}
6362 
6363 	/* ipsq can't change because ill_lock  is held */
6364 	ipsq = ill->ill_phyint->phyint_ipsq;
6365 	if (ipsq->ipsq_waitfor == 0) {
6366 		/* Not waiting for anything, just return. */
6367 		mutex_exit(&ill->ill_lock);
6368 		return;
6369 	}
6370 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6371 	    ipsq->ipsq_pending_ipif != NULL);
6372 	/*
6373 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6374 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6375 	 * be zero for restarting an ioctl that ends up downing the ill.
6376 	 */
6377 	ipif = ipsq->ipsq_pending_ipif;
6378 	if (ipif->ipif_ill != ill) {
6379 		/* The ioctl is pending on some other ill. */
6380 		mutex_exit(&ill->ill_lock);
6381 		return;
6382 	}
6383 
6384 	switch (ipsq->ipsq_waitfor) {
6385 	case IPIF_DOWN:
6386 		if (!ipif_is_quiescent(ipif)) {
6387 			mutex_exit(&ill->ill_lock);
6388 			return;
6389 		}
6390 		break;
6391 	case IPIF_FREE:
6392 		if (!ipif_is_freeable(ipif)) {
6393 			mutex_exit(&ill->ill_lock);
6394 			return;
6395 		}
6396 		break;
6397 
6398 	case ILL_DOWN:
6399 		if (!ill_is_quiescent(ill)) {
6400 			mutex_exit(&ill->ill_lock);
6401 			return;
6402 		}
6403 		break;
6404 	case ILL_FREE:
6405 		/*
6406 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6407 		 * waits synchronously in ip_close, and no message is queued in
6408 		 * ipsq_pending_mp at all in this case
6409 		 */
6410 		if (!ill_is_freeable(ill)) {
6411 			mutex_exit(&ill->ill_lock);
6412 			return;
6413 		}
6414 		break;
6415 
6416 	case ILL_MOVE_OK:
6417 		if (ill_quiescent_to_move(ill) != NULL) {
6418 			mutex_exit(&ill->ill_lock);
6419 			return;
6420 		}
6421 		break;
6422 	default:
6423 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6424 		    (void *)ipsq, ipsq->ipsq_waitfor);
6425 	}
6426 
6427 	/*
6428 	 * Incr refcnt for the qwriter_ip call below which
6429 	 * does a refrele
6430 	 */
6431 	ill_refhold_locked(ill);
6432 	mp = ipsq_pending_mp_get(ipsq, &connp);
6433 	mutex_exit(&ill->ill_lock);
6434 
6435 	ASSERT(mp != NULL);
6436 	/*
6437 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6438 	 * we can only get here when the current operation decides it
6439 	 * it needs to quiesce via ipsq_pending_mp_add().
6440 	 */
6441 	switch (mp->b_datap->db_type) {
6442 	case M_PCPROTO:
6443 	case M_PROTO:
6444 		/*
6445 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6446 		 */
6447 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6448 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6449 
6450 		switch (dlindp->dl_notification) {
6451 		case DL_NOTE_PHYS_ADDR:
6452 			qwriter_ip(ill, ill->ill_rq, mp,
6453 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6454 			return;
6455 		default:
6456 			ASSERT(0);
6457 		}
6458 		break;
6459 
6460 	case M_ERROR:
6461 	case M_HANGUP:
6462 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6463 		    B_TRUE);
6464 		return;
6465 
6466 	case M_IOCTL:
6467 	case M_IOCDATA:
6468 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6469 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6470 		return;
6471 
6472 	default:
6473 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6474 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6475 	}
6476 }
6477 
6478 #ifdef DEBUG
6479 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6480 static void
6481 th_trace_rrecord(th_trace_t *th_trace)
6482 {
6483 	tr_buf_t *tr_buf;
6484 	uint_t lastref;
6485 
6486 	lastref = th_trace->th_trace_lastref;
6487 	lastref++;
6488 	if (lastref == TR_BUF_MAX)
6489 		lastref = 0;
6490 	th_trace->th_trace_lastref = lastref;
6491 	tr_buf = &th_trace->th_trbuf[lastref];
6492 	tr_buf->tr_time = lbolt;
6493 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6494 }
6495 
6496 static void
6497 th_trace_free(void *value)
6498 {
6499 	th_trace_t *th_trace = value;
6500 
6501 	ASSERT(th_trace->th_refcnt == 0);
6502 	kmem_free(th_trace, sizeof (*th_trace));
6503 }
6504 
6505 /*
6506  * Find or create the per-thread hash table used to track object references.
6507  * The ipst argument is NULL if we shouldn't allocate.
6508  *
6509  * Accesses per-thread data, so there's no need to lock here.
6510  */
6511 static mod_hash_t *
6512 th_trace_gethash(ip_stack_t *ipst)
6513 {
6514 	th_hash_t *thh;
6515 
6516 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6517 		mod_hash_t *mh;
6518 		char name[256];
6519 		size_t objsize, rshift;
6520 		int retv;
6521 
6522 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6523 			return (NULL);
6524 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6525 
6526 		/*
6527 		 * We use mod_hash_create_extended here rather than the more
6528 		 * obvious mod_hash_create_ptrhash because the latter has a
6529 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6530 		 * block.
6531 		 */
6532 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6533 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6534 		rshift = highbit(objsize);
6535 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6536 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6537 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6538 		if (mh == NULL) {
6539 			kmem_free(thh, sizeof (*thh));
6540 			return (NULL);
6541 		}
6542 		thh->thh_hash = mh;
6543 		thh->thh_ipst = ipst;
6544 		/*
6545 		 * We trace ills, ipifs, ires, and nces.  All of these are
6546 		 * per-IP-stack, so the lock on the thread list is as well.
6547 		 */
6548 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6549 		list_insert_tail(&ip_thread_list, thh);
6550 		rw_exit(&ip_thread_rwlock);
6551 		retv = tsd_set(ip_thread_data, thh);
6552 		ASSERT(retv == 0);
6553 	}
6554 	return (thh != NULL ? thh->thh_hash : NULL);
6555 }
6556 
6557 boolean_t
6558 th_trace_ref(const void *obj, ip_stack_t *ipst)
6559 {
6560 	th_trace_t *th_trace;
6561 	mod_hash_t *mh;
6562 	mod_hash_val_t val;
6563 
6564 	if ((mh = th_trace_gethash(ipst)) == NULL)
6565 		return (B_FALSE);
6566 
6567 	/*
6568 	 * Attempt to locate the trace buffer for this obj and thread.
6569 	 * If it does not exist, then allocate a new trace buffer and
6570 	 * insert into the hash.
6571 	 */
6572 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6573 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6574 		if (th_trace == NULL)
6575 			return (B_FALSE);
6576 
6577 		th_trace->th_id = curthread;
6578 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6579 		    (mod_hash_val_t)th_trace) != 0) {
6580 			kmem_free(th_trace, sizeof (th_trace_t));
6581 			return (B_FALSE);
6582 		}
6583 	} else {
6584 		th_trace = (th_trace_t *)val;
6585 	}
6586 
6587 	ASSERT(th_trace->th_refcnt >= 0 &&
6588 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6589 
6590 	th_trace->th_refcnt++;
6591 	th_trace_rrecord(th_trace);
6592 	return (B_TRUE);
6593 }
6594 
6595 /*
6596  * For the purpose of tracing a reference release, we assume that global
6597  * tracing is always on and that the same thread initiated the reference hold
6598  * is releasing.
6599  */
6600 void
6601 th_trace_unref(const void *obj)
6602 {
6603 	int retv;
6604 	mod_hash_t *mh;
6605 	th_trace_t *th_trace;
6606 	mod_hash_val_t val;
6607 
6608 	mh = th_trace_gethash(NULL);
6609 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6610 	ASSERT(retv == 0);
6611 	th_trace = (th_trace_t *)val;
6612 
6613 	ASSERT(th_trace->th_refcnt > 0);
6614 	th_trace->th_refcnt--;
6615 	th_trace_rrecord(th_trace);
6616 }
6617 
6618 /*
6619  * If tracing has been disabled, then we assume that the reference counts are
6620  * now useless, and we clear them out before destroying the entries.
6621  */
6622 void
6623 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6624 {
6625 	th_hash_t	*thh;
6626 	mod_hash_t	*mh;
6627 	mod_hash_val_t	val;
6628 	th_trace_t	*th_trace;
6629 	int		retv;
6630 
6631 	rw_enter(&ip_thread_rwlock, RW_READER);
6632 	for (thh = list_head(&ip_thread_list); thh != NULL;
6633 	    thh = list_next(&ip_thread_list, thh)) {
6634 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6635 		    &val) == 0) {
6636 			th_trace = (th_trace_t *)val;
6637 			if (trace_disable)
6638 				th_trace->th_refcnt = 0;
6639 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6640 			ASSERT(retv == 0);
6641 		}
6642 	}
6643 	rw_exit(&ip_thread_rwlock);
6644 }
6645 
6646 void
6647 ipif_trace_ref(ipif_t *ipif)
6648 {
6649 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6650 
6651 	if (ipif->ipif_trace_disable)
6652 		return;
6653 
6654 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6655 		ipif->ipif_trace_disable = B_TRUE;
6656 		ipif_trace_cleanup(ipif);
6657 	}
6658 }
6659 
6660 void
6661 ipif_untrace_ref(ipif_t *ipif)
6662 {
6663 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6664 
6665 	if (!ipif->ipif_trace_disable)
6666 		th_trace_unref(ipif);
6667 }
6668 
6669 void
6670 ill_trace_ref(ill_t *ill)
6671 {
6672 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6673 
6674 	if (ill->ill_trace_disable)
6675 		return;
6676 
6677 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6678 		ill->ill_trace_disable = B_TRUE;
6679 		ill_trace_cleanup(ill);
6680 	}
6681 }
6682 
6683 void
6684 ill_untrace_ref(ill_t *ill)
6685 {
6686 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6687 
6688 	if (!ill->ill_trace_disable)
6689 		th_trace_unref(ill);
6690 }
6691 
6692 /*
6693  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6694  * failure, ipif_trace_disable is set.
6695  */
6696 static void
6697 ipif_trace_cleanup(const ipif_t *ipif)
6698 {
6699 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6700 }
6701 
6702 /*
6703  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6704  * failure, ill_trace_disable is set.
6705  */
6706 static void
6707 ill_trace_cleanup(const ill_t *ill)
6708 {
6709 	th_trace_cleanup(ill, ill->ill_trace_disable);
6710 }
6711 #endif /* DEBUG */
6712 
6713 void
6714 ipif_refhold_locked(ipif_t *ipif)
6715 {
6716 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6717 	ipif->ipif_refcnt++;
6718 	IPIF_TRACE_REF(ipif);
6719 }
6720 
6721 void
6722 ipif_refhold(ipif_t *ipif)
6723 {
6724 	ill_t	*ill;
6725 
6726 	ill = ipif->ipif_ill;
6727 	mutex_enter(&ill->ill_lock);
6728 	ipif->ipif_refcnt++;
6729 	IPIF_TRACE_REF(ipif);
6730 	mutex_exit(&ill->ill_lock);
6731 }
6732 
6733 /*
6734  * Must not be called while holding any locks. Otherwise if this is
6735  * the last reference to be released there is a chance of recursive mutex
6736  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6737  * to restart an ioctl.
6738  */
6739 void
6740 ipif_refrele(ipif_t *ipif)
6741 {
6742 	ill_t	*ill;
6743 
6744 	ill = ipif->ipif_ill;
6745 
6746 	mutex_enter(&ill->ill_lock);
6747 	ASSERT(ipif->ipif_refcnt != 0);
6748 	ipif->ipif_refcnt--;
6749 	IPIF_UNTRACE_REF(ipif);
6750 	if (ipif->ipif_refcnt != 0) {
6751 		mutex_exit(&ill->ill_lock);
6752 		return;
6753 	}
6754 
6755 	/* Drops the ill_lock */
6756 	ipif_ill_refrele_tail(ill);
6757 }
6758 
6759 ipif_t *
6760 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6761 {
6762 	ipif_t	*ipif;
6763 
6764 	mutex_enter(&ill->ill_lock);
6765 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6766 	    ipif != NULL; ipif = ipif->ipif_next) {
6767 		if (!IPIF_CAN_LOOKUP(ipif))
6768 			continue;
6769 		ipif_refhold_locked(ipif);
6770 		mutex_exit(&ill->ill_lock);
6771 		return (ipif);
6772 	}
6773 	mutex_exit(&ill->ill_lock);
6774 	return (NULL);
6775 }
6776 
6777 /*
6778  * TODO: make this table extendible at run time
6779  * Return a pointer to the mac type info for 'mac_type'
6780  */
6781 static ip_m_t *
6782 ip_m_lookup(t_uscalar_t mac_type)
6783 {
6784 	ip_m_t	*ipm;
6785 
6786 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6787 		if (ipm->ip_m_mac_type == mac_type)
6788 			return (ipm);
6789 	return (NULL);
6790 }
6791 
6792 /*
6793  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6794  * ipif_arg is passed in to associate it with the correct interface.
6795  * We may need to restart this operation if the ipif cannot be looked up
6796  * due to an exclusive operation that is currently in progress. The restart
6797  * entry point is specified by 'func'
6798  */
6799 int
6800 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6801     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6802     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6803     struct rtsa_s *sp, ip_stack_t *ipst)
6804 {
6805 	ire_t	*ire;
6806 	ire_t	*gw_ire = NULL;
6807 	ipif_t	*ipif = NULL;
6808 	boolean_t ipif_refheld = B_FALSE;
6809 	uint_t	type;
6810 	int	match_flags = MATCH_IRE_TYPE;
6811 	int	error;
6812 	tsol_gc_t *gc = NULL;
6813 	tsol_gcgrp_t *gcgrp = NULL;
6814 	boolean_t gcgrp_xtraref = B_FALSE;
6815 
6816 	ip1dbg(("ip_rt_add:"));
6817 
6818 	if (ire_arg != NULL)
6819 		*ire_arg = NULL;
6820 
6821 	/*
6822 	 * If this is the case of RTF_HOST being set, then we set the netmask
6823 	 * to all ones (regardless if one was supplied).
6824 	 */
6825 	if (flags & RTF_HOST)
6826 		mask = IP_HOST_MASK;
6827 
6828 	/*
6829 	 * Prevent routes with a zero gateway from being created (since
6830 	 * interfaces can currently be plumbed and brought up no assigned
6831 	 * address).
6832 	 */
6833 	if (gw_addr == 0)
6834 		return (ENETUNREACH);
6835 	/*
6836 	 * Get the ipif, if any, corresponding to the gw_addr
6837 	 */
6838 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6839 	    ipst);
6840 	if (ipif != NULL) {
6841 		if (IS_VNI(ipif->ipif_ill)) {
6842 			ipif_refrele(ipif);
6843 			return (EINVAL);
6844 		}
6845 		ipif_refheld = B_TRUE;
6846 	} else if (error == EINPROGRESS) {
6847 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6848 		return (EINPROGRESS);
6849 	} else {
6850 		error = 0;
6851 	}
6852 
6853 	if (ipif != NULL) {
6854 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6855 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6856 	} else {
6857 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6858 	}
6859 
6860 	/*
6861 	 * GateD will attempt to create routes with a loopback interface
6862 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6863 	 * these routes to be added, but create them as interface routes
6864 	 * since the gateway is an interface address.
6865 	 */
6866 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6867 		flags &= ~RTF_GATEWAY;
6868 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6869 		    mask == IP_HOST_MASK) {
6870 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6871 			    ALL_ZONES, NULL, match_flags, ipst);
6872 			if (ire != NULL) {
6873 				ire_refrele(ire);
6874 				if (ipif_refheld)
6875 					ipif_refrele(ipif);
6876 				return (EEXIST);
6877 			}
6878 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6879 			    "for 0x%x\n", (void *)ipif,
6880 			    ipif->ipif_ire_type,
6881 			    ntohl(ipif->ipif_lcl_addr)));
6882 			ire = ire_create(
6883 			    (uchar_t *)&dst_addr,	/* dest address */
6884 			    (uchar_t *)&mask,		/* mask */
6885 			    (uchar_t *)&ipif->ipif_src_addr,
6886 			    NULL,			/* no gateway */
6887 			    &ipif->ipif_mtu,
6888 			    NULL,
6889 			    ipif->ipif_rq,		/* recv-from queue */
6890 			    NULL,			/* no send-to queue */
6891 			    ipif->ipif_ire_type,	/* LOOPBACK */
6892 			    ipif,
6893 			    0,
6894 			    0,
6895 			    0,
6896 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6897 			    RTF_PRIVATE : 0,
6898 			    &ire_uinfo_null,
6899 			    NULL,
6900 			    NULL,
6901 			    ipst);
6902 
6903 			if (ire == NULL) {
6904 				if (ipif_refheld)
6905 					ipif_refrele(ipif);
6906 				return (ENOMEM);
6907 			}
6908 			error = ire_add(&ire, q, mp, func, B_FALSE);
6909 			if (error == 0)
6910 				goto save_ire;
6911 			if (ipif_refheld)
6912 				ipif_refrele(ipif);
6913 			return (error);
6914 
6915 		}
6916 	}
6917 
6918 	/*
6919 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6920 	 * and the gateway address provided is one of the system's interface
6921 	 * addresses.  By using the routing socket interface and supplying an
6922 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6923 	 * specifying an interface route to be created is available which uses
6924 	 * the interface index that specifies the outgoing interface rather than
6925 	 * the address of an outgoing interface (which may not be able to
6926 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6927 	 * flag, routes can be specified which not only specify the next-hop to
6928 	 * be used when routing to a certain prefix, but also which outgoing
6929 	 * interface should be used.
6930 	 *
6931 	 * Previously, interfaces would have unique addresses assigned to them
6932 	 * and so the address assigned to a particular interface could be used
6933 	 * to identify a particular interface.  One exception to this was the
6934 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6935 	 *
6936 	 * With the advent of IPv6 and its link-local addresses, this
6937 	 * restriction was relaxed and interfaces could share addresses between
6938 	 * themselves.  In fact, typically all of the link-local interfaces on
6939 	 * an IPv6 node or router will have the same link-local address.  In
6940 	 * order to differentiate between these interfaces, the use of an
6941 	 * interface index is necessary and this index can be carried inside a
6942 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6943 	 * of using the interface index, however, is that all of the ipif's that
6944 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6945 	 * cannot be used to differentiate between ipif's (or logical
6946 	 * interfaces) that belong to the same ill (physical interface).
6947 	 *
6948 	 * For example, in the following case involving IPv4 interfaces and
6949 	 * logical interfaces
6950 	 *
6951 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6952 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6953 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6954 	 *
6955 	 * the ipif's corresponding to each of these interface routes can be
6956 	 * uniquely identified by the "gateway" (actually interface address).
6957 	 *
6958 	 * In this case involving multiple IPv6 default routes to a particular
6959 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6960 	 * default route is of interest:
6961 	 *
6962 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6963 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6964 	 */
6965 
6966 	/* RTF_GATEWAY not set */
6967 	if (!(flags & RTF_GATEWAY)) {
6968 		queue_t	*stq;
6969 
6970 		if (sp != NULL) {
6971 			ip2dbg(("ip_rt_add: gateway security attributes "
6972 			    "cannot be set with interface route\n"));
6973 			if (ipif_refheld)
6974 				ipif_refrele(ipif);
6975 			return (EINVAL);
6976 		}
6977 
6978 		/*
6979 		 * As the interface index specified with the RTA_IFP sockaddr is
6980 		 * the same for all ipif's off of an ill, the matching logic
6981 		 * below uses MATCH_IRE_ILL if such an index was specified.
6982 		 * This means that routes sharing the same prefix when added
6983 		 * using a RTA_IFP sockaddr must have distinct interface
6984 		 * indices (namely, they must be on distinct ill's).
6985 		 *
6986 		 * On the other hand, since the gateway address will usually be
6987 		 * different for each ipif on the system, the matching logic
6988 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6989 		 * route.  This means that interface routes for the same prefix
6990 		 * can be created if they belong to distinct ipif's and if a
6991 		 * RTA_IFP sockaddr is not present.
6992 		 */
6993 		if (ipif_arg != NULL) {
6994 			if (ipif_refheld)  {
6995 				ipif_refrele(ipif);
6996 				ipif_refheld = B_FALSE;
6997 			}
6998 			ipif = ipif_arg;
6999 			match_flags |= MATCH_IRE_ILL;
7000 		} else {
7001 			/*
7002 			 * Check the ipif corresponding to the gw_addr
7003 			 */
7004 			if (ipif == NULL)
7005 				return (ENETUNREACH);
7006 			match_flags |= MATCH_IRE_IPIF;
7007 		}
7008 		ASSERT(ipif != NULL);
7009 
7010 		/*
7011 		 * We check for an existing entry at this point.
7012 		 *
7013 		 * Since a netmask isn't passed in via the ioctl interface
7014 		 * (SIOCADDRT), we don't check for a matching netmask in that
7015 		 * case.
7016 		 */
7017 		if (!ioctl_msg)
7018 			match_flags |= MATCH_IRE_MASK;
7019 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7020 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7021 		if (ire != NULL) {
7022 			ire_refrele(ire);
7023 			if (ipif_refheld)
7024 				ipif_refrele(ipif);
7025 			return (EEXIST);
7026 		}
7027 
7028 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7029 		    ? ipif->ipif_rq : ipif->ipif_wq;
7030 
7031 		/*
7032 		 * Create a copy of the IRE_LOOPBACK,
7033 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7034 		 * the modified address and netmask.
7035 		 */
7036 		ire = ire_create(
7037 		    (uchar_t *)&dst_addr,
7038 		    (uint8_t *)&mask,
7039 		    (uint8_t *)&ipif->ipif_src_addr,
7040 		    NULL,
7041 		    &ipif->ipif_mtu,
7042 		    NULL,
7043 		    NULL,
7044 		    stq,
7045 		    ipif->ipif_net_type,
7046 		    ipif,
7047 		    0,
7048 		    0,
7049 		    0,
7050 		    flags,
7051 		    &ire_uinfo_null,
7052 		    NULL,
7053 		    NULL,
7054 		    ipst);
7055 		if (ire == NULL) {
7056 			if (ipif_refheld)
7057 				ipif_refrele(ipif);
7058 			return (ENOMEM);
7059 		}
7060 
7061 		/*
7062 		 * Some software (for example, GateD and Sun Cluster) attempts
7063 		 * to create (what amount to) IRE_PREFIX routes with the
7064 		 * loopback address as the gateway.  This is primarily done to
7065 		 * set up prefixes with the RTF_REJECT flag set (for example,
7066 		 * when generating aggregate routes.)
7067 		 *
7068 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7069 		 * IRE_LOOPBACK, then we map the request into a
7070 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7071 		 * these interface routes, by definition, can only be that.
7072 		 *
7073 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7074 		 * routine, but rather using ire_create() directly.
7075 		 *
7076 		 */
7077 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7078 			ire->ire_type = IRE_IF_NORESOLVER;
7079 			ire->ire_flags |= RTF_BLACKHOLE;
7080 		}
7081 
7082 		error = ire_add(&ire, q, mp, func, B_FALSE);
7083 		if (error == 0)
7084 			goto save_ire;
7085 
7086 		/*
7087 		 * In the result of failure, ire_add() will have already
7088 		 * deleted the ire in question, so there is no need to
7089 		 * do that here.
7090 		 */
7091 		if (ipif_refheld)
7092 			ipif_refrele(ipif);
7093 		return (error);
7094 	}
7095 	if (ipif_refheld) {
7096 		ipif_refrele(ipif);
7097 		ipif_refheld = B_FALSE;
7098 	}
7099 
7100 	/*
7101 	 * Get an interface IRE for the specified gateway.
7102 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7103 	 * gateway, it is currently unreachable and we fail the request
7104 	 * accordingly.
7105 	 */
7106 	ipif = ipif_arg;
7107 	if (ipif_arg != NULL)
7108 		match_flags |= MATCH_IRE_ILL;
7109 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7110 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7111 	if (gw_ire == NULL)
7112 		return (ENETUNREACH);
7113 
7114 	/*
7115 	 * We create one of three types of IREs as a result of this request
7116 	 * based on the netmask.  A netmask of all ones (which is automatically
7117 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7118 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7119 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7120 	 * destination prefix.
7121 	 */
7122 	if (mask == IP_HOST_MASK)
7123 		type = IRE_HOST;
7124 	else if (mask == 0)
7125 		type = IRE_DEFAULT;
7126 	else
7127 		type = IRE_PREFIX;
7128 
7129 	/* check for a duplicate entry */
7130 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7131 	    NULL, ALL_ZONES, 0, NULL,
7132 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7133 	if (ire != NULL) {
7134 		ire_refrele(gw_ire);
7135 		ire_refrele(ire);
7136 		return (EEXIST);
7137 	}
7138 
7139 	/* Security attribute exists */
7140 	if (sp != NULL) {
7141 		tsol_gcgrp_addr_t ga;
7142 
7143 		/* find or create the gateway credentials group */
7144 		ga.ga_af = AF_INET;
7145 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7146 
7147 		/* we hold reference to it upon success */
7148 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7149 		if (gcgrp == NULL) {
7150 			ire_refrele(gw_ire);
7151 			return (ENOMEM);
7152 		}
7153 
7154 		/*
7155 		 * Create and add the security attribute to the group; a
7156 		 * reference to the group is made upon allocating a new
7157 		 * entry successfully.  If it finds an already-existing
7158 		 * entry for the security attribute in the group, it simply
7159 		 * returns it and no new reference is made to the group.
7160 		 */
7161 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7162 		if (gc == NULL) {
7163 			/* release reference held by gcgrp_lookup */
7164 			GCGRP_REFRELE(gcgrp);
7165 			ire_refrele(gw_ire);
7166 			return (ENOMEM);
7167 		}
7168 	}
7169 
7170 	/* Create the IRE. */
7171 	ire = ire_create(
7172 	    (uchar_t *)&dst_addr,		/* dest address */
7173 	    (uchar_t *)&mask,			/* mask */
7174 	    /* src address assigned by the caller? */
7175 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7176 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7177 	    (uchar_t *)&gw_addr,		/* gateway address */
7178 	    &gw_ire->ire_max_frag,
7179 	    NULL,				/* no src nce */
7180 	    NULL,				/* no recv-from queue */
7181 	    NULL,				/* no send-to queue */
7182 	    (ushort_t)type,			/* IRE type */
7183 	    ipif_arg,
7184 	    0,
7185 	    0,
7186 	    0,
7187 	    flags,
7188 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7189 	    gc,					/* security attribute */
7190 	    NULL,
7191 	    ipst);
7192 
7193 	/*
7194 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7195 	 * reference to the 'gcgrp'. We can now release the extra reference
7196 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7197 	 */
7198 	if (gcgrp_xtraref)
7199 		GCGRP_REFRELE(gcgrp);
7200 	if (ire == NULL) {
7201 		if (gc != NULL)
7202 			GC_REFRELE(gc);
7203 		ire_refrele(gw_ire);
7204 		return (ENOMEM);
7205 	}
7206 
7207 	/*
7208 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7209 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7210 	 */
7211 
7212 	/* Add the new IRE. */
7213 	error = ire_add(&ire, q, mp, func, B_FALSE);
7214 	if (error != 0) {
7215 		/*
7216 		 * In the result of failure, ire_add() will have already
7217 		 * deleted the ire in question, so there is no need to
7218 		 * do that here.
7219 		 */
7220 		ire_refrele(gw_ire);
7221 		return (error);
7222 	}
7223 
7224 	if (flags & RTF_MULTIRT) {
7225 		/*
7226 		 * Invoke the CGTP (multirouting) filtering module
7227 		 * to add the dst address in the filtering database.
7228 		 * Replicated inbound packets coming from that address
7229 		 * will be filtered to discard the duplicates.
7230 		 * It is not necessary to call the CGTP filter hook
7231 		 * when the dst address is a broadcast or multicast,
7232 		 * because an IP source address cannot be a broadcast
7233 		 * or a multicast.
7234 		 */
7235 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7236 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7237 		if (ire_dst != NULL) {
7238 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7239 			ire_refrele(ire_dst);
7240 			goto save_ire;
7241 		}
7242 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7243 		    !CLASSD(ire->ire_addr)) {
7244 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7245 			    ipst->ips_netstack->netstack_stackid,
7246 			    ire->ire_addr,
7247 			    ire->ire_gateway_addr,
7248 			    ire->ire_src_addr,
7249 			    gw_ire->ire_src_addr);
7250 			if (res != 0) {
7251 				ire_refrele(gw_ire);
7252 				ire_delete(ire);
7253 				return (res);
7254 			}
7255 		}
7256 	}
7257 
7258 	/*
7259 	 * Now that the prefix IRE entry has been created, delete any
7260 	 * existing gateway IRE cache entries as well as any IRE caches
7261 	 * using the gateway, and force them to be created through
7262 	 * ip_newroute.
7263 	 */
7264 	if (gc != NULL) {
7265 		ASSERT(gcgrp != NULL);
7266 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7267 	}
7268 
7269 save_ire:
7270 	if (gw_ire != NULL) {
7271 		ire_refrele(gw_ire);
7272 	}
7273 	if (ipif != NULL) {
7274 		/*
7275 		 * Save enough information so that we can recreate the IRE if
7276 		 * the interface goes down and then up.  The metrics associated
7277 		 * with the route will be saved as well when rts_setmetrics() is
7278 		 * called after the IRE has been created.  In the case where
7279 		 * memory cannot be allocated, none of this information will be
7280 		 * saved.
7281 		 */
7282 		ipif_save_ire(ipif, ire);
7283 	}
7284 	if (ioctl_msg)
7285 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7286 	if (ire_arg != NULL) {
7287 		/*
7288 		 * Store the ire that was successfully added into where ire_arg
7289 		 * points to so that callers don't have to look it up
7290 		 * themselves (but they are responsible for ire_refrele()ing
7291 		 * the ire when they are finished with it).
7292 		 */
7293 		*ire_arg = ire;
7294 	} else {
7295 		ire_refrele(ire);		/* Held in ire_add */
7296 	}
7297 	if (ipif_refheld)
7298 		ipif_refrele(ipif);
7299 	return (0);
7300 }
7301 
7302 /*
7303  * ip_rt_delete is called to delete an IPv4 route.
7304  * ipif_arg is passed in to associate it with the correct interface.
7305  * We may need to restart this operation if the ipif cannot be looked up
7306  * due to an exclusive operation that is currently in progress. The restart
7307  * entry point is specified by 'func'
7308  */
7309 /* ARGSUSED4 */
7310 int
7311 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7312     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7313     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7314 {
7315 	ire_t	*ire = NULL;
7316 	ipif_t	*ipif;
7317 	boolean_t ipif_refheld = B_FALSE;
7318 	uint_t	type;
7319 	uint_t	match_flags = MATCH_IRE_TYPE;
7320 	int	err = 0;
7321 
7322 	ip1dbg(("ip_rt_delete:"));
7323 	/*
7324 	 * If this is the case of RTF_HOST being set, then we set the netmask
7325 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7326 	 */
7327 	if (flags & RTF_HOST) {
7328 		mask = IP_HOST_MASK;
7329 		match_flags |= MATCH_IRE_MASK;
7330 	} else if (rtm_addrs & RTA_NETMASK) {
7331 		match_flags |= MATCH_IRE_MASK;
7332 	}
7333 
7334 	/*
7335 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7336 	 * we check if the gateway address is one of our interfaces first,
7337 	 * and fall back on RTF_GATEWAY routes.
7338 	 *
7339 	 * This makes it possible to delete an original
7340 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7341 	 *
7342 	 * As the interface index specified with the RTA_IFP sockaddr is the
7343 	 * same for all ipif's off of an ill, the matching logic below uses
7344 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7345 	 * sharing the same prefix and interface index as the the route
7346 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7347 	 * is specified in the request.
7348 	 *
7349 	 * On the other hand, since the gateway address will usually be
7350 	 * different for each ipif on the system, the matching logic
7351 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7352 	 * route.  This means that interface routes for the same prefix can be
7353 	 * uniquely identified if they belong to distinct ipif's and if a
7354 	 * RTA_IFP sockaddr is not present.
7355 	 *
7356 	 * For more detail on specifying routes by gateway address and by
7357 	 * interface index, see the comments in ip_rt_add().
7358 	 */
7359 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7360 	    ipst);
7361 	if (ipif != NULL)
7362 		ipif_refheld = B_TRUE;
7363 	else if (err == EINPROGRESS)
7364 		return (err);
7365 	else
7366 		err = 0;
7367 	if (ipif != NULL) {
7368 		if (ipif_arg != NULL) {
7369 			if (ipif_refheld) {
7370 				ipif_refrele(ipif);
7371 				ipif_refheld = B_FALSE;
7372 			}
7373 			ipif = ipif_arg;
7374 			match_flags |= MATCH_IRE_ILL;
7375 		} else {
7376 			match_flags |= MATCH_IRE_IPIF;
7377 		}
7378 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7379 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7380 			    ALL_ZONES, NULL, match_flags, ipst);
7381 		}
7382 		if (ire == NULL) {
7383 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7384 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7385 			    match_flags, ipst);
7386 		}
7387 	}
7388 
7389 	if (ire == NULL) {
7390 		/*
7391 		 * At this point, the gateway address is not one of our own
7392 		 * addresses or a matching interface route was not found.  We
7393 		 * set the IRE type to lookup based on whether
7394 		 * this is a host route, a default route or just a prefix.
7395 		 *
7396 		 * If an ipif_arg was passed in, then the lookup is based on an
7397 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7398 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7399 		 * set as the route being looked up is not a traditional
7400 		 * interface route.
7401 		 */
7402 		match_flags &= ~MATCH_IRE_IPIF;
7403 		match_flags |= MATCH_IRE_GW;
7404 		if (ipif_arg != NULL)
7405 			match_flags |= MATCH_IRE_ILL;
7406 		if (mask == IP_HOST_MASK)
7407 			type = IRE_HOST;
7408 		else if (mask == 0)
7409 			type = IRE_DEFAULT;
7410 		else
7411 			type = IRE_PREFIX;
7412 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7413 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7414 	}
7415 
7416 	if (ipif_refheld)
7417 		ipif_refrele(ipif);
7418 
7419 	/* ipif is not refheld anymore */
7420 	if (ire == NULL)
7421 		return (ESRCH);
7422 
7423 	if (ire->ire_flags & RTF_MULTIRT) {
7424 		/*
7425 		 * Invoke the CGTP (multirouting) filtering module
7426 		 * to remove the dst address from the filtering database.
7427 		 * Packets coming from that address will no longer be
7428 		 * filtered to remove duplicates.
7429 		 */
7430 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7431 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7432 			    ipst->ips_netstack->netstack_stackid,
7433 			    ire->ire_addr, ire->ire_gateway_addr);
7434 		}
7435 		ip_cgtp_bcast_delete(ire, ipst);
7436 	}
7437 
7438 	ipif = ire->ire_ipif;
7439 	if (ipif != NULL)
7440 		ipif_remove_ire(ipif, ire);
7441 	if (ioctl_msg)
7442 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7443 	ire_delete(ire);
7444 	ire_refrele(ire);
7445 	return (err);
7446 }
7447 
7448 /*
7449  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7450  */
7451 /* ARGSUSED */
7452 int
7453 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7454     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7455 {
7456 	ipaddr_t dst_addr;
7457 	ipaddr_t gw_addr;
7458 	ipaddr_t mask;
7459 	int error = 0;
7460 	mblk_t *mp1;
7461 	struct rtentry *rt;
7462 	ipif_t *ipif = NULL;
7463 	ip_stack_t	*ipst;
7464 
7465 	ASSERT(q->q_next == NULL);
7466 	ipst = CONNQ_TO_IPST(q);
7467 
7468 	ip1dbg(("ip_siocaddrt:"));
7469 	/* Existence of mp1 verified in ip_wput_nondata */
7470 	mp1 = mp->b_cont->b_cont;
7471 	rt = (struct rtentry *)mp1->b_rptr;
7472 
7473 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7474 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7475 
7476 	/*
7477 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7478 	 * to a particular host address.  In this case, we set the netmask to
7479 	 * all ones for the particular destination address.  Otherwise,
7480 	 * determine the netmask to be used based on dst_addr and the interfaces
7481 	 * in use.
7482 	 */
7483 	if (rt->rt_flags & RTF_HOST) {
7484 		mask = IP_HOST_MASK;
7485 	} else {
7486 		/*
7487 		 * Note that ip_subnet_mask returns a zero mask in the case of
7488 		 * default (an all-zeroes address).
7489 		 */
7490 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7491 	}
7492 
7493 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7494 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7495 	if (ipif != NULL)
7496 		ipif_refrele(ipif);
7497 	return (error);
7498 }
7499 
7500 /*
7501  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7502  */
7503 /* ARGSUSED */
7504 int
7505 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7506     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7507 {
7508 	ipaddr_t dst_addr;
7509 	ipaddr_t gw_addr;
7510 	ipaddr_t mask;
7511 	int error;
7512 	mblk_t *mp1;
7513 	struct rtentry *rt;
7514 	ipif_t *ipif = NULL;
7515 	ip_stack_t	*ipst;
7516 
7517 	ASSERT(q->q_next == NULL);
7518 	ipst = CONNQ_TO_IPST(q);
7519 
7520 	ip1dbg(("ip_siocdelrt:"));
7521 	/* Existence of mp1 verified in ip_wput_nondata */
7522 	mp1 = mp->b_cont->b_cont;
7523 	rt = (struct rtentry *)mp1->b_rptr;
7524 
7525 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7526 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7527 
7528 	/*
7529 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7530 	 * to a particular host address.  In this case, we set the netmask to
7531 	 * all ones for the particular destination address.  Otherwise,
7532 	 * determine the netmask to be used based on dst_addr and the interfaces
7533 	 * in use.
7534 	 */
7535 	if (rt->rt_flags & RTF_HOST) {
7536 		mask = IP_HOST_MASK;
7537 	} else {
7538 		/*
7539 		 * Note that ip_subnet_mask returns a zero mask in the case of
7540 		 * default (an all-zeroes address).
7541 		 */
7542 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7543 	}
7544 
7545 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7546 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7547 	    mp, ip_process_ioctl, ipst);
7548 	if (ipif != NULL)
7549 		ipif_refrele(ipif);
7550 	return (error);
7551 }
7552 
7553 /*
7554  * Enqueue the mp onto the ipsq, chained by b_next.
7555  * b_prev stores the function to be executed later, and b_queue the queue
7556  * where this mp originated.
7557  */
7558 void
7559 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7560     ill_t *pending_ill)
7561 {
7562 	conn_t	*connp = NULL;
7563 
7564 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7565 	ASSERT(func != NULL);
7566 
7567 	mp->b_queue = q;
7568 	mp->b_prev = (void *)func;
7569 	mp->b_next = NULL;
7570 
7571 	switch (type) {
7572 	case CUR_OP:
7573 		if (ipsq->ipsq_mptail != NULL) {
7574 			ASSERT(ipsq->ipsq_mphead != NULL);
7575 			ipsq->ipsq_mptail->b_next = mp;
7576 		} else {
7577 			ASSERT(ipsq->ipsq_mphead == NULL);
7578 			ipsq->ipsq_mphead = mp;
7579 		}
7580 		ipsq->ipsq_mptail = mp;
7581 		break;
7582 
7583 	case NEW_OP:
7584 		if (ipsq->ipsq_xopq_mptail != NULL) {
7585 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7586 			ipsq->ipsq_xopq_mptail->b_next = mp;
7587 		} else {
7588 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7589 			ipsq->ipsq_xopq_mphead = mp;
7590 		}
7591 		ipsq->ipsq_xopq_mptail = mp;
7592 		break;
7593 	default:
7594 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7595 	}
7596 
7597 	if (CONN_Q(q) && pending_ill != NULL) {
7598 		connp = Q_TO_CONN(q);
7599 
7600 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7601 		connp->conn_oper_pending_ill = pending_ill;
7602 	}
7603 }
7604 
7605 /*
7606  * Return the mp at the head of the ipsq. After emptying the ipsq
7607  * look at the next ioctl, if this ioctl is complete. Otherwise
7608  * return, we will resume when we complete the current ioctl.
7609  * The current ioctl will wait till it gets a response from the
7610  * driver below.
7611  */
7612 static mblk_t *
7613 ipsq_dq(ipsq_t *ipsq)
7614 {
7615 	mblk_t	*mp;
7616 
7617 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7618 
7619 	mp = ipsq->ipsq_mphead;
7620 	if (mp != NULL) {
7621 		ipsq->ipsq_mphead = mp->b_next;
7622 		if (ipsq->ipsq_mphead == NULL)
7623 			ipsq->ipsq_mptail = NULL;
7624 		mp->b_next = NULL;
7625 		return (mp);
7626 	}
7627 	if (ipsq->ipsq_current_ipif != NULL)
7628 		return (NULL);
7629 	mp = ipsq->ipsq_xopq_mphead;
7630 	if (mp != NULL) {
7631 		ipsq->ipsq_xopq_mphead = mp->b_next;
7632 		if (ipsq->ipsq_xopq_mphead == NULL)
7633 			ipsq->ipsq_xopq_mptail = NULL;
7634 		mp->b_next = NULL;
7635 		return (mp);
7636 	}
7637 	return (NULL);
7638 }
7639 
7640 /*
7641  * Enter the ipsq corresponding to ill, by waiting synchronously till
7642  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7643  * will have to drain completely before ipsq_enter returns success.
7644  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7645  * and the ipsq_exit logic will start the next enqueued ioctl after
7646  * completion of the current ioctl. If 'force' is used, we don't wait
7647  * for the enqueued ioctls. This is needed when a conn_close wants to
7648  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7649  * of an ill can also use this option. But we dont' use it currently.
7650  */
7651 #define	ENTER_SQ_WAIT_TICKS 100
7652 boolean_t
7653 ipsq_enter(ill_t *ill, boolean_t force)
7654 {
7655 	ipsq_t	*ipsq;
7656 	boolean_t waited_enough = B_FALSE;
7657 
7658 	/*
7659 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7660 	 * Since the <ill-ipsq> assocs could change while we wait for the
7661 	 * writer, it is easier to wait on a fixed global rather than try to
7662 	 * cv_wait on a changing ipsq.
7663 	 */
7664 	mutex_enter(&ill->ill_lock);
7665 	for (;;) {
7666 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7667 			mutex_exit(&ill->ill_lock);
7668 			return (B_FALSE);
7669 		}
7670 
7671 		ipsq = ill->ill_phyint->phyint_ipsq;
7672 		mutex_enter(&ipsq->ipsq_lock);
7673 		if (ipsq->ipsq_writer == NULL &&
7674 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7675 			break;
7676 		} else if (ipsq->ipsq_writer != NULL) {
7677 			mutex_exit(&ipsq->ipsq_lock);
7678 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7679 		} else {
7680 			mutex_exit(&ipsq->ipsq_lock);
7681 			if (force) {
7682 				(void) cv_timedwait(&ill->ill_cv,
7683 				    &ill->ill_lock,
7684 				    lbolt + ENTER_SQ_WAIT_TICKS);
7685 				waited_enough = B_TRUE;
7686 				continue;
7687 			} else {
7688 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7689 			}
7690 		}
7691 	}
7692 
7693 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7694 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7695 	ipsq->ipsq_writer = curthread;
7696 	ipsq->ipsq_reentry_cnt++;
7697 #ifdef DEBUG
7698 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7699 #endif
7700 	mutex_exit(&ipsq->ipsq_lock);
7701 	mutex_exit(&ill->ill_lock);
7702 	return (B_TRUE);
7703 }
7704 
7705 /*
7706  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7707  * certain critical operations like plumbing (i.e. most set ioctls),
7708  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7709  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7710  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7711  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7712  * threads executing in the ipsq. Responses from the driver pertain to the
7713  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7714  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7715  *
7716  * If a thread does not want to reenter the ipsq when it is already writer,
7717  * it must make sure that the specified reentry point to be called later
7718  * when the ipsq is empty, nor any code path starting from the specified reentry
7719  * point must never ever try to enter the ipsq again. Otherwise it can lead
7720  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7721  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7722  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7723  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7724  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7725  * ioctl if the current ioctl has completed. If the current ioctl is still
7726  * in progress it simply returns. The current ioctl could be waiting for
7727  * a response from another module (arp_ or the driver or could be waiting for
7728  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7729  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7730  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7731  * ipsq_current_ipif is clear which happens only on ioctl completion.
7732  */
7733 
7734 /*
7735  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7736  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7737  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7738  * completion.
7739  */
7740 ipsq_t *
7741 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7742     ipsq_func_t func, int type, boolean_t reentry_ok)
7743 {
7744 	ipsq_t	*ipsq;
7745 
7746 	/* Only 1 of ipif or ill can be specified */
7747 	ASSERT((ipif != NULL) ^ (ill != NULL));
7748 	if (ipif != NULL)
7749 		ill = ipif->ipif_ill;
7750 
7751 	/*
7752 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7753 	 * ipsq of an ill can't change when ill_lock is held.
7754 	 */
7755 	GRAB_CONN_LOCK(q);
7756 	mutex_enter(&ill->ill_lock);
7757 	ipsq = ill->ill_phyint->phyint_ipsq;
7758 	mutex_enter(&ipsq->ipsq_lock);
7759 
7760 	/*
7761 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7762 	 *    (Note: If the caller does not specify reentry_ok then neither
7763 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7764 	 *    again. Otherwise it can lead to an infinite loop
7765 	 * 2. Enter the ipsq if there is no current writer and this attempted
7766 	 *    entry is part of the current ioctl or operation
7767 	 * 3. Enter the ipsq if there is no current writer and this is a new
7768 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7769 	 *    empty and there is no ioctl (or operation) currently in progress
7770 	 */
7771 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7772 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7773 	    ipsq->ipsq_current_ipif == NULL))) ||
7774 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7775 		/* Success. */
7776 		ipsq->ipsq_reentry_cnt++;
7777 		ipsq->ipsq_writer = curthread;
7778 		mutex_exit(&ipsq->ipsq_lock);
7779 		mutex_exit(&ill->ill_lock);
7780 		RELEASE_CONN_LOCK(q);
7781 #ifdef DEBUG
7782 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7783 		    IPSQ_STACK_DEPTH);
7784 #endif
7785 		return (ipsq);
7786 	}
7787 
7788 	ipsq_enq(ipsq, q, mp, func, type, ill);
7789 
7790 	mutex_exit(&ipsq->ipsq_lock);
7791 	mutex_exit(&ill->ill_lock);
7792 	RELEASE_CONN_LOCK(q);
7793 	return (NULL);
7794 }
7795 
7796 /*
7797  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7798  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7799  * cannot be entered, the mp is queued for completion.
7800  */
7801 void
7802 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7803     boolean_t reentry_ok)
7804 {
7805 	ipsq_t	*ipsq;
7806 
7807 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7808 
7809 	/*
7810 	 * Drop the caller's refhold on the ill.  This is safe since we either
7811 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7812 	 * IPSQ, in which case we return without accessing ill anymore.  This
7813 	 * is needed because func needs to see the correct refcount.
7814 	 * e.g. removeif can work only then.
7815 	 */
7816 	ill_refrele(ill);
7817 	if (ipsq != NULL) {
7818 		(*func)(ipsq, q, mp, NULL);
7819 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7820 	}
7821 }
7822 
7823 /*
7824  * If there are more than ILL_GRP_CNT ills in a group,
7825  * we use kmem alloc'd buffers, else use the stack
7826  */
7827 #define	ILL_GRP_CNT	14
7828 /*
7829  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7830  * Called by a thread that is currently exclusive on this ipsq.
7831  */
7832 void
7833 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7834 {
7835 	queue_t	*q;
7836 	mblk_t	*mp;
7837 	ipsq_func_t	func;
7838 	int	next;
7839 	ill_t	**ill_list = NULL;
7840 	size_t	ill_list_size = 0;
7841 	int	cnt = 0;
7842 	boolean_t need_ipsq_free = B_FALSE;
7843 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7844 
7845 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7846 	mutex_enter(&ipsq->ipsq_lock);
7847 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7848 	if (ipsq->ipsq_reentry_cnt != 1) {
7849 		ipsq->ipsq_reentry_cnt--;
7850 		mutex_exit(&ipsq->ipsq_lock);
7851 		return;
7852 	}
7853 
7854 	mp = ipsq_dq(ipsq);
7855 	while (mp != NULL) {
7856 again:
7857 		mutex_exit(&ipsq->ipsq_lock);
7858 		func = (ipsq_func_t)mp->b_prev;
7859 		q = (queue_t *)mp->b_queue;
7860 		mp->b_prev = NULL;
7861 		mp->b_queue = NULL;
7862 
7863 		/*
7864 		 * If 'q' is an conn queue, it is valid, since we did a
7865 		 * a refhold on the connp, at the start of the ioctl.
7866 		 * If 'q' is an ill queue, it is valid, since close of an
7867 		 * ill will clean up the 'ipsq'.
7868 		 */
7869 		(*func)(ipsq, q, mp, NULL);
7870 
7871 		mutex_enter(&ipsq->ipsq_lock);
7872 		mp = ipsq_dq(ipsq);
7873 	}
7874 
7875 	mutex_exit(&ipsq->ipsq_lock);
7876 
7877 	/*
7878 	 * Need to grab the locks in the right order. Need to
7879 	 * atomically check (under ipsq_lock) that there are no
7880 	 * messages before relinquishing the ipsq. Also need to
7881 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7882 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7883 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7884 	 * to grab ill_g_lock as writer.
7885 	 */
7886 	rw_enter(&ipst->ips_ill_g_lock,
7887 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7888 
7889 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7890 	if (ipsq->ipsq_refs != 0) {
7891 		/* At most 2 ills v4/v6 per phyint */
7892 		cnt = ipsq->ipsq_refs << 1;
7893 		ill_list_size = cnt * sizeof (ill_t *);
7894 		/*
7895 		 * If memory allocation fails, we will do the split
7896 		 * the next time ipsq_exit is called for whatever reason.
7897 		 * As long as the ipsq_split flag is set the need to
7898 		 * split is remembered.
7899 		 */
7900 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7901 		if (ill_list != NULL)
7902 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7903 	}
7904 	mutex_enter(&ipsq->ipsq_lock);
7905 	mp = ipsq_dq(ipsq);
7906 	if (mp != NULL) {
7907 		/* oops, some message has landed up, we can't get out */
7908 		if (ill_list != NULL)
7909 			ill_unlock_ills(ill_list, cnt);
7910 		rw_exit(&ipst->ips_ill_g_lock);
7911 		if (ill_list != NULL)
7912 			kmem_free(ill_list, ill_list_size);
7913 		ill_list = NULL;
7914 		ill_list_size = 0;
7915 		cnt = 0;
7916 		goto again;
7917 	}
7918 
7919 	/*
7920 	 * Split only if no ioctl is pending and if memory alloc succeeded
7921 	 * above.
7922 	 */
7923 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7924 	    ill_list != NULL) {
7925 		/*
7926 		 * No new ill can join this ipsq since we are holding the
7927 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7928 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7929 		 * If so we will retry on the next ipsq_exit.
7930 		 */
7931 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7932 	}
7933 
7934 	/*
7935 	 * We are holding the ipsq lock, hence no new messages can
7936 	 * land up on the ipsq, and there are no messages currently.
7937 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7938 	 * atomically while holding ill locks.
7939 	 */
7940 	ipsq->ipsq_writer = NULL;
7941 	ipsq->ipsq_reentry_cnt--;
7942 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7943 #ifdef DEBUG
7944 	ipsq->ipsq_depth = 0;
7945 #endif
7946 	mutex_exit(&ipsq->ipsq_lock);
7947 	/*
7948 	 * For IPMP this should wake up all ills in this ipsq.
7949 	 * We need to hold the ill_lock while waking up waiters to
7950 	 * avoid missed wakeups. But there is no need to acquire all
7951 	 * the ill locks and then wakeup. If we have not acquired all
7952 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7953 	 * wakes up ills one at a time after getting the right ill_lock
7954 	 */
7955 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7956 	if (ill_list != NULL)
7957 		ill_unlock_ills(ill_list, cnt);
7958 	if (ipsq->ipsq_refs == 0)
7959 		need_ipsq_free = B_TRUE;
7960 	rw_exit(&ipst->ips_ill_g_lock);
7961 	if (ill_list != 0)
7962 		kmem_free(ill_list, ill_list_size);
7963 
7964 	if (need_ipsq_free) {
7965 		/*
7966 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7967 		 * looked up. ipsq can be looked up only thru ill or phyint
7968 		 * and there are no ills/phyint on this ipsq.
7969 		 */
7970 		ipsq_delete(ipsq);
7971 	}
7972 	/*
7973 	 * Now start any igmp or mld timers that could not be started
7974 	 * while inside the ipsq. The timers can't be started while inside
7975 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7976 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7977 	 * there could be a deadlock since the timeout handlers
7978 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7979 	 * wait in ipsq_enter() trying to get the ipsq.
7980 	 *
7981 	 * However there is one exception to the above. If this thread is
7982 	 * itself the igmp/mld timeout handler thread, then we don't want
7983 	 * to start any new timer until the current handler is done. The
7984 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7985 	 * all others pass B_TRUE.
7986 	 */
7987 	if (start_igmp_timer) {
7988 		mutex_enter(&ipst->ips_igmp_timer_lock);
7989 		next = ipst->ips_igmp_deferred_next;
7990 		ipst->ips_igmp_deferred_next = INFINITY;
7991 		mutex_exit(&ipst->ips_igmp_timer_lock);
7992 
7993 		if (next != INFINITY)
7994 			igmp_start_timers(next, ipst);
7995 	}
7996 
7997 	if (start_mld_timer) {
7998 		mutex_enter(&ipst->ips_mld_timer_lock);
7999 		next = ipst->ips_mld_deferred_next;
8000 		ipst->ips_mld_deferred_next = INFINITY;
8001 		mutex_exit(&ipst->ips_mld_timer_lock);
8002 
8003 		if (next != INFINITY)
8004 			mld_start_timers(next, ipst);
8005 	}
8006 }
8007 
8008 /*
8009  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8010  * and `ioccmd'.
8011  */
8012 void
8013 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8014 {
8015 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8016 
8017 	mutex_enter(&ipsq->ipsq_lock);
8018 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8019 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8020 	ipsq->ipsq_current_ipif = ipif;
8021 	ipsq->ipsq_current_ioctl = ioccmd;
8022 	mutex_exit(&ipsq->ipsq_lock);
8023 }
8024 
8025 /*
8026  * Finish the current exclusive operation on `ipsq'.  Note that other
8027  * operations will not be able to proceed until an ipsq_exit() is done.
8028  */
8029 void
8030 ipsq_current_finish(ipsq_t *ipsq)
8031 {
8032 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8033 
8034 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8035 
8036 	/*
8037 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8038 	 * (but we're careful to never set IPIF_CHANGING in that case).
8039 	 */
8040 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8041 		mutex_enter(&ipif->ipif_ill->ill_lock);
8042 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8043 
8044 		/* Send any queued event */
8045 		ill_nic_info_dispatch(ipif->ipif_ill);
8046 		mutex_exit(&ipif->ipif_ill->ill_lock);
8047 	}
8048 
8049 	mutex_enter(&ipsq->ipsq_lock);
8050 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8051 	ipsq->ipsq_current_ipif = NULL;
8052 	ipsq->ipsq_current_ioctl = 0;
8053 	mutex_exit(&ipsq->ipsq_lock);
8054 }
8055 
8056 /*
8057  * The ill is closing. Flush all messages on the ipsq that originated
8058  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8059  * for this ill since ipsq_enter could not have entered until then.
8060  * New messages can't be queued since the CONDEMNED flag is set.
8061  */
8062 static void
8063 ipsq_flush(ill_t *ill)
8064 {
8065 	queue_t	*q;
8066 	mblk_t	*prev;
8067 	mblk_t	*mp;
8068 	mblk_t	*mp_next;
8069 	ipsq_t	*ipsq;
8070 
8071 	ASSERT(IAM_WRITER_ILL(ill));
8072 	ipsq = ill->ill_phyint->phyint_ipsq;
8073 	/*
8074 	 * Flush any messages sent up by the driver.
8075 	 */
8076 	mutex_enter(&ipsq->ipsq_lock);
8077 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8078 		mp_next = mp->b_next;
8079 		q = mp->b_queue;
8080 		if (q == ill->ill_rq || q == ill->ill_wq) {
8081 			/* Remove the mp from the ipsq */
8082 			if (prev == NULL)
8083 				ipsq->ipsq_mphead = mp->b_next;
8084 			else
8085 				prev->b_next = mp->b_next;
8086 			if (ipsq->ipsq_mptail == mp) {
8087 				ASSERT(mp_next == NULL);
8088 				ipsq->ipsq_mptail = prev;
8089 			}
8090 			inet_freemsg(mp);
8091 		} else {
8092 			prev = mp;
8093 		}
8094 	}
8095 	mutex_exit(&ipsq->ipsq_lock);
8096 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8097 	ipsq_xopq_mp_cleanup(ill, NULL);
8098 	ill_pending_mp_cleanup(ill);
8099 }
8100 
8101 /* ARGSUSED */
8102 int
8103 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8104     ip_ioctl_cmd_t *ipip, void *ifreq)
8105 {
8106 	ill_t	*ill;
8107 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8108 	boolean_t isv6;
8109 	conn_t	*connp;
8110 	ip_stack_t	*ipst;
8111 
8112 	connp = Q_TO_CONN(q);
8113 	ipst = connp->conn_netstack->netstack_ip;
8114 	isv6 = connp->conn_af_isv6;
8115 	/*
8116 	 * Set original index.
8117 	 * Failover and failback move logical interfaces
8118 	 * from one physical interface to another.  The
8119 	 * original index indicates the parent of a logical
8120 	 * interface, in other words, the physical interface
8121 	 * the logical interface will be moved back to on
8122 	 * failback.
8123 	 */
8124 
8125 	/*
8126 	 * Don't allow the original index to be changed
8127 	 * for non-failover addresses, autoconfigured
8128 	 * addresses, or IPv6 link local addresses.
8129 	 */
8130 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8131 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8132 		return (EINVAL);
8133 	}
8134 	/*
8135 	 * The new original index must be in use by some
8136 	 * physical interface.
8137 	 */
8138 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8139 	    NULL, NULL, ipst);
8140 	if (ill == NULL)
8141 		return (ENXIO);
8142 	ill_refrele(ill);
8143 
8144 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8145 	/*
8146 	 * When this ipif gets failed back, don't
8147 	 * preserve the original id, as it is no
8148 	 * longer applicable.
8149 	 */
8150 	ipif->ipif_orig_ipifid = 0;
8151 	/*
8152 	 * For IPv4, change the original index of any
8153 	 * multicast addresses associated with the
8154 	 * ipif to the new value.
8155 	 */
8156 	if (!isv6) {
8157 		ilm_t *ilm;
8158 
8159 		mutex_enter(&ipif->ipif_ill->ill_lock);
8160 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8161 		    ilm = ilm->ilm_next) {
8162 			if (ilm->ilm_ipif == ipif) {
8163 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8164 			}
8165 		}
8166 		mutex_exit(&ipif->ipif_ill->ill_lock);
8167 	}
8168 	return (0);
8169 }
8170 
8171 /* ARGSUSED */
8172 int
8173 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8174     ip_ioctl_cmd_t *ipip, void *ifreq)
8175 {
8176 	struct lifreq *lifr = (struct lifreq *)ifreq;
8177 
8178 	/*
8179 	 * Get the original interface index i.e the one
8180 	 * before FAILOVER if it ever happened.
8181 	 */
8182 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8183 	return (0);
8184 }
8185 
8186 /*
8187  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8188  * refhold and return the associated ipif
8189  */
8190 /* ARGSUSED */
8191 int
8192 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8193     cmd_info_t *ci, ipsq_func_t func)
8194 {
8195 	boolean_t exists;
8196 	struct iftun_req *ta;
8197 	ipif_t	*ipif;
8198 	ill_t	*ill;
8199 	boolean_t isv6;
8200 	mblk_t	*mp1;
8201 	int	error;
8202 	conn_t	*connp;
8203 	ip_stack_t	*ipst;
8204 
8205 	/* Existence verified in ip_wput_nondata */
8206 	mp1 = mp->b_cont->b_cont;
8207 	ta = (struct iftun_req *)mp1->b_rptr;
8208 	/*
8209 	 * Null terminate the string to protect against buffer
8210 	 * overrun. String was generated by user code and may not
8211 	 * be trusted.
8212 	 */
8213 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8214 
8215 	connp = Q_TO_CONN(q);
8216 	isv6 = connp->conn_af_isv6;
8217 	ipst = connp->conn_netstack->netstack_ip;
8218 
8219 	/* Disallows implicit create */
8220 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8221 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8222 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8223 	if (ipif == NULL)
8224 		return (error);
8225 
8226 	if (ipif->ipif_id != 0) {
8227 		/*
8228 		 * We really don't want to set/get tunnel parameters
8229 		 * on virtual tunnel interfaces.  Only allow the
8230 		 * base tunnel to do these.
8231 		 */
8232 		ipif_refrele(ipif);
8233 		return (EINVAL);
8234 	}
8235 
8236 	/*
8237 	 * Send down to tunnel mod for ioctl processing.
8238 	 * Will finish ioctl in ip_rput_other().
8239 	 */
8240 	ill = ipif->ipif_ill;
8241 	if (ill->ill_net_type == IRE_LOOPBACK) {
8242 		ipif_refrele(ipif);
8243 		return (EOPNOTSUPP);
8244 	}
8245 
8246 	if (ill->ill_wq == NULL) {
8247 		ipif_refrele(ipif);
8248 		return (ENXIO);
8249 	}
8250 	/*
8251 	 * Mark the ioctl as coming from an IPv6 interface for
8252 	 * tun's convenience.
8253 	 */
8254 	if (ill->ill_isv6)
8255 		ta->ifta_flags |= 0x80000000;
8256 	ci->ci_ipif = ipif;
8257 	return (0);
8258 }
8259 
8260 /*
8261  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8262  * and return the associated ipif.
8263  * Return value:
8264  *	Non zero: An error has occurred. ci may not be filled out.
8265  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8266  *	a held ipif in ci.ci_ipif.
8267  */
8268 int
8269 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8270     cmd_info_t *ci, ipsq_func_t func)
8271 {
8272 	sin_t		*sin;
8273 	sin6_t		*sin6;
8274 	char		*name;
8275 	struct ifreq    *ifr;
8276 	struct lifreq    *lifr;
8277 	ipif_t		*ipif = NULL;
8278 	ill_t		*ill;
8279 	conn_t		*connp;
8280 	boolean_t	isv6;
8281 	boolean_t	exists;
8282 	int		err;
8283 	mblk_t		*mp1;
8284 	zoneid_t	zoneid;
8285 	ip_stack_t	*ipst;
8286 
8287 	if (q->q_next != NULL) {
8288 		ill = (ill_t *)q->q_ptr;
8289 		isv6 = ill->ill_isv6;
8290 		connp = NULL;
8291 		zoneid = ALL_ZONES;
8292 		ipst = ill->ill_ipst;
8293 	} else {
8294 		ill = NULL;
8295 		connp = Q_TO_CONN(q);
8296 		isv6 = connp->conn_af_isv6;
8297 		zoneid = connp->conn_zoneid;
8298 		if (zoneid == GLOBAL_ZONEID) {
8299 			/* global zone can access ipifs in all zones */
8300 			zoneid = ALL_ZONES;
8301 		}
8302 		ipst = connp->conn_netstack->netstack_ip;
8303 	}
8304 
8305 	/* Has been checked in ip_wput_nondata */
8306 	mp1 = mp->b_cont->b_cont;
8307 
8308 	if (ipip->ipi_cmd_type == IF_CMD) {
8309 		/* This a old style SIOC[GS]IF* command */
8310 		ifr = (struct ifreq *)mp1->b_rptr;
8311 		/*
8312 		 * Null terminate the string to protect against buffer
8313 		 * overrun. String was generated by user code and may not
8314 		 * be trusted.
8315 		 */
8316 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8317 		sin = (sin_t *)&ifr->ifr_addr;
8318 		name = ifr->ifr_name;
8319 		ci->ci_sin = sin;
8320 		ci->ci_sin6 = NULL;
8321 		ci->ci_lifr = (struct lifreq *)ifr;
8322 	} else {
8323 		/* This a new style SIOC[GS]LIF* command */
8324 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8325 		lifr = (struct lifreq *)mp1->b_rptr;
8326 		/*
8327 		 * Null terminate the string to protect against buffer
8328 		 * overrun. String was generated by user code and may not
8329 		 * be trusted.
8330 		 */
8331 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8332 		name = lifr->lifr_name;
8333 		sin = (sin_t *)&lifr->lifr_addr;
8334 		sin6 = (sin6_t *)&lifr->lifr_addr;
8335 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8336 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8337 			    LIFNAMSIZ);
8338 		}
8339 		ci->ci_sin = sin;
8340 		ci->ci_sin6 = sin6;
8341 		ci->ci_lifr = lifr;
8342 	}
8343 
8344 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8345 		/*
8346 		 * The ioctl will be failed if the ioctl comes down
8347 		 * an conn stream
8348 		 */
8349 		if (ill == NULL) {
8350 			/*
8351 			 * Not an ill queue, return EINVAL same as the
8352 			 * old error code.
8353 			 */
8354 			return (ENXIO);
8355 		}
8356 		ipif = ill->ill_ipif;
8357 		ipif_refhold(ipif);
8358 	} else {
8359 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8360 		    &exists, isv6, zoneid,
8361 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8362 		    ipst);
8363 		if (ipif == NULL) {
8364 			if (err == EINPROGRESS)
8365 				return (err);
8366 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8367 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8368 				/*
8369 				 * Need to try both v4 and v6 since this
8370 				 * ioctl can come down either v4 or v6
8371 				 * socket. The lifreq.lifr_family passed
8372 				 * down by this ioctl is AF_UNSPEC.
8373 				 */
8374 				ipif = ipif_lookup_on_name(name,
8375 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8376 				    zoneid, (connp == NULL) ? q :
8377 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8378 				if (err == EINPROGRESS)
8379 					return (err);
8380 			}
8381 			err = 0;	/* Ensure we don't use it below */
8382 		}
8383 	}
8384 
8385 	/*
8386 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8387 	 */
8388 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8389 		ipif_refrele(ipif);
8390 		return (ENXIO);
8391 	}
8392 
8393 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8394 	    name[0] == '\0') {
8395 		/*
8396 		 * Handle a or a SIOC?IF* with a null name
8397 		 * during plumb (on the ill queue before the I_PLINK).
8398 		 */
8399 		ipif = ill->ill_ipif;
8400 		ipif_refhold(ipif);
8401 	}
8402 
8403 	if (ipif == NULL)
8404 		return (ENXIO);
8405 
8406 	/*
8407 	 * Allow only GET operations if this ipif has been created
8408 	 * temporarily due to a MOVE operation.
8409 	 */
8410 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8411 		ipif_refrele(ipif);
8412 		return (EINVAL);
8413 	}
8414 
8415 	ci->ci_ipif = ipif;
8416 	return (0);
8417 }
8418 
8419 /*
8420  * Return the total number of ipifs.
8421  */
8422 static uint_t
8423 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8424 {
8425 	uint_t numifs = 0;
8426 	ill_t	*ill;
8427 	ill_walk_context_t	ctx;
8428 	ipif_t	*ipif;
8429 
8430 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8431 	ill = ILL_START_WALK_V4(&ctx, ipst);
8432 
8433 	while (ill != NULL) {
8434 		for (ipif = ill->ill_ipif; ipif != NULL;
8435 		    ipif = ipif->ipif_next) {
8436 			if (ipif->ipif_zoneid == zoneid ||
8437 			    ipif->ipif_zoneid == ALL_ZONES)
8438 				numifs++;
8439 		}
8440 		ill = ill_next(&ctx, ill);
8441 	}
8442 	rw_exit(&ipst->ips_ill_g_lock);
8443 	return (numifs);
8444 }
8445 
8446 /*
8447  * Return the total number of ipifs.
8448  */
8449 static uint_t
8450 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8451 {
8452 	uint_t numifs = 0;
8453 	ill_t	*ill;
8454 	ipif_t	*ipif;
8455 	ill_walk_context_t	ctx;
8456 
8457 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8458 
8459 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8460 	if (family == AF_INET)
8461 		ill = ILL_START_WALK_V4(&ctx, ipst);
8462 	else if (family == AF_INET6)
8463 		ill = ILL_START_WALK_V6(&ctx, ipst);
8464 	else
8465 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8466 
8467 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8468 		for (ipif = ill->ill_ipif; ipif != NULL;
8469 		    ipif = ipif->ipif_next) {
8470 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8471 			    !(lifn_flags & LIFC_NOXMIT))
8472 				continue;
8473 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8474 			    !(lifn_flags & LIFC_TEMPORARY))
8475 				continue;
8476 			if (((ipif->ipif_flags &
8477 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8478 			    IPIF_DEPRECATED)) ||
8479 			    IS_LOOPBACK(ill) ||
8480 			    !(ipif->ipif_flags & IPIF_UP)) &&
8481 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8482 				continue;
8483 
8484 			if (zoneid != ipif->ipif_zoneid &&
8485 			    ipif->ipif_zoneid != ALL_ZONES &&
8486 			    (zoneid != GLOBAL_ZONEID ||
8487 			    !(lifn_flags & LIFC_ALLZONES)))
8488 				continue;
8489 
8490 			numifs++;
8491 		}
8492 	}
8493 	rw_exit(&ipst->ips_ill_g_lock);
8494 	return (numifs);
8495 }
8496 
8497 uint_t
8498 ip_get_lifsrcofnum(ill_t *ill)
8499 {
8500 	uint_t numifs = 0;
8501 	ill_t	*ill_head = ill;
8502 	ip_stack_t	*ipst = ill->ill_ipst;
8503 
8504 	/*
8505 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8506 	 * other thread may be trying to relink the ILLs in this usesrc group
8507 	 * and adjusting the ill_usesrc_grp_next pointers
8508 	 */
8509 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8510 	if ((ill->ill_usesrc_ifindex == 0) &&
8511 	    (ill->ill_usesrc_grp_next != NULL)) {
8512 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8513 		    ill = ill->ill_usesrc_grp_next)
8514 			numifs++;
8515 	}
8516 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8517 
8518 	return (numifs);
8519 }
8520 
8521 /* Null values are passed in for ipif, sin, and ifreq */
8522 /* ARGSUSED */
8523 int
8524 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8525     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8526 {
8527 	int *nump;
8528 	conn_t *connp = Q_TO_CONN(q);
8529 
8530 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8531 
8532 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8533 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8534 
8535 	*nump = ip_get_numifs(connp->conn_zoneid,
8536 	    connp->conn_netstack->netstack_ip);
8537 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8538 	return (0);
8539 }
8540 
8541 /* Null values are passed in for ipif, sin, and ifreq */
8542 /* ARGSUSED */
8543 int
8544 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8545     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8546 {
8547 	struct lifnum *lifn;
8548 	mblk_t	*mp1;
8549 	conn_t *connp = Q_TO_CONN(q);
8550 
8551 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8552 
8553 	/* Existence checked in ip_wput_nondata */
8554 	mp1 = mp->b_cont->b_cont;
8555 
8556 	lifn = (struct lifnum *)mp1->b_rptr;
8557 	switch (lifn->lifn_family) {
8558 	case AF_UNSPEC:
8559 	case AF_INET:
8560 	case AF_INET6:
8561 		break;
8562 	default:
8563 		return (EAFNOSUPPORT);
8564 	}
8565 
8566 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8567 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8568 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8569 	return (0);
8570 }
8571 
8572 /* ARGSUSED */
8573 int
8574 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8575     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8576 {
8577 	STRUCT_HANDLE(ifconf, ifc);
8578 	mblk_t *mp1;
8579 	struct iocblk *iocp;
8580 	struct ifreq *ifr;
8581 	ill_walk_context_t	ctx;
8582 	ill_t	*ill;
8583 	ipif_t	*ipif;
8584 	struct sockaddr_in *sin;
8585 	int32_t	ifclen;
8586 	zoneid_t zoneid;
8587 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8588 
8589 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8590 
8591 	ip1dbg(("ip_sioctl_get_ifconf"));
8592 	/* Existence verified in ip_wput_nondata */
8593 	mp1 = mp->b_cont->b_cont;
8594 	iocp = (struct iocblk *)mp->b_rptr;
8595 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8596 
8597 	/*
8598 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8599 	 * the user buffer address and length into which the list of struct
8600 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8601 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8602 	 * the SIOCGIFCONF operation was redefined to simply provide
8603 	 * a large output buffer into which we are supposed to jam the ifreq
8604 	 * array.  The same ioctl command code was used, despite the fact that
8605 	 * both the applications and the kernel code had to change, thus making
8606 	 * it impossible to support both interfaces.
8607 	 *
8608 	 * For reasons not good enough to try to explain, the following
8609 	 * algorithm is used for deciding what to do with one of these:
8610 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8611 	 * form with the output buffer coming down as the continuation message.
8612 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8613 	 * and we have to copy in the ifconf structure to find out how big the
8614 	 * output buffer is and where to copy out to.  Sure no problem...
8615 	 *
8616 	 */
8617 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8618 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8619 		int numifs = 0;
8620 		size_t ifc_bufsize;
8621 
8622 		/*
8623 		 * Must be (better be!) continuation of a TRANSPARENT
8624 		 * IOCTL.  We just copied in the ifconf structure.
8625 		 */
8626 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8627 		    (struct ifconf *)mp1->b_rptr);
8628 
8629 		/*
8630 		 * Allocate a buffer to hold requested information.
8631 		 *
8632 		 * If ifc_len is larger than what is needed, we only
8633 		 * allocate what we will use.
8634 		 *
8635 		 * If ifc_len is smaller than what is needed, return
8636 		 * EINVAL.
8637 		 *
8638 		 * XXX: the ill_t structure can hava 2 counters, for
8639 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8640 		 * number of interfaces for a device, so we don't need
8641 		 * to count them here...
8642 		 */
8643 		numifs = ip_get_numifs(zoneid, ipst);
8644 
8645 		ifclen = STRUCT_FGET(ifc, ifc_len);
8646 		ifc_bufsize = numifs * sizeof (struct ifreq);
8647 		if (ifc_bufsize > ifclen) {
8648 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8649 				/* old behaviour */
8650 				return (EINVAL);
8651 			} else {
8652 				ifc_bufsize = ifclen;
8653 			}
8654 		}
8655 
8656 		mp1 = mi_copyout_alloc(q, mp,
8657 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8658 		if (mp1 == NULL)
8659 			return (ENOMEM);
8660 
8661 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8662 	}
8663 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8664 	/*
8665 	 * the SIOCGIFCONF ioctl only knows about
8666 	 * IPv4 addresses, so don't try to tell
8667 	 * it about interfaces with IPv6-only
8668 	 * addresses. (Last parm 'isv6' is B_FALSE)
8669 	 */
8670 
8671 	ifr = (struct ifreq *)mp1->b_rptr;
8672 
8673 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8674 	ill = ILL_START_WALK_V4(&ctx, ipst);
8675 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8676 		for (ipif = ill->ill_ipif; ipif != NULL;
8677 		    ipif = ipif->ipif_next) {
8678 			if (zoneid != ipif->ipif_zoneid &&
8679 			    ipif->ipif_zoneid != ALL_ZONES)
8680 				continue;
8681 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8682 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8683 					/* old behaviour */
8684 					rw_exit(&ipst->ips_ill_g_lock);
8685 					return (EINVAL);
8686 				} else {
8687 					goto if_copydone;
8688 				}
8689 			}
8690 			ipif_get_name(ipif, ifr->ifr_name,
8691 			    sizeof (ifr->ifr_name));
8692 			sin = (sin_t *)&ifr->ifr_addr;
8693 			*sin = sin_null;
8694 			sin->sin_family = AF_INET;
8695 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8696 			ifr++;
8697 		}
8698 	}
8699 if_copydone:
8700 	rw_exit(&ipst->ips_ill_g_lock);
8701 	mp1->b_wptr = (uchar_t *)ifr;
8702 
8703 	if (STRUCT_BUF(ifc) != NULL) {
8704 		STRUCT_FSET(ifc, ifc_len,
8705 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8706 	}
8707 	return (0);
8708 }
8709 
8710 /*
8711  * Get the interfaces using the address hosted on the interface passed in,
8712  * as a source adddress
8713  */
8714 /* ARGSUSED */
8715 int
8716 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8717     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8718 {
8719 	mblk_t *mp1;
8720 	ill_t	*ill, *ill_head;
8721 	ipif_t	*ipif, *orig_ipif;
8722 	int	numlifs = 0;
8723 	size_t	lifs_bufsize, lifsmaxlen;
8724 	struct	lifreq *lifr;
8725 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8726 	uint_t	ifindex;
8727 	zoneid_t zoneid;
8728 	int err = 0;
8729 	boolean_t isv6 = B_FALSE;
8730 	struct	sockaddr_in	*sin;
8731 	struct	sockaddr_in6	*sin6;
8732 	STRUCT_HANDLE(lifsrcof, lifs);
8733 	ip_stack_t		*ipst;
8734 
8735 	ipst = CONNQ_TO_IPST(q);
8736 
8737 	ASSERT(q->q_next == NULL);
8738 
8739 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8740 
8741 	/* Existence verified in ip_wput_nondata */
8742 	mp1 = mp->b_cont->b_cont;
8743 
8744 	/*
8745 	 * Must be (better be!) continuation of a TRANSPARENT
8746 	 * IOCTL.  We just copied in the lifsrcof structure.
8747 	 */
8748 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8749 	    (struct lifsrcof *)mp1->b_rptr);
8750 
8751 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8752 		return (EINVAL);
8753 
8754 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8755 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8756 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8757 	    ip_process_ioctl, &err, ipst);
8758 	if (ipif == NULL) {
8759 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8760 		    ifindex));
8761 		return (err);
8762 	}
8763 
8764 
8765 	/* Allocate a buffer to hold requested information */
8766 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8767 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8768 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8769 	/* The actual size needed is always returned in lifs_len */
8770 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8771 
8772 	/* If the amount we need is more than what is passed in, abort */
8773 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8774 		ipif_refrele(ipif);
8775 		return (0);
8776 	}
8777 
8778 	mp1 = mi_copyout_alloc(q, mp,
8779 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8780 	if (mp1 == NULL) {
8781 		ipif_refrele(ipif);
8782 		return (ENOMEM);
8783 	}
8784 
8785 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8786 	bzero(mp1->b_rptr, lifs_bufsize);
8787 
8788 	lifr = (struct lifreq *)mp1->b_rptr;
8789 
8790 	ill = ill_head = ipif->ipif_ill;
8791 	orig_ipif = ipif;
8792 
8793 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8794 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8795 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8796 
8797 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8798 	for (; (ill != NULL) && (ill != ill_head);
8799 	    ill = ill->ill_usesrc_grp_next) {
8800 
8801 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8802 			break;
8803 
8804 		ipif = ill->ill_ipif;
8805 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8806 		if (ipif->ipif_isv6) {
8807 			sin6 = (sin6_t *)&lifr->lifr_addr;
8808 			*sin6 = sin6_null;
8809 			sin6->sin6_family = AF_INET6;
8810 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8811 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8812 			    &ipif->ipif_v6net_mask);
8813 		} else {
8814 			sin = (sin_t *)&lifr->lifr_addr;
8815 			*sin = sin_null;
8816 			sin->sin_family = AF_INET;
8817 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8818 			lifr->lifr_addrlen = ip_mask_to_plen(
8819 			    ipif->ipif_net_mask);
8820 		}
8821 		lifr++;
8822 	}
8823 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8824 	rw_exit(&ipst->ips_ill_g_lock);
8825 	ipif_refrele(orig_ipif);
8826 	mp1->b_wptr = (uchar_t *)lifr;
8827 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8828 
8829 	return (0);
8830 }
8831 
8832 /* ARGSUSED */
8833 int
8834 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8835     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8836 {
8837 	mblk_t *mp1;
8838 	int	list;
8839 	ill_t	*ill;
8840 	ipif_t	*ipif;
8841 	int	flags;
8842 	int	numlifs = 0;
8843 	size_t	lifc_bufsize;
8844 	struct	lifreq *lifr;
8845 	sa_family_t	family;
8846 	struct	sockaddr_in	*sin;
8847 	struct	sockaddr_in6	*sin6;
8848 	ill_walk_context_t	ctx;
8849 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8850 	int32_t	lifclen;
8851 	zoneid_t zoneid;
8852 	STRUCT_HANDLE(lifconf, lifc);
8853 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8854 
8855 	ip1dbg(("ip_sioctl_get_lifconf"));
8856 
8857 	ASSERT(q->q_next == NULL);
8858 
8859 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8860 
8861 	/* Existence verified in ip_wput_nondata */
8862 	mp1 = mp->b_cont->b_cont;
8863 
8864 	/*
8865 	 * An extended version of SIOCGIFCONF that takes an
8866 	 * additional address family and flags field.
8867 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8868 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8869 	 * interfaces are omitted.
8870 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8871 	 * unless LIFC_TEMPORARY is specified.
8872 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8873 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8874 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8875 	 * has priority over LIFC_NOXMIT.
8876 	 */
8877 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8878 
8879 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8880 		return (EINVAL);
8881 
8882 	/*
8883 	 * Must be (better be!) continuation of a TRANSPARENT
8884 	 * IOCTL.  We just copied in the lifconf structure.
8885 	 */
8886 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8887 
8888 	family = STRUCT_FGET(lifc, lifc_family);
8889 	flags = STRUCT_FGET(lifc, lifc_flags);
8890 
8891 	switch (family) {
8892 	case AF_UNSPEC:
8893 		/*
8894 		 * walk all ILL's.
8895 		 */
8896 		list = MAX_G_HEADS;
8897 		break;
8898 	case AF_INET:
8899 		/*
8900 		 * walk only IPV4 ILL's.
8901 		 */
8902 		list = IP_V4_G_HEAD;
8903 		break;
8904 	case AF_INET6:
8905 		/*
8906 		 * walk only IPV6 ILL's.
8907 		 */
8908 		list = IP_V6_G_HEAD;
8909 		break;
8910 	default:
8911 		return (EAFNOSUPPORT);
8912 	}
8913 
8914 	/*
8915 	 * Allocate a buffer to hold requested information.
8916 	 *
8917 	 * If lifc_len is larger than what is needed, we only
8918 	 * allocate what we will use.
8919 	 *
8920 	 * If lifc_len is smaller than what is needed, return
8921 	 * EINVAL.
8922 	 */
8923 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8924 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8925 	lifclen = STRUCT_FGET(lifc, lifc_len);
8926 	if (lifc_bufsize > lifclen) {
8927 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8928 			return (EINVAL);
8929 		else
8930 			lifc_bufsize = lifclen;
8931 	}
8932 
8933 	mp1 = mi_copyout_alloc(q, mp,
8934 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8935 	if (mp1 == NULL)
8936 		return (ENOMEM);
8937 
8938 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8939 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8940 
8941 	lifr = (struct lifreq *)mp1->b_rptr;
8942 
8943 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8944 	ill = ill_first(list, list, &ctx, ipst);
8945 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8946 		for (ipif = ill->ill_ipif; ipif != NULL;
8947 		    ipif = ipif->ipif_next) {
8948 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8949 			    !(flags & LIFC_NOXMIT))
8950 				continue;
8951 
8952 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8953 			    !(flags & LIFC_TEMPORARY))
8954 				continue;
8955 
8956 			if (((ipif->ipif_flags &
8957 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8958 			    IPIF_DEPRECATED)) ||
8959 			    IS_LOOPBACK(ill) ||
8960 			    !(ipif->ipif_flags & IPIF_UP)) &&
8961 			    (flags & LIFC_EXTERNAL_SOURCE))
8962 				continue;
8963 
8964 			if (zoneid != ipif->ipif_zoneid &&
8965 			    ipif->ipif_zoneid != ALL_ZONES &&
8966 			    (zoneid != GLOBAL_ZONEID ||
8967 			    !(flags & LIFC_ALLZONES)))
8968 				continue;
8969 
8970 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8971 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8972 					rw_exit(&ipst->ips_ill_g_lock);
8973 					return (EINVAL);
8974 				} else {
8975 					goto lif_copydone;
8976 				}
8977 			}
8978 
8979 			ipif_get_name(ipif, lifr->lifr_name,
8980 			    sizeof (lifr->lifr_name));
8981 			if (ipif->ipif_isv6) {
8982 				sin6 = (sin6_t *)&lifr->lifr_addr;
8983 				*sin6 = sin6_null;
8984 				sin6->sin6_family = AF_INET6;
8985 				sin6->sin6_addr =
8986 				    ipif->ipif_v6lcl_addr;
8987 				lifr->lifr_addrlen =
8988 				    ip_mask_to_plen_v6(
8989 				    &ipif->ipif_v6net_mask);
8990 			} else {
8991 				sin = (sin_t *)&lifr->lifr_addr;
8992 				*sin = sin_null;
8993 				sin->sin_family = AF_INET;
8994 				sin->sin_addr.s_addr =
8995 				    ipif->ipif_lcl_addr;
8996 				lifr->lifr_addrlen =
8997 				    ip_mask_to_plen(
8998 				    ipif->ipif_net_mask);
8999 			}
9000 			lifr++;
9001 		}
9002 	}
9003 lif_copydone:
9004 	rw_exit(&ipst->ips_ill_g_lock);
9005 
9006 	mp1->b_wptr = (uchar_t *)lifr;
9007 	if (STRUCT_BUF(lifc) != NULL) {
9008 		STRUCT_FSET(lifc, lifc_len,
9009 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9010 	}
9011 	return (0);
9012 }
9013 
9014 /* ARGSUSED */
9015 int
9016 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9017     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9018 {
9019 	ip_stack_t	*ipst;
9020 
9021 	if (q->q_next == NULL)
9022 		ipst = CONNQ_TO_IPST(q);
9023 	else
9024 		ipst = ILLQ_TO_IPST(q);
9025 
9026 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9027 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9028 	return (0);
9029 }
9030 
9031 static void
9032 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9033 {
9034 	ip6_asp_t *table;
9035 	size_t table_size;
9036 	mblk_t *data_mp;
9037 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9038 	ip_stack_t	*ipst;
9039 
9040 	if (q->q_next == NULL)
9041 		ipst = CONNQ_TO_IPST(q);
9042 	else
9043 		ipst = ILLQ_TO_IPST(q);
9044 
9045 	/* These two ioctls are I_STR only */
9046 	if (iocp->ioc_count == TRANSPARENT) {
9047 		miocnak(q, mp, 0, EINVAL);
9048 		return;
9049 	}
9050 
9051 	data_mp = mp->b_cont;
9052 	if (data_mp == NULL) {
9053 		/* The user passed us a NULL argument */
9054 		table = NULL;
9055 		table_size = iocp->ioc_count;
9056 	} else {
9057 		/*
9058 		 * The user provided a table.  The stream head
9059 		 * may have copied in the user data in chunks,
9060 		 * so make sure everything is pulled up
9061 		 * properly.
9062 		 */
9063 		if (MBLKL(data_mp) < iocp->ioc_count) {
9064 			mblk_t *new_data_mp;
9065 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9066 			    NULL) {
9067 				miocnak(q, mp, 0, ENOMEM);
9068 				return;
9069 			}
9070 			freemsg(data_mp);
9071 			data_mp = new_data_mp;
9072 			mp->b_cont = data_mp;
9073 		}
9074 		table = (ip6_asp_t *)data_mp->b_rptr;
9075 		table_size = iocp->ioc_count;
9076 	}
9077 
9078 	switch (iocp->ioc_cmd) {
9079 	case SIOCGIP6ADDRPOLICY:
9080 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9081 		if (iocp->ioc_rval == -1)
9082 			iocp->ioc_error = EINVAL;
9083 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9084 		else if (table != NULL &&
9085 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9086 			ip6_asp_t *src = table;
9087 			ip6_asp32_t *dst = (void *)table;
9088 			int count = table_size / sizeof (ip6_asp_t);
9089 			int i;
9090 
9091 			/*
9092 			 * We need to do an in-place shrink of the array
9093 			 * to match the alignment attributes of the
9094 			 * 32-bit ABI looking at it.
9095 			 */
9096 			/* LINTED: logical expression always true: op "||" */
9097 			ASSERT(sizeof (*src) > sizeof (*dst));
9098 			for (i = 1; i < count; i++)
9099 				bcopy(src + i, dst + i, sizeof (*dst));
9100 		}
9101 #endif
9102 		break;
9103 
9104 	case SIOCSIP6ADDRPOLICY:
9105 		ASSERT(mp->b_prev == NULL);
9106 		mp->b_prev = (void *)q;
9107 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9108 		/*
9109 		 * We pass in the datamodel here so that the ip6_asp_replace()
9110 		 * routine can handle converting from 32-bit to native formats
9111 		 * where necessary.
9112 		 *
9113 		 * A better way to handle this might be to convert the inbound
9114 		 * data structure here, and hang it off a new 'mp'; thus the
9115 		 * ip6_asp_replace() logic would always be dealing with native
9116 		 * format data structures..
9117 		 *
9118 		 * (An even simpler way to handle these ioctls is to just
9119 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9120 		 * and just recompile everything that depends on it.)
9121 		 */
9122 #endif
9123 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9124 		    iocp->ioc_flag & IOC_MODELS);
9125 		return;
9126 	}
9127 
9128 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9129 	qreply(q, mp);
9130 }
9131 
9132 static void
9133 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9134 {
9135 	mblk_t 		*data_mp;
9136 	struct dstinforeq	*dir;
9137 	uint8_t		*end, *cur;
9138 	in6_addr_t	*daddr, *saddr;
9139 	ipaddr_t	v4daddr;
9140 	ire_t		*ire;
9141 	char		*slabel, *dlabel;
9142 	boolean_t	isipv4;
9143 	int		match_ire;
9144 	ill_t		*dst_ill;
9145 	ipif_t		*src_ipif, *ire_ipif;
9146 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9147 	zoneid_t	zoneid;
9148 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9149 
9150 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9151 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9152 
9153 	/*
9154 	 * This ioctl is I_STR only, and must have a
9155 	 * data mblk following the M_IOCTL mblk.
9156 	 */
9157 	data_mp = mp->b_cont;
9158 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9159 		miocnak(q, mp, 0, EINVAL);
9160 		return;
9161 	}
9162 
9163 	if (MBLKL(data_mp) < iocp->ioc_count) {
9164 		mblk_t *new_data_mp;
9165 
9166 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9167 			miocnak(q, mp, 0, ENOMEM);
9168 			return;
9169 		}
9170 		freemsg(data_mp);
9171 		data_mp = new_data_mp;
9172 		mp->b_cont = data_mp;
9173 	}
9174 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9175 
9176 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9177 	    end - cur >= sizeof (struct dstinforeq);
9178 	    cur += sizeof (struct dstinforeq)) {
9179 		dir = (struct dstinforeq *)cur;
9180 		daddr = &dir->dir_daddr;
9181 		saddr = &dir->dir_saddr;
9182 
9183 		/*
9184 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9185 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9186 		 * and ipif_select_source[_v6]() do not.
9187 		 */
9188 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9189 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9190 
9191 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9192 		if (isipv4) {
9193 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9194 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9195 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9196 		} else {
9197 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9198 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9199 		}
9200 		if (ire == NULL) {
9201 			dir->dir_dreachable = 0;
9202 
9203 			/* move on to next dst addr */
9204 			continue;
9205 		}
9206 		dir->dir_dreachable = 1;
9207 
9208 		ire_ipif = ire->ire_ipif;
9209 		if (ire_ipif == NULL)
9210 			goto next_dst;
9211 
9212 		/*
9213 		 * We expect to get back an interface ire or a
9214 		 * gateway ire cache entry.  For both types, the
9215 		 * output interface is ire_ipif->ipif_ill.
9216 		 */
9217 		dst_ill = ire_ipif->ipif_ill;
9218 		dir->dir_dmactype = dst_ill->ill_mactype;
9219 
9220 		if (isipv4) {
9221 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9222 		} else {
9223 			src_ipif = ipif_select_source_v6(dst_ill,
9224 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9225 			    zoneid);
9226 		}
9227 		if (src_ipif == NULL)
9228 			goto next_dst;
9229 
9230 		*saddr = src_ipif->ipif_v6lcl_addr;
9231 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9232 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9233 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9234 		dir->dir_sdeprecated =
9235 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9236 		ipif_refrele(src_ipif);
9237 next_dst:
9238 		ire_refrele(ire);
9239 	}
9240 	miocack(q, mp, iocp->ioc_count, 0);
9241 }
9242 
9243 
9244 /*
9245  * Check if this is an address assigned to this machine.
9246  * Skips interfaces that are down by using ire checks.
9247  * Translates mapped addresses to v4 addresses and then
9248  * treats them as such, returning true if the v4 address
9249  * associated with this mapped address is configured.
9250  * Note: Applications will have to be careful what they do
9251  * with the response; use of mapped addresses limits
9252  * what can be done with the socket, especially with
9253  * respect to socket options and ioctls - neither IPv4
9254  * options nor IPv6 sticky options/ancillary data options
9255  * may be used.
9256  */
9257 /* ARGSUSED */
9258 int
9259 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9260     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9261 {
9262 	struct sioc_addrreq *sia;
9263 	sin_t *sin;
9264 	ire_t *ire;
9265 	mblk_t *mp1;
9266 	zoneid_t zoneid;
9267 	ip_stack_t	*ipst;
9268 
9269 	ip1dbg(("ip_sioctl_tmyaddr"));
9270 
9271 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9272 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9273 	ipst = CONNQ_TO_IPST(q);
9274 
9275 	/* Existence verified in ip_wput_nondata */
9276 	mp1 = mp->b_cont->b_cont;
9277 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9278 	sin = (sin_t *)&sia->sa_addr;
9279 	switch (sin->sin_family) {
9280 	case AF_INET6: {
9281 		sin6_t *sin6 = (sin6_t *)sin;
9282 
9283 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9284 			ipaddr_t v4_addr;
9285 
9286 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9287 			    v4_addr);
9288 			ire = ire_ctable_lookup(v4_addr, 0,
9289 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9290 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9291 		} else {
9292 			in6_addr_t v6addr;
9293 
9294 			v6addr = sin6->sin6_addr;
9295 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9296 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9297 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9298 		}
9299 		break;
9300 	}
9301 	case AF_INET: {
9302 		ipaddr_t v4addr;
9303 
9304 		v4addr = sin->sin_addr.s_addr;
9305 		ire = ire_ctable_lookup(v4addr, 0,
9306 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9307 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9308 		break;
9309 	}
9310 	default:
9311 		return (EAFNOSUPPORT);
9312 	}
9313 	if (ire != NULL) {
9314 		sia->sa_res = 1;
9315 		ire_refrele(ire);
9316 	} else {
9317 		sia->sa_res = 0;
9318 	}
9319 	return (0);
9320 }
9321 
9322 /*
9323  * Check if this is an address assigned on-link i.e. neighbor,
9324  * and makes sure it's reachable from the current zone.
9325  * Returns true for my addresses as well.
9326  * Translates mapped addresses to v4 addresses and then
9327  * treats them as such, returning true if the v4 address
9328  * associated with this mapped address is configured.
9329  * Note: Applications will have to be careful what they do
9330  * with the response; use of mapped addresses limits
9331  * what can be done with the socket, especially with
9332  * respect to socket options and ioctls - neither IPv4
9333  * options nor IPv6 sticky options/ancillary data options
9334  * may be used.
9335  */
9336 /* ARGSUSED */
9337 int
9338 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9339     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9340 {
9341 	struct sioc_addrreq *sia;
9342 	sin_t *sin;
9343 	mblk_t	*mp1;
9344 	ire_t *ire = NULL;
9345 	zoneid_t zoneid;
9346 	ip_stack_t	*ipst;
9347 
9348 	ip1dbg(("ip_sioctl_tonlink"));
9349 
9350 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9351 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9352 	ipst = CONNQ_TO_IPST(q);
9353 
9354 	/* Existence verified in ip_wput_nondata */
9355 	mp1 = mp->b_cont->b_cont;
9356 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9357 	sin = (sin_t *)&sia->sa_addr;
9358 
9359 	/*
9360 	 * Match addresses with a zero gateway field to avoid
9361 	 * routes going through a router.
9362 	 * Exclude broadcast and multicast addresses.
9363 	 */
9364 	switch (sin->sin_family) {
9365 	case AF_INET6: {
9366 		sin6_t *sin6 = (sin6_t *)sin;
9367 
9368 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9369 			ipaddr_t v4_addr;
9370 
9371 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9372 			    v4_addr);
9373 			if (!CLASSD(v4_addr)) {
9374 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9375 				    NULL, NULL, zoneid, NULL,
9376 				    MATCH_IRE_GW, ipst);
9377 			}
9378 		} else {
9379 			in6_addr_t v6addr;
9380 			in6_addr_t v6gw;
9381 
9382 			v6addr = sin6->sin6_addr;
9383 			v6gw = ipv6_all_zeros;
9384 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9385 				ire = ire_route_lookup_v6(&v6addr, 0,
9386 				    &v6gw, 0, NULL, NULL, zoneid,
9387 				    NULL, MATCH_IRE_GW, ipst);
9388 			}
9389 		}
9390 		break;
9391 	}
9392 	case AF_INET: {
9393 		ipaddr_t v4addr;
9394 
9395 		v4addr = sin->sin_addr.s_addr;
9396 		if (!CLASSD(v4addr)) {
9397 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9398 			    NULL, NULL, zoneid, NULL,
9399 			    MATCH_IRE_GW, ipst);
9400 		}
9401 		break;
9402 	}
9403 	default:
9404 		return (EAFNOSUPPORT);
9405 	}
9406 	sia->sa_res = 0;
9407 	if (ire != NULL) {
9408 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9409 		    IRE_LOCAL|IRE_LOOPBACK)) {
9410 			sia->sa_res = 1;
9411 		}
9412 		ire_refrele(ire);
9413 	}
9414 	return (0);
9415 }
9416 
9417 /*
9418  * TBD: implement when kernel maintaines a list of site prefixes.
9419  */
9420 /* ARGSUSED */
9421 int
9422 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9423     ip_ioctl_cmd_t *ipip, void *ifreq)
9424 {
9425 	return (ENXIO);
9426 }
9427 
9428 /* ARGSUSED */
9429 int
9430 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9431     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9432 {
9433 	ill_t  		*ill;
9434 	mblk_t		*mp1;
9435 	conn_t		*connp;
9436 	boolean_t	success;
9437 
9438 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9439 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9440 	/* ioctl comes down on an conn */
9441 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9442 	connp = Q_TO_CONN(q);
9443 
9444 	mp->b_datap->db_type = M_IOCTL;
9445 
9446 	/*
9447 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9448 	 * The original mp contains contaminated b_next values due to 'mi',
9449 	 * which is needed to do the mi_copy_done. Unfortunately if we
9450 	 * send down the original mblk itself and if we are popped due to an
9451 	 * an unplumb before the response comes back from tunnel,
9452 	 * the streamhead (which does a freemsg) will see this contaminated
9453 	 * message and the assertion in freemsg about non-null b_next/b_prev
9454 	 * will panic a DEBUG kernel.
9455 	 */
9456 	mp1 = copymsg(mp);
9457 	if (mp1 == NULL)
9458 		return (ENOMEM);
9459 
9460 	ill = ipif->ipif_ill;
9461 	mutex_enter(&connp->conn_lock);
9462 	mutex_enter(&ill->ill_lock);
9463 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9464 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9465 		    mp, 0);
9466 	} else {
9467 		success = ill_pending_mp_add(ill, connp, mp);
9468 	}
9469 	mutex_exit(&ill->ill_lock);
9470 	mutex_exit(&connp->conn_lock);
9471 
9472 	if (success) {
9473 		ip1dbg(("sending down tunparam request "));
9474 		putnext(ill->ill_wq, mp1);
9475 		return (EINPROGRESS);
9476 	} else {
9477 		/* The conn has started closing */
9478 		freemsg(mp1);
9479 		return (EINTR);
9480 	}
9481 }
9482 
9483 /*
9484  * ARP IOCTLs.
9485  * How does IP get in the business of fronting ARP configuration/queries?
9486  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9487  * are by tradition passed in through a datagram socket.  That lands in IP.
9488  * As it happens, this is just as well since the interface is quite crude in
9489  * that it passes in no information about protocol or hardware types, or
9490  * interface association.  After making the protocol assumption, IP is in
9491  * the position to look up the name of the ILL, which ARP will need, and
9492  * format a request that can be handled by ARP.  The request is passed up
9493  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9494  * back a response.  ARP supports its own set of more general IOCTLs, in
9495  * case anyone is interested.
9496  */
9497 /* ARGSUSED */
9498 int
9499 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9500     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9501 {
9502 	mblk_t *mp1;
9503 	mblk_t *mp2;
9504 	mblk_t *pending_mp;
9505 	ipaddr_t ipaddr;
9506 	area_t *area;
9507 	struct iocblk *iocp;
9508 	conn_t *connp;
9509 	struct arpreq *ar;
9510 	struct xarpreq *xar;
9511 	int flags, alength;
9512 	char *lladdr;
9513 	ip_stack_t	*ipst;
9514 	ill_t *ill = ipif->ipif_ill;
9515 	boolean_t if_arp_ioctl = B_FALSE;
9516 
9517 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9518 	connp = Q_TO_CONN(q);
9519 	ipst = connp->conn_netstack->netstack_ip;
9520 
9521 	if (ipip->ipi_cmd_type == XARP_CMD) {
9522 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9523 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9524 		ar = NULL;
9525 
9526 		flags = xar->xarp_flags;
9527 		lladdr = LLADDR(&xar->xarp_ha);
9528 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9529 		/*
9530 		 * Validate against user's link layer address length
9531 		 * input and name and addr length limits.
9532 		 */
9533 		alength = ill->ill_phys_addr_length;
9534 		if (ipip->ipi_cmd == SIOCSXARP) {
9535 			if (alength != xar->xarp_ha.sdl_alen ||
9536 			    (alength + xar->xarp_ha.sdl_nlen >
9537 			    sizeof (xar->xarp_ha.sdl_data)))
9538 				return (EINVAL);
9539 		}
9540 	} else {
9541 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9542 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9543 		xar = NULL;
9544 
9545 		flags = ar->arp_flags;
9546 		lladdr = ar->arp_ha.sa_data;
9547 		/*
9548 		 * Theoretically, the sa_family could tell us what link
9549 		 * layer type this operation is trying to deal with. By
9550 		 * common usage AF_UNSPEC means ethernet. We'll assume
9551 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9552 		 * for now. Our new SIOC*XARP ioctls can be used more
9553 		 * generally.
9554 		 *
9555 		 * If the underlying media happens to have a non 6 byte
9556 		 * address, arp module will fail set/get, but the del
9557 		 * operation will succeed.
9558 		 */
9559 		alength = 6;
9560 		if ((ipip->ipi_cmd != SIOCDARP) &&
9561 		    (alength != ill->ill_phys_addr_length)) {
9562 			return (EINVAL);
9563 		}
9564 	}
9565 
9566 	/*
9567 	 * We are going to pass up to ARP a packet chain that looks
9568 	 * like:
9569 	 *
9570 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9571 	 *
9572 	 * Get a copy of the original IOCTL mblk to head the chain,
9573 	 * to be sent up (in mp1). Also get another copy to store
9574 	 * in the ill_pending_mp list, for matching the response
9575 	 * when it comes back from ARP.
9576 	 */
9577 	mp1 = copyb(mp);
9578 	pending_mp = copymsg(mp);
9579 	if (mp1 == NULL || pending_mp == NULL) {
9580 		if (mp1 != NULL)
9581 			freeb(mp1);
9582 		if (pending_mp != NULL)
9583 			inet_freemsg(pending_mp);
9584 		return (ENOMEM);
9585 	}
9586 
9587 	ipaddr = sin->sin_addr.s_addr;
9588 
9589 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9590 	    (caddr_t)&ipaddr);
9591 	if (mp2 == NULL) {
9592 		freeb(mp1);
9593 		inet_freemsg(pending_mp);
9594 		return (ENOMEM);
9595 	}
9596 	/* Put together the chain. */
9597 	mp1->b_cont = mp2;
9598 	mp1->b_datap->db_type = M_IOCTL;
9599 	mp2->b_cont = mp;
9600 	mp2->b_datap->db_type = M_DATA;
9601 
9602 	iocp = (struct iocblk *)mp1->b_rptr;
9603 
9604 	/*
9605 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9606 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9607 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9608 	 * ioc_count field; set ioc_count to be correct.
9609 	 */
9610 	iocp->ioc_count = MBLKL(mp1->b_cont);
9611 
9612 	/*
9613 	 * Set the proper command in the ARP message.
9614 	 * Convert the SIOC{G|S|D}ARP calls into our
9615 	 * AR_ENTRY_xxx calls.
9616 	 */
9617 	area = (area_t *)mp2->b_rptr;
9618 	switch (iocp->ioc_cmd) {
9619 	case SIOCDARP:
9620 	case SIOCDXARP:
9621 		/*
9622 		 * We defer deleting the corresponding IRE until
9623 		 * we return from arp.
9624 		 */
9625 		area->area_cmd = AR_ENTRY_DELETE;
9626 		area->area_proto_mask_offset = 0;
9627 		break;
9628 	case SIOCGARP:
9629 	case SIOCGXARP:
9630 		area->area_cmd = AR_ENTRY_SQUERY;
9631 		area->area_proto_mask_offset = 0;
9632 		break;
9633 	case SIOCSARP:
9634 	case SIOCSXARP:
9635 		/*
9636 		 * Delete the corresponding ire to make sure IP will
9637 		 * pick up any change from arp.
9638 		 */
9639 		if (!if_arp_ioctl) {
9640 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9641 		} else {
9642 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9643 			if (ipif != NULL) {
9644 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9645 				    ipst);
9646 				ipif_refrele(ipif);
9647 			}
9648 		}
9649 		break;
9650 	}
9651 	iocp->ioc_cmd = area->area_cmd;
9652 
9653 	/*
9654 	 * Fill in the rest of the ARP operation fields.
9655 	 */
9656 	area->area_hw_addr_length = alength;
9657 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9658 
9659 	/* Translate the flags. */
9660 	if (flags & ATF_PERM)
9661 		area->area_flags |= ACE_F_PERMANENT;
9662 	if (flags & ATF_PUBL)
9663 		area->area_flags |= ACE_F_PUBLISH;
9664 	if (flags & ATF_AUTHORITY)
9665 		area->area_flags |= ACE_F_AUTHORITY;
9666 
9667 	/*
9668 	 * Before sending 'mp' to ARP, we have to clear the b_next
9669 	 * and b_prev. Otherwise if STREAMS encounters such a message
9670 	 * in freemsg(), (because ARP can close any time) it can cause
9671 	 * a panic. But mi code needs the b_next and b_prev values of
9672 	 * mp->b_cont, to complete the ioctl. So we store it here
9673 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9674 	 * when the response comes down from ARP.
9675 	 */
9676 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9677 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9678 	mp->b_cont->b_next = NULL;
9679 	mp->b_cont->b_prev = NULL;
9680 
9681 	mutex_enter(&connp->conn_lock);
9682 	mutex_enter(&ill->ill_lock);
9683 	/* conn has not yet started closing, hence this can't fail */
9684 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9685 	mutex_exit(&ill->ill_lock);
9686 	mutex_exit(&connp->conn_lock);
9687 
9688 	/*
9689 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9690 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9691 	 */
9692 	putnext(ill->ill_rq, mp1);
9693 	return (EINPROGRESS);
9694 }
9695 
9696 /*
9697  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9698  * the associated sin and refhold and return the associated ipif via `ci'.
9699  */
9700 int
9701 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9702     cmd_info_t *ci, ipsq_func_t func)
9703 {
9704 	mblk_t	*mp1;
9705 	int	err;
9706 	sin_t	*sin;
9707 	conn_t	*connp;
9708 	ipif_t	*ipif;
9709 	ire_t	*ire = NULL;
9710 	ill_t	*ill = NULL;
9711 	boolean_t exists;
9712 	ip_stack_t *ipst;
9713 	struct arpreq *ar;
9714 	struct xarpreq *xar;
9715 	struct sockaddr_dl *sdl;
9716 
9717 	/* ioctl comes down on a conn */
9718 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9719 	connp = Q_TO_CONN(q);
9720 	if (connp->conn_af_isv6)
9721 		return (ENXIO);
9722 
9723 	ipst = connp->conn_netstack->netstack_ip;
9724 
9725 	/* Verified in ip_wput_nondata */
9726 	mp1 = mp->b_cont->b_cont;
9727 
9728 	if (ipip->ipi_cmd_type == XARP_CMD) {
9729 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9730 		xar = (struct xarpreq *)mp1->b_rptr;
9731 		sin = (sin_t *)&xar->xarp_pa;
9732 		sdl = &xar->xarp_ha;
9733 
9734 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9735 			return (ENXIO);
9736 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9737 			return (EINVAL);
9738 	} else {
9739 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9740 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9741 		ar = (struct arpreq *)mp1->b_rptr;
9742 		sin = (sin_t *)&ar->arp_pa;
9743 	}
9744 
9745 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9746 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9747 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9748 		    mp, func, &err, ipst);
9749 		if (ipif == NULL)
9750 			return (err);
9751 		if (ipif->ipif_id != 0 ||
9752 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9753 			ipif_refrele(ipif);
9754 			return (ENXIO);
9755 		}
9756 	} else {
9757 		/*
9758 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9759 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9760 		 * case, a simple forwarding table lookup will return the
9761 		 * IRE_IF_RESOLVER for the first interface in the group, which
9762 		 * might not be the interface on which the requested IP
9763 		 * address was resolved due to the ill selection algorithm
9764 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9765 		 * lookup first: if the IRE cache entry for the IP address is
9766 		 * still there, it will contain the ill pointer for the right
9767 		 * interface, so we use that. If the cache entry has been
9768 		 * flushed, we fall back to the forwarding table lookup. This
9769 		 * should be rare enough since IRE cache entries have a longer
9770 		 * life expectancy than ARP cache entries.
9771 		 */
9772 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9773 		    ipst);
9774 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9775 		    ((ill = ire_to_ill(ire)) == NULL) ||
9776 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9777 			if (ire != NULL)
9778 				ire_refrele(ire);
9779 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9780 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9781 			    NULL, MATCH_IRE_TYPE, ipst);
9782 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9783 
9784 				if (ire != NULL)
9785 					ire_refrele(ire);
9786 				return (ENXIO);
9787 			}
9788 		}
9789 		ASSERT(ire != NULL && ill != NULL);
9790 		ipif = ill->ill_ipif;
9791 		ipif_refhold(ipif);
9792 		ire_refrele(ire);
9793 	}
9794 	ci->ci_sin = sin;
9795 	ci->ci_ipif = ipif;
9796 	return (0);
9797 }
9798 
9799 /*
9800  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9801  * atomically set/clear the muxids. Also complete the ioctl by acking or
9802  * naking it.  Note that the code is structured such that the link type,
9803  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9804  * its clones use the persistent link, while pppd(1M) and perhaps many
9805  * other daemons may use non-persistent link.  When combined with some
9806  * ill_t states, linking and unlinking lower streams may be used as
9807  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9808  */
9809 /* ARGSUSED */
9810 void
9811 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9812 {
9813 	mblk_t		*mp1, *mp2;
9814 	struct linkblk	*li;
9815 	struct ipmx_s	*ipmxp;
9816 	ill_t		*ill;
9817 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9818 	int		err = 0;
9819 	boolean_t	entered_ipsq = B_FALSE;
9820 	boolean_t	islink;
9821 	ip_stack_t	*ipst;
9822 
9823 	if (CONN_Q(q))
9824 		ipst = CONNQ_TO_IPST(q);
9825 	else
9826 		ipst = ILLQ_TO_IPST(q);
9827 
9828 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9829 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9830 
9831 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9832 
9833 	mp1 = mp->b_cont;	/* This is the linkblk info */
9834 	li = (struct linkblk *)mp1->b_rptr;
9835 
9836 	/*
9837 	 * ARP has added this special mblk, and the utility is asking us
9838 	 * to perform consistency checks, and also atomically set the
9839 	 * muxid. Ifconfig is an example.  It achieves this by using
9840 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9841 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9842 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9843 	 * and other comments in this routine for more details.
9844 	 */
9845 	mp2 = mp1->b_cont;	/* This is added by ARP */
9846 
9847 	/*
9848 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9849 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9850 	 * get the special mblk above.  For backward compatibility, we
9851 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9852 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9853 	 * not atomic, and can leave the streams unplumbable if the utility
9854 	 * is interrupted before it does the SIOCSLIFMUXID.
9855 	 */
9856 	if (mp2 == NULL) {
9857 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9858 		if (err == EINPROGRESS)
9859 			return;
9860 		goto done;
9861 	}
9862 
9863 	/*
9864 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9865 	 * ARP has appended this last mblk to tell us whether the lower stream
9866 	 * is an arp-dev stream or an IP module stream.
9867 	 */
9868 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9869 	if (ipmxp->ipmx_arpdev_stream) {
9870 		/*
9871 		 * The lower stream is the arp-dev stream.
9872 		 */
9873 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9874 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9875 		if (ill == NULL) {
9876 			if (err == EINPROGRESS)
9877 				return;
9878 			err = EINVAL;
9879 			goto done;
9880 		}
9881 
9882 		if (ipsq == NULL) {
9883 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9884 			    NEW_OP, B_TRUE);
9885 			if (ipsq == NULL) {
9886 				ill_refrele(ill);
9887 				return;
9888 			}
9889 			entered_ipsq = B_TRUE;
9890 		}
9891 		ASSERT(IAM_WRITER_ILL(ill));
9892 		ill_refrele(ill);
9893 
9894 		/*
9895 		 * To ensure consistency between IP and ARP, the following
9896 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9897 		 * This is because the muxid's are stored in the IP stream on
9898 		 * the ill.
9899 		 *
9900 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9901 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9902 		 * not yet plinked, and it also checks that the corresponding
9903 		 * IP stream is already plinked.
9904 		 *
9905 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9906 		 * punlinking the IP stream. IP does not allow punlink of the
9907 		 * IP stream unless the arp stream has been punlinked.
9908 		 */
9909 		if ((islink &&
9910 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9911 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9912 			err = EINVAL;
9913 			goto done;
9914 		}
9915 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9916 	} else {
9917 		/*
9918 		 * The lower stream is probably an IP module stream.  Do
9919 		 * consistency checking.
9920 		 */
9921 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9922 		if (err == EINPROGRESS)
9923 			return;
9924 	}
9925 done:
9926 	if (err == 0)
9927 		miocack(q, mp, 0, 0);
9928 	else
9929 		miocnak(q, mp, 0, err);
9930 
9931 	/* Conn was refheld in ip_sioctl_copyin_setup */
9932 	if (CONN_Q(q))
9933 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9934 	if (entered_ipsq)
9935 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9936 }
9937 
9938 /*
9939  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9940  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9941  * module stream).  If `doconsist' is set, then do the extended consistency
9942  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9943  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9944  * an error code on failure.
9945  */
9946 static int
9947 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9948     struct linkblk *li, boolean_t doconsist)
9949 {
9950 	ill_t  		*ill;
9951 	queue_t		*ipwq, *dwq;
9952 	const char	*name;
9953 	struct qinit	*qinfo;
9954 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9955 	boolean_t	entered_ipsq = B_FALSE;
9956 
9957 	/*
9958 	 * Walk the lower stream to verify it's the IP module stream.
9959 	 * The IP module is identified by its name, wput function,
9960 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9961 	 * (li->l_qbot) will not vanish until this ioctl completes.
9962 	 */
9963 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9964 		qinfo = ipwq->q_qinfo;
9965 		name = qinfo->qi_minfo->mi_idname;
9966 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9967 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9968 			break;
9969 		}
9970 	}
9971 
9972 	/*
9973 	 * If this isn't an IP module stream, bail.
9974 	 */
9975 	if (ipwq == NULL)
9976 		return (0);
9977 
9978 	ill = ipwq->q_ptr;
9979 	ASSERT(ill != NULL);
9980 
9981 	if (ipsq == NULL) {
9982 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9983 		    NEW_OP, B_TRUE);
9984 		if (ipsq == NULL)
9985 			return (EINPROGRESS);
9986 		entered_ipsq = B_TRUE;
9987 	}
9988 	ASSERT(IAM_WRITER_ILL(ill));
9989 
9990 	if (doconsist) {
9991 		/*
9992 		 * Consistency checking requires that I_{P}LINK occurs
9993 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9994 		 * occurs prior to clearing ill_arp_muxid.
9995 		 */
9996 		if ((islink && ill->ill_ip_muxid != 0) ||
9997 		    (!islink && ill->ill_arp_muxid != 0)) {
9998 			if (entered_ipsq)
9999 				ipsq_exit(ipsq, B_TRUE, B_TRUE);
10000 			return (EINVAL);
10001 		}
10002 	}
10003 
10004 	/*
10005 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10006 	 * the read queue of the module immediately below IP in the ill.
10007 	 * These are used during the capability negotiation below.
10008 	 */
10009 	ill->ill_lmod_rq = NULL;
10010 	ill->ill_lmod_cnt = 0;
10011 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10012 		ill->ill_lmod_rq = RD(dwq);
10013 		for (; dwq != NULL; dwq = dwq->q_next)
10014 			ill->ill_lmod_cnt++;
10015 	}
10016 
10017 	if (doconsist)
10018 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10019 
10020 	/*
10021 	 * If there's at least one up ipif on this ill, then we're bound to
10022 	 * the underlying driver via DLPI.  In that case, renegotiate
10023 	 * capabilities to account for any possible change in modules
10024 	 * interposed between IP and the driver.
10025 	 */
10026 	if (ill->ill_ipif_up_count > 0) {
10027 		if (islink)
10028 			ill_capability_probe(ill);
10029 		else
10030 			ill_capability_reset(ill);
10031 	}
10032 
10033 	if (entered_ipsq)
10034 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10035 
10036 	return (0);
10037 }
10038 
10039 /*
10040  * Search the ioctl command in the ioctl tables and return a pointer
10041  * to the ioctl command information. The ioctl command tables are
10042  * static and fully populated at compile time.
10043  */
10044 ip_ioctl_cmd_t *
10045 ip_sioctl_lookup(int ioc_cmd)
10046 {
10047 	int index;
10048 	ip_ioctl_cmd_t *ipip;
10049 	ip_ioctl_cmd_t *ipip_end;
10050 
10051 	if (ioc_cmd == IPI_DONTCARE)
10052 		return (NULL);
10053 
10054 	/*
10055 	 * Do a 2 step search. First search the indexed table
10056 	 * based on the least significant byte of the ioctl cmd.
10057 	 * If we don't find a match, then search the misc table
10058 	 * serially.
10059 	 */
10060 	index = ioc_cmd & 0xFF;
10061 	if (index < ip_ndx_ioctl_count) {
10062 		ipip = &ip_ndx_ioctl_table[index];
10063 		if (ipip->ipi_cmd == ioc_cmd) {
10064 			/* Found a match in the ndx table */
10065 			return (ipip);
10066 		}
10067 	}
10068 
10069 	/* Search the misc table */
10070 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10071 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10072 		if (ipip->ipi_cmd == ioc_cmd)
10073 			/* Found a match in the misc table */
10074 			return (ipip);
10075 	}
10076 
10077 	return (NULL);
10078 }
10079 
10080 /*
10081  * Wrapper function for resuming deferred ioctl processing
10082  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10083  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10084  */
10085 /* ARGSUSED */
10086 void
10087 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10088     void *dummy_arg)
10089 {
10090 	ip_sioctl_copyin_setup(q, mp);
10091 }
10092 
10093 /*
10094  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10095  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10096  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10097  * We establish here the size of the block to be copied in.  mi_copyin
10098  * arranges for this to happen, an processing continues in ip_wput with
10099  * an M_IOCDATA message.
10100  */
10101 void
10102 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10103 {
10104 	int	copyin_size;
10105 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10106 	ip_ioctl_cmd_t *ipip;
10107 	cred_t *cr;
10108 	ip_stack_t	*ipst;
10109 
10110 	if (CONN_Q(q))
10111 		ipst = CONNQ_TO_IPST(q);
10112 	else
10113 		ipst = ILLQ_TO_IPST(q);
10114 
10115 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10116 	if (ipip == NULL) {
10117 		/*
10118 		 * The ioctl is not one we understand or own.
10119 		 * Pass it along to be processed down stream,
10120 		 * if this is a module instance of IP, else nak
10121 		 * the ioctl.
10122 		 */
10123 		if (q->q_next == NULL) {
10124 			goto nak;
10125 		} else {
10126 			putnext(q, mp);
10127 			return;
10128 		}
10129 	}
10130 
10131 	/*
10132 	 * If this is deferred, then we will do all the checks when we
10133 	 * come back.
10134 	 */
10135 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10136 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10137 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10138 		return;
10139 	}
10140 
10141 	/*
10142 	 * Only allow a very small subset of IP ioctls on this stream if
10143 	 * IP is a module and not a driver. Allowing ioctls to be processed
10144 	 * in this case may cause assert failures or data corruption.
10145 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10146 	 * ioctls allowed on an IP module stream, after which this stream
10147 	 * normally becomes a multiplexor (at which time the stream head
10148 	 * will fail all ioctls).
10149 	 */
10150 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10151 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10152 			/*
10153 			 * Pass common Streams ioctls which the IP
10154 			 * module does not own or consume along to
10155 			 * be processed down stream.
10156 			 */
10157 			putnext(q, mp);
10158 			return;
10159 		} else {
10160 			goto nak;
10161 		}
10162 	}
10163 
10164 	/* Make sure we have ioctl data to process. */
10165 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10166 		goto nak;
10167 
10168 	/*
10169 	 * Prefer dblk credential over ioctl credential; some synthesized
10170 	 * ioctls have kcred set because there's no way to crhold()
10171 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10172 	 * the framework; the caller of ioctl needs to hold the reference
10173 	 * for the duration of the call).
10174 	 */
10175 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10176 
10177 	/* Make sure normal users don't send down privileged ioctls */
10178 	if ((ipip->ipi_flags & IPI_PRIV) &&
10179 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10180 		/* We checked the privilege earlier but log it here */
10181 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10182 		return;
10183 	}
10184 
10185 	/*
10186 	 * The ioctl command tables can only encode fixed length
10187 	 * ioctl data. If the length is variable, the table will
10188 	 * encode the length as zero. Such special cases are handled
10189 	 * below in the switch.
10190 	 */
10191 	if (ipip->ipi_copyin_size != 0) {
10192 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10193 		return;
10194 	}
10195 
10196 	switch (iocp->ioc_cmd) {
10197 	case O_SIOCGIFCONF:
10198 	case SIOCGIFCONF:
10199 		/*
10200 		 * This IOCTL is hilarious.  See comments in
10201 		 * ip_sioctl_get_ifconf for the story.
10202 		 */
10203 		if (iocp->ioc_count == TRANSPARENT)
10204 			copyin_size = SIZEOF_STRUCT(ifconf,
10205 			    iocp->ioc_flag);
10206 		else
10207 			copyin_size = iocp->ioc_count;
10208 		mi_copyin(q, mp, NULL, copyin_size);
10209 		return;
10210 
10211 	case O_SIOCGLIFCONF:
10212 	case SIOCGLIFCONF:
10213 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10214 		mi_copyin(q, mp, NULL, copyin_size);
10215 		return;
10216 
10217 	case SIOCGLIFSRCOF:
10218 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10219 		mi_copyin(q, mp, NULL, copyin_size);
10220 		return;
10221 	case SIOCGIP6ADDRPOLICY:
10222 		ip_sioctl_ip6addrpolicy(q, mp);
10223 		ip6_asp_table_refrele(ipst);
10224 		return;
10225 
10226 	case SIOCSIP6ADDRPOLICY:
10227 		ip_sioctl_ip6addrpolicy(q, mp);
10228 		return;
10229 
10230 	case SIOCGDSTINFO:
10231 		ip_sioctl_dstinfo(q, mp);
10232 		ip6_asp_table_refrele(ipst);
10233 		return;
10234 
10235 	case I_PLINK:
10236 	case I_PUNLINK:
10237 	case I_LINK:
10238 	case I_UNLINK:
10239 		/*
10240 		 * We treat non-persistent link similarly as the persistent
10241 		 * link case, in terms of plumbing/unplumbing, as well as
10242 		 * dynamic re-plumbing events indicator.  See comments
10243 		 * in ip_sioctl_plink() for more.
10244 		 *
10245 		 * Request can be enqueued in the 'ipsq' while waiting
10246 		 * to become exclusive. So bump up the conn ref.
10247 		 */
10248 		if (CONN_Q(q))
10249 			CONN_INC_REF(Q_TO_CONN(q));
10250 		ip_sioctl_plink(NULL, q, mp, NULL);
10251 		return;
10252 
10253 	case ND_GET:
10254 	case ND_SET:
10255 		/*
10256 		 * Use of the nd table requires holding the reader lock.
10257 		 * Modifying the nd table thru nd_load/nd_unload requires
10258 		 * the writer lock.
10259 		 */
10260 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10261 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10262 			rw_exit(&ipst->ips_ip_g_nd_lock);
10263 
10264 			if (iocp->ioc_error)
10265 				iocp->ioc_count = 0;
10266 			mp->b_datap->db_type = M_IOCACK;
10267 			qreply(q, mp);
10268 			return;
10269 		}
10270 		rw_exit(&ipst->ips_ip_g_nd_lock);
10271 		/*
10272 		 * We don't understand this subioctl of ND_GET / ND_SET.
10273 		 * Maybe intended for some driver / module below us
10274 		 */
10275 		if (q->q_next) {
10276 			putnext(q, mp);
10277 		} else {
10278 			iocp->ioc_error = ENOENT;
10279 			mp->b_datap->db_type = M_IOCNAK;
10280 			iocp->ioc_count = 0;
10281 			qreply(q, mp);
10282 		}
10283 		return;
10284 
10285 	case IP_IOCTL:
10286 		ip_wput_ioctl(q, mp);
10287 		return;
10288 	default:
10289 		cmn_err(CE_PANIC, "should not happen ");
10290 	}
10291 nak:
10292 	if (mp->b_cont != NULL) {
10293 		freemsg(mp->b_cont);
10294 		mp->b_cont = NULL;
10295 	}
10296 	iocp->ioc_error = EINVAL;
10297 	mp->b_datap->db_type = M_IOCNAK;
10298 	iocp->ioc_count = 0;
10299 	qreply(q, mp);
10300 }
10301 
10302 /* ip_wput hands off ARP IOCTL responses to us */
10303 void
10304 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10305 {
10306 	struct arpreq *ar;
10307 	struct xarpreq *xar;
10308 	area_t	*area;
10309 	mblk_t	*area_mp;
10310 	struct iocblk *iocp;
10311 	mblk_t	*orig_ioc_mp, *tmp;
10312 	struct iocblk	*orig_iocp;
10313 	ill_t *ill;
10314 	conn_t *connp = NULL;
10315 	uint_t ioc_id;
10316 	mblk_t *pending_mp;
10317 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10318 	int *flagsp;
10319 	char *storage = NULL;
10320 	sin_t *sin;
10321 	ipaddr_t addr;
10322 	int err;
10323 	ip_stack_t *ipst;
10324 
10325 	ill = q->q_ptr;
10326 	ASSERT(ill != NULL);
10327 	ipst = ill->ill_ipst;
10328 
10329 	/*
10330 	 * We should get back from ARP a packet chain that looks like:
10331 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10332 	 */
10333 	if (!(area_mp = mp->b_cont) ||
10334 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10335 	    !(orig_ioc_mp = area_mp->b_cont) ||
10336 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10337 		freemsg(mp);
10338 		return;
10339 	}
10340 
10341 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10342 
10343 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10344 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10345 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10346 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10347 		x_arp_ioctl = B_TRUE;
10348 		xar = (struct xarpreq *)tmp->b_rptr;
10349 		sin = (sin_t *)&xar->xarp_pa;
10350 		flagsp = &xar->xarp_flags;
10351 		storage = xar->xarp_ha.sdl_data;
10352 		if (xar->xarp_ha.sdl_nlen != 0)
10353 			ifx_arp_ioctl = B_TRUE;
10354 	} else {
10355 		ar = (struct arpreq *)tmp->b_rptr;
10356 		sin = (sin_t *)&ar->arp_pa;
10357 		flagsp = &ar->arp_flags;
10358 		storage = ar->arp_ha.sa_data;
10359 	}
10360 
10361 	iocp = (struct iocblk *)mp->b_rptr;
10362 
10363 	/*
10364 	 * Pick out the originating queue based on the ioc_id.
10365 	 */
10366 	ioc_id = iocp->ioc_id;
10367 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10368 	if (pending_mp == NULL) {
10369 		ASSERT(connp == NULL);
10370 		inet_freemsg(mp);
10371 		return;
10372 	}
10373 	ASSERT(connp != NULL);
10374 	q = CONNP_TO_WQ(connp);
10375 
10376 	/* Uncouple the internally generated IOCTL from the original one */
10377 	area = (area_t *)area_mp->b_rptr;
10378 	area_mp->b_cont = NULL;
10379 
10380 	/*
10381 	 * Restore the b_next and b_prev used by mi code. This is needed
10382 	 * to complete the ioctl using mi* functions. We stored them in
10383 	 * the pending mp prior to sending the request to ARP.
10384 	 */
10385 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10386 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10387 	inet_freemsg(pending_mp);
10388 
10389 	/*
10390 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10391 	 * Catch the case where there is an IRE_CACHE by no entry in the
10392 	 * arp table.
10393 	 */
10394 	addr = sin->sin_addr.s_addr;
10395 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10396 		ire_t			*ire;
10397 		dl_unitdata_req_t	*dlup;
10398 		mblk_t			*llmp;
10399 		int			addr_len;
10400 		ill_t			*ipsqill = NULL;
10401 
10402 		if (ifx_arp_ioctl) {
10403 			/*
10404 			 * There's no need to lookup the ill, since
10405 			 * we've already done that when we started
10406 			 * processing the ioctl and sent the message
10407 			 * to ARP on that ill.  So use the ill that
10408 			 * is stored in q->q_ptr.
10409 			 */
10410 			ipsqill = ill;
10411 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10412 			    ipsqill->ill_ipif, ALL_ZONES,
10413 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10414 		} else {
10415 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10416 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10417 			if (ire != NULL)
10418 				ipsqill = ire_to_ill(ire);
10419 		}
10420 
10421 		if ((x_arp_ioctl) && (ipsqill != NULL))
10422 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10423 
10424 		if (ire != NULL) {
10425 			/*
10426 			 * Since the ire obtained from cachetable is used for
10427 			 * mac addr copying below, treat an incomplete ire as if
10428 			 * as if we never found it.
10429 			 */
10430 			if (ire->ire_nce != NULL &&
10431 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10432 				ire_refrele(ire);
10433 				ire = NULL;
10434 				ipsqill = NULL;
10435 				goto errack;
10436 			}
10437 			*flagsp = ATF_INUSE;
10438 			llmp = (ire->ire_nce != NULL ?
10439 			    ire->ire_nce->nce_res_mp : NULL);
10440 			if (llmp != NULL && ipsqill != NULL) {
10441 				uchar_t *macaddr;
10442 
10443 				addr_len = ipsqill->ill_phys_addr_length;
10444 				if (x_arp_ioctl && ((addr_len +
10445 				    ipsqill->ill_name_length) >
10446 				    sizeof (xar->xarp_ha.sdl_data))) {
10447 					ire_refrele(ire);
10448 					freemsg(mp);
10449 					ip_ioctl_finish(q, orig_ioc_mp,
10450 					    EINVAL, NO_COPYOUT, NULL);
10451 					return;
10452 				}
10453 				*flagsp |= ATF_COM;
10454 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10455 				if (ipsqill->ill_sap_length < 0)
10456 					macaddr = llmp->b_rptr +
10457 					    dlup->dl_dest_addr_offset;
10458 				else
10459 					macaddr = llmp->b_rptr +
10460 					    dlup->dl_dest_addr_offset +
10461 					    ipsqill->ill_sap_length;
10462 				/*
10463 				 * For SIOCGARP, MAC address length
10464 				 * validation has already been done
10465 				 * before the ioctl was issued to ARP to
10466 				 * allow it to progress only on 6 byte
10467 				 * addressable (ethernet like) media. Thus
10468 				 * the mac address copying can not overwrite
10469 				 * the sa_data area below.
10470 				 */
10471 				bcopy(macaddr, storage, addr_len);
10472 			}
10473 			/* Ditch the internal IOCTL. */
10474 			freemsg(mp);
10475 			ire_refrele(ire);
10476 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10477 			return;
10478 		}
10479 	}
10480 
10481 	/*
10482 	 * Delete the coresponding IRE_CACHE if any.
10483 	 * Reset the error if there was one (in case there was no entry
10484 	 * in arp.)
10485 	 */
10486 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10487 		ipif_t *ipintf = NULL;
10488 
10489 		if (ifx_arp_ioctl) {
10490 			/*
10491 			 * There's no need to lookup the ill, since
10492 			 * we've already done that when we started
10493 			 * processing the ioctl and sent the message
10494 			 * to ARP on that ill.  So use the ill that
10495 			 * is stored in q->q_ptr.
10496 			 */
10497 			ipintf = ill->ill_ipif;
10498 		}
10499 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10500 			/*
10501 			 * The address in "addr" may be an entry for a
10502 			 * router. If that's true, then any off-net
10503 			 * IRE_CACHE entries that go through the router
10504 			 * with address "addr" must be clobbered. Use
10505 			 * ire_walk to achieve this goal.
10506 			 */
10507 			if (ifx_arp_ioctl)
10508 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10509 				    ire_delete_cache_gw, (char *)&addr, ill);
10510 			else
10511 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10512 				    ALL_ZONES, ipst);
10513 			iocp->ioc_error = 0;
10514 		}
10515 	}
10516 errack:
10517 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10518 		err = iocp->ioc_error;
10519 		freemsg(mp);
10520 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10521 		return;
10522 	}
10523 
10524 	/*
10525 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10526 	 * the area_t into the struct {x}arpreq.
10527 	 */
10528 	if (x_arp_ioctl) {
10529 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10530 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10531 		    sizeof (xar->xarp_ha.sdl_data)) {
10532 			freemsg(mp);
10533 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10534 			    NULL);
10535 			return;
10536 		}
10537 	}
10538 	*flagsp = ATF_INUSE;
10539 	if (area->area_flags & ACE_F_PERMANENT)
10540 		*flagsp |= ATF_PERM;
10541 	if (area->area_flags & ACE_F_PUBLISH)
10542 		*flagsp |= ATF_PUBL;
10543 	if (area->area_flags & ACE_F_AUTHORITY)
10544 		*flagsp |= ATF_AUTHORITY;
10545 	if (area->area_hw_addr_length != 0) {
10546 		*flagsp |= ATF_COM;
10547 		/*
10548 		 * For SIOCGARP, MAC address length validation has
10549 		 * already been done before the ioctl was issued to ARP
10550 		 * to allow it to progress only on 6 byte addressable
10551 		 * (ethernet like) media. Thus the mac address copying
10552 		 * can not overwrite the sa_data area below.
10553 		 */
10554 		bcopy((char *)area + area->area_hw_addr_offset,
10555 		    storage, area->area_hw_addr_length);
10556 	}
10557 
10558 	/* Ditch the internal IOCTL. */
10559 	freemsg(mp);
10560 	/* Complete the original. */
10561 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10562 }
10563 
10564 /*
10565  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10566  * interface) create the next available logical interface for this
10567  * physical interface.
10568  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10569  * ipif with the specified name.
10570  *
10571  * If the address family is not AF_UNSPEC then set the address as well.
10572  *
10573  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10574  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10575  *
10576  * Executed as a writer on the ill or ill group.
10577  * So no lock is needed to traverse the ipif chain, or examine the
10578  * phyint flags.
10579  */
10580 /* ARGSUSED */
10581 int
10582 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10583     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10584 {
10585 	mblk_t	*mp1;
10586 	struct lifreq *lifr;
10587 	boolean_t	isv6;
10588 	boolean_t	exists;
10589 	char 	*name;
10590 	char	*endp;
10591 	char	*cp;
10592 	int	namelen;
10593 	ipif_t	*ipif;
10594 	long	id;
10595 	ipsq_t	*ipsq;
10596 	ill_t	*ill;
10597 	sin_t	*sin;
10598 	int	err = 0;
10599 	boolean_t found_sep = B_FALSE;
10600 	conn_t	*connp;
10601 	zoneid_t zoneid;
10602 	int	orig_ifindex = 0;
10603 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10604 
10605 	ASSERT(q->q_next == NULL);
10606 	ip1dbg(("ip_sioctl_addif\n"));
10607 	/* Existence of mp1 has been checked in ip_wput_nondata */
10608 	mp1 = mp->b_cont->b_cont;
10609 	/*
10610 	 * Null terminate the string to protect against buffer
10611 	 * overrun. String was generated by user code and may not
10612 	 * be trusted.
10613 	 */
10614 	lifr = (struct lifreq *)mp1->b_rptr;
10615 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10616 	name = lifr->lifr_name;
10617 	ASSERT(CONN_Q(q));
10618 	connp = Q_TO_CONN(q);
10619 	isv6 = connp->conn_af_isv6;
10620 	zoneid = connp->conn_zoneid;
10621 	namelen = mi_strlen(name);
10622 	if (namelen == 0)
10623 		return (EINVAL);
10624 
10625 	exists = B_FALSE;
10626 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10627 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10628 		/*
10629 		 * Allow creating lo0 using SIOCLIFADDIF.
10630 		 * can't be any other writer thread. So can pass null below
10631 		 * for the last 4 args to ipif_lookup_name.
10632 		 */
10633 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10634 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10635 		/* Prevent any further action */
10636 		if (ipif == NULL) {
10637 			return (ENOBUFS);
10638 		} else if (!exists) {
10639 			/* We created the ipif now and as writer */
10640 			ipif_refrele(ipif);
10641 			return (0);
10642 		} else {
10643 			ill = ipif->ipif_ill;
10644 			ill_refhold(ill);
10645 			ipif_refrele(ipif);
10646 		}
10647 	} else {
10648 		/* Look for a colon in the name. */
10649 		endp = &name[namelen];
10650 		for (cp = endp; --cp > name; ) {
10651 			if (*cp == IPIF_SEPARATOR_CHAR) {
10652 				found_sep = B_TRUE;
10653 				/*
10654 				 * Reject any non-decimal aliases for plumbing
10655 				 * of logical interfaces. Aliases with leading
10656 				 * zeroes are also rejected as they introduce
10657 				 * ambiguity in the naming of the interfaces.
10658 				 * Comparing with "0" takes care of all such
10659 				 * cases.
10660 				 */
10661 				if ((strncmp("0", cp+1, 1)) == 0)
10662 					return (EINVAL);
10663 
10664 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10665 				    id <= 0 || *endp != '\0') {
10666 					return (EINVAL);
10667 				}
10668 				*cp = '\0';
10669 				break;
10670 			}
10671 		}
10672 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10673 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10674 		if (found_sep)
10675 			*cp = IPIF_SEPARATOR_CHAR;
10676 		if (ill == NULL)
10677 			return (err);
10678 	}
10679 
10680 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10681 	    B_TRUE);
10682 
10683 	/*
10684 	 * Release the refhold due to the lookup, now that we are excl
10685 	 * or we are just returning
10686 	 */
10687 	ill_refrele(ill);
10688 
10689 	if (ipsq == NULL)
10690 		return (EINPROGRESS);
10691 
10692 	/*
10693 	 * If the interface is failed, inactive or offlined, look for a working
10694 	 * interface in the ill group and create the ipif there. If we can't
10695 	 * find a good interface, create the ipif anyway so that in.mpathd can
10696 	 * move it to the first repaired interface.
10697 	 */
10698 	if ((ill->ill_phyint->phyint_flags &
10699 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10700 	    ill->ill_phyint->phyint_groupname_len != 0) {
10701 		phyint_t *phyi;
10702 		char *groupname = ill->ill_phyint->phyint_groupname;
10703 
10704 		/*
10705 		 * We're looking for a working interface, but it doesn't matter
10706 		 * if it's up or down; so instead of following the group lists,
10707 		 * we look at each physical interface and compare the groupname.
10708 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10709 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10710 		 * Otherwise we create the ipif on the failed interface.
10711 		 */
10712 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10713 		phyi = avl_first(&ipst->ips_phyint_g_list->
10714 		    phyint_list_avl_by_index);
10715 		for (; phyi != NULL;
10716 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10717 		    phyint_list_avl_by_index,
10718 		    phyi, AVL_AFTER)) {
10719 			if (phyi->phyint_groupname_len == 0)
10720 				continue;
10721 			ASSERT(phyi->phyint_groupname != NULL);
10722 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10723 			    !(phyi->phyint_flags &
10724 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10725 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10726 			    (phyi->phyint_illv4 != NULL))) {
10727 				break;
10728 			}
10729 		}
10730 		rw_exit(&ipst->ips_ill_g_lock);
10731 
10732 		if (phyi != NULL) {
10733 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10734 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10735 			    phyi->phyint_illv4);
10736 		}
10737 	}
10738 
10739 	/*
10740 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10741 	 * before or after us.
10742 	 */
10743 	ASSERT(IAM_WRITER_ILL(ill));
10744 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10745 
10746 	if (found_sep && orig_ifindex == 0) {
10747 		/* Now see if there is an IPIF with this unit number. */
10748 		for (ipif = ill->ill_ipif; ipif != NULL;
10749 		    ipif = ipif->ipif_next) {
10750 			if (ipif->ipif_id == id) {
10751 				err = EEXIST;
10752 				goto done;
10753 			}
10754 		}
10755 	}
10756 
10757 	/*
10758 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10759 	 * of lo0. We never come here when we plumb lo0:0. It
10760 	 * happens in ipif_lookup_on_name.
10761 	 * The specified unit number is ignored when we create the ipif on a
10762 	 * different interface. However, we save it in ipif_orig_ipifid below so
10763 	 * that the ipif fails back to the right position.
10764 	 */
10765 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10766 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10767 		err = ENOBUFS;
10768 		goto done;
10769 	}
10770 
10771 	/* Return created name with ioctl */
10772 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10773 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10774 	ip1dbg(("created %s\n", lifr->lifr_name));
10775 
10776 	/* Set address */
10777 	sin = (sin_t *)&lifr->lifr_addr;
10778 	if (sin->sin_family != AF_UNSPEC) {
10779 		err = ip_sioctl_addr(ipif, sin, q, mp,
10780 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10781 	}
10782 
10783 	/* Set ifindex and unit number for failback */
10784 	if (err == 0 && orig_ifindex != 0) {
10785 		ipif->ipif_orig_ifindex = orig_ifindex;
10786 		if (found_sep) {
10787 			ipif->ipif_orig_ipifid = id;
10788 		}
10789 	}
10790 
10791 done:
10792 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10793 	return (err);
10794 }
10795 
10796 /*
10797  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10798  * interface) delete it based on the IP address (on this physical interface).
10799  * Otherwise delete it based on the ipif_id.
10800  * Also, special handling to allow a removeif of lo0.
10801  */
10802 /* ARGSUSED */
10803 int
10804 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10805     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10806 {
10807 	conn_t		*connp;
10808 	ill_t		*ill = ipif->ipif_ill;
10809 	boolean_t	 success;
10810 	ip_stack_t	*ipst;
10811 
10812 	ipst = CONNQ_TO_IPST(q);
10813 
10814 	ASSERT(q->q_next == NULL);
10815 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10816 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10817 	ASSERT(IAM_WRITER_IPIF(ipif));
10818 
10819 	connp = Q_TO_CONN(q);
10820 	/*
10821 	 * Special case for unplumbing lo0 (the loopback physical interface).
10822 	 * If unplumbing lo0, the incoming address structure has been
10823 	 * initialized to all zeros. When unplumbing lo0, all its logical
10824 	 * interfaces must be removed too.
10825 	 *
10826 	 * Note that this interface may be called to remove a specific
10827 	 * loopback logical interface (eg, lo0:1). But in that case
10828 	 * ipif->ipif_id != 0 so that the code path for that case is the
10829 	 * same as any other interface (meaning it skips the code directly
10830 	 * below).
10831 	 */
10832 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10833 		if (sin->sin_family == AF_UNSPEC &&
10834 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10835 			/*
10836 			 * Mark it condemned. No new ref. will be made to ill.
10837 			 */
10838 			mutex_enter(&ill->ill_lock);
10839 			ill->ill_state_flags |= ILL_CONDEMNED;
10840 			for (ipif = ill->ill_ipif; ipif != NULL;
10841 			    ipif = ipif->ipif_next) {
10842 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10843 			}
10844 			mutex_exit(&ill->ill_lock);
10845 
10846 			ipif = ill->ill_ipif;
10847 			/* unplumb the loopback interface */
10848 			ill_delete(ill);
10849 			mutex_enter(&connp->conn_lock);
10850 			mutex_enter(&ill->ill_lock);
10851 			ASSERT(ill->ill_group == NULL);
10852 
10853 			/* Are any references to this ill active */
10854 			if (ill_is_freeable(ill)) {
10855 				mutex_exit(&ill->ill_lock);
10856 				mutex_exit(&connp->conn_lock);
10857 				ill_delete_tail(ill);
10858 				mutex_enter(&ill->ill_lock);
10859 				ill_nic_info_dispatch(ill);
10860 				mutex_exit(&ill->ill_lock);
10861 				mi_free(ill);
10862 				return (0);
10863 			}
10864 			success = ipsq_pending_mp_add(connp, ipif,
10865 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10866 			mutex_exit(&connp->conn_lock);
10867 			mutex_exit(&ill->ill_lock);
10868 			if (success)
10869 				return (EINPROGRESS);
10870 			else
10871 				return (EINTR);
10872 		}
10873 	}
10874 
10875 	/*
10876 	 * We are exclusive on the ipsq, so an ill move will be serialized
10877 	 * before or after us.
10878 	 */
10879 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10880 
10881 	if (ipif->ipif_id == 0) {
10882 
10883 		ipsq_t *ipsq;
10884 
10885 		/* Find based on address */
10886 		if (ipif->ipif_isv6) {
10887 			sin6_t *sin6;
10888 
10889 			if (sin->sin_family != AF_INET6)
10890 				return (EAFNOSUPPORT);
10891 
10892 			sin6 = (sin6_t *)sin;
10893 			/* We are a writer, so we should be able to lookup */
10894 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10895 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10896 			if (ipif == NULL) {
10897 				/*
10898 				 * Maybe the address in on another interface in
10899 				 * the same IPMP group? We check this below.
10900 				 */
10901 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10902 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10903 				    ipst);
10904 			}
10905 		} else {
10906 			ipaddr_t addr;
10907 
10908 			if (sin->sin_family != AF_INET)
10909 				return (EAFNOSUPPORT);
10910 
10911 			addr = sin->sin_addr.s_addr;
10912 			/* We are a writer, so we should be able to lookup */
10913 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10914 			    NULL, NULL, NULL, ipst);
10915 			if (ipif == NULL) {
10916 				/*
10917 				 * Maybe the address in on another interface in
10918 				 * the same IPMP group? We check this below.
10919 				 */
10920 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10921 				    NULL, NULL, NULL, NULL, ipst);
10922 			}
10923 		}
10924 		if (ipif == NULL) {
10925 			return (EADDRNOTAVAIL);
10926 		}
10927 
10928 		/*
10929 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10930 		 * lifr_name of the physical interface but with an ip address
10931 		 * lifr_addr of a logical interface plumbed over it.
10932 		 * So update ipsq_current_ipif once ipif points to the
10933 		 * correct interface after doing ipif_lookup_addr().
10934 		 */
10935 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10936 		ASSERT(ipsq != NULL);
10937 
10938 		mutex_enter(&ipsq->ipsq_lock);
10939 		ipsq->ipsq_current_ipif = ipif;
10940 		mutex_exit(&ipsq->ipsq_lock);
10941 
10942 		/*
10943 		 * When the address to be removed is hosted on a different
10944 		 * interface, we check if the interface is in the same IPMP
10945 		 * group as the specified one; if so we proceed with the
10946 		 * removal.
10947 		 * ill->ill_group is NULL when the ill is down, so we have to
10948 		 * compare the group names instead.
10949 		 */
10950 		if (ipif->ipif_ill != ill &&
10951 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10952 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10953 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10954 		    ill->ill_phyint->phyint_groupname) != 0)) {
10955 			ipif_refrele(ipif);
10956 			return (EADDRNOTAVAIL);
10957 		}
10958 
10959 		/* This is a writer */
10960 		ipif_refrele(ipif);
10961 	}
10962 
10963 	/*
10964 	 * Can not delete instance zero since it is tied to the ill.
10965 	 */
10966 	if (ipif->ipif_id == 0)
10967 		return (EBUSY);
10968 
10969 	mutex_enter(&ill->ill_lock);
10970 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10971 	mutex_exit(&ill->ill_lock);
10972 
10973 	ipif_free(ipif);
10974 
10975 	mutex_enter(&connp->conn_lock);
10976 	mutex_enter(&ill->ill_lock);
10977 
10978 
10979 	/* Are any references to this ipif active */
10980 	if (ipif_is_freeable(ipif)) {
10981 		mutex_exit(&ill->ill_lock);
10982 		mutex_exit(&connp->conn_lock);
10983 		ipif_non_duplicate(ipif);
10984 		ipif_down_tail(ipif);
10985 		ipif_free_tail(ipif); /* frees ipif */
10986 		return (0);
10987 	}
10988 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10989 	    IPIF_FREE);
10990 	mutex_exit(&ill->ill_lock);
10991 	mutex_exit(&connp->conn_lock);
10992 	if (success)
10993 		return (EINPROGRESS);
10994 	else
10995 		return (EINTR);
10996 }
10997 
10998 /*
10999  * Restart the removeif ioctl. The refcnt has gone down to 0.
11000  * The ipif is already condemned. So can't find it thru lookups.
11001  */
11002 /* ARGSUSED */
11003 int
11004 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11005     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11006 {
11007 	ill_t *ill = ipif->ipif_ill;
11008 
11009 	ASSERT(IAM_WRITER_IPIF(ipif));
11010 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11011 
11012 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11013 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11014 
11015 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11016 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
11017 		ill_delete_tail(ill);
11018 		mutex_enter(&ill->ill_lock);
11019 		ill_nic_info_dispatch(ill);
11020 		mutex_exit(&ill->ill_lock);
11021 		mi_free(ill);
11022 		return (0);
11023 	}
11024 
11025 	ipif_non_duplicate(ipif);
11026 	ipif_down_tail(ipif);
11027 	ipif_free_tail(ipif);
11028 
11029 	ILL_UNMARK_CHANGING(ill);
11030 	return (0);
11031 }
11032 
11033 /*
11034  * Set the local interface address.
11035  * Allow an address of all zero when the interface is down.
11036  */
11037 /* ARGSUSED */
11038 int
11039 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11040     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11041 {
11042 	int err = 0;
11043 	in6_addr_t v6addr;
11044 	boolean_t need_up = B_FALSE;
11045 
11046 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11047 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11048 
11049 	ASSERT(IAM_WRITER_IPIF(ipif));
11050 
11051 	if (ipif->ipif_isv6) {
11052 		sin6_t *sin6;
11053 		ill_t *ill;
11054 		phyint_t *phyi;
11055 
11056 		if (sin->sin_family != AF_INET6)
11057 			return (EAFNOSUPPORT);
11058 
11059 		sin6 = (sin6_t *)sin;
11060 		v6addr = sin6->sin6_addr;
11061 		ill = ipif->ipif_ill;
11062 		phyi = ill->ill_phyint;
11063 
11064 		/*
11065 		 * Enforce that true multicast interfaces have a link-local
11066 		 * address for logical unit 0.
11067 		 */
11068 		if (ipif->ipif_id == 0 &&
11069 		    (ill->ill_flags & ILLF_MULTICAST) &&
11070 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11071 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11072 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11073 			return (EADDRNOTAVAIL);
11074 		}
11075 
11076 		/*
11077 		 * up interfaces shouldn't have the unspecified address
11078 		 * unless they also have the IPIF_NOLOCAL flags set and
11079 		 * have a subnet assigned.
11080 		 */
11081 		if ((ipif->ipif_flags & IPIF_UP) &&
11082 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11083 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11084 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11085 			return (EADDRNOTAVAIL);
11086 		}
11087 
11088 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11089 			return (EADDRNOTAVAIL);
11090 	} else {
11091 		ipaddr_t addr;
11092 
11093 		if (sin->sin_family != AF_INET)
11094 			return (EAFNOSUPPORT);
11095 
11096 		addr = sin->sin_addr.s_addr;
11097 
11098 		/* Allow 0 as the local address. */
11099 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11100 			return (EADDRNOTAVAIL);
11101 
11102 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11103 	}
11104 
11105 
11106 	/*
11107 	 * Even if there is no change we redo things just to rerun
11108 	 * ipif_set_default.
11109 	 */
11110 	if (ipif->ipif_flags & IPIF_UP) {
11111 		/*
11112 		 * Setting a new local address, make sure
11113 		 * we have net and subnet bcast ire's for
11114 		 * the old address if we need them.
11115 		 */
11116 		if (!ipif->ipif_isv6)
11117 			ipif_check_bcast_ires(ipif);
11118 		/*
11119 		 * If the interface is already marked up,
11120 		 * we call ipif_down which will take care
11121 		 * of ditching any IREs that have been set
11122 		 * up based on the old interface address.
11123 		 */
11124 		err = ipif_logical_down(ipif, q, mp);
11125 		if (err == EINPROGRESS)
11126 			return (err);
11127 		ipif_down_tail(ipif);
11128 		need_up = 1;
11129 	}
11130 
11131 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11132 	return (err);
11133 }
11134 
11135 int
11136 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11137     boolean_t need_up)
11138 {
11139 	in6_addr_t v6addr;
11140 	in6_addr_t ov6addr;
11141 	ipaddr_t addr;
11142 	sin6_t	*sin6;
11143 	int	sinlen;
11144 	int	err = 0;
11145 	ill_t	*ill = ipif->ipif_ill;
11146 	boolean_t need_dl_down;
11147 	boolean_t need_arp_down;
11148 	struct iocblk *iocp;
11149 
11150 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11151 
11152 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11153 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11154 	ASSERT(IAM_WRITER_IPIF(ipif));
11155 
11156 	/* Must cancel any pending timer before taking the ill_lock */
11157 	if (ipif->ipif_recovery_id != 0)
11158 		(void) untimeout(ipif->ipif_recovery_id);
11159 	ipif->ipif_recovery_id = 0;
11160 
11161 	if (ipif->ipif_isv6) {
11162 		sin6 = (sin6_t *)sin;
11163 		v6addr = sin6->sin6_addr;
11164 		sinlen = sizeof (struct sockaddr_in6);
11165 	} else {
11166 		addr = sin->sin_addr.s_addr;
11167 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11168 		sinlen = sizeof (struct sockaddr_in);
11169 	}
11170 	mutex_enter(&ill->ill_lock);
11171 	ov6addr = ipif->ipif_v6lcl_addr;
11172 	ipif->ipif_v6lcl_addr = v6addr;
11173 	sctp_update_ipif_addr(ipif, ov6addr);
11174 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11175 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11176 	} else {
11177 		ipif->ipif_v6src_addr = v6addr;
11178 	}
11179 	ipif->ipif_addr_ready = 0;
11180 
11181 	/*
11182 	 * If the interface was previously marked as a duplicate, then since
11183 	 * we've now got a "new" address, it should no longer be considered a
11184 	 * duplicate -- even if the "new" address is the same as the old one.
11185 	 * Note that if all ipifs are down, we may have a pending ARP down
11186 	 * event to handle.  This is because we want to recover from duplicates
11187 	 * and thus delay tearing down ARP until the duplicates have been
11188 	 * removed or disabled.
11189 	 */
11190 	need_dl_down = need_arp_down = B_FALSE;
11191 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11192 		need_arp_down = !need_up;
11193 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11194 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11195 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11196 			need_dl_down = B_TRUE;
11197 		}
11198 	}
11199 
11200 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11201 	    !ill->ill_is_6to4tun) {
11202 		queue_t *wqp = ill->ill_wq;
11203 
11204 		/*
11205 		 * The local address of this interface is a 6to4 address,
11206 		 * check if this interface is in fact a 6to4 tunnel or just
11207 		 * an interface configured with a 6to4 address.  We are only
11208 		 * interested in the former.
11209 		 */
11210 		if (wqp != NULL) {
11211 			while ((wqp->q_next != NULL) &&
11212 			    (wqp->q_next->q_qinfo != NULL) &&
11213 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11214 
11215 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11216 				    == TUN6TO4_MODID) {
11217 					/* set for use in IP */
11218 					ill->ill_is_6to4tun = 1;
11219 					break;
11220 				}
11221 				wqp = wqp->q_next;
11222 			}
11223 		}
11224 	}
11225 
11226 	ipif_set_default(ipif);
11227 
11228 	/*
11229 	 * When publishing an interface address change event, we only notify
11230 	 * the event listeners of the new address.  It is assumed that if they
11231 	 * actively care about the addresses assigned that they will have
11232 	 * already discovered the previous address assigned (if there was one.)
11233 	 *
11234 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11235 	 */
11236 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11237 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11238 		    NE_ADDRESS_CHANGE, sin, sinlen);
11239 	}
11240 
11241 	mutex_exit(&ill->ill_lock);
11242 
11243 	if (need_up) {
11244 		/*
11245 		 * Now bring the interface back up.  If this
11246 		 * is the only IPIF for the ILL, ipif_up
11247 		 * will have to re-bind to the device, so
11248 		 * we may get back EINPROGRESS, in which
11249 		 * case, this IOCTL will get completed in
11250 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11251 		 */
11252 		err = ipif_up(ipif, q, mp);
11253 	}
11254 
11255 	if (need_dl_down)
11256 		ill_dl_down(ill);
11257 	if (need_arp_down)
11258 		ipif_arp_down(ipif);
11259 
11260 	return (err);
11261 }
11262 
11263 
11264 /*
11265  * Restart entry point to restart the address set operation after the
11266  * refcounts have dropped to zero.
11267  */
11268 /* ARGSUSED */
11269 int
11270 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11271     ip_ioctl_cmd_t *ipip, void *ifreq)
11272 {
11273 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11274 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11275 	ASSERT(IAM_WRITER_IPIF(ipif));
11276 	ipif_down_tail(ipif);
11277 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11278 }
11279 
11280 /* ARGSUSED */
11281 int
11282 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11283     ip_ioctl_cmd_t *ipip, void *if_req)
11284 {
11285 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11286 	struct lifreq *lifr = (struct lifreq *)if_req;
11287 
11288 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11289 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11290 	/*
11291 	 * The net mask and address can't change since we have a
11292 	 * reference to the ipif. So no lock is necessary.
11293 	 */
11294 	if (ipif->ipif_isv6) {
11295 		*sin6 = sin6_null;
11296 		sin6->sin6_family = AF_INET6;
11297 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11298 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11299 		lifr->lifr_addrlen =
11300 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11301 	} else {
11302 		*sin = sin_null;
11303 		sin->sin_family = AF_INET;
11304 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11305 		if (ipip->ipi_cmd_type == LIF_CMD) {
11306 			lifr->lifr_addrlen =
11307 			    ip_mask_to_plen(ipif->ipif_net_mask);
11308 		}
11309 	}
11310 	return (0);
11311 }
11312 
11313 /*
11314  * Set the destination address for a pt-pt interface.
11315  */
11316 /* ARGSUSED */
11317 int
11318 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11319     ip_ioctl_cmd_t *ipip, void *if_req)
11320 {
11321 	int err = 0;
11322 	in6_addr_t v6addr;
11323 	boolean_t need_up = B_FALSE;
11324 
11325 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11326 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11327 	ASSERT(IAM_WRITER_IPIF(ipif));
11328 
11329 	if (ipif->ipif_isv6) {
11330 		sin6_t *sin6;
11331 
11332 		if (sin->sin_family != AF_INET6)
11333 			return (EAFNOSUPPORT);
11334 
11335 		sin6 = (sin6_t *)sin;
11336 		v6addr = sin6->sin6_addr;
11337 
11338 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11339 			return (EADDRNOTAVAIL);
11340 	} else {
11341 		ipaddr_t addr;
11342 
11343 		if (sin->sin_family != AF_INET)
11344 			return (EAFNOSUPPORT);
11345 
11346 		addr = sin->sin_addr.s_addr;
11347 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11348 			return (EADDRNOTAVAIL);
11349 
11350 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11351 	}
11352 
11353 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11354 		return (0);	/* No change */
11355 
11356 	if (ipif->ipif_flags & IPIF_UP) {
11357 		/*
11358 		 * If the interface is already marked up,
11359 		 * we call ipif_down which will take care
11360 		 * of ditching any IREs that have been set
11361 		 * up based on the old pp dst address.
11362 		 */
11363 		err = ipif_logical_down(ipif, q, mp);
11364 		if (err == EINPROGRESS)
11365 			return (err);
11366 		ipif_down_tail(ipif);
11367 		need_up = B_TRUE;
11368 	}
11369 	/*
11370 	 * could return EINPROGRESS. If so ioctl will complete in
11371 	 * ip_rput_dlpi_writer
11372 	 */
11373 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11374 	return (err);
11375 }
11376 
11377 static int
11378 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11379     boolean_t need_up)
11380 {
11381 	in6_addr_t v6addr;
11382 	ill_t	*ill = ipif->ipif_ill;
11383 	int	err = 0;
11384 	boolean_t need_dl_down;
11385 	boolean_t need_arp_down;
11386 
11387 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11388 	    ipif->ipif_id, (void *)ipif));
11389 
11390 	/* Must cancel any pending timer before taking the ill_lock */
11391 	if (ipif->ipif_recovery_id != 0)
11392 		(void) untimeout(ipif->ipif_recovery_id);
11393 	ipif->ipif_recovery_id = 0;
11394 
11395 	if (ipif->ipif_isv6) {
11396 		sin6_t *sin6;
11397 
11398 		sin6 = (sin6_t *)sin;
11399 		v6addr = sin6->sin6_addr;
11400 	} else {
11401 		ipaddr_t addr;
11402 
11403 		addr = sin->sin_addr.s_addr;
11404 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11405 	}
11406 	mutex_enter(&ill->ill_lock);
11407 	/* Set point to point destination address. */
11408 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11409 		/*
11410 		 * Allow this as a means of creating logical
11411 		 * pt-pt interfaces on top of e.g. an Ethernet.
11412 		 * XXX Undocumented HACK for testing.
11413 		 * pt-pt interfaces are created with NUD disabled.
11414 		 */
11415 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11416 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11417 		if (ipif->ipif_isv6)
11418 			ill->ill_flags |= ILLF_NONUD;
11419 	}
11420 
11421 	/*
11422 	 * If the interface was previously marked as a duplicate, then since
11423 	 * we've now got a "new" address, it should no longer be considered a
11424 	 * duplicate -- even if the "new" address is the same as the old one.
11425 	 * Note that if all ipifs are down, we may have a pending ARP down
11426 	 * event to handle.
11427 	 */
11428 	need_dl_down = need_arp_down = B_FALSE;
11429 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11430 		need_arp_down = !need_up;
11431 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11432 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11433 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11434 			need_dl_down = B_TRUE;
11435 		}
11436 	}
11437 
11438 	/* Set the new address. */
11439 	ipif->ipif_v6pp_dst_addr = v6addr;
11440 	/* Make sure subnet tracks pp_dst */
11441 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11442 	mutex_exit(&ill->ill_lock);
11443 
11444 	if (need_up) {
11445 		/*
11446 		 * Now bring the interface back up.  If this
11447 		 * is the only IPIF for the ILL, ipif_up
11448 		 * will have to re-bind to the device, so
11449 		 * we may get back EINPROGRESS, in which
11450 		 * case, this IOCTL will get completed in
11451 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11452 		 */
11453 		err = ipif_up(ipif, q, mp);
11454 	}
11455 
11456 	if (need_dl_down)
11457 		ill_dl_down(ill);
11458 
11459 	if (need_arp_down)
11460 		ipif_arp_down(ipif);
11461 	return (err);
11462 }
11463 
11464 /*
11465  * Restart entry point to restart the dstaddress set operation after the
11466  * refcounts have dropped to zero.
11467  */
11468 /* ARGSUSED */
11469 int
11470 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11471     ip_ioctl_cmd_t *ipip, void *ifreq)
11472 {
11473 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11474 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11475 	ipif_down_tail(ipif);
11476 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11477 }
11478 
11479 /* ARGSUSED */
11480 int
11481 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11482     ip_ioctl_cmd_t *ipip, void *if_req)
11483 {
11484 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11485 
11486 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11487 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11488 	/*
11489 	 * Get point to point destination address. The addresses can't
11490 	 * change since we hold a reference to the ipif.
11491 	 */
11492 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11493 		return (EADDRNOTAVAIL);
11494 
11495 	if (ipif->ipif_isv6) {
11496 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11497 		*sin6 = sin6_null;
11498 		sin6->sin6_family = AF_INET6;
11499 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11500 	} else {
11501 		*sin = sin_null;
11502 		sin->sin_family = AF_INET;
11503 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11504 	}
11505 	return (0);
11506 }
11507 
11508 /*
11509  * part of ipmp, make this func return the active/inactive state and
11510  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11511  */
11512 /*
11513  * This function either sets or clears the IFF_INACTIVE flag.
11514  *
11515  * As long as there are some addresses or multicast memberships on the
11516  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11517  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11518  * will be used for outbound packets.
11519  *
11520  * Caller needs to verify the validity of setting IFF_INACTIVE.
11521  */
11522 static void
11523 phyint_inactive(phyint_t *phyi)
11524 {
11525 	ill_t *ill_v4;
11526 	ill_t *ill_v6;
11527 	ipif_t *ipif;
11528 	ilm_t *ilm;
11529 
11530 	ill_v4 = phyi->phyint_illv4;
11531 	ill_v6 = phyi->phyint_illv6;
11532 
11533 	/*
11534 	 * No need for a lock while traversing the list since iam
11535 	 * a writer
11536 	 */
11537 	if (ill_v4 != NULL) {
11538 		ASSERT(IAM_WRITER_ILL(ill_v4));
11539 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11540 		    ipif = ipif->ipif_next) {
11541 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11542 				mutex_enter(&phyi->phyint_lock);
11543 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11544 				mutex_exit(&phyi->phyint_lock);
11545 				return;
11546 			}
11547 		}
11548 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11549 		    ilm = ilm->ilm_next) {
11550 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11551 				mutex_enter(&phyi->phyint_lock);
11552 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11553 				mutex_exit(&phyi->phyint_lock);
11554 				return;
11555 			}
11556 		}
11557 	}
11558 	if (ill_v6 != NULL) {
11559 		ill_v6 = phyi->phyint_illv6;
11560 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11561 		    ipif = ipif->ipif_next) {
11562 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11563 				mutex_enter(&phyi->phyint_lock);
11564 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11565 				mutex_exit(&phyi->phyint_lock);
11566 				return;
11567 			}
11568 		}
11569 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11570 		    ilm = ilm->ilm_next) {
11571 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11572 				mutex_enter(&phyi->phyint_lock);
11573 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11574 				mutex_exit(&phyi->phyint_lock);
11575 				return;
11576 			}
11577 		}
11578 	}
11579 	mutex_enter(&phyi->phyint_lock);
11580 	phyi->phyint_flags |= PHYI_INACTIVE;
11581 	mutex_exit(&phyi->phyint_lock);
11582 }
11583 
11584 /*
11585  * This function is called only when the phyint flags change. Currently
11586  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11587  * that we can select a good ill.
11588  */
11589 static void
11590 ip_redo_nomination(phyint_t *phyi)
11591 {
11592 	ill_t *ill_v4;
11593 
11594 	ill_v4 = phyi->phyint_illv4;
11595 
11596 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11597 		ASSERT(IAM_WRITER_ILL(ill_v4));
11598 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11599 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11600 	}
11601 }
11602 
11603 /*
11604  * Heuristic to check if ill is INACTIVE.
11605  * Checks if ill has an ipif with an usable ip address.
11606  *
11607  * Return values:
11608  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11609  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11610  */
11611 static boolean_t
11612 ill_is_inactive(ill_t *ill)
11613 {
11614 	ipif_t *ipif;
11615 
11616 	/* Check whether it is in an IPMP group */
11617 	if (ill->ill_phyint->phyint_groupname == NULL)
11618 		return (B_FALSE);
11619 
11620 	if (ill->ill_ipif_up_count == 0)
11621 		return (B_TRUE);
11622 
11623 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11624 		uint64_t flags = ipif->ipif_flags;
11625 
11626 		/*
11627 		 * This ipif is usable if it is IPIF_UP and not a
11628 		 * dedicated test address.  A dedicated test address
11629 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11630 		 * (note in particular that V6 test addresses are
11631 		 * link-local data addresses and thus are marked
11632 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11633 		 */
11634 		if ((flags & IPIF_UP) &&
11635 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11636 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11637 			return (B_FALSE);
11638 	}
11639 	return (B_TRUE);
11640 }
11641 
11642 /*
11643  * Set interface flags.
11644  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11645  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11646  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11647  *
11648  * NOTE : We really don't enforce that ipif_id zero should be used
11649  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11650  *	  is because applications generally does SICGLIFFLAGS and
11651  *	  ORs in the new flags (that affects the logical) and does a
11652  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11653  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11654  *	  flags that will be turned on is correct with respect to
11655  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11656  */
11657 /* ARGSUSED */
11658 int
11659 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11660     ip_ioctl_cmd_t *ipip, void *if_req)
11661 {
11662 	uint64_t turn_on;
11663 	uint64_t turn_off;
11664 	int	err;
11665 	boolean_t need_up = B_FALSE;
11666 	phyint_t *phyi;
11667 	ill_t *ill;
11668 	uint64_t intf_flags;
11669 	boolean_t phyint_flags_modified = B_FALSE;
11670 	uint64_t flags;
11671 	struct ifreq *ifr;
11672 	struct lifreq *lifr;
11673 	boolean_t set_linklocal = B_FALSE;
11674 	boolean_t zero_source = B_FALSE;
11675 	ip_stack_t *ipst;
11676 
11677 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11678 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11679 
11680 	ASSERT(IAM_WRITER_IPIF(ipif));
11681 
11682 	ill = ipif->ipif_ill;
11683 	phyi = ill->ill_phyint;
11684 	ipst = ill->ill_ipst;
11685 
11686 	if (ipip->ipi_cmd_type == IF_CMD) {
11687 		ifr = (struct ifreq *)if_req;
11688 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11689 	} else {
11690 		lifr = (struct lifreq *)if_req;
11691 		flags = lifr->lifr_flags;
11692 	}
11693 
11694 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11695 
11696 	/*
11697 	 * Has the flags been set correctly till now ?
11698 	 */
11699 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11700 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11701 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11702 	/*
11703 	 * Compare the new flags to the old, and partition
11704 	 * into those coming on and those going off.
11705 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11706 	 */
11707 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11708 		flags |= intf_flags & ~0xFFFF;
11709 
11710 	/*
11711 	 * First check which bits will change and then which will
11712 	 * go on and off
11713 	 */
11714 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11715 	if (!turn_on)
11716 		return (0);	/* No change */
11717 
11718 	turn_off = intf_flags & turn_on;
11719 	turn_on ^= turn_off;
11720 	err = 0;
11721 
11722 	/*
11723 	 * Don't allow any bits belonging to the logical interface
11724 	 * to be set or cleared on the replacement ipif that was
11725 	 * created temporarily during a MOVE.
11726 	 */
11727 	if (ipif->ipif_replace_zero &&
11728 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11729 		return (EINVAL);
11730 	}
11731 
11732 	/*
11733 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11734 	 * IPv6 interfaces.
11735 	 */
11736 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11737 		return (EINVAL);
11738 
11739 	/*
11740 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11741 	 */
11742 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11743 		return (EINVAL);
11744 
11745 	/*
11746 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11747 	 * interfaces.  It makes no sense in that context.
11748 	 */
11749 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11750 		return (EINVAL);
11751 
11752 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11753 		zero_source = B_TRUE;
11754 
11755 	/*
11756 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11757 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11758 	 * If the link local address isn't set, and can be set, it will get
11759 	 * set later on in this function.
11760 	 */
11761 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11762 	    (flags & IFF_UP) && !zero_source &&
11763 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11764 		if (ipif_cant_setlinklocal(ipif))
11765 			return (EINVAL);
11766 		set_linklocal = B_TRUE;
11767 	}
11768 
11769 	/*
11770 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11771 	 * same time. No need to grab ill_g_usesrc_lock here, see
11772 	 * synchronization notes in ip.c
11773 	 */
11774 	if (turn_on & PHYI_STANDBY &&
11775 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11776 		return (EINVAL);
11777 	}
11778 
11779 	/*
11780 	 * If we modify physical interface flags, we'll potentially need to
11781 	 * send up two routing socket messages for the changes (one for the
11782 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11783 	 */
11784 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11785 		phyint_flags_modified = B_TRUE;
11786 
11787 	/*
11788 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11789 	 * we need to flush the IRE_CACHES belonging to this ill.
11790 	 * We handle this case here without doing the DOWN/UP dance
11791 	 * like it is done for other flags. If some other flags are
11792 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11793 	 * below will handle it by bringing it down and then
11794 	 * bringing it UP.
11795 	 */
11796 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11797 		ill_t *ill_v4, *ill_v6;
11798 
11799 		ill_v4 = phyi->phyint_illv4;
11800 		ill_v6 = phyi->phyint_illv6;
11801 
11802 		/*
11803 		 * First set the INACTIVE flag if needed. Then delete the ires.
11804 		 * ire_add will atomically prevent creating new IRE_CACHEs
11805 		 * unless hidden flag is set.
11806 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11807 		 */
11808 		if ((turn_on & PHYI_FAILED) &&
11809 		    ((intf_flags & PHYI_STANDBY) ||
11810 		    !ipst->ips_ipmp_enable_failback)) {
11811 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11812 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11813 		}
11814 		if ((turn_off & PHYI_FAILED) &&
11815 		    ((intf_flags & PHYI_STANDBY) ||
11816 		    (!ipst->ips_ipmp_enable_failback &&
11817 		    ill_is_inactive(ill)))) {
11818 			phyint_inactive(phyi);
11819 		}
11820 
11821 		if (turn_on & PHYI_STANDBY) {
11822 			/*
11823 			 * We implicitly set INACTIVE only when STANDBY is set.
11824 			 * INACTIVE is also set on non-STANDBY phyint when user
11825 			 * disables FAILBACK using configuration file.
11826 			 * Do not allow STANDBY to be set on such INACTIVE
11827 			 * phyint
11828 			 */
11829 			if (phyi->phyint_flags & PHYI_INACTIVE)
11830 				return (EINVAL);
11831 			if (!(phyi->phyint_flags & PHYI_FAILED))
11832 				phyint_inactive(phyi);
11833 		}
11834 		if (turn_off & PHYI_STANDBY) {
11835 			if (ipst->ips_ipmp_enable_failback) {
11836 				/*
11837 				 * Reset PHYI_INACTIVE.
11838 				 */
11839 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11840 			} else if (ill_is_inactive(ill) &&
11841 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11842 				/*
11843 				 * Need to set INACTIVE, when user sets
11844 				 * STANDBY on a non-STANDBY phyint and
11845 				 * later resets STANDBY
11846 				 */
11847 				phyint_inactive(phyi);
11848 			}
11849 		}
11850 		/*
11851 		 * We should always send up a message so that the
11852 		 * daemons come to know of it. Note that the zeroth
11853 		 * interface can be down and the check below for IPIF_UP
11854 		 * will not make sense as we are actually setting
11855 		 * a phyint flag here. We assume that the ipif used
11856 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11857 		 * send up any message for non-zero ipifs).
11858 		 */
11859 		phyint_flags_modified = B_TRUE;
11860 
11861 		if (ill_v4 != NULL) {
11862 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11863 			    IRE_CACHE, ill_stq_cache_delete,
11864 			    (char *)ill_v4, ill_v4);
11865 			illgrp_reset_schednext(ill_v4);
11866 		}
11867 		if (ill_v6 != NULL) {
11868 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11869 			    IRE_CACHE, ill_stq_cache_delete,
11870 			    (char *)ill_v6, ill_v6);
11871 			illgrp_reset_schednext(ill_v6);
11872 		}
11873 	}
11874 
11875 	/*
11876 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11877 	 * status of the interface and, if the interface is part of an IPMP
11878 	 * group, all other interfaces that are part of the same IPMP
11879 	 * group.
11880 	 */
11881 	if ((turn_on | turn_off) & ILLF_ROUTER)
11882 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11883 
11884 	/*
11885 	 * If the interface is not UP and we are not going to
11886 	 * bring it UP, record the flags and return. When the
11887 	 * interface comes UP later, the right actions will be
11888 	 * taken.
11889 	 */
11890 	if (!(ipif->ipif_flags & IPIF_UP) &&
11891 	    !(turn_on & IPIF_UP)) {
11892 		/* Record new flags in their respective places. */
11893 		mutex_enter(&ill->ill_lock);
11894 		mutex_enter(&ill->ill_phyint->phyint_lock);
11895 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11896 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11897 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11898 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11899 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11900 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11901 		mutex_exit(&ill->ill_lock);
11902 		mutex_exit(&ill->ill_phyint->phyint_lock);
11903 
11904 		/*
11905 		 * We do the broadcast and nomination here rather
11906 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11907 		 * the case of FAILBACK from INACTIVE standby to the
11908 		 * interface that has been repaired, PHYI_FAILED has not
11909 		 * been cleared yet. If there are only two interfaces in
11910 		 * that group, all we have is a FAILED and INACTIVE
11911 		 * interface. If we do the nomination soon after a failback,
11912 		 * the broadcast nomination code would select the
11913 		 * INACTIVE interface for receiving broadcasts as FAILED is
11914 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11915 		 * receive broadcast packets, we need to redo nomination
11916 		 * when the FAILED is cleared here. Thus, in general we
11917 		 * always do the nomination here for FAILED, STANDBY
11918 		 * and OFFLINE.
11919 		 */
11920 		if (((turn_on | turn_off) &
11921 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11922 			ip_redo_nomination(phyi);
11923 		}
11924 		if (phyint_flags_modified) {
11925 			if (phyi->phyint_illv4 != NULL) {
11926 				ip_rts_ifmsg(phyi->phyint_illv4->
11927 				    ill_ipif);
11928 			}
11929 			if (phyi->phyint_illv6 != NULL) {
11930 				ip_rts_ifmsg(phyi->phyint_illv6->
11931 				    ill_ipif);
11932 			}
11933 		}
11934 		return (0);
11935 	} else if (set_linklocal || zero_source) {
11936 		mutex_enter(&ill->ill_lock);
11937 		if (set_linklocal)
11938 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11939 		if (zero_source)
11940 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11941 		mutex_exit(&ill->ill_lock);
11942 	}
11943 
11944 	/*
11945 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11946 	 * or point-to-point interfaces with an unspecified destination. We do
11947 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11948 	 * have a subnet assigned, which is how in.ndpd currently manages its
11949 	 * onlink prefix list when no addresses are configured with those
11950 	 * prefixes.
11951 	 */
11952 	if (ipif->ipif_isv6 &&
11953 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11954 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11955 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11956 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11957 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11958 		return (EINVAL);
11959 	}
11960 
11961 	/*
11962 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11963 	 * from being brought up.
11964 	 */
11965 	if (!ipif->ipif_isv6 &&
11966 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11967 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11968 		return (EINVAL);
11969 	}
11970 
11971 	/*
11972 	 * The only flag changes that we currently take specific action on
11973 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11974 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11975 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11976 	 * the flags and bringing it back up again.
11977 	 */
11978 	if ((turn_on|turn_off) &
11979 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11980 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11981 		/*
11982 		 * Taking this ipif down, make sure we have
11983 		 * valid net and subnet bcast ire's for other
11984 		 * logical interfaces, if we need them.
11985 		 */
11986 		if (!ipif->ipif_isv6)
11987 			ipif_check_bcast_ires(ipif);
11988 
11989 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11990 		    !(turn_off & IPIF_UP)) {
11991 			need_up = B_TRUE;
11992 			if (ipif->ipif_flags & IPIF_UP)
11993 				ill->ill_logical_down = 1;
11994 			turn_on &= ~IPIF_UP;
11995 		}
11996 		err = ipif_down(ipif, q, mp);
11997 		ip1dbg(("ipif_down returns %d err ", err));
11998 		if (err == EINPROGRESS)
11999 			return (err);
12000 		ipif_down_tail(ipif);
12001 	}
12002 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12003 }
12004 
12005 static int
12006 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12007     boolean_t need_up)
12008 {
12009 	ill_t	*ill;
12010 	phyint_t *phyi;
12011 	uint64_t turn_on;
12012 	uint64_t turn_off;
12013 	uint64_t intf_flags;
12014 	boolean_t phyint_flags_modified = B_FALSE;
12015 	int	err = 0;
12016 	boolean_t set_linklocal = B_FALSE;
12017 	boolean_t zero_source = B_FALSE;
12018 
12019 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12020 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12021 
12022 	ASSERT(IAM_WRITER_IPIF(ipif));
12023 
12024 	ill = ipif->ipif_ill;
12025 	phyi = ill->ill_phyint;
12026 
12027 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12028 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12029 
12030 	turn_off = intf_flags & turn_on;
12031 	turn_on ^= turn_off;
12032 
12033 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12034 		phyint_flags_modified = B_TRUE;
12035 
12036 	/*
12037 	 * Now we change the flags. Track current value of
12038 	 * other flags in their respective places.
12039 	 */
12040 	mutex_enter(&ill->ill_lock);
12041 	mutex_enter(&phyi->phyint_lock);
12042 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12043 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12044 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12045 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12046 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12047 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12048 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12049 		set_linklocal = B_TRUE;
12050 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12051 	}
12052 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12053 		zero_source = B_TRUE;
12054 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12055 	}
12056 	mutex_exit(&ill->ill_lock);
12057 	mutex_exit(&phyi->phyint_lock);
12058 
12059 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12060 		ip_redo_nomination(phyi);
12061 
12062 	if (set_linklocal)
12063 		(void) ipif_setlinklocal(ipif);
12064 
12065 	if (zero_source)
12066 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12067 	else
12068 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12069 
12070 	if (need_up) {
12071 		/*
12072 		 * XXX ipif_up really does not know whether a phyint flags
12073 		 * was modified or not. So, it sends up information on
12074 		 * only one routing sockets message. As we don't bring up
12075 		 * the interface and also set STANDBY/FAILED simultaneously
12076 		 * it should be okay.
12077 		 */
12078 		err = ipif_up(ipif, q, mp);
12079 	} else {
12080 		/*
12081 		 * Make sure routing socket sees all changes to the flags.
12082 		 * ipif_up_done* handles this when we use ipif_up.
12083 		 */
12084 		if (phyint_flags_modified) {
12085 			if (phyi->phyint_illv4 != NULL) {
12086 				ip_rts_ifmsg(phyi->phyint_illv4->
12087 				    ill_ipif);
12088 			}
12089 			if (phyi->phyint_illv6 != NULL) {
12090 				ip_rts_ifmsg(phyi->phyint_illv6->
12091 				    ill_ipif);
12092 			}
12093 		} else {
12094 			ip_rts_ifmsg(ipif);
12095 		}
12096 		/*
12097 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12098 		 * this in need_up case.
12099 		 */
12100 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12101 	}
12102 	return (err);
12103 }
12104 
12105 /*
12106  * Restart entry point to restart the flags restart operation after the
12107  * refcounts have dropped to zero.
12108  */
12109 /* ARGSUSED */
12110 int
12111 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12112     ip_ioctl_cmd_t *ipip, void *if_req)
12113 {
12114 	int	err;
12115 	struct ifreq *ifr = (struct ifreq *)if_req;
12116 	struct lifreq *lifr = (struct lifreq *)if_req;
12117 
12118 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12119 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12120 
12121 	ipif_down_tail(ipif);
12122 	if (ipip->ipi_cmd_type == IF_CMD) {
12123 		/*
12124 		 * Since ip_sioctl_flags expects an int and ifr_flags
12125 		 * is a short we need to cast ifr_flags into an int
12126 		 * to avoid having sign extension cause bits to get
12127 		 * set that should not be.
12128 		 */
12129 		err = ip_sioctl_flags_tail(ipif,
12130 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12131 		    q, mp, B_TRUE);
12132 	} else {
12133 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12134 		    q, mp, B_TRUE);
12135 	}
12136 	return (err);
12137 }
12138 
12139 /*
12140  * Can operate on either a module or a driver queue.
12141  */
12142 /* ARGSUSED */
12143 int
12144 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12145     ip_ioctl_cmd_t *ipip, void *if_req)
12146 {
12147 	/*
12148 	 * Has the flags been set correctly till now ?
12149 	 */
12150 	ill_t *ill = ipif->ipif_ill;
12151 	phyint_t *phyi = ill->ill_phyint;
12152 
12153 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12154 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12155 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12156 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12157 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12158 
12159 	/*
12160 	 * Need a lock since some flags can be set even when there are
12161 	 * references to the ipif.
12162 	 */
12163 	mutex_enter(&ill->ill_lock);
12164 	if (ipip->ipi_cmd_type == IF_CMD) {
12165 		struct ifreq *ifr = (struct ifreq *)if_req;
12166 
12167 		/* Get interface flags (low 16 only). */
12168 		ifr->ifr_flags = ((ipif->ipif_flags |
12169 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12170 	} else {
12171 		struct lifreq *lifr = (struct lifreq *)if_req;
12172 
12173 		/* Get interface flags. */
12174 		lifr->lifr_flags = ipif->ipif_flags |
12175 		    ill->ill_flags | phyi->phyint_flags;
12176 	}
12177 	mutex_exit(&ill->ill_lock);
12178 	return (0);
12179 }
12180 
12181 /* ARGSUSED */
12182 int
12183 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12184     ip_ioctl_cmd_t *ipip, void *if_req)
12185 {
12186 	int mtu;
12187 	int ip_min_mtu;
12188 	struct ifreq	*ifr;
12189 	struct lifreq *lifr;
12190 	ire_t	*ire;
12191 	ip_stack_t *ipst;
12192 
12193 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12194 	    ipif->ipif_id, (void *)ipif));
12195 	if (ipip->ipi_cmd_type == IF_CMD) {
12196 		ifr = (struct ifreq *)if_req;
12197 		mtu = ifr->ifr_metric;
12198 	} else {
12199 		lifr = (struct lifreq *)if_req;
12200 		mtu = lifr->lifr_mtu;
12201 	}
12202 
12203 	if (ipif->ipif_isv6)
12204 		ip_min_mtu = IPV6_MIN_MTU;
12205 	else
12206 		ip_min_mtu = IP_MIN_MTU;
12207 
12208 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12209 		return (EINVAL);
12210 
12211 	/*
12212 	 * Change the MTU size in all relevant ire's.
12213 	 * Mtu change Vs. new ire creation - protocol below.
12214 	 * First change ipif_mtu and the ire_max_frag of the
12215 	 * interface ire. Then do an ire walk and change the
12216 	 * ire_max_frag of all affected ires. During ire_add
12217 	 * under the bucket lock, set the ire_max_frag of the
12218 	 * new ire being created from the ipif/ire from which
12219 	 * it is being derived. If an mtu change happens after
12220 	 * the ire is added, the new ire will be cleaned up.
12221 	 * Conversely if the mtu change happens before the ire
12222 	 * is added, ire_add will see the new value of the mtu.
12223 	 */
12224 	ipif->ipif_mtu = mtu;
12225 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12226 
12227 	if (ipif->ipif_isv6)
12228 		ire = ipif_to_ire_v6(ipif);
12229 	else
12230 		ire = ipif_to_ire(ipif);
12231 	if (ire != NULL) {
12232 		ire->ire_max_frag = ipif->ipif_mtu;
12233 		ire_refrele(ire);
12234 	}
12235 	ipst = ipif->ipif_ill->ill_ipst;
12236 	if (ipif->ipif_flags & IPIF_UP) {
12237 		if (ipif->ipif_isv6)
12238 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12239 			    ipst);
12240 		else
12241 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12242 			    ipst);
12243 	}
12244 	/* Update the MTU in SCTP's list */
12245 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12246 	return (0);
12247 }
12248 
12249 /* Get interface MTU. */
12250 /* ARGSUSED */
12251 int
12252 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12253 	ip_ioctl_cmd_t *ipip, void *if_req)
12254 {
12255 	struct ifreq	*ifr;
12256 	struct lifreq	*lifr;
12257 
12258 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12259 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12260 	if (ipip->ipi_cmd_type == IF_CMD) {
12261 		ifr = (struct ifreq *)if_req;
12262 		ifr->ifr_metric = ipif->ipif_mtu;
12263 	} else {
12264 		lifr = (struct lifreq *)if_req;
12265 		lifr->lifr_mtu = ipif->ipif_mtu;
12266 	}
12267 	return (0);
12268 }
12269 
12270 /* Set interface broadcast address. */
12271 /* ARGSUSED2 */
12272 int
12273 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12274 	ip_ioctl_cmd_t *ipip, void *if_req)
12275 {
12276 	ipaddr_t addr;
12277 	ire_t	*ire;
12278 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12279 
12280 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12281 	    ipif->ipif_id));
12282 
12283 	ASSERT(IAM_WRITER_IPIF(ipif));
12284 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12285 		return (EADDRNOTAVAIL);
12286 
12287 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12288 
12289 	if (sin->sin_family != AF_INET)
12290 		return (EAFNOSUPPORT);
12291 
12292 	addr = sin->sin_addr.s_addr;
12293 	if (ipif->ipif_flags & IPIF_UP) {
12294 		/*
12295 		 * If we are already up, make sure the new
12296 		 * broadcast address makes sense.  If it does,
12297 		 * there should be an IRE for it already.
12298 		 * Don't match on ipif, only on the ill
12299 		 * since we are sharing these now. Don't use
12300 		 * MATCH_IRE_ILL_GROUP as we are looking for
12301 		 * the broadcast ire on this ill and each ill
12302 		 * in the group has its own broadcast ire.
12303 		 */
12304 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12305 		    ipif, ALL_ZONES, NULL,
12306 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12307 		if (ire == NULL) {
12308 			return (EINVAL);
12309 		} else {
12310 			ire_refrele(ire);
12311 		}
12312 	}
12313 	/*
12314 	 * Changing the broadcast addr for this ipif.
12315 	 * Make sure we have valid net and subnet bcast
12316 	 * ire's for other logical interfaces, if needed.
12317 	 */
12318 	if (addr != ipif->ipif_brd_addr)
12319 		ipif_check_bcast_ires(ipif);
12320 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12321 	return (0);
12322 }
12323 
12324 /* Get interface broadcast address. */
12325 /* ARGSUSED */
12326 int
12327 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12328     ip_ioctl_cmd_t *ipip, void *if_req)
12329 {
12330 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12331 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12332 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12333 		return (EADDRNOTAVAIL);
12334 
12335 	/* IPIF_BROADCAST not possible with IPv6 */
12336 	ASSERT(!ipif->ipif_isv6);
12337 	*sin = sin_null;
12338 	sin->sin_family = AF_INET;
12339 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12340 	return (0);
12341 }
12342 
12343 /*
12344  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12345  */
12346 /* ARGSUSED */
12347 int
12348 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12349     ip_ioctl_cmd_t *ipip, void *if_req)
12350 {
12351 	int err = 0;
12352 	in6_addr_t v6mask;
12353 
12354 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12355 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12356 
12357 	ASSERT(IAM_WRITER_IPIF(ipif));
12358 
12359 	if (ipif->ipif_isv6) {
12360 		sin6_t *sin6;
12361 
12362 		if (sin->sin_family != AF_INET6)
12363 			return (EAFNOSUPPORT);
12364 
12365 		sin6 = (sin6_t *)sin;
12366 		v6mask = sin6->sin6_addr;
12367 	} else {
12368 		ipaddr_t mask;
12369 
12370 		if (sin->sin_family != AF_INET)
12371 			return (EAFNOSUPPORT);
12372 
12373 		mask = sin->sin_addr.s_addr;
12374 		V4MASK_TO_V6(mask, v6mask);
12375 	}
12376 
12377 	/*
12378 	 * No big deal if the interface isn't already up, or the mask
12379 	 * isn't really changing, or this is pt-pt.
12380 	 */
12381 	if (!(ipif->ipif_flags & IPIF_UP) ||
12382 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12383 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12384 		ipif->ipif_v6net_mask = v6mask;
12385 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12386 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12387 			    ipif->ipif_v6net_mask,
12388 			    ipif->ipif_v6subnet);
12389 		}
12390 		return (0);
12391 	}
12392 	/*
12393 	 * Make sure we have valid net and subnet broadcast ire's
12394 	 * for the old netmask, if needed by other logical interfaces.
12395 	 */
12396 	if (!ipif->ipif_isv6)
12397 		ipif_check_bcast_ires(ipif);
12398 
12399 	err = ipif_logical_down(ipif, q, mp);
12400 	if (err == EINPROGRESS)
12401 		return (err);
12402 	ipif_down_tail(ipif);
12403 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12404 	return (err);
12405 }
12406 
12407 static int
12408 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12409 {
12410 	in6_addr_t v6mask;
12411 	int err = 0;
12412 
12413 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12414 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12415 
12416 	if (ipif->ipif_isv6) {
12417 		sin6_t *sin6;
12418 
12419 		sin6 = (sin6_t *)sin;
12420 		v6mask = sin6->sin6_addr;
12421 	} else {
12422 		ipaddr_t mask;
12423 
12424 		mask = sin->sin_addr.s_addr;
12425 		V4MASK_TO_V6(mask, v6mask);
12426 	}
12427 
12428 	ipif->ipif_v6net_mask = v6mask;
12429 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12430 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12431 		    ipif->ipif_v6subnet);
12432 	}
12433 	err = ipif_up(ipif, q, mp);
12434 
12435 	if (err == 0 || err == EINPROGRESS) {
12436 		/*
12437 		 * The interface must be DL_BOUND if this packet has to
12438 		 * go out on the wire. Since we only go through a logical
12439 		 * down and are bound with the driver during an internal
12440 		 * down/up that is satisfied.
12441 		 */
12442 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12443 			/* Potentially broadcast an address mask reply. */
12444 			ipif_mask_reply(ipif);
12445 		}
12446 	}
12447 	return (err);
12448 }
12449 
12450 /* ARGSUSED */
12451 int
12452 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12453     ip_ioctl_cmd_t *ipip, void *if_req)
12454 {
12455 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12456 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12457 	ipif_down_tail(ipif);
12458 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12459 }
12460 
12461 /* Get interface net mask. */
12462 /* ARGSUSED */
12463 int
12464 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12465     ip_ioctl_cmd_t *ipip, void *if_req)
12466 {
12467 	struct lifreq *lifr = (struct lifreq *)if_req;
12468 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12469 
12470 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12471 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12472 
12473 	/*
12474 	 * net mask can't change since we have a reference to the ipif.
12475 	 */
12476 	if (ipif->ipif_isv6) {
12477 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12478 		*sin6 = sin6_null;
12479 		sin6->sin6_family = AF_INET6;
12480 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12481 		lifr->lifr_addrlen =
12482 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12483 	} else {
12484 		*sin = sin_null;
12485 		sin->sin_family = AF_INET;
12486 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12487 		if (ipip->ipi_cmd_type == LIF_CMD) {
12488 			lifr->lifr_addrlen =
12489 			    ip_mask_to_plen(ipif->ipif_net_mask);
12490 		}
12491 	}
12492 	return (0);
12493 }
12494 
12495 /* ARGSUSED */
12496 int
12497 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12498     ip_ioctl_cmd_t *ipip, void *if_req)
12499 {
12500 
12501 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12502 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12503 	/*
12504 	 * Set interface metric.  We don't use this for
12505 	 * anything but we keep track of it in case it is
12506 	 * important to routing applications or such.
12507 	 */
12508 	if (ipip->ipi_cmd_type == IF_CMD) {
12509 		struct ifreq    *ifr;
12510 
12511 		ifr = (struct ifreq *)if_req;
12512 		ipif->ipif_metric = ifr->ifr_metric;
12513 	} else {
12514 		struct lifreq   *lifr;
12515 
12516 		lifr = (struct lifreq *)if_req;
12517 		ipif->ipif_metric = lifr->lifr_metric;
12518 	}
12519 	return (0);
12520 }
12521 
12522 
12523 /* ARGSUSED */
12524 int
12525 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12526     ip_ioctl_cmd_t *ipip, void *if_req)
12527 {
12528 
12529 	/* Get interface metric. */
12530 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12531 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12532 	if (ipip->ipi_cmd_type == IF_CMD) {
12533 		struct ifreq    *ifr;
12534 
12535 		ifr = (struct ifreq *)if_req;
12536 		ifr->ifr_metric = ipif->ipif_metric;
12537 	} else {
12538 		struct lifreq   *lifr;
12539 
12540 		lifr = (struct lifreq *)if_req;
12541 		lifr->lifr_metric = ipif->ipif_metric;
12542 	}
12543 
12544 	return (0);
12545 }
12546 
12547 /* ARGSUSED */
12548 int
12549 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12550     ip_ioctl_cmd_t *ipip, void *if_req)
12551 {
12552 
12553 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12554 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12555 	/*
12556 	 * Set the muxid returned from I_PLINK.
12557 	 */
12558 	if (ipip->ipi_cmd_type == IF_CMD) {
12559 		struct ifreq *ifr = (struct ifreq *)if_req;
12560 
12561 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12562 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12563 	} else {
12564 		struct lifreq *lifr = (struct lifreq *)if_req;
12565 
12566 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12567 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12568 	}
12569 	return (0);
12570 }
12571 
12572 /* ARGSUSED */
12573 int
12574 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12575     ip_ioctl_cmd_t *ipip, void *if_req)
12576 {
12577 
12578 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12579 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12580 	/*
12581 	 * Get the muxid saved in ill for I_PUNLINK.
12582 	 */
12583 	if (ipip->ipi_cmd_type == IF_CMD) {
12584 		struct ifreq *ifr = (struct ifreq *)if_req;
12585 
12586 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12587 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12588 	} else {
12589 		struct lifreq *lifr = (struct lifreq *)if_req;
12590 
12591 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12592 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12593 	}
12594 	return (0);
12595 }
12596 
12597 /*
12598  * Set the subnet prefix. Does not modify the broadcast address.
12599  */
12600 /* ARGSUSED */
12601 int
12602 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12603     ip_ioctl_cmd_t *ipip, void *if_req)
12604 {
12605 	int err = 0;
12606 	in6_addr_t v6addr;
12607 	in6_addr_t v6mask;
12608 	boolean_t need_up = B_FALSE;
12609 	int addrlen;
12610 
12611 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12612 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12613 
12614 	ASSERT(IAM_WRITER_IPIF(ipif));
12615 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12616 
12617 	if (ipif->ipif_isv6) {
12618 		sin6_t *sin6;
12619 
12620 		if (sin->sin_family != AF_INET6)
12621 			return (EAFNOSUPPORT);
12622 
12623 		sin6 = (sin6_t *)sin;
12624 		v6addr = sin6->sin6_addr;
12625 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12626 			return (EADDRNOTAVAIL);
12627 	} else {
12628 		ipaddr_t addr;
12629 
12630 		if (sin->sin_family != AF_INET)
12631 			return (EAFNOSUPPORT);
12632 
12633 		addr = sin->sin_addr.s_addr;
12634 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12635 			return (EADDRNOTAVAIL);
12636 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12637 		/* Add 96 bits */
12638 		addrlen += IPV6_ABITS - IP_ABITS;
12639 	}
12640 
12641 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12642 		return (EINVAL);
12643 
12644 	/* Check if bits in the address is set past the mask */
12645 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12646 		return (EINVAL);
12647 
12648 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12649 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12650 		return (0);	/* No change */
12651 
12652 	if (ipif->ipif_flags & IPIF_UP) {
12653 		/*
12654 		 * If the interface is already marked up,
12655 		 * we call ipif_down which will take care
12656 		 * of ditching any IREs that have been set
12657 		 * up based on the old interface address.
12658 		 */
12659 		err = ipif_logical_down(ipif, q, mp);
12660 		if (err == EINPROGRESS)
12661 			return (err);
12662 		ipif_down_tail(ipif);
12663 		need_up = B_TRUE;
12664 	}
12665 
12666 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12667 	return (err);
12668 }
12669 
12670 static int
12671 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12672     queue_t *q, mblk_t *mp, boolean_t need_up)
12673 {
12674 	ill_t	*ill = ipif->ipif_ill;
12675 	int	err = 0;
12676 
12677 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12678 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12679 
12680 	/* Set the new address. */
12681 	mutex_enter(&ill->ill_lock);
12682 	ipif->ipif_v6net_mask = v6mask;
12683 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12684 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12685 		    ipif->ipif_v6subnet);
12686 	}
12687 	mutex_exit(&ill->ill_lock);
12688 
12689 	if (need_up) {
12690 		/*
12691 		 * Now bring the interface back up.  If this
12692 		 * is the only IPIF for the ILL, ipif_up
12693 		 * will have to re-bind to the device, so
12694 		 * we may get back EINPROGRESS, in which
12695 		 * case, this IOCTL will get completed in
12696 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12697 		 */
12698 		err = ipif_up(ipif, q, mp);
12699 		if (err == EINPROGRESS)
12700 			return (err);
12701 	}
12702 	return (err);
12703 }
12704 
12705 /* ARGSUSED */
12706 int
12707 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12708     ip_ioctl_cmd_t *ipip, void *if_req)
12709 {
12710 	int	addrlen;
12711 	in6_addr_t v6addr;
12712 	in6_addr_t v6mask;
12713 	struct lifreq *lifr = (struct lifreq *)if_req;
12714 
12715 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12716 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12717 	ipif_down_tail(ipif);
12718 
12719 	addrlen = lifr->lifr_addrlen;
12720 	if (ipif->ipif_isv6) {
12721 		sin6_t *sin6;
12722 
12723 		sin6 = (sin6_t *)sin;
12724 		v6addr = sin6->sin6_addr;
12725 	} else {
12726 		ipaddr_t addr;
12727 
12728 		addr = sin->sin_addr.s_addr;
12729 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12730 		addrlen += IPV6_ABITS - IP_ABITS;
12731 	}
12732 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12733 
12734 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12735 }
12736 
12737 /* ARGSUSED */
12738 int
12739 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12740     ip_ioctl_cmd_t *ipip, void *if_req)
12741 {
12742 	struct lifreq *lifr = (struct lifreq *)if_req;
12743 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12744 
12745 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12746 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12747 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12748 
12749 	if (ipif->ipif_isv6) {
12750 		*sin6 = sin6_null;
12751 		sin6->sin6_family = AF_INET6;
12752 		sin6->sin6_addr = ipif->ipif_v6subnet;
12753 		lifr->lifr_addrlen =
12754 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12755 	} else {
12756 		*sin = sin_null;
12757 		sin->sin_family = AF_INET;
12758 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12759 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12760 	}
12761 	return (0);
12762 }
12763 
12764 /*
12765  * Set the IPv6 address token.
12766  */
12767 /* ARGSUSED */
12768 int
12769 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12770     ip_ioctl_cmd_t *ipi, void *if_req)
12771 {
12772 	ill_t *ill = ipif->ipif_ill;
12773 	int err;
12774 	in6_addr_t v6addr;
12775 	in6_addr_t v6mask;
12776 	boolean_t need_up = B_FALSE;
12777 	int i;
12778 	sin6_t *sin6 = (sin6_t *)sin;
12779 	struct lifreq *lifr = (struct lifreq *)if_req;
12780 	int addrlen;
12781 
12782 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12783 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12784 	ASSERT(IAM_WRITER_IPIF(ipif));
12785 
12786 	addrlen = lifr->lifr_addrlen;
12787 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12788 	if (ipif->ipif_id != 0)
12789 		return (EINVAL);
12790 
12791 	if (!ipif->ipif_isv6)
12792 		return (EINVAL);
12793 
12794 	if (addrlen > IPV6_ABITS)
12795 		return (EINVAL);
12796 
12797 	v6addr = sin6->sin6_addr;
12798 
12799 	/*
12800 	 * The length of the token is the length from the end.  To get
12801 	 * the proper mask for this, compute the mask of the bits not
12802 	 * in the token; ie. the prefix, and then xor to get the mask.
12803 	 */
12804 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12805 		return (EINVAL);
12806 	for (i = 0; i < 4; i++) {
12807 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12808 	}
12809 
12810 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12811 	    ill->ill_token_length == addrlen)
12812 		return (0);	/* No change */
12813 
12814 	if (ipif->ipif_flags & IPIF_UP) {
12815 		err = ipif_logical_down(ipif, q, mp);
12816 		if (err == EINPROGRESS)
12817 			return (err);
12818 		ipif_down_tail(ipif);
12819 		need_up = B_TRUE;
12820 	}
12821 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12822 	return (err);
12823 }
12824 
12825 static int
12826 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12827     mblk_t *mp, boolean_t need_up)
12828 {
12829 	in6_addr_t v6addr;
12830 	in6_addr_t v6mask;
12831 	ill_t	*ill = ipif->ipif_ill;
12832 	int	i;
12833 	int	err = 0;
12834 
12835 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12836 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12837 	v6addr = sin6->sin6_addr;
12838 	/*
12839 	 * The length of the token is the length from the end.  To get
12840 	 * the proper mask for this, compute the mask of the bits not
12841 	 * in the token; ie. the prefix, and then xor to get the mask.
12842 	 */
12843 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12844 	for (i = 0; i < 4; i++)
12845 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12846 
12847 	mutex_enter(&ill->ill_lock);
12848 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12849 	ill->ill_token_length = addrlen;
12850 	mutex_exit(&ill->ill_lock);
12851 
12852 	if (need_up) {
12853 		/*
12854 		 * Now bring the interface back up.  If this
12855 		 * is the only IPIF for the ILL, ipif_up
12856 		 * will have to re-bind to the device, so
12857 		 * we may get back EINPROGRESS, in which
12858 		 * case, this IOCTL will get completed in
12859 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12860 		 */
12861 		err = ipif_up(ipif, q, mp);
12862 		if (err == EINPROGRESS)
12863 			return (err);
12864 	}
12865 	return (err);
12866 }
12867 
12868 /* ARGSUSED */
12869 int
12870 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12871     ip_ioctl_cmd_t *ipi, void *if_req)
12872 {
12873 	ill_t *ill;
12874 	sin6_t *sin6 = (sin6_t *)sin;
12875 	struct lifreq *lifr = (struct lifreq *)if_req;
12876 
12877 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12878 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12879 	if (ipif->ipif_id != 0)
12880 		return (EINVAL);
12881 
12882 	ill = ipif->ipif_ill;
12883 	if (!ill->ill_isv6)
12884 		return (ENXIO);
12885 
12886 	*sin6 = sin6_null;
12887 	sin6->sin6_family = AF_INET6;
12888 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12889 	sin6->sin6_addr = ill->ill_token;
12890 	lifr->lifr_addrlen = ill->ill_token_length;
12891 	return (0);
12892 }
12893 
12894 /*
12895  * Set (hardware) link specific information that might override
12896  * what was acquired through the DL_INFO_ACK.
12897  * The logic is as follows.
12898  *
12899  * become exclusive
12900  * set CHANGING flag
12901  * change mtu on affected IREs
12902  * clear CHANGING flag
12903  *
12904  * An ire add that occurs before the CHANGING flag is set will have its mtu
12905  * changed by the ip_sioctl_lnkinfo.
12906  *
12907  * During the time the CHANGING flag is set, no new ires will be added to the
12908  * bucket, and ire add will fail (due the CHANGING flag).
12909  *
12910  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12911  * before it is added to the bucket.
12912  *
12913  * Obviously only 1 thread can set the CHANGING flag and we need to become
12914  * exclusive to set the flag.
12915  */
12916 /* ARGSUSED */
12917 int
12918 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12919     ip_ioctl_cmd_t *ipi, void *if_req)
12920 {
12921 	ill_t		*ill = ipif->ipif_ill;
12922 	ipif_t		*nipif;
12923 	int		ip_min_mtu;
12924 	boolean_t	mtu_walk = B_FALSE;
12925 	struct lifreq	*lifr = (struct lifreq *)if_req;
12926 	lif_ifinfo_req_t *lir;
12927 	ire_t		*ire;
12928 
12929 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12930 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12931 	lir = &lifr->lifr_ifinfo;
12932 	ASSERT(IAM_WRITER_IPIF(ipif));
12933 
12934 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12935 	if (ipif->ipif_id != 0)
12936 		return (EINVAL);
12937 
12938 	/* Set interface MTU. */
12939 	if (ipif->ipif_isv6)
12940 		ip_min_mtu = IPV6_MIN_MTU;
12941 	else
12942 		ip_min_mtu = IP_MIN_MTU;
12943 
12944 	/*
12945 	 * Verify values before we set anything. Allow zero to
12946 	 * mean unspecified.
12947 	 */
12948 	if (lir->lir_maxmtu != 0 &&
12949 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12950 	    lir->lir_maxmtu < ip_min_mtu))
12951 		return (EINVAL);
12952 	if (lir->lir_reachtime != 0 &&
12953 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12954 		return (EINVAL);
12955 	if (lir->lir_reachretrans != 0 &&
12956 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12957 		return (EINVAL);
12958 
12959 	mutex_enter(&ill->ill_lock);
12960 	ill->ill_state_flags |= ILL_CHANGING;
12961 	for (nipif = ill->ill_ipif; nipif != NULL;
12962 	    nipif = nipif->ipif_next) {
12963 		nipif->ipif_state_flags |= IPIF_CHANGING;
12964 	}
12965 
12966 	mutex_exit(&ill->ill_lock);
12967 
12968 	if (lir->lir_maxmtu != 0) {
12969 		ill->ill_max_mtu = lir->lir_maxmtu;
12970 		ill->ill_mtu_userspecified = 1;
12971 		mtu_walk = B_TRUE;
12972 	}
12973 
12974 	if (lir->lir_reachtime != 0)
12975 		ill->ill_reachable_time = lir->lir_reachtime;
12976 
12977 	if (lir->lir_reachretrans != 0)
12978 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12979 
12980 	ill->ill_max_hops = lir->lir_maxhops;
12981 
12982 	ill->ill_max_buf = ND_MAX_Q;
12983 
12984 	if (mtu_walk) {
12985 		/*
12986 		 * Set the MTU on all ipifs associated with this ill except
12987 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12988 		 */
12989 		for (nipif = ill->ill_ipif; nipif != NULL;
12990 		    nipif = nipif->ipif_next) {
12991 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12992 				continue;
12993 
12994 			nipif->ipif_mtu = ill->ill_max_mtu;
12995 
12996 			if (!(nipif->ipif_flags & IPIF_UP))
12997 				continue;
12998 
12999 			if (nipif->ipif_isv6)
13000 				ire = ipif_to_ire_v6(nipif);
13001 			else
13002 				ire = ipif_to_ire(nipif);
13003 			if (ire != NULL) {
13004 				ire->ire_max_frag = ipif->ipif_mtu;
13005 				ire_refrele(ire);
13006 			}
13007 			if (ill->ill_isv6) {
13008 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13009 				    ipif_mtu_change, (char *)nipif,
13010 				    ill);
13011 			} else {
13012 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13013 				    ipif_mtu_change, (char *)nipif,
13014 				    ill);
13015 			}
13016 		}
13017 	}
13018 
13019 	mutex_enter(&ill->ill_lock);
13020 	for (nipif = ill->ill_ipif; nipif != NULL;
13021 	    nipif = nipif->ipif_next) {
13022 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13023 	}
13024 	ILL_UNMARK_CHANGING(ill);
13025 	mutex_exit(&ill->ill_lock);
13026 
13027 	return (0);
13028 }
13029 
13030 /* ARGSUSED */
13031 int
13032 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13033     ip_ioctl_cmd_t *ipi, void *if_req)
13034 {
13035 	struct lif_ifinfo_req *lir;
13036 	ill_t *ill = ipif->ipif_ill;
13037 
13038 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13039 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13040 	if (ipif->ipif_id != 0)
13041 		return (EINVAL);
13042 
13043 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13044 	lir->lir_maxhops = ill->ill_max_hops;
13045 	lir->lir_reachtime = ill->ill_reachable_time;
13046 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13047 	lir->lir_maxmtu = ill->ill_max_mtu;
13048 
13049 	return (0);
13050 }
13051 
13052 /*
13053  * Return best guess as to the subnet mask for the specified address.
13054  * Based on the subnet masks for all the configured interfaces.
13055  *
13056  * We end up returning a zero mask in the case of default, multicast or
13057  * experimental.
13058  */
13059 static ipaddr_t
13060 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13061 {
13062 	ipaddr_t net_mask;
13063 	ill_t	*ill;
13064 	ipif_t	*ipif;
13065 	ill_walk_context_t ctx;
13066 	ipif_t	*fallback_ipif = NULL;
13067 
13068 	net_mask = ip_net_mask(addr);
13069 	if (net_mask == 0) {
13070 		*ipifp = NULL;
13071 		return (0);
13072 	}
13073 
13074 	/* Let's check to see if this is maybe a local subnet route. */
13075 	/* this function only applies to IPv4 interfaces */
13076 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13077 	ill = ILL_START_WALK_V4(&ctx, ipst);
13078 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13079 		mutex_enter(&ill->ill_lock);
13080 		for (ipif = ill->ill_ipif; ipif != NULL;
13081 		    ipif = ipif->ipif_next) {
13082 			if (!IPIF_CAN_LOOKUP(ipif))
13083 				continue;
13084 			if (!(ipif->ipif_flags & IPIF_UP))
13085 				continue;
13086 			if ((ipif->ipif_subnet & net_mask) ==
13087 			    (addr & net_mask)) {
13088 				/*
13089 				 * Don't trust pt-pt interfaces if there are
13090 				 * other interfaces.
13091 				 */
13092 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13093 					if (fallback_ipif == NULL) {
13094 						ipif_refhold_locked(ipif);
13095 						fallback_ipif = ipif;
13096 					}
13097 					continue;
13098 				}
13099 
13100 				/*
13101 				 * Fine. Just assume the same net mask as the
13102 				 * directly attached subnet interface is using.
13103 				 */
13104 				ipif_refhold_locked(ipif);
13105 				mutex_exit(&ill->ill_lock);
13106 				rw_exit(&ipst->ips_ill_g_lock);
13107 				if (fallback_ipif != NULL)
13108 					ipif_refrele(fallback_ipif);
13109 				*ipifp = ipif;
13110 				return (ipif->ipif_net_mask);
13111 			}
13112 		}
13113 		mutex_exit(&ill->ill_lock);
13114 	}
13115 	rw_exit(&ipst->ips_ill_g_lock);
13116 
13117 	*ipifp = fallback_ipif;
13118 	return ((fallback_ipif != NULL) ?
13119 	    fallback_ipif->ipif_net_mask : net_mask);
13120 }
13121 
13122 /*
13123  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13124  */
13125 static void
13126 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13127 {
13128 	IOCP	iocp;
13129 	ipft_t	*ipft;
13130 	ipllc_t	*ipllc;
13131 	mblk_t	*mp1;
13132 	cred_t	*cr;
13133 	int	error = 0;
13134 	conn_t	*connp;
13135 
13136 	ip1dbg(("ip_wput_ioctl"));
13137 	iocp = (IOCP)mp->b_rptr;
13138 	mp1 = mp->b_cont;
13139 	if (mp1 == NULL) {
13140 		iocp->ioc_error = EINVAL;
13141 		mp->b_datap->db_type = M_IOCNAK;
13142 		iocp->ioc_count = 0;
13143 		qreply(q, mp);
13144 		return;
13145 	}
13146 
13147 	/*
13148 	 * These IOCTLs provide various control capabilities to
13149 	 * upstream agents such as ULPs and processes.	There
13150 	 * are currently two such IOCTLs implemented.  They
13151 	 * are used by TCP to provide update information for
13152 	 * existing IREs and to forcibly delete an IRE for a
13153 	 * host that is not responding, thereby forcing an
13154 	 * attempt at a new route.
13155 	 */
13156 	iocp->ioc_error = EINVAL;
13157 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13158 		goto done;
13159 
13160 	ipllc = (ipllc_t *)mp1->b_rptr;
13161 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13162 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13163 			break;
13164 	}
13165 	/*
13166 	 * prefer credential from mblk over ioctl;
13167 	 * see ip_sioctl_copyin_setup
13168 	 */
13169 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13170 
13171 	/*
13172 	 * Refhold the conn in case the request gets queued up in some lookup
13173 	 */
13174 	ASSERT(CONN_Q(q));
13175 	connp = Q_TO_CONN(q);
13176 	CONN_INC_REF(connp);
13177 	if (ipft->ipft_pfi &&
13178 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13179 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13180 		error = (*ipft->ipft_pfi)(q,
13181 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13182 	}
13183 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13184 		/*
13185 		 * CONN_OPER_PENDING_DONE happens in the function called
13186 		 * through ipft_pfi above.
13187 		 */
13188 		return;
13189 	}
13190 
13191 	CONN_OPER_PENDING_DONE(connp);
13192 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13193 		freemsg(mp);
13194 		return;
13195 	}
13196 	iocp->ioc_error = error;
13197 
13198 done:
13199 	mp->b_datap->db_type = M_IOCACK;
13200 	if (iocp->ioc_error)
13201 		iocp->ioc_count = 0;
13202 	qreply(q, mp);
13203 }
13204 
13205 /*
13206  * Lookup an ipif using the sequence id (ipif_seqid)
13207  */
13208 ipif_t *
13209 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13210 {
13211 	ipif_t *ipif;
13212 
13213 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13214 
13215 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13216 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13217 			return (ipif);
13218 	}
13219 	return (NULL);
13220 }
13221 
13222 /*
13223  * Assign a unique id for the ipif. This is used later when we send
13224  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13225  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13226  * IRE is added, we verify that ipif has not disappeared.
13227  */
13228 
13229 static void
13230 ipif_assign_seqid(ipif_t *ipif)
13231 {
13232 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13233 
13234 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13235 }
13236 
13237 /*
13238  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13239  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13240  * be inserted into the first space available in the list. The value of
13241  * ipif_id will then be set to the appropriate value for its position.
13242  */
13243 static int
13244 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13245 {
13246 	ill_t *ill;
13247 	ipif_t *tipif;
13248 	ipif_t **tipifp;
13249 	int id;
13250 	ip_stack_t	*ipst;
13251 
13252 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13253 	    IAM_WRITER_IPIF(ipif));
13254 
13255 	ill = ipif->ipif_ill;
13256 	ASSERT(ill != NULL);
13257 	ipst = ill->ill_ipst;
13258 
13259 	/*
13260 	 * In the case of lo0:0 we already hold the ill_g_lock.
13261 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13262 	 * ipif_insert. Another such caller is ipif_move.
13263 	 */
13264 	if (acquire_g_lock)
13265 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13266 	if (acquire_ill_lock)
13267 		mutex_enter(&ill->ill_lock);
13268 	id = ipif->ipif_id;
13269 	tipifp = &(ill->ill_ipif);
13270 	if (id == -1) {	/* need to find a real id */
13271 		id = 0;
13272 		while ((tipif = *tipifp) != NULL) {
13273 			ASSERT(tipif->ipif_id >= id);
13274 			if (tipif->ipif_id != id)
13275 				break; /* non-consecutive id */
13276 			id++;
13277 			tipifp = &(tipif->ipif_next);
13278 		}
13279 		/* limit number of logical interfaces */
13280 		if (id >= ipst->ips_ip_addrs_per_if) {
13281 			if (acquire_ill_lock)
13282 				mutex_exit(&ill->ill_lock);
13283 			if (acquire_g_lock)
13284 				rw_exit(&ipst->ips_ill_g_lock);
13285 			return (-1);
13286 		}
13287 		ipif->ipif_id = id; /* assign new id */
13288 	} else if (id < ipst->ips_ip_addrs_per_if) {
13289 		/* we have a real id; insert ipif in the right place */
13290 		while ((tipif = *tipifp) != NULL) {
13291 			ASSERT(tipif->ipif_id != id);
13292 			if (tipif->ipif_id > id)
13293 				break; /* found correct location */
13294 			tipifp = &(tipif->ipif_next);
13295 		}
13296 	} else {
13297 		if (acquire_ill_lock)
13298 			mutex_exit(&ill->ill_lock);
13299 		if (acquire_g_lock)
13300 			rw_exit(&ipst->ips_ill_g_lock);
13301 		return (-1);
13302 	}
13303 
13304 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13305 
13306 	ipif->ipif_next = tipif;
13307 	*tipifp = ipif;
13308 	if (acquire_ill_lock)
13309 		mutex_exit(&ill->ill_lock);
13310 	if (acquire_g_lock)
13311 		rw_exit(&ipst->ips_ill_g_lock);
13312 	return (0);
13313 }
13314 
13315 static void
13316 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13317 {
13318 	ipif_t	**ipifp;
13319 	ill_t	*ill = ipif->ipif_ill;
13320 
13321 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13322 	if (acquire_ill_lock)
13323 		mutex_enter(&ill->ill_lock);
13324 	else
13325 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13326 
13327 	ipifp = &ill->ill_ipif;
13328 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13329 		if (*ipifp == ipif) {
13330 			*ipifp = ipif->ipif_next;
13331 			break;
13332 		}
13333 	}
13334 
13335 	if (acquire_ill_lock)
13336 		mutex_exit(&ill->ill_lock);
13337 }
13338 
13339 /*
13340  * Allocate and initialize a new interface control structure.  (Always
13341  * called as writer.)
13342  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13343  * is not part of the global linked list of ills. ipif_seqid is unique
13344  * in the system and to preserve the uniqueness, it is assigned only
13345  * when ill becomes part of the global list. At that point ill will
13346  * have a name. If it doesn't get assigned here, it will get assigned
13347  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13348  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13349  * the interface flags or any other information from the DL_INFO_ACK for
13350  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13351  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13352  * second DL_INFO_ACK comes in from the driver.
13353  */
13354 static ipif_t *
13355 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13356 {
13357 	ipif_t	*ipif;
13358 	phyint_t *phyi;
13359 
13360 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13361 	    ill->ill_name, id, (void *)ill));
13362 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13363 
13364 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13365 		return (NULL);
13366 	*ipif = ipif_zero;	/* start clean */
13367 
13368 	ipif->ipif_ill = ill;
13369 	ipif->ipif_id = id;	/* could be -1 */
13370 	/*
13371 	 * Inherit the zoneid from the ill; for the shared stack instance
13372 	 * this is always the global zone
13373 	 */
13374 	ipif->ipif_zoneid = ill->ill_zoneid;
13375 
13376 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13377 
13378 	ipif->ipif_refcnt = 0;
13379 	ipif->ipif_saved_ire_cnt = 0;
13380 
13381 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13382 		mi_free(ipif);
13383 		return (NULL);
13384 	}
13385 	/* -1 id should have been replaced by real id */
13386 	id = ipif->ipif_id;
13387 	ASSERT(id >= 0);
13388 
13389 	if (ill->ill_name[0] != '\0')
13390 		ipif_assign_seqid(ipif);
13391 
13392 	/*
13393 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13394 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13395 	 * ioctl sets ipif_orig_ipifid to zero.
13396 	 */
13397 	ipif->ipif_orig_ipifid = id;
13398 
13399 	/*
13400 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13401 	 * The ipif is still not up and can't be looked up until the
13402 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13403 	 */
13404 	mutex_enter(&ill->ill_lock);
13405 	mutex_enter(&ill->ill_phyint->phyint_lock);
13406 	/*
13407 	 * Set the running flag when logical interface zero is created.
13408 	 * For subsequent logical interfaces, a DLPI link down
13409 	 * notification message may have cleared the running flag to
13410 	 * indicate the link is down, so we shouldn't just blindly set it.
13411 	 */
13412 	if (id == 0)
13413 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13414 	ipif->ipif_ire_type = ire_type;
13415 	phyi = ill->ill_phyint;
13416 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13417 
13418 	if (ipif->ipif_isv6) {
13419 		ill->ill_flags |= ILLF_IPV6;
13420 	} else {
13421 		ipaddr_t inaddr_any = INADDR_ANY;
13422 
13423 		ill->ill_flags |= ILLF_IPV4;
13424 
13425 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13426 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13427 		    &ipif->ipif_v6lcl_addr);
13428 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13429 		    &ipif->ipif_v6src_addr);
13430 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13431 		    &ipif->ipif_v6subnet);
13432 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13433 		    &ipif->ipif_v6net_mask);
13434 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13435 		    &ipif->ipif_v6brd_addr);
13436 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13437 		    &ipif->ipif_v6pp_dst_addr);
13438 	}
13439 
13440 	/*
13441 	 * Don't set the interface flags etc. now, will do it in
13442 	 * ip_ll_subnet_defaults.
13443 	 */
13444 	if (!initialize) {
13445 		mutex_exit(&ill->ill_lock);
13446 		mutex_exit(&ill->ill_phyint->phyint_lock);
13447 		return (ipif);
13448 	}
13449 	ipif->ipif_mtu = ill->ill_max_mtu;
13450 
13451 	if (ill->ill_bcast_addr_length != 0) {
13452 		/*
13453 		 * Later detect lack of DLPI driver multicast
13454 		 * capability by catching DL_ENABMULTI errors in
13455 		 * ip_rput_dlpi.
13456 		 */
13457 		ill->ill_flags |= ILLF_MULTICAST;
13458 		if (!ipif->ipif_isv6)
13459 			ipif->ipif_flags |= IPIF_BROADCAST;
13460 	} else {
13461 		if (ill->ill_net_type != IRE_LOOPBACK) {
13462 			if (ipif->ipif_isv6)
13463 				/*
13464 				 * Note: xresolv interfaces will eventually need
13465 				 * NOARP set here as well, but that will require
13466 				 * those external resolvers to have some
13467 				 * knowledge of that flag and act appropriately.
13468 				 * Not to be changed at present.
13469 				 */
13470 				ill->ill_flags |= ILLF_NONUD;
13471 			else
13472 				ill->ill_flags |= ILLF_NOARP;
13473 		}
13474 		if (ill->ill_phys_addr_length == 0) {
13475 			if (ill->ill_media &&
13476 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13477 				ipif->ipif_flags |= IPIF_NOXMIT;
13478 				phyi->phyint_flags |= PHYI_VIRTUAL;
13479 			} else {
13480 				/* pt-pt supports multicast. */
13481 				ill->ill_flags |= ILLF_MULTICAST;
13482 				if (ill->ill_net_type == IRE_LOOPBACK) {
13483 					phyi->phyint_flags |=
13484 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13485 				} else {
13486 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13487 				}
13488 			}
13489 		}
13490 	}
13491 	mutex_exit(&ill->ill_lock);
13492 	mutex_exit(&ill->ill_phyint->phyint_lock);
13493 	return (ipif);
13494 }
13495 
13496 /*
13497  * If appropriate, send a message up to the resolver delete the entry
13498  * for the address of this interface which is going out of business.
13499  * (Always called as writer).
13500  *
13501  * NOTE : We need to check for NULL mps as some of the fields are
13502  *	  initialized only for some interface types. See ipif_resolver_up()
13503  *	  for details.
13504  */
13505 void
13506 ipif_arp_down(ipif_t *ipif)
13507 {
13508 	mblk_t	*mp;
13509 	ill_t	*ill = ipif->ipif_ill;
13510 
13511 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13512 	ASSERT(IAM_WRITER_IPIF(ipif));
13513 
13514 	/* Delete the mapping for the local address */
13515 	mp = ipif->ipif_arp_del_mp;
13516 	if (mp != NULL) {
13517 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13518 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13519 		putnext(ill->ill_rq, mp);
13520 		ipif->ipif_arp_del_mp = NULL;
13521 	}
13522 
13523 	/*
13524 	 * If this is the last ipif that is going down and there are no
13525 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13526 	 * clean up ARP completely.
13527 	 */
13528 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13529 
13530 		/* Send up AR_INTERFACE_DOWN message */
13531 		mp = ill->ill_arp_down_mp;
13532 		if (mp != NULL) {
13533 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13534 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13535 			    ipif->ipif_id));
13536 			putnext(ill->ill_rq, mp);
13537 			ill->ill_arp_down_mp = NULL;
13538 		}
13539 
13540 		/* Tell ARP to delete the multicast mappings */
13541 		mp = ill->ill_arp_del_mapping_mp;
13542 		if (mp != NULL) {
13543 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13544 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13545 			    ipif->ipif_id));
13546 			putnext(ill->ill_rq, mp);
13547 			ill->ill_arp_del_mapping_mp = NULL;
13548 		}
13549 	}
13550 }
13551 
13552 /*
13553  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13554  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13555  * that it wants the add_mp allocated in this function to be returned
13556  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13557  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13558  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13559  * as it does a ipif_arp_down after calling this function - which will
13560  * remove what we add here.
13561  *
13562  * Returns -1 on failures and 0 on success.
13563  */
13564 int
13565 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13566 {
13567 	mblk_t	*del_mp = NULL;
13568 	mblk_t *add_mp = NULL;
13569 	mblk_t *mp;
13570 	ill_t	*ill = ipif->ipif_ill;
13571 	phyint_t *phyi = ill->ill_phyint;
13572 	ipaddr_t addr, mask, extract_mask = 0;
13573 	arma_t	*arma;
13574 	uint8_t *maddr, *bphys_addr;
13575 	uint32_t hw_start;
13576 	dl_unitdata_req_t *dlur;
13577 
13578 	ASSERT(IAM_WRITER_IPIF(ipif));
13579 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13580 		return (0);
13581 
13582 	/*
13583 	 * Delete the existing mapping from ARP. Normally ipif_down
13584 	 * -> ipif_arp_down should send this up to ARP. The only
13585 	 * reason we would find this when we are switching from
13586 	 * Multicast to Broadcast where we did not do a down.
13587 	 */
13588 	mp = ill->ill_arp_del_mapping_mp;
13589 	if (mp != NULL) {
13590 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13591 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13592 		putnext(ill->ill_rq, mp);
13593 		ill->ill_arp_del_mapping_mp = NULL;
13594 	}
13595 
13596 	if (arp_add_mapping_mp != NULL)
13597 		*arp_add_mapping_mp = NULL;
13598 
13599 	/*
13600 	 * Check that the address is not to long for the constant
13601 	 * length reserved in the template arma_t.
13602 	 */
13603 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13604 		return (-1);
13605 
13606 	/* Add mapping mblk */
13607 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13608 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13609 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13610 	    (caddr_t)&addr);
13611 	if (add_mp == NULL)
13612 		return (-1);
13613 	arma = (arma_t *)add_mp->b_rptr;
13614 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13615 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13616 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13617 
13618 	/*
13619 	 * Determine the broadcast address.
13620 	 */
13621 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13622 	if (ill->ill_sap_length < 0)
13623 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13624 	else
13625 		bphys_addr = (uchar_t *)dlur +
13626 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13627 	/*
13628 	 * Check PHYI_MULTI_BCAST and length of physical
13629 	 * address to determine if we use the mapping or the
13630 	 * broadcast address.
13631 	 */
13632 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13633 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13634 		    bphys_addr, maddr, &hw_start, &extract_mask))
13635 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13636 
13637 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13638 	    (ill->ill_flags & ILLF_MULTICAST)) {
13639 		/* Make sure this will not match the "exact" entry. */
13640 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13641 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13642 		    (caddr_t)&addr);
13643 		if (del_mp == NULL) {
13644 			freemsg(add_mp);
13645 			return (-1);
13646 		}
13647 		bcopy(&extract_mask, (char *)arma +
13648 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13649 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13650 			/* Use link-layer broadcast address for MULTI_BCAST */
13651 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13652 			ip2dbg(("ipif_arp_setup_multicast: adding"
13653 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13654 		} else {
13655 			arma->arma_hw_mapping_start = hw_start;
13656 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13657 			    " ARP setup for %s\n", ill->ill_name));
13658 		}
13659 	} else {
13660 		freemsg(add_mp);
13661 		ASSERT(del_mp == NULL);
13662 		/* It is neither MULTICAST nor MULTI_BCAST */
13663 		return (0);
13664 	}
13665 	ASSERT(add_mp != NULL && del_mp != NULL);
13666 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13667 	ill->ill_arp_del_mapping_mp = del_mp;
13668 	if (arp_add_mapping_mp != NULL) {
13669 		/* The caller just wants the mblks allocated */
13670 		*arp_add_mapping_mp = add_mp;
13671 	} else {
13672 		/* The caller wants us to send it to arp */
13673 		putnext(ill->ill_rq, add_mp);
13674 	}
13675 	return (0);
13676 }
13677 
13678 /*
13679  * Get the resolver set up for a new interface address.
13680  * (Always called as writer.)
13681  * Called both for IPv4 and IPv6 interfaces,
13682  * though it only sets up the resolver for v6
13683  * if it's an xresolv interface (one using an external resolver).
13684  * Honors ILLF_NOARP.
13685  * The enumerated value res_act is used to tune the behavior.
13686  * If set to Res_act_initial, then we set up all the resolver
13687  * structures for a new interface.  If set to Res_act_move, then
13688  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13689  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13690  * asynchronous hardware address change notification.  If set to
13691  * Res_act_defend, then we tell ARP that it needs to send a single
13692  * gratuitous message in defense of the address.
13693  * Returns error on failure.
13694  */
13695 int
13696 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13697 {
13698 	caddr_t	addr;
13699 	mblk_t	*arp_up_mp = NULL;
13700 	mblk_t	*arp_down_mp = NULL;
13701 	mblk_t	*arp_add_mp = NULL;
13702 	mblk_t	*arp_del_mp = NULL;
13703 	mblk_t	*arp_add_mapping_mp = NULL;
13704 	mblk_t	*arp_del_mapping_mp = NULL;
13705 	ill_t	*ill = ipif->ipif_ill;
13706 	uchar_t	*area_p = NULL;
13707 	uchar_t	*ared_p = NULL;
13708 	int	err = ENOMEM;
13709 	boolean_t was_dup;
13710 
13711 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13712 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13713 	ASSERT(IAM_WRITER_IPIF(ipif));
13714 
13715 	was_dup = B_FALSE;
13716 	if (res_act == Res_act_initial) {
13717 		ipif->ipif_addr_ready = 0;
13718 		/*
13719 		 * We're bringing an interface up here.  There's no way that we
13720 		 * should need to shut down ARP now.
13721 		 */
13722 		mutex_enter(&ill->ill_lock);
13723 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13724 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13725 			ill->ill_ipif_dup_count--;
13726 			was_dup = B_TRUE;
13727 		}
13728 		mutex_exit(&ill->ill_lock);
13729 	}
13730 	if (ipif->ipif_recovery_id != 0)
13731 		(void) untimeout(ipif->ipif_recovery_id);
13732 	ipif->ipif_recovery_id = 0;
13733 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13734 		ipif->ipif_addr_ready = 1;
13735 		return (0);
13736 	}
13737 	/* NDP will set the ipif_addr_ready flag when it's ready */
13738 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13739 		return (0);
13740 
13741 	if (ill->ill_isv6) {
13742 		/*
13743 		 * External resolver for IPv6
13744 		 */
13745 		ASSERT(res_act == Res_act_initial);
13746 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13747 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13748 			area_p = (uchar_t *)&ip6_area_template;
13749 			ared_p = (uchar_t *)&ip6_ared_template;
13750 		}
13751 	} else {
13752 		/*
13753 		 * IPv4 arp case. If the ARP stream has already started
13754 		 * closing, fail this request for ARP bringup. Else
13755 		 * record the fact that an ARP bringup is pending.
13756 		 */
13757 		mutex_enter(&ill->ill_lock);
13758 		if (ill->ill_arp_closing) {
13759 			mutex_exit(&ill->ill_lock);
13760 			err = EINVAL;
13761 			goto failed;
13762 		} else {
13763 			if (ill->ill_ipif_up_count == 0 &&
13764 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13765 				ill->ill_arp_bringup_pending = 1;
13766 			mutex_exit(&ill->ill_lock);
13767 		}
13768 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13769 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13770 			area_p = (uchar_t *)&ip_area_template;
13771 			ared_p = (uchar_t *)&ip_ared_template;
13772 		}
13773 	}
13774 
13775 	/*
13776 	 * Add an entry for the local address in ARP only if it
13777 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13778 	 */
13779 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13780 		area_t *area;
13781 
13782 		/* Now ask ARP to publish our address. */
13783 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13784 		if (arp_add_mp == NULL)
13785 			goto failed;
13786 		area = (area_t *)arp_add_mp->b_rptr;
13787 		if (res_act != Res_act_initial) {
13788 			/*
13789 			 * Copy the new hardware address and length into
13790 			 * arp_add_mp to be sent to ARP.
13791 			 */
13792 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13793 			bcopy(ill->ill_phys_addr,
13794 			    ((char *)area + area->area_hw_addr_offset),
13795 			    area->area_hw_addr_length);
13796 		}
13797 
13798 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13799 		    ACE_F_MYADDR;
13800 
13801 		if (res_act == Res_act_defend) {
13802 			area->area_flags |= ACE_F_DEFEND;
13803 			/*
13804 			 * If we're just defending our address now, then
13805 			 * there's no need to set up ARP multicast mappings.
13806 			 * The publish command is enough.
13807 			 */
13808 			goto done;
13809 		}
13810 
13811 		if (res_act != Res_act_initial)
13812 			goto arp_setup_multicast;
13813 
13814 		/*
13815 		 * Allocate an ARP deletion message so we know we can tell ARP
13816 		 * when the interface goes down.
13817 		 */
13818 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13819 		if (arp_del_mp == NULL)
13820 			goto failed;
13821 
13822 	} else {
13823 		if (res_act != Res_act_initial)
13824 			goto done;
13825 	}
13826 	/*
13827 	 * Need to bring up ARP or setup multicast mapping only
13828 	 * when the first interface is coming UP.
13829 	 */
13830 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13831 	    was_dup) {
13832 		goto done;
13833 	}
13834 
13835 	/*
13836 	 * Allocate an ARP down message (to be saved) and an ARP up
13837 	 * message.
13838 	 */
13839 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13840 	if (arp_down_mp == NULL)
13841 		goto failed;
13842 
13843 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13844 	if (arp_up_mp == NULL)
13845 		goto failed;
13846 
13847 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13848 		goto done;
13849 
13850 arp_setup_multicast:
13851 	/*
13852 	 * Setup the multicast mappings. This function initializes
13853 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13854 	 * IPv6.
13855 	 */
13856 	if (!ill->ill_isv6) {
13857 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13858 		if (err != 0)
13859 			goto failed;
13860 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13861 		ASSERT(arp_add_mapping_mp != NULL);
13862 	}
13863 
13864 done:
13865 	if (arp_del_mp != NULL) {
13866 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13867 		ipif->ipif_arp_del_mp = arp_del_mp;
13868 	}
13869 	if (arp_down_mp != NULL) {
13870 		ASSERT(ill->ill_arp_down_mp == NULL);
13871 		ill->ill_arp_down_mp = arp_down_mp;
13872 	}
13873 	if (arp_del_mapping_mp != NULL) {
13874 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13875 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13876 	}
13877 	if (arp_up_mp != NULL) {
13878 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13879 		    ill->ill_name, ipif->ipif_id));
13880 		putnext(ill->ill_rq, arp_up_mp);
13881 	}
13882 	if (arp_add_mp != NULL) {
13883 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13884 		    ill->ill_name, ipif->ipif_id));
13885 		/*
13886 		 * If it's an extended ARP implementation, then we'll wait to
13887 		 * hear that DAD has finished before using the interface.
13888 		 */
13889 		if (!ill->ill_arp_extend)
13890 			ipif->ipif_addr_ready = 1;
13891 		putnext(ill->ill_rq, arp_add_mp);
13892 	} else {
13893 		ipif->ipif_addr_ready = 1;
13894 	}
13895 	if (arp_add_mapping_mp != NULL) {
13896 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13897 		    ill->ill_name, ipif->ipif_id));
13898 		putnext(ill->ill_rq, arp_add_mapping_mp);
13899 	}
13900 	if (res_act != Res_act_initial)
13901 		return (0);
13902 
13903 	if (ill->ill_flags & ILLF_NOARP)
13904 		err = ill_arp_off(ill);
13905 	else
13906 		err = ill_arp_on(ill);
13907 	if (err != 0) {
13908 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13909 		freemsg(ipif->ipif_arp_del_mp);
13910 		freemsg(ill->ill_arp_down_mp);
13911 		freemsg(ill->ill_arp_del_mapping_mp);
13912 		ipif->ipif_arp_del_mp = NULL;
13913 		ill->ill_arp_down_mp = NULL;
13914 		ill->ill_arp_del_mapping_mp = NULL;
13915 		return (err);
13916 	}
13917 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13918 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13919 
13920 failed:
13921 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13922 	freemsg(arp_add_mp);
13923 	freemsg(arp_del_mp);
13924 	freemsg(arp_add_mapping_mp);
13925 	freemsg(arp_up_mp);
13926 	freemsg(arp_down_mp);
13927 	ill->ill_arp_bringup_pending = 0;
13928 	return (err);
13929 }
13930 
13931 /*
13932  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13933  * just gone back up.
13934  */
13935 static void
13936 ipif_arp_start_dad(ipif_t *ipif)
13937 {
13938 	ill_t *ill = ipif->ipif_ill;
13939 	mblk_t *arp_add_mp;
13940 	area_t *area;
13941 
13942 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13943 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13944 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13945 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13946 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13947 		/*
13948 		 * If we can't contact ARP for some reason, that's not really a
13949 		 * problem.  Just send out the routing socket notification that
13950 		 * DAD completion would have done, and continue.
13951 		 */
13952 		ipif_mask_reply(ipif);
13953 		ip_rts_ifmsg(ipif);
13954 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13955 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13956 		ipif->ipif_addr_ready = 1;
13957 		return;
13958 	}
13959 
13960 	/* Setting the 'unverified' flag restarts DAD */
13961 	area = (area_t *)arp_add_mp->b_rptr;
13962 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13963 	    ACE_F_UNVERIFIED;
13964 	putnext(ill->ill_rq, arp_add_mp);
13965 }
13966 
13967 static void
13968 ipif_ndp_start_dad(ipif_t *ipif)
13969 {
13970 	nce_t *nce;
13971 
13972 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13973 	if (nce == NULL)
13974 		return;
13975 
13976 	if (!ndp_restart_dad(nce)) {
13977 		/*
13978 		 * If we can't restart DAD for some reason, that's not really a
13979 		 * problem.  Just send out the routing socket notification that
13980 		 * DAD completion would have done, and continue.
13981 		 */
13982 		ip_rts_ifmsg(ipif);
13983 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13984 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13985 		ipif->ipif_addr_ready = 1;
13986 	}
13987 	NCE_REFRELE(nce);
13988 }
13989 
13990 /*
13991  * Restart duplicate address detection on all interfaces on the given ill.
13992  *
13993  * This is called when an interface transitions from down to up
13994  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13995  *
13996  * Note that since the underlying physical link has transitioned, we must cause
13997  * at least one routing socket message to be sent here, either via DAD
13998  * completion or just by default on the first ipif.  (If we don't do this, then
13999  * in.mpathd will see long delays when doing link-based failure recovery.)
14000  */
14001 void
14002 ill_restart_dad(ill_t *ill, boolean_t went_up)
14003 {
14004 	ipif_t *ipif;
14005 
14006 	if (ill == NULL)
14007 		return;
14008 
14009 	/*
14010 	 * If layer two doesn't support duplicate address detection, then just
14011 	 * send the routing socket message now and be done with it.
14012 	 */
14013 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14014 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14015 		ip_rts_ifmsg(ill->ill_ipif);
14016 		return;
14017 	}
14018 
14019 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14020 		if (went_up) {
14021 			if (ipif->ipif_flags & IPIF_UP) {
14022 				if (ill->ill_isv6)
14023 					ipif_ndp_start_dad(ipif);
14024 				else
14025 					ipif_arp_start_dad(ipif);
14026 			} else if (ill->ill_isv6 &&
14027 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14028 				/*
14029 				 * For IPv4, the ARP module itself will
14030 				 * automatically start the DAD process when it
14031 				 * sees DL_NOTE_LINK_UP.  We respond to the
14032 				 * AR_CN_READY at the completion of that task.
14033 				 * For IPv6, we must kick off the bring-up
14034 				 * process now.
14035 				 */
14036 				ndp_do_recovery(ipif);
14037 			} else {
14038 				/*
14039 				 * Unfortunately, the first ipif is "special"
14040 				 * and represents the underlying ill in the
14041 				 * routing socket messages.  Thus, when this
14042 				 * one ipif is down, we must still notify so
14043 				 * that the user knows the IFF_RUNNING status
14044 				 * change.  (If the first ipif is up, then
14045 				 * we'll handle eventual routing socket
14046 				 * notification via DAD completion.)
14047 				 */
14048 				if (ipif == ill->ill_ipif)
14049 					ip_rts_ifmsg(ill->ill_ipif);
14050 			}
14051 		} else {
14052 			/*
14053 			 * After link down, we'll need to send a new routing
14054 			 * message when the link comes back, so clear
14055 			 * ipif_addr_ready.
14056 			 */
14057 			ipif->ipif_addr_ready = 0;
14058 		}
14059 	}
14060 
14061 	/*
14062 	 * If we've torn down links, then notify the user right away.
14063 	 */
14064 	if (!went_up)
14065 		ip_rts_ifmsg(ill->ill_ipif);
14066 }
14067 
14068 /*
14069  * Wakeup all threads waiting to enter the ipsq, and sleeping
14070  * on any of the ills in this ipsq. The ill_lock of the ill
14071  * must be held so that waiters don't miss wakeups
14072  */
14073 static void
14074 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14075 {
14076 	phyint_t *phyint;
14077 
14078 	phyint = ipsq->ipsq_phyint_list;
14079 	while (phyint != NULL) {
14080 		if (phyint->phyint_illv4) {
14081 			if (!caller_holds_lock)
14082 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14083 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14084 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14085 			if (!caller_holds_lock)
14086 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14087 		}
14088 		if (phyint->phyint_illv6) {
14089 			if (!caller_holds_lock)
14090 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14091 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14092 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14093 			if (!caller_holds_lock)
14094 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14095 		}
14096 		phyint = phyint->phyint_ipsq_next;
14097 	}
14098 }
14099 
14100 static ipsq_t *
14101 ipsq_create(char *groupname, ip_stack_t *ipst)
14102 {
14103 	ipsq_t	*ipsq;
14104 
14105 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14106 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14107 	if (ipsq == NULL) {
14108 		return (NULL);
14109 	}
14110 
14111 	if (groupname != NULL)
14112 		(void) strcpy(ipsq->ipsq_name, groupname);
14113 	else
14114 		ipsq->ipsq_name[0] = '\0';
14115 
14116 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14117 	ipsq->ipsq_flags |= IPSQ_GROUP;
14118 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14119 	ipst->ips_ipsq_g_head = ipsq;
14120 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14121 	return (ipsq);
14122 }
14123 
14124 /*
14125  * Return an ipsq correspoding to the groupname. If 'create' is true
14126  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14127  * uniquely with an IPMP group. However during IPMP groupname operations,
14128  * multiple IPMP groups may be associated with a single ipsq. But no
14129  * IPMP group can be associated with more than 1 ipsq at any time.
14130  * For example
14131  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14132  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14133  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14134  *
14135  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14136  * status shown below during the execution of the above command.
14137  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14138  *
14139  * After the completion of the above groupname command we return to the stable
14140  * state shown below.
14141  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14142  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14143  *
14144  * Because of the above, we don't search based on the ipsq_name since that
14145  * would miss the correct ipsq during certain windows as shown above.
14146  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14147  * natural state.
14148  */
14149 static ipsq_t *
14150 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14151     ip_stack_t *ipst)
14152 {
14153 	ipsq_t	*ipsq;
14154 	int	group_len;
14155 	phyint_t *phyint;
14156 
14157 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14158 
14159 	group_len = strlen(groupname);
14160 	ASSERT(group_len != 0);
14161 	group_len++;
14162 
14163 	for (ipsq = ipst->ips_ipsq_g_head;
14164 	    ipsq != NULL;
14165 	    ipsq = ipsq->ipsq_next) {
14166 		/*
14167 		 * When an ipsq is being split, and ill_split_ipsq
14168 		 * calls this function, we exclude it from being considered.
14169 		 */
14170 		if (ipsq == exclude_ipsq)
14171 			continue;
14172 
14173 		/*
14174 		 * Compare against the ipsq_name. The groupname change happens
14175 		 * in 2 phases. The 1st phase merges the from group into
14176 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14177 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14178 		 * ipsq_name. At this point the phyint_groupname has not been
14179 		 * updated.
14180 		 */
14181 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14182 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14183 			/*
14184 			 * Verify that an ipmp groupname is exactly
14185 			 * part of 1 ipsq and is not found in any other
14186 			 * ipsq.
14187 			 */
14188 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14189 			    NULL);
14190 			return (ipsq);
14191 		}
14192 
14193 		/*
14194 		 * Comparison against ipsq_name alone is not sufficient.
14195 		 * In the case when groups are currently being
14196 		 * merged, the ipsq could hold other IPMP groups temporarily.
14197 		 * so we walk the phyint list and compare against the
14198 		 * phyint_groupname as well.
14199 		 */
14200 		phyint = ipsq->ipsq_phyint_list;
14201 		while (phyint != NULL) {
14202 			if ((group_len == phyint->phyint_groupname_len) &&
14203 			    (bcmp(phyint->phyint_groupname, groupname,
14204 			    group_len) == 0)) {
14205 				/*
14206 				 * Verify that an ipmp groupname is exactly
14207 				 * part of 1 ipsq and is not found in any other
14208 				 * ipsq.
14209 				 */
14210 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14211 				    ipst) == NULL);
14212 				return (ipsq);
14213 			}
14214 			phyint = phyint->phyint_ipsq_next;
14215 		}
14216 	}
14217 	if (create)
14218 		ipsq = ipsq_create(groupname, ipst);
14219 	return (ipsq);
14220 }
14221 
14222 static void
14223 ipsq_delete(ipsq_t *ipsq)
14224 {
14225 	ipsq_t *nipsq;
14226 	ipsq_t *pipsq = NULL;
14227 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14228 
14229 	/*
14230 	 * We don't hold the ipsq lock, but we are sure no new
14231 	 * messages can land up, since the ipsq_refs is zero.
14232 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14233 	 * is associated with this ipsq. (Lookups are based on ill_name
14234 	 * or phyint_groupname)
14235 	 */
14236 	ASSERT(ipsq->ipsq_refs == 0);
14237 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14238 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14239 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14240 		/*
14241 		 * This is not the ipsq of an IPMP group.
14242 		 */
14243 		ipsq->ipsq_ipst = NULL;
14244 		kmem_free(ipsq, sizeof (ipsq_t));
14245 		return;
14246 	}
14247 
14248 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14249 
14250 	/*
14251 	 * Locate the ipsq  before we can remove it from
14252 	 * the singly linked list of ipsq's.
14253 	 */
14254 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14255 	    nipsq = nipsq->ipsq_next) {
14256 		if (nipsq == ipsq) {
14257 			break;
14258 		}
14259 		pipsq = nipsq;
14260 	}
14261 
14262 	ASSERT(nipsq == ipsq);
14263 
14264 	/* unlink ipsq from the list */
14265 	if (pipsq != NULL)
14266 		pipsq->ipsq_next = ipsq->ipsq_next;
14267 	else
14268 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14269 	ipsq->ipsq_ipst = NULL;
14270 	kmem_free(ipsq, sizeof (ipsq_t));
14271 	rw_exit(&ipst->ips_ill_g_lock);
14272 }
14273 
14274 static void
14275 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14276     queue_t *q)
14277 {
14278 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14279 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14280 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14281 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14282 	ASSERT(current_mp != NULL);
14283 
14284 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14285 	    NEW_OP, NULL);
14286 
14287 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14288 	    new_ipsq->ipsq_xopq_mphead != NULL);
14289 
14290 	/*
14291 	 * move from old ipsq to the new ipsq.
14292 	 */
14293 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14294 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14295 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14296 
14297 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14298 }
14299 
14300 void
14301 ill_group_cleanup(ill_t *ill)
14302 {
14303 	ill_t *ill_v4;
14304 	ill_t *ill_v6;
14305 	ipif_t *ipif;
14306 
14307 	ill_v4 = ill->ill_phyint->phyint_illv4;
14308 	ill_v6 = ill->ill_phyint->phyint_illv6;
14309 
14310 	if (ill_v4 != NULL) {
14311 		mutex_enter(&ill_v4->ill_lock);
14312 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14313 		    ipif = ipif->ipif_next) {
14314 			IPIF_UNMARK_MOVING(ipif);
14315 		}
14316 		ill_v4->ill_up_ipifs = B_FALSE;
14317 		mutex_exit(&ill_v4->ill_lock);
14318 	}
14319 
14320 	if (ill_v6 != NULL) {
14321 		mutex_enter(&ill_v6->ill_lock);
14322 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14323 		    ipif = ipif->ipif_next) {
14324 			IPIF_UNMARK_MOVING(ipif);
14325 		}
14326 		ill_v6->ill_up_ipifs = B_FALSE;
14327 		mutex_exit(&ill_v6->ill_lock);
14328 	}
14329 }
14330 /*
14331  * This function is called when an ill has had a change in its group status
14332  * to bring up all the ipifs that were up before the change.
14333  */
14334 int
14335 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14336 {
14337 	ipif_t *ipif;
14338 	ill_t *ill_v4;
14339 	ill_t *ill_v6;
14340 	ill_t *from_ill;
14341 	int err = 0;
14342 
14343 
14344 	ASSERT(IAM_WRITER_ILL(ill));
14345 
14346 	/*
14347 	 * Except for ipif_state_flags and ill_state_flags the other
14348 	 * fields of the ipif/ill that are modified below are protected
14349 	 * implicitly since we are a writer. We would have tried to down
14350 	 * even an ipif that was already down, in ill_down_ipifs. So we
14351 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14352 	 */
14353 	ill_v4 = ill->ill_phyint->phyint_illv4;
14354 	ill_v6 = ill->ill_phyint->phyint_illv6;
14355 	if (ill_v4 != NULL) {
14356 		ill_v4->ill_up_ipifs = B_TRUE;
14357 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14358 		    ipif = ipif->ipif_next) {
14359 			mutex_enter(&ill_v4->ill_lock);
14360 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14361 			IPIF_UNMARK_MOVING(ipif);
14362 			mutex_exit(&ill_v4->ill_lock);
14363 			if (ipif->ipif_was_up) {
14364 				if (!(ipif->ipif_flags & IPIF_UP))
14365 					err = ipif_up(ipif, q, mp);
14366 				ipif->ipif_was_up = B_FALSE;
14367 				if (err != 0) {
14368 					/*
14369 					 * Can there be any other error ?
14370 					 */
14371 					ASSERT(err == EINPROGRESS);
14372 					return (err);
14373 				}
14374 			}
14375 		}
14376 		mutex_enter(&ill_v4->ill_lock);
14377 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14378 		mutex_exit(&ill_v4->ill_lock);
14379 		ill_v4->ill_up_ipifs = B_FALSE;
14380 		if (ill_v4->ill_move_in_progress) {
14381 			ASSERT(ill_v4->ill_move_peer != NULL);
14382 			ill_v4->ill_move_in_progress = B_FALSE;
14383 			from_ill = ill_v4->ill_move_peer;
14384 			from_ill->ill_move_in_progress = B_FALSE;
14385 			from_ill->ill_move_peer = NULL;
14386 			mutex_enter(&from_ill->ill_lock);
14387 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14388 			mutex_exit(&from_ill->ill_lock);
14389 			if (ill_v6 == NULL) {
14390 				if (from_ill->ill_phyint->phyint_flags &
14391 				    PHYI_STANDBY) {
14392 					phyint_inactive(from_ill->ill_phyint);
14393 				}
14394 				if (ill_v4->ill_phyint->phyint_flags &
14395 				    PHYI_STANDBY) {
14396 					phyint_inactive(ill_v4->ill_phyint);
14397 				}
14398 			}
14399 			ill_v4->ill_move_peer = NULL;
14400 		}
14401 	}
14402 
14403 	if (ill_v6 != NULL) {
14404 		ill_v6->ill_up_ipifs = B_TRUE;
14405 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14406 		    ipif = ipif->ipif_next) {
14407 			mutex_enter(&ill_v6->ill_lock);
14408 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14409 			IPIF_UNMARK_MOVING(ipif);
14410 			mutex_exit(&ill_v6->ill_lock);
14411 			if (ipif->ipif_was_up) {
14412 				if (!(ipif->ipif_flags & IPIF_UP))
14413 					err = ipif_up(ipif, q, mp);
14414 				ipif->ipif_was_up = B_FALSE;
14415 				if (err != 0) {
14416 					/*
14417 					 * Can there be any other error ?
14418 					 */
14419 					ASSERT(err == EINPROGRESS);
14420 					return (err);
14421 				}
14422 			}
14423 		}
14424 		mutex_enter(&ill_v6->ill_lock);
14425 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14426 		mutex_exit(&ill_v6->ill_lock);
14427 		ill_v6->ill_up_ipifs = B_FALSE;
14428 		if (ill_v6->ill_move_in_progress) {
14429 			ASSERT(ill_v6->ill_move_peer != NULL);
14430 			ill_v6->ill_move_in_progress = B_FALSE;
14431 			from_ill = ill_v6->ill_move_peer;
14432 			from_ill->ill_move_in_progress = B_FALSE;
14433 			from_ill->ill_move_peer = NULL;
14434 			mutex_enter(&from_ill->ill_lock);
14435 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14436 			mutex_exit(&from_ill->ill_lock);
14437 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14438 				phyint_inactive(from_ill->ill_phyint);
14439 			}
14440 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14441 				phyint_inactive(ill_v6->ill_phyint);
14442 			}
14443 			ill_v6->ill_move_peer = NULL;
14444 		}
14445 	}
14446 	return (0);
14447 }
14448 
14449 /*
14450  * bring down all the approriate ipifs.
14451  */
14452 /* ARGSUSED */
14453 static void
14454 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14455 {
14456 	ipif_t *ipif;
14457 
14458 	ASSERT(IAM_WRITER_ILL(ill));
14459 
14460 	/*
14461 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14462 	 * are modified below are protected implicitly since we are a writer
14463 	 */
14464 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14465 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14466 			continue;
14467 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14468 			/*
14469 			 * We go through the ipif_down logic even if the ipif
14470 			 * is already down, since routes can be added based
14471 			 * on down ipifs. Going through ipif_down once again
14472 			 * will delete any IREs created based on these routes.
14473 			 */
14474 			if (ipif->ipif_flags & IPIF_UP)
14475 				ipif->ipif_was_up = B_TRUE;
14476 			/*
14477 			 * If called with chk_nofailover true ipif is moving.
14478 			 */
14479 			mutex_enter(&ill->ill_lock);
14480 			if (chk_nofailover) {
14481 				ipif->ipif_state_flags |=
14482 				    IPIF_MOVING | IPIF_CHANGING;
14483 			} else {
14484 				ipif->ipif_state_flags |= IPIF_CHANGING;
14485 			}
14486 			mutex_exit(&ill->ill_lock);
14487 			/*
14488 			 * Need to re-create net/subnet bcast ires if
14489 			 * they are dependent on ipif.
14490 			 */
14491 			if (!ipif->ipif_isv6)
14492 				ipif_check_bcast_ires(ipif);
14493 			(void) ipif_logical_down(ipif, NULL, NULL);
14494 			ipif_non_duplicate(ipif);
14495 			ipif_down_tail(ipif);
14496 		}
14497 	}
14498 }
14499 
14500 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14501 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14502 	(ipsq)->ipsq_refs++;				\
14503 }
14504 
14505 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14506 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14507 	(ipsq)->ipsq_refs--;				\
14508 	if ((ipsq)->ipsq_refs == 0)				\
14509 		(ipsq)->ipsq_name[0] = '\0'; 		\
14510 }
14511 
14512 /*
14513  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14514  * new_ipsq.
14515  */
14516 static void
14517 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14518 {
14519 	phyint_t *phyint;
14520 	phyint_t *next_phyint;
14521 
14522 	/*
14523 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14524 	 * writer and the ill_lock of the ill in question. Also the dest
14525 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14526 	 */
14527 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14528 
14529 	phyint = cur_ipsq->ipsq_phyint_list;
14530 	cur_ipsq->ipsq_phyint_list = NULL;
14531 	while (phyint != NULL) {
14532 		next_phyint = phyint->phyint_ipsq_next;
14533 		IPSQ_DEC_REF(cur_ipsq, ipst);
14534 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14535 		new_ipsq->ipsq_phyint_list = phyint;
14536 		IPSQ_INC_REF(new_ipsq, ipst);
14537 		phyint->phyint_ipsq = new_ipsq;
14538 		phyint = next_phyint;
14539 	}
14540 }
14541 
14542 #define	SPLIT_SUCCESS		0
14543 #define	SPLIT_NOT_NEEDED	1
14544 #define	SPLIT_FAILED		2
14545 
14546 int
14547 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14548     ip_stack_t *ipst)
14549 {
14550 	ipsq_t *newipsq = NULL;
14551 
14552 	/*
14553 	 * Assertions denote pre-requisites for changing the ipsq of
14554 	 * a phyint
14555 	 */
14556 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14557 	/*
14558 	 * <ill-phyint> assocs can't change while ill_g_lock
14559 	 * is held as writer. See ill_phyint_reinit()
14560 	 */
14561 	ASSERT(phyint->phyint_illv4 == NULL ||
14562 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14563 	ASSERT(phyint->phyint_illv6 == NULL ||
14564 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14565 
14566 	if ((phyint->phyint_groupname_len !=
14567 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14568 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14569 	    phyint->phyint_groupname_len) != 0)) {
14570 		/*
14571 		 * Once we fail in creating a new ipsq due to memory shortage,
14572 		 * don't attempt to create new ipsq again, based on another
14573 		 * phyint, since we want all phyints belonging to an IPMP group
14574 		 * to be in the same ipsq even in the event of mem alloc fails.
14575 		 */
14576 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14577 		    cur_ipsq, ipst);
14578 		if (newipsq == NULL) {
14579 			/* Memory allocation failure */
14580 			return (SPLIT_FAILED);
14581 		} else {
14582 			/* ipsq_refs protected by ill_g_lock (writer) */
14583 			IPSQ_DEC_REF(cur_ipsq, ipst);
14584 			phyint->phyint_ipsq = newipsq;
14585 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14586 			newipsq->ipsq_phyint_list = phyint;
14587 			IPSQ_INC_REF(newipsq, ipst);
14588 			return (SPLIT_SUCCESS);
14589 		}
14590 	}
14591 	return (SPLIT_NOT_NEEDED);
14592 }
14593 
14594 /*
14595  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14596  * to do this split
14597  */
14598 static int
14599 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14600 {
14601 	ipsq_t *newipsq;
14602 
14603 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14604 	/*
14605 	 * <ill-phyint> assocs can't change while ill_g_lock
14606 	 * is held as writer. See ill_phyint_reinit()
14607 	 */
14608 
14609 	ASSERT(phyint->phyint_illv4 == NULL ||
14610 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14611 	ASSERT(phyint->phyint_illv6 == NULL ||
14612 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14613 
14614 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14615 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14616 		/*
14617 		 * ipsq_init failed due to no memory
14618 		 * caller will use the same ipsq
14619 		 */
14620 		return (SPLIT_FAILED);
14621 	}
14622 
14623 	/* ipsq_ref is protected by ill_g_lock (writer) */
14624 	IPSQ_DEC_REF(cur_ipsq, ipst);
14625 
14626 	/*
14627 	 * This is a new ipsq that is unknown to the world.
14628 	 * So we don't need to hold ipsq_lock,
14629 	 */
14630 	newipsq = phyint->phyint_ipsq;
14631 	newipsq->ipsq_writer = NULL;
14632 	newipsq->ipsq_reentry_cnt--;
14633 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14634 #ifdef DEBUG
14635 	newipsq->ipsq_depth = 0;
14636 #endif
14637 
14638 	return (SPLIT_SUCCESS);
14639 }
14640 
14641 /*
14642  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14643  * ipsq's representing their individual groups or themselves. Return
14644  * whether split needs to be retried again later.
14645  */
14646 static boolean_t
14647 ill_split_ipsq(ipsq_t *cur_ipsq)
14648 {
14649 	phyint_t *phyint;
14650 	phyint_t *next_phyint;
14651 	int	error;
14652 	boolean_t need_retry = B_FALSE;
14653 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14654 
14655 	phyint = cur_ipsq->ipsq_phyint_list;
14656 	cur_ipsq->ipsq_phyint_list = NULL;
14657 	while (phyint != NULL) {
14658 		next_phyint = phyint->phyint_ipsq_next;
14659 		/*
14660 		 * 'created' will tell us whether the callee actually
14661 		 * created an ipsq. Lack of memory may force the callee
14662 		 * to return without creating an ipsq.
14663 		 */
14664 		if (phyint->phyint_groupname == NULL) {
14665 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14666 		} else {
14667 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14668 			    need_retry, ipst);
14669 		}
14670 
14671 		switch (error) {
14672 		case SPLIT_FAILED:
14673 			need_retry = B_TRUE;
14674 			/* FALLTHRU */
14675 		case SPLIT_NOT_NEEDED:
14676 			/*
14677 			 * Keep it on the list.
14678 			 */
14679 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14680 			cur_ipsq->ipsq_phyint_list = phyint;
14681 			break;
14682 		case SPLIT_SUCCESS:
14683 			break;
14684 		default:
14685 			ASSERT(0);
14686 		}
14687 
14688 		phyint = next_phyint;
14689 	}
14690 	return (need_retry);
14691 }
14692 
14693 /*
14694  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14695  * and return the ills in the list. This list will be
14696  * needed to unlock all the ills later on by the caller.
14697  * The <ill-ipsq> associations could change between the
14698  * lock and unlock. Hence the unlock can't traverse the
14699  * ipsq to get the list of ills.
14700  */
14701 static int
14702 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14703 {
14704 	int	cnt = 0;
14705 	phyint_t	*phyint;
14706 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14707 
14708 	/*
14709 	 * The caller holds ill_g_lock to ensure that the ill memberships
14710 	 * of the ipsq don't change
14711 	 */
14712 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14713 
14714 	phyint = ipsq->ipsq_phyint_list;
14715 	while (phyint != NULL) {
14716 		if (phyint->phyint_illv4 != NULL) {
14717 			ASSERT(cnt < list_max);
14718 			list[cnt++] = phyint->phyint_illv4;
14719 		}
14720 		if (phyint->phyint_illv6 != NULL) {
14721 			ASSERT(cnt < list_max);
14722 			list[cnt++] = phyint->phyint_illv6;
14723 		}
14724 		phyint = phyint->phyint_ipsq_next;
14725 	}
14726 	ill_lock_ills(list, cnt);
14727 	return (cnt);
14728 }
14729 
14730 void
14731 ill_lock_ills(ill_t **list, int cnt)
14732 {
14733 	int	i;
14734 
14735 	if (cnt > 1) {
14736 		boolean_t try_again;
14737 		do {
14738 			try_again = B_FALSE;
14739 			for (i = 0; i < cnt - 1; i++) {
14740 				if (list[i] < list[i + 1]) {
14741 					ill_t	*tmp;
14742 
14743 					/* swap the elements */
14744 					tmp = list[i];
14745 					list[i] = list[i + 1];
14746 					list[i + 1] = tmp;
14747 					try_again = B_TRUE;
14748 				}
14749 			}
14750 		} while (try_again);
14751 	}
14752 
14753 	for (i = 0; i < cnt; i++) {
14754 		if (i == 0) {
14755 			if (list[i] != NULL)
14756 				mutex_enter(&list[i]->ill_lock);
14757 			else
14758 				return;
14759 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14760 			mutex_enter(&list[i]->ill_lock);
14761 		}
14762 	}
14763 }
14764 
14765 void
14766 ill_unlock_ills(ill_t **list, int cnt)
14767 {
14768 	int	i;
14769 
14770 	for (i = 0; i < cnt; i++) {
14771 		if ((i == 0) && (list[i] != NULL)) {
14772 			mutex_exit(&list[i]->ill_lock);
14773 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14774 			mutex_exit(&list[i]->ill_lock);
14775 		}
14776 	}
14777 }
14778 
14779 /*
14780  * Merge all the ills from 1 ipsq group into another ipsq group.
14781  * The source ipsq group is specified by the ipsq associated with
14782  * 'from_ill'. The destination ipsq group is specified by the ipsq
14783  * associated with 'to_ill' or 'groupname' respectively.
14784  * Note that ipsq itself does not have a reference count mechanism
14785  * and functions don't look up an ipsq and pass it around. Instead
14786  * functions pass around an ill or groupname, and the ipsq is looked
14787  * up from the ill or groupname and the required operation performed
14788  * atomically with the lookup on the ipsq.
14789  */
14790 static int
14791 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14792     queue_t *q)
14793 {
14794 	ipsq_t *old_ipsq;
14795 	ipsq_t *new_ipsq;
14796 	ill_t	**ill_list;
14797 	int	cnt;
14798 	size_t	ill_list_size;
14799 	boolean_t became_writer_on_new_sq = B_FALSE;
14800 	ip_stack_t	*ipst = from_ill->ill_ipst;
14801 
14802 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14803 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14804 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14805 
14806 	/*
14807 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14808 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14809 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14810 	 */
14811 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14812 
14813 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14814 	if (groupname != NULL)
14815 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14816 	else {
14817 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14818 	}
14819 
14820 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14821 
14822 	/*
14823 	 * both groups are on the same ipsq.
14824 	 */
14825 	if (old_ipsq == new_ipsq) {
14826 		rw_exit(&ipst->ips_ill_g_lock);
14827 		return (0);
14828 	}
14829 
14830 	cnt = old_ipsq->ipsq_refs << 1;
14831 	ill_list_size = cnt * sizeof (ill_t *);
14832 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14833 	if (ill_list == NULL) {
14834 		rw_exit(&ipst->ips_ill_g_lock);
14835 		return (ENOMEM);
14836 	}
14837 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14838 
14839 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14840 	mutex_enter(&new_ipsq->ipsq_lock);
14841 	if ((new_ipsq->ipsq_writer == NULL &&
14842 	    new_ipsq->ipsq_current_ipif == NULL) ||
14843 	    (new_ipsq->ipsq_writer == curthread)) {
14844 		new_ipsq->ipsq_writer = curthread;
14845 		new_ipsq->ipsq_reentry_cnt++;
14846 		became_writer_on_new_sq = B_TRUE;
14847 	}
14848 
14849 	/*
14850 	 * We are holding ill_g_lock as writer and all the ill locks of
14851 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14852 	 * message can land up on the old ipsq even though we don't hold the
14853 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14854 	 */
14855 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14856 
14857 	/*
14858 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14859 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14860 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14861 	 */
14862 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14863 
14864 	/*
14865 	 * Mark the new ipsq as needing a split since it is currently
14866 	 * being shared by more than 1 IPMP group. The split will
14867 	 * occur at the end of ipsq_exit
14868 	 */
14869 	new_ipsq->ipsq_split = B_TRUE;
14870 
14871 	/* Now release all the locks */
14872 	mutex_exit(&new_ipsq->ipsq_lock);
14873 	ill_unlock_ills(ill_list, cnt);
14874 	rw_exit(&ipst->ips_ill_g_lock);
14875 
14876 	kmem_free(ill_list, ill_list_size);
14877 
14878 	/*
14879 	 * If we succeeded in becoming writer on the new ipsq, then
14880 	 * drain the new ipsq and start processing  all enqueued messages
14881 	 * including the current ioctl we are processing which is either
14882 	 * a set groupname or failover/failback.
14883 	 */
14884 	if (became_writer_on_new_sq)
14885 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14886 
14887 	/*
14888 	 * syncq has been changed and all the messages have been moved.
14889 	 */
14890 	mutex_enter(&old_ipsq->ipsq_lock);
14891 	old_ipsq->ipsq_current_ipif = NULL;
14892 	old_ipsq->ipsq_current_ioctl = 0;
14893 	mutex_exit(&old_ipsq->ipsq_lock);
14894 	return (EINPROGRESS);
14895 }
14896 
14897 /*
14898  * Delete and add the loopback copy and non-loopback copy of
14899  * the BROADCAST ire corresponding to ill and addr. Used to
14900  * group broadcast ires together when ill becomes part of
14901  * a group.
14902  *
14903  * This function is also called when ill is leaving the group
14904  * so that the ires belonging to the group gets re-grouped.
14905  */
14906 static void
14907 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14908 {
14909 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14910 	ire_t **ire_ptpn = &ire_head;
14911 	ip_stack_t	*ipst = ill->ill_ipst;
14912 
14913 	/*
14914 	 * The loopback and non-loopback IREs are inserted in the order in which
14915 	 * they're found, on the basis that they are correctly ordered (loopback
14916 	 * first).
14917 	 */
14918 	for (;;) {
14919 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14920 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14921 		if (ire == NULL)
14922 			break;
14923 
14924 		/*
14925 		 * we are passing in KM_SLEEP because it is not easy to
14926 		 * go back to a sane state in case of memory failure.
14927 		 */
14928 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14929 		ASSERT(nire != NULL);
14930 		bzero(nire, sizeof (ire_t));
14931 		/*
14932 		 * Don't use ire_max_frag directly since we don't
14933 		 * hold on to 'ire' until we add the new ire 'nire' and
14934 		 * we don't want the new ire to have a dangling reference
14935 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14936 		 * be in sync with the ipif_mtu of the associate ipif.
14937 		 * For eg. this happens as a result of SIOCSLIFNAME,
14938 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14939 		 * the driver. A change in ire_max_frag triggered as
14940 		 * as a result of path mtu discovery, or due to an
14941 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14942 		 * route change -mtu command does not apply to broadcast ires.
14943 		 *
14944 		 * XXX We need a recovery strategy here if ire_init fails
14945 		 */
14946 		if (ire_init(nire,
14947 		    (uchar_t *)&ire->ire_addr,
14948 		    (uchar_t *)&ire->ire_mask,
14949 		    (uchar_t *)&ire->ire_src_addr,
14950 		    (uchar_t *)&ire->ire_gateway_addr,
14951 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14952 		    &ire->ire_ipif->ipif_mtu,
14953 		    ire->ire_nce,
14954 		    ire->ire_rfq,
14955 		    ire->ire_stq,
14956 		    ire->ire_type,
14957 		    ire->ire_ipif,
14958 		    ire->ire_cmask,
14959 		    ire->ire_phandle,
14960 		    ire->ire_ihandle,
14961 		    ire->ire_flags,
14962 		    &ire->ire_uinfo,
14963 		    NULL,
14964 		    NULL,
14965 		    ipst) == NULL) {
14966 			cmn_err(CE_PANIC, "ire_init() failed");
14967 		}
14968 		ire_delete(ire);
14969 		ire_refrele(ire);
14970 
14971 		/*
14972 		 * The newly created IREs are inserted at the tail of the list
14973 		 * starting with ire_head. As we've just allocated them no one
14974 		 * knows about them so it's safe.
14975 		 */
14976 		*ire_ptpn = nire;
14977 		ire_ptpn = &nire->ire_next;
14978 	}
14979 
14980 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14981 		int error;
14982 		ire_t *oire;
14983 		/* unlink the IRE from our list before calling ire_add() */
14984 		nire_next = nire->ire_next;
14985 		nire->ire_next = NULL;
14986 
14987 		/* ire_add adds the ire at the right place in the list */
14988 		oire = nire;
14989 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14990 		ASSERT(error == 0);
14991 		ASSERT(oire == nire);
14992 		ire_refrele(nire);	/* Held in ire_add */
14993 	}
14994 }
14995 
14996 /*
14997  * This function is usually called when an ill is inserted in
14998  * a group and all the ipifs are already UP. As all the ipifs
14999  * are already UP, the broadcast ires have already been created
15000  * and been inserted. But, ire_add_v4 would not have grouped properly.
15001  * We need to re-group for the benefit of ip_wput_ire which
15002  * expects BROADCAST ires to be grouped properly to avoid sending
15003  * more than one copy of the broadcast packet per group.
15004  *
15005  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15006  *	  because when ipif_up_done ends up calling this, ires have
15007  *        already been added before illgrp_insert i.e before ill_group
15008  *	  has been initialized.
15009  */
15010 static void
15011 ill_group_bcast_for_xmit(ill_t *ill)
15012 {
15013 	ill_group_t *illgrp;
15014 	ipif_t *ipif;
15015 	ipaddr_t addr;
15016 	ipaddr_t net_mask;
15017 	ipaddr_t subnet_netmask;
15018 
15019 	illgrp = ill->ill_group;
15020 
15021 	/*
15022 	 * This function is called even when an ill is deleted from
15023 	 * the group. Hence, illgrp could be null.
15024 	 */
15025 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15026 		return;
15027 
15028 	/*
15029 	 * Delete all the BROADCAST ires matching this ill and add
15030 	 * them back. This time, ire_add_v4 should take care of
15031 	 * grouping them with others because ill is part of the
15032 	 * group.
15033 	 */
15034 	ill_bcast_delete_and_add(ill, 0);
15035 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15036 
15037 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15038 
15039 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15040 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15041 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15042 		} else {
15043 			net_mask = htonl(IN_CLASSA_NET);
15044 		}
15045 		addr = net_mask & ipif->ipif_subnet;
15046 		ill_bcast_delete_and_add(ill, addr);
15047 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15048 
15049 		subnet_netmask = ipif->ipif_net_mask;
15050 		addr = ipif->ipif_subnet;
15051 		ill_bcast_delete_and_add(ill, addr);
15052 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15053 	}
15054 }
15055 
15056 /*
15057  * This function is called from illgrp_delete when ill is being deleted
15058  * from the group.
15059  *
15060  * As ill is not there in the group anymore, any address belonging
15061  * to this ill should be cleared of IRE_MARK_NORECV.
15062  */
15063 static void
15064 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15065 {
15066 	ire_t *ire;
15067 	irb_t *irb;
15068 	ip_stack_t	*ipst = ill->ill_ipst;
15069 
15070 	ASSERT(ill->ill_group == NULL);
15071 
15072 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15073 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15074 
15075 	if (ire != NULL) {
15076 		/*
15077 		 * IPMP and plumbing operations are serialized on the ipsq, so
15078 		 * no one will insert or delete a broadcast ire under our feet.
15079 		 */
15080 		irb = ire->ire_bucket;
15081 		rw_enter(&irb->irb_lock, RW_READER);
15082 		ire_refrele(ire);
15083 
15084 		for (; ire != NULL; ire = ire->ire_next) {
15085 			if (ire->ire_addr != addr)
15086 				break;
15087 			if (ire_to_ill(ire) != ill)
15088 				continue;
15089 
15090 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15091 			ire->ire_marks &= ~IRE_MARK_NORECV;
15092 		}
15093 		rw_exit(&irb->irb_lock);
15094 	}
15095 }
15096 
15097 /*
15098  * This function must be called only after the broadcast ires
15099  * have been grouped together. For a given address addr, nominate
15100  * only one of the ires whose interface is not FAILED or OFFLINE.
15101  *
15102  * This is also called when an ipif goes down, so that we can nominate
15103  * a different ire with the same address for receiving.
15104  */
15105 static void
15106 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15107 {
15108 	irb_t *irb;
15109 	ire_t *ire;
15110 	ire_t *ire1;
15111 	ire_t *save_ire;
15112 	ire_t **irep = NULL;
15113 	boolean_t first = B_TRUE;
15114 	ire_t *clear_ire = NULL;
15115 	ire_t *start_ire = NULL;
15116 	ire_t	*new_lb_ire;
15117 	ire_t	*new_nlb_ire;
15118 	boolean_t new_lb_ire_used = B_FALSE;
15119 	boolean_t new_nlb_ire_used = B_FALSE;
15120 	uint64_t match_flags;
15121 	uint64_t phyi_flags;
15122 	boolean_t fallback = B_FALSE;
15123 	uint_t	max_frag;
15124 
15125 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15126 	    NULL, MATCH_IRE_TYPE, ipst);
15127 	/*
15128 	 * We may not be able to find some ires if a previous
15129 	 * ire_create failed. This happens when an ipif goes
15130 	 * down and we are unable to create BROADCAST ires due
15131 	 * to memory failure. Thus, we have to check for NULL
15132 	 * below. This should handle the case for LOOPBACK,
15133 	 * POINTOPOINT and interfaces with some POINTOPOINT
15134 	 * logicals for which there are no BROADCAST ires.
15135 	 */
15136 	if (ire == NULL)
15137 		return;
15138 	/*
15139 	 * Currently IRE_BROADCASTS are deleted when an ipif
15140 	 * goes down which runs exclusively. Thus, setting
15141 	 * IRE_MARK_RCVD should not race with ire_delete marking
15142 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15143 	 * be consistent with other parts of the code that walks
15144 	 * a given bucket.
15145 	 */
15146 	save_ire = ire;
15147 	irb = ire->ire_bucket;
15148 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15149 	if (new_lb_ire == NULL) {
15150 		ire_refrele(ire);
15151 		return;
15152 	}
15153 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15154 	if (new_nlb_ire == NULL) {
15155 		ire_refrele(ire);
15156 		kmem_cache_free(ire_cache, new_lb_ire);
15157 		return;
15158 	}
15159 	IRB_REFHOLD(irb);
15160 	rw_enter(&irb->irb_lock, RW_WRITER);
15161 	/*
15162 	 * Get to the first ire matching the address and the
15163 	 * group. If the address does not match we are done
15164 	 * as we could not find the IRE. If the address matches
15165 	 * we should get to the first one matching the group.
15166 	 */
15167 	while (ire != NULL) {
15168 		if (ire->ire_addr != addr ||
15169 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15170 			break;
15171 		}
15172 		ire = ire->ire_next;
15173 	}
15174 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15175 	start_ire = ire;
15176 redo:
15177 	while (ire != NULL && ire->ire_addr == addr &&
15178 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15179 		/*
15180 		 * The first ire for any address within a group
15181 		 * should always be the one with IRE_MARK_NORECV cleared
15182 		 * so that ip_wput_ire can avoid searching for one.
15183 		 * Note down the insertion point which will be used
15184 		 * later.
15185 		 */
15186 		if (first && (irep == NULL))
15187 			irep = ire->ire_ptpn;
15188 		/*
15189 		 * PHYI_FAILED is set when the interface fails.
15190 		 * This interface might have become good, but the
15191 		 * daemon has not yet detected. We should still
15192 		 * not receive on this. PHYI_OFFLINE should never
15193 		 * be picked as this has been offlined and soon
15194 		 * be removed.
15195 		 */
15196 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15197 		if (phyi_flags & PHYI_OFFLINE) {
15198 			ire->ire_marks |= IRE_MARK_NORECV;
15199 			ire = ire->ire_next;
15200 			continue;
15201 		}
15202 		if (phyi_flags & match_flags) {
15203 			ire->ire_marks |= IRE_MARK_NORECV;
15204 			ire = ire->ire_next;
15205 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15206 			    PHYI_INACTIVE) {
15207 				fallback = B_TRUE;
15208 			}
15209 			continue;
15210 		}
15211 		if (first) {
15212 			/*
15213 			 * We will move this to the front of the list later
15214 			 * on.
15215 			 */
15216 			clear_ire = ire;
15217 			ire->ire_marks &= ~IRE_MARK_NORECV;
15218 		} else {
15219 			ire->ire_marks |= IRE_MARK_NORECV;
15220 		}
15221 		first = B_FALSE;
15222 		ire = ire->ire_next;
15223 	}
15224 	/*
15225 	 * If we never nominated anybody, try nominating at least
15226 	 * an INACTIVE, if we found one. Do it only once though.
15227 	 */
15228 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15229 	    fallback) {
15230 		match_flags = PHYI_FAILED;
15231 		ire = start_ire;
15232 		irep = NULL;
15233 		goto redo;
15234 	}
15235 	ire_refrele(save_ire);
15236 
15237 	/*
15238 	 * irep non-NULL indicates that we entered the while loop
15239 	 * above. If clear_ire is at the insertion point, we don't
15240 	 * have to do anything. clear_ire will be NULL if all the
15241 	 * interfaces are failed.
15242 	 *
15243 	 * We cannot unlink and reinsert the ire at the right place
15244 	 * in the list since there can be other walkers of this bucket.
15245 	 * Instead we delete and recreate the ire
15246 	 */
15247 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15248 		ire_t *clear_ire_stq = NULL;
15249 
15250 		bzero(new_lb_ire, sizeof (ire_t));
15251 		/* XXX We need a recovery strategy here. */
15252 		if (ire_init(new_lb_ire,
15253 		    (uchar_t *)&clear_ire->ire_addr,
15254 		    (uchar_t *)&clear_ire->ire_mask,
15255 		    (uchar_t *)&clear_ire->ire_src_addr,
15256 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15257 		    &clear_ire->ire_max_frag,
15258 		    NULL, /* let ire_nce_init derive the resolver info */
15259 		    clear_ire->ire_rfq,
15260 		    clear_ire->ire_stq,
15261 		    clear_ire->ire_type,
15262 		    clear_ire->ire_ipif,
15263 		    clear_ire->ire_cmask,
15264 		    clear_ire->ire_phandle,
15265 		    clear_ire->ire_ihandle,
15266 		    clear_ire->ire_flags,
15267 		    &clear_ire->ire_uinfo,
15268 		    NULL,
15269 		    NULL,
15270 		    ipst) == NULL)
15271 			cmn_err(CE_PANIC, "ire_init() failed");
15272 		if (clear_ire->ire_stq == NULL) {
15273 			ire_t *ire_next = clear_ire->ire_next;
15274 			if (ire_next != NULL &&
15275 			    ire_next->ire_stq != NULL &&
15276 			    ire_next->ire_addr == clear_ire->ire_addr &&
15277 			    ire_next->ire_ipif->ipif_ill ==
15278 			    clear_ire->ire_ipif->ipif_ill) {
15279 				clear_ire_stq = ire_next;
15280 
15281 				bzero(new_nlb_ire, sizeof (ire_t));
15282 				/* XXX We need a recovery strategy here. */
15283 				if (ire_init(new_nlb_ire,
15284 				    (uchar_t *)&clear_ire_stq->ire_addr,
15285 				    (uchar_t *)&clear_ire_stq->ire_mask,
15286 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15287 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15288 				    &clear_ire_stq->ire_max_frag,
15289 				    NULL,
15290 				    clear_ire_stq->ire_rfq,
15291 				    clear_ire_stq->ire_stq,
15292 				    clear_ire_stq->ire_type,
15293 				    clear_ire_stq->ire_ipif,
15294 				    clear_ire_stq->ire_cmask,
15295 				    clear_ire_stq->ire_phandle,
15296 				    clear_ire_stq->ire_ihandle,
15297 				    clear_ire_stq->ire_flags,
15298 				    &clear_ire_stq->ire_uinfo,
15299 				    NULL,
15300 				    NULL,
15301 				    ipst) == NULL)
15302 					cmn_err(CE_PANIC, "ire_init() failed");
15303 			}
15304 		}
15305 
15306 		/*
15307 		 * Delete the ire. We can't call ire_delete() since
15308 		 * we are holding the bucket lock. We can't release the
15309 		 * bucket lock since we can't allow irep to change. So just
15310 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15311 		 * ire from the list and do the refrele.
15312 		 */
15313 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15314 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15315 
15316 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15317 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15318 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15319 		}
15320 
15321 		/*
15322 		 * Also take care of otherfields like ib/ob pkt count
15323 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15324 		 */
15325 
15326 		/* Set the max_frag before adding the ire */
15327 		max_frag = *new_lb_ire->ire_max_fragp;
15328 		new_lb_ire->ire_max_fragp = NULL;
15329 		new_lb_ire->ire_max_frag = max_frag;
15330 
15331 		/* Add the new ire's. Insert at *irep */
15332 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15333 		ire1 = *irep;
15334 		if (ire1 != NULL)
15335 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15336 		new_lb_ire->ire_next = ire1;
15337 		/* Link the new one in. */
15338 		new_lb_ire->ire_ptpn = irep;
15339 		membar_producer();
15340 		*irep = new_lb_ire;
15341 		new_lb_ire_used = B_TRUE;
15342 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15343 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15344 		DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), new_lb_ire->ire_ipif,
15345 		    (char *), "ire", (void *), new_lb_ire);
15346 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15347 
15348 		if (clear_ire_stq != NULL) {
15349 			/* Set the max_frag before adding the ire */
15350 			max_frag = *new_nlb_ire->ire_max_fragp;
15351 			new_nlb_ire->ire_max_fragp = NULL;
15352 			new_nlb_ire->ire_max_frag = max_frag;
15353 
15354 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15355 			irep = &new_lb_ire->ire_next;
15356 			/* Add the new ire. Insert at *irep */
15357 			ire1 = *irep;
15358 			if (ire1 != NULL)
15359 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15360 			new_nlb_ire->ire_next = ire1;
15361 			/* Link the new one in. */
15362 			new_nlb_ire->ire_ptpn = irep;
15363 			membar_producer();
15364 			*irep = new_nlb_ire;
15365 			new_nlb_ire_used = B_TRUE;
15366 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15367 			    ire_stats_inserted);
15368 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15369 			DTRACE_PROBE3(ipif__incr__cnt,
15370 			    (ipif_t *), new_nlb_ire->ire_ipif,
15371 			    (char *), "ire", (void *), new_nlb_ire);
15372 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15373 			DTRACE_PROBE3(ill__incr__cnt,
15374 			    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15375 			    (char *), "ire", (void *), new_nlb_ire);
15376 			((ill_t *)(new_nlb_ire->ire_stq->q_ptr))->ill_ire_cnt++;
15377 		}
15378 	}
15379 	rw_exit(&irb->irb_lock);
15380 	if (!new_lb_ire_used)
15381 		kmem_cache_free(ire_cache, new_lb_ire);
15382 	if (!new_nlb_ire_used)
15383 		kmem_cache_free(ire_cache, new_nlb_ire);
15384 	IRB_REFRELE(irb);
15385 }
15386 
15387 /*
15388  * Whenever an ipif goes down we have to renominate a different
15389  * broadcast ire to receive. Whenever an ipif comes up, we need
15390  * to make sure that we have only one nominated to receive.
15391  */
15392 static void
15393 ipif_renominate_bcast(ipif_t *ipif)
15394 {
15395 	ill_t *ill = ipif->ipif_ill;
15396 	ipaddr_t subnet_addr;
15397 	ipaddr_t net_addr;
15398 	ipaddr_t net_mask = 0;
15399 	ipaddr_t subnet_netmask;
15400 	ipaddr_t addr;
15401 	ill_group_t *illgrp;
15402 	ip_stack_t	*ipst = ill->ill_ipst;
15403 
15404 	illgrp = ill->ill_group;
15405 	/*
15406 	 * If this is the last ipif going down, it might take
15407 	 * the ill out of the group. In that case ipif_down ->
15408 	 * illgrp_delete takes care of doing the nomination.
15409 	 * ipif_down does not call for this case.
15410 	 */
15411 	ASSERT(illgrp != NULL);
15412 
15413 	/* There could not have been any ires associated with this */
15414 	if (ipif->ipif_subnet == 0)
15415 		return;
15416 
15417 	ill_mark_bcast(illgrp, 0, ipst);
15418 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15419 
15420 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15421 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15422 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15423 	} else {
15424 		net_mask = htonl(IN_CLASSA_NET);
15425 	}
15426 	addr = net_mask & ipif->ipif_subnet;
15427 	ill_mark_bcast(illgrp, addr, ipst);
15428 
15429 	net_addr = ~net_mask | addr;
15430 	ill_mark_bcast(illgrp, net_addr, ipst);
15431 
15432 	subnet_netmask = ipif->ipif_net_mask;
15433 	addr = ipif->ipif_subnet;
15434 	ill_mark_bcast(illgrp, addr, ipst);
15435 
15436 	subnet_addr = ~subnet_netmask | addr;
15437 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15438 }
15439 
15440 /*
15441  * Whenever we form or delete ill groups, we need to nominate one set of
15442  * BROADCAST ires for receiving in the group.
15443  *
15444  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15445  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15446  *    for ill_ipif_up_count to be non-zero. This is the only case where
15447  *    ill_ipif_up_count is zero and we would still find the ires.
15448  *
15449  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15450  *    ipif is UP and we just have to do the nomination.
15451  *
15452  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15453  *    from the group. So, we have to do the nomination.
15454  *
15455  * Because of (3), there could be just one ill in the group. But we have
15456  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15457  * Thus, this function does not optimize when there is only one ill as
15458  * it is not correct for (3).
15459  */
15460 static void
15461 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15462 {
15463 	ill_t *ill;
15464 	ipif_t *ipif;
15465 	ipaddr_t subnet_addr;
15466 	ipaddr_t prev_subnet_addr = 0;
15467 	ipaddr_t net_addr;
15468 	ipaddr_t prev_net_addr = 0;
15469 	ipaddr_t net_mask = 0;
15470 	ipaddr_t subnet_netmask;
15471 	ipaddr_t addr;
15472 	ip_stack_t	*ipst;
15473 
15474 	/*
15475 	 * When the last memeber is leaving, there is nothing to
15476 	 * nominate.
15477 	 */
15478 	if (illgrp->illgrp_ill_count == 0) {
15479 		ASSERT(illgrp->illgrp_ill == NULL);
15480 		return;
15481 	}
15482 
15483 	ill = illgrp->illgrp_ill;
15484 	ASSERT(!ill->ill_isv6);
15485 	ipst = ill->ill_ipst;
15486 	/*
15487 	 * We assume that ires with same address and belonging to the
15488 	 * same group, has been grouped together. Nominating a *single*
15489 	 * ill in the group for sending and receiving broadcast is done
15490 	 * by making sure that the first BROADCAST ire (which will be
15491 	 * the one returned by ire_ctable_lookup for ip_rput and the
15492 	 * one that will be used in ip_wput_ire) will be the one that
15493 	 * will not have IRE_MARK_NORECV set.
15494 	 *
15495 	 * 1) ip_rput checks and discards packets received on ires marked
15496 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15497 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15498 	 *    first ire in the group for every broadcast address in the group.
15499 	 *    ip_rput will accept packets only on the first ire i.e only
15500 	 *    one copy of the ill.
15501 	 *
15502 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15503 	 *    packet for the whole group. It needs to send out on the ill
15504 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15505 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15506 	 *    the copy echoed back on other port where the ire is not marked
15507 	 *    with IRE_MARK_NORECV.
15508 	 *
15509 	 * Note that we just need to have the first IRE either loopback or
15510 	 * non-loopback (either of them may not exist if ire_create failed
15511 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15512 	 * always hit the first one and hence will always accept one copy.
15513 	 *
15514 	 * We have a broadcast ire per ill for all the unique prefixes
15515 	 * hosted on that ill. As we don't have a way of knowing the
15516 	 * unique prefixes on a given ill and hence in the whole group,
15517 	 * we just call ill_mark_bcast on all the prefixes that exist
15518 	 * in the group. For the common case of one prefix, the code
15519 	 * below optimizes by remebering the last address used for
15520 	 * markng. In the case of multiple prefixes, this will still
15521 	 * optimize depending the order of prefixes.
15522 	 *
15523 	 * The only unique address across the whole group is 0.0.0.0 and
15524 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15525 	 * the first ire in the bucket for receiving and disables the
15526 	 * others.
15527 	 */
15528 	ill_mark_bcast(illgrp, 0, ipst);
15529 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15530 	for (; ill != NULL; ill = ill->ill_group_next) {
15531 
15532 		for (ipif = ill->ill_ipif; ipif != NULL;
15533 		    ipif = ipif->ipif_next) {
15534 
15535 			if (!(ipif->ipif_flags & IPIF_UP) ||
15536 			    ipif->ipif_subnet == 0) {
15537 				continue;
15538 			}
15539 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15540 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15541 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15542 			} else {
15543 				net_mask = htonl(IN_CLASSA_NET);
15544 			}
15545 			addr = net_mask & ipif->ipif_subnet;
15546 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15547 				ill_mark_bcast(illgrp, addr, ipst);
15548 				net_addr = ~net_mask | addr;
15549 				ill_mark_bcast(illgrp, net_addr, ipst);
15550 			}
15551 			prev_net_addr = addr;
15552 
15553 			subnet_netmask = ipif->ipif_net_mask;
15554 			addr = ipif->ipif_subnet;
15555 			if (prev_subnet_addr == 0 ||
15556 			    prev_subnet_addr != addr) {
15557 				ill_mark_bcast(illgrp, addr, ipst);
15558 				subnet_addr = ~subnet_netmask | addr;
15559 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15560 			}
15561 			prev_subnet_addr = addr;
15562 		}
15563 	}
15564 }
15565 
15566 /*
15567  * This function is called while forming ill groups.
15568  *
15569  * Currently, we handle only allmulti groups. We want to join
15570  * allmulti on only one of the ills in the groups. In future,
15571  * when we have link aggregation, we may have to join normal
15572  * multicast groups on multiple ills as switch does inbound load
15573  * balancing. Following are the functions that calls this
15574  * function :
15575  *
15576  * 1) ill_recover_multicast : Interface is coming back UP.
15577  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15578  *    will call ill_recover_multicast to recover all the multicast
15579  *    groups. We need to make sure that only one member is joined
15580  *    in the ill group.
15581  *
15582  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15583  *    Somebody is joining allmulti. We need to make sure that only one
15584  *    member is joined in the group.
15585  *
15586  * 3) illgrp_insert : If allmulti has already joined, we need to make
15587  *    sure that only one member is joined in the group.
15588  *
15589  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15590  *    allmulti who we have nominated. We need to pick someother ill.
15591  *
15592  * 5) illgrp_delete : The ill we nominated is leaving the group,
15593  *    we need to pick a new ill to join the group.
15594  *
15595  * For (1), (2), (5) - we just have to check whether there is
15596  * a good ill joined in the group. If we could not find any ills
15597  * joined the group, we should join.
15598  *
15599  * For (4), the one that was nominated to receive, left the group.
15600  * There could be nobody joined in the group when this function is
15601  * called.
15602  *
15603  * For (3) - we need to explicitly check whether there are multiple
15604  * ills joined in the group.
15605  *
15606  * For simplicity, we don't differentiate any of the above cases. We
15607  * just leave the group if it is joined on any of them and join on
15608  * the first good ill.
15609  */
15610 int
15611 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15612 {
15613 	ilm_t *ilm;
15614 	ill_t *ill;
15615 	ill_t *fallback_inactive_ill = NULL;
15616 	ill_t *fallback_failed_ill = NULL;
15617 	int ret = 0;
15618 
15619 	/*
15620 	 * Leave the allmulti on all the ills and start fresh.
15621 	 */
15622 	for (ill = illgrp->illgrp_ill; ill != NULL;
15623 	    ill = ill->ill_group_next) {
15624 		if (ill->ill_join_allmulti)
15625 			(void) ip_leave_allmulti(ill->ill_ipif);
15626 	}
15627 
15628 	/*
15629 	 * Choose a good ill. Fallback to inactive or failed if
15630 	 * none available. We need to fallback to FAILED in the
15631 	 * case where we have 2 interfaces in a group - where
15632 	 * one of them is failed and another is a good one and
15633 	 * the good one (not marked inactive) is leaving the group.
15634 	 */
15635 	ret = 0;
15636 	for (ill = illgrp->illgrp_ill; ill != NULL;
15637 	    ill = ill->ill_group_next) {
15638 		/* Never pick an offline interface */
15639 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15640 			continue;
15641 
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 = ip_join_allmulti(ill->ill_ipif);
15653 				/*
15654 				 * ip_join_allmulti can fail because of memory
15655 				 * failures. So, make sure we join at least
15656 				 * 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 				ret = ip_join_allmulti(ill->ill_ipif);
15675 				return (ret);
15676 			}
15677 		}
15678 	} else if ((ill = fallback_failed_ill) != NULL) {
15679 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15680 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15681 				ret = ip_join_allmulti(ill->ill_ipif);
15682 				return (ret);
15683 			}
15684 		}
15685 	}
15686 	return (0);
15687 }
15688 
15689 /*
15690  * This function is called from illgrp_delete after it is
15691  * deleted from the group to reschedule responsibilities
15692  * to a different ill.
15693  */
15694 static void
15695 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15696 {
15697 	ilm_t	*ilm;
15698 	ipif_t	*ipif;
15699 	ipaddr_t subnet_addr;
15700 	ipaddr_t net_addr;
15701 	ipaddr_t net_mask = 0;
15702 	ipaddr_t subnet_netmask;
15703 	ipaddr_t addr;
15704 	ip_stack_t *ipst = ill->ill_ipst;
15705 
15706 	ASSERT(ill->ill_group == NULL);
15707 	/*
15708 	 * Broadcast Responsibility:
15709 	 *
15710 	 * 1. If this ill has been nominated for receiving broadcast
15711 	 * packets, we need to find a new one. Before we find a new
15712 	 * one, we need to re-group the ires that are part of this new
15713 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15714 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15715 	 * thing for us.
15716 	 *
15717 	 * 2. If this ill was not nominated for receiving broadcast
15718 	 * packets, we need to clear the IRE_MARK_NORECV flag
15719 	 * so that we continue to send up broadcast packets.
15720 	 */
15721 	if (!ill->ill_isv6) {
15722 		/*
15723 		 * Case 1 above : No optimization here. Just redo the
15724 		 * nomination.
15725 		 */
15726 		ill_group_bcast_for_xmit(ill);
15727 		ill_nominate_bcast_rcv(illgrp);
15728 
15729 		/*
15730 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15731 		 */
15732 		ill_clear_bcast_mark(ill, 0);
15733 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15734 
15735 		for (ipif = ill->ill_ipif; ipif != NULL;
15736 		    ipif = ipif->ipif_next) {
15737 
15738 			if (!(ipif->ipif_flags & IPIF_UP) ||
15739 			    ipif->ipif_subnet == 0) {
15740 				continue;
15741 			}
15742 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15743 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15744 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15745 			} else {
15746 				net_mask = htonl(IN_CLASSA_NET);
15747 			}
15748 			addr = net_mask & ipif->ipif_subnet;
15749 			ill_clear_bcast_mark(ill, addr);
15750 
15751 			net_addr = ~net_mask | addr;
15752 			ill_clear_bcast_mark(ill, net_addr);
15753 
15754 			subnet_netmask = ipif->ipif_net_mask;
15755 			addr = ipif->ipif_subnet;
15756 			ill_clear_bcast_mark(ill, addr);
15757 
15758 			subnet_addr = ~subnet_netmask | addr;
15759 			ill_clear_bcast_mark(ill, subnet_addr);
15760 		}
15761 	}
15762 
15763 	/*
15764 	 * Multicast Responsibility.
15765 	 *
15766 	 * If we have joined allmulti on this one, find a new member
15767 	 * in the group to join allmulti. As this ill is already part
15768 	 * of allmulti, we don't have to join on this one.
15769 	 *
15770 	 * If we have not joined allmulti on this one, there is no
15771 	 * responsibility to handoff. But we need to take new
15772 	 * responsibility i.e, join allmulti on this one if we need
15773 	 * to.
15774 	 */
15775 	if (ill->ill_join_allmulti) {
15776 		(void) ill_nominate_mcast_rcv(illgrp);
15777 	} else {
15778 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15779 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15780 				(void) ip_join_allmulti(ill->ill_ipif);
15781 				break;
15782 			}
15783 		}
15784 	}
15785 
15786 	/*
15787 	 * We intentionally do the flushing of IRE_CACHES only matching
15788 	 * on the ill and not on groups. Note that we are already deleted
15789 	 * from the group.
15790 	 *
15791 	 * This will make sure that all IRE_CACHES whose stq is pointing
15792 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15793 	 * deleted and IRE_CACHES that are not pointing at this ill will
15794 	 * be left alone.
15795 	 */
15796 	if (ill->ill_isv6) {
15797 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15798 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15799 	} else {
15800 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15801 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15802 	}
15803 
15804 	/*
15805 	 * Some conn may have cached one of the IREs deleted above. By removing
15806 	 * the ire reference, we clean up the extra reference to the ill held in
15807 	 * ire->ire_stq.
15808 	 */
15809 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15810 
15811 	/*
15812 	 * Re-do source address selection for all the members in the
15813 	 * group, if they borrowed source address from one of the ipifs
15814 	 * in this ill.
15815 	 */
15816 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15817 		if (ill->ill_isv6) {
15818 			ipif_update_other_ipifs_v6(ipif, illgrp);
15819 		} else {
15820 			ipif_update_other_ipifs(ipif, illgrp);
15821 		}
15822 	}
15823 }
15824 
15825 /*
15826  * Delete the ill from the group. The caller makes sure that it is
15827  * in a group and it okay to delete from the group. So, we always
15828  * delete here.
15829  */
15830 static void
15831 illgrp_delete(ill_t *ill)
15832 {
15833 	ill_group_t *illgrp;
15834 	ill_group_t *tmpg;
15835 	ill_t *tmp_ill;
15836 	ip_stack_t	*ipst = ill->ill_ipst;
15837 
15838 	/*
15839 	 * Reset illgrp_ill_schednext if it was pointing at us.
15840 	 * We need to do this before we set ill_group to NULL.
15841 	 */
15842 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15843 	mutex_enter(&ill->ill_lock);
15844 
15845 	illgrp_reset_schednext(ill);
15846 
15847 	illgrp = ill->ill_group;
15848 
15849 	/* Delete the ill from illgrp. */
15850 	if (illgrp->illgrp_ill == ill) {
15851 		illgrp->illgrp_ill = ill->ill_group_next;
15852 	} else {
15853 		tmp_ill = illgrp->illgrp_ill;
15854 		while (tmp_ill->ill_group_next != ill) {
15855 			tmp_ill = tmp_ill->ill_group_next;
15856 			ASSERT(tmp_ill != NULL);
15857 		}
15858 		tmp_ill->ill_group_next = ill->ill_group_next;
15859 	}
15860 	ill->ill_group = NULL;
15861 	ill->ill_group_next = NULL;
15862 
15863 	illgrp->illgrp_ill_count--;
15864 	mutex_exit(&ill->ill_lock);
15865 	rw_exit(&ipst->ips_ill_g_lock);
15866 
15867 	/*
15868 	 * As this ill is leaving the group, we need to hand off
15869 	 * the responsibilities to the other ills in the group, if
15870 	 * this ill had some responsibilities.
15871 	 */
15872 
15873 	ill_handoff_responsibility(ill, illgrp);
15874 
15875 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15876 
15877 	if (illgrp->illgrp_ill_count == 0) {
15878 
15879 		ASSERT(illgrp->illgrp_ill == NULL);
15880 		if (ill->ill_isv6) {
15881 			if (illgrp == ipst->ips_illgrp_head_v6) {
15882 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15883 			} else {
15884 				tmpg = ipst->ips_illgrp_head_v6;
15885 				while (tmpg->illgrp_next != illgrp) {
15886 					tmpg = tmpg->illgrp_next;
15887 					ASSERT(tmpg != NULL);
15888 				}
15889 				tmpg->illgrp_next = illgrp->illgrp_next;
15890 			}
15891 		} else {
15892 			if (illgrp == ipst->ips_illgrp_head_v4) {
15893 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15894 			} else {
15895 				tmpg = ipst->ips_illgrp_head_v4;
15896 				while (tmpg->illgrp_next != illgrp) {
15897 					tmpg = tmpg->illgrp_next;
15898 					ASSERT(tmpg != NULL);
15899 				}
15900 				tmpg->illgrp_next = illgrp->illgrp_next;
15901 			}
15902 		}
15903 		mutex_destroy(&illgrp->illgrp_lock);
15904 		mi_free(illgrp);
15905 	}
15906 	rw_exit(&ipst->ips_ill_g_lock);
15907 
15908 	/*
15909 	 * Even though the ill is out of the group its not necessary
15910 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15911 	 * We will split the ipsq when phyint_groupname is set to NULL.
15912 	 */
15913 
15914 	/*
15915 	 * Send a routing sockets message if we are deleting from
15916 	 * groups with names.
15917 	 */
15918 	if (ill->ill_phyint->phyint_groupname_len != 0)
15919 		ip_rts_ifmsg(ill->ill_ipif);
15920 }
15921 
15922 /*
15923  * Re-do source address selection. This is normally called when
15924  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15925  * ipif comes up.
15926  */
15927 void
15928 ill_update_source_selection(ill_t *ill)
15929 {
15930 	ipif_t *ipif;
15931 
15932 	ASSERT(IAM_WRITER_ILL(ill));
15933 
15934 	if (ill->ill_group != NULL)
15935 		ill = ill->ill_group->illgrp_ill;
15936 
15937 	for (; ill != NULL; ill = ill->ill_group_next) {
15938 		for (ipif = ill->ill_ipif; ipif != NULL;
15939 		    ipif = ipif->ipif_next) {
15940 			if (ill->ill_isv6)
15941 				ipif_recreate_interface_routes_v6(NULL, ipif);
15942 			else
15943 				ipif_recreate_interface_routes(NULL, ipif);
15944 		}
15945 	}
15946 }
15947 
15948 /*
15949  * Insert ill in a group headed by illgrp_head. The caller can either
15950  * pass a groupname in which case we search for a group with the
15951  * same name to insert in or pass a group to insert in. This function
15952  * would only search groups with names.
15953  *
15954  * NOTE : The caller should make sure that there is at least one ipif
15955  *	  UP on this ill so that illgrp_scheduler can pick this ill
15956  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15957  *	  already sent a DL_UNBIND to the driver and we don't want to
15958  *	  send anymore packets. We don't assert for ipif_up_count
15959  *	  to be greater than zero, because ipif_up_done wants to call
15960  *	  this function before bumping up the ipif_up_count. See
15961  *	  ipif_up_done() for details.
15962  */
15963 int
15964 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15965     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15966 {
15967 	ill_group_t *illgrp;
15968 	ill_t *prev_ill;
15969 	phyint_t *phyi;
15970 	ip_stack_t	*ipst = ill->ill_ipst;
15971 
15972 	ASSERT(ill->ill_group == NULL);
15973 
15974 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15975 	mutex_enter(&ill->ill_lock);
15976 
15977 	if (groupname != NULL) {
15978 		/*
15979 		 * Look for a group with a matching groupname to insert.
15980 		 */
15981 		for (illgrp = *illgrp_head; illgrp != NULL;
15982 		    illgrp = illgrp->illgrp_next) {
15983 
15984 			ill_t *tmp_ill;
15985 
15986 			/*
15987 			 * If we have an ill_group_t in the list which has
15988 			 * no ill_t assigned then we must be in the process of
15989 			 * removing this group. We skip this as illgrp_delete()
15990 			 * will remove it from the list.
15991 			 */
15992 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15993 				ASSERT(illgrp->illgrp_ill_count == 0);
15994 				continue;
15995 			}
15996 
15997 			ASSERT(tmp_ill->ill_phyint != NULL);
15998 			phyi = tmp_ill->ill_phyint;
15999 			/*
16000 			 * Look at groups which has names only.
16001 			 */
16002 			if (phyi->phyint_groupname_len == 0)
16003 				continue;
16004 			/*
16005 			 * Names are stored in the phyint common to both
16006 			 * IPv4 and IPv6.
16007 			 */
16008 			if (mi_strcmp(phyi->phyint_groupname,
16009 			    groupname) == 0) {
16010 				break;
16011 			}
16012 		}
16013 	} else {
16014 		/*
16015 		 * If the caller passes in a NULL "grp_to_insert", we
16016 		 * allocate one below and insert this singleton.
16017 		 */
16018 		illgrp = grp_to_insert;
16019 	}
16020 
16021 	ill->ill_group_next = NULL;
16022 
16023 	if (illgrp == NULL) {
16024 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16025 		if (illgrp == NULL) {
16026 			return (ENOMEM);
16027 		}
16028 		illgrp->illgrp_next = *illgrp_head;
16029 		*illgrp_head = illgrp;
16030 		illgrp->illgrp_ill = ill;
16031 		illgrp->illgrp_ill_count = 1;
16032 		ill->ill_group = illgrp;
16033 		/*
16034 		 * Used in illgrp_scheduler to protect multiple threads
16035 		 * from traversing the list.
16036 		 */
16037 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16038 	} else {
16039 		ASSERT(ill->ill_net_type ==
16040 		    illgrp->illgrp_ill->ill_net_type);
16041 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16042 
16043 		/* Insert ill at tail of this group */
16044 		prev_ill = illgrp->illgrp_ill;
16045 		while (prev_ill->ill_group_next != NULL)
16046 			prev_ill = prev_ill->ill_group_next;
16047 		prev_ill->ill_group_next = ill;
16048 		ill->ill_group = illgrp;
16049 		illgrp->illgrp_ill_count++;
16050 		/*
16051 		 * Inherit group properties. Currently only forwarding
16052 		 * is the property we try to keep the same with all the
16053 		 * ills. When there are more, we will abstract this into
16054 		 * a function.
16055 		 */
16056 		ill->ill_flags &= ~ILLF_ROUTER;
16057 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16058 	}
16059 	mutex_exit(&ill->ill_lock);
16060 	rw_exit(&ipst->ips_ill_g_lock);
16061 
16062 	/*
16063 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16064 	 *    may be zero as it has not yet been bumped. But the ires
16065 	 *    have already been added. So, we do the nomination here
16066 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16067 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16068 	 *    ill_ipif_up_count here while nominating broadcast ires for
16069 	 *    receive.
16070 	 *
16071 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16072 	 *    to group them properly as ire_add() has already happened
16073 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16074 	 *    case, we need to do it here anyway.
16075 	 */
16076 	if (!ill->ill_isv6) {
16077 		ill_group_bcast_for_xmit(ill);
16078 		ill_nominate_bcast_rcv(illgrp);
16079 	}
16080 
16081 	if (!ipif_is_coming_up) {
16082 		/*
16083 		 * When ipif_up_done() calls this function, the multicast
16084 		 * groups have not been joined yet. So, there is no point in
16085 		 * nomination. ip_join_allmulti will handle groups when
16086 		 * ill_recover_multicast is called from ipif_up_done() later.
16087 		 */
16088 		(void) ill_nominate_mcast_rcv(illgrp);
16089 		/*
16090 		 * ipif_up_done calls ill_update_source_selection
16091 		 * anyway. Moreover, we don't want to re-create
16092 		 * interface routes while ipif_up_done() still has reference
16093 		 * to them. Refer to ipif_up_done() for more details.
16094 		 */
16095 		ill_update_source_selection(ill);
16096 	}
16097 
16098 	/*
16099 	 * Send a routing sockets message if we are inserting into
16100 	 * groups with names.
16101 	 */
16102 	if (groupname != NULL)
16103 		ip_rts_ifmsg(ill->ill_ipif);
16104 	return (0);
16105 }
16106 
16107 /*
16108  * Return the first phyint matching the groupname. There could
16109  * be more than one when there are ill groups.
16110  *
16111  * If 'usable' is set, then we exclude ones that are marked with any of
16112  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16113  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16114  * emulation of ipmp.
16115  */
16116 phyint_t *
16117 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16118 {
16119 	phyint_t *phyi;
16120 
16121 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16122 	/*
16123 	 * Group names are stored in the phyint - a common structure
16124 	 * to both IPv4 and IPv6.
16125 	 */
16126 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16127 	for (; phyi != NULL;
16128 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16129 	    phyi, AVL_AFTER)) {
16130 		if (phyi->phyint_groupname_len == 0)
16131 			continue;
16132 		/*
16133 		 * Skip the ones that should not be used since the callers
16134 		 * sometime use this for sending packets.
16135 		 */
16136 		if (usable && (phyi->phyint_flags &
16137 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16138 			continue;
16139 
16140 		ASSERT(phyi->phyint_groupname != NULL);
16141 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16142 			return (phyi);
16143 	}
16144 	return (NULL);
16145 }
16146 
16147 
16148 /*
16149  * Return the first usable phyint matching the group index. By 'usable'
16150  * we exclude ones that are marked ununsable with any of
16151  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16152  *
16153  * Used only for the ipmp/netinfo emulation of ipmp.
16154  */
16155 phyint_t *
16156 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16157 {
16158 	phyint_t *phyi;
16159 
16160 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16161 
16162 	if (!ipst->ips_ipmp_hook_emulation)
16163 		return (NULL);
16164 
16165 	/*
16166 	 * Group indicies are stored in the phyint - a common structure
16167 	 * to both IPv4 and IPv6.
16168 	 */
16169 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16170 	for (; phyi != NULL;
16171 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16172 	    phyi, AVL_AFTER)) {
16173 		/* Ignore the ones that do not have a group */
16174 		if (phyi->phyint_groupname_len == 0)
16175 			continue;
16176 
16177 		ASSERT(phyi->phyint_group_ifindex != 0);
16178 		/*
16179 		 * Skip the ones that should not be used since the callers
16180 		 * sometime use this for sending packets.
16181 		 */
16182 		if (phyi->phyint_flags &
16183 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16184 			continue;
16185 		if (phyi->phyint_group_ifindex == group_ifindex)
16186 			return (phyi);
16187 	}
16188 	return (NULL);
16189 }
16190 
16191 
16192 /*
16193  * MT notes on creation and deletion of IPMP groups
16194  *
16195  * Creation and deletion of IPMP groups introduce the need to merge or
16196  * split the associated serialization objects i.e the ipsq's. Normally all
16197  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16198  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16199  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16200  * is a need to change the <ill-ipsq> association and we have to operate on both
16201  * the source and destination IPMP groups. For eg. attempting to set the
16202  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16203  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16204  * source or destination IPMP group are mapped to a single ipsq for executing
16205  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16206  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16207  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16208  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16209  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16210  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16211  *
16212  * In the above example the ioctl handling code locates the current ipsq of hme0
16213  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16214  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16215  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16216  * the destination ipsq. If the destination ipsq is not busy, it also enters
16217  * the destination ipsq exclusively. Now the actual groupname setting operation
16218  * can proceed. If the destination ipsq is busy, the operation is enqueued
16219  * on the destination (merged) ipsq and will be handled in the unwind from
16220  * ipsq_exit.
16221  *
16222  * To prevent other threads accessing the ill while the group name change is
16223  * in progres, we bring down the ipifs which also removes the ill from the
16224  * group. The group is changed in phyint and when the first ipif on the ill
16225  * is brought up, the ill is inserted into the right IPMP group by
16226  * illgrp_insert.
16227  */
16228 /* ARGSUSED */
16229 int
16230 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16231     ip_ioctl_cmd_t *ipip, void *ifreq)
16232 {
16233 	int i;
16234 	char *tmp;
16235 	int namelen;
16236 	ill_t *ill = ipif->ipif_ill;
16237 	ill_t *ill_v4, *ill_v6;
16238 	int err = 0;
16239 	phyint_t *phyi;
16240 	phyint_t *phyi_tmp;
16241 	struct lifreq *lifr;
16242 	mblk_t	*mp1;
16243 	char *groupname;
16244 	ipsq_t *ipsq;
16245 	ip_stack_t	*ipst = ill->ill_ipst;
16246 
16247 	ASSERT(IAM_WRITER_IPIF(ipif));
16248 
16249 	/* Existance verified in ip_wput_nondata */
16250 	mp1 = mp->b_cont->b_cont;
16251 	lifr = (struct lifreq *)mp1->b_rptr;
16252 	groupname = lifr->lifr_groupname;
16253 
16254 	if (ipif->ipif_id != 0)
16255 		return (EINVAL);
16256 
16257 	phyi = ill->ill_phyint;
16258 	ASSERT(phyi != NULL);
16259 
16260 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16261 		return (EINVAL);
16262 
16263 	tmp = groupname;
16264 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16265 		;
16266 
16267 	if (i == LIFNAMSIZ) {
16268 		/* no null termination */
16269 		return (EINVAL);
16270 	}
16271 
16272 	/*
16273 	 * Calculate the namelen exclusive of the null
16274 	 * termination character.
16275 	 */
16276 	namelen = tmp - groupname;
16277 
16278 	ill_v4 = phyi->phyint_illv4;
16279 	ill_v6 = phyi->phyint_illv6;
16280 
16281 	/*
16282 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16283 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16284 	 * synchronization notes in ip.c
16285 	 */
16286 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16287 		return (EINVAL);
16288 	}
16289 
16290 	/*
16291 	 * mark the ill as changing.
16292 	 * this should queue all new requests on the syncq.
16293 	 */
16294 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16295 
16296 	if (ill_v4 != NULL)
16297 		ill_v4->ill_state_flags |= ILL_CHANGING;
16298 	if (ill_v6 != NULL)
16299 		ill_v6->ill_state_flags |= ILL_CHANGING;
16300 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16301 
16302 	if (namelen == 0) {
16303 		/*
16304 		 * Null string means remove this interface from the
16305 		 * existing group.
16306 		 */
16307 		if (phyi->phyint_groupname_len == 0) {
16308 			/*
16309 			 * Never was in a group.
16310 			 */
16311 			err = 0;
16312 			goto done;
16313 		}
16314 
16315 		/*
16316 		 * IPv4 or IPv6 may be temporarily out of the group when all
16317 		 * the ipifs are down. Thus, we need to check for ill_group to
16318 		 * be non-NULL.
16319 		 */
16320 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16321 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16322 			mutex_enter(&ill_v4->ill_lock);
16323 			if (!ill_is_quiescent(ill_v4)) {
16324 				/*
16325 				 * ipsq_pending_mp_add will not fail since
16326 				 * connp is NULL
16327 				 */
16328 				(void) ipsq_pending_mp_add(NULL,
16329 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16330 				mutex_exit(&ill_v4->ill_lock);
16331 				err = EINPROGRESS;
16332 				goto done;
16333 			}
16334 			mutex_exit(&ill_v4->ill_lock);
16335 		}
16336 
16337 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16338 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16339 			mutex_enter(&ill_v6->ill_lock);
16340 			if (!ill_is_quiescent(ill_v6)) {
16341 				(void) ipsq_pending_mp_add(NULL,
16342 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16343 				mutex_exit(&ill_v6->ill_lock);
16344 				err = EINPROGRESS;
16345 				goto done;
16346 			}
16347 			mutex_exit(&ill_v6->ill_lock);
16348 		}
16349 
16350 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16351 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16352 		mutex_enter(&phyi->phyint_lock);
16353 		ASSERT(phyi->phyint_groupname != NULL);
16354 		mi_free(phyi->phyint_groupname);
16355 		phyi->phyint_groupname = NULL;
16356 		phyi->phyint_groupname_len = 0;
16357 
16358 		/* Restore the ifindex used to be the per interface one */
16359 		phyi->phyint_group_ifindex = 0;
16360 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16361 		mutex_exit(&phyi->phyint_lock);
16362 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16363 		rw_exit(&ipst->ips_ill_g_lock);
16364 		err = ill_up_ipifs(ill, q, mp);
16365 
16366 		/*
16367 		 * set the split flag so that the ipsq can be split
16368 		 */
16369 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16370 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16371 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16372 
16373 	} else {
16374 		if (phyi->phyint_groupname_len != 0) {
16375 			ASSERT(phyi->phyint_groupname != NULL);
16376 			/* Are we inserting in the same group ? */
16377 			if (mi_strcmp(groupname,
16378 			    phyi->phyint_groupname) == 0) {
16379 				err = 0;
16380 				goto done;
16381 			}
16382 		}
16383 
16384 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16385 		/*
16386 		 * Merge ipsq for the group's.
16387 		 * This check is here as multiple groups/ills might be
16388 		 * sharing the same ipsq.
16389 		 * If we have to merege than the operation is restarted
16390 		 * on the new ipsq.
16391 		 */
16392 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16393 		if (phyi->phyint_ipsq != ipsq) {
16394 			rw_exit(&ipst->ips_ill_g_lock);
16395 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16396 			goto done;
16397 		}
16398 		/*
16399 		 * Running exclusive on new ipsq.
16400 		 */
16401 
16402 		ASSERT(ipsq != NULL);
16403 		ASSERT(ipsq->ipsq_writer == curthread);
16404 
16405 		/*
16406 		 * Check whether the ill_type and ill_net_type matches before
16407 		 * we allocate any memory so that the cleanup is easier.
16408 		 *
16409 		 * We can't group dissimilar ones as we can't load spread
16410 		 * packets across the group because of potential link-level
16411 		 * header differences.
16412 		 */
16413 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16414 		if (phyi_tmp != NULL) {
16415 			if ((ill_v4 != NULL &&
16416 			    phyi_tmp->phyint_illv4 != NULL) &&
16417 			    ((ill_v4->ill_net_type !=
16418 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16419 			    (ill_v4->ill_type !=
16420 			    phyi_tmp->phyint_illv4->ill_type))) {
16421 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16422 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16423 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16424 				rw_exit(&ipst->ips_ill_g_lock);
16425 				return (EINVAL);
16426 			}
16427 			if ((ill_v6 != NULL &&
16428 			    phyi_tmp->phyint_illv6 != NULL) &&
16429 			    ((ill_v6->ill_net_type !=
16430 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16431 			    (ill_v6->ill_type !=
16432 			    phyi_tmp->phyint_illv6->ill_type))) {
16433 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16434 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16435 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16436 				rw_exit(&ipst->ips_ill_g_lock);
16437 				return (EINVAL);
16438 			}
16439 		}
16440 
16441 		rw_exit(&ipst->ips_ill_g_lock);
16442 
16443 		/*
16444 		 * bring down all v4 ipifs.
16445 		 */
16446 		if (ill_v4 != NULL) {
16447 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16448 		}
16449 
16450 		/*
16451 		 * bring down all v6 ipifs.
16452 		 */
16453 		if (ill_v6 != NULL) {
16454 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16455 		}
16456 
16457 		/*
16458 		 * make sure all ipifs are down and there are no active
16459 		 * references. Call to ipsq_pending_mp_add will not fail
16460 		 * since connp is NULL.
16461 		 */
16462 		if (ill_v4 != NULL) {
16463 			mutex_enter(&ill_v4->ill_lock);
16464 			if (!ill_is_quiescent(ill_v4)) {
16465 				(void) ipsq_pending_mp_add(NULL,
16466 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16467 				mutex_exit(&ill_v4->ill_lock);
16468 				err = EINPROGRESS;
16469 				goto done;
16470 			}
16471 			mutex_exit(&ill_v4->ill_lock);
16472 		}
16473 
16474 		if (ill_v6 != NULL) {
16475 			mutex_enter(&ill_v6->ill_lock);
16476 			if (!ill_is_quiescent(ill_v6)) {
16477 				(void) ipsq_pending_mp_add(NULL,
16478 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16479 				mutex_exit(&ill_v6->ill_lock);
16480 				err = EINPROGRESS;
16481 				goto done;
16482 			}
16483 			mutex_exit(&ill_v6->ill_lock);
16484 		}
16485 
16486 		/*
16487 		 * allocate including space for null terminator
16488 		 * before we insert.
16489 		 */
16490 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16491 		if (tmp == NULL)
16492 			return (ENOMEM);
16493 
16494 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16495 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16496 		mutex_enter(&phyi->phyint_lock);
16497 		if (phyi->phyint_groupname_len != 0) {
16498 			ASSERT(phyi->phyint_groupname != NULL);
16499 			mi_free(phyi->phyint_groupname);
16500 		}
16501 
16502 		/*
16503 		 * setup the new group name.
16504 		 */
16505 		phyi->phyint_groupname = tmp;
16506 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16507 		phyi->phyint_groupname_len = namelen + 1;
16508 
16509 		if (ipst->ips_ipmp_hook_emulation) {
16510 			/*
16511 			 * If the group already exists we use the existing
16512 			 * group_ifindex, otherwise we pick a new index here.
16513 			 */
16514 			if (phyi_tmp != NULL) {
16515 				phyi->phyint_group_ifindex =
16516 				    phyi_tmp->phyint_group_ifindex;
16517 			} else {
16518 				/* XXX We need a recovery strategy here. */
16519 				if (!ip_assign_ifindex(
16520 				    &phyi->phyint_group_ifindex, ipst))
16521 					cmn_err(CE_PANIC,
16522 					    "ip_assign_ifindex() failed");
16523 			}
16524 		}
16525 		/*
16526 		 * Select whether the netinfo and hook use the per-interface
16527 		 * or per-group ifindex.
16528 		 */
16529 		if (ipst->ips_ipmp_hook_emulation)
16530 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16531 		else
16532 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16533 
16534 		if (ipst->ips_ipmp_hook_emulation &&
16535 		    phyi_tmp != NULL) {
16536 			/* First phyint in group - group PLUMB event */
16537 			ill_nic_info_plumb(ill, B_TRUE);
16538 		}
16539 		mutex_exit(&phyi->phyint_lock);
16540 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16541 		rw_exit(&ipst->ips_ill_g_lock);
16542 
16543 		err = ill_up_ipifs(ill, q, mp);
16544 	}
16545 
16546 done:
16547 	/*
16548 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16549 	 */
16550 	if (err != EINPROGRESS) {
16551 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16552 		if (ill_v4 != NULL)
16553 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16554 		if (ill_v6 != NULL)
16555 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16556 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16557 	}
16558 	return (err);
16559 }
16560 
16561 /* ARGSUSED */
16562 int
16563 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16564     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16565 {
16566 	ill_t *ill;
16567 	phyint_t *phyi;
16568 	struct lifreq *lifr;
16569 	mblk_t	*mp1;
16570 
16571 	/* Existence verified in ip_wput_nondata */
16572 	mp1 = mp->b_cont->b_cont;
16573 	lifr = (struct lifreq *)mp1->b_rptr;
16574 	ill = ipif->ipif_ill;
16575 	phyi = ill->ill_phyint;
16576 
16577 	lifr->lifr_groupname[0] = '\0';
16578 	/*
16579 	 * ill_group may be null if all the interfaces
16580 	 * are down. But still, the phyint should always
16581 	 * hold the name.
16582 	 */
16583 	if (phyi->phyint_groupname_len != 0) {
16584 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16585 		    phyi->phyint_groupname_len);
16586 	}
16587 
16588 	return (0);
16589 }
16590 
16591 
16592 typedef struct conn_move_s {
16593 	ill_t	*cm_from_ill;
16594 	ill_t	*cm_to_ill;
16595 	int	cm_ifindex;
16596 } conn_move_t;
16597 
16598 /*
16599  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16600  */
16601 static void
16602 conn_move(conn_t *connp, caddr_t arg)
16603 {
16604 	conn_move_t *connm;
16605 	int ifindex;
16606 	int i;
16607 	ill_t *from_ill;
16608 	ill_t *to_ill;
16609 	ilg_t *ilg;
16610 	ilm_t *ret_ilm;
16611 
16612 	connm = (conn_move_t *)arg;
16613 	ifindex = connm->cm_ifindex;
16614 	from_ill = connm->cm_from_ill;
16615 	to_ill = connm->cm_to_ill;
16616 
16617 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16618 
16619 	/* All multicast fields protected by conn_lock */
16620 	mutex_enter(&connp->conn_lock);
16621 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16622 	if ((connp->conn_outgoing_ill == from_ill) &&
16623 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16624 		connp->conn_outgoing_ill = to_ill;
16625 		connp->conn_incoming_ill = to_ill;
16626 	}
16627 
16628 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16629 
16630 	if ((connp->conn_multicast_ill == from_ill) &&
16631 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16632 		connp->conn_multicast_ill = connm->cm_to_ill;
16633 	}
16634 
16635 	/*
16636 	 * Change the ilg_ill to point to the new one. This assumes
16637 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16638 	 * has been told to receive packets on this interface.
16639 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16640 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16641 	 * some ilms may not have moved. We check to see whether
16642 	 * the ilms have moved to to_ill. We can't check on from_ill
16643 	 * as in the process of moving, we could have split an ilm
16644 	 * in to two - which has the same orig_ifindex and v6group.
16645 	 *
16646 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16647 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16648 	 */
16649 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16650 		ilg = &connp->conn_ilg[i];
16651 		if ((ilg->ilg_ill == from_ill) &&
16652 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16653 			/* ifindex != 0 indicates failback */
16654 			if (ifindex != 0) {
16655 				connp->conn_ilg[i].ilg_ill = to_ill;
16656 				continue;
16657 			}
16658 
16659 			mutex_enter(&to_ill->ill_lock);
16660 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16661 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16662 			    connp->conn_zoneid);
16663 			mutex_exit(&to_ill->ill_lock);
16664 
16665 			if (ret_ilm != NULL)
16666 				connp->conn_ilg[i].ilg_ill = to_ill;
16667 		}
16668 	}
16669 	mutex_exit(&connp->conn_lock);
16670 }
16671 
16672 static void
16673 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16674 {
16675 	conn_move_t connm;
16676 	ip_stack_t	*ipst = from_ill->ill_ipst;
16677 
16678 	connm.cm_from_ill = from_ill;
16679 	connm.cm_to_ill = to_ill;
16680 	connm.cm_ifindex = ifindex;
16681 
16682 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16683 }
16684 
16685 /*
16686  * ilm has been moved from from_ill to to_ill.
16687  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16688  * appropriately.
16689  *
16690  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16691  *	  the code there de-references ipif_ill to get the ill to
16692  *	  send multicast requests. It does not work as ipif is on its
16693  *	  move and already moved when this function is called.
16694  *	  Thus, we need to use from_ill and to_ill send down multicast
16695  *	  requests.
16696  */
16697 static void
16698 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16699 {
16700 	ipif_t *ipif;
16701 	ilm_t *ilm;
16702 
16703 	/*
16704 	 * See whether we need to send down DL_ENABMULTI_REQ on
16705 	 * to_ill as ilm has just been added.
16706 	 */
16707 	ASSERT(IAM_WRITER_ILL(to_ill));
16708 	ASSERT(IAM_WRITER_ILL(from_ill));
16709 
16710 	ILM_WALKER_HOLD(to_ill);
16711 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16712 
16713 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16714 			continue;
16715 		/*
16716 		 * no locks held, ill/ipif cannot dissappear as long
16717 		 * as we are writer.
16718 		 */
16719 		ipif = to_ill->ill_ipif;
16720 		/*
16721 		 * No need to hold any lock as we are the writer and this
16722 		 * can only be changed by a writer.
16723 		 */
16724 		ilm->ilm_is_new = B_FALSE;
16725 
16726 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16727 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16728 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16729 			    "resolver\n"));
16730 			continue;		/* Must be IRE_IF_NORESOLVER */
16731 		}
16732 
16733 
16734 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16735 			ip1dbg(("ilm_send_multicast_reqs: "
16736 			    "to_ill MULTI_BCAST\n"));
16737 			goto from;
16738 		}
16739 
16740 		if (to_ill->ill_isv6)
16741 			mld_joingroup(ilm);
16742 		else
16743 			igmp_joingroup(ilm);
16744 
16745 		if (to_ill->ill_ipif_up_count == 0) {
16746 			/*
16747 			 * Nobody there. All multicast addresses will be
16748 			 * re-joined when we get the DL_BIND_ACK bringing the
16749 			 * interface up.
16750 			 */
16751 			ilm->ilm_notify_driver = B_FALSE;
16752 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16753 			goto from;
16754 		}
16755 
16756 		/*
16757 		 * For allmulti address, we want to join on only one interface.
16758 		 * Checking for ilm_numentries_v6 is not correct as you may
16759 		 * find an ilm with zero address on to_ill, but we may not
16760 		 * have nominated to_ill for receiving. Thus, if we have
16761 		 * nominated from_ill (ill_join_allmulti is set), nominate
16762 		 * only if to_ill is not already nominated (to_ill normally
16763 		 * should not have been nominated if "from_ill" has already
16764 		 * been nominated. As we don't prevent failovers from happening
16765 		 * across groups, we don't assert).
16766 		 */
16767 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16768 			/*
16769 			 * There is no need to hold ill locks as we are
16770 			 * writer on both ills and when ill_join_allmulti
16771 			 * is changed the thread is always a writer.
16772 			 */
16773 			if (from_ill->ill_join_allmulti &&
16774 			    !to_ill->ill_join_allmulti) {
16775 				(void) ip_join_allmulti(to_ill->ill_ipif);
16776 			}
16777 		} else if (ilm->ilm_notify_driver) {
16778 
16779 			/*
16780 			 * This is a newly moved ilm so we need to tell the
16781 			 * driver about the new group. There can be more than
16782 			 * one ilm's for the same group in the list each with a
16783 			 * different orig_ifindex. We have to inform the driver
16784 			 * once. In ilm_move_v[4,6] we only set the flag
16785 			 * ilm_notify_driver for the first ilm.
16786 			 */
16787 
16788 			(void) ip_ll_send_enabmulti_req(to_ill,
16789 			    &ilm->ilm_v6addr);
16790 		}
16791 
16792 		ilm->ilm_notify_driver = B_FALSE;
16793 
16794 		/*
16795 		 * See whether we need to send down DL_DISABMULTI_REQ on
16796 		 * from_ill as ilm has just been removed.
16797 		 */
16798 from:
16799 		ipif = from_ill->ill_ipif;
16800 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16801 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16802 			ip1dbg(("ilm_send_multicast_reqs: "
16803 			    "from_ill not resolver\n"));
16804 			continue;		/* Must be IRE_IF_NORESOLVER */
16805 		}
16806 
16807 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16808 			ip1dbg(("ilm_send_multicast_reqs: "
16809 			    "from_ill MULTI_BCAST\n"));
16810 			continue;
16811 		}
16812 
16813 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16814 			if (from_ill->ill_join_allmulti)
16815 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16816 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16817 			(void) ip_ll_send_disabmulti_req(from_ill,
16818 			    &ilm->ilm_v6addr);
16819 		}
16820 	}
16821 	ILM_WALKER_RELE(to_ill);
16822 }
16823 
16824 /*
16825  * This function is called when all multicast memberships needs
16826  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16827  * called only once unlike the IPv4 counterpart where it is called after
16828  * every logical interface is moved. The reason is due to multicast
16829  * memberships are joined using an interface address in IPv4 while in
16830  * IPv6, interface index is used.
16831  */
16832 static void
16833 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16834 {
16835 	ilm_t	*ilm;
16836 	ilm_t	*ilm_next;
16837 	ilm_t	*new_ilm;
16838 	ilm_t	**ilmp;
16839 	int	count;
16840 	char buf[INET6_ADDRSTRLEN];
16841 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16842 	ip_stack_t	*ipst = from_ill->ill_ipst;
16843 
16844 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16845 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16846 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16847 
16848 	if (ifindex == 0) {
16849 		/*
16850 		 * Form the solicited node mcast address which is used later.
16851 		 */
16852 		ipif_t *ipif;
16853 
16854 		ipif = from_ill->ill_ipif;
16855 		ASSERT(ipif->ipif_id == 0);
16856 
16857 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16858 	}
16859 
16860 	ilmp = &from_ill->ill_ilm;
16861 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16862 		ilm_next = ilm->ilm_next;
16863 
16864 		if (ilm->ilm_flags & ILM_DELETED) {
16865 			ilmp = &ilm->ilm_next;
16866 			continue;
16867 		}
16868 
16869 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16870 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16871 		ASSERT(ilm->ilm_orig_ifindex != 0);
16872 		if (ilm->ilm_orig_ifindex == ifindex) {
16873 			/*
16874 			 * We are failing back multicast memberships.
16875 			 * If the same ilm exists in to_ill, it means somebody
16876 			 * has joined the same group there e.g. ff02::1
16877 			 * is joined within the kernel when the interfaces
16878 			 * came UP.
16879 			 */
16880 			ASSERT(ilm->ilm_ipif == NULL);
16881 			if (new_ilm != NULL) {
16882 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16883 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16884 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16885 					new_ilm->ilm_is_new = B_TRUE;
16886 				}
16887 			} else {
16888 				/*
16889 				 * check if we can just move the ilm
16890 				 */
16891 				if (from_ill->ill_ilm_walker_cnt != 0) {
16892 					/*
16893 					 * We have walkers we cannot move
16894 					 * the ilm, so allocate a new ilm,
16895 					 * this (old) ilm will be marked
16896 					 * ILM_DELETED at the end of the loop
16897 					 * and will be freed when the
16898 					 * last walker exits.
16899 					 */
16900 					new_ilm = (ilm_t *)mi_zalloc
16901 					    (sizeof (ilm_t));
16902 					if (new_ilm == NULL) {
16903 						ip0dbg(("ilm_move_v6: "
16904 						    "FAILBACK of IPv6"
16905 						    " multicast address %s : "
16906 						    "from %s to"
16907 						    " %s failed : ENOMEM \n",
16908 						    inet_ntop(AF_INET6,
16909 						    &ilm->ilm_v6addr, buf,
16910 						    sizeof (buf)),
16911 						    from_ill->ill_name,
16912 						    to_ill->ill_name));
16913 
16914 							ilmp = &ilm->ilm_next;
16915 							continue;
16916 					}
16917 					*new_ilm = *ilm;
16918 					/*
16919 					 * we don't want new_ilm linked to
16920 					 * ilm's filter list.
16921 					 */
16922 					new_ilm->ilm_filter = NULL;
16923 				} else {
16924 					/*
16925 					 * No walkers we can move the ilm.
16926 					 * lets take it out of the list.
16927 					 */
16928 					*ilmp = ilm->ilm_next;
16929 					ilm->ilm_next = NULL;
16930 					new_ilm = ilm;
16931 				}
16932 
16933 				/*
16934 				 * if this is the first ilm for the group
16935 				 * set ilm_notify_driver so that we notify the
16936 				 * driver in ilm_send_multicast_reqs.
16937 				 */
16938 				if (ilm_lookup_ill_v6(to_ill,
16939 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16940 					new_ilm->ilm_notify_driver = B_TRUE;
16941 
16942 				new_ilm->ilm_ill = to_ill;
16943 				/* Add to the to_ill's list */
16944 				new_ilm->ilm_next = to_ill->ill_ilm;
16945 				to_ill->ill_ilm = new_ilm;
16946 				/*
16947 				 * set the flag so that mld_joingroup is
16948 				 * called in ilm_send_multicast_reqs().
16949 				 */
16950 				new_ilm->ilm_is_new = B_TRUE;
16951 			}
16952 			goto bottom;
16953 		} else if (ifindex != 0) {
16954 			/*
16955 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16956 			 * has not matched above, look at the next ilm.
16957 			 */
16958 			ilmp = &ilm->ilm_next;
16959 			continue;
16960 		}
16961 		/*
16962 		 * If we are here, it means ifindex is 0. Failover
16963 		 * everything.
16964 		 *
16965 		 * We need to handle solicited node mcast address
16966 		 * and all_nodes mcast address differently as they
16967 		 * are joined witin the kenrel (ipif_multicast_up)
16968 		 * and potentially from the userland. We are called
16969 		 * after the ipifs of from_ill has been moved.
16970 		 * If we still find ilms on ill with solicited node
16971 		 * mcast address or all_nodes mcast address, it must
16972 		 * belong to the UP interface that has not moved e.g.
16973 		 * ipif_id 0 with the link local prefix does not move.
16974 		 * We join this on the new ill accounting for all the
16975 		 * userland memberships so that applications don't
16976 		 * see any failure.
16977 		 *
16978 		 * We need to make sure that we account only for the
16979 		 * solicited node and all node multicast addresses
16980 		 * that was brought UP on these. In the case of
16981 		 * a failover from A to B, we might have ilms belonging
16982 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16983 		 * for the membership from the userland. If we are failing
16984 		 * over from B to C now, we will find the ones belonging
16985 		 * to A on B. These don't account for the ill_ipif_up_count.
16986 		 * They just move from B to C. The check below on
16987 		 * ilm_orig_ifindex ensures that.
16988 		 */
16989 		if ((ilm->ilm_orig_ifindex ==
16990 		    from_ill->ill_phyint->phyint_ifindex) &&
16991 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16992 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16993 		    &ilm->ilm_v6addr))) {
16994 			ASSERT(ilm->ilm_refcnt > 0);
16995 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16996 			/*
16997 			 * For indentation reasons, we are not using a
16998 			 * "else" here.
16999 			 */
17000 			if (count == 0) {
17001 				ilmp = &ilm->ilm_next;
17002 				continue;
17003 			}
17004 			ilm->ilm_refcnt -= count;
17005 			if (new_ilm != NULL) {
17006 				/*
17007 				 * Can find one with the same
17008 				 * ilm_orig_ifindex, if we are failing
17009 				 * over to a STANDBY. This happens
17010 				 * when somebody wants to join a group
17011 				 * on a STANDBY interface and we
17012 				 * internally join on a different one.
17013 				 * If we had joined on from_ill then, a
17014 				 * failover now will find a new ilm
17015 				 * with this index.
17016 				 */
17017 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17018 				    " new ilm on %s, group address %s\n",
17019 				    to_ill->ill_name,
17020 				    inet_ntop(AF_INET6,
17021 				    &ilm->ilm_v6addr, buf,
17022 				    sizeof (buf))));
17023 				new_ilm->ilm_refcnt += count;
17024 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17025 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17026 					new_ilm->ilm_is_new = B_TRUE;
17027 				}
17028 			} else {
17029 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17030 				if (new_ilm == NULL) {
17031 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17032 					    " multicast address %s : from %s to"
17033 					    " %s failed : ENOMEM \n",
17034 					    inet_ntop(AF_INET6,
17035 					    &ilm->ilm_v6addr, buf,
17036 					    sizeof (buf)), from_ill->ill_name,
17037 					    to_ill->ill_name));
17038 					ilmp = &ilm->ilm_next;
17039 					continue;
17040 				}
17041 				*new_ilm = *ilm;
17042 				new_ilm->ilm_filter = NULL;
17043 				new_ilm->ilm_refcnt = count;
17044 				new_ilm->ilm_timer = INFINITY;
17045 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17046 				new_ilm->ilm_is_new = B_TRUE;
17047 				/*
17048 				 * If the to_ill has not joined this
17049 				 * group we need to tell the driver in
17050 				 * ill_send_multicast_reqs.
17051 				 */
17052 				if (ilm_lookup_ill_v6(to_ill,
17053 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17054 					new_ilm->ilm_notify_driver = B_TRUE;
17055 
17056 				new_ilm->ilm_ill = to_ill;
17057 				/* Add to the to_ill's list */
17058 				new_ilm->ilm_next = to_ill->ill_ilm;
17059 				to_ill->ill_ilm = new_ilm;
17060 				ASSERT(new_ilm->ilm_ipif == NULL);
17061 			}
17062 			if (ilm->ilm_refcnt == 0) {
17063 				goto bottom;
17064 			} else {
17065 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17066 				CLEAR_SLIST(new_ilm->ilm_filter);
17067 				ilmp = &ilm->ilm_next;
17068 			}
17069 			continue;
17070 		} else {
17071 			/*
17072 			 * ifindex = 0 means, move everything pointing at
17073 			 * from_ill. We are doing this becuase ill has
17074 			 * either FAILED or became INACTIVE.
17075 			 *
17076 			 * As we would like to move things later back to
17077 			 * from_ill, we want to retain the identity of this
17078 			 * ilm. Thus, we don't blindly increment the reference
17079 			 * count on the ilms matching the address alone. We
17080 			 * need to match on the ilm_orig_index also. new_ilm
17081 			 * was obtained by matching ilm_orig_index also.
17082 			 */
17083 			if (new_ilm != NULL) {
17084 				/*
17085 				 * This is possible only if a previous restore
17086 				 * was incomplete i.e restore to
17087 				 * ilm_orig_ifindex left some ilms because
17088 				 * of some failures. Thus when we are failing
17089 				 * again, we might find our old friends there.
17090 				 */
17091 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17092 				    " on %s, group address %s\n",
17093 				    to_ill->ill_name,
17094 				    inet_ntop(AF_INET6,
17095 				    &ilm->ilm_v6addr, buf,
17096 				    sizeof (buf))));
17097 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17098 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17099 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17100 					new_ilm->ilm_is_new = B_TRUE;
17101 				}
17102 			} else {
17103 				if (from_ill->ill_ilm_walker_cnt != 0) {
17104 					new_ilm = (ilm_t *)
17105 					    mi_zalloc(sizeof (ilm_t));
17106 					if (new_ilm == NULL) {
17107 						ip0dbg(("ilm_move_v6: "
17108 						    "FAILOVER of IPv6"
17109 						    " multicast address %s : "
17110 						    "from %s to"
17111 						    " %s failed : ENOMEM \n",
17112 						    inet_ntop(AF_INET6,
17113 						    &ilm->ilm_v6addr, buf,
17114 						    sizeof (buf)),
17115 						    from_ill->ill_name,
17116 						    to_ill->ill_name));
17117 
17118 							ilmp = &ilm->ilm_next;
17119 							continue;
17120 					}
17121 					*new_ilm = *ilm;
17122 					new_ilm->ilm_filter = NULL;
17123 				} else {
17124 					*ilmp = ilm->ilm_next;
17125 					new_ilm = ilm;
17126 				}
17127 				/*
17128 				 * If the to_ill has not joined this
17129 				 * group we need to tell the driver in
17130 				 * ill_send_multicast_reqs.
17131 				 */
17132 				if (ilm_lookup_ill_v6(to_ill,
17133 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17134 					new_ilm->ilm_notify_driver = B_TRUE;
17135 
17136 				/* Add to the to_ill's list */
17137 				new_ilm->ilm_next = to_ill->ill_ilm;
17138 				to_ill->ill_ilm = new_ilm;
17139 				ASSERT(ilm->ilm_ipif == NULL);
17140 				new_ilm->ilm_ill = to_ill;
17141 				new_ilm->ilm_is_new = B_TRUE;
17142 			}
17143 
17144 		}
17145 
17146 bottom:
17147 		/*
17148 		 * Revert multicast filter state to (EXCLUDE, NULL).
17149 		 * new_ilm->ilm_is_new should already be set if needed.
17150 		 */
17151 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17152 		CLEAR_SLIST(new_ilm->ilm_filter);
17153 		/*
17154 		 * We allocated/got a new ilm, free the old one.
17155 		 */
17156 		if (new_ilm != ilm) {
17157 			if (from_ill->ill_ilm_walker_cnt == 0) {
17158 				*ilmp = ilm->ilm_next;
17159 				ilm->ilm_next = NULL;
17160 				FREE_SLIST(ilm->ilm_filter);
17161 				FREE_SLIST(ilm->ilm_pendsrcs);
17162 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17163 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17164 				mi_free((char *)ilm);
17165 			} else {
17166 				ilm->ilm_flags |= ILM_DELETED;
17167 				from_ill->ill_ilm_cleanup_reqd = 1;
17168 				ilmp = &ilm->ilm_next;
17169 			}
17170 		}
17171 	}
17172 }
17173 
17174 /*
17175  * Move all the multicast memberships to to_ill. Called when
17176  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17177  * different from IPv6 counterpart as multicast memberships are associated
17178  * with ills in IPv6. This function is called after every ipif is moved
17179  * unlike IPv6, where it is moved only once.
17180  */
17181 static void
17182 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17183 {
17184 	ilm_t	*ilm;
17185 	ilm_t	*ilm_next;
17186 	ilm_t	*new_ilm;
17187 	ilm_t	**ilmp;
17188 	ip_stack_t	*ipst = from_ill->ill_ipst;
17189 
17190 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17191 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17192 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17193 
17194 	ilmp = &from_ill->ill_ilm;
17195 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17196 		ilm_next = ilm->ilm_next;
17197 
17198 		if (ilm->ilm_flags & ILM_DELETED) {
17199 			ilmp = &ilm->ilm_next;
17200 			continue;
17201 		}
17202 
17203 		ASSERT(ilm->ilm_ipif != NULL);
17204 
17205 		if (ilm->ilm_ipif != ipif) {
17206 			ilmp = &ilm->ilm_next;
17207 			continue;
17208 		}
17209 
17210 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17211 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17212 			new_ilm = ilm_lookup_ipif(ipif,
17213 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17214 			if (new_ilm != NULL) {
17215 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17216 				/*
17217 				 * We still need to deal with the from_ill.
17218 				 */
17219 				new_ilm->ilm_is_new = B_TRUE;
17220 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17221 				CLEAR_SLIST(new_ilm->ilm_filter);
17222 				goto delete_ilm;
17223 			}
17224 			/*
17225 			 * If we could not find one e.g. ipif is
17226 			 * still down on to_ill, we add this ilm
17227 			 * on ill_new to preserve the reference
17228 			 * count.
17229 			 */
17230 		}
17231 		/*
17232 		 * When ipifs move, ilms always move with it
17233 		 * to the NEW ill. Thus we should never be
17234 		 * able to find ilm till we really move it here.
17235 		 */
17236 		ASSERT(ilm_lookup_ipif(ipif,
17237 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17238 
17239 		if (from_ill->ill_ilm_walker_cnt != 0) {
17240 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17241 			if (new_ilm == NULL) {
17242 				char buf[INET6_ADDRSTRLEN];
17243 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17244 				    " multicast address %s : "
17245 				    "from %s to"
17246 				    " %s failed : ENOMEM \n",
17247 				    inet_ntop(AF_INET,
17248 				    &ilm->ilm_v6addr, buf,
17249 				    sizeof (buf)),
17250 				    from_ill->ill_name,
17251 				    to_ill->ill_name));
17252 
17253 				ilmp = &ilm->ilm_next;
17254 				continue;
17255 			}
17256 			*new_ilm = *ilm;
17257 			/* We don't want new_ilm linked to ilm's filter list */
17258 			new_ilm->ilm_filter = NULL;
17259 		} else {
17260 			/* Remove from the list */
17261 			*ilmp = ilm->ilm_next;
17262 			new_ilm = ilm;
17263 		}
17264 
17265 		/*
17266 		 * If we have never joined this group on the to_ill
17267 		 * make sure we tell the driver.
17268 		 */
17269 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17270 		    ALL_ZONES) == NULL)
17271 			new_ilm->ilm_notify_driver = B_TRUE;
17272 
17273 		/* Add to the to_ill's list */
17274 		new_ilm->ilm_next = to_ill->ill_ilm;
17275 		to_ill->ill_ilm = new_ilm;
17276 		new_ilm->ilm_is_new = B_TRUE;
17277 
17278 		/*
17279 		 * Revert multicast filter state to (EXCLUDE, NULL)
17280 		 */
17281 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17282 		CLEAR_SLIST(new_ilm->ilm_filter);
17283 
17284 		/*
17285 		 * Delete only if we have allocated a new ilm.
17286 		 */
17287 		if (new_ilm != ilm) {
17288 delete_ilm:
17289 			if (from_ill->ill_ilm_walker_cnt == 0) {
17290 				/* Remove from the list */
17291 				*ilmp = ilm->ilm_next;
17292 				ilm->ilm_next = NULL;
17293 				FREE_SLIST(ilm->ilm_filter);
17294 				FREE_SLIST(ilm->ilm_pendsrcs);
17295 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17296 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17297 				mi_free((char *)ilm);
17298 			} else {
17299 				ilm->ilm_flags |= ILM_DELETED;
17300 				from_ill->ill_ilm_cleanup_reqd = 1;
17301 				ilmp = &ilm->ilm_next;
17302 			}
17303 		}
17304 	}
17305 }
17306 
17307 static uint_t
17308 ipif_get_id(ill_t *ill, uint_t id)
17309 {
17310 	uint_t	unit;
17311 	ipif_t	*tipif;
17312 	boolean_t found = B_FALSE;
17313 	ip_stack_t	*ipst = ill->ill_ipst;
17314 
17315 	/*
17316 	 * During failback, we want to go back to the same id
17317 	 * instead of the smallest id so that the original
17318 	 * configuration is maintained. id is non-zero in that
17319 	 * case.
17320 	 */
17321 	if (id != 0) {
17322 		/*
17323 		 * While failing back, if we still have an ipif with
17324 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17325 		 * as soon as we return from this function. It was
17326 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17327 		 * we can choose the smallest id. Thus we return zero
17328 		 * in that case ignoring the hint.
17329 		 */
17330 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17331 			return (0);
17332 		for (tipif = ill->ill_ipif; tipif != NULL;
17333 		    tipif = tipif->ipif_next) {
17334 			if (tipif->ipif_id == id) {
17335 				found = B_TRUE;
17336 				break;
17337 			}
17338 		}
17339 		/*
17340 		 * If somebody already plumbed another logical
17341 		 * with the same id, we won't be able to find it.
17342 		 */
17343 		if (!found)
17344 			return (id);
17345 	}
17346 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17347 		found = B_FALSE;
17348 		for (tipif = ill->ill_ipif; tipif != NULL;
17349 		    tipif = tipif->ipif_next) {
17350 			if (tipif->ipif_id == unit) {
17351 				found = B_TRUE;
17352 				break;
17353 			}
17354 		}
17355 		if (!found)
17356 			break;
17357 	}
17358 	return (unit);
17359 }
17360 
17361 /* ARGSUSED */
17362 static int
17363 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17364     ipif_t **rep_ipif_ptr)
17365 {
17366 	ill_t	*from_ill;
17367 	ipif_t	*rep_ipif;
17368 	uint_t	unit;
17369 	int err = 0;
17370 	ipif_t	*to_ipif;
17371 	struct iocblk	*iocp;
17372 	boolean_t failback_cmd;
17373 	boolean_t remove_ipif;
17374 	int	rc;
17375 	ip_stack_t	*ipst;
17376 
17377 	ASSERT(IAM_WRITER_ILL(to_ill));
17378 	ASSERT(IAM_WRITER_IPIF(ipif));
17379 
17380 	iocp = (struct iocblk *)mp->b_rptr;
17381 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17382 	remove_ipif = B_FALSE;
17383 
17384 	from_ill = ipif->ipif_ill;
17385 	ipst = from_ill->ill_ipst;
17386 
17387 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17388 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17389 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17390 
17391 	/*
17392 	 * Don't move LINK LOCAL addresses as they are tied to
17393 	 * physical interface.
17394 	 */
17395 	if (from_ill->ill_isv6 &&
17396 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17397 		ipif->ipif_was_up = B_FALSE;
17398 		IPIF_UNMARK_MOVING(ipif);
17399 		return (0);
17400 	}
17401 
17402 	/*
17403 	 * We set the ipif_id to maximum so that the search for
17404 	 * ipif_id will pick the lowest number i.e 0 in the
17405 	 * following 2 cases :
17406 	 *
17407 	 * 1) We have a replacement ipif at the head of to_ill.
17408 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17409 	 *    on to_ill and hence the MOVE might fail. We want to
17410 	 *    remove it only if we could move the ipif. Thus, by
17411 	 *    setting it to the MAX value, we make the search in
17412 	 *    ipif_get_id return the zeroth id.
17413 	 *
17414 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17415 	 *    we might just have a zero address plumbed on the ipif
17416 	 *    with zero id in the case of IPv4. We remove that while
17417 	 *    doing the failback. We want to remove it only if we
17418 	 *    could move the ipif. Thus, by setting it to the MAX
17419 	 *    value, we make the search in ipif_get_id return the
17420 	 *    zeroth id.
17421 	 *
17422 	 * Both (1) and (2) are done only when when we are moving
17423 	 * an ipif (either due to failover/failback) which originally
17424 	 * belonged to this interface i.e the ipif_orig_ifindex is
17425 	 * the same as to_ill's ifindex. This is needed so that
17426 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17427 	 * from B -> A (B is being removed from the group) and
17428 	 * FAILBACK from A -> B restores the original configuration.
17429 	 * Without the check for orig_ifindex, the second FAILOVER
17430 	 * could make the ipif belonging to B replace the A's zeroth
17431 	 * ipif and the subsequent failback re-creating the replacement
17432 	 * ipif again.
17433 	 *
17434 	 * NOTE : We created the replacement ipif when we did a
17435 	 * FAILOVER (See below). We could check for FAILBACK and
17436 	 * then look for replacement ipif to be removed. But we don't
17437 	 * want to do that because we wan't to allow the possibility
17438 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17439 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17440 	 * from B -> A.
17441 	 */
17442 	to_ipif = to_ill->ill_ipif;
17443 	if ((to_ill->ill_phyint->phyint_ifindex ==
17444 	    ipif->ipif_orig_ifindex) &&
17445 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17446 		ASSERT(to_ipif->ipif_id == 0);
17447 		remove_ipif = B_TRUE;
17448 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17449 	}
17450 	/*
17451 	 * Find the lowest logical unit number on the to_ill.
17452 	 * If we are failing back, try to get the original id
17453 	 * rather than the lowest one so that the original
17454 	 * configuration is maintained.
17455 	 *
17456 	 * XXX need a better scheme for this.
17457 	 */
17458 	if (failback_cmd) {
17459 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17460 	} else {
17461 		unit = ipif_get_id(to_ill, 0);
17462 	}
17463 
17464 	/* Reset back to zero in case we fail below */
17465 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17466 		to_ipif->ipif_id = 0;
17467 
17468 	if (unit == ipst->ips_ip_addrs_per_if) {
17469 		ipif->ipif_was_up = B_FALSE;
17470 		IPIF_UNMARK_MOVING(ipif);
17471 		return (EINVAL);
17472 	}
17473 
17474 	/*
17475 	 * ipif is ready to move from "from_ill" to "to_ill".
17476 	 *
17477 	 * 1) If we are moving ipif with id zero, create a
17478 	 *    replacement ipif for this ipif on from_ill. If this fails
17479 	 *    fail the MOVE operation.
17480 	 *
17481 	 * 2) Remove the replacement ipif on to_ill if any.
17482 	 *    We could remove the replacement ipif when we are moving
17483 	 *    the ipif with id zero. But what if somebody already
17484 	 *    unplumbed it ? Thus we always remove it if it is present.
17485 	 *    We want to do it only if we are sure we are going to
17486 	 *    move the ipif to to_ill which is why there are no
17487 	 *    returns due to error till ipif is linked to to_ill.
17488 	 *    Note that the first ipif that we failback will always
17489 	 *    be zero if it is present.
17490 	 */
17491 	if (ipif->ipif_id == 0) {
17492 		ipaddr_t inaddr_any = INADDR_ANY;
17493 
17494 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17495 		if (rep_ipif == NULL) {
17496 			ipif->ipif_was_up = B_FALSE;
17497 			IPIF_UNMARK_MOVING(ipif);
17498 			return (ENOMEM);
17499 		}
17500 		*rep_ipif = ipif_zero;
17501 		/*
17502 		 * Before we put the ipif on the list, store the addresses
17503 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17504 		 * assumes so. This logic is not any different from what
17505 		 * ipif_allocate does.
17506 		 */
17507 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17508 		    &rep_ipif->ipif_v6lcl_addr);
17509 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17510 		    &rep_ipif->ipif_v6src_addr);
17511 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17512 		    &rep_ipif->ipif_v6subnet);
17513 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17514 		    &rep_ipif->ipif_v6net_mask);
17515 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17516 		    &rep_ipif->ipif_v6brd_addr);
17517 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17518 		    &rep_ipif->ipif_v6pp_dst_addr);
17519 		/*
17520 		 * We mark IPIF_NOFAILOVER so that this can never
17521 		 * move.
17522 		 */
17523 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17524 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17525 		rep_ipif->ipif_replace_zero = B_TRUE;
17526 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17527 		    MUTEX_DEFAULT, NULL);
17528 		rep_ipif->ipif_id = 0;
17529 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17530 		rep_ipif->ipif_ill = from_ill;
17531 		rep_ipif->ipif_orig_ifindex =
17532 		    from_ill->ill_phyint->phyint_ifindex;
17533 		/* Insert at head */
17534 		rep_ipif->ipif_next = from_ill->ill_ipif;
17535 		from_ill->ill_ipif = rep_ipif;
17536 		/*
17537 		 * We don't really care to let apps know about
17538 		 * this interface.
17539 		 */
17540 	}
17541 
17542 	if (remove_ipif) {
17543 		/*
17544 		 * We set to a max value above for this case to get
17545 		 * id zero. ASSERT that we did get one.
17546 		 */
17547 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17548 		rep_ipif = to_ipif;
17549 		to_ill->ill_ipif = rep_ipif->ipif_next;
17550 		rep_ipif->ipif_next = NULL;
17551 		/*
17552 		 * If some apps scanned and find this interface,
17553 		 * it is time to let them know, so that they can
17554 		 * delete it.
17555 		 */
17556 
17557 		*rep_ipif_ptr = rep_ipif;
17558 	}
17559 
17560 	/* Get it out of the ILL interface list. */
17561 	ipif_remove(ipif, B_FALSE);
17562 
17563 	/* Assign the new ill */
17564 	ipif->ipif_ill = to_ill;
17565 	ipif->ipif_id = unit;
17566 	/* id has already been checked */
17567 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17568 	ASSERT(rc == 0);
17569 	/* Let SCTP update its list */
17570 	sctp_move_ipif(ipif, from_ill, to_ill);
17571 	/*
17572 	 * Handle the failover and failback of ipif_t between
17573 	 * ill_t that have differing maximum mtu values.
17574 	 */
17575 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17576 		if (ipif->ipif_saved_mtu == 0) {
17577 			/*
17578 			 * As this ipif_t is moving to an ill_t
17579 			 * that has a lower ill_max_mtu, its
17580 			 * ipif_mtu needs to be saved so it can
17581 			 * be restored during failback or during
17582 			 * failover to an ill_t which has a
17583 			 * higher ill_max_mtu.
17584 			 */
17585 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17586 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17587 		} else {
17588 			/*
17589 			 * The ipif_t is, once again, moving to
17590 			 * an ill_t that has a lower maximum mtu
17591 			 * value.
17592 			 */
17593 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17594 		}
17595 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17596 	    ipif->ipif_saved_mtu != 0) {
17597 		/*
17598 		 * The mtu of this ipif_t had to be reduced
17599 		 * during an earlier failover; this is an
17600 		 * opportunity for it to be increased (either as
17601 		 * part of another failover or a failback).
17602 		 */
17603 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17604 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17605 			ipif->ipif_saved_mtu = 0;
17606 		} else {
17607 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17608 		}
17609 	}
17610 
17611 	/*
17612 	 * We preserve all the other fields of the ipif including
17613 	 * ipif_saved_ire_mp. The routes that are saved here will
17614 	 * be recreated on the new interface and back on the old
17615 	 * interface when we move back.
17616 	 */
17617 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17618 
17619 	return (err);
17620 }
17621 
17622 static int
17623 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17624     int ifindex, ipif_t **rep_ipif_ptr)
17625 {
17626 	ipif_t *mipif;
17627 	ipif_t *ipif_next;
17628 	int err;
17629 
17630 	/*
17631 	 * We don't really try to MOVE back things if some of the
17632 	 * operations fail. The daemon will take care of moving again
17633 	 * later on.
17634 	 */
17635 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17636 		ipif_next = mipif->ipif_next;
17637 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17638 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17639 
17640 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17641 
17642 			/*
17643 			 * When the MOVE fails, it is the job of the
17644 			 * application to take care of this properly
17645 			 * i.e try again if it is ENOMEM.
17646 			 */
17647 			if (mipif->ipif_ill != from_ill) {
17648 				/*
17649 				 * ipif has moved.
17650 				 *
17651 				 * Move the multicast memberships associated
17652 				 * with this ipif to the new ill. For IPv6, we
17653 				 * do it once after all the ipifs are moved
17654 				 * (in ill_move) as they are not associated
17655 				 * with ipifs.
17656 				 *
17657 				 * We need to move the ilms as the ipif has
17658 				 * already been moved to a new ill even
17659 				 * in the case of errors. Neither
17660 				 * ilm_free(ipif) will find the ilm
17661 				 * when somebody unplumbs this ipif nor
17662 				 * ilm_delete(ilm) will be able to find the
17663 				 * ilm, if we don't move now.
17664 				 */
17665 				if (!from_ill->ill_isv6)
17666 					ilm_move_v4(from_ill, to_ill, mipif);
17667 			}
17668 
17669 			if (err != 0)
17670 				return (err);
17671 		}
17672 	}
17673 	return (0);
17674 }
17675 
17676 static int
17677 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17678 {
17679 	int ifindex;
17680 	int err;
17681 	struct iocblk	*iocp;
17682 	ipif_t	*ipif;
17683 	ipif_t *rep_ipif_ptr = NULL;
17684 	ipif_t	*from_ipif = NULL;
17685 	boolean_t check_rep_if = B_FALSE;
17686 	ip_stack_t	*ipst = from_ill->ill_ipst;
17687 
17688 	iocp = (struct iocblk *)mp->b_rptr;
17689 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17690 		/*
17691 		 * Move everything pointing at from_ill to to_ill.
17692 		 * We acheive this by passing in 0 as ifindex.
17693 		 */
17694 		ifindex = 0;
17695 	} else {
17696 		/*
17697 		 * Move everything pointing at from_ill whose original
17698 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17699 		 * We acheive this by passing in ifindex rather than 0.
17700 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17701 		 */
17702 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17703 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17704 	}
17705 
17706 	/*
17707 	 * Determine if there is at least one ipif that would move from
17708 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17709 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17710 	 * the move, in which case we need to quiesce the replacement ipif also.
17711 	 */
17712 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17713 	    from_ipif = from_ipif->ipif_next) {
17714 		if (((ifindex == 0) ||
17715 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17716 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17717 			check_rep_if = B_TRUE;
17718 			break;
17719 		}
17720 	}
17721 
17722 
17723 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17724 
17725 	GRAB_ILL_LOCKS(from_ill, to_ill);
17726 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17727 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17728 		    mp, ILL_MOVE_OK);
17729 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17730 		return (EINPROGRESS);
17731 	}
17732 
17733 	/* Check if the replacement ipif is quiescent to delete */
17734 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17735 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17736 		to_ill->ill_ipif->ipif_state_flags |=
17737 		    IPIF_MOVING | IPIF_CHANGING;
17738 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17739 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17740 			    mp, ILL_MOVE_OK);
17741 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17742 			return (EINPROGRESS);
17743 		}
17744 	}
17745 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17746 
17747 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17748 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17749 	GRAB_ILL_LOCKS(from_ill, to_ill);
17750 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17751 
17752 	/* ilm_move is done inside ipif_move for IPv4 */
17753 	if (err == 0 && from_ill->ill_isv6)
17754 		ilm_move_v6(from_ill, to_ill, ifindex);
17755 
17756 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17757 	rw_exit(&ipst->ips_ill_g_lock);
17758 
17759 	/*
17760 	 * send rts messages and multicast messages.
17761 	 */
17762 	if (rep_ipif_ptr != NULL) {
17763 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17764 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17765 			rep_ipif_ptr->ipif_recovery_id = 0;
17766 		}
17767 		ip_rts_ifmsg(rep_ipif_ptr);
17768 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17769 #ifdef DEBUG
17770 		ipif_trace_cleanup(rep_ipif_ptr);
17771 #endif
17772 		mi_free(rep_ipif_ptr);
17773 	}
17774 
17775 	conn_move_ill(from_ill, to_ill, ifindex);
17776 
17777 	return (err);
17778 }
17779 
17780 /*
17781  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17782  * Also checks for the validity of the arguments.
17783  * Note: We are already exclusive inside the from group.
17784  * It is upto the caller to release refcnt on the to_ill's.
17785  */
17786 static int
17787 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17788     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17789 {
17790 	int dst_index;
17791 	ipif_t *ipif_v4, *ipif_v6;
17792 	struct lifreq *lifr;
17793 	mblk_t *mp1;
17794 	boolean_t exists;
17795 	sin_t	*sin;
17796 	int	err = 0;
17797 	ip_stack_t	*ipst;
17798 
17799 	if (CONN_Q(q))
17800 		ipst = CONNQ_TO_IPST(q);
17801 	else
17802 		ipst = ILLQ_TO_IPST(q);
17803 
17804 
17805 	if ((mp1 = mp->b_cont) == NULL)
17806 		return (EPROTO);
17807 
17808 	if ((mp1 = mp1->b_cont) == NULL)
17809 		return (EPROTO);
17810 
17811 	lifr = (struct lifreq *)mp1->b_rptr;
17812 	sin = (sin_t *)&lifr->lifr_addr;
17813 
17814 	/*
17815 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17816 	 * specific operations.
17817 	 */
17818 	if (sin->sin_family != AF_UNSPEC)
17819 		return (EINVAL);
17820 
17821 	/*
17822 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17823 	 * NULLs for the last 4 args and we know the lookup won't fail
17824 	 * with EINPROGRESS.
17825 	 */
17826 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17827 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17828 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17829 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17830 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17831 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17832 
17833 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17834 		return (ENXIO);
17835 
17836 	if (ipif_v4 != NULL) {
17837 		ASSERT(ipif_v4->ipif_refcnt != 0);
17838 		if (ipif_v4->ipif_id != 0) {
17839 			err = EINVAL;
17840 			goto done;
17841 		}
17842 
17843 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17844 		*ill_from_v4 = ipif_v4->ipif_ill;
17845 	}
17846 
17847 	if (ipif_v6 != NULL) {
17848 		ASSERT(ipif_v6->ipif_refcnt != 0);
17849 		if (ipif_v6->ipif_id != 0) {
17850 			err = EINVAL;
17851 			goto done;
17852 		}
17853 
17854 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17855 		*ill_from_v6 = ipif_v6->ipif_ill;
17856 	}
17857 
17858 	err = 0;
17859 	dst_index = lifr->lifr_movetoindex;
17860 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17861 	    q, mp, ip_process_ioctl, &err, ipst);
17862 	if (err != 0) {
17863 		/*
17864 		 * There could be only v6.
17865 		 */
17866 		if (err != ENXIO)
17867 			goto done;
17868 		err = 0;
17869 	}
17870 
17871 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17872 	    q, mp, ip_process_ioctl, &err, ipst);
17873 	if (err != 0) {
17874 		if (err != ENXIO)
17875 			goto done;
17876 		if (*ill_to_v4 == NULL) {
17877 			err = ENXIO;
17878 			goto done;
17879 		}
17880 		err = 0;
17881 	}
17882 
17883 	/*
17884 	 * If we have something to MOVE i.e "from" not NULL,
17885 	 * "to" should be non-NULL.
17886 	 */
17887 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17888 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17889 		err = EINVAL;
17890 	}
17891 
17892 done:
17893 	if (ipif_v4 != NULL)
17894 		ipif_refrele(ipif_v4);
17895 	if (ipif_v6 != NULL)
17896 		ipif_refrele(ipif_v6);
17897 	return (err);
17898 }
17899 
17900 /*
17901  * FAILOVER and FAILBACK are modelled as MOVE operations.
17902  *
17903  * We don't check whether the MOVE is within the same group or
17904  * not, because this ioctl can be used as a generic mechanism
17905  * to failover from interface A to B, though things will function
17906  * only if they are really part of the same group. Moreover,
17907  * all ipifs may be down and hence temporarily out of the group.
17908  *
17909  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17910  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17911  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17912  * have been deleted and there are no active references. Once quiescent the
17913  * ipif's are moved and brought up on the new ill.
17914  *
17915  * Normally the source ill and destination ill belong to the same IPMP group
17916  * and hence the same ipsq_t. In the event they don't belong to the same
17917  * same group the two ipsq's are first merged into one ipsq - that of the
17918  * to_ill. The multicast memberships on the source and destination ill cannot
17919  * change during the move operation since multicast joins/leaves also have to
17920  * execute on the same ipsq and are hence serialized.
17921  */
17922 /* ARGSUSED */
17923 int
17924 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17925     ip_ioctl_cmd_t *ipip, void *ifreq)
17926 {
17927 	ill_t *ill_to_v4 = NULL;
17928 	ill_t *ill_to_v6 = NULL;
17929 	ill_t *ill_from_v4 = NULL;
17930 	ill_t *ill_from_v6 = NULL;
17931 	int err = 0;
17932 
17933 	/*
17934 	 * setup from and to ill's, we can get EINPROGRESS only for
17935 	 * to_ill's.
17936 	 */
17937 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17938 	    &ill_to_v4, &ill_to_v6);
17939 
17940 	if (err != 0) {
17941 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17942 		goto done;
17943 	}
17944 
17945 	/*
17946 	 * nothing to do.
17947 	 */
17948 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17949 		goto done;
17950 	}
17951 
17952 	/*
17953 	 * nothing to do.
17954 	 */
17955 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17956 		goto done;
17957 	}
17958 
17959 	/*
17960 	 * Mark the ill as changing.
17961 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17962 	 * in ill_up_ipifs in case of error they are cleared below.
17963 	 */
17964 
17965 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17966 	if (ill_from_v4 != NULL)
17967 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17968 	if (ill_from_v6 != NULL)
17969 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17970 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17971 
17972 	/*
17973 	 * Make sure that both src and dst are
17974 	 * in the same syncq group. If not make it happen.
17975 	 * We are not holding any locks because we are the writer
17976 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17977 	 * to protect to_ipsq against changing.
17978 	 */
17979 	if (ill_from_v4 != NULL) {
17980 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17981 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17982 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17983 			    NULL, mp, q);
17984 			goto err_ret;
17985 
17986 		}
17987 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17988 	} else {
17989 
17990 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17991 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17992 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17993 			    NULL, mp, q);
17994 			goto err_ret;
17995 
17996 		}
17997 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17998 	}
17999 
18000 	/*
18001 	 * Now that the ipsq's have been merged and we are the writer
18002 	 * lets mark to_ill as changing as well.
18003 	 */
18004 
18005 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18006 	if (ill_to_v4 != NULL)
18007 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18008 	if (ill_to_v6 != NULL)
18009 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18010 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18011 
18012 	/*
18013 	 * Its ok for us to proceed with the move even if
18014 	 * ill_pending_mp is non null on one of the from ill's as the reply
18015 	 * should not be looking at the ipif, it should only care about the
18016 	 * ill itself.
18017 	 */
18018 
18019 	/*
18020 	 * lets move ipv4 first.
18021 	 */
18022 	if (ill_from_v4 != NULL) {
18023 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18024 		ill_from_v4->ill_move_in_progress = B_TRUE;
18025 		ill_to_v4->ill_move_in_progress = B_TRUE;
18026 		ill_to_v4->ill_move_peer = ill_from_v4;
18027 		ill_from_v4->ill_move_peer = ill_to_v4;
18028 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18029 	}
18030 
18031 	/*
18032 	 * Now lets move ipv6.
18033 	 */
18034 	if (err == 0 && ill_from_v6 != NULL) {
18035 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18036 		ill_from_v6->ill_move_in_progress = B_TRUE;
18037 		ill_to_v6->ill_move_in_progress = B_TRUE;
18038 		ill_to_v6->ill_move_peer = ill_from_v6;
18039 		ill_from_v6->ill_move_peer = ill_to_v6;
18040 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18041 	}
18042 
18043 err_ret:
18044 	/*
18045 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18046 	 * moved to become quiescent.
18047 	 */
18048 	if (err == EINPROGRESS) {
18049 		goto done;
18050 	}
18051 
18052 	/*
18053 	 * if err is set ill_up_ipifs will not be called
18054 	 * lets clear the flags.
18055 	 */
18056 
18057 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18058 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18059 	/*
18060 	 * Some of the clearing may be redundant. But it is simple
18061 	 * not making any extra checks.
18062 	 */
18063 	if (ill_from_v6 != NULL) {
18064 		ill_from_v6->ill_move_in_progress = B_FALSE;
18065 		ill_from_v6->ill_move_peer = NULL;
18066 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18067 	}
18068 	if (ill_from_v4 != NULL) {
18069 		ill_from_v4->ill_move_in_progress = B_FALSE;
18070 		ill_from_v4->ill_move_peer = NULL;
18071 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18072 	}
18073 	if (ill_to_v6 != NULL) {
18074 		ill_to_v6->ill_move_in_progress = B_FALSE;
18075 		ill_to_v6->ill_move_peer = NULL;
18076 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18077 	}
18078 	if (ill_to_v4 != NULL) {
18079 		ill_to_v4->ill_move_in_progress = B_FALSE;
18080 		ill_to_v4->ill_move_peer = NULL;
18081 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18082 	}
18083 
18084 	/*
18085 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18086 	 * Do this always to maintain proper state i.e even in case of errors.
18087 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18088 	 * we need not call on both v4 and v6 interfaces.
18089 	 */
18090 	if (ill_from_v4 != NULL) {
18091 		if ((ill_from_v4->ill_phyint->phyint_flags &
18092 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18093 			phyint_inactive(ill_from_v4->ill_phyint);
18094 		}
18095 	} else if (ill_from_v6 != NULL) {
18096 		if ((ill_from_v6->ill_phyint->phyint_flags &
18097 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18098 			phyint_inactive(ill_from_v6->ill_phyint);
18099 		}
18100 	}
18101 
18102 	if (ill_to_v4 != NULL) {
18103 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18104 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18105 		}
18106 	} else if (ill_to_v6 != NULL) {
18107 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18108 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18109 		}
18110 	}
18111 
18112 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18113 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18114 
18115 no_err:
18116 	/*
18117 	 * lets bring the interfaces up on the to_ill.
18118 	 */
18119 	if (err == 0) {
18120 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18121 		    q, mp);
18122 	}
18123 
18124 	if (err == 0) {
18125 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18126 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18127 
18128 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18129 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18130 	}
18131 done:
18132 
18133 	if (ill_to_v4 != NULL) {
18134 		ill_refrele(ill_to_v4);
18135 	}
18136 	if (ill_to_v6 != NULL) {
18137 		ill_refrele(ill_to_v6);
18138 	}
18139 
18140 	return (err);
18141 }
18142 
18143 static void
18144 ill_dl_down(ill_t *ill)
18145 {
18146 	/*
18147 	 * The ill is down; unbind but stay attached since we're still
18148 	 * associated with a PPA. If we have negotiated DLPI capabilites
18149 	 * with the data link service provider (IDS_OK) then reset them.
18150 	 * The interval between unbinding and rebinding is potentially
18151 	 * unbounded hence we cannot assume things will be the same.
18152 	 * The DLPI capabilities will be probed again when the data link
18153 	 * is brought up.
18154 	 */
18155 	mblk_t	*mp = ill->ill_unbind_mp;
18156 
18157 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18158 
18159 	ill->ill_unbind_mp = NULL;
18160 	if (mp != NULL) {
18161 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18162 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18163 		    ill->ill_name));
18164 		mutex_enter(&ill->ill_lock);
18165 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18166 		mutex_exit(&ill->ill_lock);
18167 		/*
18168 		 * Reset the capabilities if the negotiation is done or is
18169 		 * still in progress. Note that ill_capability_reset() will
18170 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18171 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18172 		 *
18173 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18174 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18175 		 * the capabilities renegotiation from happening.
18176 		 */
18177 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18178 			ill_capability_reset(ill);
18179 		ill->ill_capab_reneg = B_FALSE;
18180 
18181 		ill_dlpi_send(ill, mp);
18182 	}
18183 
18184 	/*
18185 	 * Toss all of our multicast memberships.  We could keep them, but
18186 	 * then we'd have to do bookkeeping of any joins and leaves performed
18187 	 * by the application while the the interface is down (we can't just
18188 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18189 	 * on a downed interface).
18190 	 */
18191 	ill_leave_multicast(ill);
18192 
18193 	mutex_enter(&ill->ill_lock);
18194 	ill->ill_dl_up = 0;
18195 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18196 	mutex_exit(&ill->ill_lock);
18197 }
18198 
18199 static void
18200 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18201 {
18202 	union DL_primitives *dlp;
18203 	t_uscalar_t prim;
18204 
18205 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18206 
18207 	dlp = (union DL_primitives *)mp->b_rptr;
18208 	prim = dlp->dl_primitive;
18209 
18210 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18211 	    dl_primstr(prim), prim, ill->ill_name));
18212 
18213 	switch (prim) {
18214 	case DL_PHYS_ADDR_REQ:
18215 	{
18216 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18217 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18218 		break;
18219 	}
18220 	case DL_BIND_REQ:
18221 		mutex_enter(&ill->ill_lock);
18222 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18223 		mutex_exit(&ill->ill_lock);
18224 		break;
18225 	}
18226 
18227 	/*
18228 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18229 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18230 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18231 	 */
18232 	mutex_enter(&ill->ill_lock);
18233 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18234 	    (prim == DL_UNBIND_REQ)) {
18235 		ill->ill_dlpi_pending = prim;
18236 	}
18237 	mutex_exit(&ill->ill_lock);
18238 
18239 	putnext(ill->ill_wq, mp);
18240 }
18241 
18242 /*
18243  * Helper function for ill_dlpi_send().
18244  */
18245 /* ARGSUSED */
18246 static void
18247 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18248 {
18249 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18250 }
18251 
18252 /*
18253  * Send a DLPI control message to the driver but make sure there
18254  * is only one outstanding message. Uses ill_dlpi_pending to tell
18255  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18256  * when an ACK or a NAK is received to process the next queued message.
18257  */
18258 void
18259 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18260 {
18261 	mblk_t **mpp;
18262 
18263 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18264 
18265 	/*
18266 	 * To ensure that any DLPI requests for current exclusive operation
18267 	 * are always completely sent before any DLPI messages for other
18268 	 * operations, require writer access before enqueuing.
18269 	 */
18270 	if (!IAM_WRITER_ILL(ill)) {
18271 		ill_refhold(ill);
18272 		/* qwriter_ip() does the ill_refrele() */
18273 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18274 		    NEW_OP, B_TRUE);
18275 		return;
18276 	}
18277 
18278 	mutex_enter(&ill->ill_lock);
18279 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18280 		/* Must queue message. Tail insertion */
18281 		mpp = &ill->ill_dlpi_deferred;
18282 		while (*mpp != NULL)
18283 			mpp = &((*mpp)->b_next);
18284 
18285 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18286 		    ill->ill_name));
18287 
18288 		*mpp = mp;
18289 		mutex_exit(&ill->ill_lock);
18290 		return;
18291 	}
18292 	mutex_exit(&ill->ill_lock);
18293 	ill_dlpi_dispatch(ill, mp);
18294 }
18295 
18296 /*
18297  * Send all deferred DLPI messages without waiting for their ACKs.
18298  */
18299 void
18300 ill_dlpi_send_deferred(ill_t *ill)
18301 {
18302 	mblk_t *mp, *nextmp;
18303 
18304 	/*
18305 	 * Clear ill_dlpi_pending so that the message is not queued in
18306 	 * ill_dlpi_send().
18307 	 */
18308 	mutex_enter(&ill->ill_lock);
18309 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18310 	mp = ill->ill_dlpi_deferred;
18311 	ill->ill_dlpi_deferred = NULL;
18312 	mutex_exit(&ill->ill_lock);
18313 
18314 	for (; mp != NULL; mp = nextmp) {
18315 		nextmp = mp->b_next;
18316 		mp->b_next = NULL;
18317 		ill_dlpi_send(ill, mp);
18318 	}
18319 }
18320 
18321 /*
18322  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18323  */
18324 boolean_t
18325 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18326 {
18327 	t_uscalar_t pending;
18328 
18329 	mutex_enter(&ill->ill_lock);
18330 	if (ill->ill_dlpi_pending == prim) {
18331 		mutex_exit(&ill->ill_lock);
18332 		return (B_TRUE);
18333 	}
18334 
18335 	/*
18336 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18337 	 * without waiting, so don't print any warnings in that case.
18338 	 */
18339 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18340 		mutex_exit(&ill->ill_lock);
18341 		return (B_FALSE);
18342 	}
18343 	pending = ill->ill_dlpi_pending;
18344 	mutex_exit(&ill->ill_lock);
18345 
18346 	if (pending == DL_PRIM_INVAL) {
18347 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18348 		    "received unsolicited ack for %s on %s\n",
18349 		    dl_primstr(prim), ill->ill_name);
18350 	} else {
18351 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18352 		    "received unexpected ack for %s on %s (expecting %s)\n",
18353 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18354 	}
18355 	return (B_FALSE);
18356 }
18357 
18358 /*
18359  * Called when an DLPI control message has been acked or nacked to
18360  * send down the next queued message (if any).
18361  */
18362 void
18363 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18364 {
18365 	mblk_t *mp;
18366 
18367 	ASSERT(IAM_WRITER_ILL(ill));
18368 	mutex_enter(&ill->ill_lock);
18369 
18370 	ASSERT(prim != DL_PRIM_INVAL);
18371 	ASSERT(ill->ill_dlpi_pending == prim);
18372 
18373 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18374 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18375 
18376 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18377 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18378 		cv_signal(&ill->ill_cv);
18379 		mutex_exit(&ill->ill_lock);
18380 		return;
18381 	}
18382 
18383 	ill->ill_dlpi_deferred = mp->b_next;
18384 	mp->b_next = NULL;
18385 	mutex_exit(&ill->ill_lock);
18386 
18387 	ill_dlpi_dispatch(ill, mp);
18388 }
18389 
18390 void
18391 conn_delete_ire(conn_t *connp, caddr_t arg)
18392 {
18393 	ipif_t	*ipif = (ipif_t *)arg;
18394 	ire_t	*ire;
18395 
18396 	/*
18397 	 * Look at the cached ires on conns which has pointers to ipifs.
18398 	 * We just call ire_refrele which clears up the reference
18399 	 * to ire. Called when a conn closes. Also called from ipif_free
18400 	 * to cleanup indirect references to the stale ipif via the cached ire.
18401 	 */
18402 	mutex_enter(&connp->conn_lock);
18403 	ire = connp->conn_ire_cache;
18404 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18405 		connp->conn_ire_cache = NULL;
18406 		mutex_exit(&connp->conn_lock);
18407 		IRE_REFRELE_NOTR(ire);
18408 		return;
18409 	}
18410 	mutex_exit(&connp->conn_lock);
18411 
18412 }
18413 
18414 /*
18415  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18416  * of IREs. Those IREs may have been previously cached in the conn structure.
18417  * This ipcl_walk() walker function releases all references to such IREs based
18418  * on the condemned flag.
18419  */
18420 /* ARGSUSED */
18421 void
18422 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18423 {
18424 	ire_t	*ire;
18425 
18426 	mutex_enter(&connp->conn_lock);
18427 	ire = connp->conn_ire_cache;
18428 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18429 		connp->conn_ire_cache = NULL;
18430 		mutex_exit(&connp->conn_lock);
18431 		IRE_REFRELE_NOTR(ire);
18432 		return;
18433 	}
18434 	mutex_exit(&connp->conn_lock);
18435 }
18436 
18437 /*
18438  * Take down a specific interface, but don't lose any information about it.
18439  * Also delete interface from its interface group (ifgrp).
18440  * (Always called as writer.)
18441  * This function goes through the down sequence even if the interface is
18442  * already down. There are 2 reasons.
18443  * a. Currently we permit interface routes that depend on down interfaces
18444  *    to be added. This behaviour itself is questionable. However it appears
18445  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18446  *    time. We go thru the cleanup in order to remove these routes.
18447  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18448  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18449  *    down, but we need to cleanup i.e. do ill_dl_down and
18450  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18451  *
18452  * IP-MT notes:
18453  *
18454  * Model of reference to interfaces.
18455  *
18456  * The following members in ipif_t track references to the ipif.
18457  *	int     ipif_refcnt;    Active reference count
18458  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18459  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18460  *
18461  * The following members in ill_t track references to the ill.
18462  *	int             ill_refcnt;     active refcnt
18463  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18464  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18465  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18466  *
18467  * Reference to an ipif or ill can be obtained in any of the following ways.
18468  *
18469  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18470  * Pointers to ipif / ill from other data structures viz ire and conn.
18471  * Implicit reference to the ipif / ill by holding a reference to the ire.
18472  *
18473  * The ipif/ill lookup functions return a reference held ipif / ill.
18474  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18475  * This is a purely dynamic reference count associated with threads holding
18476  * references to the ipif / ill. Pointers from other structures do not
18477  * count towards this reference count.
18478  *
18479  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18480  * associated with the ipif/ill. This is incremented whenever a new
18481  * ire is created referencing the ipif/ill. This is done atomically inside
18482  * ire_add_v[46] where the ire is actually added to the ire hash table.
18483  * The count is decremented in ire_inactive where the ire is destroyed.
18484  *
18485  * nce's reference ill's thru nce_ill and the count of nce's associated with
18486  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18487  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18488  * table. Similarly it is decremented in ndp_inactive() where the nce
18489  * is destroyed.
18490  *
18491  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18492  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18493  * in ilm_walker_cleanup() or ilm_delete().
18494  *
18495  * Flow of ioctls involving interface down/up
18496  *
18497  * The following is the sequence of an attempt to set some critical flags on an
18498  * up interface.
18499  * ip_sioctl_flags
18500  * ipif_down
18501  * wait for ipif to be quiescent
18502  * ipif_down_tail
18503  * ip_sioctl_flags_tail
18504  *
18505  * All set ioctls that involve down/up sequence would have a skeleton similar
18506  * to the above. All the *tail functions are called after the refcounts have
18507  * dropped to the appropriate values.
18508  *
18509  * The mechanism to quiesce an ipif is as follows.
18510  *
18511  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18512  * on the ipif. Callers either pass a flag requesting wait or the lookup
18513  *  functions will return NULL.
18514  *
18515  * Delete all ires referencing this ipif
18516  *
18517  * Any thread attempting to do an ipif_refhold on an ipif that has been
18518  * obtained thru a cached pointer will first make sure that
18519  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18520  * increment the refcount.
18521  *
18522  * The above guarantees that the ipif refcount will eventually come down to
18523  * zero and the ipif will quiesce, once all threads that currently hold a
18524  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18525  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18526  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18527  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18528  * in ip.h
18529  *
18530  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18531  *
18532  * Threads trying to lookup an ipif or ill can pass a flag requesting
18533  * wait and restart if the ipif / ill cannot be looked up currently.
18534  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18535  * failure if the ipif is currently undergoing an exclusive operation, and
18536  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18537  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18538  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18539  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18540  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18541  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18542  * until we release the ipsq_lock, even though the the ill/ipif state flags
18543  * can change after we drop the ill_lock.
18544  *
18545  * An attempt to send out a packet using an ipif that is currently
18546  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18547  * operation and restart it later when the exclusive condition on the ipif ends.
18548  * This is an example of not passing the wait flag to the lookup functions. For
18549  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18550  * out a multicast packet on that ipif will fail while the ipif is
18551  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18552  * currently IPIF_CHANGING will also fail.
18553  */
18554 int
18555 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18556 {
18557 	ill_t		*ill = ipif->ipif_ill;
18558 	phyint_t	*phyi;
18559 	conn_t		*connp;
18560 	boolean_t	success;
18561 	boolean_t	ipif_was_up = B_FALSE;
18562 	ip_stack_t	*ipst = ill->ill_ipst;
18563 
18564 	ASSERT(IAM_WRITER_IPIF(ipif));
18565 
18566 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18567 
18568 	if (ipif->ipif_flags & IPIF_UP) {
18569 		mutex_enter(&ill->ill_lock);
18570 		ipif->ipif_flags &= ~IPIF_UP;
18571 		ASSERT(ill->ill_ipif_up_count > 0);
18572 		--ill->ill_ipif_up_count;
18573 		mutex_exit(&ill->ill_lock);
18574 		ipif_was_up = B_TRUE;
18575 		/* Update status in SCTP's list */
18576 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18577 	}
18578 
18579 	/*
18580 	 * Blow away memberships we established in ipif_multicast_up().
18581 	 */
18582 	ipif_multicast_down(ipif);
18583 
18584 	/*
18585 	 * Remove from the mapping for __sin6_src_id. We insert only
18586 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18587 	 * stored as mapped addresses, we need to check for mapped
18588 	 * INADDR_ANY also.
18589 	 */
18590 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18591 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18592 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18593 		int err;
18594 
18595 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18596 		    ipif->ipif_zoneid, ipst);
18597 		if (err != 0) {
18598 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18599 		}
18600 	}
18601 
18602 	/*
18603 	 * Before we delete the ill from the group (if any), we need
18604 	 * to make sure that we delete all the routes dependent on
18605 	 * this and also any ipifs dependent on this ipif for
18606 	 * source address. We need to do before we delete from
18607 	 * the group because
18608 	 *
18609 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18610 	 *
18611 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18612 	 *    for re-doing source address selection. Note that
18613 	 *    ipif_select_source[_v6] called from
18614 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18615 	 *    because we have already marked down here i.e cleared
18616 	 *    IPIF_UP.
18617 	 */
18618 	if (ipif->ipif_isv6) {
18619 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18620 		    ipst);
18621 	} else {
18622 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18623 		    ipst);
18624 	}
18625 
18626 	/*
18627 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18628 	 * ires have been deleted above. Otherwise a thread could end up
18629 	 * caching an ire in a conn after we have finished the cleanup of the
18630 	 * conn. The caching is done after making sure that the ire is not yet
18631 	 * condemned. Also documented in the block comment above ip_output
18632 	 */
18633 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18634 	/* Also, delete the ires cached in SCTP */
18635 	sctp_ire_cache_flush(ipif);
18636 
18637 	/*
18638 	 * Update any other ipifs which have used "our" local address as
18639 	 * a source address. This entails removing and recreating IRE_INTERFACE
18640 	 * entries for such ipifs.
18641 	 */
18642 	if (ipif->ipif_isv6)
18643 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18644 	else
18645 		ipif_update_other_ipifs(ipif, ill->ill_group);
18646 
18647 	if (ipif_was_up) {
18648 		/*
18649 		 * Check whether it is last ipif to leave this group.
18650 		 * If this is the last ipif to leave, we should remove
18651 		 * this ill from the group as ipif_select_source will not
18652 		 * be able to find any useful ipifs if this ill is selected
18653 		 * for load balancing.
18654 		 *
18655 		 * For nameless groups, we should call ifgrp_delete if this
18656 		 * belongs to some group. As this ipif is going down, we may
18657 		 * need to reconstruct groups.
18658 		 */
18659 		phyi = ill->ill_phyint;
18660 		/*
18661 		 * If the phyint_groupname_len is 0, it may or may not
18662 		 * be in the nameless group. If the phyint_groupname_len is
18663 		 * not 0, then this ill should be part of some group.
18664 		 * As we always insert this ill in the group if
18665 		 * phyint_groupname_len is not zero when the first ipif
18666 		 * comes up (in ipif_up_done), it should be in a group
18667 		 * when the namelen is not 0.
18668 		 *
18669 		 * NOTE : When we delete the ill from the group,it will
18670 		 * blow away all the IRE_CACHES pointing either at this ipif or
18671 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18672 		 * should be pointing at this ill.
18673 		 */
18674 		ASSERT(phyi->phyint_groupname_len == 0 ||
18675 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18676 
18677 		if (phyi->phyint_groupname_len != 0) {
18678 			if (ill->ill_ipif_up_count == 0)
18679 				illgrp_delete(ill);
18680 		}
18681 
18682 		/*
18683 		 * If we have deleted some of the broadcast ires associated
18684 		 * with this ipif, we need to re-nominate somebody else if
18685 		 * the ires that we deleted were the nominated ones.
18686 		 */
18687 		if (ill->ill_group != NULL && !ill->ill_isv6)
18688 			ipif_renominate_bcast(ipif);
18689 	}
18690 
18691 	/*
18692 	 * neighbor-discovery or arp entries for this interface.
18693 	 */
18694 	ipif_ndp_down(ipif);
18695 
18696 	/*
18697 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18698 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18699 	 * and ill_delete -> ipif_free -> ipif_down
18700 	 */
18701 	if (mp == NULL) {
18702 		ASSERT(q == NULL);
18703 		return (0);
18704 	}
18705 
18706 	if (CONN_Q(q)) {
18707 		connp = Q_TO_CONN(q);
18708 		mutex_enter(&connp->conn_lock);
18709 	} else {
18710 		connp = NULL;
18711 	}
18712 	mutex_enter(&ill->ill_lock);
18713 	/*
18714 	 * Are there any ire's pointing to this ipif that are still active ?
18715 	 * If this is the last ipif going down, are there any ire's pointing
18716 	 * to this ill that are still active ?
18717 	 */
18718 	if (ipif_is_quiescent(ipif)) {
18719 		mutex_exit(&ill->ill_lock);
18720 		if (connp != NULL)
18721 			mutex_exit(&connp->conn_lock);
18722 		return (0);
18723 	}
18724 
18725 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18726 	    ill->ill_name, (void *)ill));
18727 	/*
18728 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18729 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18730 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18731 	 */
18732 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18733 	if (!success) {
18734 		/* The conn is closing. So just return */
18735 		ASSERT(connp != NULL);
18736 		mutex_exit(&ill->ill_lock);
18737 		mutex_exit(&connp->conn_lock);
18738 		return (EINTR);
18739 	}
18740 
18741 	mutex_exit(&ill->ill_lock);
18742 	if (connp != NULL)
18743 		mutex_exit(&connp->conn_lock);
18744 	return (EINPROGRESS);
18745 }
18746 
18747 void
18748 ipif_down_tail(ipif_t *ipif)
18749 {
18750 	ill_t	*ill = ipif->ipif_ill;
18751 
18752 	/*
18753 	 * Skip any loopback interface (null wq).
18754 	 * If this is the last logical interface on the ill
18755 	 * have ill_dl_down tell the driver we are gone (unbind)
18756 	 * Note that lun 0 can ipif_down even though
18757 	 * there are other logical units that are up.
18758 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18759 	 */
18760 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18761 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18762 	    ill->ill_dl_up) {
18763 		ill_dl_down(ill);
18764 	}
18765 	ill->ill_logical_down = 0;
18766 
18767 	/*
18768 	 * Have to be after removing the routes in ipif_down_delete_ire.
18769 	 */
18770 	if (ipif->ipif_isv6) {
18771 		if (ill->ill_flags & ILLF_XRESOLV)
18772 			ipif_arp_down(ipif);
18773 	} else {
18774 		ipif_arp_down(ipif);
18775 	}
18776 
18777 	ip_rts_ifmsg(ipif);
18778 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18779 }
18780 
18781 /*
18782  * Bring interface logically down without bringing the physical interface
18783  * down e.g. when the netmask is changed. This avoids long lasting link
18784  * negotiations between an ethernet interface and a certain switches.
18785  */
18786 static int
18787 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18788 {
18789 	/*
18790 	 * The ill_logical_down flag is a transient flag. It is set here
18791 	 * and is cleared once the down has completed in ipif_down_tail.
18792 	 * This flag does not indicate whether the ill stream is in the
18793 	 * DL_BOUND state with the driver. Instead this flag is used by
18794 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18795 	 * the driver. The state of the ill stream i.e. whether it is
18796 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18797 	 */
18798 	ipif->ipif_ill->ill_logical_down = 1;
18799 	return (ipif_down(ipif, q, mp));
18800 }
18801 
18802 /*
18803  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18804  * If the usesrc client ILL is already part of a usesrc group or not,
18805  * in either case a ire_stq with the matching usesrc client ILL will
18806  * locate the IRE's that need to be deleted. We want IREs to be created
18807  * with the new source address.
18808  */
18809 static void
18810 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18811 {
18812 	ill_t	*ucill = (ill_t *)ill_arg;
18813 
18814 	ASSERT(IAM_WRITER_ILL(ucill));
18815 
18816 	if (ire->ire_stq == NULL)
18817 		return;
18818 
18819 	if ((ire->ire_type == IRE_CACHE) &&
18820 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18821 		ire_delete(ire);
18822 }
18823 
18824 /*
18825  * ire_walk routine to delete every IRE dependent on the interface
18826  * address that is going down.	(Always called as writer.)
18827  * Works for both v4 and v6.
18828  * In addition for checking for ire_ipif matches it also checks for
18829  * IRE_CACHE entries which have the same source address as the
18830  * disappearing ipif since ipif_select_source might have picked
18831  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18832  * care of any IRE_INTERFACE with the disappearing source address.
18833  */
18834 static void
18835 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18836 {
18837 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18838 	ill_t *ire_ill;
18839 	ill_t *ipif_ill;
18840 
18841 	ASSERT(IAM_WRITER_IPIF(ipif));
18842 	if (ire->ire_ipif == NULL)
18843 		return;
18844 
18845 	/*
18846 	 * For IPv4, we derive source addresses for an IRE from ipif's
18847 	 * belonging to the same IPMP group as the IRE's outgoing
18848 	 * interface.  If an IRE's outgoing interface isn't in the
18849 	 * same IPMP group as a particular ipif, then that ipif
18850 	 * couldn't have been used as a source address for this IRE.
18851 	 *
18852 	 * For IPv6, source addresses are only restricted to the IPMP group
18853 	 * if the IRE is for a link-local address or a multicast address.
18854 	 * Otherwise, source addresses for an IRE can be chosen from
18855 	 * interfaces other than the the outgoing interface for that IRE.
18856 	 *
18857 	 * For source address selection details, see ipif_select_source()
18858 	 * and ipif_select_source_v6().
18859 	 */
18860 	if (ire->ire_ipversion == IPV4_VERSION ||
18861 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18862 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18863 		ire_ill = ire->ire_ipif->ipif_ill;
18864 		ipif_ill = ipif->ipif_ill;
18865 
18866 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18867 			return;
18868 		}
18869 	}
18870 
18871 
18872 	if (ire->ire_ipif != ipif) {
18873 		/*
18874 		 * Look for a matching source address.
18875 		 */
18876 		if (ire->ire_type != IRE_CACHE)
18877 			return;
18878 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18879 			return;
18880 
18881 		if (ire->ire_ipversion == IPV4_VERSION) {
18882 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18883 				return;
18884 		} else {
18885 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18886 			    &ipif->ipif_v6lcl_addr))
18887 				return;
18888 		}
18889 		ire_delete(ire);
18890 		return;
18891 	}
18892 	/*
18893 	 * ire_delete() will do an ire_flush_cache which will delete
18894 	 * all ire_ipif matches
18895 	 */
18896 	ire_delete(ire);
18897 }
18898 
18899 /*
18900  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18901  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18902  * 2) when an interface is brought up or down (on that ill).
18903  * This ensures that the IRE_CACHE entries don't retain stale source
18904  * address selection results.
18905  */
18906 void
18907 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18908 {
18909 	ill_t	*ill = (ill_t *)ill_arg;
18910 	ill_t	*ipif_ill;
18911 
18912 	ASSERT(IAM_WRITER_ILL(ill));
18913 	/*
18914 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18915 	 * Hence this should be IRE_CACHE.
18916 	 */
18917 	ASSERT(ire->ire_type == IRE_CACHE);
18918 
18919 	/*
18920 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18921 	 * We are only interested in IRE_CACHES that has borrowed
18922 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18923 	 * for which we need to look at ire_ipif->ipif_ill match
18924 	 * with ill.
18925 	 */
18926 	ASSERT(ire->ire_ipif != NULL);
18927 	ipif_ill = ire->ire_ipif->ipif_ill;
18928 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18929 	    ipif_ill->ill_group == ill->ill_group)) {
18930 		ire_delete(ire);
18931 	}
18932 }
18933 
18934 /*
18935  * Delete all the ire whose stq references ill_arg.
18936  */
18937 static void
18938 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18939 {
18940 	ill_t	*ill = (ill_t *)ill_arg;
18941 	ill_t	*ire_ill;
18942 
18943 	ASSERT(IAM_WRITER_ILL(ill));
18944 	/*
18945 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18946 	 * Hence this should be IRE_CACHE.
18947 	 */
18948 	ASSERT(ire->ire_type == IRE_CACHE);
18949 
18950 	/*
18951 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18952 	 * matches ill. We are only interested in IRE_CACHES that
18953 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18954 	 * filtering here.
18955 	 */
18956 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18957 
18958 	if (ire_ill == ill)
18959 		ire_delete(ire);
18960 }
18961 
18962 /*
18963  * This is called when an ill leaves the group. We want to delete
18964  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18965  * pointing at ill.
18966  */
18967 static void
18968 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18969 {
18970 	ill_t	*ill = (ill_t *)ill_arg;
18971 
18972 	ASSERT(IAM_WRITER_ILL(ill));
18973 	ASSERT(ill->ill_group == NULL);
18974 	/*
18975 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18976 	 * Hence this should be IRE_CACHE.
18977 	 */
18978 	ASSERT(ire->ire_type == IRE_CACHE);
18979 	/*
18980 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18981 	 * matches ill. We are interested in both.
18982 	 */
18983 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18984 	    (ire->ire_ipif->ipif_ill == ill));
18985 
18986 	ire_delete(ire);
18987 }
18988 
18989 /*
18990  * Initiate deallocate of an IPIF. Always called as writer. Called by
18991  * ill_delete or ip_sioctl_removeif.
18992  */
18993 static void
18994 ipif_free(ipif_t *ipif)
18995 {
18996 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18997 
18998 	ASSERT(IAM_WRITER_IPIF(ipif));
18999 
19000 	if (ipif->ipif_recovery_id != 0)
19001 		(void) untimeout(ipif->ipif_recovery_id);
19002 	ipif->ipif_recovery_id = 0;
19003 
19004 	/* Remove conn references */
19005 	reset_conn_ipif(ipif);
19006 
19007 	/*
19008 	 * Make sure we have valid net and subnet broadcast ire's for the
19009 	 * other ipif's which share them with this ipif.
19010 	 */
19011 	if (!ipif->ipif_isv6)
19012 		ipif_check_bcast_ires(ipif);
19013 
19014 	/*
19015 	 * Take down the interface. We can be called either from ill_delete
19016 	 * or from ip_sioctl_removeif.
19017 	 */
19018 	(void) ipif_down(ipif, NULL, NULL);
19019 
19020 	/*
19021 	 * Now that the interface is down, there's no chance it can still
19022 	 * become a duplicate.  Cancel any timer that may have been set while
19023 	 * tearing down.
19024 	 */
19025 	if (ipif->ipif_recovery_id != 0)
19026 		(void) untimeout(ipif->ipif_recovery_id);
19027 	ipif->ipif_recovery_id = 0;
19028 
19029 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19030 	/* Remove pointers to this ill in the multicast routing tables */
19031 	reset_mrt_vif_ipif(ipif);
19032 	rw_exit(&ipst->ips_ill_g_lock);
19033 }
19034 
19035 /*
19036  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19037  * also ill_move().
19038  */
19039 static void
19040 ipif_free_tail(ipif_t *ipif)
19041 {
19042 	mblk_t	*mp;
19043 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19044 
19045 	/*
19046 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19047 	 */
19048 	mutex_enter(&ipif->ipif_saved_ire_lock);
19049 	mp = ipif->ipif_saved_ire_mp;
19050 	ipif->ipif_saved_ire_mp = NULL;
19051 	mutex_exit(&ipif->ipif_saved_ire_lock);
19052 	freemsg(mp);
19053 
19054 	/*
19055 	 * Need to hold both ill_g_lock and ill_lock while
19056 	 * inserting or removing an ipif from the linked list
19057 	 * of ipifs hanging off the ill.
19058 	 */
19059 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19060 
19061 	ASSERT(ilm_walk_ipif(ipif) == 0);
19062 
19063 #ifdef DEBUG
19064 	ipif_trace_cleanup(ipif);
19065 #endif
19066 
19067 	/* Ask SCTP to take it out of it list */
19068 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19069 
19070 	/* Get it out of the ILL interface list. */
19071 	ipif_remove(ipif, B_TRUE);
19072 	rw_exit(&ipst->ips_ill_g_lock);
19073 
19074 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19075 
19076 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19077 	ASSERT(ipif->ipif_recovery_id == 0);
19078 
19079 	/* Free the memory. */
19080 	mi_free(ipif);
19081 }
19082 
19083 /*
19084  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19085  * is zero.
19086  */
19087 void
19088 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19089 {
19090 	char	lbuf[LIFNAMSIZ];
19091 	char	*name;
19092 	size_t	name_len;
19093 
19094 	buf[0] = '\0';
19095 	name = ipif->ipif_ill->ill_name;
19096 	name_len = ipif->ipif_ill->ill_name_length;
19097 	if (ipif->ipif_id != 0) {
19098 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19099 		    ipif->ipif_id);
19100 		name = lbuf;
19101 		name_len = mi_strlen(name) + 1;
19102 	}
19103 	len -= 1;
19104 	buf[len] = '\0';
19105 	len = MIN(len, name_len);
19106 	bcopy(name, buf, len);
19107 }
19108 
19109 /*
19110  * Find an IPIF based on the name passed in.  Names can be of the
19111  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19112  * The <phys> string can have forms like <dev><#> (e.g., le0),
19113  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19114  * When there is no colon, the implied unit id is zero. <phys> must
19115  * correspond to the name of an ILL.  (May be called as writer.)
19116  */
19117 static ipif_t *
19118 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19119     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19120     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19121 {
19122 	char	*cp;
19123 	char	*endp;
19124 	long	id;
19125 	ill_t	*ill;
19126 	ipif_t	*ipif;
19127 	uint_t	ire_type;
19128 	boolean_t did_alloc = B_FALSE;
19129 	ipsq_t	*ipsq;
19130 
19131 	if (error != NULL)
19132 		*error = 0;
19133 
19134 	/*
19135 	 * If the caller wants to us to create the ipif, make sure we have a
19136 	 * valid zoneid
19137 	 */
19138 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19139 
19140 	if (namelen == 0) {
19141 		if (error != NULL)
19142 			*error = ENXIO;
19143 		return (NULL);
19144 	}
19145 
19146 	*exists = B_FALSE;
19147 	/* Look for a colon in the name. */
19148 	endp = &name[namelen];
19149 	for (cp = endp; --cp > name; ) {
19150 		if (*cp == IPIF_SEPARATOR_CHAR)
19151 			break;
19152 	}
19153 
19154 	if (*cp == IPIF_SEPARATOR_CHAR) {
19155 		/*
19156 		 * Reject any non-decimal aliases for logical
19157 		 * interfaces. Aliases with leading zeroes
19158 		 * are also rejected as they introduce ambiguity
19159 		 * in the naming of the interfaces.
19160 		 * In order to confirm with existing semantics,
19161 		 * and to not break any programs/script relying
19162 		 * on that behaviour, if<0>:0 is considered to be
19163 		 * a valid interface.
19164 		 *
19165 		 * If alias has two or more digits and the first
19166 		 * is zero, fail.
19167 		 */
19168 		if (&cp[2] < endp && cp[1] == '0') {
19169 			if (error != NULL)
19170 				*error = EINVAL;
19171 			return (NULL);
19172 		}
19173 	}
19174 
19175 	if (cp <= name) {
19176 		cp = endp;
19177 	} else {
19178 		*cp = '\0';
19179 	}
19180 
19181 	/*
19182 	 * Look up the ILL, based on the portion of the name
19183 	 * before the slash. ill_lookup_on_name returns a held ill.
19184 	 * Temporary to check whether ill exists already. If so
19185 	 * ill_lookup_on_name will clear it.
19186 	 */
19187 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19188 	    q, mp, func, error, &did_alloc, ipst);
19189 	if (cp != endp)
19190 		*cp = IPIF_SEPARATOR_CHAR;
19191 	if (ill == NULL)
19192 		return (NULL);
19193 
19194 	/* Establish the unit number in the name. */
19195 	id = 0;
19196 	if (cp < endp && *endp == '\0') {
19197 		/* If there was a colon, the unit number follows. */
19198 		cp++;
19199 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19200 			ill_refrele(ill);
19201 			if (error != NULL)
19202 				*error = ENXIO;
19203 			return (NULL);
19204 		}
19205 	}
19206 
19207 	GRAB_CONN_LOCK(q);
19208 	mutex_enter(&ill->ill_lock);
19209 	/* Now see if there is an IPIF with this unit number. */
19210 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19211 		if (ipif->ipif_id == id) {
19212 			if (zoneid != ALL_ZONES &&
19213 			    zoneid != ipif->ipif_zoneid &&
19214 			    ipif->ipif_zoneid != ALL_ZONES) {
19215 				mutex_exit(&ill->ill_lock);
19216 				RELEASE_CONN_LOCK(q);
19217 				ill_refrele(ill);
19218 				if (error != NULL)
19219 					*error = ENXIO;
19220 				return (NULL);
19221 			}
19222 			/*
19223 			 * The block comment at the start of ipif_down
19224 			 * explains the use of the macros used below
19225 			 */
19226 			if (IPIF_CAN_LOOKUP(ipif)) {
19227 				ipif_refhold_locked(ipif);
19228 				mutex_exit(&ill->ill_lock);
19229 				if (!did_alloc)
19230 					*exists = B_TRUE;
19231 				/*
19232 				 * Drop locks before calling ill_refrele
19233 				 * since it can potentially call into
19234 				 * ipif_ill_refrele_tail which can end up
19235 				 * in trying to acquire any lock.
19236 				 */
19237 				RELEASE_CONN_LOCK(q);
19238 				ill_refrele(ill);
19239 				return (ipif);
19240 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19241 				ipsq = ill->ill_phyint->phyint_ipsq;
19242 				mutex_enter(&ipsq->ipsq_lock);
19243 				mutex_exit(&ill->ill_lock);
19244 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19245 				mutex_exit(&ipsq->ipsq_lock);
19246 				RELEASE_CONN_LOCK(q);
19247 				ill_refrele(ill);
19248 				if (error != NULL)
19249 					*error = EINPROGRESS;
19250 				return (NULL);
19251 			}
19252 		}
19253 	}
19254 	RELEASE_CONN_LOCK(q);
19255 
19256 	if (!do_alloc) {
19257 		mutex_exit(&ill->ill_lock);
19258 		ill_refrele(ill);
19259 		if (error != NULL)
19260 			*error = ENXIO;
19261 		return (NULL);
19262 	}
19263 
19264 	/*
19265 	 * If none found, atomically allocate and return a new one.
19266 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19267 	 * to support "receive only" use of lo0:1 etc. as is still done
19268 	 * below as an initial guess.
19269 	 * However, this is now likely to be overriden later in ipif_up_done()
19270 	 * when we know for sure what address has been configured on the
19271 	 * interface, since we might have more than one loopback interface
19272 	 * with a loopback address, e.g. in the case of zones, and all the
19273 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19274 	 */
19275 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19276 		ire_type = IRE_LOOPBACK;
19277 	else
19278 		ire_type = IRE_LOCAL;
19279 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19280 	if (ipif != NULL)
19281 		ipif_refhold_locked(ipif);
19282 	else if (error != NULL)
19283 		*error = ENOMEM;
19284 	mutex_exit(&ill->ill_lock);
19285 	ill_refrele(ill);
19286 	return (ipif);
19287 }
19288 
19289 /*
19290  * This routine is called whenever a new address comes up on an ipif.  If
19291  * we are configured to respond to address mask requests, then we are supposed
19292  * to broadcast an address mask reply at this time.  This routine is also
19293  * called if we are already up, but a netmask change is made.  This is legal
19294  * but might not make the system manager very popular.	(May be called
19295  * as writer.)
19296  */
19297 void
19298 ipif_mask_reply(ipif_t *ipif)
19299 {
19300 	icmph_t	*icmph;
19301 	ipha_t	*ipha;
19302 	mblk_t	*mp;
19303 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19304 
19305 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19306 
19307 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19308 		return;
19309 
19310 	/* ICMP mask reply is IPv4 only */
19311 	ASSERT(!ipif->ipif_isv6);
19312 	/* ICMP mask reply is not for a loopback interface */
19313 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19314 
19315 	mp = allocb(REPLY_LEN, BPRI_HI);
19316 	if (mp == NULL)
19317 		return;
19318 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19319 
19320 	ipha = (ipha_t *)mp->b_rptr;
19321 	bzero(ipha, REPLY_LEN);
19322 	*ipha = icmp_ipha;
19323 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19324 	ipha->ipha_src = ipif->ipif_src_addr;
19325 	ipha->ipha_dst = ipif->ipif_brd_addr;
19326 	ipha->ipha_length = htons(REPLY_LEN);
19327 	ipha->ipha_ident = 0;
19328 
19329 	icmph = (icmph_t *)&ipha[1];
19330 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19331 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19332 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19333 
19334 	put(ipif->ipif_wq, mp);
19335 
19336 #undef	REPLY_LEN
19337 }
19338 
19339 /*
19340  * When the mtu in the ipif changes, we call this routine through ire_walk
19341  * to update all the relevant IREs.
19342  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19343  */
19344 static void
19345 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19346 {
19347 	ipif_t *ipif = (ipif_t *)ipif_arg;
19348 
19349 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19350 		return;
19351 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19352 }
19353 
19354 /*
19355  * When the mtu in the ill changes, we call this routine through ire_walk
19356  * to update all the relevant IREs.
19357  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19358  */
19359 void
19360 ill_mtu_change(ire_t *ire, char *ill_arg)
19361 {
19362 	ill_t	*ill = (ill_t *)ill_arg;
19363 
19364 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19365 		return;
19366 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19367 }
19368 
19369 /*
19370  * Join the ipif specific multicast groups.
19371  * Must be called after a mapping has been set up in the resolver.  (Always
19372  * called as writer.)
19373  */
19374 void
19375 ipif_multicast_up(ipif_t *ipif)
19376 {
19377 	int err, index;
19378 	ill_t *ill;
19379 
19380 	ASSERT(IAM_WRITER_IPIF(ipif));
19381 
19382 	ill = ipif->ipif_ill;
19383 	index = ill->ill_phyint->phyint_ifindex;
19384 
19385 	ip1dbg(("ipif_multicast_up\n"));
19386 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19387 		return;
19388 
19389 	if (ipif->ipif_isv6) {
19390 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19391 			return;
19392 
19393 		/* Join the all hosts multicast address */
19394 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19395 		/*
19396 		 * Passing B_TRUE means we have to join the multicast
19397 		 * membership on this interface even though this is
19398 		 * FAILED. If we join on a different one in the group,
19399 		 * we will not be able to delete the membership later
19400 		 * as we currently don't track where we join when we
19401 		 * join within the kernel unlike applications where
19402 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19403 		 * for more on this.
19404 		 */
19405 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19406 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19407 		if (err != 0) {
19408 			ip0dbg(("ipif_multicast_up: "
19409 			    "all_hosts_mcast failed %d\n",
19410 			    err));
19411 			return;
19412 		}
19413 		/*
19414 		 * Enable multicast for the solicited node multicast address
19415 		 */
19416 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19417 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19418 
19419 			ipv6_multi.s6_addr32[3] |=
19420 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19421 
19422 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19423 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19424 			    NULL);
19425 			if (err != 0) {
19426 				ip0dbg(("ipif_multicast_up: solicited MC"
19427 				    " failed %d\n", err));
19428 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19429 				    ill, ill->ill_phyint->phyint_ifindex,
19430 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19431 				return;
19432 			}
19433 		}
19434 	} else {
19435 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19436 			return;
19437 
19438 		/* Join the all hosts multicast address */
19439 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19440 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19441 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19442 		if (err) {
19443 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19444 			return;
19445 		}
19446 	}
19447 	ipif->ipif_multicast_up = 1;
19448 }
19449 
19450 /*
19451  * Blow away any multicast groups that we joined in ipif_multicast_up().
19452  * (Explicit memberships are blown away in ill_leave_multicast() when the
19453  * ill is brought down.)
19454  */
19455 static void
19456 ipif_multicast_down(ipif_t *ipif)
19457 {
19458 	int err;
19459 
19460 	ASSERT(IAM_WRITER_IPIF(ipif));
19461 
19462 	ip1dbg(("ipif_multicast_down\n"));
19463 	if (!ipif->ipif_multicast_up)
19464 		return;
19465 
19466 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19467 
19468 	if (!ipif->ipif_isv6) {
19469 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19470 		    B_TRUE);
19471 		if (err != 0)
19472 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19473 
19474 		ipif->ipif_multicast_up = 0;
19475 		return;
19476 	}
19477 
19478 	/*
19479 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19480 	 * we should look for ilms on this ill rather than the ones that have
19481 	 * been failed over here.  They are here temporarily. As
19482 	 * ipif_multicast_up has joined on this ill, we should delete only
19483 	 * from this ill.
19484 	 */
19485 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19486 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19487 	    B_TRUE, B_TRUE);
19488 	if (err != 0) {
19489 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19490 		    err));
19491 	}
19492 	/*
19493 	 * Disable multicast for the solicited node multicast address
19494 	 */
19495 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19496 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19497 
19498 		ipv6_multi.s6_addr32[3] |=
19499 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19500 
19501 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19502 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19503 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19504 
19505 		if (err != 0) {
19506 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19507 			    err));
19508 		}
19509 	}
19510 
19511 	ipif->ipif_multicast_up = 0;
19512 }
19513 
19514 /*
19515  * Used when an interface comes up to recreate any extra routes on this
19516  * interface.
19517  */
19518 static ire_t **
19519 ipif_recover_ire(ipif_t *ipif)
19520 {
19521 	mblk_t	*mp;
19522 	ire_t	**ipif_saved_irep;
19523 	ire_t	**irep;
19524 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19525 
19526 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19527 	    ipif->ipif_id));
19528 
19529 	mutex_enter(&ipif->ipif_saved_ire_lock);
19530 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19531 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19532 	if (ipif_saved_irep == NULL) {
19533 		mutex_exit(&ipif->ipif_saved_ire_lock);
19534 		return (NULL);
19535 	}
19536 
19537 	irep = ipif_saved_irep;
19538 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19539 		ire_t		*ire;
19540 		queue_t		*rfq;
19541 		queue_t		*stq;
19542 		ifrt_t		*ifrt;
19543 		uchar_t		*src_addr;
19544 		uchar_t		*gateway_addr;
19545 		ushort_t	type;
19546 
19547 		/*
19548 		 * When the ire was initially created and then added in
19549 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19550 		 * in the case of a traditional interface route, or as one of
19551 		 * the IRE_OFFSUBNET types (with the exception of
19552 		 * IRE_HOST types ire which is created by icmp_redirect() and
19553 		 * which we don't need to save or recover).  In the case where
19554 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19555 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19556 		 * to satisfy software like GateD and Sun Cluster which creates
19557 		 * routes using the the loopback interface's address as a
19558 		 * gateway.
19559 		 *
19560 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19561 		 * ire_create() will be called in the same way here as
19562 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19563 		 * the route looks like a traditional interface route (where
19564 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19565 		 * the saved ifrt->ifrt_type.  This means that in the case where
19566 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19567 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19568 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19569 		 */
19570 		ifrt = (ifrt_t *)mp->b_rptr;
19571 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19572 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19573 			rfq = NULL;
19574 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19575 			    ? ipif->ipif_rq : ipif->ipif_wq;
19576 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19577 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19578 			    : (uint8_t *)&ipif->ipif_src_addr;
19579 			gateway_addr = NULL;
19580 			type = ipif->ipif_net_type;
19581 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19582 			/* Recover multiroute broadcast IRE. */
19583 			rfq = ipif->ipif_rq;
19584 			stq = ipif->ipif_wq;
19585 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19586 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19587 			    : (uint8_t *)&ipif->ipif_src_addr;
19588 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19589 			type = ifrt->ifrt_type;
19590 		} else {
19591 			rfq = NULL;
19592 			stq = NULL;
19593 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19594 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19595 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19596 			type = ifrt->ifrt_type;
19597 		}
19598 
19599 		/*
19600 		 * Create a copy of the IRE with the saved address and netmask.
19601 		 */
19602 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19603 		    "0x%x/0x%x\n",
19604 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19605 		    ntohl(ifrt->ifrt_addr),
19606 		    ntohl(ifrt->ifrt_mask)));
19607 		ire = ire_create(
19608 		    (uint8_t *)&ifrt->ifrt_addr,
19609 		    (uint8_t *)&ifrt->ifrt_mask,
19610 		    src_addr,
19611 		    gateway_addr,
19612 		    &ifrt->ifrt_max_frag,
19613 		    NULL,
19614 		    rfq,
19615 		    stq,
19616 		    type,
19617 		    ipif,
19618 		    0,
19619 		    0,
19620 		    0,
19621 		    ifrt->ifrt_flags,
19622 		    &ifrt->ifrt_iulp_info,
19623 		    NULL,
19624 		    NULL,
19625 		    ipst);
19626 
19627 		if (ire == NULL) {
19628 			mutex_exit(&ipif->ipif_saved_ire_lock);
19629 			kmem_free(ipif_saved_irep,
19630 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19631 			return (NULL);
19632 		}
19633 
19634 		/*
19635 		 * Some software (for example, GateD and Sun Cluster) attempts
19636 		 * to create (what amount to) IRE_PREFIX routes with the
19637 		 * loopback address as the gateway.  This is primarily done to
19638 		 * set up prefixes with the RTF_REJECT flag set (for example,
19639 		 * when generating aggregate routes.)
19640 		 *
19641 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19642 		 * IRE_LOOPBACK, then we map the request into a
19643 		 * IRE_IF_NORESOLVER.
19644 		 */
19645 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19646 			ire->ire_type = IRE_IF_NORESOLVER;
19647 		/*
19648 		 * ire held by ire_add, will be refreled' towards the
19649 		 * the end of ipif_up_done
19650 		 */
19651 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19652 		*irep = ire;
19653 		irep++;
19654 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19655 	}
19656 	mutex_exit(&ipif->ipif_saved_ire_lock);
19657 	return (ipif_saved_irep);
19658 }
19659 
19660 /*
19661  * Used to set the netmask and broadcast address to default values when the
19662  * interface is brought up.  (Always called as writer.)
19663  */
19664 static void
19665 ipif_set_default(ipif_t *ipif)
19666 {
19667 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19668 
19669 	if (!ipif->ipif_isv6) {
19670 		/*
19671 		 * Interface holds an IPv4 address. Default
19672 		 * mask is the natural netmask.
19673 		 */
19674 		if (!ipif->ipif_net_mask) {
19675 			ipaddr_t	v4mask;
19676 
19677 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19678 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19679 		}
19680 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19681 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19682 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19683 		} else {
19684 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19685 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19686 		}
19687 		/*
19688 		 * NOTE: SunOS 4.X does this even if the broadcast address
19689 		 * has been already set thus we do the same here.
19690 		 */
19691 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19692 			ipaddr_t	v4addr;
19693 
19694 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19695 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19696 		}
19697 	} else {
19698 		/*
19699 		 * Interface holds an IPv6-only address.  Default
19700 		 * mask is all-ones.
19701 		 */
19702 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19703 			ipif->ipif_v6net_mask = ipv6_all_ones;
19704 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19705 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19706 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19707 		} else {
19708 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19709 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19710 		}
19711 	}
19712 }
19713 
19714 /*
19715  * Return 0 if this address can be used as local address without causing
19716  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19717  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19718  * Special checks are needed to allow the same IPv6 link-local address
19719  * on different ills.
19720  * TODO: allowing the same site-local address on different ill's.
19721  */
19722 int
19723 ip_addr_availability_check(ipif_t *new_ipif)
19724 {
19725 	in6_addr_t our_v6addr;
19726 	ill_t *ill;
19727 	ipif_t *ipif;
19728 	ill_walk_context_t ctx;
19729 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19730 
19731 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19732 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19733 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19734 
19735 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19736 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19737 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19738 		return (0);
19739 
19740 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19741 
19742 	if (new_ipif->ipif_isv6)
19743 		ill = ILL_START_WALK_V6(&ctx, ipst);
19744 	else
19745 		ill = ILL_START_WALK_V4(&ctx, ipst);
19746 
19747 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19748 		for (ipif = ill->ill_ipif; ipif != NULL;
19749 		    ipif = ipif->ipif_next) {
19750 			if ((ipif == new_ipif) ||
19751 			    !(ipif->ipif_flags & IPIF_UP) ||
19752 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19753 				continue;
19754 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19755 			    &our_v6addr)) {
19756 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19757 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19758 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19759 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19760 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19761 				    new_ipif->ipif_ill != ill)
19762 					continue;
19763 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19764 				    new_ipif->ipif_ill != ill)
19765 					continue;
19766 				else if (new_ipif->ipif_zoneid !=
19767 				    ipif->ipif_zoneid &&
19768 				    ipif->ipif_zoneid != ALL_ZONES &&
19769 				    IS_LOOPBACK(ill))
19770 					continue;
19771 				else if (new_ipif->ipif_ill == ill)
19772 					return (EADDRINUSE);
19773 				else
19774 					return (EADDRNOTAVAIL);
19775 			}
19776 		}
19777 	}
19778 
19779 	return (0);
19780 }
19781 
19782 /*
19783  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19784  * IREs for the ipif.
19785  * When the routine returns EINPROGRESS then mp has been consumed and
19786  * the ioctl will be acked from ip_rput_dlpi.
19787  */
19788 static int
19789 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19790 {
19791 	ill_t	*ill = ipif->ipif_ill;
19792 	boolean_t isv6 = ipif->ipif_isv6;
19793 	int	err = 0;
19794 	boolean_t success;
19795 
19796 	ASSERT(IAM_WRITER_IPIF(ipif));
19797 
19798 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19799 
19800 	/* Shouldn't get here if it is already up. */
19801 	if (ipif->ipif_flags & IPIF_UP)
19802 		return (EALREADY);
19803 
19804 	/* Skip arp/ndp for any loopback interface. */
19805 	if (ill->ill_wq != NULL) {
19806 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19807 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19808 
19809 		if (!ill->ill_dl_up) {
19810 			/*
19811 			 * ill_dl_up is not yet set. i.e. we are yet to
19812 			 * DL_BIND with the driver and this is the first
19813 			 * logical interface on the ill to become "up".
19814 			 * Tell the driver to get going (via DL_BIND_REQ).
19815 			 * Note that changing "significant" IFF_ flags
19816 			 * address/netmask etc cause a down/up dance, but
19817 			 * does not cause an unbind (DL_UNBIND) with the driver
19818 			 */
19819 			return (ill_dl_up(ill, ipif, mp, q));
19820 		}
19821 
19822 		/*
19823 		 * ipif_resolver_up may end up sending an
19824 		 * AR_INTERFACE_UP message to ARP, which would, in
19825 		 * turn send a DLPI message to the driver. ioctls are
19826 		 * serialized and so we cannot send more than one
19827 		 * interface up message at a time. If ipif_resolver_up
19828 		 * does send an interface up message to ARP, we get
19829 		 * EINPROGRESS and we will complete in ip_arp_done.
19830 		 */
19831 
19832 		ASSERT(connp != NULL || !CONN_Q(q));
19833 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19834 		if (connp != NULL)
19835 			mutex_enter(&connp->conn_lock);
19836 		mutex_enter(&ill->ill_lock);
19837 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19838 		mutex_exit(&ill->ill_lock);
19839 		if (connp != NULL)
19840 			mutex_exit(&connp->conn_lock);
19841 		if (!success)
19842 			return (EINTR);
19843 
19844 		/*
19845 		 * Crank up IPv6 neighbor discovery
19846 		 * Unlike ARP, this should complete when
19847 		 * ipif_ndp_up returns. However, for
19848 		 * ILLF_XRESOLV interfaces we also send a
19849 		 * AR_INTERFACE_UP to the external resolver.
19850 		 * That ioctl will complete in ip_rput.
19851 		 */
19852 		if (isv6) {
19853 			err = ipif_ndp_up(ipif);
19854 			if (err != 0) {
19855 				if (err != EINPROGRESS)
19856 					mp = ipsq_pending_mp_get(ipsq, &connp);
19857 				return (err);
19858 			}
19859 		}
19860 		/* Now, ARP */
19861 		err = ipif_resolver_up(ipif, Res_act_initial);
19862 		if (err == EINPROGRESS) {
19863 			/* We will complete it in ip_arp_done */
19864 			return (err);
19865 		}
19866 		mp = ipsq_pending_mp_get(ipsq, &connp);
19867 		ASSERT(mp != NULL);
19868 		if (err != 0)
19869 			return (err);
19870 	} else {
19871 		/*
19872 		 * Interfaces without underlying hardware don't do duplicate
19873 		 * address detection.
19874 		 */
19875 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19876 		ipif->ipif_addr_ready = 1;
19877 	}
19878 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19879 }
19880 
19881 /*
19882  * Perform a bind for the physical device.
19883  * When the routine returns EINPROGRESS then mp has been consumed and
19884  * the ioctl will be acked from ip_rput_dlpi.
19885  * Allocate an unbind message and save it until ipif_down.
19886  */
19887 static int
19888 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19889 {
19890 	areq_t	*areq;
19891 	mblk_t	*areq_mp = NULL;
19892 	mblk_t	*bind_mp = NULL;
19893 	mblk_t	*unbind_mp = NULL;
19894 	conn_t	*connp;
19895 	boolean_t success;
19896 	uint16_t sap_addr;
19897 
19898 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19899 	ASSERT(IAM_WRITER_ILL(ill));
19900 	ASSERT(mp != NULL);
19901 
19902 	/* Create a resolver cookie for ARP */
19903 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19904 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19905 		if (areq_mp == NULL)
19906 			return (ENOMEM);
19907 
19908 		freemsg(ill->ill_resolver_mp);
19909 		ill->ill_resolver_mp = areq_mp;
19910 		areq = (areq_t *)areq_mp->b_rptr;
19911 		sap_addr = ill->ill_sap;
19912 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19913 	}
19914 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19915 	    DL_BIND_REQ);
19916 	if (bind_mp == NULL)
19917 		goto bad;
19918 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19919 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19920 
19921 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19922 	if (unbind_mp == NULL)
19923 		goto bad;
19924 
19925 	/*
19926 	 * Record state needed to complete this operation when the
19927 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19928 	 */
19929 	ASSERT(WR(q)->q_next == NULL);
19930 	connp = Q_TO_CONN(q);
19931 
19932 	mutex_enter(&connp->conn_lock);
19933 	mutex_enter(&ipif->ipif_ill->ill_lock);
19934 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19935 	mutex_exit(&ipif->ipif_ill->ill_lock);
19936 	mutex_exit(&connp->conn_lock);
19937 	if (!success)
19938 		goto bad;
19939 
19940 	/*
19941 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19942 	 * the interface goes down.
19943 	 */
19944 	ASSERT(ill->ill_unbind_mp == NULL);
19945 	ill->ill_unbind_mp = unbind_mp;
19946 
19947 	ill_dlpi_send(ill, bind_mp);
19948 	/* Send down link-layer capabilities probe if not already done. */
19949 	ill_capability_probe(ill);
19950 
19951 	/*
19952 	 * Sysid used to rely on the fact that netboots set domainname
19953 	 * and the like. Now that miniroot boots aren't strictly netboots
19954 	 * and miniroot network configuration is driven from userland
19955 	 * these things still need to be set. This situation can be detected
19956 	 * by comparing the interface being configured here to the one
19957 	 * dhcifname was set to reference by the boot loader. Once sysid is
19958 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19959 	 */
19960 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
19961 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
19962 	    (strlen(srpc_domain) == 0)) {
19963 		if (dhcpinit() != 0)
19964 			cmn_err(CE_WARN, "no cached dhcp response");
19965 	}
19966 
19967 	/*
19968 	 * This operation will complete in ip_rput_dlpi with either
19969 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19970 	 */
19971 	return (EINPROGRESS);
19972 bad:
19973 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19974 	/*
19975 	 * We don't have to check for possible removal from illgrp
19976 	 * as we have not yet inserted in illgrp. For groups
19977 	 * without names, this ipif is still not UP and hence
19978 	 * this could not have possibly had any influence in forming
19979 	 * groups.
19980 	 */
19981 
19982 	freemsg(bind_mp);
19983 	freemsg(unbind_mp);
19984 	return (ENOMEM);
19985 }
19986 
19987 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19988 
19989 /*
19990  * DLPI and ARP is up.
19991  * Create all the IREs associated with an interface bring up multicast.
19992  * Set the interface flag and finish other initialization
19993  * that potentially had to be differed to after DL_BIND_ACK.
19994  */
19995 int
19996 ipif_up_done(ipif_t *ipif)
19997 {
19998 	ire_t	*ire_array[20];
19999 	ire_t	**irep = ire_array;
20000 	ire_t	**irep1;
20001 	ipaddr_t net_mask = 0;
20002 	ipaddr_t subnet_mask, route_mask;
20003 	ill_t	*ill = ipif->ipif_ill;
20004 	queue_t	*stq;
20005 	ipif_t	 *src_ipif;
20006 	ipif_t   *tmp_ipif;
20007 	boolean_t	flush_ire_cache = B_TRUE;
20008 	int	err = 0;
20009 	phyint_t *phyi;
20010 	ire_t	**ipif_saved_irep = NULL;
20011 	int ipif_saved_ire_cnt;
20012 	int	cnt;
20013 	boolean_t	src_ipif_held = B_FALSE;
20014 	boolean_t	ire_added = B_FALSE;
20015 	boolean_t	loopback = B_FALSE;
20016 	ip_stack_t	*ipst = ill->ill_ipst;
20017 
20018 	ip1dbg(("ipif_up_done(%s:%u)\n",
20019 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20020 	/* Check if this is a loopback interface */
20021 	if (ipif->ipif_ill->ill_wq == NULL)
20022 		loopback = B_TRUE;
20023 
20024 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20025 	/*
20026 	 * If all other interfaces for this ill are down or DEPRECATED,
20027 	 * or otherwise unsuitable for source address selection, remove
20028 	 * any IRE_CACHE entries for this ill to make sure source
20029 	 * address selection gets to take this new ipif into account.
20030 	 * No need to hold ill_lock while traversing the ipif list since
20031 	 * we are writer
20032 	 */
20033 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20034 	    tmp_ipif = tmp_ipif->ipif_next) {
20035 		if (((tmp_ipif->ipif_flags &
20036 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20037 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20038 		    (tmp_ipif == ipif))
20039 			continue;
20040 		/* first useable pre-existing interface */
20041 		flush_ire_cache = B_FALSE;
20042 		break;
20043 	}
20044 	if (flush_ire_cache)
20045 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20046 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20047 
20048 	/*
20049 	 * Figure out which way the send-to queue should go.  Only
20050 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20051 	 * should show up here.
20052 	 */
20053 	switch (ill->ill_net_type) {
20054 	case IRE_IF_RESOLVER:
20055 		stq = ill->ill_rq;
20056 		break;
20057 	case IRE_IF_NORESOLVER:
20058 	case IRE_LOOPBACK:
20059 		stq = ill->ill_wq;
20060 		break;
20061 	default:
20062 		return (EINVAL);
20063 	}
20064 
20065 	if (IS_LOOPBACK(ill)) {
20066 		/*
20067 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20068 		 * ipif_lookup_on_name(), but in the case of zones we can have
20069 		 * several loopback addresses on lo0. So all the interfaces with
20070 		 * loopback addresses need to be marked IRE_LOOPBACK.
20071 		 */
20072 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20073 		    htonl(INADDR_LOOPBACK))
20074 			ipif->ipif_ire_type = IRE_LOOPBACK;
20075 		else
20076 			ipif->ipif_ire_type = IRE_LOCAL;
20077 	}
20078 
20079 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20080 		/*
20081 		 * Can't use our source address. Select a different
20082 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20083 		 */
20084 		src_ipif = ipif_select_source(ipif->ipif_ill,
20085 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20086 		if (src_ipif == NULL)
20087 			src_ipif = ipif;	/* Last resort */
20088 		else
20089 			src_ipif_held = B_TRUE;
20090 	} else {
20091 		src_ipif = ipif;
20092 	}
20093 
20094 	/* Create all the IREs associated with this interface */
20095 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20096 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20097 
20098 		/*
20099 		 * If we're on a labeled system then make sure that zone-
20100 		 * private addresses have proper remote host database entries.
20101 		 */
20102 		if (is_system_labeled() &&
20103 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20104 		    !tsol_check_interface_address(ipif))
20105 			return (EINVAL);
20106 
20107 		/* Register the source address for __sin6_src_id */
20108 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20109 		    ipif->ipif_zoneid, ipst);
20110 		if (err != 0) {
20111 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20112 			return (err);
20113 		}
20114 
20115 		/* If the interface address is set, create the local IRE. */
20116 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20117 		    (void *)ipif,
20118 		    ipif->ipif_ire_type,
20119 		    ntohl(ipif->ipif_lcl_addr)));
20120 		*irep++ = ire_create(
20121 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20122 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20123 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20124 		    NULL,				/* no gateway */
20125 		    &ip_loopback_mtuplus,		/* max frag size */
20126 		    NULL,
20127 		    ipif->ipif_rq,			/* recv-from queue */
20128 		    NULL,				/* no send-to queue */
20129 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20130 		    ipif,
20131 		    0,
20132 		    0,
20133 		    0,
20134 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20135 		    RTF_PRIVATE : 0,
20136 		    &ire_uinfo_null,
20137 		    NULL,
20138 		    NULL,
20139 		    ipst);
20140 	} else {
20141 		ip1dbg((
20142 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20143 		    ipif->ipif_ire_type,
20144 		    ntohl(ipif->ipif_lcl_addr),
20145 		    (uint_t)ipif->ipif_flags));
20146 	}
20147 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20148 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20149 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20150 	} else {
20151 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20152 	}
20153 
20154 	subnet_mask = ipif->ipif_net_mask;
20155 
20156 	/*
20157 	 * If mask was not specified, use natural netmask of
20158 	 * interface address. Also, store this mask back into the
20159 	 * ipif struct.
20160 	 */
20161 	if (subnet_mask == 0) {
20162 		subnet_mask = net_mask;
20163 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20164 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20165 		    ipif->ipif_v6subnet);
20166 	}
20167 
20168 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20169 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20170 	    ipif->ipif_subnet != INADDR_ANY) {
20171 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20172 
20173 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20174 			route_mask = IP_HOST_MASK;
20175 		} else {
20176 			route_mask = subnet_mask;
20177 		}
20178 
20179 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20180 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20181 		    (void *)ipif, (void *)ill,
20182 		    ill->ill_net_type,
20183 		    ntohl(ipif->ipif_subnet)));
20184 		*irep++ = ire_create(
20185 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20186 		    (uchar_t *)&route_mask,		/* mask */
20187 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20188 		    NULL,				/* no gateway */
20189 		    &ipif->ipif_mtu,			/* max frag */
20190 		    NULL,
20191 		    NULL,				/* no recv queue */
20192 		    stq,				/* send-to queue */
20193 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20194 		    ipif,
20195 		    0,
20196 		    0,
20197 		    0,
20198 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20199 		    &ire_uinfo_null,
20200 		    NULL,
20201 		    NULL,
20202 		    ipst);
20203 	}
20204 
20205 	/*
20206 	 * Create any necessary broadcast IREs.
20207 	 */
20208 	if (ipif->ipif_flags & IPIF_BROADCAST)
20209 		irep = ipif_create_bcast_ires(ipif, irep);
20210 
20211 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20212 
20213 	/* If an earlier ire_create failed, get out now */
20214 	for (irep1 = irep; irep1 > ire_array; ) {
20215 		irep1--;
20216 		if (*irep1 == NULL) {
20217 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20218 			err = ENOMEM;
20219 			goto bad;
20220 		}
20221 	}
20222 
20223 	/*
20224 	 * Need to atomically check for ip_addr_availablity_check
20225 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20226 	 * from group also.The ill_g_lock is grabbed as reader
20227 	 * just to make sure no new ills or new ipifs are being added
20228 	 * to the system while we are checking the uniqueness of addresses.
20229 	 */
20230 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20231 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20232 	/* Mark it up, and increment counters. */
20233 	ipif->ipif_flags |= IPIF_UP;
20234 	ill->ill_ipif_up_count++;
20235 	err = ip_addr_availability_check(ipif);
20236 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20237 	rw_exit(&ipst->ips_ill_g_lock);
20238 
20239 	if (err != 0) {
20240 		/*
20241 		 * Our address may already be up on the same ill. In this case,
20242 		 * the ARP entry for our ipif replaced the one for the other
20243 		 * ipif. So we don't want to delete it (otherwise the other ipif
20244 		 * would be unable to send packets).
20245 		 * ip_addr_availability_check() identifies this case for us and
20246 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20247 		 * which is the expected error code.
20248 		 */
20249 		if (err == EADDRINUSE) {
20250 			freemsg(ipif->ipif_arp_del_mp);
20251 			ipif->ipif_arp_del_mp = NULL;
20252 			err = EADDRNOTAVAIL;
20253 		}
20254 		ill->ill_ipif_up_count--;
20255 		ipif->ipif_flags &= ~IPIF_UP;
20256 		goto bad;
20257 	}
20258 
20259 	/*
20260 	 * Add in all newly created IREs.  ire_create_bcast() has
20261 	 * already checked for duplicates of the IRE_BROADCAST type.
20262 	 * We want to add before we call ifgrp_insert which wants
20263 	 * to know whether IRE_IF_RESOLVER exists or not.
20264 	 *
20265 	 * NOTE : We refrele the ire though we may branch to "bad"
20266 	 *	  later on where we do ire_delete. This is okay
20267 	 *	  because nobody can delete it as we are running
20268 	 *	  exclusively.
20269 	 */
20270 	for (irep1 = irep; irep1 > ire_array; ) {
20271 		irep1--;
20272 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20273 		/*
20274 		 * refheld by ire_add. refele towards the end of the func
20275 		 */
20276 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20277 	}
20278 	ire_added = B_TRUE;
20279 	/*
20280 	 * Form groups if possible.
20281 	 *
20282 	 * If we are supposed to be in a ill_group with a name, insert it
20283 	 * now as we know that at least one ipif is UP. Otherwise form
20284 	 * nameless groups.
20285 	 *
20286 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20287 	 * this ipif into the appropriate interface group, or create a
20288 	 * new one. If this is already in a nameless group, we try to form
20289 	 * a bigger group looking at other ills potentially sharing this
20290 	 * ipif's prefix.
20291 	 */
20292 	phyi = ill->ill_phyint;
20293 	if (phyi->phyint_groupname_len != 0) {
20294 		ASSERT(phyi->phyint_groupname != NULL);
20295 		if (ill->ill_ipif_up_count == 1) {
20296 			ASSERT(ill->ill_group == NULL);
20297 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20298 			    phyi->phyint_groupname, NULL, B_TRUE);
20299 			if (err != 0) {
20300 				ip1dbg(("ipif_up_done: illgrp allocation "
20301 				    "failed, error %d\n", err));
20302 				goto bad;
20303 			}
20304 		}
20305 		ASSERT(ill->ill_group != NULL);
20306 	}
20307 
20308 	/*
20309 	 * When this is part of group, we need to make sure that
20310 	 * any broadcast ires created because of this ipif coming
20311 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20312 	 * so that we don't receive duplicate broadcast packets.
20313 	 */
20314 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20315 		ipif_renominate_bcast(ipif);
20316 
20317 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20318 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20319 	ipif_saved_irep = ipif_recover_ire(ipif);
20320 
20321 	if (!loopback) {
20322 		/*
20323 		 * If the broadcast address has been set, make sure it makes
20324 		 * sense based on the interface address.
20325 		 * Only match on ill since we are sharing broadcast addresses.
20326 		 */
20327 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20328 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20329 			ire_t	*ire;
20330 
20331 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20332 			    IRE_BROADCAST, ipif, ALL_ZONES,
20333 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20334 
20335 			if (ire == NULL) {
20336 				/*
20337 				 * If there isn't a matching broadcast IRE,
20338 				 * revert to the default for this netmask.
20339 				 */
20340 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20341 				mutex_enter(&ipif->ipif_ill->ill_lock);
20342 				ipif_set_default(ipif);
20343 				mutex_exit(&ipif->ipif_ill->ill_lock);
20344 			} else {
20345 				ire_refrele(ire);
20346 			}
20347 		}
20348 
20349 	}
20350 
20351 	/* This is the first interface on this ill */
20352 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20353 		/*
20354 		 * Need to recover all multicast memberships in the driver.
20355 		 * This had to be deferred until we had attached.
20356 		 */
20357 		ill_recover_multicast(ill);
20358 	}
20359 	/* Join the allhosts multicast address */
20360 	ipif_multicast_up(ipif);
20361 
20362 	if (!loopback) {
20363 		/*
20364 		 * See whether anybody else would benefit from the
20365 		 * new ipif that we added. We call this always rather
20366 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20367 		 * ipif is for the benefit of illgrp_insert (done above)
20368 		 * which does not do source address selection as it does
20369 		 * not want to re-create interface routes that we are
20370 		 * having reference to it here.
20371 		 */
20372 		ill_update_source_selection(ill);
20373 	}
20374 
20375 	for (irep1 = irep; irep1 > ire_array; ) {
20376 		irep1--;
20377 		if (*irep1 != NULL) {
20378 			/* was held in ire_add */
20379 			ire_refrele(*irep1);
20380 		}
20381 	}
20382 
20383 	cnt = ipif_saved_ire_cnt;
20384 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20385 		if (*irep1 != NULL) {
20386 			/* was held in ire_add */
20387 			ire_refrele(*irep1);
20388 		}
20389 	}
20390 
20391 	if (!loopback && ipif->ipif_addr_ready) {
20392 		/* Broadcast an address mask reply. */
20393 		ipif_mask_reply(ipif);
20394 	}
20395 	if (ipif_saved_irep != NULL) {
20396 		kmem_free(ipif_saved_irep,
20397 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20398 	}
20399 	if (src_ipif_held)
20400 		ipif_refrele(src_ipif);
20401 
20402 	/*
20403 	 * This had to be deferred until we had bound.  Tell routing sockets and
20404 	 * others that this interface is up if it looks like the address has
20405 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20406 	 * duplicate address detection to do its thing.
20407 	 */
20408 	if (ipif->ipif_addr_ready) {
20409 		ip_rts_ifmsg(ipif);
20410 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20411 		/* Let SCTP update the status for this ipif */
20412 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20413 	}
20414 	return (0);
20415 
20416 bad:
20417 	ip1dbg(("ipif_up_done: FAILED \n"));
20418 	/*
20419 	 * We don't have to bother removing from ill groups because
20420 	 *
20421 	 * 1) For groups with names, we insert only when the first ipif
20422 	 *    comes up. In that case if it fails, it will not be in any
20423 	 *    group. So, we need not try to remove for that case.
20424 	 *
20425 	 * 2) For groups without names, either we tried to insert ipif_ill
20426 	 *    in a group as singleton or found some other group to become
20427 	 *    a bigger group. For the former, if it fails we don't have
20428 	 *    anything to do as ipif_ill is not in the group and for the
20429 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20430 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20431 	 */
20432 	while (irep > ire_array) {
20433 		irep--;
20434 		if (*irep != NULL) {
20435 			ire_delete(*irep);
20436 			if (ire_added)
20437 				ire_refrele(*irep);
20438 		}
20439 	}
20440 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20441 
20442 	if (ipif_saved_irep != NULL) {
20443 		kmem_free(ipif_saved_irep,
20444 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20445 	}
20446 	if (src_ipif_held)
20447 		ipif_refrele(src_ipif);
20448 
20449 	ipif_arp_down(ipif);
20450 	return (err);
20451 }
20452 
20453 /*
20454  * Turn off the ARP with the ILLF_NOARP flag.
20455  */
20456 static int
20457 ill_arp_off(ill_t *ill)
20458 {
20459 	mblk_t	*arp_off_mp = NULL;
20460 	mblk_t	*arp_on_mp = NULL;
20461 
20462 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20463 
20464 	ASSERT(IAM_WRITER_ILL(ill));
20465 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20466 
20467 	/*
20468 	 * If the on message is still around we've already done
20469 	 * an arp_off without doing an arp_on thus there is no
20470 	 * work needed.
20471 	 */
20472 	if (ill->ill_arp_on_mp != NULL)
20473 		return (0);
20474 
20475 	/*
20476 	 * Allocate an ARP on message (to be saved) and an ARP off message
20477 	 */
20478 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20479 	if (!arp_off_mp)
20480 		return (ENOMEM);
20481 
20482 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20483 	if (!arp_on_mp)
20484 		goto failed;
20485 
20486 	ASSERT(ill->ill_arp_on_mp == NULL);
20487 	ill->ill_arp_on_mp = arp_on_mp;
20488 
20489 	/* Send an AR_INTERFACE_OFF request */
20490 	putnext(ill->ill_rq, arp_off_mp);
20491 	return (0);
20492 failed:
20493 
20494 	if (arp_off_mp)
20495 		freemsg(arp_off_mp);
20496 	return (ENOMEM);
20497 }
20498 
20499 /*
20500  * Turn on ARP by turning off the ILLF_NOARP flag.
20501  */
20502 static int
20503 ill_arp_on(ill_t *ill)
20504 {
20505 	mblk_t	*mp;
20506 
20507 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20508 
20509 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20510 
20511 	ASSERT(IAM_WRITER_ILL(ill));
20512 	/*
20513 	 * Send an AR_INTERFACE_ON request if we have already done
20514 	 * an arp_off (which allocated the message).
20515 	 */
20516 	if (ill->ill_arp_on_mp != NULL) {
20517 		mp = ill->ill_arp_on_mp;
20518 		ill->ill_arp_on_mp = NULL;
20519 		putnext(ill->ill_rq, mp);
20520 	}
20521 	return (0);
20522 }
20523 
20524 /*
20525  * Called after either deleting ill from the group or when setting
20526  * FAILED or STANDBY on the interface.
20527  */
20528 static void
20529 illgrp_reset_schednext(ill_t *ill)
20530 {
20531 	ill_group_t *illgrp;
20532 	ill_t *save_ill;
20533 
20534 	ASSERT(IAM_WRITER_ILL(ill));
20535 	/*
20536 	 * When called from illgrp_delete, ill_group will be non-NULL.
20537 	 * But when called from ip_sioctl_flags, it could be NULL if
20538 	 * somebody is setting FAILED/INACTIVE on some interface which
20539 	 * is not part of a group.
20540 	 */
20541 	illgrp = ill->ill_group;
20542 	if (illgrp == NULL)
20543 		return;
20544 	if (illgrp->illgrp_ill_schednext != ill)
20545 		return;
20546 
20547 	illgrp->illgrp_ill_schednext = NULL;
20548 	save_ill = ill;
20549 	/*
20550 	 * Choose a good ill to be the next one for
20551 	 * outbound traffic. As the flags FAILED/STANDBY is
20552 	 * not yet marked when called from ip_sioctl_flags,
20553 	 * we check for ill separately.
20554 	 */
20555 	for (ill = illgrp->illgrp_ill; ill != NULL;
20556 	    ill = ill->ill_group_next) {
20557 		if ((ill != save_ill) &&
20558 		    !(ill->ill_phyint->phyint_flags &
20559 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20560 			illgrp->illgrp_ill_schednext = ill;
20561 			return;
20562 		}
20563 	}
20564 }
20565 
20566 /*
20567  * Given an ill, find the next ill in the group to be scheduled.
20568  * (This should be called by ip_newroute() before ire_create().)
20569  * The passed in ill may be pulled out of the group, after we have picked
20570  * up a different outgoing ill from the same group. However ire add will
20571  * atomically check this.
20572  */
20573 ill_t *
20574 illgrp_scheduler(ill_t *ill)
20575 {
20576 	ill_t *retill;
20577 	ill_group_t *illgrp;
20578 	int illcnt;
20579 	int i;
20580 	uint64_t flags;
20581 	ip_stack_t	*ipst = ill->ill_ipst;
20582 
20583 	/*
20584 	 * We don't use a lock to check for the ill_group. If this ill
20585 	 * is currently being inserted we may end up just returning this
20586 	 * ill itself. That is ok.
20587 	 */
20588 	if (ill->ill_group == NULL) {
20589 		ill_refhold(ill);
20590 		return (ill);
20591 	}
20592 
20593 	/*
20594 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20595 	 * a set of stable ills. No ill can be added or deleted or change
20596 	 * group while we hold the reader lock.
20597 	 */
20598 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20599 	if ((illgrp = ill->ill_group) == NULL) {
20600 		rw_exit(&ipst->ips_ill_g_lock);
20601 		ill_refhold(ill);
20602 		return (ill);
20603 	}
20604 
20605 	illcnt = illgrp->illgrp_ill_count;
20606 	mutex_enter(&illgrp->illgrp_lock);
20607 	retill = illgrp->illgrp_ill_schednext;
20608 
20609 	if (retill == NULL)
20610 		retill = illgrp->illgrp_ill;
20611 
20612 	/*
20613 	 * We do a circular search beginning at illgrp_ill_schednext
20614 	 * or illgrp_ill. We don't check the flags against the ill lock
20615 	 * since it can change anytime. The ire creation will be atomic
20616 	 * and will fail if the ill is FAILED or OFFLINE.
20617 	 */
20618 	for (i = 0; i < illcnt; i++) {
20619 		flags = retill->ill_phyint->phyint_flags;
20620 
20621 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20622 		    ILL_CAN_LOOKUP(retill)) {
20623 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20624 			ill_refhold(retill);
20625 			break;
20626 		}
20627 		retill = retill->ill_group_next;
20628 		if (retill == NULL)
20629 			retill = illgrp->illgrp_ill;
20630 	}
20631 	mutex_exit(&illgrp->illgrp_lock);
20632 	rw_exit(&ipst->ips_ill_g_lock);
20633 
20634 	return (i == illcnt ? NULL : retill);
20635 }
20636 
20637 /*
20638  * Checks for availbility of a usable source address (if there is one) when the
20639  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20640  * this selection is done regardless of the destination.
20641  */
20642 boolean_t
20643 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20644 {
20645 	uint_t	ifindex;
20646 	ipif_t	*ipif = NULL;
20647 	ill_t	*uill;
20648 	boolean_t isv6;
20649 	ip_stack_t	*ipst = ill->ill_ipst;
20650 
20651 	ASSERT(ill != NULL);
20652 
20653 	isv6 = ill->ill_isv6;
20654 	ifindex = ill->ill_usesrc_ifindex;
20655 	if (ifindex != 0) {
20656 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20657 		    NULL, ipst);
20658 		if (uill == NULL)
20659 			return (NULL);
20660 		mutex_enter(&uill->ill_lock);
20661 		for (ipif = uill->ill_ipif; ipif != NULL;
20662 		    ipif = ipif->ipif_next) {
20663 			if (!IPIF_CAN_LOOKUP(ipif))
20664 				continue;
20665 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20666 				continue;
20667 			if (!(ipif->ipif_flags & IPIF_UP))
20668 				continue;
20669 			if (ipif->ipif_zoneid != zoneid)
20670 				continue;
20671 			if ((isv6 &&
20672 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20673 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20674 				continue;
20675 			mutex_exit(&uill->ill_lock);
20676 			ill_refrele(uill);
20677 			return (B_TRUE);
20678 		}
20679 		mutex_exit(&uill->ill_lock);
20680 		ill_refrele(uill);
20681 	}
20682 	return (B_FALSE);
20683 }
20684 
20685 /*
20686  * Determine the best source address given a destination address and an ill.
20687  * Prefers non-deprecated over deprecated but will return a deprecated
20688  * address if there is no other choice. If there is a usable source address
20689  * on the interface pointed to by ill_usesrc_ifindex then that is given
20690  * first preference.
20691  *
20692  * Returns NULL if there is no suitable source address for the ill.
20693  * This only occurs when there is no valid source address for the ill.
20694  */
20695 ipif_t *
20696 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20697 {
20698 	ipif_t *ipif;
20699 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20700 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20701 	int index = 0;
20702 	boolean_t wrapped = B_FALSE;
20703 	boolean_t same_subnet_only = B_FALSE;
20704 	boolean_t ipif_same_found, ipif_other_found;
20705 	boolean_t specific_found;
20706 	ill_t	*till, *usill = NULL;
20707 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20708 	ip_stack_t	*ipst = ill->ill_ipst;
20709 
20710 	if (ill->ill_usesrc_ifindex != 0) {
20711 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20712 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20713 		if (usill != NULL)
20714 			ill = usill;	/* Select source from usesrc ILL */
20715 		else
20716 			return (NULL);
20717 	}
20718 
20719 	/*
20720 	 * If we're dealing with an unlabeled destination on a labeled system,
20721 	 * make sure that we ignore source addresses that are incompatible with
20722 	 * the destination's default label.  That destination's default label
20723 	 * must dominate the minimum label on the source address.
20724 	 */
20725 	dst_rhtp = NULL;
20726 	if (is_system_labeled()) {
20727 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20728 		if (dst_rhtp == NULL)
20729 			return (NULL);
20730 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20731 			TPC_RELE(dst_rhtp);
20732 			dst_rhtp = NULL;
20733 		}
20734 	}
20735 
20736 	/*
20737 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20738 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20739 	 * After selecting the right ipif, under ill_lock make sure ipif is
20740 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20741 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20742 	 * but not under a lock.
20743 	 */
20744 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20745 
20746 retry:
20747 	till = ill;
20748 	ipif_arr[0] = NULL;
20749 
20750 	if (till->ill_group != NULL)
20751 		till = till->ill_group->illgrp_ill;
20752 
20753 	/*
20754 	 * Choose one good source address from each ill across the group.
20755 	 * If possible choose a source address in the same subnet as
20756 	 * the destination address.
20757 	 *
20758 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20759 	 * This is okay because of the following.
20760 	 *
20761 	 *    If PHYI_FAILED is set and we still have non-deprecated
20762 	 *    addresses, it means the addresses have not yet been
20763 	 *    failed over to a different interface. We potentially
20764 	 *    select them to create IRE_CACHES, which will be later
20765 	 *    flushed when the addresses move over.
20766 	 *
20767 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20768 	 *    addresses, it means either the user has configured them
20769 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20770 	 *    been moved over. For the former, in.mpathd does a failover
20771 	 *    when the interface becomes INACTIVE and hence we should
20772 	 *    not find them. Once INACTIVE is set, we don't allow them
20773 	 *    to create logical interfaces anymore. For the latter, a
20774 	 *    flush will happen when INACTIVE is cleared which will
20775 	 *    flush the IRE_CACHES.
20776 	 *
20777 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20778 	 *    over soon. We potentially select them to create IRE_CACHEs,
20779 	 *    which will be later flushed when the addresses move over.
20780 	 *
20781 	 * NOTE : As ipif_select_source is called to borrow source address
20782 	 * for an ipif that is part of a group, source address selection
20783 	 * will be re-done whenever the group changes i.e either an
20784 	 * insertion/deletion in the group.
20785 	 *
20786 	 * Fill ipif_arr[] with source addresses, using these rules:
20787 	 *
20788 	 *	1. At most one source address from a given ill ends up
20789 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20790 	 *	   associated with a given ill ends up in ipif_arr[].
20791 	 *
20792 	 *	2. If there is at least one non-deprecated ipif in the
20793 	 *	   IPMP group with a source address on the same subnet as
20794 	 *	   our destination, then fill ipif_arr[] only with
20795 	 *	   source addresses on the same subnet as our destination.
20796 	 *	   Note that because of (1), only the first
20797 	 *	   non-deprecated ipif found with a source address
20798 	 *	   matching the destination ends up in ipif_arr[].
20799 	 *
20800 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20801 	 *	   addresses not in the same subnet as our destination.
20802 	 *	   Again, because of (1), only the first off-subnet source
20803 	 *	   address will be chosen.
20804 	 *
20805 	 *	4. If there are no non-deprecated ipifs, then just use
20806 	 *	   the source address associated with the last deprecated
20807 	 *	   one we find that happens to be on the same subnet,
20808 	 *	   otherwise the first one not in the same subnet.
20809 	 */
20810 	specific_found = B_FALSE;
20811 	for (; till != NULL; till = till->ill_group_next) {
20812 		ipif_same_found = B_FALSE;
20813 		ipif_other_found = B_FALSE;
20814 		for (ipif = till->ill_ipif; ipif != NULL;
20815 		    ipif = ipif->ipif_next) {
20816 			if (!IPIF_CAN_LOOKUP(ipif))
20817 				continue;
20818 			/* Always skip NOLOCAL and ANYCAST interfaces */
20819 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20820 				continue;
20821 			if (!(ipif->ipif_flags & IPIF_UP) ||
20822 			    !ipif->ipif_addr_ready)
20823 				continue;
20824 			if (ipif->ipif_zoneid != zoneid &&
20825 			    ipif->ipif_zoneid != ALL_ZONES)
20826 				continue;
20827 			/*
20828 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20829 			 * but are not valid as source addresses.
20830 			 */
20831 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20832 				continue;
20833 
20834 			/*
20835 			 * Check compatibility of local address for
20836 			 * destination's default label if we're on a labeled
20837 			 * system.  Incompatible addresses can't be used at
20838 			 * all.
20839 			 */
20840 			if (dst_rhtp != NULL) {
20841 				boolean_t incompat;
20842 
20843 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20844 				    IPV4_VERSION, B_FALSE);
20845 				if (src_rhtp == NULL)
20846 					continue;
20847 				incompat =
20848 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20849 				    src_rhtp->tpc_tp.tp_doi !=
20850 				    dst_rhtp->tpc_tp.tp_doi ||
20851 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20852 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20853 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20854 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20855 				TPC_RELE(src_rhtp);
20856 				if (incompat)
20857 					continue;
20858 			}
20859 
20860 			/*
20861 			 * We prefer not to use all all-zones addresses, if we
20862 			 * can avoid it, as they pose problems with unlabeled
20863 			 * destinations.
20864 			 */
20865 			if (ipif->ipif_zoneid != ALL_ZONES) {
20866 				if (!specific_found &&
20867 				    (!same_subnet_only ||
20868 				    (ipif->ipif_net_mask & dst) ==
20869 				    ipif->ipif_subnet)) {
20870 					index = 0;
20871 					specific_found = B_TRUE;
20872 					ipif_other_found = B_FALSE;
20873 				}
20874 			} else {
20875 				if (specific_found)
20876 					continue;
20877 			}
20878 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20879 				if (ipif_dep == NULL ||
20880 				    (ipif->ipif_net_mask & dst) ==
20881 				    ipif->ipif_subnet)
20882 					ipif_dep = ipif;
20883 				continue;
20884 			}
20885 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20886 				/* found a source address in the same subnet */
20887 				if (!same_subnet_only) {
20888 					same_subnet_only = B_TRUE;
20889 					index = 0;
20890 				}
20891 				ipif_same_found = B_TRUE;
20892 			} else {
20893 				if (same_subnet_only || ipif_other_found)
20894 					continue;
20895 				ipif_other_found = B_TRUE;
20896 			}
20897 			ipif_arr[index++] = ipif;
20898 			if (index == MAX_IPIF_SELECT_SOURCE) {
20899 				wrapped = B_TRUE;
20900 				index = 0;
20901 			}
20902 			if (ipif_same_found)
20903 				break;
20904 		}
20905 	}
20906 
20907 	if (ipif_arr[0] == NULL) {
20908 		ipif = ipif_dep;
20909 	} else {
20910 		if (wrapped)
20911 			index = MAX_IPIF_SELECT_SOURCE;
20912 		ipif = ipif_arr[ipif_rand(ipst) % index];
20913 		ASSERT(ipif != NULL);
20914 	}
20915 
20916 	if (ipif != NULL) {
20917 		mutex_enter(&ipif->ipif_ill->ill_lock);
20918 		if (!IPIF_CAN_LOOKUP(ipif)) {
20919 			mutex_exit(&ipif->ipif_ill->ill_lock);
20920 			goto retry;
20921 		}
20922 		ipif_refhold_locked(ipif);
20923 		mutex_exit(&ipif->ipif_ill->ill_lock);
20924 	}
20925 
20926 	rw_exit(&ipst->ips_ill_g_lock);
20927 	if (usill != NULL)
20928 		ill_refrele(usill);
20929 	if (dst_rhtp != NULL)
20930 		TPC_RELE(dst_rhtp);
20931 
20932 #ifdef DEBUG
20933 	if (ipif == NULL) {
20934 		char buf1[INET6_ADDRSTRLEN];
20935 
20936 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20937 		    ill->ill_name,
20938 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20939 	} else {
20940 		char buf1[INET6_ADDRSTRLEN];
20941 		char buf2[INET6_ADDRSTRLEN];
20942 
20943 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20944 		    ipif->ipif_ill->ill_name,
20945 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20946 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20947 		    buf2, sizeof (buf2))));
20948 	}
20949 #endif /* DEBUG */
20950 	return (ipif);
20951 }
20952 
20953 
20954 /*
20955  * If old_ipif is not NULL, see if ipif was derived from old
20956  * ipif and if so, recreate the interface route by re-doing
20957  * source address selection. This happens when ipif_down ->
20958  * ipif_update_other_ipifs calls us.
20959  *
20960  * If old_ipif is NULL, just redo the source address selection
20961  * if needed. This happens when illgrp_insert or ipif_up_done
20962  * calls us.
20963  */
20964 static void
20965 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20966 {
20967 	ire_t *ire;
20968 	ire_t *ipif_ire;
20969 	queue_t *stq;
20970 	ipif_t *nipif;
20971 	ill_t *ill;
20972 	boolean_t need_rele = B_FALSE;
20973 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20974 
20975 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20976 	ASSERT(IAM_WRITER_IPIF(ipif));
20977 
20978 	ill = ipif->ipif_ill;
20979 	if (!(ipif->ipif_flags &
20980 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20981 		/*
20982 		 * Can't possibly have borrowed the source
20983 		 * from old_ipif.
20984 		 */
20985 		return;
20986 	}
20987 
20988 	/*
20989 	 * Is there any work to be done? No work if the address
20990 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
20991 	 * ipif_select_source() does not borrow addresses from
20992 	 * NOLOCAL and ANYCAST interfaces).
20993 	 */
20994 	if ((old_ipif != NULL) &&
20995 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20996 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20997 	    (old_ipif->ipif_flags &
20998 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20999 		return;
21000 	}
21001 
21002 	/*
21003 	 * Perform the same checks as when creating the
21004 	 * IRE_INTERFACE in ipif_up_done.
21005 	 */
21006 	if (!(ipif->ipif_flags & IPIF_UP))
21007 		return;
21008 
21009 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21010 	    (ipif->ipif_subnet == INADDR_ANY))
21011 		return;
21012 
21013 	ipif_ire = ipif_to_ire(ipif);
21014 	if (ipif_ire == NULL)
21015 		return;
21016 
21017 	/*
21018 	 * We know that ipif uses some other source for its
21019 	 * IRE_INTERFACE. Is it using the source of this
21020 	 * old_ipif?
21021 	 */
21022 	if (old_ipif != NULL &&
21023 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21024 		ire_refrele(ipif_ire);
21025 		return;
21026 	}
21027 	if (ip_debug > 2) {
21028 		/* ip1dbg */
21029 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21030 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21031 	}
21032 
21033 	stq = ipif_ire->ire_stq;
21034 
21035 	/*
21036 	 * Can't use our source address. Select a different
21037 	 * source address for the IRE_INTERFACE.
21038 	 */
21039 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21040 	if (nipif == NULL) {
21041 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21042 		nipif = ipif;
21043 	} else {
21044 		need_rele = B_TRUE;
21045 	}
21046 
21047 	ire = ire_create(
21048 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21049 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21050 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21051 	    NULL,				/* no gateway */
21052 	    &ipif->ipif_mtu,			/* max frag */
21053 	    NULL,				/* no src nce */
21054 	    NULL,				/* no recv from queue */
21055 	    stq,				/* send-to queue */
21056 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21057 	    ipif,
21058 	    0,
21059 	    0,
21060 	    0,
21061 	    0,
21062 	    &ire_uinfo_null,
21063 	    NULL,
21064 	    NULL,
21065 	    ipst);
21066 
21067 	if (ire != NULL) {
21068 		ire_t *ret_ire;
21069 		int error;
21070 
21071 		/*
21072 		 * We don't need ipif_ire anymore. We need to delete
21073 		 * before we add so that ire_add does not detect
21074 		 * duplicates.
21075 		 */
21076 		ire_delete(ipif_ire);
21077 		ret_ire = ire;
21078 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21079 		ASSERT(error == 0);
21080 		ASSERT(ire == ret_ire);
21081 		/* Held in ire_add */
21082 		ire_refrele(ret_ire);
21083 	}
21084 	/*
21085 	 * Either we are falling through from above or could not
21086 	 * allocate a replacement.
21087 	 */
21088 	ire_refrele(ipif_ire);
21089 	if (need_rele)
21090 		ipif_refrele(nipif);
21091 }
21092 
21093 /*
21094  * This old_ipif is going away.
21095  *
21096  * Determine if any other ipif's is using our address as
21097  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21098  * IPIF_DEPRECATED).
21099  * Find the IRE_INTERFACE for such ipifs and recreate them
21100  * to use an different source address following the rules in
21101  * ipif_up_done.
21102  *
21103  * This function takes an illgrp as an argument so that illgrp_delete
21104  * can call this to update source address even after deleting the
21105  * old_ipif->ipif_ill from the ill group.
21106  */
21107 static void
21108 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21109 {
21110 	ipif_t *ipif;
21111 	ill_t *ill;
21112 	char	buf[INET6_ADDRSTRLEN];
21113 
21114 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21115 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21116 
21117 	ill = old_ipif->ipif_ill;
21118 
21119 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21120 	    ill->ill_name,
21121 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21122 	    buf, sizeof (buf))));
21123 	/*
21124 	 * If this part of a group, look at all ills as ipif_select_source
21125 	 * borrows source address across all the ills in the group.
21126 	 */
21127 	if (illgrp != NULL)
21128 		ill = illgrp->illgrp_ill;
21129 
21130 	for (; ill != NULL; ill = ill->ill_group_next) {
21131 		for (ipif = ill->ill_ipif; ipif != NULL;
21132 		    ipif = ipif->ipif_next) {
21133 
21134 			if (ipif == old_ipif)
21135 				continue;
21136 
21137 			ipif_recreate_interface_routes(old_ipif, ipif);
21138 		}
21139 	}
21140 }
21141 
21142 /* ARGSUSED */
21143 int
21144 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21145 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21146 {
21147 	/*
21148 	 * ill_phyint_reinit merged the v4 and v6 into a single
21149 	 * ipsq. Could also have become part of a ipmp group in the
21150 	 * process, and we might not have been able to complete the
21151 	 * operation in ipif_set_values, if we could not become
21152 	 * exclusive.  If so restart it here.
21153 	 */
21154 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21155 }
21156 
21157 
21158 /*
21159  * Can operate on either a module or a driver queue.
21160  * Returns an error if not a module queue.
21161  */
21162 /* ARGSUSED */
21163 int
21164 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21165     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21166 {
21167 	queue_t		*q1 = q;
21168 	char 		*cp;
21169 	char		interf_name[LIFNAMSIZ];
21170 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21171 
21172 	if (q->q_next == NULL) {
21173 		ip1dbg((
21174 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21175 		return (EINVAL);
21176 	}
21177 
21178 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21179 		return (EALREADY);
21180 
21181 	do {
21182 		q1 = q1->q_next;
21183 	} while (q1->q_next);
21184 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21185 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21186 
21187 	/*
21188 	 * Here we are not going to delay the ioack until after
21189 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21190 	 * original ioctl message before sending the requests.
21191 	 */
21192 	return (ipif_set_values(q, mp, interf_name, &ppa));
21193 }
21194 
21195 /* ARGSUSED */
21196 int
21197 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21198     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21199 {
21200 	return (ENXIO);
21201 }
21202 
21203 /*
21204  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21205  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21206  * ire_check_and_create_bcast()).
21207  */
21208 static ire_t **
21209 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21210 {
21211 	ipaddr_t addr;
21212 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21213 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21214 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21215 
21216 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21217 
21218 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21219 
21220 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21221 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21222 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21223 
21224 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21225 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21226 
21227 	/*
21228 	 * For backward compatibility, we create net broadcast IREs based on
21229 	 * the old "IP address class system", since some old machines only
21230 	 * respond to these class derived net broadcast.  However, we must not
21231 	 * create these net broadcast IREs if the subnetmask is shorter than
21232 	 * the IP address class based derived netmask.  Otherwise, we may
21233 	 * create a net broadcast address which is the same as an IP address
21234 	 * on the subnet -- and then TCP will refuse to talk to that address.
21235 	 */
21236 	if (netmask < subnetmask) {
21237 		addr = netmask & ipif->ipif_subnet;
21238 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21239 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21240 		    flags);
21241 	}
21242 
21243 	/*
21244 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21245 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21246 	 * created.  Creating these broadcast IREs will only create confusion
21247 	 * as `addr' will be the same as the IP address.
21248 	 */
21249 	if (subnetmask != 0xFFFFFFFF) {
21250 		addr = ipif->ipif_subnet;
21251 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21252 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21253 		    irep, flags);
21254 	}
21255 
21256 	return (irep);
21257 }
21258 
21259 /*
21260  * Broadcast IRE info structure used in the functions below.  Since we
21261  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21262  */
21263 typedef struct bcast_ireinfo {
21264 	uchar_t		bi_type;	/* BCAST_* value from below */
21265 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21266 			bi_needrep:1,	/* do we need to replace it? */
21267 			bi_haverep:1,	/* have we replaced it? */
21268 			bi_pad:5;
21269 	ipaddr_t	bi_addr;	/* IRE address */
21270 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21271 } bcast_ireinfo_t;
21272 
21273 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21274 
21275 /*
21276  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21277  * return B_TRUE if it should immediately be used to recreate the IRE.
21278  */
21279 static boolean_t
21280 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21281 {
21282 	ipaddr_t addr;
21283 
21284 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21285 
21286 	switch (bireinfop->bi_type) {
21287 	case BCAST_NET:
21288 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21289 		if (addr != bireinfop->bi_addr)
21290 			return (B_FALSE);
21291 		break;
21292 	case BCAST_SUBNET:
21293 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21294 			return (B_FALSE);
21295 		break;
21296 	}
21297 
21298 	bireinfop->bi_needrep = 1;
21299 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21300 		if (bireinfop->bi_backup == NULL)
21301 			bireinfop->bi_backup = ipif;
21302 		return (B_FALSE);
21303 	}
21304 	return (B_TRUE);
21305 }
21306 
21307 /*
21308  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21309  * them ala ire_check_and_create_bcast().
21310  */
21311 static ire_t **
21312 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21313 {
21314 	ipaddr_t mask, addr;
21315 
21316 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21317 
21318 	addr = bireinfop->bi_addr;
21319 	irep = ire_create_bcast(ipif, addr, irep);
21320 
21321 	switch (bireinfop->bi_type) {
21322 	case BCAST_NET:
21323 		mask = ip_net_mask(ipif->ipif_subnet);
21324 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21325 		break;
21326 	case BCAST_SUBNET:
21327 		mask = ipif->ipif_net_mask;
21328 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21329 		break;
21330 	}
21331 
21332 	bireinfop->bi_haverep = 1;
21333 	return (irep);
21334 }
21335 
21336 /*
21337  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21338  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21339  * that are going away are still needed.  If so, have ipif_create_bcast()
21340  * recreate them (except for the deprecated case, as explained below).
21341  */
21342 static ire_t **
21343 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21344     ire_t **irep)
21345 {
21346 	int i;
21347 	ipif_t *ipif;
21348 
21349 	ASSERT(!ill->ill_isv6);
21350 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21351 		/*
21352 		 * Skip this ipif if it's (a) the one being taken down, (b)
21353 		 * not in the same zone, or (c) has no valid local address.
21354 		 */
21355 		if (ipif == test_ipif ||
21356 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21357 		    ipif->ipif_subnet == 0 ||
21358 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21359 		    (IPIF_UP|IPIF_BROADCAST))
21360 			continue;
21361 
21362 		/*
21363 		 * For each dying IRE that hasn't yet been replaced, see if
21364 		 * `ipif' needs it and whether the IRE should be recreated on
21365 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21366 		 * will return B_FALSE even if `ipif' needs the IRE on the
21367 		 * hopes that we'll later find a needy non-deprecated ipif.
21368 		 * However, the ipif is recorded in bi_backup for possible
21369 		 * subsequent use by ipif_check_bcast_ires().
21370 		 */
21371 		for (i = 0; i < BCAST_COUNT; i++) {
21372 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21373 				continue;
21374 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21375 				continue;
21376 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21377 		}
21378 
21379 		/*
21380 		 * If we've replaced all of the broadcast IREs that are going
21381 		 * to be taken down, we know we're done.
21382 		 */
21383 		for (i = 0; i < BCAST_COUNT; i++) {
21384 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21385 				break;
21386 		}
21387 		if (i == BCAST_COUNT)
21388 			break;
21389 	}
21390 	return (irep);
21391 }
21392 
21393 /*
21394  * Check if `test_ipif' (which is going away) is associated with any existing
21395  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21396  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21397  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21398  *
21399  * This is necessary because broadcast IREs are shared.  In particular, a
21400  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21401  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21402  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21403  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21404  * same zone, they will share the same set of broadcast IREs.
21405  *
21406  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21407  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21408  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21409  */
21410 static void
21411 ipif_check_bcast_ires(ipif_t *test_ipif)
21412 {
21413 	ill_t		*ill = test_ipif->ipif_ill;
21414 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21415 	ire_t		**irep1, **irep = &ire_array[0];
21416 	uint_t 		i, willdie;
21417 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21418 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21419 
21420 	ASSERT(!test_ipif->ipif_isv6);
21421 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21422 
21423 	/*
21424 	 * No broadcast IREs for the LOOPBACK interface
21425 	 * or others such as point to point and IPIF_NOXMIT.
21426 	 */
21427 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21428 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21429 		return;
21430 
21431 	bzero(bireinfo, sizeof (bireinfo));
21432 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21433 	bireinfo[0].bi_addr = 0;
21434 
21435 	bireinfo[1].bi_type = BCAST_ALLONES;
21436 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21437 
21438 	bireinfo[2].bi_type = BCAST_NET;
21439 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21440 
21441 	if (test_ipif->ipif_net_mask != 0)
21442 		mask = test_ipif->ipif_net_mask;
21443 	bireinfo[3].bi_type = BCAST_SUBNET;
21444 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21445 
21446 	/*
21447 	 * Figure out what (if any) broadcast IREs will die as a result of
21448 	 * `test_ipif' going away.  If none will die, we're done.
21449 	 */
21450 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21451 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21452 		    test_ipif, ALL_ZONES, NULL,
21453 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21454 		if (ire != NULL) {
21455 			willdie++;
21456 			bireinfo[i].bi_willdie = 1;
21457 			ire_refrele(ire);
21458 		}
21459 	}
21460 
21461 	if (willdie == 0)
21462 		return;
21463 
21464 	/*
21465 	 * Walk through all the ipifs that will be affected by the dying IREs,
21466 	 * and recreate the IREs as necessary.
21467 	 */
21468 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21469 
21470 	/*
21471 	 * Scan through the set of broadcast IREs and see if there are any
21472 	 * that we need to replace that have not yet been replaced.  If so,
21473 	 * replace them using the appropriate backup ipif.
21474 	 */
21475 	for (i = 0; i < BCAST_COUNT; i++) {
21476 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21477 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21478 			    &bireinfo[i], irep);
21479 	}
21480 
21481 	/*
21482 	 * If we can't create all of them, don't add any of them.  (Code in
21483 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21484 	 * non-loopback copy and loopback copy for a given address.)
21485 	 */
21486 	for (irep1 = irep; irep1 > ire_array; ) {
21487 		irep1--;
21488 		if (*irep1 == NULL) {
21489 			ip0dbg(("ipif_check_bcast_ires: can't create "
21490 			    "IRE_BROADCAST, memory allocation failure\n"));
21491 			while (irep > ire_array) {
21492 				irep--;
21493 				if (*irep != NULL)
21494 					ire_delete(*irep);
21495 			}
21496 			return;
21497 		}
21498 	}
21499 
21500 	for (irep1 = irep; irep1 > ire_array; ) {
21501 		irep1--;
21502 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21503 			ire_refrele(*irep1);		/* Held in ire_add */
21504 	}
21505 }
21506 
21507 /*
21508  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21509  * from lifr_flags and the name from lifr_name.
21510  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21511  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21512  * Returns EINPROGRESS when mp has been consumed by queueing it on
21513  * ill_pending_mp and the ioctl will complete in ip_rput.
21514  *
21515  * Can operate on either a module or a driver queue.
21516  * Returns an error if not a module queue.
21517  */
21518 /* ARGSUSED */
21519 int
21520 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21521     ip_ioctl_cmd_t *ipip, void *if_req)
21522 {
21523 	ill_t	*ill = q->q_ptr;
21524 	phyint_t *phyi;
21525 	ip_stack_t *ipst;
21526 	struct lifreq *lifr = if_req;
21527 
21528 	ASSERT(ipif != NULL);
21529 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21530 
21531 	if (q->q_next == NULL) {
21532 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21533 		return (EINVAL);
21534 	}
21535 
21536 	/*
21537 	 * If we are not writer on 'q' then this interface exists already
21538 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21539 	 * so return EALREADY.
21540 	 */
21541 	if (ill != ipif->ipif_ill)
21542 		return (EALREADY);
21543 
21544 	if (ill->ill_name[0] != '\0')
21545 		return (EALREADY);
21546 
21547 	/*
21548 	 * Set all the flags. Allows all kinds of override. Provide some
21549 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21550 	 * unless there is either multicast/broadcast support in the driver
21551 	 * or it is a pt-pt link.
21552 	 */
21553 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21554 		/* Meaningless to IP thus don't allow them to be set. */
21555 		ip1dbg(("ip_setname: EINVAL 1\n"));
21556 		return (EINVAL);
21557 	}
21558 
21559 	/*
21560 	 * If there's another ill already with the requested name, ensure
21561 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21562 	 * fuse together two unrelated ills, which will cause chaos.
21563 	 */
21564 	ipst = ill->ill_ipst;
21565 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21566 	    lifr->lifr_name, NULL);
21567 	if (phyi != NULL) {
21568 		ill_t *ill_mate = phyi->phyint_illv4;
21569 
21570 		if (ill_mate == NULL)
21571 			ill_mate = phyi->phyint_illv6;
21572 		ASSERT(ill_mate != NULL);
21573 
21574 		if (ill_mate->ill_media->ip_m_mac_type !=
21575 		    ill->ill_media->ip_m_mac_type) {
21576 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21577 			    "use the same ill name on differing media\n"));
21578 			return (EINVAL);
21579 		}
21580 	}
21581 
21582 	/*
21583 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21584 	 * ill_bcast_addr_length info.
21585 	 */
21586 	if (!ill->ill_needs_attach &&
21587 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21588 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21589 	    ill->ill_bcast_addr_length == 0)) {
21590 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21591 		ip1dbg(("ip_setname: EINVAL 2\n"));
21592 		return (EINVAL);
21593 	}
21594 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21595 	    ((lifr->lifr_flags & IFF_IPV6) ||
21596 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21597 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21598 		ip1dbg(("ip_setname: EINVAL 3\n"));
21599 		return (EINVAL);
21600 	}
21601 	if (lifr->lifr_flags & IFF_UP) {
21602 		/* Can only be set with SIOCSLIFFLAGS */
21603 		ip1dbg(("ip_setname: EINVAL 4\n"));
21604 		return (EINVAL);
21605 	}
21606 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21607 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21608 		ip1dbg(("ip_setname: EINVAL 5\n"));
21609 		return (EINVAL);
21610 	}
21611 	/*
21612 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21613 	 */
21614 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21615 	    !(lifr->lifr_flags & IFF_IPV6) &&
21616 	    !(ipif->ipif_isv6)) {
21617 		ip1dbg(("ip_setname: EINVAL 6\n"));
21618 		return (EINVAL);
21619 	}
21620 
21621 	/*
21622 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21623 	 * we have all the flags here. So, we assign rather than we OR.
21624 	 * We can't OR the flags here because we don't want to set
21625 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21626 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21627 	 * on lifr_flags value here.
21628 	 */
21629 	/*
21630 	 * This ill has not been inserted into the global list.
21631 	 * So we are still single threaded and don't need any lock
21632 	 */
21633 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21634 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21635 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21636 
21637 	/* We started off as V4. */
21638 	if (ill->ill_flags & ILLF_IPV6) {
21639 		ill->ill_phyint->phyint_illv6 = ill;
21640 		ill->ill_phyint->phyint_illv4 = NULL;
21641 	}
21642 
21643 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21644 }
21645 
21646 /* ARGSUSED */
21647 int
21648 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21649     ip_ioctl_cmd_t *ipip, void *if_req)
21650 {
21651 	/*
21652 	 * ill_phyint_reinit merged the v4 and v6 into a single
21653 	 * ipsq. Could also have become part of a ipmp group in the
21654 	 * process, and we might not have been able to complete the
21655 	 * slifname in ipif_set_values, if we could not become
21656 	 * exclusive.  If so restart it here
21657 	 */
21658 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21659 }
21660 
21661 /*
21662  * Return a pointer to the ipif which matches the index, IP version type and
21663  * zoneid.
21664  */
21665 ipif_t *
21666 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21667     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21668 {
21669 	ill_t	*ill;
21670 	ipif_t	*ipif = NULL;
21671 
21672 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21673 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21674 
21675 	if (err != NULL)
21676 		*err = 0;
21677 
21678 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21679 	if (ill != NULL) {
21680 		mutex_enter(&ill->ill_lock);
21681 		for (ipif = ill->ill_ipif; ipif != NULL;
21682 		    ipif = ipif->ipif_next) {
21683 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21684 			    zoneid == ipif->ipif_zoneid ||
21685 			    ipif->ipif_zoneid == ALL_ZONES)) {
21686 				ipif_refhold_locked(ipif);
21687 				break;
21688 			}
21689 		}
21690 		mutex_exit(&ill->ill_lock);
21691 		ill_refrele(ill);
21692 		if (ipif == NULL && err != NULL)
21693 			*err = ENXIO;
21694 	}
21695 	return (ipif);
21696 }
21697 
21698 typedef struct conn_change_s {
21699 	uint_t cc_old_ifindex;
21700 	uint_t cc_new_ifindex;
21701 } conn_change_t;
21702 
21703 /*
21704  * ipcl_walk function for changing interface index.
21705  */
21706 static void
21707 conn_change_ifindex(conn_t *connp, caddr_t arg)
21708 {
21709 	conn_change_t *connc;
21710 	uint_t old_ifindex;
21711 	uint_t new_ifindex;
21712 	int i;
21713 	ilg_t *ilg;
21714 
21715 	connc = (conn_change_t *)arg;
21716 	old_ifindex = connc->cc_old_ifindex;
21717 	new_ifindex = connc->cc_new_ifindex;
21718 
21719 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21720 		connp->conn_orig_bound_ifindex = new_ifindex;
21721 
21722 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21723 		connp->conn_orig_multicast_ifindex = new_ifindex;
21724 
21725 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21726 		ilg = &connp->conn_ilg[i];
21727 		if (ilg->ilg_orig_ifindex == old_ifindex)
21728 			ilg->ilg_orig_ifindex = new_ifindex;
21729 	}
21730 }
21731 
21732 /*
21733  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21734  * to new_index if it matches the old_index.
21735  *
21736  * Failovers typically happen within a group of ills. But somebody
21737  * can remove an ill from the group after a failover happened. If
21738  * we are setting the ifindex after this, we potentially need to
21739  * look at all the ills rather than just the ones in the group.
21740  * We cut down the work by looking at matching ill_net_types
21741  * and ill_types as we could not possibly grouped them together.
21742  */
21743 static void
21744 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21745 {
21746 	ill_t *ill;
21747 	ipif_t *ipif;
21748 	uint_t old_ifindex;
21749 	uint_t new_ifindex;
21750 	ilm_t *ilm;
21751 	ill_walk_context_t ctx;
21752 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21753 
21754 	old_ifindex = connc->cc_old_ifindex;
21755 	new_ifindex = connc->cc_new_ifindex;
21756 
21757 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21758 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21759 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21760 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21761 		    (ill_orig->ill_type != ill->ill_type)) {
21762 			continue;
21763 		}
21764 		for (ipif = ill->ill_ipif; ipif != NULL;
21765 		    ipif = ipif->ipif_next) {
21766 			if (ipif->ipif_orig_ifindex == old_ifindex)
21767 				ipif->ipif_orig_ifindex = new_ifindex;
21768 		}
21769 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21770 			if (ilm->ilm_orig_ifindex == old_ifindex)
21771 				ilm->ilm_orig_ifindex = new_ifindex;
21772 		}
21773 	}
21774 	rw_exit(&ipst->ips_ill_g_lock);
21775 }
21776 
21777 /*
21778  * We first need to ensure that the new index is unique, and
21779  * then carry the change across both v4 and v6 ill representation
21780  * of the physical interface.
21781  */
21782 /* ARGSUSED */
21783 int
21784 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21785     ip_ioctl_cmd_t *ipip, void *ifreq)
21786 {
21787 	ill_t		*ill;
21788 	ill_t		*ill_other;
21789 	phyint_t	*phyi;
21790 	int		old_index;
21791 	conn_change_t	connc;
21792 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21793 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21794 	uint_t	index;
21795 	ill_t	*ill_v4;
21796 	ill_t	*ill_v6;
21797 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21798 
21799 	if (ipip->ipi_cmd_type == IF_CMD)
21800 		index = ifr->ifr_index;
21801 	else
21802 		index = lifr->lifr_index;
21803 
21804 	/*
21805 	 * Only allow on physical interface. Also, index zero is illegal.
21806 	 *
21807 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21808 	 *
21809 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21810 	 *    implies a possible failback might have to happen. As failback
21811 	 *    depends on the old index, we should fail setting the index.
21812 	 *
21813 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21814 	 *    any addresses or multicast memberships are failed over to
21815 	 *    a non-STANDBY interface. As failback depends on the old
21816 	 *    index, we should fail setting the index for this case also.
21817 	 *
21818 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21819 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21820 	 */
21821 	ill = ipif->ipif_ill;
21822 	phyi = ill->ill_phyint;
21823 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21824 	    ipif->ipif_id != 0 || index == 0) {
21825 		return (EINVAL);
21826 	}
21827 	old_index = phyi->phyint_ifindex;
21828 
21829 	/* If the index is not changing, no work to do */
21830 	if (old_index == index)
21831 		return (0);
21832 
21833 	/*
21834 	 * Use ill_lookup_on_ifindex to determine if the
21835 	 * new index is unused and if so allow the change.
21836 	 */
21837 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21838 	    ipst);
21839 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21840 	    ipst);
21841 	if (ill_v6 != NULL || ill_v4 != NULL) {
21842 		if (ill_v4 != NULL)
21843 			ill_refrele(ill_v4);
21844 		if (ill_v6 != NULL)
21845 			ill_refrele(ill_v6);
21846 		return (EBUSY);
21847 	}
21848 
21849 	/*
21850 	 * The new index is unused. Set it in the phyint.
21851 	 * Locate the other ill so that we can send a routing
21852 	 * sockets message.
21853 	 */
21854 	if (ill->ill_isv6) {
21855 		ill_other = phyi->phyint_illv4;
21856 	} else {
21857 		ill_other = phyi->phyint_illv6;
21858 	}
21859 
21860 	phyi->phyint_ifindex = index;
21861 
21862 	/* Update SCTP's ILL list */
21863 	sctp_ill_reindex(ill, old_index);
21864 
21865 	connc.cc_old_ifindex = old_index;
21866 	connc.cc_new_ifindex = index;
21867 	ip_change_ifindex(ill, &connc);
21868 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21869 
21870 	/* Send the routing sockets message */
21871 	ip_rts_ifmsg(ipif);
21872 	if (ill_other != NULL)
21873 		ip_rts_ifmsg(ill_other->ill_ipif);
21874 
21875 	return (0);
21876 }
21877 
21878 /* ARGSUSED */
21879 int
21880 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21881     ip_ioctl_cmd_t *ipip, void *ifreq)
21882 {
21883 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21884 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21885 
21886 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21887 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21888 	/* Get the interface index */
21889 	if (ipip->ipi_cmd_type == IF_CMD) {
21890 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21891 	} else {
21892 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21893 	}
21894 	return (0);
21895 }
21896 
21897 /* ARGSUSED */
21898 int
21899 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21900     ip_ioctl_cmd_t *ipip, void *ifreq)
21901 {
21902 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21903 
21904 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21905 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21906 	/* Get the interface zone */
21907 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21908 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21909 	return (0);
21910 }
21911 
21912 /*
21913  * Set the zoneid of an interface.
21914  */
21915 /* ARGSUSED */
21916 int
21917 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21918     ip_ioctl_cmd_t *ipip, void *ifreq)
21919 {
21920 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21921 	int err = 0;
21922 	boolean_t need_up = B_FALSE;
21923 	zone_t *zptr;
21924 	zone_status_t status;
21925 	zoneid_t zoneid;
21926 
21927 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21928 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21929 		if (!is_system_labeled())
21930 			return (ENOTSUP);
21931 		zoneid = GLOBAL_ZONEID;
21932 	}
21933 
21934 	/* cannot assign instance zero to a non-global zone */
21935 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21936 		return (ENOTSUP);
21937 
21938 	/*
21939 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21940 	 * the event of a race with the zone shutdown processing, since IP
21941 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21942 	 * interface will be cleaned up even if the zone is shut down
21943 	 * immediately after the status check. If the interface can't be brought
21944 	 * down right away, and the zone is shut down before the restart
21945 	 * function is called, we resolve the possible races by rechecking the
21946 	 * zone status in the restart function.
21947 	 */
21948 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21949 		return (EINVAL);
21950 	status = zone_status_get(zptr);
21951 	zone_rele(zptr);
21952 
21953 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21954 		return (EINVAL);
21955 
21956 	if (ipif->ipif_flags & IPIF_UP) {
21957 		/*
21958 		 * If the interface is already marked up,
21959 		 * we call ipif_down which will take care
21960 		 * of ditching any IREs that have been set
21961 		 * up based on the old interface address.
21962 		 */
21963 		err = ipif_logical_down(ipif, q, mp);
21964 		if (err == EINPROGRESS)
21965 			return (err);
21966 		ipif_down_tail(ipif);
21967 		need_up = B_TRUE;
21968 	}
21969 
21970 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21971 	return (err);
21972 }
21973 
21974 static int
21975 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21976     queue_t *q, mblk_t *mp, boolean_t need_up)
21977 {
21978 	int	err = 0;
21979 	ip_stack_t	*ipst;
21980 
21981 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21982 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21983 
21984 	if (CONN_Q(q))
21985 		ipst = CONNQ_TO_IPST(q);
21986 	else
21987 		ipst = ILLQ_TO_IPST(q);
21988 
21989 	/*
21990 	 * For exclusive stacks we don't allow a different zoneid than
21991 	 * global.
21992 	 */
21993 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
21994 	    zoneid != GLOBAL_ZONEID)
21995 		return (EINVAL);
21996 
21997 	/* Set the new zone id. */
21998 	ipif->ipif_zoneid = zoneid;
21999 
22000 	/* Update sctp list */
22001 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22002 
22003 	if (need_up) {
22004 		/*
22005 		 * Now bring the interface back up.  If this
22006 		 * is the only IPIF for the ILL, ipif_up
22007 		 * will have to re-bind to the device, so
22008 		 * we may get back EINPROGRESS, in which
22009 		 * case, this IOCTL will get completed in
22010 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22011 		 */
22012 		err = ipif_up(ipif, q, mp);
22013 	}
22014 	return (err);
22015 }
22016 
22017 /* ARGSUSED */
22018 int
22019 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22020     ip_ioctl_cmd_t *ipip, void *if_req)
22021 {
22022 	struct lifreq *lifr = (struct lifreq *)if_req;
22023 	zoneid_t zoneid;
22024 	zone_t *zptr;
22025 	zone_status_t status;
22026 
22027 	ASSERT(ipif->ipif_id != 0);
22028 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22029 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22030 		zoneid = GLOBAL_ZONEID;
22031 
22032 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22033 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22034 
22035 	/*
22036 	 * We recheck the zone status to resolve the following race condition:
22037 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22038 	 * 2) hme0:1 is up and can't be brought down right away;
22039 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22040 	 * 3) zone "myzone" is halted; the zone status switches to
22041 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22042 	 * the interfaces to remove - hme0:1 is not returned because it's not
22043 	 * yet in "myzone", so it won't be removed;
22044 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22045 	 * status check here, we would have hme0:1 in "myzone" after it's been
22046 	 * destroyed.
22047 	 * Note that if the status check fails, we need to bring the interface
22048 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22049 	 * ipif_up_done[_v6]().
22050 	 */
22051 	status = ZONE_IS_UNINITIALIZED;
22052 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22053 		status = zone_status_get(zptr);
22054 		zone_rele(zptr);
22055 	}
22056 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22057 		if (ipif->ipif_isv6) {
22058 			(void) ipif_up_done_v6(ipif);
22059 		} else {
22060 			(void) ipif_up_done(ipif);
22061 		}
22062 		return (EINVAL);
22063 	}
22064 
22065 	ipif_down_tail(ipif);
22066 
22067 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22068 	    B_TRUE));
22069 }
22070 
22071 /* ARGSUSED */
22072 int
22073 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22074 	ip_ioctl_cmd_t *ipip, void *ifreq)
22075 {
22076 	struct lifreq	*lifr = ifreq;
22077 
22078 	ASSERT(q->q_next == NULL);
22079 	ASSERT(CONN_Q(q));
22080 
22081 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22082 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22083 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22084 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22085 
22086 	return (0);
22087 }
22088 
22089 
22090 /* Find the previous ILL in this usesrc group */
22091 static ill_t *
22092 ill_prev_usesrc(ill_t *uill)
22093 {
22094 	ill_t *ill;
22095 
22096 	for (ill = uill->ill_usesrc_grp_next;
22097 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22098 	    ill = ill->ill_usesrc_grp_next)
22099 		/* do nothing */;
22100 	return (ill);
22101 }
22102 
22103 /*
22104  * Release all members of the usesrc group. This routine is called
22105  * from ill_delete when the interface being unplumbed is the
22106  * group head.
22107  */
22108 static void
22109 ill_disband_usesrc_group(ill_t *uill)
22110 {
22111 	ill_t *next_ill, *tmp_ill;
22112 	ip_stack_t	*ipst = uill->ill_ipst;
22113 
22114 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22115 	next_ill = uill->ill_usesrc_grp_next;
22116 
22117 	do {
22118 		ASSERT(next_ill != NULL);
22119 		tmp_ill = next_ill->ill_usesrc_grp_next;
22120 		ASSERT(tmp_ill != NULL);
22121 		next_ill->ill_usesrc_grp_next = NULL;
22122 		next_ill->ill_usesrc_ifindex = 0;
22123 		next_ill = tmp_ill;
22124 	} while (next_ill->ill_usesrc_ifindex != 0);
22125 	uill->ill_usesrc_grp_next = NULL;
22126 }
22127 
22128 /*
22129  * Remove the client usesrc ILL from the list and relink to a new list
22130  */
22131 int
22132 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22133 {
22134 	ill_t *ill, *tmp_ill;
22135 	ip_stack_t	*ipst = ucill->ill_ipst;
22136 
22137 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22138 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22139 
22140 	/*
22141 	 * Check if the usesrc client ILL passed in is not already
22142 	 * in use as a usesrc ILL i.e one whose source address is
22143 	 * in use OR a usesrc ILL is not already in use as a usesrc
22144 	 * client ILL
22145 	 */
22146 	if ((ucill->ill_usesrc_ifindex == 0) ||
22147 	    (uill->ill_usesrc_ifindex != 0)) {
22148 		return (-1);
22149 	}
22150 
22151 	ill = ill_prev_usesrc(ucill);
22152 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22153 
22154 	/* Remove from the current list */
22155 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22156 		/* Only two elements in the list */
22157 		ASSERT(ill->ill_usesrc_ifindex == 0);
22158 		ill->ill_usesrc_grp_next = NULL;
22159 	} else {
22160 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22161 	}
22162 
22163 	if (ifindex == 0) {
22164 		ucill->ill_usesrc_ifindex = 0;
22165 		ucill->ill_usesrc_grp_next = NULL;
22166 		return (0);
22167 	}
22168 
22169 	ucill->ill_usesrc_ifindex = ifindex;
22170 	tmp_ill = uill->ill_usesrc_grp_next;
22171 	uill->ill_usesrc_grp_next = ucill;
22172 	ucill->ill_usesrc_grp_next =
22173 	    (tmp_ill != NULL) ? tmp_ill : uill;
22174 	return (0);
22175 }
22176 
22177 /*
22178  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22179  * ip.c for locking details.
22180  */
22181 /* ARGSUSED */
22182 int
22183 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22184     ip_ioctl_cmd_t *ipip, void *ifreq)
22185 {
22186 	struct lifreq *lifr = (struct lifreq *)ifreq;
22187 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22188 	    ill_flag_changed = B_FALSE;
22189 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22190 	int err = 0, ret;
22191 	uint_t ifindex;
22192 	phyint_t *us_phyint, *us_cli_phyint;
22193 	ipsq_t *ipsq = NULL;
22194 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22195 
22196 	ASSERT(IAM_WRITER_IPIF(ipif));
22197 	ASSERT(q->q_next == NULL);
22198 	ASSERT(CONN_Q(q));
22199 
22200 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22201 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22202 
22203 	ASSERT(us_cli_phyint != NULL);
22204 
22205 	/*
22206 	 * If the client ILL is being used for IPMP, abort.
22207 	 * Note, this can be done before ipsq_try_enter since we are already
22208 	 * exclusive on this ILL
22209 	 */
22210 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22211 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22212 		return (EINVAL);
22213 	}
22214 
22215 	ifindex = lifr->lifr_index;
22216 	if (ifindex == 0) {
22217 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22218 			/* non usesrc group interface, nothing to reset */
22219 			return (0);
22220 		}
22221 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22222 		/* valid reset request */
22223 		reset_flg = B_TRUE;
22224 	}
22225 
22226 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22227 	    ip_process_ioctl, &err, ipst);
22228 
22229 	if (usesrc_ill == NULL) {
22230 		return (err);
22231 	}
22232 
22233 	/*
22234 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22235 	 * group nor can either of the interfaces be used for standy. So
22236 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22237 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22238 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22239 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22240 	 * the usesrc_cli_ill
22241 	 */
22242 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22243 	    NEW_OP, B_TRUE);
22244 	if (ipsq == NULL) {
22245 		err = EINPROGRESS;
22246 		/* Operation enqueued on the ipsq of the usesrc ILL */
22247 		goto done;
22248 	}
22249 
22250 	/* Check if the usesrc_ill is used for IPMP */
22251 	us_phyint = usesrc_ill->ill_phyint;
22252 	if ((us_phyint->phyint_groupname != NULL) ||
22253 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22254 		err = EINVAL;
22255 		goto done;
22256 	}
22257 
22258 	/*
22259 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22260 	 * already a client then return EINVAL
22261 	 */
22262 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22263 		err = EINVAL;
22264 		goto done;
22265 	}
22266 
22267 	/*
22268 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22269 	 * be then this is a duplicate operation.
22270 	 */
22271 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22272 		err = 0;
22273 		goto done;
22274 	}
22275 
22276 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22277 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22278 	    usesrc_ill->ill_isv6));
22279 
22280 	/*
22281 	 * The next step ensures that no new ires will be created referencing
22282 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22283 	 * we go through an ire walk deleting all ire caches that reference
22284 	 * the client ill. New ires referencing the client ill that are added
22285 	 * to the ire table before the ILL_CHANGING flag is set, will be
22286 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22287 	 * the client ill while the ILL_CHANGING flag is set will be failed
22288 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22289 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22290 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22291 	 * belong to the same usesrc group.
22292 	 */
22293 	mutex_enter(&usesrc_cli_ill->ill_lock);
22294 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22295 	mutex_exit(&usesrc_cli_ill->ill_lock);
22296 	ill_flag_changed = B_TRUE;
22297 
22298 	if (ipif->ipif_isv6)
22299 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22300 		    ALL_ZONES, ipst);
22301 	else
22302 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22303 		    ALL_ZONES, ipst);
22304 
22305 	/*
22306 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22307 	 * and the ill_usesrc_ifindex fields
22308 	 */
22309 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22310 
22311 	if (reset_flg) {
22312 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22313 		if (ret != 0) {
22314 			err = EINVAL;
22315 		}
22316 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22317 		goto done;
22318 	}
22319 
22320 	/*
22321 	 * Four possibilities to consider:
22322 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22323 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22324 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22325 	 * 4. Both are part of their respective usesrc groups
22326 	 */
22327 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22328 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22329 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22330 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22331 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22332 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22333 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22334 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22335 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22336 		/* Insert at head of list */
22337 		usesrc_cli_ill->ill_usesrc_grp_next =
22338 		    usesrc_ill->ill_usesrc_grp_next;
22339 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22340 	} else {
22341 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22342 		    ifindex);
22343 		if (ret != 0)
22344 			err = EINVAL;
22345 	}
22346 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22347 
22348 done:
22349 	if (ill_flag_changed) {
22350 		mutex_enter(&usesrc_cli_ill->ill_lock);
22351 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22352 		mutex_exit(&usesrc_cli_ill->ill_lock);
22353 	}
22354 	if (ipsq != NULL)
22355 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22356 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22357 	ill_refrele(usesrc_ill);
22358 	return (err);
22359 }
22360 
22361 /*
22362  * comparison function used by avl.
22363  */
22364 static int
22365 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22366 {
22367 
22368 	uint_t index;
22369 
22370 	ASSERT(phyip != NULL && index_ptr != NULL);
22371 
22372 	index = *((uint_t *)index_ptr);
22373 	/*
22374 	 * let the phyint with the lowest index be on top.
22375 	 */
22376 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22377 		return (1);
22378 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22379 		return (-1);
22380 	return (0);
22381 }
22382 
22383 /*
22384  * comparison function used by avl.
22385  */
22386 static int
22387 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22388 {
22389 	ill_t *ill;
22390 	int res = 0;
22391 
22392 	ASSERT(phyip != NULL && name_ptr != NULL);
22393 
22394 	if (((phyint_t *)phyip)->phyint_illv4)
22395 		ill = ((phyint_t *)phyip)->phyint_illv4;
22396 	else
22397 		ill = ((phyint_t *)phyip)->phyint_illv6;
22398 	ASSERT(ill != NULL);
22399 
22400 	res = strcmp(ill->ill_name, (char *)name_ptr);
22401 	if (res > 0)
22402 		return (1);
22403 	else if (res < 0)
22404 		return (-1);
22405 	return (0);
22406 }
22407 /*
22408  * This function is called from ill_delete when the ill is being
22409  * unplumbed. We remove the reference from the phyint and we also
22410  * free the phyint when there are no more references to it.
22411  */
22412 static void
22413 ill_phyint_free(ill_t *ill)
22414 {
22415 	phyint_t *phyi;
22416 	phyint_t *next_phyint;
22417 	ipsq_t *cur_ipsq;
22418 	ip_stack_t	*ipst = ill->ill_ipst;
22419 
22420 	ASSERT(ill->ill_phyint != NULL);
22421 
22422 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22423 	phyi = ill->ill_phyint;
22424 	ill->ill_phyint = NULL;
22425 	/*
22426 	 * ill_init allocates a phyint always to store the copy
22427 	 * of flags relevant to phyint. At that point in time, we could
22428 	 * not assign the name and hence phyint_illv4/v6 could not be
22429 	 * initialized. Later in ipif_set_values, we assign the name to
22430 	 * the ill, at which point in time we assign phyint_illv4/v6.
22431 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22432 	 */
22433 	if (ill->ill_flags & ILLF_IPV6) {
22434 		phyi->phyint_illv6 = NULL;
22435 	} else {
22436 		phyi->phyint_illv4 = NULL;
22437 	}
22438 	/*
22439 	 * ipif_down removes it from the group when the last ipif goes
22440 	 * down.
22441 	 */
22442 	ASSERT(ill->ill_group == NULL);
22443 
22444 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22445 		return;
22446 
22447 	/*
22448 	 * Make sure this phyint was put in the list.
22449 	 */
22450 	if (phyi->phyint_ifindex > 0) {
22451 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22452 		    phyi);
22453 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22454 		    phyi);
22455 	}
22456 	/*
22457 	 * remove phyint from the ipsq list.
22458 	 */
22459 	cur_ipsq = phyi->phyint_ipsq;
22460 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22461 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22462 	} else {
22463 		next_phyint = cur_ipsq->ipsq_phyint_list;
22464 		while (next_phyint != NULL) {
22465 			if (next_phyint->phyint_ipsq_next == phyi) {
22466 				next_phyint->phyint_ipsq_next =
22467 				    phyi->phyint_ipsq_next;
22468 				break;
22469 			}
22470 			next_phyint = next_phyint->phyint_ipsq_next;
22471 		}
22472 		ASSERT(next_phyint != NULL);
22473 	}
22474 	IPSQ_DEC_REF(cur_ipsq, ipst);
22475 
22476 	if (phyi->phyint_groupname_len != 0) {
22477 		ASSERT(phyi->phyint_groupname != NULL);
22478 		mi_free(phyi->phyint_groupname);
22479 	}
22480 	mi_free(phyi);
22481 }
22482 
22483 /*
22484  * Attach the ill to the phyint structure which can be shared by both
22485  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22486  * function is called from ipif_set_values and ill_lookup_on_name (for
22487  * loopback) where we know the name of the ill. We lookup the ill and if
22488  * there is one present already with the name use that phyint. Otherwise
22489  * reuse the one allocated by ill_init.
22490  */
22491 static void
22492 ill_phyint_reinit(ill_t *ill)
22493 {
22494 	boolean_t isv6 = ill->ill_isv6;
22495 	phyint_t *phyi_old;
22496 	phyint_t *phyi;
22497 	avl_index_t where = 0;
22498 	ill_t	*ill_other = NULL;
22499 	ipsq_t	*ipsq;
22500 	ip_stack_t	*ipst = ill->ill_ipst;
22501 
22502 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22503 
22504 	phyi_old = ill->ill_phyint;
22505 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22506 	    phyi_old->phyint_illv6 == NULL));
22507 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22508 	    phyi_old->phyint_illv4 == NULL));
22509 	ASSERT(phyi_old->phyint_ifindex == 0);
22510 
22511 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22512 	    ill->ill_name, &where);
22513 
22514 	/*
22515 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22516 	 *    the global list of ills. So no other thread could have located
22517 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22518 	 * 2. Now locate the other protocol instance of this ill.
22519 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22520 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22521 	 *    of neither ill can change.
22522 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22523 	 *    other ill.
22524 	 * 5. Release all locks.
22525 	 */
22526 
22527 	/*
22528 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22529 	 * we are initializing IPv4.
22530 	 */
22531 	if (phyi != NULL) {
22532 		ill_other = (isv6) ? phyi->phyint_illv4 :
22533 		    phyi->phyint_illv6;
22534 		ASSERT(ill_other->ill_phyint != NULL);
22535 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22536 		    (!isv6 && ill_other->ill_isv6));
22537 		GRAB_ILL_LOCKS(ill, ill_other);
22538 		/*
22539 		 * We are potentially throwing away phyint_flags which
22540 		 * could be different from the one that we obtain from
22541 		 * ill_other->ill_phyint. But it is okay as we are assuming
22542 		 * that the state maintained within IP is correct.
22543 		 */
22544 		mutex_enter(&phyi->phyint_lock);
22545 		if (isv6) {
22546 			ASSERT(phyi->phyint_illv6 == NULL);
22547 			phyi->phyint_illv6 = ill;
22548 		} else {
22549 			ASSERT(phyi->phyint_illv4 == NULL);
22550 			phyi->phyint_illv4 = ill;
22551 		}
22552 		/*
22553 		 * This is a new ill, currently undergoing SLIFNAME
22554 		 * So we could not have joined an IPMP group until now.
22555 		 */
22556 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22557 		    phyi_old->phyint_groupname == NULL);
22558 
22559 		/*
22560 		 * This phyi_old is going away. Decref ipsq_refs and
22561 		 * assert it is zero. The ipsq itself will be freed in
22562 		 * ipsq_exit
22563 		 */
22564 		ipsq = phyi_old->phyint_ipsq;
22565 		IPSQ_DEC_REF(ipsq, ipst);
22566 		ASSERT(ipsq->ipsq_refs == 0);
22567 		/* Get the singleton phyint out of the ipsq list */
22568 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22569 		ipsq->ipsq_phyint_list = NULL;
22570 		phyi_old->phyint_illv4 = NULL;
22571 		phyi_old->phyint_illv6 = NULL;
22572 		mi_free(phyi_old);
22573 	} else {
22574 		mutex_enter(&ill->ill_lock);
22575 		/*
22576 		 * We don't need to acquire any lock, since
22577 		 * the ill is not yet visible globally  and we
22578 		 * have not yet released the ill_g_lock.
22579 		 */
22580 		phyi = phyi_old;
22581 		mutex_enter(&phyi->phyint_lock);
22582 		/* XXX We need a recovery strategy here. */
22583 		if (!phyint_assign_ifindex(phyi, ipst))
22584 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22585 
22586 		/* No IPMP group yet, thus the hook uses the ifindex */
22587 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22588 
22589 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22590 		    (void *)phyi, where);
22591 
22592 		(void) avl_find(&ipst->ips_phyint_g_list->
22593 		    phyint_list_avl_by_index,
22594 		    &phyi->phyint_ifindex, &where);
22595 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22596 		    (void *)phyi, where);
22597 	}
22598 
22599 	/*
22600 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22601 	 * pending mp is not affected because that is per ill basis.
22602 	 */
22603 	ill->ill_phyint = phyi;
22604 
22605 	/*
22606 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22607 	 * We do this here as when the first ipif was allocated,
22608 	 * ipif_allocate does not know the right interface index.
22609 	 */
22610 
22611 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22612 	/*
22613 	 * Now that the phyint's ifindex has been assigned, complete the
22614 	 * remaining
22615 	 */
22616 
22617 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22618 	if (ill->ill_isv6) {
22619 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22620 		    ill->ill_phyint->phyint_ifindex;
22621 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22622 	} else {
22623 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22624 	}
22625 
22626 	/*
22627 	 * Generate an event within the hooks framework to indicate that
22628 	 * a new interface has just been added to IP.  For this event to
22629 	 * be generated, the network interface must, at least, have an
22630 	 * ifindex assigned to it.
22631 	 *
22632 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22633 	 * that the ordering of delivered events to listeners matches the
22634 	 * order of them in the kernel.
22635 	 *
22636 	 * This function could be called from ill_lookup_on_name. In that case
22637 	 * the interface is loopback "lo", which will not generate a NIC event.
22638 	 */
22639 	if (ill->ill_name_length <= 2 ||
22640 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22641 		/*
22642 		 * Generate nic plumb event for ill_name even if
22643 		 * ipmp_hook_emulation is set. That avoids generating events
22644 		 * for the ill_names should ipmp_hook_emulation be turned on
22645 		 * later.
22646 		 */
22647 		ill_nic_info_plumb(ill, B_FALSE);
22648 	}
22649 	RELEASE_ILL_LOCKS(ill, ill_other);
22650 	mutex_exit(&phyi->phyint_lock);
22651 }
22652 
22653 /*
22654  * Allocate a NE_PLUMB nic info event and store in the ill.
22655  * If 'group' is set we do it for the group name, otherwise the ill name.
22656  * It will be sent when we leave the ipsq.
22657  */
22658 void
22659 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22660 {
22661 	phyint_t	*phyi = ill->ill_phyint;
22662 	char		*name;
22663 	int		namelen;
22664 
22665 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22666 
22667 	if (group) {
22668 		ASSERT(phyi->phyint_groupname_len != 0);
22669 		namelen = phyi->phyint_groupname_len;
22670 		name = phyi->phyint_groupname;
22671 	} else {
22672 		namelen = ill->ill_name_length;
22673 		name = ill->ill_name;
22674 	}
22675 
22676 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22677 }
22678 
22679 /*
22680  * Unhook the nic event message from the ill and enqueue it
22681  * into the nic event taskq.
22682  */
22683 void
22684 ill_nic_info_dispatch(ill_t *ill)
22685 {
22686 	hook_nic_event_t *info;
22687 
22688 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22689 
22690 	if ((info = ill->ill_nic_event_info) != NULL) {
22691 		if (ddi_taskq_dispatch(eventq_queue_nic,
22692 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22693 			ip2dbg(("ill_nic_info_dispatch: "
22694 			    "ddi_taskq_dispatch failed\n"));
22695 			if (info->hne_data != NULL)
22696 				kmem_free(info->hne_data, info->hne_datalen);
22697 			kmem_free(info, sizeof (hook_nic_event_t));
22698 		}
22699 		ill->ill_nic_event_info = NULL;
22700 	}
22701 }
22702 
22703 /*
22704  * Notify any downstream modules of the name of this interface.
22705  * An M_IOCTL is used even though we don't expect a successful reply.
22706  * Any reply message from the driver (presumably an M_IOCNAK) will
22707  * eventually get discarded somewhere upstream.  The message format is
22708  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22709  * to IP.
22710  */
22711 static void
22712 ip_ifname_notify(ill_t *ill, queue_t *q)
22713 {
22714 	mblk_t *mp1, *mp2;
22715 	struct iocblk *iocp;
22716 	struct lifreq *lifr;
22717 
22718 	mp1 = mkiocb(SIOCSLIFNAME);
22719 	if (mp1 == NULL)
22720 		return;
22721 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22722 	if (mp2 == NULL) {
22723 		freeb(mp1);
22724 		return;
22725 	}
22726 
22727 	mp1->b_cont = mp2;
22728 	iocp = (struct iocblk *)mp1->b_rptr;
22729 	iocp->ioc_count = sizeof (struct lifreq);
22730 
22731 	lifr = (struct lifreq *)mp2->b_rptr;
22732 	mp2->b_wptr += sizeof (struct lifreq);
22733 	bzero(lifr, sizeof (struct lifreq));
22734 
22735 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22736 	lifr->lifr_ppa = ill->ill_ppa;
22737 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22738 
22739 	putnext(q, mp1);
22740 }
22741 
22742 static int
22743 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22744 {
22745 	int err;
22746 	ip_stack_t	*ipst = ill->ill_ipst;
22747 
22748 	/* Set the obsolete NDD per-interface forwarding name. */
22749 	err = ill_set_ndd_name(ill);
22750 	if (err != 0) {
22751 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22752 		    err);
22753 	}
22754 
22755 	/* Tell downstream modules where they are. */
22756 	ip_ifname_notify(ill, q);
22757 
22758 	/*
22759 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22760 	 * Error cases are ENOMEM ...
22761 	 */
22762 	err = ill_dl_phys(ill, ipif, mp, q);
22763 
22764 	/*
22765 	 * If there is no IRE expiration timer running, get one started.
22766 	 * igmp and mld timers will be triggered by the first multicast
22767 	 */
22768 	if (ipst->ips_ip_ire_expire_id == 0) {
22769 		/*
22770 		 * acquire the lock and check again.
22771 		 */
22772 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22773 		if (ipst->ips_ip_ire_expire_id == 0) {
22774 			ipst->ips_ip_ire_expire_id = timeout(
22775 			    ip_trash_timer_expire, ipst,
22776 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22777 		}
22778 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22779 	}
22780 
22781 	if (ill->ill_isv6) {
22782 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22783 		if (ipst->ips_mld_slowtimeout_id == 0) {
22784 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22785 			    (void *)ipst,
22786 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22787 		}
22788 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22789 	} else {
22790 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22791 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22792 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22793 			    (void *)ipst,
22794 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22795 		}
22796 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22797 	}
22798 
22799 	return (err);
22800 }
22801 
22802 /*
22803  * Common routine for ppa and ifname setting. Should be called exclusive.
22804  *
22805  * Returns EINPROGRESS when mp has been consumed by queueing it on
22806  * ill_pending_mp and the ioctl will complete in ip_rput.
22807  *
22808  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22809  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22810  * For SLIFNAME, we pass these values back to the userland.
22811  */
22812 static int
22813 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22814 {
22815 	ill_t	*ill;
22816 	ipif_t	*ipif;
22817 	ipsq_t	*ipsq;
22818 	char	*ppa_ptr;
22819 	char	*old_ptr;
22820 	char	old_char;
22821 	int	error;
22822 	ip_stack_t	*ipst;
22823 
22824 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22825 	ASSERT(q->q_next != NULL);
22826 	ASSERT(interf_name != NULL);
22827 
22828 	ill = (ill_t *)q->q_ptr;
22829 	ipst = ill->ill_ipst;
22830 
22831 	ASSERT(ill->ill_ipst != NULL);
22832 	ASSERT(ill->ill_name[0] == '\0');
22833 	ASSERT(IAM_WRITER_ILL(ill));
22834 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22835 	ASSERT(ill->ill_ppa == UINT_MAX);
22836 
22837 	/* The ppa is sent down by ifconfig or is chosen */
22838 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22839 		return (EINVAL);
22840 	}
22841 
22842 	/*
22843 	 * make sure ppa passed in is same as ppa in the name.
22844 	 * This check is not made when ppa == UINT_MAX in that case ppa
22845 	 * in the name could be anything. System will choose a ppa and
22846 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22847 	 */
22848 	if (*new_ppa_ptr != UINT_MAX) {
22849 		/* stoi changes the pointer */
22850 		old_ptr = ppa_ptr;
22851 		/*
22852 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22853 		 * (they don't have an externally visible ppa).  We assign one
22854 		 * here so that we can manage the interface.  Note that in
22855 		 * the past this value was always 0 for DLPI 1 drivers.
22856 		 */
22857 		if (*new_ppa_ptr == 0)
22858 			*new_ppa_ptr = stoi(&old_ptr);
22859 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22860 			return (EINVAL);
22861 	}
22862 	/*
22863 	 * terminate string before ppa
22864 	 * save char at that location.
22865 	 */
22866 	old_char = ppa_ptr[0];
22867 	ppa_ptr[0] = '\0';
22868 
22869 	ill->ill_ppa = *new_ppa_ptr;
22870 	/*
22871 	 * Finish as much work now as possible before calling ill_glist_insert
22872 	 * which makes the ill globally visible and also merges it with the
22873 	 * other protocol instance of this phyint. The remaining work is
22874 	 * done after entering the ipsq which may happen sometime later.
22875 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22876 	 */
22877 	ipif = ill->ill_ipif;
22878 
22879 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22880 	ipif_assign_seqid(ipif);
22881 
22882 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22883 		ill->ill_flags |= ILLF_IPV4;
22884 
22885 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22886 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22887 
22888 	if (ill->ill_flags & ILLF_IPV6) {
22889 
22890 		ill->ill_isv6 = B_TRUE;
22891 		if (ill->ill_rq != NULL) {
22892 			ill->ill_rq->q_qinfo = &iprinitv6;
22893 			ill->ill_wq->q_qinfo = &ipwinitv6;
22894 		}
22895 
22896 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22897 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22898 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22899 		ipif->ipif_v6subnet = ipv6_all_zeros;
22900 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22901 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22902 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22903 		/*
22904 		 * point-to-point or Non-mulicast capable
22905 		 * interfaces won't do NUD unless explicitly
22906 		 * configured to do so.
22907 		 */
22908 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22909 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22910 			ill->ill_flags |= ILLF_NONUD;
22911 		}
22912 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22913 		if (ill->ill_flags & ILLF_NOARP) {
22914 			/*
22915 			 * Note: xresolv interfaces will eventually need
22916 			 * NOARP set here as well, but that will require
22917 			 * those external resolvers to have some
22918 			 * knowledge of that flag and act appropriately.
22919 			 * Not to be changed at present.
22920 			 */
22921 			ill->ill_flags &= ~ILLF_NOARP;
22922 		}
22923 		/*
22924 		 * Set the ILLF_ROUTER flag according to the global
22925 		 * IPv6 forwarding policy.
22926 		 */
22927 		if (ipst->ips_ipv6_forward != 0)
22928 			ill->ill_flags |= ILLF_ROUTER;
22929 	} else if (ill->ill_flags & ILLF_IPV4) {
22930 		ill->ill_isv6 = B_FALSE;
22931 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22932 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22933 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22934 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22935 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22936 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22937 		/*
22938 		 * Set the ILLF_ROUTER flag according to the global
22939 		 * IPv4 forwarding policy.
22940 		 */
22941 		if (ipst->ips_ip_g_forward != 0)
22942 			ill->ill_flags |= ILLF_ROUTER;
22943 	}
22944 
22945 	ASSERT(ill->ill_phyint != NULL);
22946 
22947 	/*
22948 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22949 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22950 	 */
22951 	if (!ill_allocate_mibs(ill))
22952 		return (ENOMEM);
22953 
22954 	/*
22955 	 * Pick a default sap until we get the DL_INFO_ACK back from
22956 	 * the driver.
22957 	 */
22958 	if (ill->ill_sap == 0) {
22959 		if (ill->ill_isv6)
22960 			ill->ill_sap  = IP6_DL_SAP;
22961 		else
22962 			ill->ill_sap  = IP_DL_SAP;
22963 	}
22964 
22965 	ill->ill_ifname_pending = 1;
22966 	ill->ill_ifname_pending_err = 0;
22967 
22968 	ill_refhold(ill);
22969 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
22970 	if ((error = ill_glist_insert(ill, interf_name,
22971 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22972 		ill->ill_ppa = UINT_MAX;
22973 		ill->ill_name[0] = '\0';
22974 		/*
22975 		 * undo null termination done above.
22976 		 */
22977 		ppa_ptr[0] = old_char;
22978 		rw_exit(&ipst->ips_ill_g_lock);
22979 		ill_refrele(ill);
22980 		return (error);
22981 	}
22982 
22983 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
22984 
22985 	/*
22986 	 * When we return the buffer pointed to by interf_name should contain
22987 	 * the same name as in ill_name.
22988 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
22989 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
22990 	 * so copy full name and update the ppa ptr.
22991 	 * When ppa passed in != UINT_MAX all values are correct just undo
22992 	 * null termination, this saves a bcopy.
22993 	 */
22994 	if (*new_ppa_ptr == UINT_MAX) {
22995 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
22996 		*new_ppa_ptr = ill->ill_ppa;
22997 	} else {
22998 		/*
22999 		 * undo null termination done above.
23000 		 */
23001 		ppa_ptr[0] = old_char;
23002 	}
23003 
23004 	/* Let SCTP know about this ILL */
23005 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23006 
23007 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23008 	    B_TRUE);
23009 
23010 	rw_exit(&ipst->ips_ill_g_lock);
23011 	ill_refrele(ill);
23012 	if (ipsq == NULL)
23013 		return (EINPROGRESS);
23014 
23015 	/*
23016 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23017 	 */
23018 	if (ipsq->ipsq_current_ipif == NULL)
23019 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23020 	else
23021 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23022 
23023 	error = ipif_set_values_tail(ill, ipif, mp, q);
23024 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23025 	if (error != 0 && error != EINPROGRESS) {
23026 		/*
23027 		 * restore previous values
23028 		 */
23029 		ill->ill_isv6 = B_FALSE;
23030 	}
23031 	return (error);
23032 }
23033 
23034 
23035 void
23036 ipif_init(ip_stack_t *ipst)
23037 {
23038 	hrtime_t hrt;
23039 	int i;
23040 
23041 	/*
23042 	 * Can't call drv_getparm here as it is too early in the boot.
23043 	 * As we use ipif_src_random just for picking a different
23044 	 * source address everytime, this need not be really random.
23045 	 */
23046 	hrt = gethrtime();
23047 	ipst->ips_ipif_src_random =
23048 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23049 
23050 	for (i = 0; i < MAX_G_HEADS; i++) {
23051 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23052 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23053 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23054 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23055 	}
23056 
23057 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23058 	    ill_phyint_compare_index,
23059 	    sizeof (phyint_t),
23060 	    offsetof(struct phyint, phyint_avl_by_index));
23061 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23062 	    ill_phyint_compare_name,
23063 	    sizeof (phyint_t),
23064 	    offsetof(struct phyint, phyint_avl_by_name));
23065 }
23066 
23067 /*
23068  * Lookup the ipif corresponding to the onlink destination address. For
23069  * point-to-point interfaces, it matches with remote endpoint destination
23070  * address. For point-to-multipoint interfaces it only tries to match the
23071  * destination with the interface's subnet address. The longest, most specific
23072  * match is found to take care of such rare network configurations like -
23073  * le0: 129.146.1.1/16
23074  * le1: 129.146.2.2/24
23075  * It is used only by SO_DONTROUTE at the moment.
23076  */
23077 ipif_t *
23078 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23079 {
23080 	ipif_t	*ipif, *best_ipif;
23081 	ill_t	*ill;
23082 	ill_walk_context_t ctx;
23083 
23084 	ASSERT(zoneid != ALL_ZONES);
23085 	best_ipif = NULL;
23086 
23087 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23088 	ill = ILL_START_WALK_V4(&ctx, ipst);
23089 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23090 		mutex_enter(&ill->ill_lock);
23091 		for (ipif = ill->ill_ipif; ipif != NULL;
23092 		    ipif = ipif->ipif_next) {
23093 			if (!IPIF_CAN_LOOKUP(ipif))
23094 				continue;
23095 			if (ipif->ipif_zoneid != zoneid &&
23096 			    ipif->ipif_zoneid != ALL_ZONES)
23097 				continue;
23098 			/*
23099 			 * Point-to-point case. Look for exact match with
23100 			 * destination address.
23101 			 */
23102 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23103 				if (ipif->ipif_pp_dst_addr == addr) {
23104 					ipif_refhold_locked(ipif);
23105 					mutex_exit(&ill->ill_lock);
23106 					rw_exit(&ipst->ips_ill_g_lock);
23107 					if (best_ipif != NULL)
23108 						ipif_refrele(best_ipif);
23109 					return (ipif);
23110 				}
23111 			} else if (ipif->ipif_subnet == (addr &
23112 			    ipif->ipif_net_mask)) {
23113 				/*
23114 				 * Point-to-multipoint case. Looping through to
23115 				 * find the most specific match. If there are
23116 				 * multiple best match ipif's then prefer ipif's
23117 				 * that are UP. If there is only one best match
23118 				 * ipif and it is DOWN we must still return it.
23119 				 */
23120 				if ((best_ipif == NULL) ||
23121 				    (ipif->ipif_net_mask >
23122 				    best_ipif->ipif_net_mask) ||
23123 				    ((ipif->ipif_net_mask ==
23124 				    best_ipif->ipif_net_mask) &&
23125 				    ((ipif->ipif_flags & IPIF_UP) &&
23126 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23127 					ipif_refhold_locked(ipif);
23128 					mutex_exit(&ill->ill_lock);
23129 					rw_exit(&ipst->ips_ill_g_lock);
23130 					if (best_ipif != NULL)
23131 						ipif_refrele(best_ipif);
23132 					best_ipif = ipif;
23133 					rw_enter(&ipst->ips_ill_g_lock,
23134 					    RW_READER);
23135 					mutex_enter(&ill->ill_lock);
23136 				}
23137 			}
23138 		}
23139 		mutex_exit(&ill->ill_lock);
23140 	}
23141 	rw_exit(&ipst->ips_ill_g_lock);
23142 	return (best_ipif);
23143 }
23144 
23145 
23146 /*
23147  * Save enough information so that we can recreate the IRE if
23148  * the interface goes down and then up.
23149  */
23150 static void
23151 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23152 {
23153 	mblk_t	*save_mp;
23154 
23155 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23156 	if (save_mp != NULL) {
23157 		ifrt_t	*ifrt;
23158 
23159 		save_mp->b_wptr += sizeof (ifrt_t);
23160 		ifrt = (ifrt_t *)save_mp->b_rptr;
23161 		bzero(ifrt, sizeof (ifrt_t));
23162 		ifrt->ifrt_type = ire->ire_type;
23163 		ifrt->ifrt_addr = ire->ire_addr;
23164 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23165 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23166 		ifrt->ifrt_mask = ire->ire_mask;
23167 		ifrt->ifrt_flags = ire->ire_flags;
23168 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23169 		mutex_enter(&ipif->ipif_saved_ire_lock);
23170 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23171 		ipif->ipif_saved_ire_mp = save_mp;
23172 		ipif->ipif_saved_ire_cnt++;
23173 		mutex_exit(&ipif->ipif_saved_ire_lock);
23174 	}
23175 }
23176 
23177 
23178 static void
23179 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23180 {
23181 	mblk_t	**mpp;
23182 	mblk_t	*mp;
23183 	ifrt_t	*ifrt;
23184 
23185 	/* Remove from ipif_saved_ire_mp list if it is there */
23186 	mutex_enter(&ipif->ipif_saved_ire_lock);
23187 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23188 	    mpp = &(*mpp)->b_cont) {
23189 		/*
23190 		 * On a given ipif, the triple of address, gateway and
23191 		 * mask is unique for each saved IRE (in the case of
23192 		 * ordinary interface routes, the gateway address is
23193 		 * all-zeroes).
23194 		 */
23195 		mp = *mpp;
23196 		ifrt = (ifrt_t *)mp->b_rptr;
23197 		if (ifrt->ifrt_addr == ire->ire_addr &&
23198 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23199 		    ifrt->ifrt_mask == ire->ire_mask) {
23200 			*mpp = mp->b_cont;
23201 			ipif->ipif_saved_ire_cnt--;
23202 			freeb(mp);
23203 			break;
23204 		}
23205 	}
23206 	mutex_exit(&ipif->ipif_saved_ire_lock);
23207 }
23208 
23209 
23210 /*
23211  * IP multirouting broadcast routes handling
23212  * Append CGTP broadcast IREs to regular ones created
23213  * at ifconfig time.
23214  */
23215 static void
23216 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23217 {
23218 	ire_t *ire_prim;
23219 
23220 	ASSERT(ire != NULL);
23221 	ASSERT(ire_dst != NULL);
23222 
23223 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23224 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23225 	if (ire_prim != NULL) {
23226 		/*
23227 		 * We are in the special case of broadcasts for
23228 		 * CGTP. We add an IRE_BROADCAST that holds
23229 		 * the RTF_MULTIRT flag, the destination
23230 		 * address of ire_dst and the low level
23231 		 * info of ire_prim. In other words, CGTP
23232 		 * broadcast is added to the redundant ipif.
23233 		 */
23234 		ipif_t *ipif_prim;
23235 		ire_t  *bcast_ire;
23236 
23237 		ipif_prim = ire_prim->ire_ipif;
23238 
23239 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23240 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23241 		    (void *)ire_dst, (void *)ire_prim,
23242 		    (void *)ipif_prim));
23243 
23244 		bcast_ire = ire_create(
23245 		    (uchar_t *)&ire->ire_addr,
23246 		    (uchar_t *)&ip_g_all_ones,
23247 		    (uchar_t *)&ire_dst->ire_src_addr,
23248 		    (uchar_t *)&ire->ire_gateway_addr,
23249 		    &ipif_prim->ipif_mtu,
23250 		    NULL,
23251 		    ipif_prim->ipif_rq,
23252 		    ipif_prim->ipif_wq,
23253 		    IRE_BROADCAST,
23254 		    ipif_prim,
23255 		    0,
23256 		    0,
23257 		    0,
23258 		    ire->ire_flags,
23259 		    &ire_uinfo_null,
23260 		    NULL,
23261 		    NULL,
23262 		    ipst);
23263 
23264 		if (bcast_ire != NULL) {
23265 
23266 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23267 			    B_FALSE) == 0) {
23268 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23269 				    "added bcast_ire %p\n",
23270 				    (void *)bcast_ire));
23271 
23272 				ipif_save_ire(bcast_ire->ire_ipif,
23273 				    bcast_ire);
23274 				ire_refrele(bcast_ire);
23275 			}
23276 		}
23277 		ire_refrele(ire_prim);
23278 	}
23279 }
23280 
23281 
23282 /*
23283  * IP multirouting broadcast routes handling
23284  * Remove the broadcast ire
23285  */
23286 static void
23287 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23288 {
23289 	ire_t *ire_dst;
23290 
23291 	ASSERT(ire != NULL);
23292 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23293 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23294 	if (ire_dst != NULL) {
23295 		ire_t *ire_prim;
23296 
23297 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23298 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23299 		if (ire_prim != NULL) {
23300 			ipif_t *ipif_prim;
23301 			ire_t  *bcast_ire;
23302 
23303 			ipif_prim = ire_prim->ire_ipif;
23304 
23305 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23306 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23307 			    (void *)ire_dst, (void *)ire_prim,
23308 			    (void *)ipif_prim));
23309 
23310 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23311 			    ire->ire_gateway_addr,
23312 			    IRE_BROADCAST,
23313 			    ipif_prim, ALL_ZONES,
23314 			    NULL,
23315 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23316 			    MATCH_IRE_MASK, ipst);
23317 
23318 			if (bcast_ire != NULL) {
23319 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23320 				    "looked up bcast_ire %p\n",
23321 				    (void *)bcast_ire));
23322 				ipif_remove_ire(bcast_ire->ire_ipif,
23323 				    bcast_ire);
23324 				ire_delete(bcast_ire);
23325 				ire_refrele(bcast_ire);
23326 			}
23327 			ire_refrele(ire_prim);
23328 		}
23329 		ire_refrele(ire_dst);
23330 	}
23331 }
23332 
23333 /*
23334  * IPsec hardware acceleration capabilities related functions.
23335  */
23336 
23337 /*
23338  * Free a per-ill IPsec capabilities structure.
23339  */
23340 static void
23341 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23342 {
23343 	if (capab->auth_hw_algs != NULL)
23344 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23345 	if (capab->encr_hw_algs != NULL)
23346 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23347 	if (capab->encr_algparm != NULL)
23348 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23349 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23350 }
23351 
23352 /*
23353  * Allocate a new per-ill IPsec capabilities structure. This structure
23354  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23355  * an array which specifies, for each algorithm, whether this algorithm
23356  * is supported by the ill or not.
23357  */
23358 static ill_ipsec_capab_t *
23359 ill_ipsec_capab_alloc(void)
23360 {
23361 	ill_ipsec_capab_t *capab;
23362 	uint_t nelems;
23363 
23364 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23365 	if (capab == NULL)
23366 		return (NULL);
23367 
23368 	/* we need one bit per algorithm */
23369 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23370 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23371 
23372 	/* allocate memory to store algorithm flags */
23373 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23374 	if (capab->encr_hw_algs == NULL)
23375 		goto nomem;
23376 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23377 	if (capab->auth_hw_algs == NULL)
23378 		goto nomem;
23379 	/*
23380 	 * Leave encr_algparm NULL for now since we won't need it half
23381 	 * the time
23382 	 */
23383 	return (capab);
23384 
23385 nomem:
23386 	ill_ipsec_capab_free(capab);
23387 	return (NULL);
23388 }
23389 
23390 /*
23391  * Resize capability array.  Since we're exclusive, this is OK.
23392  */
23393 static boolean_t
23394 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23395 {
23396 	ipsec_capab_algparm_t *nalp, *oalp;
23397 	uint32_t olen, nlen;
23398 
23399 	oalp = capab->encr_algparm;
23400 	olen = capab->encr_algparm_size;
23401 
23402 	if (oalp != NULL) {
23403 		if (algid < capab->encr_algparm_end)
23404 			return (B_TRUE);
23405 	}
23406 
23407 	nlen = (algid + 1) * sizeof (*nalp);
23408 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23409 	if (nalp == NULL)
23410 		return (B_FALSE);
23411 
23412 	if (oalp != NULL) {
23413 		bcopy(oalp, nalp, olen);
23414 		kmem_free(oalp, olen);
23415 	}
23416 	capab->encr_algparm = nalp;
23417 	capab->encr_algparm_size = nlen;
23418 	capab->encr_algparm_end = algid + 1;
23419 
23420 	return (B_TRUE);
23421 }
23422 
23423 /*
23424  * Compare the capabilities of the specified ill with the protocol
23425  * and algorithms specified by the SA passed as argument.
23426  * If they match, returns B_TRUE, B_FALSE if they do not match.
23427  *
23428  * The ill can be passed as a pointer to it, or by specifying its index
23429  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23430  *
23431  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23432  * packet is eligible for hardware acceleration, and by
23433  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23434  * to a particular ill.
23435  */
23436 boolean_t
23437 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23438     ipsa_t *sa, netstack_t *ns)
23439 {
23440 	boolean_t sa_isv6;
23441 	uint_t algid;
23442 	struct ill_ipsec_capab_s *cpp;
23443 	boolean_t need_refrele = B_FALSE;
23444 	ip_stack_t	*ipst = ns->netstack_ip;
23445 
23446 	if (ill == NULL) {
23447 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23448 		    NULL, NULL, NULL, ipst);
23449 		if (ill == NULL) {
23450 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23451 			return (B_FALSE);
23452 		}
23453 		need_refrele = B_TRUE;
23454 	}
23455 
23456 	/*
23457 	 * Use the address length specified by the SA to determine
23458 	 * if it corresponds to a IPv6 address, and fail the matching
23459 	 * if the isv6 flag passed as argument does not match.
23460 	 * Note: this check is used for SADB capability checking before
23461 	 * sending SA information to an ill.
23462 	 */
23463 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23464 	if (sa_isv6 != ill_isv6)
23465 		/* protocol mismatch */
23466 		goto done;
23467 
23468 	/*
23469 	 * Check if the ill supports the protocol, algorithm(s) and
23470 	 * key size(s) specified by the SA, and get the pointers to
23471 	 * the algorithms supported by the ill.
23472 	 */
23473 	switch (sa->ipsa_type) {
23474 
23475 	case SADB_SATYPE_ESP:
23476 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23477 			/* ill does not support ESP acceleration */
23478 			goto done;
23479 		cpp = ill->ill_ipsec_capab_esp;
23480 		algid = sa->ipsa_auth_alg;
23481 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23482 			goto done;
23483 		algid = sa->ipsa_encr_alg;
23484 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23485 			goto done;
23486 		if (algid < cpp->encr_algparm_end) {
23487 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23488 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23489 				goto done;
23490 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23491 				goto done;
23492 		}
23493 		break;
23494 
23495 	case SADB_SATYPE_AH:
23496 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23497 			/* ill does not support AH acceleration */
23498 			goto done;
23499 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23500 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23501 			goto done;
23502 		break;
23503 	}
23504 
23505 	if (need_refrele)
23506 		ill_refrele(ill);
23507 	return (B_TRUE);
23508 done:
23509 	if (need_refrele)
23510 		ill_refrele(ill);
23511 	return (B_FALSE);
23512 }
23513 
23514 
23515 /*
23516  * Add a new ill to the list of IPsec capable ills.
23517  * Called from ill_capability_ipsec_ack() when an ACK was received
23518  * indicating that IPsec hardware processing was enabled for an ill.
23519  *
23520  * ill must point to the ill for which acceleration was enabled.
23521  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23522  */
23523 static void
23524 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23525 {
23526 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23527 	uint_t sa_type;
23528 	uint_t ipproto;
23529 	ip_stack_t	*ipst = ill->ill_ipst;
23530 
23531 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23532 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23533 
23534 	switch (dl_cap) {
23535 	case DL_CAPAB_IPSEC_AH:
23536 		sa_type = SADB_SATYPE_AH;
23537 		ills = &ipst->ips_ipsec_capab_ills_ah;
23538 		ipproto = IPPROTO_AH;
23539 		break;
23540 	case DL_CAPAB_IPSEC_ESP:
23541 		sa_type = SADB_SATYPE_ESP;
23542 		ills = &ipst->ips_ipsec_capab_ills_esp;
23543 		ipproto = IPPROTO_ESP;
23544 		break;
23545 	}
23546 
23547 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23548 
23549 	/*
23550 	 * Add ill index to list of hardware accelerators. If
23551 	 * already in list, do nothing.
23552 	 */
23553 	for (cur_ill = *ills; cur_ill != NULL &&
23554 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23555 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23556 		;
23557 
23558 	if (cur_ill == NULL) {
23559 		/* if this is a new entry for this ill */
23560 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23561 		if (new_ill == NULL) {
23562 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23563 			return;
23564 		}
23565 
23566 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23567 		new_ill->ill_isv6 = ill->ill_isv6;
23568 		new_ill->next = *ills;
23569 		*ills = new_ill;
23570 	} else if (!sadb_resync) {
23571 		/* not resync'ing SADB and an entry exists for this ill */
23572 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23573 		return;
23574 	}
23575 
23576 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23577 
23578 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23579 		/*
23580 		 * IPsec module for protocol loaded, initiate dump
23581 		 * of the SADB to this ill.
23582 		 */
23583 		sadb_ill_download(ill, sa_type);
23584 }
23585 
23586 /*
23587  * Remove an ill from the list of IPsec capable ills.
23588  */
23589 static void
23590 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23591 {
23592 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23593 	ip_stack_t	*ipst = ill->ill_ipst;
23594 
23595 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23596 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23597 
23598 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23599 	    &ipst->ips_ipsec_capab_ills_esp;
23600 
23601 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23602 
23603 	prev_ill = NULL;
23604 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23605 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23606 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23607 		;
23608 	if (cur_ill == NULL) {
23609 		/* entry not found */
23610 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23611 		return;
23612 	}
23613 	if (prev_ill == NULL) {
23614 		/* entry at front of list */
23615 		*ills = NULL;
23616 	} else {
23617 		prev_ill->next = cur_ill->next;
23618 	}
23619 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23620 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23621 }
23622 
23623 /*
23624  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23625  * supporting the specified IPsec protocol acceleration.
23626  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23627  * We free the mblk and, if sa is non-null, release the held referece.
23628  */
23629 void
23630 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23631     netstack_t *ns)
23632 {
23633 	ipsec_capab_ill_t *ici, *cur_ici;
23634 	ill_t *ill;
23635 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23636 	ip_stack_t	*ipst = ns->netstack_ip;
23637 
23638 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23639 	    ipst->ips_ipsec_capab_ills_esp;
23640 
23641 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23642 
23643 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23644 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23645 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23646 
23647 		/*
23648 		 * Handle the case where the ill goes away while the SADB is
23649 		 * attempting to send messages.  If it's going away, it's
23650 		 * nuking its shadow SADB, so we don't care..
23651 		 */
23652 
23653 		if (ill == NULL)
23654 			continue;
23655 
23656 		if (sa != NULL) {
23657 			/*
23658 			 * Make sure capabilities match before
23659 			 * sending SA to ill.
23660 			 */
23661 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23662 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23663 				ill_refrele(ill);
23664 				continue;
23665 			}
23666 
23667 			mutex_enter(&sa->ipsa_lock);
23668 			sa->ipsa_flags |= IPSA_F_HW;
23669 			mutex_exit(&sa->ipsa_lock);
23670 		}
23671 
23672 		/*
23673 		 * Copy template message, and add it to the front
23674 		 * of the mblk ship list. We want to avoid holding
23675 		 * the ipsec_capab_ills_lock while sending the
23676 		 * message to the ills.
23677 		 *
23678 		 * The b_next and b_prev are temporarily used
23679 		 * to build a list of mblks to be sent down, and to
23680 		 * save the ill to which they must be sent.
23681 		 */
23682 		nmp = copymsg(mp);
23683 		if (nmp == NULL) {
23684 			ill_refrele(ill);
23685 			continue;
23686 		}
23687 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23688 		nmp->b_next = mp_ship_list;
23689 		mp_ship_list = nmp;
23690 		nmp->b_prev = (mblk_t *)ill;
23691 	}
23692 
23693 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23694 
23695 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23696 		/* restore the mblk to a sane state */
23697 		next_mp = nmp->b_next;
23698 		nmp->b_next = NULL;
23699 		ill = (ill_t *)nmp->b_prev;
23700 		nmp->b_prev = NULL;
23701 
23702 		ill_dlpi_send(ill, nmp);
23703 		ill_refrele(ill);
23704 	}
23705 
23706 	if (sa != NULL)
23707 		IPSA_REFRELE(sa);
23708 	freemsg(mp);
23709 }
23710 
23711 /*
23712  * Derive an interface id from the link layer address.
23713  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23714  */
23715 static boolean_t
23716 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23717 {
23718 	char		*addr;
23719 
23720 	if (phys_length != ETHERADDRL)
23721 		return (B_FALSE);
23722 
23723 	/* Form EUI-64 like address */
23724 	addr = (char *)&v6addr->s6_addr32[2];
23725 	bcopy((char *)phys_addr, addr, 3);
23726 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23727 	addr[3] = (char)0xff;
23728 	addr[4] = (char)0xfe;
23729 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23730 	return (B_TRUE);
23731 }
23732 
23733 /* ARGSUSED */
23734 static boolean_t
23735 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23736 {
23737 	return (B_FALSE);
23738 }
23739 
23740 /* ARGSUSED */
23741 static boolean_t
23742 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23743     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23744 {
23745 	/*
23746 	 * Multicast address mappings used over Ethernet/802.X.
23747 	 * This address is used as a base for mappings.
23748 	 */
23749 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23750 	    0x00, 0x00, 0x00};
23751 
23752 	/*
23753 	 * Extract low order 32 bits from IPv6 multicast address.
23754 	 * Or that into the link layer address, starting from the
23755 	 * second byte.
23756 	 */
23757 	*hw_start = 2;
23758 	v6_extract_mask->s6_addr32[0] = 0;
23759 	v6_extract_mask->s6_addr32[1] = 0;
23760 	v6_extract_mask->s6_addr32[2] = 0;
23761 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23762 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23763 	return (B_TRUE);
23764 }
23765 
23766 /*
23767  * Indicate by return value whether multicast is supported. If not,
23768  * this code should not touch/change any parameters.
23769  */
23770 /* ARGSUSED */
23771 static boolean_t
23772 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23773     uint32_t *hw_start, ipaddr_t *extract_mask)
23774 {
23775 	/*
23776 	 * Multicast address mappings used over Ethernet/802.X.
23777 	 * This address is used as a base for mappings.
23778 	 */
23779 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23780 	    0x00, 0x00, 0x00 };
23781 
23782 	if (phys_length != ETHERADDRL)
23783 		return (B_FALSE);
23784 
23785 	*extract_mask = htonl(0x007fffff);
23786 	*hw_start = 2;
23787 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23788 	return (B_TRUE);
23789 }
23790 
23791 /*
23792  * Derive IPoIB interface id from the link layer address.
23793  */
23794 static boolean_t
23795 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23796 {
23797 	char		*addr;
23798 
23799 	if (phys_length != 20)
23800 		return (B_FALSE);
23801 	addr = (char *)&v6addr->s6_addr32[2];
23802 	bcopy(phys_addr + 12, addr, 8);
23803 	/*
23804 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23805 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23806 	 * rules. In these cases, the IBA considers these GUIDs to be in
23807 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23808 	 * required; vendors are required not to assign global EUI-64's
23809 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23810 	 * of the interface identifier. Whether the GUID is in modified
23811 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23812 	 * bit set to 1.
23813 	 */
23814 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23815 	return (B_TRUE);
23816 }
23817 
23818 /*
23819  * Note on mapping from multicast IP addresses to IPoIB multicast link
23820  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23821  * The format of an IPoIB multicast address is:
23822  *
23823  *  4 byte QPN      Scope Sign.  Pkey
23824  * +--------------------------------------------+
23825  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23826  * +--------------------------------------------+
23827  *
23828  * The Scope and Pkey components are properties of the IBA port and
23829  * network interface. They can be ascertained from the broadcast address.
23830  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23831  */
23832 
23833 static boolean_t
23834 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23835     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23836 {
23837 	/*
23838 	 * Base IPoIB IPv6 multicast address used for mappings.
23839 	 * Does not contain the IBA scope/Pkey values.
23840 	 */
23841 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23842 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23843 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23844 
23845 	/*
23846 	 * Extract low order 80 bits from IPv6 multicast address.
23847 	 * Or that into the link layer address, starting from the
23848 	 * sixth byte.
23849 	 */
23850 	*hw_start = 6;
23851 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23852 
23853 	/*
23854 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23855 	 */
23856 	*(maddr + 5) = *(bphys_addr + 5);
23857 	*(maddr + 8) = *(bphys_addr + 8);
23858 	*(maddr + 9) = *(bphys_addr + 9);
23859 
23860 	v6_extract_mask->s6_addr32[0] = 0;
23861 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23862 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23863 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23864 	return (B_TRUE);
23865 }
23866 
23867 static boolean_t
23868 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23869     uint32_t *hw_start, ipaddr_t *extract_mask)
23870 {
23871 	/*
23872 	 * Base IPoIB IPv4 multicast address used for mappings.
23873 	 * Does not contain the IBA scope/Pkey values.
23874 	 */
23875 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23876 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23877 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23878 
23879 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23880 		return (B_FALSE);
23881 
23882 	/*
23883 	 * Extract low order 28 bits from IPv4 multicast address.
23884 	 * Or that into the link layer address, starting from the
23885 	 * sixteenth byte.
23886 	 */
23887 	*extract_mask = htonl(0x0fffffff);
23888 	*hw_start = 16;
23889 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23890 
23891 	/*
23892 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23893 	 */
23894 	*(maddr + 5) = *(bphys_addr + 5);
23895 	*(maddr + 8) = *(bphys_addr + 8);
23896 	*(maddr + 9) = *(bphys_addr + 9);
23897 	return (B_TRUE);
23898 }
23899 
23900 /*
23901  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23902  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23903  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23904  * the link-local address is preferred.
23905  */
23906 boolean_t
23907 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23908 {
23909 	ipif_t	*ipif;
23910 	ipif_t	*maybe_ipif = NULL;
23911 
23912 	mutex_enter(&ill->ill_lock);
23913 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23914 		mutex_exit(&ill->ill_lock);
23915 		if (ipifp != NULL)
23916 			*ipifp = NULL;
23917 		return (B_FALSE);
23918 	}
23919 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23920 		if (!IPIF_CAN_LOOKUP(ipif))
23921 			continue;
23922 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23923 		    ipif->ipif_zoneid != ALL_ZONES)
23924 			continue;
23925 		if ((ipif->ipif_flags & flags) != flags)
23926 			continue;
23927 
23928 		if (ipifp == NULL) {
23929 			mutex_exit(&ill->ill_lock);
23930 			ASSERT(maybe_ipif == NULL);
23931 			return (B_TRUE);
23932 		}
23933 		if (!ill->ill_isv6 ||
23934 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23935 			ipif_refhold_locked(ipif);
23936 			mutex_exit(&ill->ill_lock);
23937 			*ipifp = ipif;
23938 			return (B_TRUE);
23939 		}
23940 		if (maybe_ipif == NULL)
23941 			maybe_ipif = ipif;
23942 	}
23943 	if (ipifp != NULL) {
23944 		if (maybe_ipif != NULL)
23945 			ipif_refhold_locked(maybe_ipif);
23946 		*ipifp = maybe_ipif;
23947 	}
23948 	mutex_exit(&ill->ill_lock);
23949 	return (maybe_ipif != NULL);
23950 }
23951 
23952 /*
23953  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23954  */
23955 boolean_t
23956 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23957 {
23958 	ill_t *illg;
23959 	ip_stack_t	*ipst = ill->ill_ipst;
23960 
23961 	/*
23962 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23963 	 */
23964 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23965 		return (B_TRUE);
23966 	}
23967 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23968 	if (ill->ill_group == NULL) {
23969 		/* ill not in a group */
23970 		rw_exit(&ipst->ips_ill_g_lock);
23971 		return (B_FALSE);
23972 	}
23973 
23974 	/*
23975 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
23976 	 * group. We need to look for an ipif in the zone on all the ills in the
23977 	 * group.
23978 	 */
23979 	illg = ill->ill_group->illgrp_ill;
23980 	do {
23981 		/*
23982 		 * We don't call ipif_lookup_zoneid() on ill as we already know
23983 		 * that it's not there.
23984 		 */
23985 		if (illg != ill &&
23986 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
23987 			break;
23988 		}
23989 	} while ((illg = illg->ill_group_next) != NULL);
23990 	rw_exit(&ipst->ips_ill_g_lock);
23991 	return (illg != NULL);
23992 }
23993 
23994 /*
23995  * Check if this ill is only being used to send ICMP probes for IPMP
23996  */
23997 boolean_t
23998 ill_is_probeonly(ill_t *ill)
23999 {
24000 	/*
24001 	 * Check if the interface is FAILED, or INACTIVE
24002 	 */
24003 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24004 		return (B_TRUE);
24005 
24006 	return (B_FALSE);
24007 }
24008 
24009 /*
24010  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24011  * If a pointer to an ipif_t is returned then the caller will need to do
24012  * an ill_refrele().
24013  *
24014  * If there is no real interface which matches the ifindex, then it looks
24015  * for a group that has a matching index. In the case of a group match the
24016  * lifidx must be zero. We don't need emulate the logical interfaces
24017  * since IP Filter's use of netinfo doesn't use that.
24018  */
24019 ipif_t *
24020 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24021     ip_stack_t *ipst)
24022 {
24023 	ipif_t *ipif;
24024 	ill_t *ill;
24025 
24026 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24027 	    ipst);
24028 
24029 	if (ill == NULL) {
24030 		/* Fallback to group names only if hook_emulation set */
24031 		if (!ipst->ips_ipmp_hook_emulation)
24032 			return (NULL);
24033 
24034 		if (lifidx != 0)
24035 			return (NULL);
24036 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24037 		if (ill == NULL)
24038 			return (NULL);
24039 	}
24040 
24041 	mutex_enter(&ill->ill_lock);
24042 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24043 		mutex_exit(&ill->ill_lock);
24044 		ill_refrele(ill);
24045 		return (NULL);
24046 	}
24047 
24048 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24049 		if (!IPIF_CAN_LOOKUP(ipif))
24050 			continue;
24051 		if (lifidx == ipif->ipif_id) {
24052 			ipif_refhold_locked(ipif);
24053 			break;
24054 		}
24055 	}
24056 
24057 	mutex_exit(&ill->ill_lock);
24058 	ill_refrele(ill);
24059 	return (ipif);
24060 }
24061 
24062 /*
24063  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24064  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24065  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24066  * for details.
24067  */
24068 void
24069 ill_fastpath_flush(ill_t *ill)
24070 {
24071 	ip_stack_t *ipst = ill->ill_ipst;
24072 
24073 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24074 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24075 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24076 }
24077 
24078 /*
24079  * Set the physical address information for `ill' to the contents of the
24080  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24081  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24082  * EINPROGRESS will be returned.
24083  */
24084 int
24085 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24086 {
24087 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24088 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24089 
24090 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24091 
24092 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24093 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24094 		/* Changing DL_IPV6_TOKEN is not yet supported */
24095 		return (0);
24096 	}
24097 
24098 	/*
24099 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24100 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24101 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24102 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24103 	 */
24104 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24105 		freemsg(mp);
24106 		return (ENOMEM);
24107 	}
24108 
24109 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24110 
24111 	/*
24112 	 * If we can quiesce the ill, then set the address.  If not, then
24113 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24114 	 */
24115 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24116 	mutex_enter(&ill->ill_lock);
24117 	if (!ill_is_quiescent(ill)) {
24118 		/* call cannot fail since `conn_t *' argument is NULL */
24119 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24120 		    mp, ILL_DOWN);
24121 		mutex_exit(&ill->ill_lock);
24122 		return (EINPROGRESS);
24123 	}
24124 	mutex_exit(&ill->ill_lock);
24125 
24126 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24127 	return (0);
24128 }
24129 
24130 /*
24131  * Once the ill associated with `q' has quiesced, set its physical address
24132  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24133  * are passed (linked by b_cont), since we sometimes need to save two distinct
24134  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24135  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24136  * is quiesced, we know any stale IREs with the old address information have
24137  * already been removed, so we don't need to call ill_fastpath_flush().
24138  */
24139 /* ARGSUSED */
24140 static void
24141 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24142 {
24143 	ill_t		*ill = q->q_ptr;
24144 	mblk_t		*addrmp2 = unlinkb(addrmp);
24145 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24146 	uint_t		addrlen, addroff;
24147 
24148 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24149 
24150 	addroff	= dlindp->dl_addr_offset;
24151 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24152 
24153 	switch (dlindp->dl_data) {
24154 	case DL_IPV6_LINK_LAYER_ADDR:
24155 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24156 		freemsg(addrmp2);
24157 		break;
24158 
24159 	case DL_CURR_PHYS_ADDR:
24160 		freemsg(ill->ill_phys_addr_mp);
24161 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24162 		ill->ill_phys_addr_mp = addrmp;
24163 		ill->ill_phys_addr_length = addrlen;
24164 
24165 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24166 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24167 		else
24168 			freemsg(addrmp2);
24169 		break;
24170 	default:
24171 		ASSERT(0);
24172 	}
24173 
24174 	/*
24175 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24176 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24177 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24178 	 * brought up.
24179 	 */
24180 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24181 		ipsq_current_finish(ipsq);
24182 }
24183 
24184 /*
24185  * Helper routine for setting the ill_nd_lla fields.
24186  */
24187 void
24188 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24189 {
24190 	freemsg(ill->ill_nd_lla_mp);
24191 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24192 	ill->ill_nd_lla_mp = ndmp;
24193 	ill->ill_nd_lla_len = addrlen;
24194 }
24195 
24196 major_t IP_MAJ;
24197 #define	IP	"ip"
24198 
24199 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24200 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24201 
24202 /*
24203  * Issue REMOVEIF ioctls to have the loopback interfaces
24204  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24205  * the former going away when the user-level processes in the zone
24206  * are killed  * and the latter are cleaned up by the stream head
24207  * str_stack_shutdown callback that undoes all I_PLINKs.
24208  */
24209 void
24210 ip_loopback_cleanup(ip_stack_t *ipst)
24211 {
24212 	int error;
24213 	ldi_handle_t	lh = NULL;
24214 	ldi_ident_t	li = NULL;
24215 	int		rval;
24216 	cred_t		*cr;
24217 	struct strioctl iocb;
24218 	struct lifreq	lifreq;
24219 
24220 	IP_MAJ = ddi_name_to_major(IP);
24221 
24222 #ifdef NS_DEBUG
24223 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24224 	    ipst->ips_netstack->netstack_stackid);
24225 #endif
24226 
24227 	bzero(&lifreq, sizeof (lifreq));
24228 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24229 
24230 	error = ldi_ident_from_major(IP_MAJ, &li);
24231 	if (error) {
24232 #ifdef DEBUG
24233 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24234 		    error);
24235 #endif
24236 		return;
24237 	}
24238 
24239 	cr = zone_get_kcred(netstackid_to_zoneid(
24240 	    ipst->ips_netstack->netstack_stackid));
24241 	ASSERT(cr != NULL);
24242 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24243 	if (error) {
24244 #ifdef DEBUG
24245 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24246 		    error);
24247 #endif
24248 		goto out;
24249 	}
24250 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24251 	iocb.ic_timout = 15;
24252 	iocb.ic_len = sizeof (lifreq);
24253 	iocb.ic_dp = (char *)&lifreq;
24254 
24255 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24256 	/* LINTED - statement has no consequent */
24257 	if (error) {
24258 #ifdef NS_DEBUG
24259 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24260 		    "UDP6 error %d\n", error);
24261 #endif
24262 	}
24263 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24264 	lh = NULL;
24265 
24266 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24267 	if (error) {
24268 #ifdef NS_DEBUG
24269 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24270 		    error);
24271 #endif
24272 		goto out;
24273 	}
24274 
24275 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24276 	iocb.ic_timout = 15;
24277 	iocb.ic_len = sizeof (lifreq);
24278 	iocb.ic_dp = (char *)&lifreq;
24279 
24280 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24281 	/* LINTED - statement has no consequent */
24282 	if (error) {
24283 #ifdef NS_DEBUG
24284 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24285 		    "UDP error %d\n", error);
24286 #endif
24287 	}
24288 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24289 	lh = NULL;
24290 
24291 out:
24292 	/* Close layered handles */
24293 	if (lh)
24294 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24295 	if (li)
24296 		ldi_ident_release(li);
24297 
24298 	crfree(cr);
24299 }
24300 
24301 /*
24302  * This needs to be in-sync with nic_event_t definition
24303  */
24304 static const char *
24305 ill_hook_event2str(nic_event_t event)
24306 {
24307 	switch (event) {
24308 	case NE_PLUMB:
24309 		return ("PLUMB");
24310 	case NE_UNPLUMB:
24311 		return ("UNPLUMB");
24312 	case NE_UP:
24313 		return ("UP");
24314 	case NE_DOWN:
24315 		return ("DOWN");
24316 	case NE_ADDRESS_CHANGE:
24317 		return ("ADDRESS_CHANGE");
24318 	default:
24319 		return ("UNKNOWN");
24320 	}
24321 }
24322 
24323 static void
24324 ill_hook_event_destroy(ill_t *ill)
24325 {
24326 	hook_nic_event_t	*info;
24327 
24328 	if ((info = ill->ill_nic_event_info) != NULL) {
24329 		if (info->hne_data != NULL)
24330 			kmem_free(info->hne_data, info->hne_datalen);
24331 		kmem_free(info, sizeof (hook_nic_event_t));
24332 
24333 		ill->ill_nic_event_info = NULL;
24334 	}
24335 
24336 }
24337 
24338 boolean_t
24339 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24340     nic_event_data_t data, size_t datalen)
24341 {
24342 	ip_stack_t		*ipst = ill->ill_ipst;
24343 	hook_nic_event_t	*info;
24344 	const char		*str = NULL;
24345 
24346 	/* destroy nic event info if it exists */
24347 	if ((info = ill->ill_nic_event_info) != NULL) {
24348 		str = ill_hook_event2str(info->hne_event);
24349 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24350 		    "attached for %s\n", str, ill->ill_name));
24351 		ill_hook_event_destroy(ill);
24352 	}
24353 
24354 	/* create a new nic event info */
24355 	if ((info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP)) == NULL)
24356 		goto fail;
24357 
24358 	ill->ill_nic_event_info = info;
24359 
24360 	if (event == NE_UNPLUMB)
24361 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
24362 	else
24363 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24364 	info->hne_lif = lif;
24365 	info->hne_event = event;
24366 	info->hne_family = ill->ill_isv6 ?
24367 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24368 	info->hne_data = NULL;
24369 	info->hne_datalen = 0;
24370 
24371 	if (data != NULL && datalen != 0) {
24372 		info->hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24373 		if (info->hne_data != NULL) {
24374 			bcopy(data, info->hne_data, datalen);
24375 			info->hne_datalen = datalen;
24376 		} else {
24377 			ill_hook_event_destroy(ill);
24378 			goto fail;
24379 		}
24380 	}
24381 
24382 	return (B_TRUE);
24383 fail:
24384 	str = ill_hook_event2str(event);
24385 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24386 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24387 	return (B_FALSE);
24388 }
24389