xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision eae2e508a8e70b1ec407b10bd068c080651bbe5c)
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_cnt_ire 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 		/* Find based on address */
10883 		if (ipif->ipif_isv6) {
10884 			sin6_t *sin6;
10885 
10886 			if (sin->sin_family != AF_INET6)
10887 				return (EAFNOSUPPORT);
10888 
10889 			sin6 = (sin6_t *)sin;
10890 			/* We are a writer, so we should be able to lookup */
10891 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10892 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10893 			if (ipif == NULL) {
10894 				/*
10895 				 * Maybe the address in on another interface in
10896 				 * the same IPMP group? We check this below.
10897 				 */
10898 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10899 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10900 				    ipst);
10901 			}
10902 		} else {
10903 			ipaddr_t addr;
10904 
10905 			if (sin->sin_family != AF_INET)
10906 				return (EAFNOSUPPORT);
10907 
10908 			addr = sin->sin_addr.s_addr;
10909 			/* We are a writer, so we should be able to lookup */
10910 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10911 			    NULL, NULL, NULL, ipst);
10912 			if (ipif == NULL) {
10913 				/*
10914 				 * Maybe the address in on another interface in
10915 				 * the same IPMP group? We check this below.
10916 				 */
10917 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10918 				    NULL, NULL, NULL, NULL, ipst);
10919 			}
10920 		}
10921 		if (ipif == NULL) {
10922 			return (EADDRNOTAVAIL);
10923 		}
10924 		/*
10925 		 * When the address to be removed is hosted on a different
10926 		 * interface, we check if the interface is in the same IPMP
10927 		 * group as the specified one; if so we proceed with the
10928 		 * removal.
10929 		 * ill->ill_group is NULL when the ill is down, so we have to
10930 		 * compare the group names instead.
10931 		 */
10932 		if (ipif->ipif_ill != ill &&
10933 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10934 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10935 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10936 		    ill->ill_phyint->phyint_groupname) != 0)) {
10937 			ipif_refrele(ipif);
10938 			return (EADDRNOTAVAIL);
10939 		}
10940 
10941 		/* This is a writer */
10942 		ipif_refrele(ipif);
10943 	}
10944 
10945 	/*
10946 	 * Can not delete instance zero since it is tied to the ill.
10947 	 */
10948 	if (ipif->ipif_id == 0)
10949 		return (EBUSY);
10950 
10951 	mutex_enter(&ill->ill_lock);
10952 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10953 	mutex_exit(&ill->ill_lock);
10954 
10955 	ipif_free(ipif);
10956 
10957 	mutex_enter(&connp->conn_lock);
10958 	mutex_enter(&ill->ill_lock);
10959 
10960 
10961 	/* Are any references to this ipif active */
10962 	if (ipif_is_freeable(ipif)) {
10963 		mutex_exit(&ill->ill_lock);
10964 		mutex_exit(&connp->conn_lock);
10965 		ipif_non_duplicate(ipif);
10966 		ipif_down_tail(ipif);
10967 		ipif_free_tail(ipif); /* frees ipif */
10968 		return (0);
10969 	}
10970 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10971 	    IPIF_FREE);
10972 	mutex_exit(&ill->ill_lock);
10973 	mutex_exit(&connp->conn_lock);
10974 	if (success)
10975 		return (EINPROGRESS);
10976 	else
10977 		return (EINTR);
10978 }
10979 
10980 /*
10981  * Restart the removeif ioctl. The refcnt has gone down to 0.
10982  * The ipif is already condemned. So can't find it thru lookups.
10983  */
10984 /* ARGSUSED */
10985 int
10986 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10987     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10988 {
10989 	ill_t *ill = ipif->ipif_ill;
10990 
10991 	ASSERT(IAM_WRITER_IPIF(ipif));
10992 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10993 
10994 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10995 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10996 
10997 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10998 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10999 		ill_delete_tail(ill);
11000 		mutex_enter(&ill->ill_lock);
11001 		ill_nic_info_dispatch(ill);
11002 		mutex_exit(&ill->ill_lock);
11003 		mi_free(ill);
11004 		return (0);
11005 	}
11006 
11007 	ipif_non_duplicate(ipif);
11008 	ipif_down_tail(ipif);
11009 	ipif_free_tail(ipif);
11010 
11011 	ILL_UNMARK_CHANGING(ill);
11012 	return (0);
11013 }
11014 
11015 /*
11016  * Set the local interface address.
11017  * Allow an address of all zero when the interface is down.
11018  */
11019 /* ARGSUSED */
11020 int
11021 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11022     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11023 {
11024 	int err = 0;
11025 	in6_addr_t v6addr;
11026 	boolean_t need_up = B_FALSE;
11027 
11028 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11029 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11030 
11031 	ASSERT(IAM_WRITER_IPIF(ipif));
11032 
11033 	if (ipif->ipif_isv6) {
11034 		sin6_t *sin6;
11035 		ill_t *ill;
11036 		phyint_t *phyi;
11037 
11038 		if (sin->sin_family != AF_INET6)
11039 			return (EAFNOSUPPORT);
11040 
11041 		sin6 = (sin6_t *)sin;
11042 		v6addr = sin6->sin6_addr;
11043 		ill = ipif->ipif_ill;
11044 		phyi = ill->ill_phyint;
11045 
11046 		/*
11047 		 * Enforce that true multicast interfaces have a link-local
11048 		 * address for logical unit 0.
11049 		 */
11050 		if (ipif->ipif_id == 0 &&
11051 		    (ill->ill_flags & ILLF_MULTICAST) &&
11052 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11053 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11054 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11055 			return (EADDRNOTAVAIL);
11056 		}
11057 
11058 		/*
11059 		 * up interfaces shouldn't have the unspecified address
11060 		 * unless they also have the IPIF_NOLOCAL flags set and
11061 		 * have a subnet assigned.
11062 		 */
11063 		if ((ipif->ipif_flags & IPIF_UP) &&
11064 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11065 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11066 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11067 			return (EADDRNOTAVAIL);
11068 		}
11069 
11070 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11071 			return (EADDRNOTAVAIL);
11072 	} else {
11073 		ipaddr_t addr;
11074 
11075 		if (sin->sin_family != AF_INET)
11076 			return (EAFNOSUPPORT);
11077 
11078 		addr = sin->sin_addr.s_addr;
11079 
11080 		/* Allow 0 as the local address. */
11081 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11082 			return (EADDRNOTAVAIL);
11083 
11084 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11085 	}
11086 
11087 
11088 	/*
11089 	 * Even if there is no change we redo things just to rerun
11090 	 * ipif_set_default.
11091 	 */
11092 	if (ipif->ipif_flags & IPIF_UP) {
11093 		/*
11094 		 * Setting a new local address, make sure
11095 		 * we have net and subnet bcast ire's for
11096 		 * the old address if we need them.
11097 		 */
11098 		if (!ipif->ipif_isv6)
11099 			ipif_check_bcast_ires(ipif);
11100 		/*
11101 		 * If the interface is already marked up,
11102 		 * we call ipif_down which will take care
11103 		 * of ditching any IREs that have been set
11104 		 * up based on the old interface address.
11105 		 */
11106 		err = ipif_logical_down(ipif, q, mp);
11107 		if (err == EINPROGRESS)
11108 			return (err);
11109 		ipif_down_tail(ipif);
11110 		need_up = 1;
11111 	}
11112 
11113 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11114 	return (err);
11115 }
11116 
11117 int
11118 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11119     boolean_t need_up)
11120 {
11121 	in6_addr_t v6addr;
11122 	in6_addr_t ov6addr;
11123 	ipaddr_t addr;
11124 	sin6_t	*sin6;
11125 	int	sinlen;
11126 	int	err = 0;
11127 	ill_t	*ill = ipif->ipif_ill;
11128 	boolean_t need_dl_down;
11129 	boolean_t need_arp_down;
11130 	struct iocblk *iocp;
11131 
11132 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11133 
11134 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11135 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11136 	ASSERT(IAM_WRITER_IPIF(ipif));
11137 
11138 	/* Must cancel any pending timer before taking the ill_lock */
11139 	if (ipif->ipif_recovery_id != 0)
11140 		(void) untimeout(ipif->ipif_recovery_id);
11141 	ipif->ipif_recovery_id = 0;
11142 
11143 	if (ipif->ipif_isv6) {
11144 		sin6 = (sin6_t *)sin;
11145 		v6addr = sin6->sin6_addr;
11146 		sinlen = sizeof (struct sockaddr_in6);
11147 	} else {
11148 		addr = sin->sin_addr.s_addr;
11149 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11150 		sinlen = sizeof (struct sockaddr_in);
11151 	}
11152 	mutex_enter(&ill->ill_lock);
11153 	ov6addr = ipif->ipif_v6lcl_addr;
11154 	ipif->ipif_v6lcl_addr = v6addr;
11155 	sctp_update_ipif_addr(ipif, ov6addr);
11156 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11157 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11158 	} else {
11159 		ipif->ipif_v6src_addr = v6addr;
11160 	}
11161 	ipif->ipif_addr_ready = 0;
11162 
11163 	/*
11164 	 * If the interface was previously marked as a duplicate, then since
11165 	 * we've now got a "new" address, it should no longer be considered a
11166 	 * duplicate -- even if the "new" address is the same as the old one.
11167 	 * Note that if all ipifs are down, we may have a pending ARP down
11168 	 * event to handle.  This is because we want to recover from duplicates
11169 	 * and thus delay tearing down ARP until the duplicates have been
11170 	 * removed or disabled.
11171 	 */
11172 	need_dl_down = need_arp_down = B_FALSE;
11173 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11174 		need_arp_down = !need_up;
11175 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11176 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11177 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11178 			need_dl_down = B_TRUE;
11179 		}
11180 	}
11181 
11182 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11183 	    !ill->ill_is_6to4tun) {
11184 		queue_t *wqp = ill->ill_wq;
11185 
11186 		/*
11187 		 * The local address of this interface is a 6to4 address,
11188 		 * check if this interface is in fact a 6to4 tunnel or just
11189 		 * an interface configured with a 6to4 address.  We are only
11190 		 * interested in the former.
11191 		 */
11192 		if (wqp != NULL) {
11193 			while ((wqp->q_next != NULL) &&
11194 			    (wqp->q_next->q_qinfo != NULL) &&
11195 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11196 
11197 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11198 				    == TUN6TO4_MODID) {
11199 					/* set for use in IP */
11200 					ill->ill_is_6to4tun = 1;
11201 					break;
11202 				}
11203 				wqp = wqp->q_next;
11204 			}
11205 		}
11206 	}
11207 
11208 	ipif_set_default(ipif);
11209 
11210 	/*
11211 	 * When publishing an interface address change event, we only notify
11212 	 * the event listeners of the new address.  It is assumed that if they
11213 	 * actively care about the addresses assigned that they will have
11214 	 * already discovered the previous address assigned (if there was one.)
11215 	 *
11216 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11217 	 */
11218 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11219 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11220 		    NE_ADDRESS_CHANGE, sin, sinlen);
11221 	}
11222 
11223 	mutex_exit(&ill->ill_lock);
11224 
11225 	if (need_up) {
11226 		/*
11227 		 * Now bring the interface back up.  If this
11228 		 * is the only IPIF for the ILL, ipif_up
11229 		 * will have to re-bind to the device, so
11230 		 * we may get back EINPROGRESS, in which
11231 		 * case, this IOCTL will get completed in
11232 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11233 		 */
11234 		err = ipif_up(ipif, q, mp);
11235 	}
11236 
11237 	if (need_dl_down)
11238 		ill_dl_down(ill);
11239 	if (need_arp_down)
11240 		ipif_arp_down(ipif);
11241 
11242 	return (err);
11243 }
11244 
11245 
11246 /*
11247  * Restart entry point to restart the address set operation after the
11248  * refcounts have dropped to zero.
11249  */
11250 /* ARGSUSED */
11251 int
11252 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11253     ip_ioctl_cmd_t *ipip, void *ifreq)
11254 {
11255 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11256 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11257 	ASSERT(IAM_WRITER_IPIF(ipif));
11258 	ipif_down_tail(ipif);
11259 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11260 }
11261 
11262 /* ARGSUSED */
11263 int
11264 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11265     ip_ioctl_cmd_t *ipip, void *if_req)
11266 {
11267 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11268 	struct lifreq *lifr = (struct lifreq *)if_req;
11269 
11270 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11271 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11272 	/*
11273 	 * The net mask and address can't change since we have a
11274 	 * reference to the ipif. So no lock is necessary.
11275 	 */
11276 	if (ipif->ipif_isv6) {
11277 		*sin6 = sin6_null;
11278 		sin6->sin6_family = AF_INET6;
11279 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11280 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11281 		lifr->lifr_addrlen =
11282 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11283 	} else {
11284 		*sin = sin_null;
11285 		sin->sin_family = AF_INET;
11286 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11287 		if (ipip->ipi_cmd_type == LIF_CMD) {
11288 			lifr->lifr_addrlen =
11289 			    ip_mask_to_plen(ipif->ipif_net_mask);
11290 		}
11291 	}
11292 	return (0);
11293 }
11294 
11295 /*
11296  * Set the destination address for a pt-pt interface.
11297  */
11298 /* ARGSUSED */
11299 int
11300 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11301     ip_ioctl_cmd_t *ipip, void *if_req)
11302 {
11303 	int err = 0;
11304 	in6_addr_t v6addr;
11305 	boolean_t need_up = B_FALSE;
11306 
11307 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11308 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11309 	ASSERT(IAM_WRITER_IPIF(ipif));
11310 
11311 	if (ipif->ipif_isv6) {
11312 		sin6_t *sin6;
11313 
11314 		if (sin->sin_family != AF_INET6)
11315 			return (EAFNOSUPPORT);
11316 
11317 		sin6 = (sin6_t *)sin;
11318 		v6addr = sin6->sin6_addr;
11319 
11320 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11321 			return (EADDRNOTAVAIL);
11322 	} else {
11323 		ipaddr_t addr;
11324 
11325 		if (sin->sin_family != AF_INET)
11326 			return (EAFNOSUPPORT);
11327 
11328 		addr = sin->sin_addr.s_addr;
11329 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11330 			return (EADDRNOTAVAIL);
11331 
11332 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11333 	}
11334 
11335 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11336 		return (0);	/* No change */
11337 
11338 	if (ipif->ipif_flags & IPIF_UP) {
11339 		/*
11340 		 * If the interface is already marked up,
11341 		 * we call ipif_down which will take care
11342 		 * of ditching any IREs that have been set
11343 		 * up based on the old pp dst address.
11344 		 */
11345 		err = ipif_logical_down(ipif, q, mp);
11346 		if (err == EINPROGRESS)
11347 			return (err);
11348 		ipif_down_tail(ipif);
11349 		need_up = B_TRUE;
11350 	}
11351 	/*
11352 	 * could return EINPROGRESS. If so ioctl will complete in
11353 	 * ip_rput_dlpi_writer
11354 	 */
11355 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11356 	return (err);
11357 }
11358 
11359 static int
11360 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11361     boolean_t need_up)
11362 {
11363 	in6_addr_t v6addr;
11364 	ill_t	*ill = ipif->ipif_ill;
11365 	int	err = 0;
11366 	boolean_t need_dl_down;
11367 	boolean_t need_arp_down;
11368 
11369 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11370 	    ipif->ipif_id, (void *)ipif));
11371 
11372 	/* Must cancel any pending timer before taking the ill_lock */
11373 	if (ipif->ipif_recovery_id != 0)
11374 		(void) untimeout(ipif->ipif_recovery_id);
11375 	ipif->ipif_recovery_id = 0;
11376 
11377 	if (ipif->ipif_isv6) {
11378 		sin6_t *sin6;
11379 
11380 		sin6 = (sin6_t *)sin;
11381 		v6addr = sin6->sin6_addr;
11382 	} else {
11383 		ipaddr_t addr;
11384 
11385 		addr = sin->sin_addr.s_addr;
11386 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11387 	}
11388 	mutex_enter(&ill->ill_lock);
11389 	/* Set point to point destination address. */
11390 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11391 		/*
11392 		 * Allow this as a means of creating logical
11393 		 * pt-pt interfaces on top of e.g. an Ethernet.
11394 		 * XXX Undocumented HACK for testing.
11395 		 * pt-pt interfaces are created with NUD disabled.
11396 		 */
11397 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11398 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11399 		if (ipif->ipif_isv6)
11400 			ill->ill_flags |= ILLF_NONUD;
11401 	}
11402 
11403 	/*
11404 	 * If the interface was previously marked as a duplicate, then since
11405 	 * we've now got a "new" address, it should no longer be considered a
11406 	 * duplicate -- even if the "new" address is the same as the old one.
11407 	 * Note that if all ipifs are down, we may have a pending ARP down
11408 	 * event to handle.
11409 	 */
11410 	need_dl_down = need_arp_down = B_FALSE;
11411 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11412 		need_arp_down = !need_up;
11413 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11414 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11415 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11416 			need_dl_down = B_TRUE;
11417 		}
11418 	}
11419 
11420 	/* Set the new address. */
11421 	ipif->ipif_v6pp_dst_addr = v6addr;
11422 	/* Make sure subnet tracks pp_dst */
11423 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11424 	mutex_exit(&ill->ill_lock);
11425 
11426 	if (need_up) {
11427 		/*
11428 		 * Now bring the interface back up.  If this
11429 		 * is the only IPIF for the ILL, ipif_up
11430 		 * will have to re-bind to the device, so
11431 		 * we may get back EINPROGRESS, in which
11432 		 * case, this IOCTL will get completed in
11433 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11434 		 */
11435 		err = ipif_up(ipif, q, mp);
11436 	}
11437 
11438 	if (need_dl_down)
11439 		ill_dl_down(ill);
11440 
11441 	if (need_arp_down)
11442 		ipif_arp_down(ipif);
11443 	return (err);
11444 }
11445 
11446 /*
11447  * Restart entry point to restart the dstaddress set operation after the
11448  * refcounts have dropped to zero.
11449  */
11450 /* ARGSUSED */
11451 int
11452 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11453     ip_ioctl_cmd_t *ipip, void *ifreq)
11454 {
11455 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11456 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11457 	ipif_down_tail(ipif);
11458 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11459 }
11460 
11461 /* ARGSUSED */
11462 int
11463 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11464     ip_ioctl_cmd_t *ipip, void *if_req)
11465 {
11466 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11467 
11468 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11469 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11470 	/*
11471 	 * Get point to point destination address. The addresses can't
11472 	 * change since we hold a reference to the ipif.
11473 	 */
11474 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11475 		return (EADDRNOTAVAIL);
11476 
11477 	if (ipif->ipif_isv6) {
11478 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11479 		*sin6 = sin6_null;
11480 		sin6->sin6_family = AF_INET6;
11481 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11482 	} else {
11483 		*sin = sin_null;
11484 		sin->sin_family = AF_INET;
11485 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11486 	}
11487 	return (0);
11488 }
11489 
11490 /*
11491  * part of ipmp, make this func return the active/inactive state and
11492  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11493  */
11494 /*
11495  * This function either sets or clears the IFF_INACTIVE flag.
11496  *
11497  * As long as there are some addresses or multicast memberships on the
11498  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11499  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11500  * will be used for outbound packets.
11501  *
11502  * Caller needs to verify the validity of setting IFF_INACTIVE.
11503  */
11504 static void
11505 phyint_inactive(phyint_t *phyi)
11506 {
11507 	ill_t *ill_v4;
11508 	ill_t *ill_v6;
11509 	ipif_t *ipif;
11510 	ilm_t *ilm;
11511 
11512 	ill_v4 = phyi->phyint_illv4;
11513 	ill_v6 = phyi->phyint_illv6;
11514 
11515 	/*
11516 	 * No need for a lock while traversing the list since iam
11517 	 * a writer
11518 	 */
11519 	if (ill_v4 != NULL) {
11520 		ASSERT(IAM_WRITER_ILL(ill_v4));
11521 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11522 		    ipif = ipif->ipif_next) {
11523 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11524 				mutex_enter(&phyi->phyint_lock);
11525 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11526 				mutex_exit(&phyi->phyint_lock);
11527 				return;
11528 			}
11529 		}
11530 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11531 		    ilm = ilm->ilm_next) {
11532 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11533 				mutex_enter(&phyi->phyint_lock);
11534 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11535 				mutex_exit(&phyi->phyint_lock);
11536 				return;
11537 			}
11538 		}
11539 	}
11540 	if (ill_v6 != NULL) {
11541 		ill_v6 = phyi->phyint_illv6;
11542 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11543 		    ipif = ipif->ipif_next) {
11544 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11545 				mutex_enter(&phyi->phyint_lock);
11546 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11547 				mutex_exit(&phyi->phyint_lock);
11548 				return;
11549 			}
11550 		}
11551 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11552 		    ilm = ilm->ilm_next) {
11553 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11554 				mutex_enter(&phyi->phyint_lock);
11555 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11556 				mutex_exit(&phyi->phyint_lock);
11557 				return;
11558 			}
11559 		}
11560 	}
11561 	mutex_enter(&phyi->phyint_lock);
11562 	phyi->phyint_flags |= PHYI_INACTIVE;
11563 	mutex_exit(&phyi->phyint_lock);
11564 }
11565 
11566 /*
11567  * This function is called only when the phyint flags change. Currently
11568  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11569  * that we can select a good ill.
11570  */
11571 static void
11572 ip_redo_nomination(phyint_t *phyi)
11573 {
11574 	ill_t *ill_v4;
11575 
11576 	ill_v4 = phyi->phyint_illv4;
11577 
11578 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11579 		ASSERT(IAM_WRITER_ILL(ill_v4));
11580 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11581 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11582 	}
11583 }
11584 
11585 /*
11586  * Heuristic to check if ill is INACTIVE.
11587  * Checks if ill has an ipif with an usable ip address.
11588  *
11589  * Return values:
11590  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11591  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11592  */
11593 static boolean_t
11594 ill_is_inactive(ill_t *ill)
11595 {
11596 	ipif_t *ipif;
11597 
11598 	/* Check whether it is in an IPMP group */
11599 	if (ill->ill_phyint->phyint_groupname == NULL)
11600 		return (B_FALSE);
11601 
11602 	if (ill->ill_ipif_up_count == 0)
11603 		return (B_TRUE);
11604 
11605 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11606 		uint64_t flags = ipif->ipif_flags;
11607 
11608 		/*
11609 		 * This ipif is usable if it is IPIF_UP and not a
11610 		 * dedicated test address.  A dedicated test address
11611 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11612 		 * (note in particular that V6 test addresses are
11613 		 * link-local data addresses and thus are marked
11614 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11615 		 */
11616 		if ((flags & IPIF_UP) &&
11617 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11618 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11619 			return (B_FALSE);
11620 	}
11621 	return (B_TRUE);
11622 }
11623 
11624 /*
11625  * Set interface flags.
11626  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11627  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11628  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11629  *
11630  * NOTE : We really don't enforce that ipif_id zero should be used
11631  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11632  *	  is because applications generally does SICGLIFFLAGS and
11633  *	  ORs in the new flags (that affects the logical) and does a
11634  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11635  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11636  *	  flags that will be turned on is correct with respect to
11637  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11638  */
11639 /* ARGSUSED */
11640 int
11641 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11642     ip_ioctl_cmd_t *ipip, void *if_req)
11643 {
11644 	uint64_t turn_on;
11645 	uint64_t turn_off;
11646 	int	err;
11647 	boolean_t need_up = B_FALSE;
11648 	phyint_t *phyi;
11649 	ill_t *ill;
11650 	uint64_t intf_flags;
11651 	boolean_t phyint_flags_modified = B_FALSE;
11652 	uint64_t flags;
11653 	struct ifreq *ifr;
11654 	struct lifreq *lifr;
11655 	boolean_t set_linklocal = B_FALSE;
11656 	boolean_t zero_source = B_FALSE;
11657 	ip_stack_t *ipst;
11658 
11659 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11660 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11661 
11662 	ASSERT(IAM_WRITER_IPIF(ipif));
11663 
11664 	ill = ipif->ipif_ill;
11665 	phyi = ill->ill_phyint;
11666 	ipst = ill->ill_ipst;
11667 
11668 	if (ipip->ipi_cmd_type == IF_CMD) {
11669 		ifr = (struct ifreq *)if_req;
11670 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11671 	} else {
11672 		lifr = (struct lifreq *)if_req;
11673 		flags = lifr->lifr_flags;
11674 	}
11675 
11676 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11677 
11678 	/*
11679 	 * Has the flags been set correctly till now ?
11680 	 */
11681 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11682 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11683 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11684 	/*
11685 	 * Compare the new flags to the old, and partition
11686 	 * into those coming on and those going off.
11687 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11688 	 */
11689 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11690 		flags |= intf_flags & ~0xFFFF;
11691 
11692 	/*
11693 	 * First check which bits will change and then which will
11694 	 * go on and off
11695 	 */
11696 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11697 	if (!turn_on)
11698 		return (0);	/* No change */
11699 
11700 	turn_off = intf_flags & turn_on;
11701 	turn_on ^= turn_off;
11702 	err = 0;
11703 
11704 	/*
11705 	 * Don't allow any bits belonging to the logical interface
11706 	 * to be set or cleared on the replacement ipif that was
11707 	 * created temporarily during a MOVE.
11708 	 */
11709 	if (ipif->ipif_replace_zero &&
11710 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11711 		return (EINVAL);
11712 	}
11713 
11714 	/*
11715 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11716 	 * IPv6 interfaces.
11717 	 */
11718 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11719 		return (EINVAL);
11720 
11721 	/*
11722 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11723 	 */
11724 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11725 		return (EINVAL);
11726 
11727 	/*
11728 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11729 	 * interfaces.  It makes no sense in that context.
11730 	 */
11731 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11732 		return (EINVAL);
11733 
11734 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11735 		zero_source = B_TRUE;
11736 
11737 	/*
11738 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11739 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11740 	 * If the link local address isn't set, and can be set, it will get
11741 	 * set later on in this function.
11742 	 */
11743 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11744 	    (flags & IFF_UP) && !zero_source &&
11745 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11746 		if (ipif_cant_setlinklocal(ipif))
11747 			return (EINVAL);
11748 		set_linklocal = B_TRUE;
11749 	}
11750 
11751 	/*
11752 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11753 	 * same time. No need to grab ill_g_usesrc_lock here, see
11754 	 * synchronization notes in ip.c
11755 	 */
11756 	if (turn_on & PHYI_STANDBY &&
11757 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11758 		return (EINVAL);
11759 	}
11760 
11761 	/*
11762 	 * If we modify physical interface flags, we'll potentially need to
11763 	 * send up two routing socket messages for the changes (one for the
11764 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11765 	 */
11766 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11767 		phyint_flags_modified = B_TRUE;
11768 
11769 	/*
11770 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11771 	 * we need to flush the IRE_CACHES belonging to this ill.
11772 	 * We handle this case here without doing the DOWN/UP dance
11773 	 * like it is done for other flags. If some other flags are
11774 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11775 	 * below will handle it by bringing it down and then
11776 	 * bringing it UP.
11777 	 */
11778 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11779 		ill_t *ill_v4, *ill_v6;
11780 
11781 		ill_v4 = phyi->phyint_illv4;
11782 		ill_v6 = phyi->phyint_illv6;
11783 
11784 		/*
11785 		 * First set the INACTIVE flag if needed. Then delete the ires.
11786 		 * ire_add will atomically prevent creating new IRE_CACHEs
11787 		 * unless hidden flag is set.
11788 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11789 		 */
11790 		if ((turn_on & PHYI_FAILED) &&
11791 		    ((intf_flags & PHYI_STANDBY) ||
11792 		    !ipst->ips_ipmp_enable_failback)) {
11793 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11794 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11795 		}
11796 		if ((turn_off & PHYI_FAILED) &&
11797 		    ((intf_flags & PHYI_STANDBY) ||
11798 		    (!ipst->ips_ipmp_enable_failback &&
11799 		    ill_is_inactive(ill)))) {
11800 			phyint_inactive(phyi);
11801 		}
11802 
11803 		if (turn_on & PHYI_STANDBY) {
11804 			/*
11805 			 * We implicitly set INACTIVE only when STANDBY is set.
11806 			 * INACTIVE is also set on non-STANDBY phyint when user
11807 			 * disables FAILBACK using configuration file.
11808 			 * Do not allow STANDBY to be set on such INACTIVE
11809 			 * phyint
11810 			 */
11811 			if (phyi->phyint_flags & PHYI_INACTIVE)
11812 				return (EINVAL);
11813 			if (!(phyi->phyint_flags & PHYI_FAILED))
11814 				phyint_inactive(phyi);
11815 		}
11816 		if (turn_off & PHYI_STANDBY) {
11817 			if (ipst->ips_ipmp_enable_failback) {
11818 				/*
11819 				 * Reset PHYI_INACTIVE.
11820 				 */
11821 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11822 			} else if (ill_is_inactive(ill) &&
11823 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11824 				/*
11825 				 * Need to set INACTIVE, when user sets
11826 				 * STANDBY on a non-STANDBY phyint and
11827 				 * later resets STANDBY
11828 				 */
11829 				phyint_inactive(phyi);
11830 			}
11831 		}
11832 		/*
11833 		 * We should always send up a message so that the
11834 		 * daemons come to know of it. Note that the zeroth
11835 		 * interface can be down and the check below for IPIF_UP
11836 		 * will not make sense as we are actually setting
11837 		 * a phyint flag here. We assume that the ipif used
11838 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11839 		 * send up any message for non-zero ipifs).
11840 		 */
11841 		phyint_flags_modified = B_TRUE;
11842 
11843 		if (ill_v4 != NULL) {
11844 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11845 			    IRE_CACHE, ill_stq_cache_delete,
11846 			    (char *)ill_v4, ill_v4);
11847 			illgrp_reset_schednext(ill_v4);
11848 		}
11849 		if (ill_v6 != NULL) {
11850 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11851 			    IRE_CACHE, ill_stq_cache_delete,
11852 			    (char *)ill_v6, ill_v6);
11853 			illgrp_reset_schednext(ill_v6);
11854 		}
11855 	}
11856 
11857 	/*
11858 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11859 	 * status of the interface and, if the interface is part of an IPMP
11860 	 * group, all other interfaces that are part of the same IPMP
11861 	 * group.
11862 	 */
11863 	if ((turn_on | turn_off) & ILLF_ROUTER)
11864 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11865 
11866 	/*
11867 	 * If the interface is not UP and we are not going to
11868 	 * bring it UP, record the flags and return. When the
11869 	 * interface comes UP later, the right actions will be
11870 	 * taken.
11871 	 */
11872 	if (!(ipif->ipif_flags & IPIF_UP) &&
11873 	    !(turn_on & IPIF_UP)) {
11874 		/* Record new flags in their respective places. */
11875 		mutex_enter(&ill->ill_lock);
11876 		mutex_enter(&ill->ill_phyint->phyint_lock);
11877 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11878 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11879 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11880 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11881 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11882 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11883 		mutex_exit(&ill->ill_lock);
11884 		mutex_exit(&ill->ill_phyint->phyint_lock);
11885 
11886 		/*
11887 		 * We do the broadcast and nomination here rather
11888 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11889 		 * the case of FAILBACK from INACTIVE standby to the
11890 		 * interface that has been repaired, PHYI_FAILED has not
11891 		 * been cleared yet. If there are only two interfaces in
11892 		 * that group, all we have is a FAILED and INACTIVE
11893 		 * interface. If we do the nomination soon after a failback,
11894 		 * the broadcast nomination code would select the
11895 		 * INACTIVE interface for receiving broadcasts as FAILED is
11896 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11897 		 * receive broadcast packets, we need to redo nomination
11898 		 * when the FAILED is cleared here. Thus, in general we
11899 		 * always do the nomination here for FAILED, STANDBY
11900 		 * and OFFLINE.
11901 		 */
11902 		if (((turn_on | turn_off) &
11903 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11904 			ip_redo_nomination(phyi);
11905 		}
11906 		if (phyint_flags_modified) {
11907 			if (phyi->phyint_illv4 != NULL) {
11908 				ip_rts_ifmsg(phyi->phyint_illv4->
11909 				    ill_ipif);
11910 			}
11911 			if (phyi->phyint_illv6 != NULL) {
11912 				ip_rts_ifmsg(phyi->phyint_illv6->
11913 				    ill_ipif);
11914 			}
11915 		}
11916 		return (0);
11917 	} else if (set_linklocal || zero_source) {
11918 		mutex_enter(&ill->ill_lock);
11919 		if (set_linklocal)
11920 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11921 		if (zero_source)
11922 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11923 		mutex_exit(&ill->ill_lock);
11924 	}
11925 
11926 	/*
11927 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11928 	 * or point-to-point interfaces with an unspecified destination. We do
11929 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11930 	 * have a subnet assigned, which is how in.ndpd currently manages its
11931 	 * onlink prefix list when no addresses are configured with those
11932 	 * prefixes.
11933 	 */
11934 	if (ipif->ipif_isv6 &&
11935 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11936 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11937 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11938 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11939 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11940 		return (EINVAL);
11941 	}
11942 
11943 	/*
11944 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11945 	 * from being brought up.
11946 	 */
11947 	if (!ipif->ipif_isv6 &&
11948 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11949 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11950 		return (EINVAL);
11951 	}
11952 
11953 	/*
11954 	 * The only flag changes that we currently take specific action on
11955 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11956 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11957 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11958 	 * the flags and bringing it back up again.
11959 	 */
11960 	if ((turn_on|turn_off) &
11961 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11962 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11963 		/*
11964 		 * Taking this ipif down, make sure we have
11965 		 * valid net and subnet bcast ire's for other
11966 		 * logical interfaces, if we need them.
11967 		 */
11968 		if (!ipif->ipif_isv6)
11969 			ipif_check_bcast_ires(ipif);
11970 
11971 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11972 		    !(turn_off & IPIF_UP)) {
11973 			need_up = B_TRUE;
11974 			if (ipif->ipif_flags & IPIF_UP)
11975 				ill->ill_logical_down = 1;
11976 			turn_on &= ~IPIF_UP;
11977 		}
11978 		err = ipif_down(ipif, q, mp);
11979 		ip1dbg(("ipif_down returns %d err ", err));
11980 		if (err == EINPROGRESS)
11981 			return (err);
11982 		ipif_down_tail(ipif);
11983 	}
11984 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11985 }
11986 
11987 static int
11988 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11989     boolean_t need_up)
11990 {
11991 	ill_t	*ill;
11992 	phyint_t *phyi;
11993 	uint64_t turn_on;
11994 	uint64_t turn_off;
11995 	uint64_t intf_flags;
11996 	boolean_t phyint_flags_modified = B_FALSE;
11997 	int	err = 0;
11998 	boolean_t set_linklocal = B_FALSE;
11999 	boolean_t zero_source = B_FALSE;
12000 
12001 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12002 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12003 
12004 	ASSERT(IAM_WRITER_IPIF(ipif));
12005 
12006 	ill = ipif->ipif_ill;
12007 	phyi = ill->ill_phyint;
12008 
12009 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12010 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12011 
12012 	turn_off = intf_flags & turn_on;
12013 	turn_on ^= turn_off;
12014 
12015 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12016 		phyint_flags_modified = B_TRUE;
12017 
12018 	/*
12019 	 * Now we change the flags. Track current value of
12020 	 * other flags in their respective places.
12021 	 */
12022 	mutex_enter(&ill->ill_lock);
12023 	mutex_enter(&phyi->phyint_lock);
12024 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12025 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12026 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12027 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12028 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12029 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12030 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12031 		set_linklocal = B_TRUE;
12032 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12033 	}
12034 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12035 		zero_source = B_TRUE;
12036 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12037 	}
12038 	mutex_exit(&ill->ill_lock);
12039 	mutex_exit(&phyi->phyint_lock);
12040 
12041 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12042 		ip_redo_nomination(phyi);
12043 
12044 	if (set_linklocal)
12045 		(void) ipif_setlinklocal(ipif);
12046 
12047 	if (zero_source)
12048 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12049 	else
12050 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12051 
12052 	if (need_up) {
12053 		/*
12054 		 * XXX ipif_up really does not know whether a phyint flags
12055 		 * was modified or not. So, it sends up information on
12056 		 * only one routing sockets message. As we don't bring up
12057 		 * the interface and also set STANDBY/FAILED simultaneously
12058 		 * it should be okay.
12059 		 */
12060 		err = ipif_up(ipif, q, mp);
12061 	} else {
12062 		/*
12063 		 * Make sure routing socket sees all changes to the flags.
12064 		 * ipif_up_done* handles this when we use ipif_up.
12065 		 */
12066 		if (phyint_flags_modified) {
12067 			if (phyi->phyint_illv4 != NULL) {
12068 				ip_rts_ifmsg(phyi->phyint_illv4->
12069 				    ill_ipif);
12070 			}
12071 			if (phyi->phyint_illv6 != NULL) {
12072 				ip_rts_ifmsg(phyi->phyint_illv6->
12073 				    ill_ipif);
12074 			}
12075 		} else {
12076 			ip_rts_ifmsg(ipif);
12077 		}
12078 		/*
12079 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12080 		 * this in need_up case.
12081 		 */
12082 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12083 	}
12084 	return (err);
12085 }
12086 
12087 /*
12088  * Restart entry point to restart the flags restart operation after the
12089  * refcounts have dropped to zero.
12090  */
12091 /* ARGSUSED */
12092 int
12093 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12094     ip_ioctl_cmd_t *ipip, void *if_req)
12095 {
12096 	int	err;
12097 	struct ifreq *ifr = (struct ifreq *)if_req;
12098 	struct lifreq *lifr = (struct lifreq *)if_req;
12099 
12100 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12101 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12102 
12103 	ipif_down_tail(ipif);
12104 	if (ipip->ipi_cmd_type == IF_CMD) {
12105 		/*
12106 		 * Since ip_sioctl_flags expects an int and ifr_flags
12107 		 * is a short we need to cast ifr_flags into an int
12108 		 * to avoid having sign extension cause bits to get
12109 		 * set that should not be.
12110 		 */
12111 		err = ip_sioctl_flags_tail(ipif,
12112 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12113 		    q, mp, B_TRUE);
12114 	} else {
12115 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12116 		    q, mp, B_TRUE);
12117 	}
12118 	return (err);
12119 }
12120 
12121 /*
12122  * Can operate on either a module or a driver queue.
12123  */
12124 /* ARGSUSED */
12125 int
12126 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12127     ip_ioctl_cmd_t *ipip, void *if_req)
12128 {
12129 	/*
12130 	 * Has the flags been set correctly till now ?
12131 	 */
12132 	ill_t *ill = ipif->ipif_ill;
12133 	phyint_t *phyi = ill->ill_phyint;
12134 
12135 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12136 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12137 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12138 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12139 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12140 
12141 	/*
12142 	 * Need a lock since some flags can be set even when there are
12143 	 * references to the ipif.
12144 	 */
12145 	mutex_enter(&ill->ill_lock);
12146 	if (ipip->ipi_cmd_type == IF_CMD) {
12147 		struct ifreq *ifr = (struct ifreq *)if_req;
12148 
12149 		/* Get interface flags (low 16 only). */
12150 		ifr->ifr_flags = ((ipif->ipif_flags |
12151 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12152 	} else {
12153 		struct lifreq *lifr = (struct lifreq *)if_req;
12154 
12155 		/* Get interface flags. */
12156 		lifr->lifr_flags = ipif->ipif_flags |
12157 		    ill->ill_flags | phyi->phyint_flags;
12158 	}
12159 	mutex_exit(&ill->ill_lock);
12160 	return (0);
12161 }
12162 
12163 /* ARGSUSED */
12164 int
12165 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12166     ip_ioctl_cmd_t *ipip, void *if_req)
12167 {
12168 	int mtu;
12169 	int ip_min_mtu;
12170 	struct ifreq	*ifr;
12171 	struct lifreq *lifr;
12172 	ire_t	*ire;
12173 	ip_stack_t *ipst;
12174 
12175 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12176 	    ipif->ipif_id, (void *)ipif));
12177 	if (ipip->ipi_cmd_type == IF_CMD) {
12178 		ifr = (struct ifreq *)if_req;
12179 		mtu = ifr->ifr_metric;
12180 	} else {
12181 		lifr = (struct lifreq *)if_req;
12182 		mtu = lifr->lifr_mtu;
12183 	}
12184 
12185 	if (ipif->ipif_isv6)
12186 		ip_min_mtu = IPV6_MIN_MTU;
12187 	else
12188 		ip_min_mtu = IP_MIN_MTU;
12189 
12190 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12191 		return (EINVAL);
12192 
12193 	/*
12194 	 * Change the MTU size in all relevant ire's.
12195 	 * Mtu change Vs. new ire creation - protocol below.
12196 	 * First change ipif_mtu and the ire_max_frag of the
12197 	 * interface ire. Then do an ire walk and change the
12198 	 * ire_max_frag of all affected ires. During ire_add
12199 	 * under the bucket lock, set the ire_max_frag of the
12200 	 * new ire being created from the ipif/ire from which
12201 	 * it is being derived. If an mtu change happens after
12202 	 * the ire is added, the new ire will be cleaned up.
12203 	 * Conversely if the mtu change happens before the ire
12204 	 * is added, ire_add will see the new value of the mtu.
12205 	 */
12206 	ipif->ipif_mtu = mtu;
12207 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12208 
12209 	if (ipif->ipif_isv6)
12210 		ire = ipif_to_ire_v6(ipif);
12211 	else
12212 		ire = ipif_to_ire(ipif);
12213 	if (ire != NULL) {
12214 		ire->ire_max_frag = ipif->ipif_mtu;
12215 		ire_refrele(ire);
12216 	}
12217 	ipst = ipif->ipif_ill->ill_ipst;
12218 	if (ipif->ipif_flags & IPIF_UP) {
12219 		if (ipif->ipif_isv6)
12220 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12221 			    ipst);
12222 		else
12223 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12224 			    ipst);
12225 	}
12226 	/* Update the MTU in SCTP's list */
12227 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12228 	return (0);
12229 }
12230 
12231 /* Get interface MTU. */
12232 /* ARGSUSED */
12233 int
12234 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12235 	ip_ioctl_cmd_t *ipip, void *if_req)
12236 {
12237 	struct ifreq	*ifr;
12238 	struct lifreq	*lifr;
12239 
12240 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12241 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12242 	if (ipip->ipi_cmd_type == IF_CMD) {
12243 		ifr = (struct ifreq *)if_req;
12244 		ifr->ifr_metric = ipif->ipif_mtu;
12245 	} else {
12246 		lifr = (struct lifreq *)if_req;
12247 		lifr->lifr_mtu = ipif->ipif_mtu;
12248 	}
12249 	return (0);
12250 }
12251 
12252 /* Set interface broadcast address. */
12253 /* ARGSUSED2 */
12254 int
12255 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12256 	ip_ioctl_cmd_t *ipip, void *if_req)
12257 {
12258 	ipaddr_t addr;
12259 	ire_t	*ire;
12260 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12261 
12262 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12263 	    ipif->ipif_id));
12264 
12265 	ASSERT(IAM_WRITER_IPIF(ipif));
12266 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12267 		return (EADDRNOTAVAIL);
12268 
12269 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12270 
12271 	if (sin->sin_family != AF_INET)
12272 		return (EAFNOSUPPORT);
12273 
12274 	addr = sin->sin_addr.s_addr;
12275 	if (ipif->ipif_flags & IPIF_UP) {
12276 		/*
12277 		 * If we are already up, make sure the new
12278 		 * broadcast address makes sense.  If it does,
12279 		 * there should be an IRE for it already.
12280 		 * Don't match on ipif, only on the ill
12281 		 * since we are sharing these now. Don't use
12282 		 * MATCH_IRE_ILL_GROUP as we are looking for
12283 		 * the broadcast ire on this ill and each ill
12284 		 * in the group has its own broadcast ire.
12285 		 */
12286 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12287 		    ipif, ALL_ZONES, NULL,
12288 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12289 		if (ire == NULL) {
12290 			return (EINVAL);
12291 		} else {
12292 			ire_refrele(ire);
12293 		}
12294 	}
12295 	/*
12296 	 * Changing the broadcast addr for this ipif.
12297 	 * Make sure we have valid net and subnet bcast
12298 	 * ire's for other logical interfaces, if needed.
12299 	 */
12300 	if (addr != ipif->ipif_brd_addr)
12301 		ipif_check_bcast_ires(ipif);
12302 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12303 	return (0);
12304 }
12305 
12306 /* Get interface broadcast address. */
12307 /* ARGSUSED */
12308 int
12309 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12310     ip_ioctl_cmd_t *ipip, void *if_req)
12311 {
12312 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12313 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12314 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12315 		return (EADDRNOTAVAIL);
12316 
12317 	/* IPIF_BROADCAST not possible with IPv6 */
12318 	ASSERT(!ipif->ipif_isv6);
12319 	*sin = sin_null;
12320 	sin->sin_family = AF_INET;
12321 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12322 	return (0);
12323 }
12324 
12325 /*
12326  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12327  */
12328 /* ARGSUSED */
12329 int
12330 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12331     ip_ioctl_cmd_t *ipip, void *if_req)
12332 {
12333 	int err = 0;
12334 	in6_addr_t v6mask;
12335 
12336 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12337 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12338 
12339 	ASSERT(IAM_WRITER_IPIF(ipif));
12340 
12341 	if (ipif->ipif_isv6) {
12342 		sin6_t *sin6;
12343 
12344 		if (sin->sin_family != AF_INET6)
12345 			return (EAFNOSUPPORT);
12346 
12347 		sin6 = (sin6_t *)sin;
12348 		v6mask = sin6->sin6_addr;
12349 	} else {
12350 		ipaddr_t mask;
12351 
12352 		if (sin->sin_family != AF_INET)
12353 			return (EAFNOSUPPORT);
12354 
12355 		mask = sin->sin_addr.s_addr;
12356 		V4MASK_TO_V6(mask, v6mask);
12357 	}
12358 
12359 	/*
12360 	 * No big deal if the interface isn't already up, or the mask
12361 	 * isn't really changing, or this is pt-pt.
12362 	 */
12363 	if (!(ipif->ipif_flags & IPIF_UP) ||
12364 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12365 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12366 		ipif->ipif_v6net_mask = v6mask;
12367 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12368 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12369 			    ipif->ipif_v6net_mask,
12370 			    ipif->ipif_v6subnet);
12371 		}
12372 		return (0);
12373 	}
12374 	/*
12375 	 * Make sure we have valid net and subnet broadcast ire's
12376 	 * for the old netmask, if needed by other logical interfaces.
12377 	 */
12378 	if (!ipif->ipif_isv6)
12379 		ipif_check_bcast_ires(ipif);
12380 
12381 	err = ipif_logical_down(ipif, q, mp);
12382 	if (err == EINPROGRESS)
12383 		return (err);
12384 	ipif_down_tail(ipif);
12385 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12386 	return (err);
12387 }
12388 
12389 static int
12390 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12391 {
12392 	in6_addr_t v6mask;
12393 	int err = 0;
12394 
12395 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12396 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12397 
12398 	if (ipif->ipif_isv6) {
12399 		sin6_t *sin6;
12400 
12401 		sin6 = (sin6_t *)sin;
12402 		v6mask = sin6->sin6_addr;
12403 	} else {
12404 		ipaddr_t mask;
12405 
12406 		mask = sin->sin_addr.s_addr;
12407 		V4MASK_TO_V6(mask, v6mask);
12408 	}
12409 
12410 	ipif->ipif_v6net_mask = v6mask;
12411 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12412 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12413 		    ipif->ipif_v6subnet);
12414 	}
12415 	err = ipif_up(ipif, q, mp);
12416 
12417 	if (err == 0 || err == EINPROGRESS) {
12418 		/*
12419 		 * The interface must be DL_BOUND if this packet has to
12420 		 * go out on the wire. Since we only go through a logical
12421 		 * down and are bound with the driver during an internal
12422 		 * down/up that is satisfied.
12423 		 */
12424 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12425 			/* Potentially broadcast an address mask reply. */
12426 			ipif_mask_reply(ipif);
12427 		}
12428 	}
12429 	return (err);
12430 }
12431 
12432 /* ARGSUSED */
12433 int
12434 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12435     ip_ioctl_cmd_t *ipip, void *if_req)
12436 {
12437 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12438 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12439 	ipif_down_tail(ipif);
12440 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12441 }
12442 
12443 /* Get interface net mask. */
12444 /* ARGSUSED */
12445 int
12446 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12447     ip_ioctl_cmd_t *ipip, void *if_req)
12448 {
12449 	struct lifreq *lifr = (struct lifreq *)if_req;
12450 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12451 
12452 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12453 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12454 
12455 	/*
12456 	 * net mask can't change since we have a reference to the ipif.
12457 	 */
12458 	if (ipif->ipif_isv6) {
12459 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12460 		*sin6 = sin6_null;
12461 		sin6->sin6_family = AF_INET6;
12462 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12463 		lifr->lifr_addrlen =
12464 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12465 	} else {
12466 		*sin = sin_null;
12467 		sin->sin_family = AF_INET;
12468 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12469 		if (ipip->ipi_cmd_type == LIF_CMD) {
12470 			lifr->lifr_addrlen =
12471 			    ip_mask_to_plen(ipif->ipif_net_mask);
12472 		}
12473 	}
12474 	return (0);
12475 }
12476 
12477 /* ARGSUSED */
12478 int
12479 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12480     ip_ioctl_cmd_t *ipip, void *if_req)
12481 {
12482 
12483 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12484 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12485 	/*
12486 	 * Set interface metric.  We don't use this for
12487 	 * anything but we keep track of it in case it is
12488 	 * important to routing applications or such.
12489 	 */
12490 	if (ipip->ipi_cmd_type == IF_CMD) {
12491 		struct ifreq    *ifr;
12492 
12493 		ifr = (struct ifreq *)if_req;
12494 		ipif->ipif_metric = ifr->ifr_metric;
12495 	} else {
12496 		struct lifreq   *lifr;
12497 
12498 		lifr = (struct lifreq *)if_req;
12499 		ipif->ipif_metric = lifr->lifr_metric;
12500 	}
12501 	return (0);
12502 }
12503 
12504 
12505 /* ARGSUSED */
12506 int
12507 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12508     ip_ioctl_cmd_t *ipip, void *if_req)
12509 {
12510 
12511 	/* Get interface metric. */
12512 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12513 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12514 	if (ipip->ipi_cmd_type == IF_CMD) {
12515 		struct ifreq    *ifr;
12516 
12517 		ifr = (struct ifreq *)if_req;
12518 		ifr->ifr_metric = ipif->ipif_metric;
12519 	} else {
12520 		struct lifreq   *lifr;
12521 
12522 		lifr = (struct lifreq *)if_req;
12523 		lifr->lifr_metric = ipif->ipif_metric;
12524 	}
12525 
12526 	return (0);
12527 }
12528 
12529 /* ARGSUSED */
12530 int
12531 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12532     ip_ioctl_cmd_t *ipip, void *if_req)
12533 {
12534 
12535 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12536 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12537 	/*
12538 	 * Set the muxid returned from I_PLINK.
12539 	 */
12540 	if (ipip->ipi_cmd_type == IF_CMD) {
12541 		struct ifreq *ifr = (struct ifreq *)if_req;
12542 
12543 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12544 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12545 	} else {
12546 		struct lifreq *lifr = (struct lifreq *)if_req;
12547 
12548 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12549 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12550 	}
12551 	return (0);
12552 }
12553 
12554 /* ARGSUSED */
12555 int
12556 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12557     ip_ioctl_cmd_t *ipip, void *if_req)
12558 {
12559 
12560 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12561 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12562 	/*
12563 	 * Get the muxid saved in ill for I_PUNLINK.
12564 	 */
12565 	if (ipip->ipi_cmd_type == IF_CMD) {
12566 		struct ifreq *ifr = (struct ifreq *)if_req;
12567 
12568 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12569 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12570 	} else {
12571 		struct lifreq *lifr = (struct lifreq *)if_req;
12572 
12573 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12574 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12575 	}
12576 	return (0);
12577 }
12578 
12579 /*
12580  * Set the subnet prefix. Does not modify the broadcast address.
12581  */
12582 /* ARGSUSED */
12583 int
12584 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12585     ip_ioctl_cmd_t *ipip, void *if_req)
12586 {
12587 	int err = 0;
12588 	in6_addr_t v6addr;
12589 	in6_addr_t v6mask;
12590 	boolean_t need_up = B_FALSE;
12591 	int addrlen;
12592 
12593 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12594 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12595 
12596 	ASSERT(IAM_WRITER_IPIF(ipif));
12597 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12598 
12599 	if (ipif->ipif_isv6) {
12600 		sin6_t *sin6;
12601 
12602 		if (sin->sin_family != AF_INET6)
12603 			return (EAFNOSUPPORT);
12604 
12605 		sin6 = (sin6_t *)sin;
12606 		v6addr = sin6->sin6_addr;
12607 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12608 			return (EADDRNOTAVAIL);
12609 	} else {
12610 		ipaddr_t addr;
12611 
12612 		if (sin->sin_family != AF_INET)
12613 			return (EAFNOSUPPORT);
12614 
12615 		addr = sin->sin_addr.s_addr;
12616 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12617 			return (EADDRNOTAVAIL);
12618 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12619 		/* Add 96 bits */
12620 		addrlen += IPV6_ABITS - IP_ABITS;
12621 	}
12622 
12623 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12624 		return (EINVAL);
12625 
12626 	/* Check if bits in the address is set past the mask */
12627 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12628 		return (EINVAL);
12629 
12630 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12631 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12632 		return (0);	/* No change */
12633 
12634 	if (ipif->ipif_flags & IPIF_UP) {
12635 		/*
12636 		 * If the interface is already marked up,
12637 		 * we call ipif_down which will take care
12638 		 * of ditching any IREs that have been set
12639 		 * up based on the old interface address.
12640 		 */
12641 		err = ipif_logical_down(ipif, q, mp);
12642 		if (err == EINPROGRESS)
12643 			return (err);
12644 		ipif_down_tail(ipif);
12645 		need_up = B_TRUE;
12646 	}
12647 
12648 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12649 	return (err);
12650 }
12651 
12652 static int
12653 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12654     queue_t *q, mblk_t *mp, boolean_t need_up)
12655 {
12656 	ill_t	*ill = ipif->ipif_ill;
12657 	int	err = 0;
12658 
12659 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12660 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12661 
12662 	/* Set the new address. */
12663 	mutex_enter(&ill->ill_lock);
12664 	ipif->ipif_v6net_mask = v6mask;
12665 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12666 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12667 		    ipif->ipif_v6subnet);
12668 	}
12669 	mutex_exit(&ill->ill_lock);
12670 
12671 	if (need_up) {
12672 		/*
12673 		 * Now bring the interface back up.  If this
12674 		 * is the only IPIF for the ILL, ipif_up
12675 		 * will have to re-bind to the device, so
12676 		 * we may get back EINPROGRESS, in which
12677 		 * case, this IOCTL will get completed in
12678 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12679 		 */
12680 		err = ipif_up(ipif, q, mp);
12681 		if (err == EINPROGRESS)
12682 			return (err);
12683 	}
12684 	return (err);
12685 }
12686 
12687 /* ARGSUSED */
12688 int
12689 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12690     ip_ioctl_cmd_t *ipip, void *if_req)
12691 {
12692 	int	addrlen;
12693 	in6_addr_t v6addr;
12694 	in6_addr_t v6mask;
12695 	struct lifreq *lifr = (struct lifreq *)if_req;
12696 
12697 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12698 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12699 	ipif_down_tail(ipif);
12700 
12701 	addrlen = lifr->lifr_addrlen;
12702 	if (ipif->ipif_isv6) {
12703 		sin6_t *sin6;
12704 
12705 		sin6 = (sin6_t *)sin;
12706 		v6addr = sin6->sin6_addr;
12707 	} else {
12708 		ipaddr_t addr;
12709 
12710 		addr = sin->sin_addr.s_addr;
12711 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12712 		addrlen += IPV6_ABITS - IP_ABITS;
12713 	}
12714 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12715 
12716 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12717 }
12718 
12719 /* ARGSUSED */
12720 int
12721 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12722     ip_ioctl_cmd_t *ipip, void *if_req)
12723 {
12724 	struct lifreq *lifr = (struct lifreq *)if_req;
12725 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12726 
12727 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12728 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12729 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12730 
12731 	if (ipif->ipif_isv6) {
12732 		*sin6 = sin6_null;
12733 		sin6->sin6_family = AF_INET6;
12734 		sin6->sin6_addr = ipif->ipif_v6subnet;
12735 		lifr->lifr_addrlen =
12736 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12737 	} else {
12738 		*sin = sin_null;
12739 		sin->sin_family = AF_INET;
12740 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12741 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12742 	}
12743 	return (0);
12744 }
12745 
12746 /*
12747  * Set the IPv6 address token.
12748  */
12749 /* ARGSUSED */
12750 int
12751 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12752     ip_ioctl_cmd_t *ipi, void *if_req)
12753 {
12754 	ill_t *ill = ipif->ipif_ill;
12755 	int err;
12756 	in6_addr_t v6addr;
12757 	in6_addr_t v6mask;
12758 	boolean_t need_up = B_FALSE;
12759 	int i;
12760 	sin6_t *sin6 = (sin6_t *)sin;
12761 	struct lifreq *lifr = (struct lifreq *)if_req;
12762 	int addrlen;
12763 
12764 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12765 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12766 	ASSERT(IAM_WRITER_IPIF(ipif));
12767 
12768 	addrlen = lifr->lifr_addrlen;
12769 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12770 	if (ipif->ipif_id != 0)
12771 		return (EINVAL);
12772 
12773 	if (!ipif->ipif_isv6)
12774 		return (EINVAL);
12775 
12776 	if (addrlen > IPV6_ABITS)
12777 		return (EINVAL);
12778 
12779 	v6addr = sin6->sin6_addr;
12780 
12781 	/*
12782 	 * The length of the token is the length from the end.  To get
12783 	 * the proper mask for this, compute the mask of the bits not
12784 	 * in the token; ie. the prefix, and then xor to get the mask.
12785 	 */
12786 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12787 		return (EINVAL);
12788 	for (i = 0; i < 4; i++) {
12789 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12790 	}
12791 
12792 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12793 	    ill->ill_token_length == addrlen)
12794 		return (0);	/* No change */
12795 
12796 	if (ipif->ipif_flags & IPIF_UP) {
12797 		err = ipif_logical_down(ipif, q, mp);
12798 		if (err == EINPROGRESS)
12799 			return (err);
12800 		ipif_down_tail(ipif);
12801 		need_up = B_TRUE;
12802 	}
12803 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12804 	return (err);
12805 }
12806 
12807 static int
12808 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12809     mblk_t *mp, boolean_t need_up)
12810 {
12811 	in6_addr_t v6addr;
12812 	in6_addr_t v6mask;
12813 	ill_t	*ill = ipif->ipif_ill;
12814 	int	i;
12815 	int	err = 0;
12816 
12817 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12818 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12819 	v6addr = sin6->sin6_addr;
12820 	/*
12821 	 * The length of the token is the length from the end.  To get
12822 	 * the proper mask for this, compute the mask of the bits not
12823 	 * in the token; ie. the prefix, and then xor to get the mask.
12824 	 */
12825 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12826 	for (i = 0; i < 4; i++)
12827 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12828 
12829 	mutex_enter(&ill->ill_lock);
12830 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12831 	ill->ill_token_length = addrlen;
12832 	mutex_exit(&ill->ill_lock);
12833 
12834 	if (need_up) {
12835 		/*
12836 		 * Now bring the interface back up.  If this
12837 		 * is the only IPIF for the ILL, ipif_up
12838 		 * will have to re-bind to the device, so
12839 		 * we may get back EINPROGRESS, in which
12840 		 * case, this IOCTL will get completed in
12841 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12842 		 */
12843 		err = ipif_up(ipif, q, mp);
12844 		if (err == EINPROGRESS)
12845 			return (err);
12846 	}
12847 	return (err);
12848 }
12849 
12850 /* ARGSUSED */
12851 int
12852 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12853     ip_ioctl_cmd_t *ipi, void *if_req)
12854 {
12855 	ill_t *ill;
12856 	sin6_t *sin6 = (sin6_t *)sin;
12857 	struct lifreq *lifr = (struct lifreq *)if_req;
12858 
12859 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12860 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12861 	if (ipif->ipif_id != 0)
12862 		return (EINVAL);
12863 
12864 	ill = ipif->ipif_ill;
12865 	if (!ill->ill_isv6)
12866 		return (ENXIO);
12867 
12868 	*sin6 = sin6_null;
12869 	sin6->sin6_family = AF_INET6;
12870 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12871 	sin6->sin6_addr = ill->ill_token;
12872 	lifr->lifr_addrlen = ill->ill_token_length;
12873 	return (0);
12874 }
12875 
12876 /*
12877  * Set (hardware) link specific information that might override
12878  * what was acquired through the DL_INFO_ACK.
12879  * The logic is as follows.
12880  *
12881  * become exclusive
12882  * set CHANGING flag
12883  * change mtu on affected IREs
12884  * clear CHANGING flag
12885  *
12886  * An ire add that occurs before the CHANGING flag is set will have its mtu
12887  * changed by the ip_sioctl_lnkinfo.
12888  *
12889  * During the time the CHANGING flag is set, no new ires will be added to the
12890  * bucket, and ire add will fail (due the CHANGING flag).
12891  *
12892  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12893  * before it is added to the bucket.
12894  *
12895  * Obviously only 1 thread can set the CHANGING flag and we need to become
12896  * exclusive to set the flag.
12897  */
12898 /* ARGSUSED */
12899 int
12900 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12901     ip_ioctl_cmd_t *ipi, void *if_req)
12902 {
12903 	ill_t		*ill = ipif->ipif_ill;
12904 	ipif_t		*nipif;
12905 	int		ip_min_mtu;
12906 	boolean_t	mtu_walk = B_FALSE;
12907 	struct lifreq	*lifr = (struct lifreq *)if_req;
12908 	lif_ifinfo_req_t *lir;
12909 	ire_t		*ire;
12910 
12911 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12912 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12913 	lir = &lifr->lifr_ifinfo;
12914 	ASSERT(IAM_WRITER_IPIF(ipif));
12915 
12916 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12917 	if (ipif->ipif_id != 0)
12918 		return (EINVAL);
12919 
12920 	/* Set interface MTU. */
12921 	if (ipif->ipif_isv6)
12922 		ip_min_mtu = IPV6_MIN_MTU;
12923 	else
12924 		ip_min_mtu = IP_MIN_MTU;
12925 
12926 	/*
12927 	 * Verify values before we set anything. Allow zero to
12928 	 * mean unspecified.
12929 	 */
12930 	if (lir->lir_maxmtu != 0 &&
12931 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12932 	    lir->lir_maxmtu < ip_min_mtu))
12933 		return (EINVAL);
12934 	if (lir->lir_reachtime != 0 &&
12935 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12936 		return (EINVAL);
12937 	if (lir->lir_reachretrans != 0 &&
12938 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12939 		return (EINVAL);
12940 
12941 	mutex_enter(&ill->ill_lock);
12942 	ill->ill_state_flags |= ILL_CHANGING;
12943 	for (nipif = ill->ill_ipif; nipif != NULL;
12944 	    nipif = nipif->ipif_next) {
12945 		nipif->ipif_state_flags |= IPIF_CHANGING;
12946 	}
12947 
12948 	mutex_exit(&ill->ill_lock);
12949 
12950 	if (lir->lir_maxmtu != 0) {
12951 		ill->ill_max_mtu = lir->lir_maxmtu;
12952 		ill->ill_mtu_userspecified = 1;
12953 		mtu_walk = B_TRUE;
12954 	}
12955 
12956 	if (lir->lir_reachtime != 0)
12957 		ill->ill_reachable_time = lir->lir_reachtime;
12958 
12959 	if (lir->lir_reachretrans != 0)
12960 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12961 
12962 	ill->ill_max_hops = lir->lir_maxhops;
12963 
12964 	ill->ill_max_buf = ND_MAX_Q;
12965 
12966 	if (mtu_walk) {
12967 		/*
12968 		 * Set the MTU on all ipifs associated with this ill except
12969 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12970 		 */
12971 		for (nipif = ill->ill_ipif; nipif != NULL;
12972 		    nipif = nipif->ipif_next) {
12973 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12974 				continue;
12975 
12976 			nipif->ipif_mtu = ill->ill_max_mtu;
12977 
12978 			if (!(nipif->ipif_flags & IPIF_UP))
12979 				continue;
12980 
12981 			if (nipif->ipif_isv6)
12982 				ire = ipif_to_ire_v6(nipif);
12983 			else
12984 				ire = ipif_to_ire(nipif);
12985 			if (ire != NULL) {
12986 				ire->ire_max_frag = ipif->ipif_mtu;
12987 				ire_refrele(ire);
12988 			}
12989 			if (ill->ill_isv6) {
12990 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12991 				    ipif_mtu_change, (char *)nipif,
12992 				    ill);
12993 			} else {
12994 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12995 				    ipif_mtu_change, (char *)nipif,
12996 				    ill);
12997 			}
12998 		}
12999 	}
13000 
13001 	mutex_enter(&ill->ill_lock);
13002 	for (nipif = ill->ill_ipif; nipif != NULL;
13003 	    nipif = nipif->ipif_next) {
13004 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13005 	}
13006 	ILL_UNMARK_CHANGING(ill);
13007 	mutex_exit(&ill->ill_lock);
13008 
13009 	return (0);
13010 }
13011 
13012 /* ARGSUSED */
13013 int
13014 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13015     ip_ioctl_cmd_t *ipi, void *if_req)
13016 {
13017 	struct lif_ifinfo_req *lir;
13018 	ill_t *ill = ipif->ipif_ill;
13019 
13020 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13021 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13022 	if (ipif->ipif_id != 0)
13023 		return (EINVAL);
13024 
13025 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13026 	lir->lir_maxhops = ill->ill_max_hops;
13027 	lir->lir_reachtime = ill->ill_reachable_time;
13028 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13029 	lir->lir_maxmtu = ill->ill_max_mtu;
13030 
13031 	return (0);
13032 }
13033 
13034 /*
13035  * Return best guess as to the subnet mask for the specified address.
13036  * Based on the subnet masks for all the configured interfaces.
13037  *
13038  * We end up returning a zero mask in the case of default, multicast or
13039  * experimental.
13040  */
13041 static ipaddr_t
13042 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13043 {
13044 	ipaddr_t net_mask;
13045 	ill_t	*ill;
13046 	ipif_t	*ipif;
13047 	ill_walk_context_t ctx;
13048 	ipif_t	*fallback_ipif = NULL;
13049 
13050 	net_mask = ip_net_mask(addr);
13051 	if (net_mask == 0) {
13052 		*ipifp = NULL;
13053 		return (0);
13054 	}
13055 
13056 	/* Let's check to see if this is maybe a local subnet route. */
13057 	/* this function only applies to IPv4 interfaces */
13058 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13059 	ill = ILL_START_WALK_V4(&ctx, ipst);
13060 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13061 		mutex_enter(&ill->ill_lock);
13062 		for (ipif = ill->ill_ipif; ipif != NULL;
13063 		    ipif = ipif->ipif_next) {
13064 			if (!IPIF_CAN_LOOKUP(ipif))
13065 				continue;
13066 			if (!(ipif->ipif_flags & IPIF_UP))
13067 				continue;
13068 			if ((ipif->ipif_subnet & net_mask) ==
13069 			    (addr & net_mask)) {
13070 				/*
13071 				 * Don't trust pt-pt interfaces if there are
13072 				 * other interfaces.
13073 				 */
13074 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13075 					if (fallback_ipif == NULL) {
13076 						ipif_refhold_locked(ipif);
13077 						fallback_ipif = ipif;
13078 					}
13079 					continue;
13080 				}
13081 
13082 				/*
13083 				 * Fine. Just assume the same net mask as the
13084 				 * directly attached subnet interface is using.
13085 				 */
13086 				ipif_refhold_locked(ipif);
13087 				mutex_exit(&ill->ill_lock);
13088 				rw_exit(&ipst->ips_ill_g_lock);
13089 				if (fallback_ipif != NULL)
13090 					ipif_refrele(fallback_ipif);
13091 				*ipifp = ipif;
13092 				return (ipif->ipif_net_mask);
13093 			}
13094 		}
13095 		mutex_exit(&ill->ill_lock);
13096 	}
13097 	rw_exit(&ipst->ips_ill_g_lock);
13098 
13099 	*ipifp = fallback_ipif;
13100 	return ((fallback_ipif != NULL) ?
13101 	    fallback_ipif->ipif_net_mask : net_mask);
13102 }
13103 
13104 /*
13105  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13106  */
13107 static void
13108 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13109 {
13110 	IOCP	iocp;
13111 	ipft_t	*ipft;
13112 	ipllc_t	*ipllc;
13113 	mblk_t	*mp1;
13114 	cred_t	*cr;
13115 	int	error = 0;
13116 	conn_t	*connp;
13117 
13118 	ip1dbg(("ip_wput_ioctl"));
13119 	iocp = (IOCP)mp->b_rptr;
13120 	mp1 = mp->b_cont;
13121 	if (mp1 == NULL) {
13122 		iocp->ioc_error = EINVAL;
13123 		mp->b_datap->db_type = M_IOCNAK;
13124 		iocp->ioc_count = 0;
13125 		qreply(q, mp);
13126 		return;
13127 	}
13128 
13129 	/*
13130 	 * These IOCTLs provide various control capabilities to
13131 	 * upstream agents such as ULPs and processes.	There
13132 	 * are currently two such IOCTLs implemented.  They
13133 	 * are used by TCP to provide update information for
13134 	 * existing IREs and to forcibly delete an IRE for a
13135 	 * host that is not responding, thereby forcing an
13136 	 * attempt at a new route.
13137 	 */
13138 	iocp->ioc_error = EINVAL;
13139 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13140 		goto done;
13141 
13142 	ipllc = (ipllc_t *)mp1->b_rptr;
13143 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13144 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13145 			break;
13146 	}
13147 	/*
13148 	 * prefer credential from mblk over ioctl;
13149 	 * see ip_sioctl_copyin_setup
13150 	 */
13151 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13152 
13153 	/*
13154 	 * Refhold the conn in case the request gets queued up in some lookup
13155 	 */
13156 	ASSERT(CONN_Q(q));
13157 	connp = Q_TO_CONN(q);
13158 	CONN_INC_REF(connp);
13159 	if (ipft->ipft_pfi &&
13160 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13161 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13162 		error = (*ipft->ipft_pfi)(q,
13163 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13164 	}
13165 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13166 		/*
13167 		 * CONN_OPER_PENDING_DONE happens in the function called
13168 		 * through ipft_pfi above.
13169 		 */
13170 		return;
13171 	}
13172 
13173 	CONN_OPER_PENDING_DONE(connp);
13174 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13175 		freemsg(mp);
13176 		return;
13177 	}
13178 	iocp->ioc_error = error;
13179 
13180 done:
13181 	mp->b_datap->db_type = M_IOCACK;
13182 	if (iocp->ioc_error)
13183 		iocp->ioc_count = 0;
13184 	qreply(q, mp);
13185 }
13186 
13187 /*
13188  * Lookup an ipif using the sequence id (ipif_seqid)
13189  */
13190 ipif_t *
13191 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13192 {
13193 	ipif_t *ipif;
13194 
13195 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13196 
13197 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13198 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13199 			return (ipif);
13200 	}
13201 	return (NULL);
13202 }
13203 
13204 /*
13205  * Assign a unique id for the ipif. This is used later when we send
13206  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13207  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13208  * IRE is added, we verify that ipif has not disappeared.
13209  */
13210 
13211 static void
13212 ipif_assign_seqid(ipif_t *ipif)
13213 {
13214 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13215 
13216 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13217 }
13218 
13219 /*
13220  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13221  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13222  * be inserted into the first space available in the list. The value of
13223  * ipif_id will then be set to the appropriate value for its position.
13224  */
13225 static int
13226 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13227 {
13228 	ill_t *ill;
13229 	ipif_t *tipif;
13230 	ipif_t **tipifp;
13231 	int id;
13232 	ip_stack_t	*ipst;
13233 
13234 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13235 	    IAM_WRITER_IPIF(ipif));
13236 
13237 	ill = ipif->ipif_ill;
13238 	ASSERT(ill != NULL);
13239 	ipst = ill->ill_ipst;
13240 
13241 	/*
13242 	 * In the case of lo0:0 we already hold the ill_g_lock.
13243 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13244 	 * ipif_insert. Another such caller is ipif_move.
13245 	 */
13246 	if (acquire_g_lock)
13247 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13248 	if (acquire_ill_lock)
13249 		mutex_enter(&ill->ill_lock);
13250 	id = ipif->ipif_id;
13251 	tipifp = &(ill->ill_ipif);
13252 	if (id == -1) {	/* need to find a real id */
13253 		id = 0;
13254 		while ((tipif = *tipifp) != NULL) {
13255 			ASSERT(tipif->ipif_id >= id);
13256 			if (tipif->ipif_id != id)
13257 				break; /* non-consecutive id */
13258 			id++;
13259 			tipifp = &(tipif->ipif_next);
13260 		}
13261 		/* limit number of logical interfaces */
13262 		if (id >= ipst->ips_ip_addrs_per_if) {
13263 			if (acquire_ill_lock)
13264 				mutex_exit(&ill->ill_lock);
13265 			if (acquire_g_lock)
13266 				rw_exit(&ipst->ips_ill_g_lock);
13267 			return (-1);
13268 		}
13269 		ipif->ipif_id = id; /* assign new id */
13270 	} else if (id < ipst->ips_ip_addrs_per_if) {
13271 		/* we have a real id; insert ipif in the right place */
13272 		while ((tipif = *tipifp) != NULL) {
13273 			ASSERT(tipif->ipif_id != id);
13274 			if (tipif->ipif_id > id)
13275 				break; /* found correct location */
13276 			tipifp = &(tipif->ipif_next);
13277 		}
13278 	} else {
13279 		if (acquire_ill_lock)
13280 			mutex_exit(&ill->ill_lock);
13281 		if (acquire_g_lock)
13282 			rw_exit(&ipst->ips_ill_g_lock);
13283 		return (-1);
13284 	}
13285 
13286 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13287 
13288 	ipif->ipif_next = tipif;
13289 	*tipifp = ipif;
13290 	if (acquire_ill_lock)
13291 		mutex_exit(&ill->ill_lock);
13292 	if (acquire_g_lock)
13293 		rw_exit(&ipst->ips_ill_g_lock);
13294 	return (0);
13295 }
13296 
13297 static void
13298 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13299 {
13300 	ipif_t	**ipifp;
13301 	ill_t	*ill = ipif->ipif_ill;
13302 
13303 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13304 	if (acquire_ill_lock)
13305 		mutex_enter(&ill->ill_lock);
13306 	else
13307 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13308 
13309 	ipifp = &ill->ill_ipif;
13310 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13311 		if (*ipifp == ipif) {
13312 			*ipifp = ipif->ipif_next;
13313 			break;
13314 		}
13315 	}
13316 
13317 	if (acquire_ill_lock)
13318 		mutex_exit(&ill->ill_lock);
13319 }
13320 
13321 /*
13322  * Allocate and initialize a new interface control structure.  (Always
13323  * called as writer.)
13324  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13325  * is not part of the global linked list of ills. ipif_seqid is unique
13326  * in the system and to preserve the uniqueness, it is assigned only
13327  * when ill becomes part of the global list. At that point ill will
13328  * have a name. If it doesn't get assigned here, it will get assigned
13329  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13330  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13331  * the interface flags or any other information from the DL_INFO_ACK for
13332  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13333  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13334  * second DL_INFO_ACK comes in from the driver.
13335  */
13336 static ipif_t *
13337 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13338 {
13339 	ipif_t	*ipif;
13340 	phyint_t *phyi;
13341 
13342 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13343 	    ill->ill_name, id, (void *)ill));
13344 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13345 
13346 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13347 		return (NULL);
13348 	*ipif = ipif_zero;	/* start clean */
13349 
13350 	ipif->ipif_ill = ill;
13351 	ipif->ipif_id = id;	/* could be -1 */
13352 	/*
13353 	 * Inherit the zoneid from the ill; for the shared stack instance
13354 	 * this is always the global zone
13355 	 */
13356 	ipif->ipif_zoneid = ill->ill_zoneid;
13357 
13358 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13359 
13360 	ipif->ipif_refcnt = 0;
13361 	ipif->ipif_saved_ire_cnt = 0;
13362 
13363 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13364 		mi_free(ipif);
13365 		return (NULL);
13366 	}
13367 	/* -1 id should have been replaced by real id */
13368 	id = ipif->ipif_id;
13369 	ASSERT(id >= 0);
13370 
13371 	if (ill->ill_name[0] != '\0')
13372 		ipif_assign_seqid(ipif);
13373 
13374 	/*
13375 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13376 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13377 	 * ioctl sets ipif_orig_ipifid to zero.
13378 	 */
13379 	ipif->ipif_orig_ipifid = id;
13380 
13381 	/*
13382 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13383 	 * The ipif is still not up and can't be looked up until the
13384 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13385 	 */
13386 	mutex_enter(&ill->ill_lock);
13387 	mutex_enter(&ill->ill_phyint->phyint_lock);
13388 	/*
13389 	 * Set the running flag when logical interface zero is created.
13390 	 * For subsequent logical interfaces, a DLPI link down
13391 	 * notification message may have cleared the running flag to
13392 	 * indicate the link is down, so we shouldn't just blindly set it.
13393 	 */
13394 	if (id == 0)
13395 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13396 	ipif->ipif_ire_type = ire_type;
13397 	phyi = ill->ill_phyint;
13398 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13399 
13400 	if (ipif->ipif_isv6) {
13401 		ill->ill_flags |= ILLF_IPV6;
13402 	} else {
13403 		ipaddr_t inaddr_any = INADDR_ANY;
13404 
13405 		ill->ill_flags |= ILLF_IPV4;
13406 
13407 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13408 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13409 		    &ipif->ipif_v6lcl_addr);
13410 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13411 		    &ipif->ipif_v6src_addr);
13412 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13413 		    &ipif->ipif_v6subnet);
13414 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13415 		    &ipif->ipif_v6net_mask);
13416 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13417 		    &ipif->ipif_v6brd_addr);
13418 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13419 		    &ipif->ipif_v6pp_dst_addr);
13420 	}
13421 
13422 	/*
13423 	 * Don't set the interface flags etc. now, will do it in
13424 	 * ip_ll_subnet_defaults.
13425 	 */
13426 	if (!initialize) {
13427 		mutex_exit(&ill->ill_lock);
13428 		mutex_exit(&ill->ill_phyint->phyint_lock);
13429 		return (ipif);
13430 	}
13431 	ipif->ipif_mtu = ill->ill_max_mtu;
13432 
13433 	if (ill->ill_bcast_addr_length != 0) {
13434 		/*
13435 		 * Later detect lack of DLPI driver multicast
13436 		 * capability by catching DL_ENABMULTI errors in
13437 		 * ip_rput_dlpi.
13438 		 */
13439 		ill->ill_flags |= ILLF_MULTICAST;
13440 		if (!ipif->ipif_isv6)
13441 			ipif->ipif_flags |= IPIF_BROADCAST;
13442 	} else {
13443 		if (ill->ill_net_type != IRE_LOOPBACK) {
13444 			if (ipif->ipif_isv6)
13445 				/*
13446 				 * Note: xresolv interfaces will eventually need
13447 				 * NOARP set here as well, but that will require
13448 				 * those external resolvers to have some
13449 				 * knowledge of that flag and act appropriately.
13450 				 * Not to be changed at present.
13451 				 */
13452 				ill->ill_flags |= ILLF_NONUD;
13453 			else
13454 				ill->ill_flags |= ILLF_NOARP;
13455 		}
13456 		if (ill->ill_phys_addr_length == 0) {
13457 			if (ill->ill_media &&
13458 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13459 				ipif->ipif_flags |= IPIF_NOXMIT;
13460 				phyi->phyint_flags |= PHYI_VIRTUAL;
13461 			} else {
13462 				/* pt-pt supports multicast. */
13463 				ill->ill_flags |= ILLF_MULTICAST;
13464 				if (ill->ill_net_type == IRE_LOOPBACK) {
13465 					phyi->phyint_flags |=
13466 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13467 				} else {
13468 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13469 				}
13470 			}
13471 		}
13472 	}
13473 	mutex_exit(&ill->ill_lock);
13474 	mutex_exit(&ill->ill_phyint->phyint_lock);
13475 	return (ipif);
13476 }
13477 
13478 /*
13479  * If appropriate, send a message up to the resolver delete the entry
13480  * for the address of this interface which is going out of business.
13481  * (Always called as writer).
13482  *
13483  * NOTE : We need to check for NULL mps as some of the fields are
13484  *	  initialized only for some interface types. See ipif_resolver_up()
13485  *	  for details.
13486  */
13487 void
13488 ipif_arp_down(ipif_t *ipif)
13489 {
13490 	mblk_t	*mp;
13491 	ill_t	*ill = ipif->ipif_ill;
13492 
13493 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13494 	ASSERT(IAM_WRITER_IPIF(ipif));
13495 
13496 	/* Delete the mapping for the local address */
13497 	mp = ipif->ipif_arp_del_mp;
13498 	if (mp != NULL) {
13499 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13500 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13501 		putnext(ill->ill_rq, mp);
13502 		ipif->ipif_arp_del_mp = NULL;
13503 	}
13504 
13505 	/*
13506 	 * If this is the last ipif that is going down and there are no
13507 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13508 	 * clean up ARP completely.
13509 	 */
13510 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13511 
13512 		/* Send up AR_INTERFACE_DOWN message */
13513 		mp = ill->ill_arp_down_mp;
13514 		if (mp != NULL) {
13515 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13516 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13517 			    ipif->ipif_id));
13518 			putnext(ill->ill_rq, mp);
13519 			ill->ill_arp_down_mp = NULL;
13520 		}
13521 
13522 		/* Tell ARP to delete the multicast mappings */
13523 		mp = ill->ill_arp_del_mapping_mp;
13524 		if (mp != NULL) {
13525 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13526 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13527 			    ipif->ipif_id));
13528 			putnext(ill->ill_rq, mp);
13529 			ill->ill_arp_del_mapping_mp = NULL;
13530 		}
13531 	}
13532 }
13533 
13534 /*
13535  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13536  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13537  * that it wants the add_mp allocated in this function to be returned
13538  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13539  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13540  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13541  * as it does a ipif_arp_down after calling this function - which will
13542  * remove what we add here.
13543  *
13544  * Returns -1 on failures and 0 on success.
13545  */
13546 int
13547 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13548 {
13549 	mblk_t	*del_mp = NULL;
13550 	mblk_t *add_mp = NULL;
13551 	mblk_t *mp;
13552 	ill_t	*ill = ipif->ipif_ill;
13553 	phyint_t *phyi = ill->ill_phyint;
13554 	ipaddr_t addr, mask, extract_mask = 0;
13555 	arma_t	*arma;
13556 	uint8_t *maddr, *bphys_addr;
13557 	uint32_t hw_start;
13558 	dl_unitdata_req_t *dlur;
13559 
13560 	ASSERT(IAM_WRITER_IPIF(ipif));
13561 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13562 		return (0);
13563 
13564 	/*
13565 	 * Delete the existing mapping from ARP. Normally ipif_down
13566 	 * -> ipif_arp_down should send this up to ARP. The only
13567 	 * reason we would find this when we are switching from
13568 	 * Multicast to Broadcast where we did not do a down.
13569 	 */
13570 	mp = ill->ill_arp_del_mapping_mp;
13571 	if (mp != NULL) {
13572 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13573 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13574 		putnext(ill->ill_rq, mp);
13575 		ill->ill_arp_del_mapping_mp = NULL;
13576 	}
13577 
13578 	if (arp_add_mapping_mp != NULL)
13579 		*arp_add_mapping_mp = NULL;
13580 
13581 	/*
13582 	 * Check that the address is not to long for the constant
13583 	 * length reserved in the template arma_t.
13584 	 */
13585 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13586 		return (-1);
13587 
13588 	/* Add mapping mblk */
13589 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13590 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13591 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13592 	    (caddr_t)&addr);
13593 	if (add_mp == NULL)
13594 		return (-1);
13595 	arma = (arma_t *)add_mp->b_rptr;
13596 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13597 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13598 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13599 
13600 	/*
13601 	 * Determine the broadcast address.
13602 	 */
13603 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13604 	if (ill->ill_sap_length < 0)
13605 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13606 	else
13607 		bphys_addr = (uchar_t *)dlur +
13608 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13609 	/*
13610 	 * Check PHYI_MULTI_BCAST and length of physical
13611 	 * address to determine if we use the mapping or the
13612 	 * broadcast address.
13613 	 */
13614 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13615 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13616 		    bphys_addr, maddr, &hw_start, &extract_mask))
13617 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13618 
13619 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13620 	    (ill->ill_flags & ILLF_MULTICAST)) {
13621 		/* Make sure this will not match the "exact" entry. */
13622 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13623 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13624 		    (caddr_t)&addr);
13625 		if (del_mp == NULL) {
13626 			freemsg(add_mp);
13627 			return (-1);
13628 		}
13629 		bcopy(&extract_mask, (char *)arma +
13630 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13631 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13632 			/* Use link-layer broadcast address for MULTI_BCAST */
13633 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13634 			ip2dbg(("ipif_arp_setup_multicast: adding"
13635 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13636 		} else {
13637 			arma->arma_hw_mapping_start = hw_start;
13638 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13639 			    " ARP setup for %s\n", ill->ill_name));
13640 		}
13641 	} else {
13642 		freemsg(add_mp);
13643 		ASSERT(del_mp == NULL);
13644 		/* It is neither MULTICAST nor MULTI_BCAST */
13645 		return (0);
13646 	}
13647 	ASSERT(add_mp != NULL && del_mp != NULL);
13648 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13649 	ill->ill_arp_del_mapping_mp = del_mp;
13650 	if (arp_add_mapping_mp != NULL) {
13651 		/* The caller just wants the mblks allocated */
13652 		*arp_add_mapping_mp = add_mp;
13653 	} else {
13654 		/* The caller wants us to send it to arp */
13655 		putnext(ill->ill_rq, add_mp);
13656 	}
13657 	return (0);
13658 }
13659 
13660 /*
13661  * Get the resolver set up for a new interface address.
13662  * (Always called as writer.)
13663  * Called both for IPv4 and IPv6 interfaces,
13664  * though it only sets up the resolver for v6
13665  * if it's an xresolv interface (one using an external resolver).
13666  * Honors ILLF_NOARP.
13667  * The enumerated value res_act is used to tune the behavior.
13668  * If set to Res_act_initial, then we set up all the resolver
13669  * structures for a new interface.  If set to Res_act_move, then
13670  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13671  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13672  * asynchronous hardware address change notification.  If set to
13673  * Res_act_defend, then we tell ARP that it needs to send a single
13674  * gratuitous message in defense of the address.
13675  * Returns error on failure.
13676  */
13677 int
13678 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13679 {
13680 	caddr_t	addr;
13681 	mblk_t	*arp_up_mp = NULL;
13682 	mblk_t	*arp_down_mp = NULL;
13683 	mblk_t	*arp_add_mp = NULL;
13684 	mblk_t	*arp_del_mp = NULL;
13685 	mblk_t	*arp_add_mapping_mp = NULL;
13686 	mblk_t	*arp_del_mapping_mp = NULL;
13687 	ill_t	*ill = ipif->ipif_ill;
13688 	uchar_t	*area_p = NULL;
13689 	uchar_t	*ared_p = NULL;
13690 	int	err = ENOMEM;
13691 	boolean_t was_dup;
13692 
13693 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13694 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13695 	ASSERT(IAM_WRITER_IPIF(ipif));
13696 
13697 	was_dup = B_FALSE;
13698 	if (res_act == Res_act_initial) {
13699 		ipif->ipif_addr_ready = 0;
13700 		/*
13701 		 * We're bringing an interface up here.  There's no way that we
13702 		 * should need to shut down ARP now.
13703 		 */
13704 		mutex_enter(&ill->ill_lock);
13705 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13706 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13707 			ill->ill_ipif_dup_count--;
13708 			was_dup = B_TRUE;
13709 		}
13710 		mutex_exit(&ill->ill_lock);
13711 	}
13712 	if (ipif->ipif_recovery_id != 0)
13713 		(void) untimeout(ipif->ipif_recovery_id);
13714 	ipif->ipif_recovery_id = 0;
13715 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13716 		ipif->ipif_addr_ready = 1;
13717 		return (0);
13718 	}
13719 	/* NDP will set the ipif_addr_ready flag when it's ready */
13720 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13721 		return (0);
13722 
13723 	if (ill->ill_isv6) {
13724 		/*
13725 		 * External resolver for IPv6
13726 		 */
13727 		ASSERT(res_act == Res_act_initial);
13728 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13729 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13730 			area_p = (uchar_t *)&ip6_area_template;
13731 			ared_p = (uchar_t *)&ip6_ared_template;
13732 		}
13733 	} else {
13734 		/*
13735 		 * IPv4 arp case. If the ARP stream has already started
13736 		 * closing, fail this request for ARP bringup. Else
13737 		 * record the fact that an ARP bringup is pending.
13738 		 */
13739 		mutex_enter(&ill->ill_lock);
13740 		if (ill->ill_arp_closing) {
13741 			mutex_exit(&ill->ill_lock);
13742 			err = EINVAL;
13743 			goto failed;
13744 		} else {
13745 			if (ill->ill_ipif_up_count == 0 &&
13746 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13747 				ill->ill_arp_bringup_pending = 1;
13748 			mutex_exit(&ill->ill_lock);
13749 		}
13750 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13751 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13752 			area_p = (uchar_t *)&ip_area_template;
13753 			ared_p = (uchar_t *)&ip_ared_template;
13754 		}
13755 	}
13756 
13757 	/*
13758 	 * Add an entry for the local address in ARP only if it
13759 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13760 	 */
13761 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13762 		area_t *area;
13763 
13764 		/* Now ask ARP to publish our address. */
13765 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13766 		if (arp_add_mp == NULL)
13767 			goto failed;
13768 		area = (area_t *)arp_add_mp->b_rptr;
13769 		if (res_act != Res_act_initial) {
13770 			/*
13771 			 * Copy the new hardware address and length into
13772 			 * arp_add_mp to be sent to ARP.
13773 			 */
13774 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13775 			bcopy(ill->ill_phys_addr,
13776 			    ((char *)area + area->area_hw_addr_offset),
13777 			    area->area_hw_addr_length);
13778 		}
13779 
13780 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13781 		    ACE_F_MYADDR;
13782 
13783 		if (res_act == Res_act_defend) {
13784 			area->area_flags |= ACE_F_DEFEND;
13785 			/*
13786 			 * If we're just defending our address now, then
13787 			 * there's no need to set up ARP multicast mappings.
13788 			 * The publish command is enough.
13789 			 */
13790 			goto done;
13791 		}
13792 
13793 		if (res_act != Res_act_initial)
13794 			goto arp_setup_multicast;
13795 
13796 		/*
13797 		 * Allocate an ARP deletion message so we know we can tell ARP
13798 		 * when the interface goes down.
13799 		 */
13800 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13801 		if (arp_del_mp == NULL)
13802 			goto failed;
13803 
13804 	} else {
13805 		if (res_act != Res_act_initial)
13806 			goto done;
13807 	}
13808 	/*
13809 	 * Need to bring up ARP or setup multicast mapping only
13810 	 * when the first interface is coming UP.
13811 	 */
13812 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13813 	    was_dup) {
13814 		goto done;
13815 	}
13816 
13817 	/*
13818 	 * Allocate an ARP down message (to be saved) and an ARP up
13819 	 * message.
13820 	 */
13821 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13822 	if (arp_down_mp == NULL)
13823 		goto failed;
13824 
13825 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13826 	if (arp_up_mp == NULL)
13827 		goto failed;
13828 
13829 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13830 		goto done;
13831 
13832 arp_setup_multicast:
13833 	/*
13834 	 * Setup the multicast mappings. This function initializes
13835 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13836 	 * IPv6.
13837 	 */
13838 	if (!ill->ill_isv6) {
13839 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13840 		if (err != 0)
13841 			goto failed;
13842 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13843 		ASSERT(arp_add_mapping_mp != NULL);
13844 	}
13845 
13846 done:
13847 	if (arp_del_mp != NULL) {
13848 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13849 		ipif->ipif_arp_del_mp = arp_del_mp;
13850 	}
13851 	if (arp_down_mp != NULL) {
13852 		ASSERT(ill->ill_arp_down_mp == NULL);
13853 		ill->ill_arp_down_mp = arp_down_mp;
13854 	}
13855 	if (arp_del_mapping_mp != NULL) {
13856 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13857 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13858 	}
13859 	if (arp_up_mp != NULL) {
13860 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13861 		    ill->ill_name, ipif->ipif_id));
13862 		putnext(ill->ill_rq, arp_up_mp);
13863 	}
13864 	if (arp_add_mp != NULL) {
13865 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13866 		    ill->ill_name, ipif->ipif_id));
13867 		/*
13868 		 * If it's an extended ARP implementation, then we'll wait to
13869 		 * hear that DAD has finished before using the interface.
13870 		 */
13871 		if (!ill->ill_arp_extend)
13872 			ipif->ipif_addr_ready = 1;
13873 		putnext(ill->ill_rq, arp_add_mp);
13874 	} else {
13875 		ipif->ipif_addr_ready = 1;
13876 	}
13877 	if (arp_add_mapping_mp != NULL) {
13878 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13879 		    ill->ill_name, ipif->ipif_id));
13880 		putnext(ill->ill_rq, arp_add_mapping_mp);
13881 	}
13882 	if (res_act != Res_act_initial)
13883 		return (0);
13884 
13885 	if (ill->ill_flags & ILLF_NOARP)
13886 		err = ill_arp_off(ill);
13887 	else
13888 		err = ill_arp_on(ill);
13889 	if (err != 0) {
13890 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13891 		freemsg(ipif->ipif_arp_del_mp);
13892 		freemsg(ill->ill_arp_down_mp);
13893 		freemsg(ill->ill_arp_del_mapping_mp);
13894 		ipif->ipif_arp_del_mp = NULL;
13895 		ill->ill_arp_down_mp = NULL;
13896 		ill->ill_arp_del_mapping_mp = NULL;
13897 		return (err);
13898 	}
13899 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13900 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13901 
13902 failed:
13903 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13904 	freemsg(arp_add_mp);
13905 	freemsg(arp_del_mp);
13906 	freemsg(arp_add_mapping_mp);
13907 	freemsg(arp_up_mp);
13908 	freemsg(arp_down_mp);
13909 	ill->ill_arp_bringup_pending = 0;
13910 	return (err);
13911 }
13912 
13913 /*
13914  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13915  * just gone back up.
13916  */
13917 static void
13918 ipif_arp_start_dad(ipif_t *ipif)
13919 {
13920 	ill_t *ill = ipif->ipif_ill;
13921 	mblk_t *arp_add_mp;
13922 	area_t *area;
13923 
13924 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13925 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13926 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13927 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13928 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13929 		/*
13930 		 * If we can't contact ARP for some reason, that's not really a
13931 		 * problem.  Just send out the routing socket notification that
13932 		 * DAD completion would have done, and continue.
13933 		 */
13934 		ipif_mask_reply(ipif);
13935 		ip_rts_ifmsg(ipif);
13936 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13937 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13938 		ipif->ipif_addr_ready = 1;
13939 		return;
13940 	}
13941 
13942 	/* Setting the 'unverified' flag restarts DAD */
13943 	area = (area_t *)arp_add_mp->b_rptr;
13944 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13945 	    ACE_F_UNVERIFIED;
13946 	putnext(ill->ill_rq, arp_add_mp);
13947 }
13948 
13949 static void
13950 ipif_ndp_start_dad(ipif_t *ipif)
13951 {
13952 	nce_t *nce;
13953 
13954 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13955 	if (nce == NULL)
13956 		return;
13957 
13958 	if (!ndp_restart_dad(nce)) {
13959 		/*
13960 		 * If we can't restart DAD for some reason, that's not really a
13961 		 * problem.  Just send out the routing socket notification that
13962 		 * DAD completion would have done, and continue.
13963 		 */
13964 		ip_rts_ifmsg(ipif);
13965 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13966 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13967 		ipif->ipif_addr_ready = 1;
13968 	}
13969 	NCE_REFRELE(nce);
13970 }
13971 
13972 /*
13973  * Restart duplicate address detection on all interfaces on the given ill.
13974  *
13975  * This is called when an interface transitions from down to up
13976  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13977  *
13978  * Note that since the underlying physical link has transitioned, we must cause
13979  * at least one routing socket message to be sent here, either via DAD
13980  * completion or just by default on the first ipif.  (If we don't do this, then
13981  * in.mpathd will see long delays when doing link-based failure recovery.)
13982  */
13983 void
13984 ill_restart_dad(ill_t *ill, boolean_t went_up)
13985 {
13986 	ipif_t *ipif;
13987 
13988 	if (ill == NULL)
13989 		return;
13990 
13991 	/*
13992 	 * If layer two doesn't support duplicate address detection, then just
13993 	 * send the routing socket message now and be done with it.
13994 	 */
13995 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13996 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13997 		ip_rts_ifmsg(ill->ill_ipif);
13998 		return;
13999 	}
14000 
14001 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14002 		if (went_up) {
14003 			if (ipif->ipif_flags & IPIF_UP) {
14004 				if (ill->ill_isv6)
14005 					ipif_ndp_start_dad(ipif);
14006 				else
14007 					ipif_arp_start_dad(ipif);
14008 			} else if (ill->ill_isv6 &&
14009 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14010 				/*
14011 				 * For IPv4, the ARP module itself will
14012 				 * automatically start the DAD process when it
14013 				 * sees DL_NOTE_LINK_UP.  We respond to the
14014 				 * AR_CN_READY at the completion of that task.
14015 				 * For IPv6, we must kick off the bring-up
14016 				 * process now.
14017 				 */
14018 				ndp_do_recovery(ipif);
14019 			} else {
14020 				/*
14021 				 * Unfortunately, the first ipif is "special"
14022 				 * and represents the underlying ill in the
14023 				 * routing socket messages.  Thus, when this
14024 				 * one ipif is down, we must still notify so
14025 				 * that the user knows the IFF_RUNNING status
14026 				 * change.  (If the first ipif is up, then
14027 				 * we'll handle eventual routing socket
14028 				 * notification via DAD completion.)
14029 				 */
14030 				if (ipif == ill->ill_ipif)
14031 					ip_rts_ifmsg(ill->ill_ipif);
14032 			}
14033 		} else {
14034 			/*
14035 			 * After link down, we'll need to send a new routing
14036 			 * message when the link comes back, so clear
14037 			 * ipif_addr_ready.
14038 			 */
14039 			ipif->ipif_addr_ready = 0;
14040 		}
14041 	}
14042 
14043 	/*
14044 	 * If we've torn down links, then notify the user right away.
14045 	 */
14046 	if (!went_up)
14047 		ip_rts_ifmsg(ill->ill_ipif);
14048 }
14049 
14050 /*
14051  * Wakeup all threads waiting to enter the ipsq, and sleeping
14052  * on any of the ills in this ipsq. The ill_lock of the ill
14053  * must be held so that waiters don't miss wakeups
14054  */
14055 static void
14056 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14057 {
14058 	phyint_t *phyint;
14059 
14060 	phyint = ipsq->ipsq_phyint_list;
14061 	while (phyint != NULL) {
14062 		if (phyint->phyint_illv4) {
14063 			if (!caller_holds_lock)
14064 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14065 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14066 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14067 			if (!caller_holds_lock)
14068 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14069 		}
14070 		if (phyint->phyint_illv6) {
14071 			if (!caller_holds_lock)
14072 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14073 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14074 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14075 			if (!caller_holds_lock)
14076 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14077 		}
14078 		phyint = phyint->phyint_ipsq_next;
14079 	}
14080 }
14081 
14082 static ipsq_t *
14083 ipsq_create(char *groupname, ip_stack_t *ipst)
14084 {
14085 	ipsq_t	*ipsq;
14086 
14087 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14088 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14089 	if (ipsq == NULL) {
14090 		return (NULL);
14091 	}
14092 
14093 	if (groupname != NULL)
14094 		(void) strcpy(ipsq->ipsq_name, groupname);
14095 	else
14096 		ipsq->ipsq_name[0] = '\0';
14097 
14098 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14099 	ipsq->ipsq_flags |= IPSQ_GROUP;
14100 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14101 	ipst->ips_ipsq_g_head = ipsq;
14102 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14103 	return (ipsq);
14104 }
14105 
14106 /*
14107  * Return an ipsq correspoding to the groupname. If 'create' is true
14108  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14109  * uniquely with an IPMP group. However during IPMP groupname operations,
14110  * multiple IPMP groups may be associated with a single ipsq. But no
14111  * IPMP group can be associated with more than 1 ipsq at any time.
14112  * For example
14113  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14114  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14115  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14116  *
14117  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14118  * status shown below during the execution of the above command.
14119  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14120  *
14121  * After the completion of the above groupname command we return to the stable
14122  * state shown below.
14123  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14124  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14125  *
14126  * Because of the above, we don't search based on the ipsq_name since that
14127  * would miss the correct ipsq during certain windows as shown above.
14128  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14129  * natural state.
14130  */
14131 static ipsq_t *
14132 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14133     ip_stack_t *ipst)
14134 {
14135 	ipsq_t	*ipsq;
14136 	int	group_len;
14137 	phyint_t *phyint;
14138 
14139 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14140 
14141 	group_len = strlen(groupname);
14142 	ASSERT(group_len != 0);
14143 	group_len++;
14144 
14145 	for (ipsq = ipst->ips_ipsq_g_head;
14146 	    ipsq != NULL;
14147 	    ipsq = ipsq->ipsq_next) {
14148 		/*
14149 		 * When an ipsq is being split, and ill_split_ipsq
14150 		 * calls this function, we exclude it from being considered.
14151 		 */
14152 		if (ipsq == exclude_ipsq)
14153 			continue;
14154 
14155 		/*
14156 		 * Compare against the ipsq_name. The groupname change happens
14157 		 * in 2 phases. The 1st phase merges the from group into
14158 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14159 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14160 		 * ipsq_name. At this point the phyint_groupname has not been
14161 		 * updated.
14162 		 */
14163 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14164 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14165 			/*
14166 			 * Verify that an ipmp groupname is exactly
14167 			 * part of 1 ipsq and is not found in any other
14168 			 * ipsq.
14169 			 */
14170 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14171 			    NULL);
14172 			return (ipsq);
14173 		}
14174 
14175 		/*
14176 		 * Comparison against ipsq_name alone is not sufficient.
14177 		 * In the case when groups are currently being
14178 		 * merged, the ipsq could hold other IPMP groups temporarily.
14179 		 * so we walk the phyint list and compare against the
14180 		 * phyint_groupname as well.
14181 		 */
14182 		phyint = ipsq->ipsq_phyint_list;
14183 		while (phyint != NULL) {
14184 			if ((group_len == phyint->phyint_groupname_len) &&
14185 			    (bcmp(phyint->phyint_groupname, groupname,
14186 			    group_len) == 0)) {
14187 				/*
14188 				 * Verify that an ipmp groupname is exactly
14189 				 * part of 1 ipsq and is not found in any other
14190 				 * ipsq.
14191 				 */
14192 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14193 				    ipst) == NULL);
14194 				return (ipsq);
14195 			}
14196 			phyint = phyint->phyint_ipsq_next;
14197 		}
14198 	}
14199 	if (create)
14200 		ipsq = ipsq_create(groupname, ipst);
14201 	return (ipsq);
14202 }
14203 
14204 static void
14205 ipsq_delete(ipsq_t *ipsq)
14206 {
14207 	ipsq_t *nipsq;
14208 	ipsq_t *pipsq = NULL;
14209 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14210 
14211 	/*
14212 	 * We don't hold the ipsq lock, but we are sure no new
14213 	 * messages can land up, since the ipsq_refs is zero.
14214 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14215 	 * is associated with this ipsq. (Lookups are based on ill_name
14216 	 * or phyint_groupname)
14217 	 */
14218 	ASSERT(ipsq->ipsq_refs == 0);
14219 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14220 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14221 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14222 		/*
14223 		 * This is not the ipsq of an IPMP group.
14224 		 */
14225 		ipsq->ipsq_ipst = NULL;
14226 		kmem_free(ipsq, sizeof (ipsq_t));
14227 		return;
14228 	}
14229 
14230 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14231 
14232 	/*
14233 	 * Locate the ipsq  before we can remove it from
14234 	 * the singly linked list of ipsq's.
14235 	 */
14236 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14237 	    nipsq = nipsq->ipsq_next) {
14238 		if (nipsq == ipsq) {
14239 			break;
14240 		}
14241 		pipsq = nipsq;
14242 	}
14243 
14244 	ASSERT(nipsq == ipsq);
14245 
14246 	/* unlink ipsq from the list */
14247 	if (pipsq != NULL)
14248 		pipsq->ipsq_next = ipsq->ipsq_next;
14249 	else
14250 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14251 	ipsq->ipsq_ipst = NULL;
14252 	kmem_free(ipsq, sizeof (ipsq_t));
14253 	rw_exit(&ipst->ips_ill_g_lock);
14254 }
14255 
14256 static void
14257 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14258     queue_t *q)
14259 {
14260 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14261 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14262 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14263 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14264 	ASSERT(current_mp != NULL);
14265 
14266 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14267 	    NEW_OP, NULL);
14268 
14269 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14270 	    new_ipsq->ipsq_xopq_mphead != NULL);
14271 
14272 	/*
14273 	 * move from old ipsq to the new ipsq.
14274 	 */
14275 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14276 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14277 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14278 
14279 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14280 }
14281 
14282 void
14283 ill_group_cleanup(ill_t *ill)
14284 {
14285 	ill_t *ill_v4;
14286 	ill_t *ill_v6;
14287 	ipif_t *ipif;
14288 
14289 	ill_v4 = ill->ill_phyint->phyint_illv4;
14290 	ill_v6 = ill->ill_phyint->phyint_illv6;
14291 
14292 	if (ill_v4 != NULL) {
14293 		mutex_enter(&ill_v4->ill_lock);
14294 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14295 		    ipif = ipif->ipif_next) {
14296 			IPIF_UNMARK_MOVING(ipif);
14297 		}
14298 		ill_v4->ill_up_ipifs = B_FALSE;
14299 		mutex_exit(&ill_v4->ill_lock);
14300 	}
14301 
14302 	if (ill_v6 != NULL) {
14303 		mutex_enter(&ill_v6->ill_lock);
14304 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14305 		    ipif = ipif->ipif_next) {
14306 			IPIF_UNMARK_MOVING(ipif);
14307 		}
14308 		ill_v6->ill_up_ipifs = B_FALSE;
14309 		mutex_exit(&ill_v6->ill_lock);
14310 	}
14311 }
14312 /*
14313  * This function is called when an ill has had a change in its group status
14314  * to bring up all the ipifs that were up before the change.
14315  */
14316 int
14317 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14318 {
14319 	ipif_t *ipif;
14320 	ill_t *ill_v4;
14321 	ill_t *ill_v6;
14322 	ill_t *from_ill;
14323 	int err = 0;
14324 
14325 
14326 	ASSERT(IAM_WRITER_ILL(ill));
14327 
14328 	/*
14329 	 * Except for ipif_state_flags and ill_state_flags the other
14330 	 * fields of the ipif/ill that are modified below are protected
14331 	 * implicitly since we are a writer. We would have tried to down
14332 	 * even an ipif that was already down, in ill_down_ipifs. So we
14333 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14334 	 */
14335 	ill_v4 = ill->ill_phyint->phyint_illv4;
14336 	ill_v6 = ill->ill_phyint->phyint_illv6;
14337 	if (ill_v4 != NULL) {
14338 		ill_v4->ill_up_ipifs = B_TRUE;
14339 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14340 		    ipif = ipif->ipif_next) {
14341 			mutex_enter(&ill_v4->ill_lock);
14342 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14343 			IPIF_UNMARK_MOVING(ipif);
14344 			mutex_exit(&ill_v4->ill_lock);
14345 			if (ipif->ipif_was_up) {
14346 				if (!(ipif->ipif_flags & IPIF_UP))
14347 					err = ipif_up(ipif, q, mp);
14348 				ipif->ipif_was_up = B_FALSE;
14349 				if (err != 0) {
14350 					/*
14351 					 * Can there be any other error ?
14352 					 */
14353 					ASSERT(err == EINPROGRESS);
14354 					return (err);
14355 				}
14356 			}
14357 		}
14358 		mutex_enter(&ill_v4->ill_lock);
14359 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14360 		mutex_exit(&ill_v4->ill_lock);
14361 		ill_v4->ill_up_ipifs = B_FALSE;
14362 		if (ill_v4->ill_move_in_progress) {
14363 			ASSERT(ill_v4->ill_move_peer != NULL);
14364 			ill_v4->ill_move_in_progress = B_FALSE;
14365 			from_ill = ill_v4->ill_move_peer;
14366 			from_ill->ill_move_in_progress = B_FALSE;
14367 			from_ill->ill_move_peer = NULL;
14368 			mutex_enter(&from_ill->ill_lock);
14369 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14370 			mutex_exit(&from_ill->ill_lock);
14371 			if (ill_v6 == NULL) {
14372 				if (from_ill->ill_phyint->phyint_flags &
14373 				    PHYI_STANDBY) {
14374 					phyint_inactive(from_ill->ill_phyint);
14375 				}
14376 				if (ill_v4->ill_phyint->phyint_flags &
14377 				    PHYI_STANDBY) {
14378 					phyint_inactive(ill_v4->ill_phyint);
14379 				}
14380 			}
14381 			ill_v4->ill_move_peer = NULL;
14382 		}
14383 	}
14384 
14385 	if (ill_v6 != NULL) {
14386 		ill_v6->ill_up_ipifs = B_TRUE;
14387 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14388 		    ipif = ipif->ipif_next) {
14389 			mutex_enter(&ill_v6->ill_lock);
14390 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14391 			IPIF_UNMARK_MOVING(ipif);
14392 			mutex_exit(&ill_v6->ill_lock);
14393 			if (ipif->ipif_was_up) {
14394 				if (!(ipif->ipif_flags & IPIF_UP))
14395 					err = ipif_up(ipif, q, mp);
14396 				ipif->ipif_was_up = B_FALSE;
14397 				if (err != 0) {
14398 					/*
14399 					 * Can there be any other error ?
14400 					 */
14401 					ASSERT(err == EINPROGRESS);
14402 					return (err);
14403 				}
14404 			}
14405 		}
14406 		mutex_enter(&ill_v6->ill_lock);
14407 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14408 		mutex_exit(&ill_v6->ill_lock);
14409 		ill_v6->ill_up_ipifs = B_FALSE;
14410 		if (ill_v6->ill_move_in_progress) {
14411 			ASSERT(ill_v6->ill_move_peer != NULL);
14412 			ill_v6->ill_move_in_progress = B_FALSE;
14413 			from_ill = ill_v6->ill_move_peer;
14414 			from_ill->ill_move_in_progress = B_FALSE;
14415 			from_ill->ill_move_peer = NULL;
14416 			mutex_enter(&from_ill->ill_lock);
14417 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14418 			mutex_exit(&from_ill->ill_lock);
14419 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14420 				phyint_inactive(from_ill->ill_phyint);
14421 			}
14422 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14423 				phyint_inactive(ill_v6->ill_phyint);
14424 			}
14425 			ill_v6->ill_move_peer = NULL;
14426 		}
14427 	}
14428 	return (0);
14429 }
14430 
14431 /*
14432  * bring down all the approriate ipifs.
14433  */
14434 /* ARGSUSED */
14435 static void
14436 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14437 {
14438 	ipif_t *ipif;
14439 
14440 	ASSERT(IAM_WRITER_ILL(ill));
14441 
14442 	/*
14443 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14444 	 * are modified below are protected implicitly since we are a writer
14445 	 */
14446 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14447 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14448 			continue;
14449 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14450 			/*
14451 			 * We go through the ipif_down logic even if the ipif
14452 			 * is already down, since routes can be added based
14453 			 * on down ipifs. Going through ipif_down once again
14454 			 * will delete any IREs created based on these routes.
14455 			 */
14456 			if (ipif->ipif_flags & IPIF_UP)
14457 				ipif->ipif_was_up = B_TRUE;
14458 			/*
14459 			 * If called with chk_nofailover true ipif is moving.
14460 			 */
14461 			mutex_enter(&ill->ill_lock);
14462 			if (chk_nofailover) {
14463 				ipif->ipif_state_flags |=
14464 				    IPIF_MOVING | IPIF_CHANGING;
14465 			} else {
14466 				ipif->ipif_state_flags |= IPIF_CHANGING;
14467 			}
14468 			mutex_exit(&ill->ill_lock);
14469 			/*
14470 			 * Need to re-create net/subnet bcast ires if
14471 			 * they are dependent on ipif.
14472 			 */
14473 			if (!ipif->ipif_isv6)
14474 				ipif_check_bcast_ires(ipif);
14475 			(void) ipif_logical_down(ipif, NULL, NULL);
14476 			ipif_non_duplicate(ipif);
14477 			ipif_down_tail(ipif);
14478 		}
14479 	}
14480 }
14481 
14482 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14483 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14484 	(ipsq)->ipsq_refs++;				\
14485 }
14486 
14487 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14488 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14489 	(ipsq)->ipsq_refs--;				\
14490 	if ((ipsq)->ipsq_refs == 0)				\
14491 		(ipsq)->ipsq_name[0] = '\0'; 		\
14492 }
14493 
14494 /*
14495  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14496  * new_ipsq.
14497  */
14498 static void
14499 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14500 {
14501 	phyint_t *phyint;
14502 	phyint_t *next_phyint;
14503 
14504 	/*
14505 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14506 	 * writer and the ill_lock of the ill in question. Also the dest
14507 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14508 	 */
14509 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14510 
14511 	phyint = cur_ipsq->ipsq_phyint_list;
14512 	cur_ipsq->ipsq_phyint_list = NULL;
14513 	while (phyint != NULL) {
14514 		next_phyint = phyint->phyint_ipsq_next;
14515 		IPSQ_DEC_REF(cur_ipsq, ipst);
14516 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14517 		new_ipsq->ipsq_phyint_list = phyint;
14518 		IPSQ_INC_REF(new_ipsq, ipst);
14519 		phyint->phyint_ipsq = new_ipsq;
14520 		phyint = next_phyint;
14521 	}
14522 }
14523 
14524 #define	SPLIT_SUCCESS		0
14525 #define	SPLIT_NOT_NEEDED	1
14526 #define	SPLIT_FAILED		2
14527 
14528 int
14529 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14530     ip_stack_t *ipst)
14531 {
14532 	ipsq_t *newipsq = NULL;
14533 
14534 	/*
14535 	 * Assertions denote pre-requisites for changing the ipsq of
14536 	 * a phyint
14537 	 */
14538 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14539 	/*
14540 	 * <ill-phyint> assocs can't change while ill_g_lock
14541 	 * is held as writer. See ill_phyint_reinit()
14542 	 */
14543 	ASSERT(phyint->phyint_illv4 == NULL ||
14544 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14545 	ASSERT(phyint->phyint_illv6 == NULL ||
14546 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14547 
14548 	if ((phyint->phyint_groupname_len !=
14549 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14550 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14551 	    phyint->phyint_groupname_len) != 0)) {
14552 		/*
14553 		 * Once we fail in creating a new ipsq due to memory shortage,
14554 		 * don't attempt to create new ipsq again, based on another
14555 		 * phyint, since we want all phyints belonging to an IPMP group
14556 		 * to be in the same ipsq even in the event of mem alloc fails.
14557 		 */
14558 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14559 		    cur_ipsq, ipst);
14560 		if (newipsq == NULL) {
14561 			/* Memory allocation failure */
14562 			return (SPLIT_FAILED);
14563 		} else {
14564 			/* ipsq_refs protected by ill_g_lock (writer) */
14565 			IPSQ_DEC_REF(cur_ipsq, ipst);
14566 			phyint->phyint_ipsq = newipsq;
14567 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14568 			newipsq->ipsq_phyint_list = phyint;
14569 			IPSQ_INC_REF(newipsq, ipst);
14570 			return (SPLIT_SUCCESS);
14571 		}
14572 	}
14573 	return (SPLIT_NOT_NEEDED);
14574 }
14575 
14576 /*
14577  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14578  * to do this split
14579  */
14580 static int
14581 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14582 {
14583 	ipsq_t *newipsq;
14584 
14585 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14586 	/*
14587 	 * <ill-phyint> assocs can't change while ill_g_lock
14588 	 * is held as writer. See ill_phyint_reinit()
14589 	 */
14590 
14591 	ASSERT(phyint->phyint_illv4 == NULL ||
14592 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14593 	ASSERT(phyint->phyint_illv6 == NULL ||
14594 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14595 
14596 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14597 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14598 		/*
14599 		 * ipsq_init failed due to no memory
14600 		 * caller will use the same ipsq
14601 		 */
14602 		return (SPLIT_FAILED);
14603 	}
14604 
14605 	/* ipsq_ref is protected by ill_g_lock (writer) */
14606 	IPSQ_DEC_REF(cur_ipsq, ipst);
14607 
14608 	/*
14609 	 * This is a new ipsq that is unknown to the world.
14610 	 * So we don't need to hold ipsq_lock,
14611 	 */
14612 	newipsq = phyint->phyint_ipsq;
14613 	newipsq->ipsq_writer = NULL;
14614 	newipsq->ipsq_reentry_cnt--;
14615 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14616 #ifdef DEBUG
14617 	newipsq->ipsq_depth = 0;
14618 #endif
14619 
14620 	return (SPLIT_SUCCESS);
14621 }
14622 
14623 /*
14624  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14625  * ipsq's representing their individual groups or themselves. Return
14626  * whether split needs to be retried again later.
14627  */
14628 static boolean_t
14629 ill_split_ipsq(ipsq_t *cur_ipsq)
14630 {
14631 	phyint_t *phyint;
14632 	phyint_t *next_phyint;
14633 	int	error;
14634 	boolean_t need_retry = B_FALSE;
14635 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14636 
14637 	phyint = cur_ipsq->ipsq_phyint_list;
14638 	cur_ipsq->ipsq_phyint_list = NULL;
14639 	while (phyint != NULL) {
14640 		next_phyint = phyint->phyint_ipsq_next;
14641 		/*
14642 		 * 'created' will tell us whether the callee actually
14643 		 * created an ipsq. Lack of memory may force the callee
14644 		 * to return without creating an ipsq.
14645 		 */
14646 		if (phyint->phyint_groupname == NULL) {
14647 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14648 		} else {
14649 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14650 			    need_retry, ipst);
14651 		}
14652 
14653 		switch (error) {
14654 		case SPLIT_FAILED:
14655 			need_retry = B_TRUE;
14656 			/* FALLTHRU */
14657 		case SPLIT_NOT_NEEDED:
14658 			/*
14659 			 * Keep it on the list.
14660 			 */
14661 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14662 			cur_ipsq->ipsq_phyint_list = phyint;
14663 			break;
14664 		case SPLIT_SUCCESS:
14665 			break;
14666 		default:
14667 			ASSERT(0);
14668 		}
14669 
14670 		phyint = next_phyint;
14671 	}
14672 	return (need_retry);
14673 }
14674 
14675 /*
14676  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14677  * and return the ills in the list. This list will be
14678  * needed to unlock all the ills later on by the caller.
14679  * The <ill-ipsq> associations could change between the
14680  * lock and unlock. Hence the unlock can't traverse the
14681  * ipsq to get the list of ills.
14682  */
14683 static int
14684 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14685 {
14686 	int	cnt = 0;
14687 	phyint_t	*phyint;
14688 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14689 
14690 	/*
14691 	 * The caller holds ill_g_lock to ensure that the ill memberships
14692 	 * of the ipsq don't change
14693 	 */
14694 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14695 
14696 	phyint = ipsq->ipsq_phyint_list;
14697 	while (phyint != NULL) {
14698 		if (phyint->phyint_illv4 != NULL) {
14699 			ASSERT(cnt < list_max);
14700 			list[cnt++] = phyint->phyint_illv4;
14701 		}
14702 		if (phyint->phyint_illv6 != NULL) {
14703 			ASSERT(cnt < list_max);
14704 			list[cnt++] = phyint->phyint_illv6;
14705 		}
14706 		phyint = phyint->phyint_ipsq_next;
14707 	}
14708 	ill_lock_ills(list, cnt);
14709 	return (cnt);
14710 }
14711 
14712 void
14713 ill_lock_ills(ill_t **list, int cnt)
14714 {
14715 	int	i;
14716 
14717 	if (cnt > 1) {
14718 		boolean_t try_again;
14719 		do {
14720 			try_again = B_FALSE;
14721 			for (i = 0; i < cnt - 1; i++) {
14722 				if (list[i] < list[i + 1]) {
14723 					ill_t	*tmp;
14724 
14725 					/* swap the elements */
14726 					tmp = list[i];
14727 					list[i] = list[i + 1];
14728 					list[i + 1] = tmp;
14729 					try_again = B_TRUE;
14730 				}
14731 			}
14732 		} while (try_again);
14733 	}
14734 
14735 	for (i = 0; i < cnt; i++) {
14736 		if (i == 0) {
14737 			if (list[i] != NULL)
14738 				mutex_enter(&list[i]->ill_lock);
14739 			else
14740 				return;
14741 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14742 			mutex_enter(&list[i]->ill_lock);
14743 		}
14744 	}
14745 }
14746 
14747 void
14748 ill_unlock_ills(ill_t **list, int cnt)
14749 {
14750 	int	i;
14751 
14752 	for (i = 0; i < cnt; i++) {
14753 		if ((i == 0) && (list[i] != NULL)) {
14754 			mutex_exit(&list[i]->ill_lock);
14755 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14756 			mutex_exit(&list[i]->ill_lock);
14757 		}
14758 	}
14759 }
14760 
14761 /*
14762  * Merge all the ills from 1 ipsq group into another ipsq group.
14763  * The source ipsq group is specified by the ipsq associated with
14764  * 'from_ill'. The destination ipsq group is specified by the ipsq
14765  * associated with 'to_ill' or 'groupname' respectively.
14766  * Note that ipsq itself does not have a reference count mechanism
14767  * and functions don't look up an ipsq and pass it around. Instead
14768  * functions pass around an ill or groupname, and the ipsq is looked
14769  * up from the ill or groupname and the required operation performed
14770  * atomically with the lookup on the ipsq.
14771  */
14772 static int
14773 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14774     queue_t *q)
14775 {
14776 	ipsq_t *old_ipsq;
14777 	ipsq_t *new_ipsq;
14778 	ill_t	**ill_list;
14779 	int	cnt;
14780 	size_t	ill_list_size;
14781 	boolean_t became_writer_on_new_sq = B_FALSE;
14782 	ip_stack_t	*ipst = from_ill->ill_ipst;
14783 
14784 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14785 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14786 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14787 
14788 	/*
14789 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14790 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14791 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14792 	 */
14793 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14794 
14795 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14796 	if (groupname != NULL)
14797 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14798 	else {
14799 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14800 	}
14801 
14802 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14803 
14804 	/*
14805 	 * both groups are on the same ipsq.
14806 	 */
14807 	if (old_ipsq == new_ipsq) {
14808 		rw_exit(&ipst->ips_ill_g_lock);
14809 		return (0);
14810 	}
14811 
14812 	cnt = old_ipsq->ipsq_refs << 1;
14813 	ill_list_size = cnt * sizeof (ill_t *);
14814 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14815 	if (ill_list == NULL) {
14816 		rw_exit(&ipst->ips_ill_g_lock);
14817 		return (ENOMEM);
14818 	}
14819 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14820 
14821 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14822 	mutex_enter(&new_ipsq->ipsq_lock);
14823 	if ((new_ipsq->ipsq_writer == NULL &&
14824 	    new_ipsq->ipsq_current_ipif == NULL) ||
14825 	    (new_ipsq->ipsq_writer == curthread)) {
14826 		new_ipsq->ipsq_writer = curthread;
14827 		new_ipsq->ipsq_reentry_cnt++;
14828 		became_writer_on_new_sq = B_TRUE;
14829 	}
14830 
14831 	/*
14832 	 * We are holding ill_g_lock as writer and all the ill locks of
14833 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14834 	 * message can land up on the old ipsq even though we don't hold the
14835 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14836 	 */
14837 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14838 
14839 	/*
14840 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14841 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14842 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14843 	 */
14844 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14845 
14846 	/*
14847 	 * Mark the new ipsq as needing a split since it is currently
14848 	 * being shared by more than 1 IPMP group. The split will
14849 	 * occur at the end of ipsq_exit
14850 	 */
14851 	new_ipsq->ipsq_split = B_TRUE;
14852 
14853 	/* Now release all the locks */
14854 	mutex_exit(&new_ipsq->ipsq_lock);
14855 	ill_unlock_ills(ill_list, cnt);
14856 	rw_exit(&ipst->ips_ill_g_lock);
14857 
14858 	kmem_free(ill_list, ill_list_size);
14859 
14860 	/*
14861 	 * If we succeeded in becoming writer on the new ipsq, then
14862 	 * drain the new ipsq and start processing  all enqueued messages
14863 	 * including the current ioctl we are processing which is either
14864 	 * a set groupname or failover/failback.
14865 	 */
14866 	if (became_writer_on_new_sq)
14867 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14868 
14869 	/*
14870 	 * syncq has been changed and all the messages have been moved.
14871 	 */
14872 	mutex_enter(&old_ipsq->ipsq_lock);
14873 	old_ipsq->ipsq_current_ipif = NULL;
14874 	old_ipsq->ipsq_current_ioctl = 0;
14875 	mutex_exit(&old_ipsq->ipsq_lock);
14876 	return (EINPROGRESS);
14877 }
14878 
14879 /*
14880  * Delete and add the loopback copy and non-loopback copy of
14881  * the BROADCAST ire corresponding to ill and addr. Used to
14882  * group broadcast ires together when ill becomes part of
14883  * a group.
14884  *
14885  * This function is also called when ill is leaving the group
14886  * so that the ires belonging to the group gets re-grouped.
14887  */
14888 static void
14889 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14890 {
14891 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14892 	ire_t **ire_ptpn = &ire_head;
14893 	ip_stack_t	*ipst = ill->ill_ipst;
14894 
14895 	/*
14896 	 * The loopback and non-loopback IREs are inserted in the order in which
14897 	 * they're found, on the basis that they are correctly ordered (loopback
14898 	 * first).
14899 	 */
14900 	for (;;) {
14901 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14902 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14903 		if (ire == NULL)
14904 			break;
14905 
14906 		/*
14907 		 * we are passing in KM_SLEEP because it is not easy to
14908 		 * go back to a sane state in case of memory failure.
14909 		 */
14910 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14911 		ASSERT(nire != NULL);
14912 		bzero(nire, sizeof (ire_t));
14913 		/*
14914 		 * Don't use ire_max_frag directly since we don't
14915 		 * hold on to 'ire' until we add the new ire 'nire' and
14916 		 * we don't want the new ire to have a dangling reference
14917 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14918 		 * be in sync with the ipif_mtu of the associate ipif.
14919 		 * For eg. this happens as a result of SIOCSLIFNAME,
14920 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14921 		 * the driver. A change in ire_max_frag triggered as
14922 		 * as a result of path mtu discovery, or due to an
14923 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14924 		 * route change -mtu command does not apply to broadcast ires.
14925 		 *
14926 		 * XXX We need a recovery strategy here if ire_init fails
14927 		 */
14928 		if (ire_init(nire,
14929 		    (uchar_t *)&ire->ire_addr,
14930 		    (uchar_t *)&ire->ire_mask,
14931 		    (uchar_t *)&ire->ire_src_addr,
14932 		    (uchar_t *)&ire->ire_gateway_addr,
14933 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14934 		    &ire->ire_ipif->ipif_mtu,
14935 		    ire->ire_nce,
14936 		    ire->ire_rfq,
14937 		    ire->ire_stq,
14938 		    ire->ire_type,
14939 		    ire->ire_ipif,
14940 		    ire->ire_cmask,
14941 		    ire->ire_phandle,
14942 		    ire->ire_ihandle,
14943 		    ire->ire_flags,
14944 		    &ire->ire_uinfo,
14945 		    NULL,
14946 		    NULL,
14947 		    ipst) == NULL) {
14948 			cmn_err(CE_PANIC, "ire_init() failed");
14949 		}
14950 		ire_delete(ire);
14951 		ire_refrele(ire);
14952 
14953 		/*
14954 		 * The newly created IREs are inserted at the tail of the list
14955 		 * starting with ire_head. As we've just allocated them no one
14956 		 * knows about them so it's safe.
14957 		 */
14958 		*ire_ptpn = nire;
14959 		ire_ptpn = &nire->ire_next;
14960 	}
14961 
14962 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14963 		int error;
14964 		ire_t *oire;
14965 		/* unlink the IRE from our list before calling ire_add() */
14966 		nire_next = nire->ire_next;
14967 		nire->ire_next = NULL;
14968 
14969 		/* ire_add adds the ire at the right place in the list */
14970 		oire = nire;
14971 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14972 		ASSERT(error == 0);
14973 		ASSERT(oire == nire);
14974 		ire_refrele(nire);	/* Held in ire_add */
14975 	}
14976 }
14977 
14978 /*
14979  * This function is usually called when an ill is inserted in
14980  * a group and all the ipifs are already UP. As all the ipifs
14981  * are already UP, the broadcast ires have already been created
14982  * and been inserted. But, ire_add_v4 would not have grouped properly.
14983  * We need to re-group for the benefit of ip_wput_ire which
14984  * expects BROADCAST ires to be grouped properly to avoid sending
14985  * more than one copy of the broadcast packet per group.
14986  *
14987  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14988  *	  because when ipif_up_done ends up calling this, ires have
14989  *        already been added before illgrp_insert i.e before ill_group
14990  *	  has been initialized.
14991  */
14992 static void
14993 ill_group_bcast_for_xmit(ill_t *ill)
14994 {
14995 	ill_group_t *illgrp;
14996 	ipif_t *ipif;
14997 	ipaddr_t addr;
14998 	ipaddr_t net_mask;
14999 	ipaddr_t subnet_netmask;
15000 
15001 	illgrp = ill->ill_group;
15002 
15003 	/*
15004 	 * This function is called even when an ill is deleted from
15005 	 * the group. Hence, illgrp could be null.
15006 	 */
15007 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15008 		return;
15009 
15010 	/*
15011 	 * Delete all the BROADCAST ires matching this ill and add
15012 	 * them back. This time, ire_add_v4 should take care of
15013 	 * grouping them with others because ill is part of the
15014 	 * group.
15015 	 */
15016 	ill_bcast_delete_and_add(ill, 0);
15017 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15018 
15019 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15020 
15021 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15022 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15023 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15024 		} else {
15025 			net_mask = htonl(IN_CLASSA_NET);
15026 		}
15027 		addr = net_mask & ipif->ipif_subnet;
15028 		ill_bcast_delete_and_add(ill, addr);
15029 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15030 
15031 		subnet_netmask = ipif->ipif_net_mask;
15032 		addr = ipif->ipif_subnet;
15033 		ill_bcast_delete_and_add(ill, addr);
15034 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15035 	}
15036 }
15037 
15038 /*
15039  * This function is called from illgrp_delete when ill is being deleted
15040  * from the group.
15041  *
15042  * As ill is not there in the group anymore, any address belonging
15043  * to this ill should be cleared of IRE_MARK_NORECV.
15044  */
15045 static void
15046 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15047 {
15048 	ire_t *ire;
15049 	irb_t *irb;
15050 	ip_stack_t	*ipst = ill->ill_ipst;
15051 
15052 	ASSERT(ill->ill_group == NULL);
15053 
15054 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15055 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15056 
15057 	if (ire != NULL) {
15058 		/*
15059 		 * IPMP and plumbing operations are serialized on the ipsq, so
15060 		 * no one will insert or delete a broadcast ire under our feet.
15061 		 */
15062 		irb = ire->ire_bucket;
15063 		rw_enter(&irb->irb_lock, RW_READER);
15064 		ire_refrele(ire);
15065 
15066 		for (; ire != NULL; ire = ire->ire_next) {
15067 			if (ire->ire_addr != addr)
15068 				break;
15069 			if (ire_to_ill(ire) != ill)
15070 				continue;
15071 
15072 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15073 			ire->ire_marks &= ~IRE_MARK_NORECV;
15074 		}
15075 		rw_exit(&irb->irb_lock);
15076 	}
15077 }
15078 
15079 /*
15080  * This function must be called only after the broadcast ires
15081  * have been grouped together. For a given address addr, nominate
15082  * only one of the ires whose interface is not FAILED or OFFLINE.
15083  *
15084  * This is also called when an ipif goes down, so that we can nominate
15085  * a different ire with the same address for receiving.
15086  */
15087 static void
15088 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15089 {
15090 	irb_t *irb;
15091 	ire_t *ire;
15092 	ire_t *ire1;
15093 	ire_t *save_ire;
15094 	ire_t **irep = NULL;
15095 	boolean_t first = B_TRUE;
15096 	ire_t *clear_ire = NULL;
15097 	ire_t *start_ire = NULL;
15098 	ire_t	*new_lb_ire;
15099 	ire_t	*new_nlb_ire;
15100 	boolean_t new_lb_ire_used = B_FALSE;
15101 	boolean_t new_nlb_ire_used = B_FALSE;
15102 	uint64_t match_flags;
15103 	uint64_t phyi_flags;
15104 	boolean_t fallback = B_FALSE;
15105 	uint_t	max_frag;
15106 
15107 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15108 	    NULL, MATCH_IRE_TYPE, ipst);
15109 	/*
15110 	 * We may not be able to find some ires if a previous
15111 	 * ire_create failed. This happens when an ipif goes
15112 	 * down and we are unable to create BROADCAST ires due
15113 	 * to memory failure. Thus, we have to check for NULL
15114 	 * below. This should handle the case for LOOPBACK,
15115 	 * POINTOPOINT and interfaces with some POINTOPOINT
15116 	 * logicals for which there are no BROADCAST ires.
15117 	 */
15118 	if (ire == NULL)
15119 		return;
15120 	/*
15121 	 * Currently IRE_BROADCASTS are deleted when an ipif
15122 	 * goes down which runs exclusively. Thus, setting
15123 	 * IRE_MARK_RCVD should not race with ire_delete marking
15124 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15125 	 * be consistent with other parts of the code that walks
15126 	 * a given bucket.
15127 	 */
15128 	save_ire = ire;
15129 	irb = ire->ire_bucket;
15130 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15131 	if (new_lb_ire == NULL) {
15132 		ire_refrele(ire);
15133 		return;
15134 	}
15135 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15136 	if (new_nlb_ire == NULL) {
15137 		ire_refrele(ire);
15138 		kmem_cache_free(ire_cache, new_lb_ire);
15139 		return;
15140 	}
15141 	IRB_REFHOLD(irb);
15142 	rw_enter(&irb->irb_lock, RW_WRITER);
15143 	/*
15144 	 * Get to the first ire matching the address and the
15145 	 * group. If the address does not match we are done
15146 	 * as we could not find the IRE. If the address matches
15147 	 * we should get to the first one matching the group.
15148 	 */
15149 	while (ire != NULL) {
15150 		if (ire->ire_addr != addr ||
15151 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15152 			break;
15153 		}
15154 		ire = ire->ire_next;
15155 	}
15156 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15157 	start_ire = ire;
15158 redo:
15159 	while (ire != NULL && ire->ire_addr == addr &&
15160 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15161 		/*
15162 		 * The first ire for any address within a group
15163 		 * should always be the one with IRE_MARK_NORECV cleared
15164 		 * so that ip_wput_ire can avoid searching for one.
15165 		 * Note down the insertion point which will be used
15166 		 * later.
15167 		 */
15168 		if (first && (irep == NULL))
15169 			irep = ire->ire_ptpn;
15170 		/*
15171 		 * PHYI_FAILED is set when the interface fails.
15172 		 * This interface might have become good, but the
15173 		 * daemon has not yet detected. We should still
15174 		 * not receive on this. PHYI_OFFLINE should never
15175 		 * be picked as this has been offlined and soon
15176 		 * be removed.
15177 		 */
15178 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15179 		if (phyi_flags & PHYI_OFFLINE) {
15180 			ire->ire_marks |= IRE_MARK_NORECV;
15181 			ire = ire->ire_next;
15182 			continue;
15183 		}
15184 		if (phyi_flags & match_flags) {
15185 			ire->ire_marks |= IRE_MARK_NORECV;
15186 			ire = ire->ire_next;
15187 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15188 			    PHYI_INACTIVE) {
15189 				fallback = B_TRUE;
15190 			}
15191 			continue;
15192 		}
15193 		if (first) {
15194 			/*
15195 			 * We will move this to the front of the list later
15196 			 * on.
15197 			 */
15198 			clear_ire = ire;
15199 			ire->ire_marks &= ~IRE_MARK_NORECV;
15200 		} else {
15201 			ire->ire_marks |= IRE_MARK_NORECV;
15202 		}
15203 		first = B_FALSE;
15204 		ire = ire->ire_next;
15205 	}
15206 	/*
15207 	 * If we never nominated anybody, try nominating at least
15208 	 * an INACTIVE, if we found one. Do it only once though.
15209 	 */
15210 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15211 	    fallback) {
15212 		match_flags = PHYI_FAILED;
15213 		ire = start_ire;
15214 		irep = NULL;
15215 		goto redo;
15216 	}
15217 	ire_refrele(save_ire);
15218 
15219 	/*
15220 	 * irep non-NULL indicates that we entered the while loop
15221 	 * above. If clear_ire is at the insertion point, we don't
15222 	 * have to do anything. clear_ire will be NULL if all the
15223 	 * interfaces are failed.
15224 	 *
15225 	 * We cannot unlink and reinsert the ire at the right place
15226 	 * in the list since there can be other walkers of this bucket.
15227 	 * Instead we delete and recreate the ire
15228 	 */
15229 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15230 		ire_t *clear_ire_stq = NULL;
15231 
15232 		bzero(new_lb_ire, sizeof (ire_t));
15233 		/* XXX We need a recovery strategy here. */
15234 		if (ire_init(new_lb_ire,
15235 		    (uchar_t *)&clear_ire->ire_addr,
15236 		    (uchar_t *)&clear_ire->ire_mask,
15237 		    (uchar_t *)&clear_ire->ire_src_addr,
15238 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15239 		    &clear_ire->ire_max_frag,
15240 		    NULL, /* let ire_nce_init derive the resolver info */
15241 		    clear_ire->ire_rfq,
15242 		    clear_ire->ire_stq,
15243 		    clear_ire->ire_type,
15244 		    clear_ire->ire_ipif,
15245 		    clear_ire->ire_cmask,
15246 		    clear_ire->ire_phandle,
15247 		    clear_ire->ire_ihandle,
15248 		    clear_ire->ire_flags,
15249 		    &clear_ire->ire_uinfo,
15250 		    NULL,
15251 		    NULL,
15252 		    ipst) == NULL)
15253 			cmn_err(CE_PANIC, "ire_init() failed");
15254 		if (clear_ire->ire_stq == NULL) {
15255 			ire_t *ire_next = clear_ire->ire_next;
15256 			if (ire_next != NULL &&
15257 			    ire_next->ire_stq != NULL &&
15258 			    ire_next->ire_addr == clear_ire->ire_addr &&
15259 			    ire_next->ire_ipif->ipif_ill ==
15260 			    clear_ire->ire_ipif->ipif_ill) {
15261 				clear_ire_stq = ire_next;
15262 
15263 				bzero(new_nlb_ire, sizeof (ire_t));
15264 				/* XXX We need a recovery strategy here. */
15265 				if (ire_init(new_nlb_ire,
15266 				    (uchar_t *)&clear_ire_stq->ire_addr,
15267 				    (uchar_t *)&clear_ire_stq->ire_mask,
15268 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15269 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15270 				    &clear_ire_stq->ire_max_frag,
15271 				    NULL,
15272 				    clear_ire_stq->ire_rfq,
15273 				    clear_ire_stq->ire_stq,
15274 				    clear_ire_stq->ire_type,
15275 				    clear_ire_stq->ire_ipif,
15276 				    clear_ire_stq->ire_cmask,
15277 				    clear_ire_stq->ire_phandle,
15278 				    clear_ire_stq->ire_ihandle,
15279 				    clear_ire_stq->ire_flags,
15280 				    &clear_ire_stq->ire_uinfo,
15281 				    NULL,
15282 				    NULL,
15283 				    ipst) == NULL)
15284 					cmn_err(CE_PANIC, "ire_init() failed");
15285 			}
15286 		}
15287 
15288 		/*
15289 		 * Delete the ire. We can't call ire_delete() since
15290 		 * we are holding the bucket lock. We can't release the
15291 		 * bucket lock since we can't allow irep to change. So just
15292 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15293 		 * ire from the list and do the refrele.
15294 		 */
15295 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15296 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15297 
15298 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15299 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15300 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15301 		}
15302 
15303 		/*
15304 		 * Also take care of otherfields like ib/ob pkt count
15305 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15306 		 */
15307 
15308 		/* Set the max_frag before adding the ire */
15309 		max_frag = *new_lb_ire->ire_max_fragp;
15310 		new_lb_ire->ire_max_fragp = NULL;
15311 		new_lb_ire->ire_max_frag = max_frag;
15312 
15313 		/* Add the new ire's. Insert at *irep */
15314 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15315 		ire1 = *irep;
15316 		if (ire1 != NULL)
15317 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15318 		new_lb_ire->ire_next = ire1;
15319 		/* Link the new one in. */
15320 		new_lb_ire->ire_ptpn = irep;
15321 		membar_producer();
15322 		*irep = new_lb_ire;
15323 		new_lb_ire_used = B_TRUE;
15324 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15325 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15326 		DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), new_lb_ire->ire_ipif,
15327 		    (char *), "ire", (void *), new_lb_ire);
15328 		new_lb_ire->ire_ipif->ipif_cnt_ire++;
15329 
15330 		if (clear_ire_stq != NULL) {
15331 			/* Set the max_frag before adding the ire */
15332 			max_frag = *new_nlb_ire->ire_max_fragp;
15333 			new_nlb_ire->ire_max_fragp = NULL;
15334 			new_nlb_ire->ire_max_frag = max_frag;
15335 
15336 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15337 			irep = &new_lb_ire->ire_next;
15338 			/* Add the new ire. Insert at *irep */
15339 			ire1 = *irep;
15340 			if (ire1 != NULL)
15341 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15342 			new_nlb_ire->ire_next = ire1;
15343 			/* Link the new one in. */
15344 			new_nlb_ire->ire_ptpn = irep;
15345 			membar_producer();
15346 			*irep = new_nlb_ire;
15347 			new_nlb_ire_used = B_TRUE;
15348 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15349 			    ire_stats_inserted);
15350 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15351 			DTRACE_PROBE3(ipif__incr__cnt,
15352 			    (ipif_t *), new_nlb_ire->ire_ipif,
15353 			    (char *), "ire", (void *), new_nlb_ire);
15354 			new_nlb_ire->ire_ipif->ipif_cnt_ire++;
15355 			DTRACE_PROBE3(ill__incr__cnt,
15356 			    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15357 			    (char *), "ire", (void *), new_nlb_ire);
15358 			((ill_t *)(new_nlb_ire->ire_stq->q_ptr))->ill_cnt_ire++;
15359 		}
15360 	}
15361 	rw_exit(&irb->irb_lock);
15362 	if (!new_lb_ire_used)
15363 		kmem_cache_free(ire_cache, new_lb_ire);
15364 	if (!new_nlb_ire_used)
15365 		kmem_cache_free(ire_cache, new_nlb_ire);
15366 	IRB_REFRELE(irb);
15367 }
15368 
15369 /*
15370  * Whenever an ipif goes down we have to renominate a different
15371  * broadcast ire to receive. Whenever an ipif comes up, we need
15372  * to make sure that we have only one nominated to receive.
15373  */
15374 static void
15375 ipif_renominate_bcast(ipif_t *ipif)
15376 {
15377 	ill_t *ill = ipif->ipif_ill;
15378 	ipaddr_t subnet_addr;
15379 	ipaddr_t net_addr;
15380 	ipaddr_t net_mask = 0;
15381 	ipaddr_t subnet_netmask;
15382 	ipaddr_t addr;
15383 	ill_group_t *illgrp;
15384 	ip_stack_t	*ipst = ill->ill_ipst;
15385 
15386 	illgrp = ill->ill_group;
15387 	/*
15388 	 * If this is the last ipif going down, it might take
15389 	 * the ill out of the group. In that case ipif_down ->
15390 	 * illgrp_delete takes care of doing the nomination.
15391 	 * ipif_down does not call for this case.
15392 	 */
15393 	ASSERT(illgrp != NULL);
15394 
15395 	/* There could not have been any ires associated with this */
15396 	if (ipif->ipif_subnet == 0)
15397 		return;
15398 
15399 	ill_mark_bcast(illgrp, 0, ipst);
15400 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15401 
15402 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15403 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15404 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15405 	} else {
15406 		net_mask = htonl(IN_CLASSA_NET);
15407 	}
15408 	addr = net_mask & ipif->ipif_subnet;
15409 	ill_mark_bcast(illgrp, addr, ipst);
15410 
15411 	net_addr = ~net_mask | addr;
15412 	ill_mark_bcast(illgrp, net_addr, ipst);
15413 
15414 	subnet_netmask = ipif->ipif_net_mask;
15415 	addr = ipif->ipif_subnet;
15416 	ill_mark_bcast(illgrp, addr, ipst);
15417 
15418 	subnet_addr = ~subnet_netmask | addr;
15419 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15420 }
15421 
15422 /*
15423  * Whenever we form or delete ill groups, we need to nominate one set of
15424  * BROADCAST ires for receiving in the group.
15425  *
15426  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15427  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15428  *    for ill_ipif_up_count to be non-zero. This is the only case where
15429  *    ill_ipif_up_count is zero and we would still find the ires.
15430  *
15431  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15432  *    ipif is UP and we just have to do the nomination.
15433  *
15434  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15435  *    from the group. So, we have to do the nomination.
15436  *
15437  * Because of (3), there could be just one ill in the group. But we have
15438  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15439  * Thus, this function does not optimize when there is only one ill as
15440  * it is not correct for (3).
15441  */
15442 static void
15443 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15444 {
15445 	ill_t *ill;
15446 	ipif_t *ipif;
15447 	ipaddr_t subnet_addr;
15448 	ipaddr_t prev_subnet_addr = 0;
15449 	ipaddr_t net_addr;
15450 	ipaddr_t prev_net_addr = 0;
15451 	ipaddr_t net_mask = 0;
15452 	ipaddr_t subnet_netmask;
15453 	ipaddr_t addr;
15454 	ip_stack_t	*ipst;
15455 
15456 	/*
15457 	 * When the last memeber is leaving, there is nothing to
15458 	 * nominate.
15459 	 */
15460 	if (illgrp->illgrp_ill_count == 0) {
15461 		ASSERT(illgrp->illgrp_ill == NULL);
15462 		return;
15463 	}
15464 
15465 	ill = illgrp->illgrp_ill;
15466 	ASSERT(!ill->ill_isv6);
15467 	ipst = ill->ill_ipst;
15468 	/*
15469 	 * We assume that ires with same address and belonging to the
15470 	 * same group, has been grouped together. Nominating a *single*
15471 	 * ill in the group for sending and receiving broadcast is done
15472 	 * by making sure that the first BROADCAST ire (which will be
15473 	 * the one returned by ire_ctable_lookup for ip_rput and the
15474 	 * one that will be used in ip_wput_ire) will be the one that
15475 	 * will not have IRE_MARK_NORECV set.
15476 	 *
15477 	 * 1) ip_rput checks and discards packets received on ires marked
15478 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15479 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15480 	 *    first ire in the group for every broadcast address in the group.
15481 	 *    ip_rput will accept packets only on the first ire i.e only
15482 	 *    one copy of the ill.
15483 	 *
15484 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15485 	 *    packet for the whole group. It needs to send out on the ill
15486 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15487 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15488 	 *    the copy echoed back on other port where the ire is not marked
15489 	 *    with IRE_MARK_NORECV.
15490 	 *
15491 	 * Note that we just need to have the first IRE either loopback or
15492 	 * non-loopback (either of them may not exist if ire_create failed
15493 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15494 	 * always hit the first one and hence will always accept one copy.
15495 	 *
15496 	 * We have a broadcast ire per ill for all the unique prefixes
15497 	 * hosted on that ill. As we don't have a way of knowing the
15498 	 * unique prefixes on a given ill and hence in the whole group,
15499 	 * we just call ill_mark_bcast on all the prefixes that exist
15500 	 * in the group. For the common case of one prefix, the code
15501 	 * below optimizes by remebering the last address used for
15502 	 * markng. In the case of multiple prefixes, this will still
15503 	 * optimize depending the order of prefixes.
15504 	 *
15505 	 * The only unique address across the whole group is 0.0.0.0 and
15506 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15507 	 * the first ire in the bucket for receiving and disables the
15508 	 * others.
15509 	 */
15510 	ill_mark_bcast(illgrp, 0, ipst);
15511 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15512 	for (; ill != NULL; ill = ill->ill_group_next) {
15513 
15514 		for (ipif = ill->ill_ipif; ipif != NULL;
15515 		    ipif = ipif->ipif_next) {
15516 
15517 			if (!(ipif->ipif_flags & IPIF_UP) ||
15518 			    ipif->ipif_subnet == 0) {
15519 				continue;
15520 			}
15521 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15522 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15523 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15524 			} else {
15525 				net_mask = htonl(IN_CLASSA_NET);
15526 			}
15527 			addr = net_mask & ipif->ipif_subnet;
15528 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15529 				ill_mark_bcast(illgrp, addr, ipst);
15530 				net_addr = ~net_mask | addr;
15531 				ill_mark_bcast(illgrp, net_addr, ipst);
15532 			}
15533 			prev_net_addr = addr;
15534 
15535 			subnet_netmask = ipif->ipif_net_mask;
15536 			addr = ipif->ipif_subnet;
15537 			if (prev_subnet_addr == 0 ||
15538 			    prev_subnet_addr != addr) {
15539 				ill_mark_bcast(illgrp, addr, ipst);
15540 				subnet_addr = ~subnet_netmask | addr;
15541 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15542 			}
15543 			prev_subnet_addr = addr;
15544 		}
15545 	}
15546 }
15547 
15548 /*
15549  * This function is called while forming ill groups.
15550  *
15551  * Currently, we handle only allmulti groups. We want to join
15552  * allmulti on only one of the ills in the groups. In future,
15553  * when we have link aggregation, we may have to join normal
15554  * multicast groups on multiple ills as switch does inbound load
15555  * balancing. Following are the functions that calls this
15556  * function :
15557  *
15558  * 1) ill_recover_multicast : Interface is coming back UP.
15559  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15560  *    will call ill_recover_multicast to recover all the multicast
15561  *    groups. We need to make sure that only one member is joined
15562  *    in the ill group.
15563  *
15564  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15565  *    Somebody is joining allmulti. We need to make sure that only one
15566  *    member is joined in the group.
15567  *
15568  * 3) illgrp_insert : If allmulti has already joined, we need to make
15569  *    sure that only one member is joined in the group.
15570  *
15571  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15572  *    allmulti who we have nominated. We need to pick someother ill.
15573  *
15574  * 5) illgrp_delete : The ill we nominated is leaving the group,
15575  *    we need to pick a new ill to join the group.
15576  *
15577  * For (1), (2), (5) - we just have to check whether there is
15578  * a good ill joined in the group. If we could not find any ills
15579  * joined the group, we should join.
15580  *
15581  * For (4), the one that was nominated to receive, left the group.
15582  * There could be nobody joined in the group when this function is
15583  * called.
15584  *
15585  * For (3) - we need to explicitly check whether there are multiple
15586  * ills joined in the group.
15587  *
15588  * For simplicity, we don't differentiate any of the above cases. We
15589  * just leave the group if it is joined on any of them and join on
15590  * the first good ill.
15591  */
15592 int
15593 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15594 {
15595 	ilm_t *ilm;
15596 	ill_t *ill;
15597 	ill_t *fallback_inactive_ill = NULL;
15598 	ill_t *fallback_failed_ill = NULL;
15599 	int ret = 0;
15600 
15601 	/*
15602 	 * Leave the allmulti on all the ills and start fresh.
15603 	 */
15604 	for (ill = illgrp->illgrp_ill; ill != NULL;
15605 	    ill = ill->ill_group_next) {
15606 		if (ill->ill_join_allmulti)
15607 			(void) ip_leave_allmulti(ill->ill_ipif);
15608 	}
15609 
15610 	/*
15611 	 * Choose a good ill. Fallback to inactive or failed if
15612 	 * none available. We need to fallback to FAILED in the
15613 	 * case where we have 2 interfaces in a group - where
15614 	 * one of them is failed and another is a good one and
15615 	 * the good one (not marked inactive) is leaving the group.
15616 	 */
15617 	ret = 0;
15618 	for (ill = illgrp->illgrp_ill; ill != NULL;
15619 	    ill = ill->ill_group_next) {
15620 		/* Never pick an offline interface */
15621 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15622 			continue;
15623 
15624 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15625 			fallback_failed_ill = ill;
15626 			continue;
15627 		}
15628 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15629 			fallback_inactive_ill = ill;
15630 			continue;
15631 		}
15632 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15633 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15634 				ret = ip_join_allmulti(ill->ill_ipif);
15635 				/*
15636 				 * ip_join_allmulti can fail because of memory
15637 				 * failures. So, make sure we join at least
15638 				 * on one ill.
15639 				 */
15640 				if (ill->ill_join_allmulti)
15641 					return (0);
15642 			}
15643 		}
15644 	}
15645 	if (ret != 0) {
15646 		/*
15647 		 * If we tried nominating above and failed to do so,
15648 		 * return error. We might have tried multiple times.
15649 		 * But, return the latest error.
15650 		 */
15651 		return (ret);
15652 	}
15653 	if ((ill = fallback_inactive_ill) != NULL) {
15654 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15655 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15656 				ret = ip_join_allmulti(ill->ill_ipif);
15657 				return (ret);
15658 			}
15659 		}
15660 	} else if ((ill = fallback_failed_ill) != NULL) {
15661 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15662 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15663 				ret = ip_join_allmulti(ill->ill_ipif);
15664 				return (ret);
15665 			}
15666 		}
15667 	}
15668 	return (0);
15669 }
15670 
15671 /*
15672  * This function is called from illgrp_delete after it is
15673  * deleted from the group to reschedule responsibilities
15674  * to a different ill.
15675  */
15676 static void
15677 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15678 {
15679 	ilm_t	*ilm;
15680 	ipif_t	*ipif;
15681 	ipaddr_t subnet_addr;
15682 	ipaddr_t net_addr;
15683 	ipaddr_t net_mask = 0;
15684 	ipaddr_t subnet_netmask;
15685 	ipaddr_t addr;
15686 	ip_stack_t *ipst = ill->ill_ipst;
15687 
15688 	ASSERT(ill->ill_group == NULL);
15689 	/*
15690 	 * Broadcast Responsibility:
15691 	 *
15692 	 * 1. If this ill has been nominated for receiving broadcast
15693 	 * packets, we need to find a new one. Before we find a new
15694 	 * one, we need to re-group the ires that are part of this new
15695 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15696 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15697 	 * thing for us.
15698 	 *
15699 	 * 2. If this ill was not nominated for receiving broadcast
15700 	 * packets, we need to clear the IRE_MARK_NORECV flag
15701 	 * so that we continue to send up broadcast packets.
15702 	 */
15703 	if (!ill->ill_isv6) {
15704 		/*
15705 		 * Case 1 above : No optimization here. Just redo the
15706 		 * nomination.
15707 		 */
15708 		ill_group_bcast_for_xmit(ill);
15709 		ill_nominate_bcast_rcv(illgrp);
15710 
15711 		/*
15712 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15713 		 */
15714 		ill_clear_bcast_mark(ill, 0);
15715 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15716 
15717 		for (ipif = ill->ill_ipif; ipif != NULL;
15718 		    ipif = ipif->ipif_next) {
15719 
15720 			if (!(ipif->ipif_flags & IPIF_UP) ||
15721 			    ipif->ipif_subnet == 0) {
15722 				continue;
15723 			}
15724 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15725 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15726 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15727 			} else {
15728 				net_mask = htonl(IN_CLASSA_NET);
15729 			}
15730 			addr = net_mask & ipif->ipif_subnet;
15731 			ill_clear_bcast_mark(ill, addr);
15732 
15733 			net_addr = ~net_mask | addr;
15734 			ill_clear_bcast_mark(ill, net_addr);
15735 
15736 			subnet_netmask = ipif->ipif_net_mask;
15737 			addr = ipif->ipif_subnet;
15738 			ill_clear_bcast_mark(ill, addr);
15739 
15740 			subnet_addr = ~subnet_netmask | addr;
15741 			ill_clear_bcast_mark(ill, subnet_addr);
15742 		}
15743 	}
15744 
15745 	/*
15746 	 * Multicast Responsibility.
15747 	 *
15748 	 * If we have joined allmulti on this one, find a new member
15749 	 * in the group to join allmulti. As this ill is already part
15750 	 * of allmulti, we don't have to join on this one.
15751 	 *
15752 	 * If we have not joined allmulti on this one, there is no
15753 	 * responsibility to handoff. But we need to take new
15754 	 * responsibility i.e, join allmulti on this one if we need
15755 	 * to.
15756 	 */
15757 	if (ill->ill_join_allmulti) {
15758 		(void) ill_nominate_mcast_rcv(illgrp);
15759 	} else {
15760 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15761 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15762 				(void) ip_join_allmulti(ill->ill_ipif);
15763 				break;
15764 			}
15765 		}
15766 	}
15767 
15768 	/*
15769 	 * We intentionally do the flushing of IRE_CACHES only matching
15770 	 * on the ill and not on groups. Note that we are already deleted
15771 	 * from the group.
15772 	 *
15773 	 * This will make sure that all IRE_CACHES whose stq is pointing
15774 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15775 	 * deleted and IRE_CACHES that are not pointing at this ill will
15776 	 * be left alone.
15777 	 */
15778 	if (ill->ill_isv6) {
15779 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15780 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15781 	} else {
15782 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15783 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15784 	}
15785 
15786 	/*
15787 	 * Some conn may have cached one of the IREs deleted above. By removing
15788 	 * the ire reference, we clean up the extra reference to the ill held in
15789 	 * ire->ire_stq.
15790 	 */
15791 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15792 
15793 	/*
15794 	 * Re-do source address selection for all the members in the
15795 	 * group, if they borrowed source address from one of the ipifs
15796 	 * in this ill.
15797 	 */
15798 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15799 		if (ill->ill_isv6) {
15800 			ipif_update_other_ipifs_v6(ipif, illgrp);
15801 		} else {
15802 			ipif_update_other_ipifs(ipif, illgrp);
15803 		}
15804 	}
15805 }
15806 
15807 /*
15808  * Delete the ill from the group. The caller makes sure that it is
15809  * in a group and it okay to delete from the group. So, we always
15810  * delete here.
15811  */
15812 static void
15813 illgrp_delete(ill_t *ill)
15814 {
15815 	ill_group_t *illgrp;
15816 	ill_group_t *tmpg;
15817 	ill_t *tmp_ill;
15818 	ip_stack_t	*ipst = ill->ill_ipst;
15819 
15820 	/*
15821 	 * Reset illgrp_ill_schednext if it was pointing at us.
15822 	 * We need to do this before we set ill_group to NULL.
15823 	 */
15824 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15825 	mutex_enter(&ill->ill_lock);
15826 
15827 	illgrp_reset_schednext(ill);
15828 
15829 	illgrp = ill->ill_group;
15830 
15831 	/* Delete the ill from illgrp. */
15832 	if (illgrp->illgrp_ill == ill) {
15833 		illgrp->illgrp_ill = ill->ill_group_next;
15834 	} else {
15835 		tmp_ill = illgrp->illgrp_ill;
15836 		while (tmp_ill->ill_group_next != ill) {
15837 			tmp_ill = tmp_ill->ill_group_next;
15838 			ASSERT(tmp_ill != NULL);
15839 		}
15840 		tmp_ill->ill_group_next = ill->ill_group_next;
15841 	}
15842 	ill->ill_group = NULL;
15843 	ill->ill_group_next = NULL;
15844 
15845 	illgrp->illgrp_ill_count--;
15846 	mutex_exit(&ill->ill_lock);
15847 	rw_exit(&ipst->ips_ill_g_lock);
15848 
15849 	/*
15850 	 * As this ill is leaving the group, we need to hand off
15851 	 * the responsibilities to the other ills in the group, if
15852 	 * this ill had some responsibilities.
15853 	 */
15854 
15855 	ill_handoff_responsibility(ill, illgrp);
15856 
15857 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15858 
15859 	if (illgrp->illgrp_ill_count == 0) {
15860 
15861 		ASSERT(illgrp->illgrp_ill == NULL);
15862 		if (ill->ill_isv6) {
15863 			if (illgrp == ipst->ips_illgrp_head_v6) {
15864 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15865 			} else {
15866 				tmpg = ipst->ips_illgrp_head_v6;
15867 				while (tmpg->illgrp_next != illgrp) {
15868 					tmpg = tmpg->illgrp_next;
15869 					ASSERT(tmpg != NULL);
15870 				}
15871 				tmpg->illgrp_next = illgrp->illgrp_next;
15872 			}
15873 		} else {
15874 			if (illgrp == ipst->ips_illgrp_head_v4) {
15875 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15876 			} else {
15877 				tmpg = ipst->ips_illgrp_head_v4;
15878 				while (tmpg->illgrp_next != illgrp) {
15879 					tmpg = tmpg->illgrp_next;
15880 					ASSERT(tmpg != NULL);
15881 				}
15882 				tmpg->illgrp_next = illgrp->illgrp_next;
15883 			}
15884 		}
15885 		mutex_destroy(&illgrp->illgrp_lock);
15886 		mi_free(illgrp);
15887 	}
15888 	rw_exit(&ipst->ips_ill_g_lock);
15889 
15890 	/*
15891 	 * Even though the ill is out of the group its not necessary
15892 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15893 	 * We will split the ipsq when phyint_groupname is set to NULL.
15894 	 */
15895 
15896 	/*
15897 	 * Send a routing sockets message if we are deleting from
15898 	 * groups with names.
15899 	 */
15900 	if (ill->ill_phyint->phyint_groupname_len != 0)
15901 		ip_rts_ifmsg(ill->ill_ipif);
15902 }
15903 
15904 /*
15905  * Re-do source address selection. This is normally called when
15906  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15907  * ipif comes up.
15908  */
15909 void
15910 ill_update_source_selection(ill_t *ill)
15911 {
15912 	ipif_t *ipif;
15913 
15914 	ASSERT(IAM_WRITER_ILL(ill));
15915 
15916 	if (ill->ill_group != NULL)
15917 		ill = ill->ill_group->illgrp_ill;
15918 
15919 	for (; ill != NULL; ill = ill->ill_group_next) {
15920 		for (ipif = ill->ill_ipif; ipif != NULL;
15921 		    ipif = ipif->ipif_next) {
15922 			if (ill->ill_isv6)
15923 				ipif_recreate_interface_routes_v6(NULL, ipif);
15924 			else
15925 				ipif_recreate_interface_routes(NULL, ipif);
15926 		}
15927 	}
15928 }
15929 
15930 /*
15931  * Insert ill in a group headed by illgrp_head. The caller can either
15932  * pass a groupname in which case we search for a group with the
15933  * same name to insert in or pass a group to insert in. This function
15934  * would only search groups with names.
15935  *
15936  * NOTE : The caller should make sure that there is at least one ipif
15937  *	  UP on this ill so that illgrp_scheduler can pick this ill
15938  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15939  *	  already sent a DL_UNBIND to the driver and we don't want to
15940  *	  send anymore packets. We don't assert for ipif_up_count
15941  *	  to be greater than zero, because ipif_up_done wants to call
15942  *	  this function before bumping up the ipif_up_count. See
15943  *	  ipif_up_done() for details.
15944  */
15945 int
15946 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15947     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15948 {
15949 	ill_group_t *illgrp;
15950 	ill_t *prev_ill;
15951 	phyint_t *phyi;
15952 	ip_stack_t	*ipst = ill->ill_ipst;
15953 
15954 	ASSERT(ill->ill_group == NULL);
15955 
15956 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15957 	mutex_enter(&ill->ill_lock);
15958 
15959 	if (groupname != NULL) {
15960 		/*
15961 		 * Look for a group with a matching groupname to insert.
15962 		 */
15963 		for (illgrp = *illgrp_head; illgrp != NULL;
15964 		    illgrp = illgrp->illgrp_next) {
15965 
15966 			ill_t *tmp_ill;
15967 
15968 			/*
15969 			 * If we have an ill_group_t in the list which has
15970 			 * no ill_t assigned then we must be in the process of
15971 			 * removing this group. We skip this as illgrp_delete()
15972 			 * will remove it from the list.
15973 			 */
15974 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15975 				ASSERT(illgrp->illgrp_ill_count == 0);
15976 				continue;
15977 			}
15978 
15979 			ASSERT(tmp_ill->ill_phyint != NULL);
15980 			phyi = tmp_ill->ill_phyint;
15981 			/*
15982 			 * Look at groups which has names only.
15983 			 */
15984 			if (phyi->phyint_groupname_len == 0)
15985 				continue;
15986 			/*
15987 			 * Names are stored in the phyint common to both
15988 			 * IPv4 and IPv6.
15989 			 */
15990 			if (mi_strcmp(phyi->phyint_groupname,
15991 			    groupname) == 0) {
15992 				break;
15993 			}
15994 		}
15995 	} else {
15996 		/*
15997 		 * If the caller passes in a NULL "grp_to_insert", we
15998 		 * allocate one below and insert this singleton.
15999 		 */
16000 		illgrp = grp_to_insert;
16001 	}
16002 
16003 	ill->ill_group_next = NULL;
16004 
16005 	if (illgrp == NULL) {
16006 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16007 		if (illgrp == NULL) {
16008 			return (ENOMEM);
16009 		}
16010 		illgrp->illgrp_next = *illgrp_head;
16011 		*illgrp_head = illgrp;
16012 		illgrp->illgrp_ill = ill;
16013 		illgrp->illgrp_ill_count = 1;
16014 		ill->ill_group = illgrp;
16015 		/*
16016 		 * Used in illgrp_scheduler to protect multiple threads
16017 		 * from traversing the list.
16018 		 */
16019 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16020 	} else {
16021 		ASSERT(ill->ill_net_type ==
16022 		    illgrp->illgrp_ill->ill_net_type);
16023 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16024 
16025 		/* Insert ill at tail of this group */
16026 		prev_ill = illgrp->illgrp_ill;
16027 		while (prev_ill->ill_group_next != NULL)
16028 			prev_ill = prev_ill->ill_group_next;
16029 		prev_ill->ill_group_next = ill;
16030 		ill->ill_group = illgrp;
16031 		illgrp->illgrp_ill_count++;
16032 		/*
16033 		 * Inherit group properties. Currently only forwarding
16034 		 * is the property we try to keep the same with all the
16035 		 * ills. When there are more, we will abstract this into
16036 		 * a function.
16037 		 */
16038 		ill->ill_flags &= ~ILLF_ROUTER;
16039 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16040 	}
16041 	mutex_exit(&ill->ill_lock);
16042 	rw_exit(&ipst->ips_ill_g_lock);
16043 
16044 	/*
16045 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16046 	 *    may be zero as it has not yet been bumped. But the ires
16047 	 *    have already been added. So, we do the nomination here
16048 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16049 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16050 	 *    ill_ipif_up_count here while nominating broadcast ires for
16051 	 *    receive.
16052 	 *
16053 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16054 	 *    to group them properly as ire_add() has already happened
16055 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16056 	 *    case, we need to do it here anyway.
16057 	 */
16058 	if (!ill->ill_isv6) {
16059 		ill_group_bcast_for_xmit(ill);
16060 		ill_nominate_bcast_rcv(illgrp);
16061 	}
16062 
16063 	if (!ipif_is_coming_up) {
16064 		/*
16065 		 * When ipif_up_done() calls this function, the multicast
16066 		 * groups have not been joined yet. So, there is no point in
16067 		 * nomination. ip_join_allmulti will handle groups when
16068 		 * ill_recover_multicast is called from ipif_up_done() later.
16069 		 */
16070 		(void) ill_nominate_mcast_rcv(illgrp);
16071 		/*
16072 		 * ipif_up_done calls ill_update_source_selection
16073 		 * anyway. Moreover, we don't want to re-create
16074 		 * interface routes while ipif_up_done() still has reference
16075 		 * to them. Refer to ipif_up_done() for more details.
16076 		 */
16077 		ill_update_source_selection(ill);
16078 	}
16079 
16080 	/*
16081 	 * Send a routing sockets message if we are inserting into
16082 	 * groups with names.
16083 	 */
16084 	if (groupname != NULL)
16085 		ip_rts_ifmsg(ill->ill_ipif);
16086 	return (0);
16087 }
16088 
16089 /*
16090  * Return the first phyint matching the groupname. There could
16091  * be more than one when there are ill groups.
16092  *
16093  * If 'usable' is set, then we exclude ones that are marked with any of
16094  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16095  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16096  * emulation of ipmp.
16097  */
16098 phyint_t *
16099 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16100 {
16101 	phyint_t *phyi;
16102 
16103 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16104 	/*
16105 	 * Group names are stored in the phyint - a common structure
16106 	 * to both IPv4 and IPv6.
16107 	 */
16108 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16109 	for (; phyi != NULL;
16110 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16111 	    phyi, AVL_AFTER)) {
16112 		if (phyi->phyint_groupname_len == 0)
16113 			continue;
16114 		/*
16115 		 * Skip the ones that should not be used since the callers
16116 		 * sometime use this for sending packets.
16117 		 */
16118 		if (usable && (phyi->phyint_flags &
16119 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16120 			continue;
16121 
16122 		ASSERT(phyi->phyint_groupname != NULL);
16123 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16124 			return (phyi);
16125 	}
16126 	return (NULL);
16127 }
16128 
16129 
16130 /*
16131  * Return the first usable phyint matching the group index. By 'usable'
16132  * we exclude ones that are marked ununsable with any of
16133  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16134  *
16135  * Used only for the ipmp/netinfo emulation of ipmp.
16136  */
16137 phyint_t *
16138 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16139 {
16140 	phyint_t *phyi;
16141 
16142 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16143 
16144 	if (!ipst->ips_ipmp_hook_emulation)
16145 		return (NULL);
16146 
16147 	/*
16148 	 * Group indicies are stored in the phyint - a common structure
16149 	 * to both IPv4 and IPv6.
16150 	 */
16151 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16152 	for (; phyi != NULL;
16153 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16154 	    phyi, AVL_AFTER)) {
16155 		/* Ignore the ones that do not have a group */
16156 		if (phyi->phyint_groupname_len == 0)
16157 			continue;
16158 
16159 		ASSERT(phyi->phyint_group_ifindex != 0);
16160 		/*
16161 		 * Skip the ones that should not be used since the callers
16162 		 * sometime use this for sending packets.
16163 		 */
16164 		if (phyi->phyint_flags &
16165 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16166 			continue;
16167 		if (phyi->phyint_group_ifindex == group_ifindex)
16168 			return (phyi);
16169 	}
16170 	return (NULL);
16171 }
16172 
16173 
16174 /*
16175  * MT notes on creation and deletion of IPMP groups
16176  *
16177  * Creation and deletion of IPMP groups introduce the need to merge or
16178  * split the associated serialization objects i.e the ipsq's. Normally all
16179  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16180  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16181  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16182  * is a need to change the <ill-ipsq> association and we have to operate on both
16183  * the source and destination IPMP groups. For eg. attempting to set the
16184  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16185  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16186  * source or destination IPMP group are mapped to a single ipsq for executing
16187  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16188  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16189  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16190  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16191  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16192  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16193  *
16194  * In the above example the ioctl handling code locates the current ipsq of hme0
16195  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16196  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16197  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16198  * the destination ipsq. If the destination ipsq is not busy, it also enters
16199  * the destination ipsq exclusively. Now the actual groupname setting operation
16200  * can proceed. If the destination ipsq is busy, the operation is enqueued
16201  * on the destination (merged) ipsq and will be handled in the unwind from
16202  * ipsq_exit.
16203  *
16204  * To prevent other threads accessing the ill while the group name change is
16205  * in progres, we bring down the ipifs which also removes the ill from the
16206  * group. The group is changed in phyint and when the first ipif on the ill
16207  * is brought up, the ill is inserted into the right IPMP group by
16208  * illgrp_insert.
16209  */
16210 /* ARGSUSED */
16211 int
16212 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16213     ip_ioctl_cmd_t *ipip, void *ifreq)
16214 {
16215 	int i;
16216 	char *tmp;
16217 	int namelen;
16218 	ill_t *ill = ipif->ipif_ill;
16219 	ill_t *ill_v4, *ill_v6;
16220 	int err = 0;
16221 	phyint_t *phyi;
16222 	phyint_t *phyi_tmp;
16223 	struct lifreq *lifr;
16224 	mblk_t	*mp1;
16225 	char *groupname;
16226 	ipsq_t *ipsq;
16227 	ip_stack_t	*ipst = ill->ill_ipst;
16228 
16229 	ASSERT(IAM_WRITER_IPIF(ipif));
16230 
16231 	/* Existance verified in ip_wput_nondata */
16232 	mp1 = mp->b_cont->b_cont;
16233 	lifr = (struct lifreq *)mp1->b_rptr;
16234 	groupname = lifr->lifr_groupname;
16235 
16236 	if (ipif->ipif_id != 0)
16237 		return (EINVAL);
16238 
16239 	phyi = ill->ill_phyint;
16240 	ASSERT(phyi != NULL);
16241 
16242 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16243 		return (EINVAL);
16244 
16245 	tmp = groupname;
16246 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16247 		;
16248 
16249 	if (i == LIFNAMSIZ) {
16250 		/* no null termination */
16251 		return (EINVAL);
16252 	}
16253 
16254 	/*
16255 	 * Calculate the namelen exclusive of the null
16256 	 * termination character.
16257 	 */
16258 	namelen = tmp - groupname;
16259 
16260 	ill_v4 = phyi->phyint_illv4;
16261 	ill_v6 = phyi->phyint_illv6;
16262 
16263 	/*
16264 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16265 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16266 	 * synchronization notes in ip.c
16267 	 */
16268 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16269 		return (EINVAL);
16270 	}
16271 
16272 	/*
16273 	 * mark the ill as changing.
16274 	 * this should queue all new requests on the syncq.
16275 	 */
16276 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16277 
16278 	if (ill_v4 != NULL)
16279 		ill_v4->ill_state_flags |= ILL_CHANGING;
16280 	if (ill_v6 != NULL)
16281 		ill_v6->ill_state_flags |= ILL_CHANGING;
16282 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16283 
16284 	if (namelen == 0) {
16285 		/*
16286 		 * Null string means remove this interface from the
16287 		 * existing group.
16288 		 */
16289 		if (phyi->phyint_groupname_len == 0) {
16290 			/*
16291 			 * Never was in a group.
16292 			 */
16293 			err = 0;
16294 			goto done;
16295 		}
16296 
16297 		/*
16298 		 * IPv4 or IPv6 may be temporarily out of the group when all
16299 		 * the ipifs are down. Thus, we need to check for ill_group to
16300 		 * be non-NULL.
16301 		 */
16302 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16303 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16304 			mutex_enter(&ill_v4->ill_lock);
16305 			if (!ill_is_quiescent(ill_v4)) {
16306 				/*
16307 				 * ipsq_pending_mp_add will not fail since
16308 				 * connp is NULL
16309 				 */
16310 				(void) ipsq_pending_mp_add(NULL,
16311 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16312 				mutex_exit(&ill_v4->ill_lock);
16313 				err = EINPROGRESS;
16314 				goto done;
16315 			}
16316 			mutex_exit(&ill_v4->ill_lock);
16317 		}
16318 
16319 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16320 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16321 			mutex_enter(&ill_v6->ill_lock);
16322 			if (!ill_is_quiescent(ill_v6)) {
16323 				(void) ipsq_pending_mp_add(NULL,
16324 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16325 				mutex_exit(&ill_v6->ill_lock);
16326 				err = EINPROGRESS;
16327 				goto done;
16328 			}
16329 			mutex_exit(&ill_v6->ill_lock);
16330 		}
16331 
16332 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16333 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16334 		mutex_enter(&phyi->phyint_lock);
16335 		ASSERT(phyi->phyint_groupname != NULL);
16336 		mi_free(phyi->phyint_groupname);
16337 		phyi->phyint_groupname = NULL;
16338 		phyi->phyint_groupname_len = 0;
16339 
16340 		/* Restore the ifindex used to be the per interface one */
16341 		phyi->phyint_group_ifindex = 0;
16342 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16343 		mutex_exit(&phyi->phyint_lock);
16344 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16345 		rw_exit(&ipst->ips_ill_g_lock);
16346 		err = ill_up_ipifs(ill, q, mp);
16347 
16348 		/*
16349 		 * set the split flag so that the ipsq can be split
16350 		 */
16351 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16352 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16353 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16354 
16355 	} else {
16356 		if (phyi->phyint_groupname_len != 0) {
16357 			ASSERT(phyi->phyint_groupname != NULL);
16358 			/* Are we inserting in the same group ? */
16359 			if (mi_strcmp(groupname,
16360 			    phyi->phyint_groupname) == 0) {
16361 				err = 0;
16362 				goto done;
16363 			}
16364 		}
16365 
16366 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16367 		/*
16368 		 * Merge ipsq for the group's.
16369 		 * This check is here as multiple groups/ills might be
16370 		 * sharing the same ipsq.
16371 		 * If we have to merege than the operation is restarted
16372 		 * on the new ipsq.
16373 		 */
16374 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16375 		if (phyi->phyint_ipsq != ipsq) {
16376 			rw_exit(&ipst->ips_ill_g_lock);
16377 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16378 			goto done;
16379 		}
16380 		/*
16381 		 * Running exclusive on new ipsq.
16382 		 */
16383 
16384 		ASSERT(ipsq != NULL);
16385 		ASSERT(ipsq->ipsq_writer == curthread);
16386 
16387 		/*
16388 		 * Check whether the ill_type and ill_net_type matches before
16389 		 * we allocate any memory so that the cleanup is easier.
16390 		 *
16391 		 * We can't group dissimilar ones as we can't load spread
16392 		 * packets across the group because of potential link-level
16393 		 * header differences.
16394 		 */
16395 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16396 		if (phyi_tmp != NULL) {
16397 			if ((ill_v4 != NULL &&
16398 			    phyi_tmp->phyint_illv4 != NULL) &&
16399 			    ((ill_v4->ill_net_type !=
16400 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16401 			    (ill_v4->ill_type !=
16402 			    phyi_tmp->phyint_illv4->ill_type))) {
16403 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16404 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16405 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16406 				rw_exit(&ipst->ips_ill_g_lock);
16407 				return (EINVAL);
16408 			}
16409 			if ((ill_v6 != NULL &&
16410 			    phyi_tmp->phyint_illv6 != NULL) &&
16411 			    ((ill_v6->ill_net_type !=
16412 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16413 			    (ill_v6->ill_type !=
16414 			    phyi_tmp->phyint_illv6->ill_type))) {
16415 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16416 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16417 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16418 				rw_exit(&ipst->ips_ill_g_lock);
16419 				return (EINVAL);
16420 			}
16421 		}
16422 
16423 		rw_exit(&ipst->ips_ill_g_lock);
16424 
16425 		/*
16426 		 * bring down all v4 ipifs.
16427 		 */
16428 		if (ill_v4 != NULL) {
16429 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16430 		}
16431 
16432 		/*
16433 		 * bring down all v6 ipifs.
16434 		 */
16435 		if (ill_v6 != NULL) {
16436 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16437 		}
16438 
16439 		/*
16440 		 * make sure all ipifs are down and there are no active
16441 		 * references. Call to ipsq_pending_mp_add will not fail
16442 		 * since connp is NULL.
16443 		 */
16444 		if (ill_v4 != NULL) {
16445 			mutex_enter(&ill_v4->ill_lock);
16446 			if (!ill_is_quiescent(ill_v4)) {
16447 				(void) ipsq_pending_mp_add(NULL,
16448 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16449 				mutex_exit(&ill_v4->ill_lock);
16450 				err = EINPROGRESS;
16451 				goto done;
16452 			}
16453 			mutex_exit(&ill_v4->ill_lock);
16454 		}
16455 
16456 		if (ill_v6 != NULL) {
16457 			mutex_enter(&ill_v6->ill_lock);
16458 			if (!ill_is_quiescent(ill_v6)) {
16459 				(void) ipsq_pending_mp_add(NULL,
16460 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16461 				mutex_exit(&ill_v6->ill_lock);
16462 				err = EINPROGRESS;
16463 				goto done;
16464 			}
16465 			mutex_exit(&ill_v6->ill_lock);
16466 		}
16467 
16468 		/*
16469 		 * allocate including space for null terminator
16470 		 * before we insert.
16471 		 */
16472 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16473 		if (tmp == NULL)
16474 			return (ENOMEM);
16475 
16476 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16477 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16478 		mutex_enter(&phyi->phyint_lock);
16479 		if (phyi->phyint_groupname_len != 0) {
16480 			ASSERT(phyi->phyint_groupname != NULL);
16481 			mi_free(phyi->phyint_groupname);
16482 		}
16483 
16484 		/*
16485 		 * setup the new group name.
16486 		 */
16487 		phyi->phyint_groupname = tmp;
16488 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16489 		phyi->phyint_groupname_len = namelen + 1;
16490 
16491 		if (ipst->ips_ipmp_hook_emulation) {
16492 			/*
16493 			 * If the group already exists we use the existing
16494 			 * group_ifindex, otherwise we pick a new index here.
16495 			 */
16496 			if (phyi_tmp != NULL) {
16497 				phyi->phyint_group_ifindex =
16498 				    phyi_tmp->phyint_group_ifindex;
16499 			} else {
16500 				/* XXX We need a recovery strategy here. */
16501 				if (!ip_assign_ifindex(
16502 				    &phyi->phyint_group_ifindex, ipst))
16503 					cmn_err(CE_PANIC,
16504 					    "ip_assign_ifindex() failed");
16505 			}
16506 		}
16507 		/*
16508 		 * Select whether the netinfo and hook use the per-interface
16509 		 * or per-group ifindex.
16510 		 */
16511 		if (ipst->ips_ipmp_hook_emulation)
16512 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16513 		else
16514 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16515 
16516 		if (ipst->ips_ipmp_hook_emulation &&
16517 		    phyi_tmp != NULL) {
16518 			/* First phyint in group - group PLUMB event */
16519 			ill_nic_info_plumb(ill, B_TRUE);
16520 		}
16521 		mutex_exit(&phyi->phyint_lock);
16522 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16523 		rw_exit(&ipst->ips_ill_g_lock);
16524 
16525 		err = ill_up_ipifs(ill, q, mp);
16526 	}
16527 
16528 done:
16529 	/*
16530 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16531 	 */
16532 	if (err != EINPROGRESS) {
16533 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16534 		if (ill_v4 != NULL)
16535 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16536 		if (ill_v6 != NULL)
16537 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16538 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16539 	}
16540 	return (err);
16541 }
16542 
16543 /* ARGSUSED */
16544 int
16545 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16546     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16547 {
16548 	ill_t *ill;
16549 	phyint_t *phyi;
16550 	struct lifreq *lifr;
16551 	mblk_t	*mp1;
16552 
16553 	/* Existence verified in ip_wput_nondata */
16554 	mp1 = mp->b_cont->b_cont;
16555 	lifr = (struct lifreq *)mp1->b_rptr;
16556 	ill = ipif->ipif_ill;
16557 	phyi = ill->ill_phyint;
16558 
16559 	lifr->lifr_groupname[0] = '\0';
16560 	/*
16561 	 * ill_group may be null if all the interfaces
16562 	 * are down. But still, the phyint should always
16563 	 * hold the name.
16564 	 */
16565 	if (phyi->phyint_groupname_len != 0) {
16566 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16567 		    phyi->phyint_groupname_len);
16568 	}
16569 
16570 	return (0);
16571 }
16572 
16573 
16574 typedef struct conn_move_s {
16575 	ill_t	*cm_from_ill;
16576 	ill_t	*cm_to_ill;
16577 	int	cm_ifindex;
16578 } conn_move_t;
16579 
16580 /*
16581  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16582  */
16583 static void
16584 conn_move(conn_t *connp, caddr_t arg)
16585 {
16586 	conn_move_t *connm;
16587 	int ifindex;
16588 	int i;
16589 	ill_t *from_ill;
16590 	ill_t *to_ill;
16591 	ilg_t *ilg;
16592 	ilm_t *ret_ilm;
16593 
16594 	connm = (conn_move_t *)arg;
16595 	ifindex = connm->cm_ifindex;
16596 	from_ill = connm->cm_from_ill;
16597 	to_ill = connm->cm_to_ill;
16598 
16599 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16600 
16601 	/* All multicast fields protected by conn_lock */
16602 	mutex_enter(&connp->conn_lock);
16603 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16604 	if ((connp->conn_outgoing_ill == from_ill) &&
16605 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16606 		connp->conn_outgoing_ill = to_ill;
16607 		connp->conn_incoming_ill = to_ill;
16608 	}
16609 
16610 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16611 
16612 	if ((connp->conn_multicast_ill == from_ill) &&
16613 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16614 		connp->conn_multicast_ill = connm->cm_to_ill;
16615 	}
16616 
16617 	/*
16618 	 * Change the ilg_ill to point to the new one. This assumes
16619 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16620 	 * has been told to receive packets on this interface.
16621 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16622 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16623 	 * some ilms may not have moved. We check to see whether
16624 	 * the ilms have moved to to_ill. We can't check on from_ill
16625 	 * as in the process of moving, we could have split an ilm
16626 	 * in to two - which has the same orig_ifindex and v6group.
16627 	 *
16628 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16629 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16630 	 */
16631 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16632 		ilg = &connp->conn_ilg[i];
16633 		if ((ilg->ilg_ill == from_ill) &&
16634 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16635 			/* ifindex != 0 indicates failback */
16636 			if (ifindex != 0) {
16637 				connp->conn_ilg[i].ilg_ill = to_ill;
16638 				continue;
16639 			}
16640 
16641 			mutex_enter(&to_ill->ill_lock);
16642 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16643 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16644 			    connp->conn_zoneid);
16645 			mutex_exit(&to_ill->ill_lock);
16646 
16647 			if (ret_ilm != NULL)
16648 				connp->conn_ilg[i].ilg_ill = to_ill;
16649 		}
16650 	}
16651 	mutex_exit(&connp->conn_lock);
16652 }
16653 
16654 static void
16655 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16656 {
16657 	conn_move_t connm;
16658 	ip_stack_t	*ipst = from_ill->ill_ipst;
16659 
16660 	connm.cm_from_ill = from_ill;
16661 	connm.cm_to_ill = to_ill;
16662 	connm.cm_ifindex = ifindex;
16663 
16664 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16665 }
16666 
16667 /*
16668  * ilm has been moved from from_ill to to_ill.
16669  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16670  * appropriately.
16671  *
16672  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16673  *	  the code there de-references ipif_ill to get the ill to
16674  *	  send multicast requests. It does not work as ipif is on its
16675  *	  move and already moved when this function is called.
16676  *	  Thus, we need to use from_ill and to_ill send down multicast
16677  *	  requests.
16678  */
16679 static void
16680 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16681 {
16682 	ipif_t *ipif;
16683 	ilm_t *ilm;
16684 
16685 	/*
16686 	 * See whether we need to send down DL_ENABMULTI_REQ on
16687 	 * to_ill as ilm has just been added.
16688 	 */
16689 	ASSERT(IAM_WRITER_ILL(to_ill));
16690 	ASSERT(IAM_WRITER_ILL(from_ill));
16691 
16692 	ILM_WALKER_HOLD(to_ill);
16693 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16694 
16695 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16696 			continue;
16697 		/*
16698 		 * no locks held, ill/ipif cannot dissappear as long
16699 		 * as we are writer.
16700 		 */
16701 		ipif = to_ill->ill_ipif;
16702 		/*
16703 		 * No need to hold any lock as we are the writer and this
16704 		 * can only be changed by a writer.
16705 		 */
16706 		ilm->ilm_is_new = B_FALSE;
16707 
16708 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16709 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16710 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16711 			    "resolver\n"));
16712 			continue;		/* Must be IRE_IF_NORESOLVER */
16713 		}
16714 
16715 
16716 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16717 			ip1dbg(("ilm_send_multicast_reqs: "
16718 			    "to_ill MULTI_BCAST\n"));
16719 			goto from;
16720 		}
16721 
16722 		if (to_ill->ill_isv6)
16723 			mld_joingroup(ilm);
16724 		else
16725 			igmp_joingroup(ilm);
16726 
16727 		if (to_ill->ill_ipif_up_count == 0) {
16728 			/*
16729 			 * Nobody there. All multicast addresses will be
16730 			 * re-joined when we get the DL_BIND_ACK bringing the
16731 			 * interface up.
16732 			 */
16733 			ilm->ilm_notify_driver = B_FALSE;
16734 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16735 			goto from;
16736 		}
16737 
16738 		/*
16739 		 * For allmulti address, we want to join on only one interface.
16740 		 * Checking for ilm_numentries_v6 is not correct as you may
16741 		 * find an ilm with zero address on to_ill, but we may not
16742 		 * have nominated to_ill for receiving. Thus, if we have
16743 		 * nominated from_ill (ill_join_allmulti is set), nominate
16744 		 * only if to_ill is not already nominated (to_ill normally
16745 		 * should not have been nominated if "from_ill" has already
16746 		 * been nominated. As we don't prevent failovers from happening
16747 		 * across groups, we don't assert).
16748 		 */
16749 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16750 			/*
16751 			 * There is no need to hold ill locks as we are
16752 			 * writer on both ills and when ill_join_allmulti
16753 			 * is changed the thread is always a writer.
16754 			 */
16755 			if (from_ill->ill_join_allmulti &&
16756 			    !to_ill->ill_join_allmulti) {
16757 				(void) ip_join_allmulti(to_ill->ill_ipif);
16758 			}
16759 		} else if (ilm->ilm_notify_driver) {
16760 
16761 			/*
16762 			 * This is a newly moved ilm so we need to tell the
16763 			 * driver about the new group. There can be more than
16764 			 * one ilm's for the same group in the list each with a
16765 			 * different orig_ifindex. We have to inform the driver
16766 			 * once. In ilm_move_v[4,6] we only set the flag
16767 			 * ilm_notify_driver for the first ilm.
16768 			 */
16769 
16770 			(void) ip_ll_send_enabmulti_req(to_ill,
16771 			    &ilm->ilm_v6addr);
16772 		}
16773 
16774 		ilm->ilm_notify_driver = B_FALSE;
16775 
16776 		/*
16777 		 * See whether we need to send down DL_DISABMULTI_REQ on
16778 		 * from_ill as ilm has just been removed.
16779 		 */
16780 from:
16781 		ipif = from_ill->ill_ipif;
16782 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16783 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16784 			ip1dbg(("ilm_send_multicast_reqs: "
16785 			    "from_ill not resolver\n"));
16786 			continue;		/* Must be IRE_IF_NORESOLVER */
16787 		}
16788 
16789 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16790 			ip1dbg(("ilm_send_multicast_reqs: "
16791 			    "from_ill MULTI_BCAST\n"));
16792 			continue;
16793 		}
16794 
16795 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16796 			if (from_ill->ill_join_allmulti)
16797 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16798 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16799 			(void) ip_ll_send_disabmulti_req(from_ill,
16800 			    &ilm->ilm_v6addr);
16801 		}
16802 	}
16803 	ILM_WALKER_RELE(to_ill);
16804 }
16805 
16806 /*
16807  * This function is called when all multicast memberships needs
16808  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16809  * called only once unlike the IPv4 counterpart where it is called after
16810  * every logical interface is moved. The reason is due to multicast
16811  * memberships are joined using an interface address in IPv4 while in
16812  * IPv6, interface index is used.
16813  */
16814 static void
16815 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16816 {
16817 	ilm_t	*ilm;
16818 	ilm_t	*ilm_next;
16819 	ilm_t	*new_ilm;
16820 	ilm_t	**ilmp;
16821 	int	count;
16822 	char buf[INET6_ADDRSTRLEN];
16823 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16824 	ip_stack_t	*ipst = from_ill->ill_ipst;
16825 
16826 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16827 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16828 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16829 
16830 	if (ifindex == 0) {
16831 		/*
16832 		 * Form the solicited node mcast address which is used later.
16833 		 */
16834 		ipif_t *ipif;
16835 
16836 		ipif = from_ill->ill_ipif;
16837 		ASSERT(ipif->ipif_id == 0);
16838 
16839 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16840 	}
16841 
16842 	ilmp = &from_ill->ill_ilm;
16843 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16844 		ilm_next = ilm->ilm_next;
16845 
16846 		if (ilm->ilm_flags & ILM_DELETED) {
16847 			ilmp = &ilm->ilm_next;
16848 			continue;
16849 		}
16850 
16851 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16852 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16853 		ASSERT(ilm->ilm_orig_ifindex != 0);
16854 		if (ilm->ilm_orig_ifindex == ifindex) {
16855 			/*
16856 			 * We are failing back multicast memberships.
16857 			 * If the same ilm exists in to_ill, it means somebody
16858 			 * has joined the same group there e.g. ff02::1
16859 			 * is joined within the kernel when the interfaces
16860 			 * came UP.
16861 			 */
16862 			ASSERT(ilm->ilm_ipif == NULL);
16863 			if (new_ilm != NULL) {
16864 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16865 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16866 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16867 					new_ilm->ilm_is_new = B_TRUE;
16868 				}
16869 			} else {
16870 				/*
16871 				 * check if we can just move the ilm
16872 				 */
16873 				if (from_ill->ill_ilm_walker_cnt != 0) {
16874 					/*
16875 					 * We have walkers we cannot move
16876 					 * the ilm, so allocate a new ilm,
16877 					 * this (old) ilm will be marked
16878 					 * ILM_DELETED at the end of the loop
16879 					 * and will be freed when the
16880 					 * last walker exits.
16881 					 */
16882 					new_ilm = (ilm_t *)mi_zalloc
16883 					    (sizeof (ilm_t));
16884 					if (new_ilm == NULL) {
16885 						ip0dbg(("ilm_move_v6: "
16886 						    "FAILBACK of IPv6"
16887 						    " multicast address %s : "
16888 						    "from %s to"
16889 						    " %s failed : ENOMEM \n",
16890 						    inet_ntop(AF_INET6,
16891 						    &ilm->ilm_v6addr, buf,
16892 						    sizeof (buf)),
16893 						    from_ill->ill_name,
16894 						    to_ill->ill_name));
16895 
16896 							ilmp = &ilm->ilm_next;
16897 							continue;
16898 					}
16899 					*new_ilm = *ilm;
16900 					/*
16901 					 * we don't want new_ilm linked to
16902 					 * ilm's filter list.
16903 					 */
16904 					new_ilm->ilm_filter = NULL;
16905 				} else {
16906 					/*
16907 					 * No walkers we can move the ilm.
16908 					 * lets take it out of the list.
16909 					 */
16910 					*ilmp = ilm->ilm_next;
16911 					ilm->ilm_next = NULL;
16912 					new_ilm = ilm;
16913 				}
16914 
16915 				/*
16916 				 * if this is the first ilm for the group
16917 				 * set ilm_notify_driver so that we notify the
16918 				 * driver in ilm_send_multicast_reqs.
16919 				 */
16920 				if (ilm_lookup_ill_v6(to_ill,
16921 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16922 					new_ilm->ilm_notify_driver = B_TRUE;
16923 
16924 				new_ilm->ilm_ill = to_ill;
16925 				/* Add to the to_ill's list */
16926 				new_ilm->ilm_next = to_ill->ill_ilm;
16927 				to_ill->ill_ilm = new_ilm;
16928 				/*
16929 				 * set the flag so that mld_joingroup is
16930 				 * called in ilm_send_multicast_reqs().
16931 				 */
16932 				new_ilm->ilm_is_new = B_TRUE;
16933 			}
16934 			goto bottom;
16935 		} else if (ifindex != 0) {
16936 			/*
16937 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16938 			 * has not matched above, look at the next ilm.
16939 			 */
16940 			ilmp = &ilm->ilm_next;
16941 			continue;
16942 		}
16943 		/*
16944 		 * If we are here, it means ifindex is 0. Failover
16945 		 * everything.
16946 		 *
16947 		 * We need to handle solicited node mcast address
16948 		 * and all_nodes mcast address differently as they
16949 		 * are joined witin the kenrel (ipif_multicast_up)
16950 		 * and potentially from the userland. We are called
16951 		 * after the ipifs of from_ill has been moved.
16952 		 * If we still find ilms on ill with solicited node
16953 		 * mcast address or all_nodes mcast address, it must
16954 		 * belong to the UP interface that has not moved e.g.
16955 		 * ipif_id 0 with the link local prefix does not move.
16956 		 * We join this on the new ill accounting for all the
16957 		 * userland memberships so that applications don't
16958 		 * see any failure.
16959 		 *
16960 		 * We need to make sure that we account only for the
16961 		 * solicited node and all node multicast addresses
16962 		 * that was brought UP on these. In the case of
16963 		 * a failover from A to B, we might have ilms belonging
16964 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16965 		 * for the membership from the userland. If we are failing
16966 		 * over from B to C now, we will find the ones belonging
16967 		 * to A on B. These don't account for the ill_ipif_up_count.
16968 		 * They just move from B to C. The check below on
16969 		 * ilm_orig_ifindex ensures that.
16970 		 */
16971 		if ((ilm->ilm_orig_ifindex ==
16972 		    from_ill->ill_phyint->phyint_ifindex) &&
16973 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16974 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16975 		    &ilm->ilm_v6addr))) {
16976 			ASSERT(ilm->ilm_refcnt > 0);
16977 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16978 			/*
16979 			 * For indentation reasons, we are not using a
16980 			 * "else" here.
16981 			 */
16982 			if (count == 0) {
16983 				ilmp = &ilm->ilm_next;
16984 				continue;
16985 			}
16986 			ilm->ilm_refcnt -= count;
16987 			if (new_ilm != NULL) {
16988 				/*
16989 				 * Can find one with the same
16990 				 * ilm_orig_ifindex, if we are failing
16991 				 * over to a STANDBY. This happens
16992 				 * when somebody wants to join a group
16993 				 * on a STANDBY interface and we
16994 				 * internally join on a different one.
16995 				 * If we had joined on from_ill then, a
16996 				 * failover now will find a new ilm
16997 				 * with this index.
16998 				 */
16999 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17000 				    " new ilm on %s, group address %s\n",
17001 				    to_ill->ill_name,
17002 				    inet_ntop(AF_INET6,
17003 				    &ilm->ilm_v6addr, buf,
17004 				    sizeof (buf))));
17005 				new_ilm->ilm_refcnt += count;
17006 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17007 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17008 					new_ilm->ilm_is_new = B_TRUE;
17009 				}
17010 			} else {
17011 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17012 				if (new_ilm == NULL) {
17013 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17014 					    " multicast address %s : from %s to"
17015 					    " %s failed : ENOMEM \n",
17016 					    inet_ntop(AF_INET6,
17017 					    &ilm->ilm_v6addr, buf,
17018 					    sizeof (buf)), from_ill->ill_name,
17019 					    to_ill->ill_name));
17020 					ilmp = &ilm->ilm_next;
17021 					continue;
17022 				}
17023 				*new_ilm = *ilm;
17024 				new_ilm->ilm_filter = NULL;
17025 				new_ilm->ilm_refcnt = count;
17026 				new_ilm->ilm_timer = INFINITY;
17027 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17028 				new_ilm->ilm_is_new = B_TRUE;
17029 				/*
17030 				 * If the to_ill has not joined this
17031 				 * group we need to tell the driver in
17032 				 * ill_send_multicast_reqs.
17033 				 */
17034 				if (ilm_lookup_ill_v6(to_ill,
17035 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17036 					new_ilm->ilm_notify_driver = B_TRUE;
17037 
17038 				new_ilm->ilm_ill = to_ill;
17039 				/* Add to the to_ill's list */
17040 				new_ilm->ilm_next = to_ill->ill_ilm;
17041 				to_ill->ill_ilm = new_ilm;
17042 				ASSERT(new_ilm->ilm_ipif == NULL);
17043 			}
17044 			if (ilm->ilm_refcnt == 0) {
17045 				goto bottom;
17046 			} else {
17047 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17048 				CLEAR_SLIST(new_ilm->ilm_filter);
17049 				ilmp = &ilm->ilm_next;
17050 			}
17051 			continue;
17052 		} else {
17053 			/*
17054 			 * ifindex = 0 means, move everything pointing at
17055 			 * from_ill. We are doing this becuase ill has
17056 			 * either FAILED or became INACTIVE.
17057 			 *
17058 			 * As we would like to move things later back to
17059 			 * from_ill, we want to retain the identity of this
17060 			 * ilm. Thus, we don't blindly increment the reference
17061 			 * count on the ilms matching the address alone. We
17062 			 * need to match on the ilm_orig_index also. new_ilm
17063 			 * was obtained by matching ilm_orig_index also.
17064 			 */
17065 			if (new_ilm != NULL) {
17066 				/*
17067 				 * This is possible only if a previous restore
17068 				 * was incomplete i.e restore to
17069 				 * ilm_orig_ifindex left some ilms because
17070 				 * of some failures. Thus when we are failing
17071 				 * again, we might find our old friends there.
17072 				 */
17073 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17074 				    " on %s, group address %s\n",
17075 				    to_ill->ill_name,
17076 				    inet_ntop(AF_INET6,
17077 				    &ilm->ilm_v6addr, buf,
17078 				    sizeof (buf))));
17079 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17080 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17081 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17082 					new_ilm->ilm_is_new = B_TRUE;
17083 				}
17084 			} else {
17085 				if (from_ill->ill_ilm_walker_cnt != 0) {
17086 					new_ilm = (ilm_t *)
17087 					    mi_zalloc(sizeof (ilm_t));
17088 					if (new_ilm == NULL) {
17089 						ip0dbg(("ilm_move_v6: "
17090 						    "FAILOVER of IPv6"
17091 						    " multicast address %s : "
17092 						    "from %s to"
17093 						    " %s failed : ENOMEM \n",
17094 						    inet_ntop(AF_INET6,
17095 						    &ilm->ilm_v6addr, buf,
17096 						    sizeof (buf)),
17097 						    from_ill->ill_name,
17098 						    to_ill->ill_name));
17099 
17100 							ilmp = &ilm->ilm_next;
17101 							continue;
17102 					}
17103 					*new_ilm = *ilm;
17104 					new_ilm->ilm_filter = NULL;
17105 				} else {
17106 					*ilmp = ilm->ilm_next;
17107 					new_ilm = ilm;
17108 				}
17109 				/*
17110 				 * If the to_ill has not joined this
17111 				 * group we need to tell the driver in
17112 				 * ill_send_multicast_reqs.
17113 				 */
17114 				if (ilm_lookup_ill_v6(to_ill,
17115 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17116 					new_ilm->ilm_notify_driver = B_TRUE;
17117 
17118 				/* Add to the to_ill's list */
17119 				new_ilm->ilm_next = to_ill->ill_ilm;
17120 				to_ill->ill_ilm = new_ilm;
17121 				ASSERT(ilm->ilm_ipif == NULL);
17122 				new_ilm->ilm_ill = to_ill;
17123 				new_ilm->ilm_is_new = B_TRUE;
17124 			}
17125 
17126 		}
17127 
17128 bottom:
17129 		/*
17130 		 * Revert multicast filter state to (EXCLUDE, NULL).
17131 		 * new_ilm->ilm_is_new should already be set if needed.
17132 		 */
17133 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17134 		CLEAR_SLIST(new_ilm->ilm_filter);
17135 		/*
17136 		 * We allocated/got a new ilm, free the old one.
17137 		 */
17138 		if (new_ilm != ilm) {
17139 			if (from_ill->ill_ilm_walker_cnt == 0) {
17140 				*ilmp = ilm->ilm_next;
17141 				ilm->ilm_next = NULL;
17142 				FREE_SLIST(ilm->ilm_filter);
17143 				FREE_SLIST(ilm->ilm_pendsrcs);
17144 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17145 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17146 				mi_free((char *)ilm);
17147 			} else {
17148 				ilm->ilm_flags |= ILM_DELETED;
17149 				from_ill->ill_ilm_cleanup_reqd = 1;
17150 				ilmp = &ilm->ilm_next;
17151 			}
17152 		}
17153 	}
17154 }
17155 
17156 /*
17157  * Move all the multicast memberships to to_ill. Called when
17158  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17159  * different from IPv6 counterpart as multicast memberships are associated
17160  * with ills in IPv6. This function is called after every ipif is moved
17161  * unlike IPv6, where it is moved only once.
17162  */
17163 static void
17164 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17165 {
17166 	ilm_t	*ilm;
17167 	ilm_t	*ilm_next;
17168 	ilm_t	*new_ilm;
17169 	ilm_t	**ilmp;
17170 	ip_stack_t	*ipst = from_ill->ill_ipst;
17171 
17172 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17173 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17174 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17175 
17176 	ilmp = &from_ill->ill_ilm;
17177 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17178 		ilm_next = ilm->ilm_next;
17179 
17180 		if (ilm->ilm_flags & ILM_DELETED) {
17181 			ilmp = &ilm->ilm_next;
17182 			continue;
17183 		}
17184 
17185 		ASSERT(ilm->ilm_ipif != NULL);
17186 
17187 		if (ilm->ilm_ipif != ipif) {
17188 			ilmp = &ilm->ilm_next;
17189 			continue;
17190 		}
17191 
17192 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17193 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17194 			new_ilm = ilm_lookup_ipif(ipif,
17195 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17196 			if (new_ilm != NULL) {
17197 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17198 				/*
17199 				 * We still need to deal with the from_ill.
17200 				 */
17201 				new_ilm->ilm_is_new = B_TRUE;
17202 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17203 				CLEAR_SLIST(new_ilm->ilm_filter);
17204 				goto delete_ilm;
17205 			}
17206 			/*
17207 			 * If we could not find one e.g. ipif is
17208 			 * still down on to_ill, we add this ilm
17209 			 * on ill_new to preserve the reference
17210 			 * count.
17211 			 */
17212 		}
17213 		/*
17214 		 * When ipifs move, ilms always move with it
17215 		 * to the NEW ill. Thus we should never be
17216 		 * able to find ilm till we really move it here.
17217 		 */
17218 		ASSERT(ilm_lookup_ipif(ipif,
17219 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17220 
17221 		if (from_ill->ill_ilm_walker_cnt != 0) {
17222 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17223 			if (new_ilm == NULL) {
17224 				char buf[INET6_ADDRSTRLEN];
17225 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17226 				    " multicast address %s : "
17227 				    "from %s to"
17228 				    " %s failed : ENOMEM \n",
17229 				    inet_ntop(AF_INET,
17230 				    &ilm->ilm_v6addr, buf,
17231 				    sizeof (buf)),
17232 				    from_ill->ill_name,
17233 				    to_ill->ill_name));
17234 
17235 				ilmp = &ilm->ilm_next;
17236 				continue;
17237 			}
17238 			*new_ilm = *ilm;
17239 			/* We don't want new_ilm linked to ilm's filter list */
17240 			new_ilm->ilm_filter = NULL;
17241 		} else {
17242 			/* Remove from the list */
17243 			*ilmp = ilm->ilm_next;
17244 			new_ilm = ilm;
17245 		}
17246 
17247 		/*
17248 		 * If we have never joined this group on the to_ill
17249 		 * make sure we tell the driver.
17250 		 */
17251 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17252 		    ALL_ZONES) == NULL)
17253 			new_ilm->ilm_notify_driver = B_TRUE;
17254 
17255 		/* Add to the to_ill's list */
17256 		new_ilm->ilm_next = to_ill->ill_ilm;
17257 		to_ill->ill_ilm = new_ilm;
17258 		new_ilm->ilm_is_new = B_TRUE;
17259 
17260 		/*
17261 		 * Revert multicast filter state to (EXCLUDE, NULL)
17262 		 */
17263 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17264 		CLEAR_SLIST(new_ilm->ilm_filter);
17265 
17266 		/*
17267 		 * Delete only if we have allocated a new ilm.
17268 		 */
17269 		if (new_ilm != ilm) {
17270 delete_ilm:
17271 			if (from_ill->ill_ilm_walker_cnt == 0) {
17272 				/* Remove from the list */
17273 				*ilmp = ilm->ilm_next;
17274 				ilm->ilm_next = NULL;
17275 				FREE_SLIST(ilm->ilm_filter);
17276 				FREE_SLIST(ilm->ilm_pendsrcs);
17277 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17278 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17279 				mi_free((char *)ilm);
17280 			} else {
17281 				ilm->ilm_flags |= ILM_DELETED;
17282 				from_ill->ill_ilm_cleanup_reqd = 1;
17283 				ilmp = &ilm->ilm_next;
17284 			}
17285 		}
17286 	}
17287 }
17288 
17289 static uint_t
17290 ipif_get_id(ill_t *ill, uint_t id)
17291 {
17292 	uint_t	unit;
17293 	ipif_t	*tipif;
17294 	boolean_t found = B_FALSE;
17295 	ip_stack_t	*ipst = ill->ill_ipst;
17296 
17297 	/*
17298 	 * During failback, we want to go back to the same id
17299 	 * instead of the smallest id so that the original
17300 	 * configuration is maintained. id is non-zero in that
17301 	 * case.
17302 	 */
17303 	if (id != 0) {
17304 		/*
17305 		 * While failing back, if we still have an ipif with
17306 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17307 		 * as soon as we return from this function. It was
17308 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17309 		 * we can choose the smallest id. Thus we return zero
17310 		 * in that case ignoring the hint.
17311 		 */
17312 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17313 			return (0);
17314 		for (tipif = ill->ill_ipif; tipif != NULL;
17315 		    tipif = tipif->ipif_next) {
17316 			if (tipif->ipif_id == id) {
17317 				found = B_TRUE;
17318 				break;
17319 			}
17320 		}
17321 		/*
17322 		 * If somebody already plumbed another logical
17323 		 * with the same id, we won't be able to find it.
17324 		 */
17325 		if (!found)
17326 			return (id);
17327 	}
17328 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17329 		found = B_FALSE;
17330 		for (tipif = ill->ill_ipif; tipif != NULL;
17331 		    tipif = tipif->ipif_next) {
17332 			if (tipif->ipif_id == unit) {
17333 				found = B_TRUE;
17334 				break;
17335 			}
17336 		}
17337 		if (!found)
17338 			break;
17339 	}
17340 	return (unit);
17341 }
17342 
17343 /* ARGSUSED */
17344 static int
17345 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17346     ipif_t **rep_ipif_ptr)
17347 {
17348 	ill_t	*from_ill;
17349 	ipif_t	*rep_ipif;
17350 	uint_t	unit;
17351 	int err = 0;
17352 	ipif_t	*to_ipif;
17353 	struct iocblk	*iocp;
17354 	boolean_t failback_cmd;
17355 	boolean_t remove_ipif;
17356 	int	rc;
17357 	ip_stack_t	*ipst;
17358 
17359 	ASSERT(IAM_WRITER_ILL(to_ill));
17360 	ASSERT(IAM_WRITER_IPIF(ipif));
17361 
17362 	iocp = (struct iocblk *)mp->b_rptr;
17363 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17364 	remove_ipif = B_FALSE;
17365 
17366 	from_ill = ipif->ipif_ill;
17367 	ipst = from_ill->ill_ipst;
17368 
17369 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17370 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17371 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17372 
17373 	/*
17374 	 * Don't move LINK LOCAL addresses as they are tied to
17375 	 * physical interface.
17376 	 */
17377 	if (from_ill->ill_isv6 &&
17378 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17379 		ipif->ipif_was_up = B_FALSE;
17380 		IPIF_UNMARK_MOVING(ipif);
17381 		return (0);
17382 	}
17383 
17384 	/*
17385 	 * We set the ipif_id to maximum so that the search for
17386 	 * ipif_id will pick the lowest number i.e 0 in the
17387 	 * following 2 cases :
17388 	 *
17389 	 * 1) We have a replacement ipif at the head of to_ill.
17390 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17391 	 *    on to_ill and hence the MOVE might fail. We want to
17392 	 *    remove it only if we could move the ipif. Thus, by
17393 	 *    setting it to the MAX value, we make the search in
17394 	 *    ipif_get_id return the zeroth id.
17395 	 *
17396 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17397 	 *    we might just have a zero address plumbed on the ipif
17398 	 *    with zero id in the case of IPv4. We remove that while
17399 	 *    doing the failback. We want to remove it only if we
17400 	 *    could move the ipif. Thus, by setting it to the MAX
17401 	 *    value, we make the search in ipif_get_id return the
17402 	 *    zeroth id.
17403 	 *
17404 	 * Both (1) and (2) are done only when when we are moving
17405 	 * an ipif (either due to failover/failback) which originally
17406 	 * belonged to this interface i.e the ipif_orig_ifindex is
17407 	 * the same as to_ill's ifindex. This is needed so that
17408 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17409 	 * from B -> A (B is being removed from the group) and
17410 	 * FAILBACK from A -> B restores the original configuration.
17411 	 * Without the check for orig_ifindex, the second FAILOVER
17412 	 * could make the ipif belonging to B replace the A's zeroth
17413 	 * ipif and the subsequent failback re-creating the replacement
17414 	 * ipif again.
17415 	 *
17416 	 * NOTE : We created the replacement ipif when we did a
17417 	 * FAILOVER (See below). We could check for FAILBACK and
17418 	 * then look for replacement ipif to be removed. But we don't
17419 	 * want to do that because we wan't to allow the possibility
17420 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17421 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17422 	 * from B -> A.
17423 	 */
17424 	to_ipif = to_ill->ill_ipif;
17425 	if ((to_ill->ill_phyint->phyint_ifindex ==
17426 	    ipif->ipif_orig_ifindex) &&
17427 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17428 		ASSERT(to_ipif->ipif_id == 0);
17429 		remove_ipif = B_TRUE;
17430 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17431 	}
17432 	/*
17433 	 * Find the lowest logical unit number on the to_ill.
17434 	 * If we are failing back, try to get the original id
17435 	 * rather than the lowest one so that the original
17436 	 * configuration is maintained.
17437 	 *
17438 	 * XXX need a better scheme for this.
17439 	 */
17440 	if (failback_cmd) {
17441 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17442 	} else {
17443 		unit = ipif_get_id(to_ill, 0);
17444 	}
17445 
17446 	/* Reset back to zero in case we fail below */
17447 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17448 		to_ipif->ipif_id = 0;
17449 
17450 	if (unit == ipst->ips_ip_addrs_per_if) {
17451 		ipif->ipif_was_up = B_FALSE;
17452 		IPIF_UNMARK_MOVING(ipif);
17453 		return (EINVAL);
17454 	}
17455 
17456 	/*
17457 	 * ipif is ready to move from "from_ill" to "to_ill".
17458 	 *
17459 	 * 1) If we are moving ipif with id zero, create a
17460 	 *    replacement ipif for this ipif on from_ill. If this fails
17461 	 *    fail the MOVE operation.
17462 	 *
17463 	 * 2) Remove the replacement ipif on to_ill if any.
17464 	 *    We could remove the replacement ipif when we are moving
17465 	 *    the ipif with id zero. But what if somebody already
17466 	 *    unplumbed it ? Thus we always remove it if it is present.
17467 	 *    We want to do it only if we are sure we are going to
17468 	 *    move the ipif to to_ill which is why there are no
17469 	 *    returns due to error till ipif is linked to to_ill.
17470 	 *    Note that the first ipif that we failback will always
17471 	 *    be zero if it is present.
17472 	 */
17473 	if (ipif->ipif_id == 0) {
17474 		ipaddr_t inaddr_any = INADDR_ANY;
17475 
17476 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17477 		if (rep_ipif == NULL) {
17478 			ipif->ipif_was_up = B_FALSE;
17479 			IPIF_UNMARK_MOVING(ipif);
17480 			return (ENOMEM);
17481 		}
17482 		*rep_ipif = ipif_zero;
17483 		/*
17484 		 * Before we put the ipif on the list, store the addresses
17485 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17486 		 * assumes so. This logic is not any different from what
17487 		 * ipif_allocate does.
17488 		 */
17489 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17490 		    &rep_ipif->ipif_v6lcl_addr);
17491 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17492 		    &rep_ipif->ipif_v6src_addr);
17493 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17494 		    &rep_ipif->ipif_v6subnet);
17495 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17496 		    &rep_ipif->ipif_v6net_mask);
17497 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17498 		    &rep_ipif->ipif_v6brd_addr);
17499 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17500 		    &rep_ipif->ipif_v6pp_dst_addr);
17501 		/*
17502 		 * We mark IPIF_NOFAILOVER so that this can never
17503 		 * move.
17504 		 */
17505 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17506 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17507 		rep_ipif->ipif_replace_zero = B_TRUE;
17508 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17509 		    MUTEX_DEFAULT, NULL);
17510 		rep_ipif->ipif_id = 0;
17511 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17512 		rep_ipif->ipif_ill = from_ill;
17513 		rep_ipif->ipif_orig_ifindex =
17514 		    from_ill->ill_phyint->phyint_ifindex;
17515 		/* Insert at head */
17516 		rep_ipif->ipif_next = from_ill->ill_ipif;
17517 		from_ill->ill_ipif = rep_ipif;
17518 		/*
17519 		 * We don't really care to let apps know about
17520 		 * this interface.
17521 		 */
17522 	}
17523 
17524 	if (remove_ipif) {
17525 		/*
17526 		 * We set to a max value above for this case to get
17527 		 * id zero. ASSERT that we did get one.
17528 		 */
17529 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17530 		rep_ipif = to_ipif;
17531 		to_ill->ill_ipif = rep_ipif->ipif_next;
17532 		rep_ipif->ipif_next = NULL;
17533 		/*
17534 		 * If some apps scanned and find this interface,
17535 		 * it is time to let them know, so that they can
17536 		 * delete it.
17537 		 */
17538 
17539 		*rep_ipif_ptr = rep_ipif;
17540 	}
17541 
17542 	/* Get it out of the ILL interface list. */
17543 	ipif_remove(ipif, B_FALSE);
17544 
17545 	/* Assign the new ill */
17546 	ipif->ipif_ill = to_ill;
17547 	ipif->ipif_id = unit;
17548 	/* id has already been checked */
17549 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17550 	ASSERT(rc == 0);
17551 	/* Let SCTP update its list */
17552 	sctp_move_ipif(ipif, from_ill, to_ill);
17553 	/*
17554 	 * Handle the failover and failback of ipif_t between
17555 	 * ill_t that have differing maximum mtu values.
17556 	 */
17557 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17558 		if (ipif->ipif_saved_mtu == 0) {
17559 			/*
17560 			 * As this ipif_t is moving to an ill_t
17561 			 * that has a lower ill_max_mtu, its
17562 			 * ipif_mtu needs to be saved so it can
17563 			 * be restored during failback or during
17564 			 * failover to an ill_t which has a
17565 			 * higher ill_max_mtu.
17566 			 */
17567 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17568 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17569 		} else {
17570 			/*
17571 			 * The ipif_t is, once again, moving to
17572 			 * an ill_t that has a lower maximum mtu
17573 			 * value.
17574 			 */
17575 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17576 		}
17577 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17578 	    ipif->ipif_saved_mtu != 0) {
17579 		/*
17580 		 * The mtu of this ipif_t had to be reduced
17581 		 * during an earlier failover; this is an
17582 		 * opportunity for it to be increased (either as
17583 		 * part of another failover or a failback).
17584 		 */
17585 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17586 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17587 			ipif->ipif_saved_mtu = 0;
17588 		} else {
17589 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17590 		}
17591 	}
17592 
17593 	/*
17594 	 * We preserve all the other fields of the ipif including
17595 	 * ipif_saved_ire_mp. The routes that are saved here will
17596 	 * be recreated on the new interface and back on the old
17597 	 * interface when we move back.
17598 	 */
17599 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17600 
17601 	return (err);
17602 }
17603 
17604 static int
17605 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17606     int ifindex, ipif_t **rep_ipif_ptr)
17607 {
17608 	ipif_t *mipif;
17609 	ipif_t *ipif_next;
17610 	int err;
17611 
17612 	/*
17613 	 * We don't really try to MOVE back things if some of the
17614 	 * operations fail. The daemon will take care of moving again
17615 	 * later on.
17616 	 */
17617 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17618 		ipif_next = mipif->ipif_next;
17619 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17620 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17621 
17622 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17623 
17624 			/*
17625 			 * When the MOVE fails, it is the job of the
17626 			 * application to take care of this properly
17627 			 * i.e try again if it is ENOMEM.
17628 			 */
17629 			if (mipif->ipif_ill != from_ill) {
17630 				/*
17631 				 * ipif has moved.
17632 				 *
17633 				 * Move the multicast memberships associated
17634 				 * with this ipif to the new ill. For IPv6, we
17635 				 * do it once after all the ipifs are moved
17636 				 * (in ill_move) as they are not associated
17637 				 * with ipifs.
17638 				 *
17639 				 * We need to move the ilms as the ipif has
17640 				 * already been moved to a new ill even
17641 				 * in the case of errors. Neither
17642 				 * ilm_free(ipif) will find the ilm
17643 				 * when somebody unplumbs this ipif nor
17644 				 * ilm_delete(ilm) will be able to find the
17645 				 * ilm, if we don't move now.
17646 				 */
17647 				if (!from_ill->ill_isv6)
17648 					ilm_move_v4(from_ill, to_ill, mipif);
17649 			}
17650 
17651 			if (err != 0)
17652 				return (err);
17653 		}
17654 	}
17655 	return (0);
17656 }
17657 
17658 static int
17659 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17660 {
17661 	int ifindex;
17662 	int err;
17663 	struct iocblk	*iocp;
17664 	ipif_t	*ipif;
17665 	ipif_t *rep_ipif_ptr = NULL;
17666 	ipif_t	*from_ipif = NULL;
17667 	boolean_t check_rep_if = B_FALSE;
17668 	ip_stack_t	*ipst = from_ill->ill_ipst;
17669 
17670 	iocp = (struct iocblk *)mp->b_rptr;
17671 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17672 		/*
17673 		 * Move everything pointing at from_ill to to_ill.
17674 		 * We acheive this by passing in 0 as ifindex.
17675 		 */
17676 		ifindex = 0;
17677 	} else {
17678 		/*
17679 		 * Move everything pointing at from_ill whose original
17680 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17681 		 * We acheive this by passing in ifindex rather than 0.
17682 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17683 		 */
17684 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17685 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17686 	}
17687 
17688 	/*
17689 	 * Determine if there is at least one ipif that would move from
17690 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17691 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17692 	 * the move, in which case we need to quiesce the replacement ipif also.
17693 	 */
17694 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17695 	    from_ipif = from_ipif->ipif_next) {
17696 		if (((ifindex == 0) ||
17697 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17698 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17699 			check_rep_if = B_TRUE;
17700 			break;
17701 		}
17702 	}
17703 
17704 
17705 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17706 
17707 	GRAB_ILL_LOCKS(from_ill, to_ill);
17708 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17709 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17710 		    mp, ILL_MOVE_OK);
17711 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17712 		return (EINPROGRESS);
17713 	}
17714 
17715 	/* Check if the replacement ipif is quiescent to delete */
17716 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17717 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17718 		to_ill->ill_ipif->ipif_state_flags |=
17719 		    IPIF_MOVING | IPIF_CHANGING;
17720 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17721 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17722 			    mp, ILL_MOVE_OK);
17723 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17724 			return (EINPROGRESS);
17725 		}
17726 	}
17727 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17728 
17729 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17730 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17731 	GRAB_ILL_LOCKS(from_ill, to_ill);
17732 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17733 
17734 	/* ilm_move is done inside ipif_move for IPv4 */
17735 	if (err == 0 && from_ill->ill_isv6)
17736 		ilm_move_v6(from_ill, to_ill, ifindex);
17737 
17738 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17739 	rw_exit(&ipst->ips_ill_g_lock);
17740 
17741 	/*
17742 	 * send rts messages and multicast messages.
17743 	 */
17744 	if (rep_ipif_ptr != NULL) {
17745 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17746 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17747 			rep_ipif_ptr->ipif_recovery_id = 0;
17748 		}
17749 		ip_rts_ifmsg(rep_ipif_ptr);
17750 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17751 #ifdef DEBUG
17752 		ipif_trace_cleanup(rep_ipif_ptr);
17753 #endif
17754 		mi_free(rep_ipif_ptr);
17755 	}
17756 
17757 	conn_move_ill(from_ill, to_ill, ifindex);
17758 
17759 	return (err);
17760 }
17761 
17762 /*
17763  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17764  * Also checks for the validity of the arguments.
17765  * Note: We are already exclusive inside the from group.
17766  * It is upto the caller to release refcnt on the to_ill's.
17767  */
17768 static int
17769 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17770     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17771 {
17772 	int dst_index;
17773 	ipif_t *ipif_v4, *ipif_v6;
17774 	struct lifreq *lifr;
17775 	mblk_t *mp1;
17776 	boolean_t exists;
17777 	sin_t	*sin;
17778 	int	err = 0;
17779 	ip_stack_t	*ipst;
17780 
17781 	if (CONN_Q(q))
17782 		ipst = CONNQ_TO_IPST(q);
17783 	else
17784 		ipst = ILLQ_TO_IPST(q);
17785 
17786 
17787 	if ((mp1 = mp->b_cont) == NULL)
17788 		return (EPROTO);
17789 
17790 	if ((mp1 = mp1->b_cont) == NULL)
17791 		return (EPROTO);
17792 
17793 	lifr = (struct lifreq *)mp1->b_rptr;
17794 	sin = (sin_t *)&lifr->lifr_addr;
17795 
17796 	/*
17797 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17798 	 * specific operations.
17799 	 */
17800 	if (sin->sin_family != AF_UNSPEC)
17801 		return (EINVAL);
17802 
17803 	/*
17804 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17805 	 * NULLs for the last 4 args and we know the lookup won't fail
17806 	 * with EINPROGRESS.
17807 	 */
17808 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17809 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17810 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17811 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17812 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17813 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17814 
17815 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17816 		return (ENXIO);
17817 
17818 	if (ipif_v4 != NULL) {
17819 		ASSERT(ipif_v4->ipif_refcnt != 0);
17820 		if (ipif_v4->ipif_id != 0) {
17821 			err = EINVAL;
17822 			goto done;
17823 		}
17824 
17825 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17826 		*ill_from_v4 = ipif_v4->ipif_ill;
17827 	}
17828 
17829 	if (ipif_v6 != NULL) {
17830 		ASSERT(ipif_v6->ipif_refcnt != 0);
17831 		if (ipif_v6->ipif_id != 0) {
17832 			err = EINVAL;
17833 			goto done;
17834 		}
17835 
17836 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17837 		*ill_from_v6 = ipif_v6->ipif_ill;
17838 	}
17839 
17840 	err = 0;
17841 	dst_index = lifr->lifr_movetoindex;
17842 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17843 	    q, mp, ip_process_ioctl, &err, ipst);
17844 	if (err != 0) {
17845 		/*
17846 		 * There could be only v6.
17847 		 */
17848 		if (err != ENXIO)
17849 			goto done;
17850 		err = 0;
17851 	}
17852 
17853 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17854 	    q, mp, ip_process_ioctl, &err, ipst);
17855 	if (err != 0) {
17856 		if (err != ENXIO)
17857 			goto done;
17858 		if (*ill_to_v4 == NULL) {
17859 			err = ENXIO;
17860 			goto done;
17861 		}
17862 		err = 0;
17863 	}
17864 
17865 	/*
17866 	 * If we have something to MOVE i.e "from" not NULL,
17867 	 * "to" should be non-NULL.
17868 	 */
17869 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17870 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17871 		err = EINVAL;
17872 	}
17873 
17874 done:
17875 	if (ipif_v4 != NULL)
17876 		ipif_refrele(ipif_v4);
17877 	if (ipif_v6 != NULL)
17878 		ipif_refrele(ipif_v6);
17879 	return (err);
17880 }
17881 
17882 /*
17883  * FAILOVER and FAILBACK are modelled as MOVE operations.
17884  *
17885  * We don't check whether the MOVE is within the same group or
17886  * not, because this ioctl can be used as a generic mechanism
17887  * to failover from interface A to B, though things will function
17888  * only if they are really part of the same group. Moreover,
17889  * all ipifs may be down and hence temporarily out of the group.
17890  *
17891  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17892  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17893  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17894  * have been deleted and there are no active references. Once quiescent the
17895  * ipif's are moved and brought up on the new ill.
17896  *
17897  * Normally the source ill and destination ill belong to the same IPMP group
17898  * and hence the same ipsq_t. In the event they don't belong to the same
17899  * same group the two ipsq's are first merged into one ipsq - that of the
17900  * to_ill. The multicast memberships on the source and destination ill cannot
17901  * change during the move operation since multicast joins/leaves also have to
17902  * execute on the same ipsq and are hence serialized.
17903  */
17904 /* ARGSUSED */
17905 int
17906 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17907     ip_ioctl_cmd_t *ipip, void *ifreq)
17908 {
17909 	ill_t *ill_to_v4 = NULL;
17910 	ill_t *ill_to_v6 = NULL;
17911 	ill_t *ill_from_v4 = NULL;
17912 	ill_t *ill_from_v6 = NULL;
17913 	int err = 0;
17914 
17915 	/*
17916 	 * setup from and to ill's, we can get EINPROGRESS only for
17917 	 * to_ill's.
17918 	 */
17919 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17920 	    &ill_to_v4, &ill_to_v6);
17921 
17922 	if (err != 0) {
17923 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17924 		goto done;
17925 	}
17926 
17927 	/*
17928 	 * nothing to do.
17929 	 */
17930 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17931 		goto done;
17932 	}
17933 
17934 	/*
17935 	 * nothing to do.
17936 	 */
17937 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17938 		goto done;
17939 	}
17940 
17941 	/*
17942 	 * Mark the ill as changing.
17943 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17944 	 * in ill_up_ipifs in case of error they are cleared below.
17945 	 */
17946 
17947 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17948 	if (ill_from_v4 != NULL)
17949 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17950 	if (ill_from_v6 != NULL)
17951 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17952 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17953 
17954 	/*
17955 	 * Make sure that both src and dst are
17956 	 * in the same syncq group. If not make it happen.
17957 	 * We are not holding any locks because we are the writer
17958 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17959 	 * to protect to_ipsq against changing.
17960 	 */
17961 	if (ill_from_v4 != NULL) {
17962 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17963 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17964 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17965 			    NULL, mp, q);
17966 			goto err_ret;
17967 
17968 		}
17969 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17970 	} else {
17971 
17972 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17973 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17974 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17975 			    NULL, mp, q);
17976 			goto err_ret;
17977 
17978 		}
17979 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17980 	}
17981 
17982 	/*
17983 	 * Now that the ipsq's have been merged and we are the writer
17984 	 * lets mark to_ill as changing as well.
17985 	 */
17986 
17987 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17988 	if (ill_to_v4 != NULL)
17989 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17990 	if (ill_to_v6 != NULL)
17991 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17992 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17993 
17994 	/*
17995 	 * Its ok for us to proceed with the move even if
17996 	 * ill_pending_mp is non null on one of the from ill's as the reply
17997 	 * should not be looking at the ipif, it should only care about the
17998 	 * ill itself.
17999 	 */
18000 
18001 	/*
18002 	 * lets move ipv4 first.
18003 	 */
18004 	if (ill_from_v4 != NULL) {
18005 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18006 		ill_from_v4->ill_move_in_progress = B_TRUE;
18007 		ill_to_v4->ill_move_in_progress = B_TRUE;
18008 		ill_to_v4->ill_move_peer = ill_from_v4;
18009 		ill_from_v4->ill_move_peer = ill_to_v4;
18010 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18011 	}
18012 
18013 	/*
18014 	 * Now lets move ipv6.
18015 	 */
18016 	if (err == 0 && ill_from_v6 != NULL) {
18017 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18018 		ill_from_v6->ill_move_in_progress = B_TRUE;
18019 		ill_to_v6->ill_move_in_progress = B_TRUE;
18020 		ill_to_v6->ill_move_peer = ill_from_v6;
18021 		ill_from_v6->ill_move_peer = ill_to_v6;
18022 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18023 	}
18024 
18025 err_ret:
18026 	/*
18027 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18028 	 * moved to become quiescent.
18029 	 */
18030 	if (err == EINPROGRESS) {
18031 		goto done;
18032 	}
18033 
18034 	/*
18035 	 * if err is set ill_up_ipifs will not be called
18036 	 * lets clear the flags.
18037 	 */
18038 
18039 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18040 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18041 	/*
18042 	 * Some of the clearing may be redundant. But it is simple
18043 	 * not making any extra checks.
18044 	 */
18045 	if (ill_from_v6 != NULL) {
18046 		ill_from_v6->ill_move_in_progress = B_FALSE;
18047 		ill_from_v6->ill_move_peer = NULL;
18048 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18049 	}
18050 	if (ill_from_v4 != NULL) {
18051 		ill_from_v4->ill_move_in_progress = B_FALSE;
18052 		ill_from_v4->ill_move_peer = NULL;
18053 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18054 	}
18055 	if (ill_to_v6 != NULL) {
18056 		ill_to_v6->ill_move_in_progress = B_FALSE;
18057 		ill_to_v6->ill_move_peer = NULL;
18058 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18059 	}
18060 	if (ill_to_v4 != NULL) {
18061 		ill_to_v4->ill_move_in_progress = B_FALSE;
18062 		ill_to_v4->ill_move_peer = NULL;
18063 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18064 	}
18065 
18066 	/*
18067 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18068 	 * Do this always to maintain proper state i.e even in case of errors.
18069 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18070 	 * we need not call on both v4 and v6 interfaces.
18071 	 */
18072 	if (ill_from_v4 != NULL) {
18073 		if ((ill_from_v4->ill_phyint->phyint_flags &
18074 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18075 			phyint_inactive(ill_from_v4->ill_phyint);
18076 		}
18077 	} else if (ill_from_v6 != NULL) {
18078 		if ((ill_from_v6->ill_phyint->phyint_flags &
18079 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18080 			phyint_inactive(ill_from_v6->ill_phyint);
18081 		}
18082 	}
18083 
18084 	if (ill_to_v4 != NULL) {
18085 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18086 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18087 		}
18088 	} else if (ill_to_v6 != NULL) {
18089 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18090 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18091 		}
18092 	}
18093 
18094 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18095 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18096 
18097 no_err:
18098 	/*
18099 	 * lets bring the interfaces up on the to_ill.
18100 	 */
18101 	if (err == 0) {
18102 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18103 		    q, mp);
18104 	}
18105 
18106 	if (err == 0) {
18107 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18108 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18109 
18110 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18111 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18112 	}
18113 done:
18114 
18115 	if (ill_to_v4 != NULL) {
18116 		ill_refrele(ill_to_v4);
18117 	}
18118 	if (ill_to_v6 != NULL) {
18119 		ill_refrele(ill_to_v6);
18120 	}
18121 
18122 	return (err);
18123 }
18124 
18125 static void
18126 ill_dl_down(ill_t *ill)
18127 {
18128 	/*
18129 	 * The ill is down; unbind but stay attached since we're still
18130 	 * associated with a PPA. If we have negotiated DLPI capabilites
18131 	 * with the data link service provider (IDS_OK) then reset them.
18132 	 * The interval between unbinding and rebinding is potentially
18133 	 * unbounded hence we cannot assume things will be the same.
18134 	 * The DLPI capabilities will be probed again when the data link
18135 	 * is brought up.
18136 	 */
18137 	mblk_t	*mp = ill->ill_unbind_mp;
18138 
18139 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18140 
18141 	ill->ill_unbind_mp = NULL;
18142 	if (mp != NULL) {
18143 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18144 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18145 		    ill->ill_name));
18146 		mutex_enter(&ill->ill_lock);
18147 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18148 		mutex_exit(&ill->ill_lock);
18149 		/*
18150 		 * Reset the capabilities if the negotiation is done or is
18151 		 * still in progress. Note that ill_capability_reset() will
18152 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18153 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18154 		 *
18155 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18156 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18157 		 * the capabilities renegotiation from happening.
18158 		 */
18159 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18160 			ill_capability_reset(ill);
18161 		ill->ill_capab_reneg = B_FALSE;
18162 
18163 		ill_dlpi_send(ill, mp);
18164 	}
18165 
18166 	/*
18167 	 * Toss all of our multicast memberships.  We could keep them, but
18168 	 * then we'd have to do bookkeeping of any joins and leaves performed
18169 	 * by the application while the the interface is down (we can't just
18170 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18171 	 * on a downed interface).
18172 	 */
18173 	ill_leave_multicast(ill);
18174 
18175 	mutex_enter(&ill->ill_lock);
18176 	ill->ill_dl_up = 0;
18177 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18178 	mutex_exit(&ill->ill_lock);
18179 }
18180 
18181 static void
18182 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18183 {
18184 	union DL_primitives *dlp;
18185 	t_uscalar_t prim;
18186 
18187 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18188 
18189 	dlp = (union DL_primitives *)mp->b_rptr;
18190 	prim = dlp->dl_primitive;
18191 
18192 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18193 	    dl_primstr(prim), prim, ill->ill_name));
18194 
18195 	switch (prim) {
18196 	case DL_PHYS_ADDR_REQ:
18197 	{
18198 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18199 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18200 		break;
18201 	}
18202 	case DL_BIND_REQ:
18203 		mutex_enter(&ill->ill_lock);
18204 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18205 		mutex_exit(&ill->ill_lock);
18206 		break;
18207 	}
18208 
18209 	/*
18210 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18211 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18212 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18213 	 */
18214 	mutex_enter(&ill->ill_lock);
18215 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18216 	    (prim == DL_UNBIND_REQ)) {
18217 		ill->ill_dlpi_pending = prim;
18218 	}
18219 	mutex_exit(&ill->ill_lock);
18220 
18221 	putnext(ill->ill_wq, mp);
18222 }
18223 
18224 /*
18225  * Helper function for ill_dlpi_send().
18226  */
18227 /* ARGSUSED */
18228 static void
18229 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18230 {
18231 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18232 }
18233 
18234 /*
18235  * Send a DLPI control message to the driver but make sure there
18236  * is only one outstanding message. Uses ill_dlpi_pending to tell
18237  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18238  * when an ACK or a NAK is received to process the next queued message.
18239  */
18240 void
18241 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18242 {
18243 	mblk_t **mpp;
18244 
18245 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18246 
18247 	/*
18248 	 * To ensure that any DLPI requests for current exclusive operation
18249 	 * are always completely sent before any DLPI messages for other
18250 	 * operations, require writer access before enqueuing.
18251 	 */
18252 	if (!IAM_WRITER_ILL(ill)) {
18253 		ill_refhold(ill);
18254 		/* qwriter_ip() does the ill_refrele() */
18255 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18256 		    NEW_OP, B_TRUE);
18257 		return;
18258 	}
18259 
18260 	mutex_enter(&ill->ill_lock);
18261 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18262 		/* Must queue message. Tail insertion */
18263 		mpp = &ill->ill_dlpi_deferred;
18264 		while (*mpp != NULL)
18265 			mpp = &((*mpp)->b_next);
18266 
18267 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18268 		    ill->ill_name));
18269 
18270 		*mpp = mp;
18271 		mutex_exit(&ill->ill_lock);
18272 		return;
18273 	}
18274 	mutex_exit(&ill->ill_lock);
18275 	ill_dlpi_dispatch(ill, mp);
18276 }
18277 
18278 /*
18279  * Send all deferred DLPI messages without waiting for their ACKs.
18280  */
18281 void
18282 ill_dlpi_send_deferred(ill_t *ill)
18283 {
18284 	mblk_t *mp, *nextmp;
18285 
18286 	/*
18287 	 * Clear ill_dlpi_pending so that the message is not queued in
18288 	 * ill_dlpi_send().
18289 	 */
18290 	mutex_enter(&ill->ill_lock);
18291 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18292 	mp = ill->ill_dlpi_deferred;
18293 	ill->ill_dlpi_deferred = NULL;
18294 	mutex_exit(&ill->ill_lock);
18295 
18296 	for (; mp != NULL; mp = nextmp) {
18297 		nextmp = mp->b_next;
18298 		mp->b_next = NULL;
18299 		ill_dlpi_send(ill, mp);
18300 	}
18301 }
18302 
18303 /*
18304  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18305  */
18306 boolean_t
18307 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18308 {
18309 	t_uscalar_t pending;
18310 
18311 	mutex_enter(&ill->ill_lock);
18312 	if (ill->ill_dlpi_pending == prim) {
18313 		mutex_exit(&ill->ill_lock);
18314 		return (B_TRUE);
18315 	}
18316 
18317 	/*
18318 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18319 	 * without waiting, so don't print any warnings in that case.
18320 	 */
18321 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18322 		mutex_exit(&ill->ill_lock);
18323 		return (B_FALSE);
18324 	}
18325 	pending = ill->ill_dlpi_pending;
18326 	mutex_exit(&ill->ill_lock);
18327 
18328 	if (pending == DL_PRIM_INVAL) {
18329 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18330 		    "received unsolicited ack for %s on %s\n",
18331 		    dl_primstr(prim), ill->ill_name);
18332 	} else {
18333 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18334 		    "received unexpected ack for %s on %s (expecting %s)\n",
18335 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18336 	}
18337 	return (B_FALSE);
18338 }
18339 
18340 /*
18341  * Called when an DLPI control message has been acked or nacked to
18342  * send down the next queued message (if any).
18343  */
18344 void
18345 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18346 {
18347 	mblk_t *mp;
18348 
18349 	ASSERT(IAM_WRITER_ILL(ill));
18350 	mutex_enter(&ill->ill_lock);
18351 
18352 	ASSERT(prim != DL_PRIM_INVAL);
18353 	ASSERT(ill->ill_dlpi_pending == prim);
18354 
18355 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18356 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18357 
18358 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18359 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18360 		cv_signal(&ill->ill_cv);
18361 		mutex_exit(&ill->ill_lock);
18362 		return;
18363 	}
18364 
18365 	ill->ill_dlpi_deferred = mp->b_next;
18366 	mp->b_next = NULL;
18367 	mutex_exit(&ill->ill_lock);
18368 
18369 	ill_dlpi_dispatch(ill, mp);
18370 }
18371 
18372 void
18373 conn_delete_ire(conn_t *connp, caddr_t arg)
18374 {
18375 	ipif_t	*ipif = (ipif_t *)arg;
18376 	ire_t	*ire;
18377 
18378 	/*
18379 	 * Look at the cached ires on conns which has pointers to ipifs.
18380 	 * We just call ire_refrele which clears up the reference
18381 	 * to ire. Called when a conn closes. Also called from ipif_free
18382 	 * to cleanup indirect references to the stale ipif via the cached ire.
18383 	 */
18384 	mutex_enter(&connp->conn_lock);
18385 	ire = connp->conn_ire_cache;
18386 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18387 		connp->conn_ire_cache = NULL;
18388 		mutex_exit(&connp->conn_lock);
18389 		IRE_REFRELE_NOTR(ire);
18390 		return;
18391 	}
18392 	mutex_exit(&connp->conn_lock);
18393 
18394 }
18395 
18396 /*
18397  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18398  * of IREs. Those IREs may have been previously cached in the conn structure.
18399  * This ipcl_walk() walker function releases all references to such IREs based
18400  * on the condemned flag.
18401  */
18402 /* ARGSUSED */
18403 void
18404 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18405 {
18406 	ire_t	*ire;
18407 
18408 	mutex_enter(&connp->conn_lock);
18409 	ire = connp->conn_ire_cache;
18410 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18411 		connp->conn_ire_cache = NULL;
18412 		mutex_exit(&connp->conn_lock);
18413 		IRE_REFRELE_NOTR(ire);
18414 		return;
18415 	}
18416 	mutex_exit(&connp->conn_lock);
18417 }
18418 
18419 /*
18420  * Take down a specific interface, but don't lose any information about it.
18421  * Also delete interface from its interface group (ifgrp).
18422  * (Always called as writer.)
18423  * This function goes through the down sequence even if the interface is
18424  * already down. There are 2 reasons.
18425  * a. Currently we permit interface routes that depend on down interfaces
18426  *    to be added. This behaviour itself is questionable. However it appears
18427  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18428  *    time. We go thru the cleanup in order to remove these routes.
18429  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18430  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18431  *    down, but we need to cleanup i.e. do ill_dl_down and
18432  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18433  *
18434  * IP-MT notes:
18435  *
18436  * Model of reference to interfaces.
18437  *
18438  * The following members in ipif_t track references to the ipif.
18439  *	int     ipif_refcnt;    Active reference count
18440  *	uint_t  ipif_cnt_ire;   Number of ire's referencing this ipif
18441  *	uint_t  ipif_cnt_ilm;   Number of ilms's references this ipif.
18442  *
18443  * The following members in ill_t track references to the ill.
18444  *	int             ill_refcnt;     active refcnt
18445  *	uint_t          ill_cnt_ire;	Number of ires referencing ill
18446  *	uint_t          ill_cnt_nce;	Number of nces referencing ill
18447  *	uint_t          ill_cnt_ilm;	Number of ilms referencing ill
18448  *
18449  * Reference to an ipif or ill can be obtained in any of the following ways.
18450  *
18451  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18452  * Pointers to ipif / ill from other data structures viz ire and conn.
18453  * Implicit reference to the ipif / ill by holding a reference to the ire.
18454  *
18455  * The ipif/ill lookup functions return a reference held ipif / ill.
18456  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18457  * This is a purely dynamic reference count associated with threads holding
18458  * references to the ipif / ill. Pointers from other structures do not
18459  * count towards this reference count.
18460  *
18461  * ipif_cnt_ire/ill_cnt_ire is the number of ire's
18462  * associated with the ipif/ill. This is incremented whenever a new
18463  * ire is created referencing the ipif/ill. This is done atomically inside
18464  * ire_add_v[46] where the ire is actually added to the ire hash table.
18465  * The count is decremented in ire_inactive where the ire is destroyed.
18466  *
18467  * nce's reference ill's thru nce_ill and the count of nce's associated with
18468  * an ill is recorded in ill_cnt_nce. This is incremented atomically in
18469  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18470  * table. Similarly it is decremented in ndp_inactive() where the nce
18471  * is destroyed.
18472  *
18473  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18474  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18475  * in ilm_walker_cleanup() or ilm_delete().
18476  *
18477  * Flow of ioctls involving interface down/up
18478  *
18479  * The following is the sequence of an attempt to set some critical flags on an
18480  * up interface.
18481  * ip_sioctl_flags
18482  * ipif_down
18483  * wait for ipif to be quiescent
18484  * ipif_down_tail
18485  * ip_sioctl_flags_tail
18486  *
18487  * All set ioctls that involve down/up sequence would have a skeleton similar
18488  * to the above. All the *tail functions are called after the refcounts have
18489  * dropped to the appropriate values.
18490  *
18491  * The mechanism to quiesce an ipif is as follows.
18492  *
18493  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18494  * on the ipif. Callers either pass a flag requesting wait or the lookup
18495  *  functions will return NULL.
18496  *
18497  * Delete all ires referencing this ipif
18498  *
18499  * Any thread attempting to do an ipif_refhold on an ipif that has been
18500  * obtained thru a cached pointer will first make sure that
18501  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18502  * increment the refcount.
18503  *
18504  * The above guarantees that the ipif refcount will eventually come down to
18505  * zero and the ipif will quiesce, once all threads that currently hold a
18506  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18507  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18508  * have also been ire_inactive'd. i.e. when ipif_cnt_{ire, ill} and
18509  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18510  * in ip.h
18511  *
18512  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18513  *
18514  * Threads trying to lookup an ipif or ill can pass a flag requesting
18515  * wait and restart if the ipif / ill cannot be looked up currently.
18516  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18517  * failure if the ipif is currently undergoing an exclusive operation, and
18518  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18519  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18520  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18521  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18522  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18523  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18524  * until we release the ipsq_lock, even though the the ill/ipif state flags
18525  * can change after we drop the ill_lock.
18526  *
18527  * An attempt to send out a packet using an ipif that is currently
18528  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18529  * operation and restart it later when the exclusive condition on the ipif ends.
18530  * This is an example of not passing the wait flag to the lookup functions. For
18531  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18532  * out a multicast packet on that ipif will fail while the ipif is
18533  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18534  * currently IPIF_CHANGING will also fail.
18535  */
18536 int
18537 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18538 {
18539 	ill_t		*ill = ipif->ipif_ill;
18540 	phyint_t	*phyi;
18541 	conn_t		*connp;
18542 	boolean_t	success;
18543 	boolean_t	ipif_was_up = B_FALSE;
18544 	ip_stack_t	*ipst = ill->ill_ipst;
18545 
18546 	ASSERT(IAM_WRITER_IPIF(ipif));
18547 
18548 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18549 
18550 	if (ipif->ipif_flags & IPIF_UP) {
18551 		mutex_enter(&ill->ill_lock);
18552 		ipif->ipif_flags &= ~IPIF_UP;
18553 		ASSERT(ill->ill_ipif_up_count > 0);
18554 		--ill->ill_ipif_up_count;
18555 		mutex_exit(&ill->ill_lock);
18556 		ipif_was_up = B_TRUE;
18557 		/* Update status in SCTP's list */
18558 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18559 	}
18560 
18561 	/*
18562 	 * Blow away memberships we established in ipif_multicast_up().
18563 	 */
18564 	ipif_multicast_down(ipif);
18565 
18566 	/*
18567 	 * Remove from the mapping for __sin6_src_id. We insert only
18568 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18569 	 * stored as mapped addresses, we need to check for mapped
18570 	 * INADDR_ANY also.
18571 	 */
18572 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18573 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18574 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18575 		int err;
18576 
18577 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18578 		    ipif->ipif_zoneid, ipst);
18579 		if (err != 0) {
18580 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18581 		}
18582 	}
18583 
18584 	/*
18585 	 * Before we delete the ill from the group (if any), we need
18586 	 * to make sure that we delete all the routes dependent on
18587 	 * this and also any ipifs dependent on this ipif for
18588 	 * source address. We need to do before we delete from
18589 	 * the group because
18590 	 *
18591 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18592 	 *
18593 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18594 	 *    for re-doing source address selection. Note that
18595 	 *    ipif_select_source[_v6] called from
18596 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18597 	 *    because we have already marked down here i.e cleared
18598 	 *    IPIF_UP.
18599 	 */
18600 	if (ipif->ipif_isv6) {
18601 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18602 		    ipst);
18603 	} else {
18604 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18605 		    ipst);
18606 	}
18607 
18608 	/*
18609 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18610 	 * ires have been deleted above. Otherwise a thread could end up
18611 	 * caching an ire in a conn after we have finished the cleanup of the
18612 	 * conn. The caching is done after making sure that the ire is not yet
18613 	 * condemned. Also documented in the block comment above ip_output
18614 	 */
18615 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18616 	/* Also, delete the ires cached in SCTP */
18617 	sctp_ire_cache_flush(ipif);
18618 
18619 	/*
18620 	 * Update any other ipifs which have used "our" local address as
18621 	 * a source address. This entails removing and recreating IRE_INTERFACE
18622 	 * entries for such ipifs.
18623 	 */
18624 	if (ipif->ipif_isv6)
18625 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18626 	else
18627 		ipif_update_other_ipifs(ipif, ill->ill_group);
18628 
18629 	if (ipif_was_up) {
18630 		/*
18631 		 * Check whether it is last ipif to leave this group.
18632 		 * If this is the last ipif to leave, we should remove
18633 		 * this ill from the group as ipif_select_source will not
18634 		 * be able to find any useful ipifs if this ill is selected
18635 		 * for load balancing.
18636 		 *
18637 		 * For nameless groups, we should call ifgrp_delete if this
18638 		 * belongs to some group. As this ipif is going down, we may
18639 		 * need to reconstruct groups.
18640 		 */
18641 		phyi = ill->ill_phyint;
18642 		/*
18643 		 * If the phyint_groupname_len is 0, it may or may not
18644 		 * be in the nameless group. If the phyint_groupname_len is
18645 		 * not 0, then this ill should be part of some group.
18646 		 * As we always insert this ill in the group if
18647 		 * phyint_groupname_len is not zero when the first ipif
18648 		 * comes up (in ipif_up_done), it should be in a group
18649 		 * when the namelen is not 0.
18650 		 *
18651 		 * NOTE : When we delete the ill from the group,it will
18652 		 * blow away all the IRE_CACHES pointing either at this ipif or
18653 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18654 		 * should be pointing at this ill.
18655 		 */
18656 		ASSERT(phyi->phyint_groupname_len == 0 ||
18657 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18658 
18659 		if (phyi->phyint_groupname_len != 0) {
18660 			if (ill->ill_ipif_up_count == 0)
18661 				illgrp_delete(ill);
18662 		}
18663 
18664 		/*
18665 		 * If we have deleted some of the broadcast ires associated
18666 		 * with this ipif, we need to re-nominate somebody else if
18667 		 * the ires that we deleted were the nominated ones.
18668 		 */
18669 		if (ill->ill_group != NULL && !ill->ill_isv6)
18670 			ipif_renominate_bcast(ipif);
18671 	}
18672 
18673 	/*
18674 	 * neighbor-discovery or arp entries for this interface.
18675 	 */
18676 	ipif_ndp_down(ipif);
18677 
18678 	/*
18679 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18680 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18681 	 * and ill_delete -> ipif_free -> ipif_down
18682 	 */
18683 	if (mp == NULL) {
18684 		ASSERT(q == NULL);
18685 		return (0);
18686 	}
18687 
18688 	if (CONN_Q(q)) {
18689 		connp = Q_TO_CONN(q);
18690 		mutex_enter(&connp->conn_lock);
18691 	} else {
18692 		connp = NULL;
18693 	}
18694 	mutex_enter(&ill->ill_lock);
18695 	/*
18696 	 * Are there any ire's pointing to this ipif that are still active ?
18697 	 * If this is the last ipif going down, are there any ire's pointing
18698 	 * to this ill that are still active ?
18699 	 */
18700 	if (ipif_is_quiescent(ipif)) {
18701 		mutex_exit(&ill->ill_lock);
18702 		if (connp != NULL)
18703 			mutex_exit(&connp->conn_lock);
18704 		return (0);
18705 	}
18706 
18707 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18708 	    ill->ill_name, (void *)ill));
18709 	/*
18710 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18711 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18712 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18713 	 */
18714 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18715 	if (!success) {
18716 		/* The conn is closing. So just return */
18717 		ASSERT(connp != NULL);
18718 		mutex_exit(&ill->ill_lock);
18719 		mutex_exit(&connp->conn_lock);
18720 		return (EINTR);
18721 	}
18722 
18723 	mutex_exit(&ill->ill_lock);
18724 	if (connp != NULL)
18725 		mutex_exit(&connp->conn_lock);
18726 	return (EINPROGRESS);
18727 }
18728 
18729 void
18730 ipif_down_tail(ipif_t *ipif)
18731 {
18732 	ill_t	*ill = ipif->ipif_ill;
18733 
18734 	/*
18735 	 * Skip any loopback interface (null wq).
18736 	 * If this is the last logical interface on the ill
18737 	 * have ill_dl_down tell the driver we are gone (unbind)
18738 	 * Note that lun 0 can ipif_down even though
18739 	 * there are other logical units that are up.
18740 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18741 	 */
18742 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18743 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18744 	    ill->ill_dl_up) {
18745 		ill_dl_down(ill);
18746 	}
18747 	ill->ill_logical_down = 0;
18748 
18749 	/*
18750 	 * Have to be after removing the routes in ipif_down_delete_ire.
18751 	 */
18752 	if (ipif->ipif_isv6) {
18753 		if (ill->ill_flags & ILLF_XRESOLV)
18754 			ipif_arp_down(ipif);
18755 	} else {
18756 		ipif_arp_down(ipif);
18757 	}
18758 
18759 	ip_rts_ifmsg(ipif);
18760 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18761 }
18762 
18763 /*
18764  * Bring interface logically down without bringing the physical interface
18765  * down e.g. when the netmask is changed. This avoids long lasting link
18766  * negotiations between an ethernet interface and a certain switches.
18767  */
18768 static int
18769 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18770 {
18771 	/*
18772 	 * The ill_logical_down flag is a transient flag. It is set here
18773 	 * and is cleared once the down has completed in ipif_down_tail.
18774 	 * This flag does not indicate whether the ill stream is in the
18775 	 * DL_BOUND state with the driver. Instead this flag is used by
18776 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18777 	 * the driver. The state of the ill stream i.e. whether it is
18778 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18779 	 */
18780 	ipif->ipif_ill->ill_logical_down = 1;
18781 	return (ipif_down(ipif, q, mp));
18782 }
18783 
18784 /*
18785  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18786  * If the usesrc client ILL is already part of a usesrc group or not,
18787  * in either case a ire_stq with the matching usesrc client ILL will
18788  * locate the IRE's that need to be deleted. We want IREs to be created
18789  * with the new source address.
18790  */
18791 static void
18792 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18793 {
18794 	ill_t	*ucill = (ill_t *)ill_arg;
18795 
18796 	ASSERT(IAM_WRITER_ILL(ucill));
18797 
18798 	if (ire->ire_stq == NULL)
18799 		return;
18800 
18801 	if ((ire->ire_type == IRE_CACHE) &&
18802 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18803 		ire_delete(ire);
18804 }
18805 
18806 /*
18807  * ire_walk routine to delete every IRE dependent on the interface
18808  * address that is going down.	(Always called as writer.)
18809  * Works for both v4 and v6.
18810  * In addition for checking for ire_ipif matches it also checks for
18811  * IRE_CACHE entries which have the same source address as the
18812  * disappearing ipif since ipif_select_source might have picked
18813  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18814  * care of any IRE_INTERFACE with the disappearing source address.
18815  */
18816 static void
18817 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18818 {
18819 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18820 	ill_t *ire_ill;
18821 	ill_t *ipif_ill;
18822 
18823 	ASSERT(IAM_WRITER_IPIF(ipif));
18824 	if (ire->ire_ipif == NULL)
18825 		return;
18826 
18827 	/*
18828 	 * For IPv4, we derive source addresses for an IRE from ipif's
18829 	 * belonging to the same IPMP group as the IRE's outgoing
18830 	 * interface.  If an IRE's outgoing interface isn't in the
18831 	 * same IPMP group as a particular ipif, then that ipif
18832 	 * couldn't have been used as a source address for this IRE.
18833 	 *
18834 	 * For IPv6, source addresses are only restricted to the IPMP group
18835 	 * if the IRE is for a link-local address or a multicast address.
18836 	 * Otherwise, source addresses for an IRE can be chosen from
18837 	 * interfaces other than the the outgoing interface for that IRE.
18838 	 *
18839 	 * For source address selection details, see ipif_select_source()
18840 	 * and ipif_select_source_v6().
18841 	 */
18842 	if (ire->ire_ipversion == IPV4_VERSION ||
18843 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18844 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18845 		ire_ill = ire->ire_ipif->ipif_ill;
18846 		ipif_ill = ipif->ipif_ill;
18847 
18848 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18849 			return;
18850 		}
18851 	}
18852 
18853 
18854 	if (ire->ire_ipif != ipif) {
18855 		/*
18856 		 * Look for a matching source address.
18857 		 */
18858 		if (ire->ire_type != IRE_CACHE)
18859 			return;
18860 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18861 			return;
18862 
18863 		if (ire->ire_ipversion == IPV4_VERSION) {
18864 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18865 				return;
18866 		} else {
18867 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18868 			    &ipif->ipif_v6lcl_addr))
18869 				return;
18870 		}
18871 		ire_delete(ire);
18872 		return;
18873 	}
18874 	/*
18875 	 * ire_delete() will do an ire_flush_cache which will delete
18876 	 * all ire_ipif matches
18877 	 */
18878 	ire_delete(ire);
18879 }
18880 
18881 /*
18882  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18883  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18884  * 2) when an interface is brought up or down (on that ill).
18885  * This ensures that the IRE_CACHE entries don't retain stale source
18886  * address selection results.
18887  */
18888 void
18889 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18890 {
18891 	ill_t	*ill = (ill_t *)ill_arg;
18892 	ill_t	*ipif_ill;
18893 
18894 	ASSERT(IAM_WRITER_ILL(ill));
18895 	/*
18896 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18897 	 * Hence this should be IRE_CACHE.
18898 	 */
18899 	ASSERT(ire->ire_type == IRE_CACHE);
18900 
18901 	/*
18902 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18903 	 * We are only interested in IRE_CACHES that has borrowed
18904 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18905 	 * for which we need to look at ire_ipif->ipif_ill match
18906 	 * with ill.
18907 	 */
18908 	ASSERT(ire->ire_ipif != NULL);
18909 	ipif_ill = ire->ire_ipif->ipif_ill;
18910 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18911 	    ipif_ill->ill_group == ill->ill_group)) {
18912 		ire_delete(ire);
18913 	}
18914 }
18915 
18916 /*
18917  * Delete all the ire whose stq references ill_arg.
18918  */
18919 static void
18920 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18921 {
18922 	ill_t	*ill = (ill_t *)ill_arg;
18923 	ill_t	*ire_ill;
18924 
18925 	ASSERT(IAM_WRITER_ILL(ill));
18926 	/*
18927 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18928 	 * Hence this should be IRE_CACHE.
18929 	 */
18930 	ASSERT(ire->ire_type == IRE_CACHE);
18931 
18932 	/*
18933 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18934 	 * matches ill. We are only interested in IRE_CACHES that
18935 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18936 	 * filtering here.
18937 	 */
18938 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18939 
18940 	if (ire_ill == ill)
18941 		ire_delete(ire);
18942 }
18943 
18944 /*
18945  * This is called when an ill leaves the group. We want to delete
18946  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18947  * pointing at ill.
18948  */
18949 static void
18950 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18951 {
18952 	ill_t	*ill = (ill_t *)ill_arg;
18953 
18954 	ASSERT(IAM_WRITER_ILL(ill));
18955 	ASSERT(ill->ill_group == NULL);
18956 	/*
18957 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18958 	 * Hence this should be IRE_CACHE.
18959 	 */
18960 	ASSERT(ire->ire_type == IRE_CACHE);
18961 	/*
18962 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18963 	 * matches ill. We are interested in both.
18964 	 */
18965 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18966 	    (ire->ire_ipif->ipif_ill == ill));
18967 
18968 	ire_delete(ire);
18969 }
18970 
18971 /*
18972  * Initiate deallocate of an IPIF. Always called as writer. Called by
18973  * ill_delete or ip_sioctl_removeif.
18974  */
18975 static void
18976 ipif_free(ipif_t *ipif)
18977 {
18978 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18979 
18980 	ASSERT(IAM_WRITER_IPIF(ipif));
18981 
18982 	if (ipif->ipif_recovery_id != 0)
18983 		(void) untimeout(ipif->ipif_recovery_id);
18984 	ipif->ipif_recovery_id = 0;
18985 
18986 	/* Remove conn references */
18987 	reset_conn_ipif(ipif);
18988 
18989 	/*
18990 	 * Make sure we have valid net and subnet broadcast ire's for the
18991 	 * other ipif's which share them with this ipif.
18992 	 */
18993 	if (!ipif->ipif_isv6)
18994 		ipif_check_bcast_ires(ipif);
18995 
18996 	/*
18997 	 * Take down the interface. We can be called either from ill_delete
18998 	 * or from ip_sioctl_removeif.
18999 	 */
19000 	(void) ipif_down(ipif, NULL, NULL);
19001 
19002 	/*
19003 	 * Now that the interface is down, there's no chance it can still
19004 	 * become a duplicate.  Cancel any timer that may have been set while
19005 	 * tearing down.
19006 	 */
19007 	if (ipif->ipif_recovery_id != 0)
19008 		(void) untimeout(ipif->ipif_recovery_id);
19009 	ipif->ipif_recovery_id = 0;
19010 
19011 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19012 	/* Remove pointers to this ill in the multicast routing tables */
19013 	reset_mrt_vif_ipif(ipif);
19014 	rw_exit(&ipst->ips_ill_g_lock);
19015 }
19016 
19017 /*
19018  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19019  * also ill_move().
19020  */
19021 static void
19022 ipif_free_tail(ipif_t *ipif)
19023 {
19024 	mblk_t	*mp;
19025 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19026 
19027 	/*
19028 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19029 	 */
19030 	mutex_enter(&ipif->ipif_saved_ire_lock);
19031 	mp = ipif->ipif_saved_ire_mp;
19032 	ipif->ipif_saved_ire_mp = NULL;
19033 	mutex_exit(&ipif->ipif_saved_ire_lock);
19034 	freemsg(mp);
19035 
19036 	/*
19037 	 * Need to hold both ill_g_lock and ill_lock while
19038 	 * inserting or removing an ipif from the linked list
19039 	 * of ipifs hanging off the ill.
19040 	 */
19041 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19042 
19043 	ASSERT(ilm_walk_ipif(ipif) == 0);
19044 
19045 #ifdef DEBUG
19046 	ipif_trace_cleanup(ipif);
19047 #endif
19048 
19049 	/* Ask SCTP to take it out of it list */
19050 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19051 
19052 	/* Get it out of the ILL interface list. */
19053 	ipif_remove(ipif, B_TRUE);
19054 	rw_exit(&ipst->ips_ill_g_lock);
19055 
19056 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19057 
19058 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19059 	ASSERT(ipif->ipif_recovery_id == 0);
19060 
19061 	/* Free the memory. */
19062 	mi_free(ipif);
19063 }
19064 
19065 /*
19066  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19067  * is zero.
19068  */
19069 void
19070 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19071 {
19072 	char	lbuf[LIFNAMSIZ];
19073 	char	*name;
19074 	size_t	name_len;
19075 
19076 	buf[0] = '\0';
19077 	name = ipif->ipif_ill->ill_name;
19078 	name_len = ipif->ipif_ill->ill_name_length;
19079 	if (ipif->ipif_id != 0) {
19080 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19081 		    ipif->ipif_id);
19082 		name = lbuf;
19083 		name_len = mi_strlen(name) + 1;
19084 	}
19085 	len -= 1;
19086 	buf[len] = '\0';
19087 	len = MIN(len, name_len);
19088 	bcopy(name, buf, len);
19089 }
19090 
19091 /*
19092  * Find an IPIF based on the name passed in.  Names can be of the
19093  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19094  * The <phys> string can have forms like <dev><#> (e.g., le0),
19095  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19096  * When there is no colon, the implied unit id is zero. <phys> must
19097  * correspond to the name of an ILL.  (May be called as writer.)
19098  */
19099 static ipif_t *
19100 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19101     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19102     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19103 {
19104 	char	*cp;
19105 	char	*endp;
19106 	long	id;
19107 	ill_t	*ill;
19108 	ipif_t	*ipif;
19109 	uint_t	ire_type;
19110 	boolean_t did_alloc = B_FALSE;
19111 	ipsq_t	*ipsq;
19112 
19113 	if (error != NULL)
19114 		*error = 0;
19115 
19116 	/*
19117 	 * If the caller wants to us to create the ipif, make sure we have a
19118 	 * valid zoneid
19119 	 */
19120 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19121 
19122 	if (namelen == 0) {
19123 		if (error != NULL)
19124 			*error = ENXIO;
19125 		return (NULL);
19126 	}
19127 
19128 	*exists = B_FALSE;
19129 	/* Look for a colon in the name. */
19130 	endp = &name[namelen];
19131 	for (cp = endp; --cp > name; ) {
19132 		if (*cp == IPIF_SEPARATOR_CHAR)
19133 			break;
19134 	}
19135 
19136 	if (*cp == IPIF_SEPARATOR_CHAR) {
19137 		/*
19138 		 * Reject any non-decimal aliases for logical
19139 		 * interfaces. Aliases with leading zeroes
19140 		 * are also rejected as they introduce ambiguity
19141 		 * in the naming of the interfaces.
19142 		 * In order to confirm with existing semantics,
19143 		 * and to not break any programs/script relying
19144 		 * on that behaviour, if<0>:0 is considered to be
19145 		 * a valid interface.
19146 		 *
19147 		 * If alias has two or more digits and the first
19148 		 * is zero, fail.
19149 		 */
19150 		if (&cp[2] < endp && cp[1] == '0') {
19151 			if (error != NULL)
19152 				*error = EINVAL;
19153 			return (NULL);
19154 		}
19155 	}
19156 
19157 	if (cp <= name) {
19158 		cp = endp;
19159 	} else {
19160 		*cp = '\0';
19161 	}
19162 
19163 	/*
19164 	 * Look up the ILL, based on the portion of the name
19165 	 * before the slash. ill_lookup_on_name returns a held ill.
19166 	 * Temporary to check whether ill exists already. If so
19167 	 * ill_lookup_on_name will clear it.
19168 	 */
19169 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19170 	    q, mp, func, error, &did_alloc, ipst);
19171 	if (cp != endp)
19172 		*cp = IPIF_SEPARATOR_CHAR;
19173 	if (ill == NULL)
19174 		return (NULL);
19175 
19176 	/* Establish the unit number in the name. */
19177 	id = 0;
19178 	if (cp < endp && *endp == '\0') {
19179 		/* If there was a colon, the unit number follows. */
19180 		cp++;
19181 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19182 			ill_refrele(ill);
19183 			if (error != NULL)
19184 				*error = ENXIO;
19185 			return (NULL);
19186 		}
19187 	}
19188 
19189 	GRAB_CONN_LOCK(q);
19190 	mutex_enter(&ill->ill_lock);
19191 	/* Now see if there is an IPIF with this unit number. */
19192 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19193 		if (ipif->ipif_id == id) {
19194 			if (zoneid != ALL_ZONES &&
19195 			    zoneid != ipif->ipif_zoneid &&
19196 			    ipif->ipif_zoneid != ALL_ZONES) {
19197 				mutex_exit(&ill->ill_lock);
19198 				RELEASE_CONN_LOCK(q);
19199 				ill_refrele(ill);
19200 				if (error != NULL)
19201 					*error = ENXIO;
19202 				return (NULL);
19203 			}
19204 			/*
19205 			 * The block comment at the start of ipif_down
19206 			 * explains the use of the macros used below
19207 			 */
19208 			if (IPIF_CAN_LOOKUP(ipif)) {
19209 				ipif_refhold_locked(ipif);
19210 				mutex_exit(&ill->ill_lock);
19211 				if (!did_alloc)
19212 					*exists = B_TRUE;
19213 				/*
19214 				 * Drop locks before calling ill_refrele
19215 				 * since it can potentially call into
19216 				 * ipif_ill_refrele_tail which can end up
19217 				 * in trying to acquire any lock.
19218 				 */
19219 				RELEASE_CONN_LOCK(q);
19220 				ill_refrele(ill);
19221 				return (ipif);
19222 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19223 				ipsq = ill->ill_phyint->phyint_ipsq;
19224 				mutex_enter(&ipsq->ipsq_lock);
19225 				mutex_exit(&ill->ill_lock);
19226 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19227 				mutex_exit(&ipsq->ipsq_lock);
19228 				RELEASE_CONN_LOCK(q);
19229 				ill_refrele(ill);
19230 				if (error != NULL)
19231 					*error = EINPROGRESS;
19232 				return (NULL);
19233 			}
19234 		}
19235 	}
19236 	RELEASE_CONN_LOCK(q);
19237 
19238 	if (!do_alloc) {
19239 		mutex_exit(&ill->ill_lock);
19240 		ill_refrele(ill);
19241 		if (error != NULL)
19242 			*error = ENXIO;
19243 		return (NULL);
19244 	}
19245 
19246 	/*
19247 	 * If none found, atomically allocate and return a new one.
19248 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19249 	 * to support "receive only" use of lo0:1 etc. as is still done
19250 	 * below as an initial guess.
19251 	 * However, this is now likely to be overriden later in ipif_up_done()
19252 	 * when we know for sure what address has been configured on the
19253 	 * interface, since we might have more than one loopback interface
19254 	 * with a loopback address, e.g. in the case of zones, and all the
19255 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19256 	 */
19257 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19258 		ire_type = IRE_LOOPBACK;
19259 	else
19260 		ire_type = IRE_LOCAL;
19261 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19262 	if (ipif != NULL)
19263 		ipif_refhold_locked(ipif);
19264 	else if (error != NULL)
19265 		*error = ENOMEM;
19266 	mutex_exit(&ill->ill_lock);
19267 	ill_refrele(ill);
19268 	return (ipif);
19269 }
19270 
19271 /*
19272  * This routine is called whenever a new address comes up on an ipif.  If
19273  * we are configured to respond to address mask requests, then we are supposed
19274  * to broadcast an address mask reply at this time.  This routine is also
19275  * called if we are already up, but a netmask change is made.  This is legal
19276  * but might not make the system manager very popular.	(May be called
19277  * as writer.)
19278  */
19279 void
19280 ipif_mask_reply(ipif_t *ipif)
19281 {
19282 	icmph_t	*icmph;
19283 	ipha_t	*ipha;
19284 	mblk_t	*mp;
19285 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19286 
19287 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19288 
19289 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19290 		return;
19291 
19292 	/* ICMP mask reply is IPv4 only */
19293 	ASSERT(!ipif->ipif_isv6);
19294 	/* ICMP mask reply is not for a loopback interface */
19295 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19296 
19297 	mp = allocb(REPLY_LEN, BPRI_HI);
19298 	if (mp == NULL)
19299 		return;
19300 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19301 
19302 	ipha = (ipha_t *)mp->b_rptr;
19303 	bzero(ipha, REPLY_LEN);
19304 	*ipha = icmp_ipha;
19305 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19306 	ipha->ipha_src = ipif->ipif_src_addr;
19307 	ipha->ipha_dst = ipif->ipif_brd_addr;
19308 	ipha->ipha_length = htons(REPLY_LEN);
19309 	ipha->ipha_ident = 0;
19310 
19311 	icmph = (icmph_t *)&ipha[1];
19312 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19313 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19314 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19315 
19316 	put(ipif->ipif_wq, mp);
19317 
19318 #undef	REPLY_LEN
19319 }
19320 
19321 /*
19322  * When the mtu in the ipif changes, we call this routine through ire_walk
19323  * to update all the relevant IREs.
19324  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19325  */
19326 static void
19327 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19328 {
19329 	ipif_t *ipif = (ipif_t *)ipif_arg;
19330 
19331 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19332 		return;
19333 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19334 }
19335 
19336 /*
19337  * When the mtu in the ill changes, we call this routine through ire_walk
19338  * to update all the relevant IREs.
19339  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19340  */
19341 void
19342 ill_mtu_change(ire_t *ire, char *ill_arg)
19343 {
19344 	ill_t	*ill = (ill_t *)ill_arg;
19345 
19346 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19347 		return;
19348 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19349 }
19350 
19351 /*
19352  * Join the ipif specific multicast groups.
19353  * Must be called after a mapping has been set up in the resolver.  (Always
19354  * called as writer.)
19355  */
19356 void
19357 ipif_multicast_up(ipif_t *ipif)
19358 {
19359 	int err, index;
19360 	ill_t *ill;
19361 
19362 	ASSERT(IAM_WRITER_IPIF(ipif));
19363 
19364 	ill = ipif->ipif_ill;
19365 	index = ill->ill_phyint->phyint_ifindex;
19366 
19367 	ip1dbg(("ipif_multicast_up\n"));
19368 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19369 		return;
19370 
19371 	if (ipif->ipif_isv6) {
19372 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19373 			return;
19374 
19375 		/* Join the all hosts multicast address */
19376 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19377 		/*
19378 		 * Passing B_TRUE means we have to join the multicast
19379 		 * membership on this interface even though this is
19380 		 * FAILED. If we join on a different one in the group,
19381 		 * we will not be able to delete the membership later
19382 		 * as we currently don't track where we join when we
19383 		 * join within the kernel unlike applications where
19384 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19385 		 * for more on this.
19386 		 */
19387 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19388 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19389 		if (err != 0) {
19390 			ip0dbg(("ipif_multicast_up: "
19391 			    "all_hosts_mcast failed %d\n",
19392 			    err));
19393 			return;
19394 		}
19395 		/*
19396 		 * Enable multicast for the solicited node multicast address
19397 		 */
19398 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19399 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19400 
19401 			ipv6_multi.s6_addr32[3] |=
19402 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19403 
19404 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19405 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19406 			    NULL);
19407 			if (err != 0) {
19408 				ip0dbg(("ipif_multicast_up: solicited MC"
19409 				    " failed %d\n", err));
19410 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19411 				    ill, ill->ill_phyint->phyint_ifindex,
19412 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19413 				return;
19414 			}
19415 		}
19416 	} else {
19417 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19418 			return;
19419 
19420 		/* Join the all hosts multicast address */
19421 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19422 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19423 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19424 		if (err) {
19425 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19426 			return;
19427 		}
19428 	}
19429 	ipif->ipif_multicast_up = 1;
19430 }
19431 
19432 /*
19433  * Blow away any multicast groups that we joined in ipif_multicast_up().
19434  * (Explicit memberships are blown away in ill_leave_multicast() when the
19435  * ill is brought down.)
19436  */
19437 static void
19438 ipif_multicast_down(ipif_t *ipif)
19439 {
19440 	int err;
19441 
19442 	ASSERT(IAM_WRITER_IPIF(ipif));
19443 
19444 	ip1dbg(("ipif_multicast_down\n"));
19445 	if (!ipif->ipif_multicast_up)
19446 		return;
19447 
19448 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19449 
19450 	if (!ipif->ipif_isv6) {
19451 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19452 		    B_TRUE);
19453 		if (err != 0)
19454 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19455 
19456 		ipif->ipif_multicast_up = 0;
19457 		return;
19458 	}
19459 
19460 	/*
19461 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19462 	 * we should look for ilms on this ill rather than the ones that have
19463 	 * been failed over here.  They are here temporarily. As
19464 	 * ipif_multicast_up has joined on this ill, we should delete only
19465 	 * from this ill.
19466 	 */
19467 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19468 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19469 	    B_TRUE, B_TRUE);
19470 	if (err != 0) {
19471 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19472 		    err));
19473 	}
19474 	/*
19475 	 * Disable multicast for the solicited node multicast address
19476 	 */
19477 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19478 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19479 
19480 		ipv6_multi.s6_addr32[3] |=
19481 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19482 
19483 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19484 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19485 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19486 
19487 		if (err != 0) {
19488 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19489 			    err));
19490 		}
19491 	}
19492 
19493 	ipif->ipif_multicast_up = 0;
19494 }
19495 
19496 /*
19497  * Used when an interface comes up to recreate any extra routes on this
19498  * interface.
19499  */
19500 static ire_t **
19501 ipif_recover_ire(ipif_t *ipif)
19502 {
19503 	mblk_t	*mp;
19504 	ire_t	**ipif_saved_irep;
19505 	ire_t	**irep;
19506 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19507 
19508 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19509 	    ipif->ipif_id));
19510 
19511 	mutex_enter(&ipif->ipif_saved_ire_lock);
19512 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19513 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19514 	if (ipif_saved_irep == NULL) {
19515 		mutex_exit(&ipif->ipif_saved_ire_lock);
19516 		return (NULL);
19517 	}
19518 
19519 	irep = ipif_saved_irep;
19520 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19521 		ire_t		*ire;
19522 		queue_t		*rfq;
19523 		queue_t		*stq;
19524 		ifrt_t		*ifrt;
19525 		uchar_t		*src_addr;
19526 		uchar_t		*gateway_addr;
19527 		ushort_t	type;
19528 
19529 		/*
19530 		 * When the ire was initially created and then added in
19531 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19532 		 * in the case of a traditional interface route, or as one of
19533 		 * the IRE_OFFSUBNET types (with the exception of
19534 		 * IRE_HOST types ire which is created by icmp_redirect() and
19535 		 * which we don't need to save or recover).  In the case where
19536 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19537 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19538 		 * to satisfy software like GateD and Sun Cluster which creates
19539 		 * routes using the the loopback interface's address as a
19540 		 * gateway.
19541 		 *
19542 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19543 		 * ire_create() will be called in the same way here as
19544 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19545 		 * the route looks like a traditional interface route (where
19546 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19547 		 * the saved ifrt->ifrt_type.  This means that in the case where
19548 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19549 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19550 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19551 		 */
19552 		ifrt = (ifrt_t *)mp->b_rptr;
19553 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19554 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19555 			rfq = NULL;
19556 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19557 			    ? ipif->ipif_rq : ipif->ipif_wq;
19558 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19559 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19560 			    : (uint8_t *)&ipif->ipif_src_addr;
19561 			gateway_addr = NULL;
19562 			type = ipif->ipif_net_type;
19563 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19564 			/* Recover multiroute broadcast IRE. */
19565 			rfq = ipif->ipif_rq;
19566 			stq = ipif->ipif_wq;
19567 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19568 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19569 			    : (uint8_t *)&ipif->ipif_src_addr;
19570 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19571 			type = ifrt->ifrt_type;
19572 		} else {
19573 			rfq = NULL;
19574 			stq = NULL;
19575 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19576 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19577 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19578 			type = ifrt->ifrt_type;
19579 		}
19580 
19581 		/*
19582 		 * Create a copy of the IRE with the saved address and netmask.
19583 		 */
19584 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19585 		    "0x%x/0x%x\n",
19586 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19587 		    ntohl(ifrt->ifrt_addr),
19588 		    ntohl(ifrt->ifrt_mask)));
19589 		ire = ire_create(
19590 		    (uint8_t *)&ifrt->ifrt_addr,
19591 		    (uint8_t *)&ifrt->ifrt_mask,
19592 		    src_addr,
19593 		    gateway_addr,
19594 		    &ifrt->ifrt_max_frag,
19595 		    NULL,
19596 		    rfq,
19597 		    stq,
19598 		    type,
19599 		    ipif,
19600 		    0,
19601 		    0,
19602 		    0,
19603 		    ifrt->ifrt_flags,
19604 		    &ifrt->ifrt_iulp_info,
19605 		    NULL,
19606 		    NULL,
19607 		    ipst);
19608 
19609 		if (ire == NULL) {
19610 			mutex_exit(&ipif->ipif_saved_ire_lock);
19611 			kmem_free(ipif_saved_irep,
19612 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19613 			return (NULL);
19614 		}
19615 
19616 		/*
19617 		 * Some software (for example, GateD and Sun Cluster) attempts
19618 		 * to create (what amount to) IRE_PREFIX routes with the
19619 		 * loopback address as the gateway.  This is primarily done to
19620 		 * set up prefixes with the RTF_REJECT flag set (for example,
19621 		 * when generating aggregate routes.)
19622 		 *
19623 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19624 		 * IRE_LOOPBACK, then we map the request into a
19625 		 * IRE_IF_NORESOLVER.
19626 		 */
19627 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19628 			ire->ire_type = IRE_IF_NORESOLVER;
19629 		/*
19630 		 * ire held by ire_add, will be refreled' towards the
19631 		 * the end of ipif_up_done
19632 		 */
19633 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19634 		*irep = ire;
19635 		irep++;
19636 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19637 	}
19638 	mutex_exit(&ipif->ipif_saved_ire_lock);
19639 	return (ipif_saved_irep);
19640 }
19641 
19642 /*
19643  * Used to set the netmask and broadcast address to default values when the
19644  * interface is brought up.  (Always called as writer.)
19645  */
19646 static void
19647 ipif_set_default(ipif_t *ipif)
19648 {
19649 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19650 
19651 	if (!ipif->ipif_isv6) {
19652 		/*
19653 		 * Interface holds an IPv4 address. Default
19654 		 * mask is the natural netmask.
19655 		 */
19656 		if (!ipif->ipif_net_mask) {
19657 			ipaddr_t	v4mask;
19658 
19659 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19660 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19661 		}
19662 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19663 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19664 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19665 		} else {
19666 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19667 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19668 		}
19669 		/*
19670 		 * NOTE: SunOS 4.X does this even if the broadcast address
19671 		 * has been already set thus we do the same here.
19672 		 */
19673 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19674 			ipaddr_t	v4addr;
19675 
19676 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19677 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19678 		}
19679 	} else {
19680 		/*
19681 		 * Interface holds an IPv6-only address.  Default
19682 		 * mask is all-ones.
19683 		 */
19684 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19685 			ipif->ipif_v6net_mask = ipv6_all_ones;
19686 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19687 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19688 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19689 		} else {
19690 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19691 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19692 		}
19693 	}
19694 }
19695 
19696 /*
19697  * Return 0 if this address can be used as local address without causing
19698  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19699  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19700  * Special checks are needed to allow the same IPv6 link-local address
19701  * on different ills.
19702  * TODO: allowing the same site-local address on different ill's.
19703  */
19704 int
19705 ip_addr_availability_check(ipif_t *new_ipif)
19706 {
19707 	in6_addr_t our_v6addr;
19708 	ill_t *ill;
19709 	ipif_t *ipif;
19710 	ill_walk_context_t ctx;
19711 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19712 
19713 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19714 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19715 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19716 
19717 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19718 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19719 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19720 		return (0);
19721 
19722 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19723 
19724 	if (new_ipif->ipif_isv6)
19725 		ill = ILL_START_WALK_V6(&ctx, ipst);
19726 	else
19727 		ill = ILL_START_WALK_V4(&ctx, ipst);
19728 
19729 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19730 		for (ipif = ill->ill_ipif; ipif != NULL;
19731 		    ipif = ipif->ipif_next) {
19732 			if ((ipif == new_ipif) ||
19733 			    !(ipif->ipif_flags & IPIF_UP) ||
19734 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19735 				continue;
19736 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19737 			    &our_v6addr)) {
19738 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19739 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19740 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19741 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19742 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19743 				    new_ipif->ipif_ill != ill)
19744 					continue;
19745 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19746 				    new_ipif->ipif_ill != ill)
19747 					continue;
19748 				else if (new_ipif->ipif_zoneid !=
19749 				    ipif->ipif_zoneid &&
19750 				    ipif->ipif_zoneid != ALL_ZONES &&
19751 				    IS_LOOPBACK(ill))
19752 					continue;
19753 				else if (new_ipif->ipif_ill == ill)
19754 					return (EADDRINUSE);
19755 				else
19756 					return (EADDRNOTAVAIL);
19757 			}
19758 		}
19759 	}
19760 
19761 	return (0);
19762 }
19763 
19764 /*
19765  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19766  * IREs for the ipif.
19767  * When the routine returns EINPROGRESS then mp has been consumed and
19768  * the ioctl will be acked from ip_rput_dlpi.
19769  */
19770 static int
19771 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19772 {
19773 	ill_t	*ill = ipif->ipif_ill;
19774 	boolean_t isv6 = ipif->ipif_isv6;
19775 	int	err = 0;
19776 	boolean_t success;
19777 
19778 	ASSERT(IAM_WRITER_IPIF(ipif));
19779 
19780 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19781 
19782 	/* Shouldn't get here if it is already up. */
19783 	if (ipif->ipif_flags & IPIF_UP)
19784 		return (EALREADY);
19785 
19786 	/* Skip arp/ndp for any loopback interface. */
19787 	if (ill->ill_wq != NULL) {
19788 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19789 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19790 
19791 		if (!ill->ill_dl_up) {
19792 			/*
19793 			 * ill_dl_up is not yet set. i.e. we are yet to
19794 			 * DL_BIND with the driver and this is the first
19795 			 * logical interface on the ill to become "up".
19796 			 * Tell the driver to get going (via DL_BIND_REQ).
19797 			 * Note that changing "significant" IFF_ flags
19798 			 * address/netmask etc cause a down/up dance, but
19799 			 * does not cause an unbind (DL_UNBIND) with the driver
19800 			 */
19801 			return (ill_dl_up(ill, ipif, mp, q));
19802 		}
19803 
19804 		/*
19805 		 * ipif_resolver_up may end up sending an
19806 		 * AR_INTERFACE_UP message to ARP, which would, in
19807 		 * turn send a DLPI message to the driver. ioctls are
19808 		 * serialized and so we cannot send more than one
19809 		 * interface up message at a time. If ipif_resolver_up
19810 		 * does send an interface up message to ARP, we get
19811 		 * EINPROGRESS and we will complete in ip_arp_done.
19812 		 */
19813 
19814 		ASSERT(connp != NULL || !CONN_Q(q));
19815 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19816 		if (connp != NULL)
19817 			mutex_enter(&connp->conn_lock);
19818 		mutex_enter(&ill->ill_lock);
19819 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19820 		mutex_exit(&ill->ill_lock);
19821 		if (connp != NULL)
19822 			mutex_exit(&connp->conn_lock);
19823 		if (!success)
19824 			return (EINTR);
19825 
19826 		/*
19827 		 * Crank up IPv6 neighbor discovery
19828 		 * Unlike ARP, this should complete when
19829 		 * ipif_ndp_up returns. However, for
19830 		 * ILLF_XRESOLV interfaces we also send a
19831 		 * AR_INTERFACE_UP to the external resolver.
19832 		 * That ioctl will complete in ip_rput.
19833 		 */
19834 		if (isv6) {
19835 			err = ipif_ndp_up(ipif);
19836 			if (err != 0) {
19837 				if (err != EINPROGRESS)
19838 					mp = ipsq_pending_mp_get(ipsq, &connp);
19839 				return (err);
19840 			}
19841 		}
19842 		/* Now, ARP */
19843 		err = ipif_resolver_up(ipif, Res_act_initial);
19844 		if (err == EINPROGRESS) {
19845 			/* We will complete it in ip_arp_done */
19846 			return (err);
19847 		}
19848 		mp = ipsq_pending_mp_get(ipsq, &connp);
19849 		ASSERT(mp != NULL);
19850 		if (err != 0)
19851 			return (err);
19852 	} else {
19853 		/*
19854 		 * Interfaces without underlying hardware don't do duplicate
19855 		 * address detection.
19856 		 */
19857 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19858 		ipif->ipif_addr_ready = 1;
19859 	}
19860 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19861 }
19862 
19863 /*
19864  * Perform a bind for the physical device.
19865  * When the routine returns EINPROGRESS then mp has been consumed and
19866  * the ioctl will be acked from ip_rput_dlpi.
19867  * Allocate an unbind message and save it until ipif_down.
19868  */
19869 static int
19870 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19871 {
19872 	areq_t	*areq;
19873 	mblk_t	*areq_mp = NULL;
19874 	mblk_t	*bind_mp = NULL;
19875 	mblk_t	*unbind_mp = NULL;
19876 	conn_t	*connp;
19877 	boolean_t success;
19878 	uint16_t sap_addr;
19879 
19880 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19881 	ASSERT(IAM_WRITER_ILL(ill));
19882 	ASSERT(mp != NULL);
19883 
19884 	/* Create a resolver cookie for ARP */
19885 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19886 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19887 		if (areq_mp == NULL)
19888 			return (ENOMEM);
19889 
19890 		freemsg(ill->ill_resolver_mp);
19891 		ill->ill_resolver_mp = areq_mp;
19892 		areq = (areq_t *)areq_mp->b_rptr;
19893 		sap_addr = ill->ill_sap;
19894 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19895 	}
19896 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19897 	    DL_BIND_REQ);
19898 	if (bind_mp == NULL)
19899 		goto bad;
19900 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19901 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19902 
19903 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19904 	if (unbind_mp == NULL)
19905 		goto bad;
19906 
19907 	/*
19908 	 * Record state needed to complete this operation when the
19909 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19910 	 */
19911 	ASSERT(WR(q)->q_next == NULL);
19912 	connp = Q_TO_CONN(q);
19913 
19914 	mutex_enter(&connp->conn_lock);
19915 	mutex_enter(&ipif->ipif_ill->ill_lock);
19916 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19917 	mutex_exit(&ipif->ipif_ill->ill_lock);
19918 	mutex_exit(&connp->conn_lock);
19919 	if (!success)
19920 		goto bad;
19921 
19922 	/*
19923 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19924 	 * the interface goes down.
19925 	 */
19926 	ASSERT(ill->ill_unbind_mp == NULL);
19927 	ill->ill_unbind_mp = unbind_mp;
19928 
19929 	ill_dlpi_send(ill, bind_mp);
19930 	/* Send down link-layer capabilities probe if not already done. */
19931 	ill_capability_probe(ill);
19932 
19933 	/*
19934 	 * Sysid used to rely on the fact that netboots set domainname
19935 	 * and the like. Now that miniroot boots aren't strictly netboots
19936 	 * and miniroot network configuration is driven from userland
19937 	 * these things still need to be set. This situation can be detected
19938 	 * by comparing the interface being configured here to the one
19939 	 * dhcifname was set to reference by the boot loader. Once sysid is
19940 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19941 	 */
19942 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
19943 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
19944 	    (strlen(srpc_domain) == 0)) {
19945 		if (dhcpinit() != 0)
19946 			cmn_err(CE_WARN, "no cached dhcp response");
19947 	}
19948 
19949 	/*
19950 	 * This operation will complete in ip_rput_dlpi with either
19951 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19952 	 */
19953 	return (EINPROGRESS);
19954 bad:
19955 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19956 	/*
19957 	 * We don't have to check for possible removal from illgrp
19958 	 * as we have not yet inserted in illgrp. For groups
19959 	 * without names, this ipif is still not UP and hence
19960 	 * this could not have possibly had any influence in forming
19961 	 * groups.
19962 	 */
19963 
19964 	freemsg(bind_mp);
19965 	freemsg(unbind_mp);
19966 	return (ENOMEM);
19967 }
19968 
19969 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19970 
19971 /*
19972  * DLPI and ARP is up.
19973  * Create all the IREs associated with an interface bring up multicast.
19974  * Set the interface flag and finish other initialization
19975  * that potentially had to be differed to after DL_BIND_ACK.
19976  */
19977 int
19978 ipif_up_done(ipif_t *ipif)
19979 {
19980 	ire_t	*ire_array[20];
19981 	ire_t	**irep = ire_array;
19982 	ire_t	**irep1;
19983 	ipaddr_t net_mask = 0;
19984 	ipaddr_t subnet_mask, route_mask;
19985 	ill_t	*ill = ipif->ipif_ill;
19986 	queue_t	*stq;
19987 	ipif_t	 *src_ipif;
19988 	ipif_t   *tmp_ipif;
19989 	boolean_t	flush_ire_cache = B_TRUE;
19990 	int	err = 0;
19991 	phyint_t *phyi;
19992 	ire_t	**ipif_saved_irep = NULL;
19993 	int ipif_saved_ire_cnt;
19994 	int	cnt;
19995 	boolean_t	src_ipif_held = B_FALSE;
19996 	boolean_t	ire_added = B_FALSE;
19997 	boolean_t	loopback = B_FALSE;
19998 	ip_stack_t	*ipst = ill->ill_ipst;
19999 
20000 	ip1dbg(("ipif_up_done(%s:%u)\n",
20001 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20002 	/* Check if this is a loopback interface */
20003 	if (ipif->ipif_ill->ill_wq == NULL)
20004 		loopback = B_TRUE;
20005 
20006 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20007 	/*
20008 	 * If all other interfaces for this ill are down or DEPRECATED,
20009 	 * or otherwise unsuitable for source address selection, remove
20010 	 * any IRE_CACHE entries for this ill to make sure source
20011 	 * address selection gets to take this new ipif into account.
20012 	 * No need to hold ill_lock while traversing the ipif list since
20013 	 * we are writer
20014 	 */
20015 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20016 	    tmp_ipif = tmp_ipif->ipif_next) {
20017 		if (((tmp_ipif->ipif_flags &
20018 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20019 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20020 		    (tmp_ipif == ipif))
20021 			continue;
20022 		/* first useable pre-existing interface */
20023 		flush_ire_cache = B_FALSE;
20024 		break;
20025 	}
20026 	if (flush_ire_cache)
20027 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20028 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20029 
20030 	/*
20031 	 * Figure out which way the send-to queue should go.  Only
20032 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20033 	 * should show up here.
20034 	 */
20035 	switch (ill->ill_net_type) {
20036 	case IRE_IF_RESOLVER:
20037 		stq = ill->ill_rq;
20038 		break;
20039 	case IRE_IF_NORESOLVER:
20040 	case IRE_LOOPBACK:
20041 		stq = ill->ill_wq;
20042 		break;
20043 	default:
20044 		return (EINVAL);
20045 	}
20046 
20047 	if (IS_LOOPBACK(ill)) {
20048 		/*
20049 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20050 		 * ipif_lookup_on_name(), but in the case of zones we can have
20051 		 * several loopback addresses on lo0. So all the interfaces with
20052 		 * loopback addresses need to be marked IRE_LOOPBACK.
20053 		 */
20054 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20055 		    htonl(INADDR_LOOPBACK))
20056 			ipif->ipif_ire_type = IRE_LOOPBACK;
20057 		else
20058 			ipif->ipif_ire_type = IRE_LOCAL;
20059 	}
20060 
20061 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20062 		/*
20063 		 * Can't use our source address. Select a different
20064 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20065 		 */
20066 		src_ipif = ipif_select_source(ipif->ipif_ill,
20067 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20068 		if (src_ipif == NULL)
20069 			src_ipif = ipif;	/* Last resort */
20070 		else
20071 			src_ipif_held = B_TRUE;
20072 	} else {
20073 		src_ipif = ipif;
20074 	}
20075 
20076 	/* Create all the IREs associated with this interface */
20077 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20078 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20079 
20080 		/*
20081 		 * If we're on a labeled system then make sure that zone-
20082 		 * private addresses have proper remote host database entries.
20083 		 */
20084 		if (is_system_labeled() &&
20085 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20086 		    !tsol_check_interface_address(ipif))
20087 			return (EINVAL);
20088 
20089 		/* Register the source address for __sin6_src_id */
20090 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20091 		    ipif->ipif_zoneid, ipst);
20092 		if (err != 0) {
20093 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20094 			return (err);
20095 		}
20096 
20097 		/* If the interface address is set, create the local IRE. */
20098 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20099 		    (void *)ipif,
20100 		    ipif->ipif_ire_type,
20101 		    ntohl(ipif->ipif_lcl_addr)));
20102 		*irep++ = ire_create(
20103 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20104 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20105 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20106 		    NULL,				/* no gateway */
20107 		    &ip_loopback_mtuplus,		/* max frag size */
20108 		    NULL,
20109 		    ipif->ipif_rq,			/* recv-from queue */
20110 		    NULL,				/* no send-to queue */
20111 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20112 		    ipif,
20113 		    0,
20114 		    0,
20115 		    0,
20116 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20117 		    RTF_PRIVATE : 0,
20118 		    &ire_uinfo_null,
20119 		    NULL,
20120 		    NULL,
20121 		    ipst);
20122 	} else {
20123 		ip1dbg((
20124 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20125 		    ipif->ipif_ire_type,
20126 		    ntohl(ipif->ipif_lcl_addr),
20127 		    (uint_t)ipif->ipif_flags));
20128 	}
20129 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20130 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20131 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20132 	} else {
20133 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20134 	}
20135 
20136 	subnet_mask = ipif->ipif_net_mask;
20137 
20138 	/*
20139 	 * If mask was not specified, use natural netmask of
20140 	 * interface address. Also, store this mask back into the
20141 	 * ipif struct.
20142 	 */
20143 	if (subnet_mask == 0) {
20144 		subnet_mask = net_mask;
20145 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20146 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20147 		    ipif->ipif_v6subnet);
20148 	}
20149 
20150 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20151 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20152 	    ipif->ipif_subnet != INADDR_ANY) {
20153 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20154 
20155 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20156 			route_mask = IP_HOST_MASK;
20157 		} else {
20158 			route_mask = subnet_mask;
20159 		}
20160 
20161 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20162 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20163 		    (void *)ipif, (void *)ill,
20164 		    ill->ill_net_type,
20165 		    ntohl(ipif->ipif_subnet)));
20166 		*irep++ = ire_create(
20167 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20168 		    (uchar_t *)&route_mask,		/* mask */
20169 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20170 		    NULL,				/* no gateway */
20171 		    &ipif->ipif_mtu,			/* max frag */
20172 		    NULL,
20173 		    NULL,				/* no recv queue */
20174 		    stq,				/* send-to queue */
20175 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20176 		    ipif,
20177 		    0,
20178 		    0,
20179 		    0,
20180 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20181 		    &ire_uinfo_null,
20182 		    NULL,
20183 		    NULL,
20184 		    ipst);
20185 	}
20186 
20187 	/*
20188 	 * Create any necessary broadcast IREs.
20189 	 */
20190 	if (ipif->ipif_flags & IPIF_BROADCAST)
20191 		irep = ipif_create_bcast_ires(ipif, irep);
20192 
20193 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20194 
20195 	/* If an earlier ire_create failed, get out now */
20196 	for (irep1 = irep; irep1 > ire_array; ) {
20197 		irep1--;
20198 		if (*irep1 == NULL) {
20199 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20200 			err = ENOMEM;
20201 			goto bad;
20202 		}
20203 	}
20204 
20205 	/*
20206 	 * Need to atomically check for ip_addr_availablity_check
20207 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20208 	 * from group also.The ill_g_lock is grabbed as reader
20209 	 * just to make sure no new ills or new ipifs are being added
20210 	 * to the system while we are checking the uniqueness of addresses.
20211 	 */
20212 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20213 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20214 	/* Mark it up, and increment counters. */
20215 	ipif->ipif_flags |= IPIF_UP;
20216 	ill->ill_ipif_up_count++;
20217 	err = ip_addr_availability_check(ipif);
20218 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20219 	rw_exit(&ipst->ips_ill_g_lock);
20220 
20221 	if (err != 0) {
20222 		/*
20223 		 * Our address may already be up on the same ill. In this case,
20224 		 * the ARP entry for our ipif replaced the one for the other
20225 		 * ipif. So we don't want to delete it (otherwise the other ipif
20226 		 * would be unable to send packets).
20227 		 * ip_addr_availability_check() identifies this case for us and
20228 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20229 		 * which is the expected error code.
20230 		 */
20231 		if (err == EADDRINUSE) {
20232 			freemsg(ipif->ipif_arp_del_mp);
20233 			ipif->ipif_arp_del_mp = NULL;
20234 			err = EADDRNOTAVAIL;
20235 		}
20236 		ill->ill_ipif_up_count--;
20237 		ipif->ipif_flags &= ~IPIF_UP;
20238 		goto bad;
20239 	}
20240 
20241 	/*
20242 	 * Add in all newly created IREs.  ire_create_bcast() has
20243 	 * already checked for duplicates of the IRE_BROADCAST type.
20244 	 * We want to add before we call ifgrp_insert which wants
20245 	 * to know whether IRE_IF_RESOLVER exists or not.
20246 	 *
20247 	 * NOTE : We refrele the ire though we may branch to "bad"
20248 	 *	  later on where we do ire_delete. This is okay
20249 	 *	  because nobody can delete it as we are running
20250 	 *	  exclusively.
20251 	 */
20252 	for (irep1 = irep; irep1 > ire_array; ) {
20253 		irep1--;
20254 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20255 		/*
20256 		 * refheld by ire_add. refele towards the end of the func
20257 		 */
20258 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20259 	}
20260 	ire_added = B_TRUE;
20261 	/*
20262 	 * Form groups if possible.
20263 	 *
20264 	 * If we are supposed to be in a ill_group with a name, insert it
20265 	 * now as we know that at least one ipif is UP. Otherwise form
20266 	 * nameless groups.
20267 	 *
20268 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20269 	 * this ipif into the appropriate interface group, or create a
20270 	 * new one. If this is already in a nameless group, we try to form
20271 	 * a bigger group looking at other ills potentially sharing this
20272 	 * ipif's prefix.
20273 	 */
20274 	phyi = ill->ill_phyint;
20275 	if (phyi->phyint_groupname_len != 0) {
20276 		ASSERT(phyi->phyint_groupname != NULL);
20277 		if (ill->ill_ipif_up_count == 1) {
20278 			ASSERT(ill->ill_group == NULL);
20279 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20280 			    phyi->phyint_groupname, NULL, B_TRUE);
20281 			if (err != 0) {
20282 				ip1dbg(("ipif_up_done: illgrp allocation "
20283 				    "failed, error %d\n", err));
20284 				goto bad;
20285 			}
20286 		}
20287 		ASSERT(ill->ill_group != NULL);
20288 	}
20289 
20290 	/*
20291 	 * When this is part of group, we need to make sure that
20292 	 * any broadcast ires created because of this ipif coming
20293 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20294 	 * so that we don't receive duplicate broadcast packets.
20295 	 */
20296 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20297 		ipif_renominate_bcast(ipif);
20298 
20299 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20300 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20301 	ipif_saved_irep = ipif_recover_ire(ipif);
20302 
20303 	if (!loopback) {
20304 		/*
20305 		 * If the broadcast address has been set, make sure it makes
20306 		 * sense based on the interface address.
20307 		 * Only match on ill since we are sharing broadcast addresses.
20308 		 */
20309 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20310 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20311 			ire_t	*ire;
20312 
20313 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20314 			    IRE_BROADCAST, ipif, ALL_ZONES,
20315 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20316 
20317 			if (ire == NULL) {
20318 				/*
20319 				 * If there isn't a matching broadcast IRE,
20320 				 * revert to the default for this netmask.
20321 				 */
20322 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20323 				mutex_enter(&ipif->ipif_ill->ill_lock);
20324 				ipif_set_default(ipif);
20325 				mutex_exit(&ipif->ipif_ill->ill_lock);
20326 			} else {
20327 				ire_refrele(ire);
20328 			}
20329 		}
20330 
20331 	}
20332 
20333 	/* This is the first interface on this ill */
20334 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20335 		/*
20336 		 * Need to recover all multicast memberships in the driver.
20337 		 * This had to be deferred until we had attached.
20338 		 */
20339 		ill_recover_multicast(ill);
20340 	}
20341 	/* Join the allhosts multicast address */
20342 	ipif_multicast_up(ipif);
20343 
20344 	if (!loopback) {
20345 		/*
20346 		 * See whether anybody else would benefit from the
20347 		 * new ipif that we added. We call this always rather
20348 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20349 		 * ipif is for the benefit of illgrp_insert (done above)
20350 		 * which does not do source address selection as it does
20351 		 * not want to re-create interface routes that we are
20352 		 * having reference to it here.
20353 		 */
20354 		ill_update_source_selection(ill);
20355 	}
20356 
20357 	for (irep1 = irep; irep1 > ire_array; ) {
20358 		irep1--;
20359 		if (*irep1 != NULL) {
20360 			/* was held in ire_add */
20361 			ire_refrele(*irep1);
20362 		}
20363 	}
20364 
20365 	cnt = ipif_saved_ire_cnt;
20366 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20367 		if (*irep1 != NULL) {
20368 			/* was held in ire_add */
20369 			ire_refrele(*irep1);
20370 		}
20371 	}
20372 
20373 	if (!loopback && ipif->ipif_addr_ready) {
20374 		/* Broadcast an address mask reply. */
20375 		ipif_mask_reply(ipif);
20376 	}
20377 	if (ipif_saved_irep != NULL) {
20378 		kmem_free(ipif_saved_irep,
20379 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20380 	}
20381 	if (src_ipif_held)
20382 		ipif_refrele(src_ipif);
20383 
20384 	/*
20385 	 * This had to be deferred until we had bound.  Tell routing sockets and
20386 	 * others that this interface is up if it looks like the address has
20387 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20388 	 * duplicate address detection to do its thing.
20389 	 */
20390 	if (ipif->ipif_addr_ready) {
20391 		ip_rts_ifmsg(ipif);
20392 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20393 		/* Let SCTP update the status for this ipif */
20394 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20395 	}
20396 	return (0);
20397 
20398 bad:
20399 	ip1dbg(("ipif_up_done: FAILED \n"));
20400 	/*
20401 	 * We don't have to bother removing from ill groups because
20402 	 *
20403 	 * 1) For groups with names, we insert only when the first ipif
20404 	 *    comes up. In that case if it fails, it will not be in any
20405 	 *    group. So, we need not try to remove for that case.
20406 	 *
20407 	 * 2) For groups without names, either we tried to insert ipif_ill
20408 	 *    in a group as singleton or found some other group to become
20409 	 *    a bigger group. For the former, if it fails we don't have
20410 	 *    anything to do as ipif_ill is not in the group and for the
20411 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20412 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20413 	 */
20414 	while (irep > ire_array) {
20415 		irep--;
20416 		if (*irep != NULL) {
20417 			ire_delete(*irep);
20418 			if (ire_added)
20419 				ire_refrele(*irep);
20420 		}
20421 	}
20422 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20423 
20424 	if (ipif_saved_irep != NULL) {
20425 		kmem_free(ipif_saved_irep,
20426 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20427 	}
20428 	if (src_ipif_held)
20429 		ipif_refrele(src_ipif);
20430 
20431 	ipif_arp_down(ipif);
20432 	return (err);
20433 }
20434 
20435 /*
20436  * Turn off the ARP with the ILLF_NOARP flag.
20437  */
20438 static int
20439 ill_arp_off(ill_t *ill)
20440 {
20441 	mblk_t	*arp_off_mp = NULL;
20442 	mblk_t	*arp_on_mp = NULL;
20443 
20444 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20445 
20446 	ASSERT(IAM_WRITER_ILL(ill));
20447 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20448 
20449 	/*
20450 	 * If the on message is still around we've already done
20451 	 * an arp_off without doing an arp_on thus there is no
20452 	 * work needed.
20453 	 */
20454 	if (ill->ill_arp_on_mp != NULL)
20455 		return (0);
20456 
20457 	/*
20458 	 * Allocate an ARP on message (to be saved) and an ARP off message
20459 	 */
20460 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20461 	if (!arp_off_mp)
20462 		return (ENOMEM);
20463 
20464 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20465 	if (!arp_on_mp)
20466 		goto failed;
20467 
20468 	ASSERT(ill->ill_arp_on_mp == NULL);
20469 	ill->ill_arp_on_mp = arp_on_mp;
20470 
20471 	/* Send an AR_INTERFACE_OFF request */
20472 	putnext(ill->ill_rq, arp_off_mp);
20473 	return (0);
20474 failed:
20475 
20476 	if (arp_off_mp)
20477 		freemsg(arp_off_mp);
20478 	return (ENOMEM);
20479 }
20480 
20481 /*
20482  * Turn on ARP by turning off the ILLF_NOARP flag.
20483  */
20484 static int
20485 ill_arp_on(ill_t *ill)
20486 {
20487 	mblk_t	*mp;
20488 
20489 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20490 
20491 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20492 
20493 	ASSERT(IAM_WRITER_ILL(ill));
20494 	/*
20495 	 * Send an AR_INTERFACE_ON request if we have already done
20496 	 * an arp_off (which allocated the message).
20497 	 */
20498 	if (ill->ill_arp_on_mp != NULL) {
20499 		mp = ill->ill_arp_on_mp;
20500 		ill->ill_arp_on_mp = NULL;
20501 		putnext(ill->ill_rq, mp);
20502 	}
20503 	return (0);
20504 }
20505 
20506 /*
20507  * Called after either deleting ill from the group or when setting
20508  * FAILED or STANDBY on the interface.
20509  */
20510 static void
20511 illgrp_reset_schednext(ill_t *ill)
20512 {
20513 	ill_group_t *illgrp;
20514 	ill_t *save_ill;
20515 
20516 	ASSERT(IAM_WRITER_ILL(ill));
20517 	/*
20518 	 * When called from illgrp_delete, ill_group will be non-NULL.
20519 	 * But when called from ip_sioctl_flags, it could be NULL if
20520 	 * somebody is setting FAILED/INACTIVE on some interface which
20521 	 * is not part of a group.
20522 	 */
20523 	illgrp = ill->ill_group;
20524 	if (illgrp == NULL)
20525 		return;
20526 	if (illgrp->illgrp_ill_schednext != ill)
20527 		return;
20528 
20529 	illgrp->illgrp_ill_schednext = NULL;
20530 	save_ill = ill;
20531 	/*
20532 	 * Choose a good ill to be the next one for
20533 	 * outbound traffic. As the flags FAILED/STANDBY is
20534 	 * not yet marked when called from ip_sioctl_flags,
20535 	 * we check for ill separately.
20536 	 */
20537 	for (ill = illgrp->illgrp_ill; ill != NULL;
20538 	    ill = ill->ill_group_next) {
20539 		if ((ill != save_ill) &&
20540 		    !(ill->ill_phyint->phyint_flags &
20541 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20542 			illgrp->illgrp_ill_schednext = ill;
20543 			return;
20544 		}
20545 	}
20546 }
20547 
20548 /*
20549  * Given an ill, find the next ill in the group to be scheduled.
20550  * (This should be called by ip_newroute() before ire_create().)
20551  * The passed in ill may be pulled out of the group, after we have picked
20552  * up a different outgoing ill from the same group. However ire add will
20553  * atomically check this.
20554  */
20555 ill_t *
20556 illgrp_scheduler(ill_t *ill)
20557 {
20558 	ill_t *retill;
20559 	ill_group_t *illgrp;
20560 	int illcnt;
20561 	int i;
20562 	uint64_t flags;
20563 	ip_stack_t	*ipst = ill->ill_ipst;
20564 
20565 	/*
20566 	 * We don't use a lock to check for the ill_group. If this ill
20567 	 * is currently being inserted we may end up just returning this
20568 	 * ill itself. That is ok.
20569 	 */
20570 	if (ill->ill_group == NULL) {
20571 		ill_refhold(ill);
20572 		return (ill);
20573 	}
20574 
20575 	/*
20576 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20577 	 * a set of stable ills. No ill can be added or deleted or change
20578 	 * group while we hold the reader lock.
20579 	 */
20580 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20581 	if ((illgrp = ill->ill_group) == NULL) {
20582 		rw_exit(&ipst->ips_ill_g_lock);
20583 		ill_refhold(ill);
20584 		return (ill);
20585 	}
20586 
20587 	illcnt = illgrp->illgrp_ill_count;
20588 	mutex_enter(&illgrp->illgrp_lock);
20589 	retill = illgrp->illgrp_ill_schednext;
20590 
20591 	if (retill == NULL)
20592 		retill = illgrp->illgrp_ill;
20593 
20594 	/*
20595 	 * We do a circular search beginning at illgrp_ill_schednext
20596 	 * or illgrp_ill. We don't check the flags against the ill lock
20597 	 * since it can change anytime. The ire creation will be atomic
20598 	 * and will fail if the ill is FAILED or OFFLINE.
20599 	 */
20600 	for (i = 0; i < illcnt; i++) {
20601 		flags = retill->ill_phyint->phyint_flags;
20602 
20603 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20604 		    ILL_CAN_LOOKUP(retill)) {
20605 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20606 			ill_refhold(retill);
20607 			break;
20608 		}
20609 		retill = retill->ill_group_next;
20610 		if (retill == NULL)
20611 			retill = illgrp->illgrp_ill;
20612 	}
20613 	mutex_exit(&illgrp->illgrp_lock);
20614 	rw_exit(&ipst->ips_ill_g_lock);
20615 
20616 	return (i == illcnt ? NULL : retill);
20617 }
20618 
20619 /*
20620  * Checks for availbility of a usable source address (if there is one) when the
20621  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20622  * this selection is done regardless of the destination.
20623  */
20624 boolean_t
20625 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20626 {
20627 	uint_t	ifindex;
20628 	ipif_t	*ipif = NULL;
20629 	ill_t	*uill;
20630 	boolean_t isv6;
20631 	ip_stack_t	*ipst = ill->ill_ipst;
20632 
20633 	ASSERT(ill != NULL);
20634 
20635 	isv6 = ill->ill_isv6;
20636 	ifindex = ill->ill_usesrc_ifindex;
20637 	if (ifindex != 0) {
20638 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20639 		    NULL, ipst);
20640 		if (uill == NULL)
20641 			return (NULL);
20642 		mutex_enter(&uill->ill_lock);
20643 		for (ipif = uill->ill_ipif; ipif != NULL;
20644 		    ipif = ipif->ipif_next) {
20645 			if (!IPIF_CAN_LOOKUP(ipif))
20646 				continue;
20647 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20648 				continue;
20649 			if (!(ipif->ipif_flags & IPIF_UP))
20650 				continue;
20651 			if (ipif->ipif_zoneid != zoneid)
20652 				continue;
20653 			if ((isv6 &&
20654 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20655 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20656 				continue;
20657 			mutex_exit(&uill->ill_lock);
20658 			ill_refrele(uill);
20659 			return (B_TRUE);
20660 		}
20661 		mutex_exit(&uill->ill_lock);
20662 		ill_refrele(uill);
20663 	}
20664 	return (B_FALSE);
20665 }
20666 
20667 /*
20668  * Determine the best source address given a destination address and an ill.
20669  * Prefers non-deprecated over deprecated but will return a deprecated
20670  * address if there is no other choice. If there is a usable source address
20671  * on the interface pointed to by ill_usesrc_ifindex then that is given
20672  * first preference.
20673  *
20674  * Returns NULL if there is no suitable source address for the ill.
20675  * This only occurs when there is no valid source address for the ill.
20676  */
20677 ipif_t *
20678 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20679 {
20680 	ipif_t *ipif;
20681 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20682 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20683 	int index = 0;
20684 	boolean_t wrapped = B_FALSE;
20685 	boolean_t same_subnet_only = B_FALSE;
20686 	boolean_t ipif_same_found, ipif_other_found;
20687 	boolean_t specific_found;
20688 	ill_t	*till, *usill = NULL;
20689 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20690 	ip_stack_t	*ipst = ill->ill_ipst;
20691 
20692 	if (ill->ill_usesrc_ifindex != 0) {
20693 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20694 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20695 		if (usill != NULL)
20696 			ill = usill;	/* Select source from usesrc ILL */
20697 		else
20698 			return (NULL);
20699 	}
20700 
20701 	/*
20702 	 * If we're dealing with an unlabeled destination on a labeled system,
20703 	 * make sure that we ignore source addresses that are incompatible with
20704 	 * the destination's default label.  That destination's default label
20705 	 * must dominate the minimum label on the source address.
20706 	 */
20707 	dst_rhtp = NULL;
20708 	if (is_system_labeled()) {
20709 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20710 		if (dst_rhtp == NULL)
20711 			return (NULL);
20712 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20713 			TPC_RELE(dst_rhtp);
20714 			dst_rhtp = NULL;
20715 		}
20716 	}
20717 
20718 	/*
20719 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20720 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20721 	 * After selecting the right ipif, under ill_lock make sure ipif is
20722 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20723 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20724 	 * but not under a lock.
20725 	 */
20726 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20727 
20728 retry:
20729 	till = ill;
20730 	ipif_arr[0] = NULL;
20731 
20732 	if (till->ill_group != NULL)
20733 		till = till->ill_group->illgrp_ill;
20734 
20735 	/*
20736 	 * Choose one good source address from each ill across the group.
20737 	 * If possible choose a source address in the same subnet as
20738 	 * the destination address.
20739 	 *
20740 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20741 	 * This is okay because of the following.
20742 	 *
20743 	 *    If PHYI_FAILED is set and we still have non-deprecated
20744 	 *    addresses, it means the addresses have not yet been
20745 	 *    failed over to a different interface. We potentially
20746 	 *    select them to create IRE_CACHES, which will be later
20747 	 *    flushed when the addresses move over.
20748 	 *
20749 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20750 	 *    addresses, it means either the user has configured them
20751 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20752 	 *    been moved over. For the former, in.mpathd does a failover
20753 	 *    when the interface becomes INACTIVE and hence we should
20754 	 *    not find them. Once INACTIVE is set, we don't allow them
20755 	 *    to create logical interfaces anymore. For the latter, a
20756 	 *    flush will happen when INACTIVE is cleared which will
20757 	 *    flush the IRE_CACHES.
20758 	 *
20759 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20760 	 *    over soon. We potentially select them to create IRE_CACHEs,
20761 	 *    which will be later flushed when the addresses move over.
20762 	 *
20763 	 * NOTE : As ipif_select_source is called to borrow source address
20764 	 * for an ipif that is part of a group, source address selection
20765 	 * will be re-done whenever the group changes i.e either an
20766 	 * insertion/deletion in the group.
20767 	 *
20768 	 * Fill ipif_arr[] with source addresses, using these rules:
20769 	 *
20770 	 *	1. At most one source address from a given ill ends up
20771 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20772 	 *	   associated with a given ill ends up in ipif_arr[].
20773 	 *
20774 	 *	2. If there is at least one non-deprecated ipif in the
20775 	 *	   IPMP group with a source address on the same subnet as
20776 	 *	   our destination, then fill ipif_arr[] only with
20777 	 *	   source addresses on the same subnet as our destination.
20778 	 *	   Note that because of (1), only the first
20779 	 *	   non-deprecated ipif found with a source address
20780 	 *	   matching the destination ends up in ipif_arr[].
20781 	 *
20782 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20783 	 *	   addresses not in the same subnet as our destination.
20784 	 *	   Again, because of (1), only the first off-subnet source
20785 	 *	   address will be chosen.
20786 	 *
20787 	 *	4. If there are no non-deprecated ipifs, then just use
20788 	 *	   the source address associated with the last deprecated
20789 	 *	   one we find that happens to be on the same subnet,
20790 	 *	   otherwise the first one not in the same subnet.
20791 	 */
20792 	specific_found = B_FALSE;
20793 	for (; till != NULL; till = till->ill_group_next) {
20794 		ipif_same_found = B_FALSE;
20795 		ipif_other_found = B_FALSE;
20796 		for (ipif = till->ill_ipif; ipif != NULL;
20797 		    ipif = ipif->ipif_next) {
20798 			if (!IPIF_CAN_LOOKUP(ipif))
20799 				continue;
20800 			/* Always skip NOLOCAL and ANYCAST interfaces */
20801 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20802 				continue;
20803 			if (!(ipif->ipif_flags & IPIF_UP) ||
20804 			    !ipif->ipif_addr_ready)
20805 				continue;
20806 			if (ipif->ipif_zoneid != zoneid &&
20807 			    ipif->ipif_zoneid != ALL_ZONES)
20808 				continue;
20809 			/*
20810 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20811 			 * but are not valid as source addresses.
20812 			 */
20813 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20814 				continue;
20815 
20816 			/*
20817 			 * Check compatibility of local address for
20818 			 * destination's default label if we're on a labeled
20819 			 * system.  Incompatible addresses can't be used at
20820 			 * all.
20821 			 */
20822 			if (dst_rhtp != NULL) {
20823 				boolean_t incompat;
20824 
20825 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20826 				    IPV4_VERSION, B_FALSE);
20827 				if (src_rhtp == NULL)
20828 					continue;
20829 				incompat =
20830 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20831 				    src_rhtp->tpc_tp.tp_doi !=
20832 				    dst_rhtp->tpc_tp.tp_doi ||
20833 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20834 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20835 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20836 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20837 				TPC_RELE(src_rhtp);
20838 				if (incompat)
20839 					continue;
20840 			}
20841 
20842 			/*
20843 			 * We prefer not to use all all-zones addresses, if we
20844 			 * can avoid it, as they pose problems with unlabeled
20845 			 * destinations.
20846 			 */
20847 			if (ipif->ipif_zoneid != ALL_ZONES) {
20848 				if (!specific_found &&
20849 				    (!same_subnet_only ||
20850 				    (ipif->ipif_net_mask & dst) ==
20851 				    ipif->ipif_subnet)) {
20852 					index = 0;
20853 					specific_found = B_TRUE;
20854 					ipif_other_found = B_FALSE;
20855 				}
20856 			} else {
20857 				if (specific_found)
20858 					continue;
20859 			}
20860 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20861 				if (ipif_dep == NULL ||
20862 				    (ipif->ipif_net_mask & dst) ==
20863 				    ipif->ipif_subnet)
20864 					ipif_dep = ipif;
20865 				continue;
20866 			}
20867 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20868 				/* found a source address in the same subnet */
20869 				if (!same_subnet_only) {
20870 					same_subnet_only = B_TRUE;
20871 					index = 0;
20872 				}
20873 				ipif_same_found = B_TRUE;
20874 			} else {
20875 				if (same_subnet_only || ipif_other_found)
20876 					continue;
20877 				ipif_other_found = B_TRUE;
20878 			}
20879 			ipif_arr[index++] = ipif;
20880 			if (index == MAX_IPIF_SELECT_SOURCE) {
20881 				wrapped = B_TRUE;
20882 				index = 0;
20883 			}
20884 			if (ipif_same_found)
20885 				break;
20886 		}
20887 	}
20888 
20889 	if (ipif_arr[0] == NULL) {
20890 		ipif = ipif_dep;
20891 	} else {
20892 		if (wrapped)
20893 			index = MAX_IPIF_SELECT_SOURCE;
20894 		ipif = ipif_arr[ipif_rand(ipst) % index];
20895 		ASSERT(ipif != NULL);
20896 	}
20897 
20898 	if (ipif != NULL) {
20899 		mutex_enter(&ipif->ipif_ill->ill_lock);
20900 		if (!IPIF_CAN_LOOKUP(ipif)) {
20901 			mutex_exit(&ipif->ipif_ill->ill_lock);
20902 			goto retry;
20903 		}
20904 		ipif_refhold_locked(ipif);
20905 		mutex_exit(&ipif->ipif_ill->ill_lock);
20906 	}
20907 
20908 	rw_exit(&ipst->ips_ill_g_lock);
20909 	if (usill != NULL)
20910 		ill_refrele(usill);
20911 	if (dst_rhtp != NULL)
20912 		TPC_RELE(dst_rhtp);
20913 
20914 #ifdef DEBUG
20915 	if (ipif == NULL) {
20916 		char buf1[INET6_ADDRSTRLEN];
20917 
20918 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20919 		    ill->ill_name,
20920 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20921 	} else {
20922 		char buf1[INET6_ADDRSTRLEN];
20923 		char buf2[INET6_ADDRSTRLEN];
20924 
20925 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20926 		    ipif->ipif_ill->ill_name,
20927 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20928 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20929 		    buf2, sizeof (buf2))));
20930 	}
20931 #endif /* DEBUG */
20932 	return (ipif);
20933 }
20934 
20935 
20936 /*
20937  * If old_ipif is not NULL, see if ipif was derived from old
20938  * ipif and if so, recreate the interface route by re-doing
20939  * source address selection. This happens when ipif_down ->
20940  * ipif_update_other_ipifs calls us.
20941  *
20942  * If old_ipif is NULL, just redo the source address selection
20943  * if needed. This happens when illgrp_insert or ipif_up_done
20944  * calls us.
20945  */
20946 static void
20947 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20948 {
20949 	ire_t *ire;
20950 	ire_t *ipif_ire;
20951 	queue_t *stq;
20952 	ipif_t *nipif;
20953 	ill_t *ill;
20954 	boolean_t need_rele = B_FALSE;
20955 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20956 
20957 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20958 	ASSERT(IAM_WRITER_IPIF(ipif));
20959 
20960 	ill = ipif->ipif_ill;
20961 	if (!(ipif->ipif_flags &
20962 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20963 		/*
20964 		 * Can't possibly have borrowed the source
20965 		 * from old_ipif.
20966 		 */
20967 		return;
20968 	}
20969 
20970 	/*
20971 	 * Is there any work to be done? No work if the address
20972 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
20973 	 * ipif_select_source() does not borrow addresses from
20974 	 * NOLOCAL and ANYCAST interfaces).
20975 	 */
20976 	if ((old_ipif != NULL) &&
20977 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20978 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20979 	    (old_ipif->ipif_flags &
20980 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20981 		return;
20982 	}
20983 
20984 	/*
20985 	 * Perform the same checks as when creating the
20986 	 * IRE_INTERFACE in ipif_up_done.
20987 	 */
20988 	if (!(ipif->ipif_flags & IPIF_UP))
20989 		return;
20990 
20991 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
20992 	    (ipif->ipif_subnet == INADDR_ANY))
20993 		return;
20994 
20995 	ipif_ire = ipif_to_ire(ipif);
20996 	if (ipif_ire == NULL)
20997 		return;
20998 
20999 	/*
21000 	 * We know that ipif uses some other source for its
21001 	 * IRE_INTERFACE. Is it using the source of this
21002 	 * old_ipif?
21003 	 */
21004 	if (old_ipif != NULL &&
21005 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21006 		ire_refrele(ipif_ire);
21007 		return;
21008 	}
21009 	if (ip_debug > 2) {
21010 		/* ip1dbg */
21011 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21012 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21013 	}
21014 
21015 	stq = ipif_ire->ire_stq;
21016 
21017 	/*
21018 	 * Can't use our source address. Select a different
21019 	 * source address for the IRE_INTERFACE.
21020 	 */
21021 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21022 	if (nipif == NULL) {
21023 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21024 		nipif = ipif;
21025 	} else {
21026 		need_rele = B_TRUE;
21027 	}
21028 
21029 	ire = ire_create(
21030 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21031 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21032 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21033 	    NULL,				/* no gateway */
21034 	    &ipif->ipif_mtu,			/* max frag */
21035 	    NULL,				/* no src nce */
21036 	    NULL,				/* no recv from queue */
21037 	    stq,				/* send-to queue */
21038 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21039 	    ipif,
21040 	    0,
21041 	    0,
21042 	    0,
21043 	    0,
21044 	    &ire_uinfo_null,
21045 	    NULL,
21046 	    NULL,
21047 	    ipst);
21048 
21049 	if (ire != NULL) {
21050 		ire_t *ret_ire;
21051 		int error;
21052 
21053 		/*
21054 		 * We don't need ipif_ire anymore. We need to delete
21055 		 * before we add so that ire_add does not detect
21056 		 * duplicates.
21057 		 */
21058 		ire_delete(ipif_ire);
21059 		ret_ire = ire;
21060 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21061 		ASSERT(error == 0);
21062 		ASSERT(ire == ret_ire);
21063 		/* Held in ire_add */
21064 		ire_refrele(ret_ire);
21065 	}
21066 	/*
21067 	 * Either we are falling through from above or could not
21068 	 * allocate a replacement.
21069 	 */
21070 	ire_refrele(ipif_ire);
21071 	if (need_rele)
21072 		ipif_refrele(nipif);
21073 }
21074 
21075 /*
21076  * This old_ipif is going away.
21077  *
21078  * Determine if any other ipif's is using our address as
21079  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21080  * IPIF_DEPRECATED).
21081  * Find the IRE_INTERFACE for such ipifs and recreate them
21082  * to use an different source address following the rules in
21083  * ipif_up_done.
21084  *
21085  * This function takes an illgrp as an argument so that illgrp_delete
21086  * can call this to update source address even after deleting the
21087  * old_ipif->ipif_ill from the ill group.
21088  */
21089 static void
21090 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21091 {
21092 	ipif_t *ipif;
21093 	ill_t *ill;
21094 	char	buf[INET6_ADDRSTRLEN];
21095 
21096 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21097 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21098 
21099 	ill = old_ipif->ipif_ill;
21100 
21101 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21102 	    ill->ill_name,
21103 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21104 	    buf, sizeof (buf))));
21105 	/*
21106 	 * If this part of a group, look at all ills as ipif_select_source
21107 	 * borrows source address across all the ills in the group.
21108 	 */
21109 	if (illgrp != NULL)
21110 		ill = illgrp->illgrp_ill;
21111 
21112 	for (; ill != NULL; ill = ill->ill_group_next) {
21113 		for (ipif = ill->ill_ipif; ipif != NULL;
21114 		    ipif = ipif->ipif_next) {
21115 
21116 			if (ipif == old_ipif)
21117 				continue;
21118 
21119 			ipif_recreate_interface_routes(old_ipif, ipif);
21120 		}
21121 	}
21122 }
21123 
21124 /* ARGSUSED */
21125 int
21126 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21127 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21128 {
21129 	/*
21130 	 * ill_phyint_reinit merged the v4 and v6 into a single
21131 	 * ipsq. Could also have become part of a ipmp group in the
21132 	 * process, and we might not have been able to complete the
21133 	 * operation in ipif_set_values, if we could not become
21134 	 * exclusive.  If so restart it here.
21135 	 */
21136 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21137 }
21138 
21139 
21140 /*
21141  * Can operate on either a module or a driver queue.
21142  * Returns an error if not a module queue.
21143  */
21144 /* ARGSUSED */
21145 int
21146 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21147     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21148 {
21149 	queue_t		*q1 = q;
21150 	char 		*cp;
21151 	char		interf_name[LIFNAMSIZ];
21152 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21153 
21154 	if (q->q_next == NULL) {
21155 		ip1dbg((
21156 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21157 		return (EINVAL);
21158 	}
21159 
21160 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21161 		return (EALREADY);
21162 
21163 	do {
21164 		q1 = q1->q_next;
21165 	} while (q1->q_next);
21166 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21167 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21168 
21169 	/*
21170 	 * Here we are not going to delay the ioack until after
21171 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21172 	 * original ioctl message before sending the requests.
21173 	 */
21174 	return (ipif_set_values(q, mp, interf_name, &ppa));
21175 }
21176 
21177 /* ARGSUSED */
21178 int
21179 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21180     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21181 {
21182 	return (ENXIO);
21183 }
21184 
21185 /*
21186  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21187  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21188  * ire_check_and_create_bcast()).
21189  */
21190 static ire_t **
21191 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21192 {
21193 	ipaddr_t addr;
21194 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21195 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21196 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21197 
21198 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21199 
21200 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21201 
21202 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21203 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21204 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21205 
21206 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21207 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21208 
21209 	/*
21210 	 * For backward compatibility, we create net broadcast IREs based on
21211 	 * the old "IP address class system", since some old machines only
21212 	 * respond to these class derived net broadcast.  However, we must not
21213 	 * create these net broadcast IREs if the subnetmask is shorter than
21214 	 * the IP address class based derived netmask.  Otherwise, we may
21215 	 * create a net broadcast address which is the same as an IP address
21216 	 * on the subnet -- and then TCP will refuse to talk to that address.
21217 	 */
21218 	if (netmask < subnetmask) {
21219 		addr = netmask & ipif->ipif_subnet;
21220 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21221 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21222 		    flags);
21223 	}
21224 
21225 	/*
21226 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21227 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21228 	 * created.  Creating these broadcast IREs will only create confusion
21229 	 * as `addr' will be the same as the IP address.
21230 	 */
21231 	if (subnetmask != 0xFFFFFFFF) {
21232 		addr = ipif->ipif_subnet;
21233 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21234 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21235 		    irep, flags);
21236 	}
21237 
21238 	return (irep);
21239 }
21240 
21241 /*
21242  * Broadcast IRE info structure used in the functions below.  Since we
21243  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21244  */
21245 typedef struct bcast_ireinfo {
21246 	uchar_t		bi_type;	/* BCAST_* value from below */
21247 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21248 			bi_needrep:1,	/* do we need to replace it? */
21249 			bi_haverep:1,	/* have we replaced it? */
21250 			bi_pad:5;
21251 	ipaddr_t	bi_addr;	/* IRE address */
21252 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21253 } bcast_ireinfo_t;
21254 
21255 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21256 
21257 /*
21258  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21259  * return B_TRUE if it should immediately be used to recreate the IRE.
21260  */
21261 static boolean_t
21262 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21263 {
21264 	ipaddr_t addr;
21265 
21266 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21267 
21268 	switch (bireinfop->bi_type) {
21269 	case BCAST_NET:
21270 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21271 		if (addr != bireinfop->bi_addr)
21272 			return (B_FALSE);
21273 		break;
21274 	case BCAST_SUBNET:
21275 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21276 			return (B_FALSE);
21277 		break;
21278 	}
21279 
21280 	bireinfop->bi_needrep = 1;
21281 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21282 		if (bireinfop->bi_backup == NULL)
21283 			bireinfop->bi_backup = ipif;
21284 		return (B_FALSE);
21285 	}
21286 	return (B_TRUE);
21287 }
21288 
21289 /*
21290  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21291  * them ala ire_check_and_create_bcast().
21292  */
21293 static ire_t **
21294 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21295 {
21296 	ipaddr_t mask, addr;
21297 
21298 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21299 
21300 	addr = bireinfop->bi_addr;
21301 	irep = ire_create_bcast(ipif, addr, irep);
21302 
21303 	switch (bireinfop->bi_type) {
21304 	case BCAST_NET:
21305 		mask = ip_net_mask(ipif->ipif_subnet);
21306 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21307 		break;
21308 	case BCAST_SUBNET:
21309 		mask = ipif->ipif_net_mask;
21310 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21311 		break;
21312 	}
21313 
21314 	bireinfop->bi_haverep = 1;
21315 	return (irep);
21316 }
21317 
21318 /*
21319  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21320  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21321  * that are going away are still needed.  If so, have ipif_create_bcast()
21322  * recreate them (except for the deprecated case, as explained below).
21323  */
21324 static ire_t **
21325 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21326     ire_t **irep)
21327 {
21328 	int i;
21329 	ipif_t *ipif;
21330 
21331 	ASSERT(!ill->ill_isv6);
21332 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21333 		/*
21334 		 * Skip this ipif if it's (a) the one being taken down, (b)
21335 		 * not in the same zone, or (c) has no valid local address.
21336 		 */
21337 		if (ipif == test_ipif ||
21338 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21339 		    ipif->ipif_subnet == 0 ||
21340 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21341 		    (IPIF_UP|IPIF_BROADCAST))
21342 			continue;
21343 
21344 		/*
21345 		 * For each dying IRE that hasn't yet been replaced, see if
21346 		 * `ipif' needs it and whether the IRE should be recreated on
21347 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21348 		 * will return B_FALSE even if `ipif' needs the IRE on the
21349 		 * hopes that we'll later find a needy non-deprecated ipif.
21350 		 * However, the ipif is recorded in bi_backup for possible
21351 		 * subsequent use by ipif_check_bcast_ires().
21352 		 */
21353 		for (i = 0; i < BCAST_COUNT; i++) {
21354 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21355 				continue;
21356 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21357 				continue;
21358 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21359 		}
21360 
21361 		/*
21362 		 * If we've replaced all of the broadcast IREs that are going
21363 		 * to be taken down, we know we're done.
21364 		 */
21365 		for (i = 0; i < BCAST_COUNT; i++) {
21366 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21367 				break;
21368 		}
21369 		if (i == BCAST_COUNT)
21370 			break;
21371 	}
21372 	return (irep);
21373 }
21374 
21375 /*
21376  * Check if `test_ipif' (which is going away) is associated with any existing
21377  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21378  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21379  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21380  *
21381  * This is necessary because broadcast IREs are shared.  In particular, a
21382  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21383  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21384  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21385  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21386  * same zone, they will share the same set of broadcast IREs.
21387  *
21388  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21389  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21390  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21391  */
21392 static void
21393 ipif_check_bcast_ires(ipif_t *test_ipif)
21394 {
21395 	ill_t		*ill = test_ipif->ipif_ill;
21396 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21397 	ire_t		**irep1, **irep = &ire_array[0];
21398 	uint_t 		i, willdie;
21399 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21400 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21401 
21402 	ASSERT(!test_ipif->ipif_isv6);
21403 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21404 
21405 	/*
21406 	 * No broadcast IREs for the LOOPBACK interface
21407 	 * or others such as point to point and IPIF_NOXMIT.
21408 	 */
21409 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21410 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21411 		return;
21412 
21413 	bzero(bireinfo, sizeof (bireinfo));
21414 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21415 	bireinfo[0].bi_addr = 0;
21416 
21417 	bireinfo[1].bi_type = BCAST_ALLONES;
21418 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21419 
21420 	bireinfo[2].bi_type = BCAST_NET;
21421 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21422 
21423 	if (test_ipif->ipif_net_mask != 0)
21424 		mask = test_ipif->ipif_net_mask;
21425 	bireinfo[3].bi_type = BCAST_SUBNET;
21426 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21427 
21428 	/*
21429 	 * Figure out what (if any) broadcast IREs will die as a result of
21430 	 * `test_ipif' going away.  If none will die, we're done.
21431 	 */
21432 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21433 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21434 		    test_ipif, ALL_ZONES, NULL,
21435 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21436 		if (ire != NULL) {
21437 			willdie++;
21438 			bireinfo[i].bi_willdie = 1;
21439 			ire_refrele(ire);
21440 		}
21441 	}
21442 
21443 	if (willdie == 0)
21444 		return;
21445 
21446 	/*
21447 	 * Walk through all the ipifs that will be affected by the dying IREs,
21448 	 * and recreate the IREs as necessary.
21449 	 */
21450 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21451 
21452 	/*
21453 	 * Scan through the set of broadcast IREs and see if there are any
21454 	 * that we need to replace that have not yet been replaced.  If so,
21455 	 * replace them using the appropriate backup ipif.
21456 	 */
21457 	for (i = 0; i < BCAST_COUNT; i++) {
21458 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21459 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21460 			    &bireinfo[i], irep);
21461 	}
21462 
21463 	/*
21464 	 * If we can't create all of them, don't add any of them.  (Code in
21465 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21466 	 * non-loopback copy and loopback copy for a given address.)
21467 	 */
21468 	for (irep1 = irep; irep1 > ire_array; ) {
21469 		irep1--;
21470 		if (*irep1 == NULL) {
21471 			ip0dbg(("ipif_check_bcast_ires: can't create "
21472 			    "IRE_BROADCAST, memory allocation failure\n"));
21473 			while (irep > ire_array) {
21474 				irep--;
21475 				if (*irep != NULL)
21476 					ire_delete(*irep);
21477 			}
21478 			return;
21479 		}
21480 	}
21481 
21482 	for (irep1 = irep; irep1 > ire_array; ) {
21483 		irep1--;
21484 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21485 			ire_refrele(*irep1);		/* Held in ire_add */
21486 	}
21487 }
21488 
21489 /*
21490  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21491  * from lifr_flags and the name from lifr_name.
21492  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21493  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21494  * Returns EINPROGRESS when mp has been consumed by queueing it on
21495  * ill_pending_mp and the ioctl will complete in ip_rput.
21496  *
21497  * Can operate on either a module or a driver queue.
21498  * Returns an error if not a module queue.
21499  */
21500 /* ARGSUSED */
21501 int
21502 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21503     ip_ioctl_cmd_t *ipip, void *if_req)
21504 {
21505 	ill_t	*ill = q->q_ptr;
21506 	phyint_t *phyi;
21507 	ip_stack_t *ipst;
21508 	struct lifreq *lifr = if_req;
21509 
21510 	ASSERT(ipif != NULL);
21511 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21512 
21513 	if (q->q_next == NULL) {
21514 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21515 		return (EINVAL);
21516 	}
21517 
21518 	/*
21519 	 * If we are not writer on 'q' then this interface exists already
21520 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21521 	 * so return EALREADY.
21522 	 */
21523 	if (ill != ipif->ipif_ill)
21524 		return (EALREADY);
21525 
21526 	if (ill->ill_name[0] != '\0')
21527 		return (EALREADY);
21528 
21529 	/*
21530 	 * Set all the flags. Allows all kinds of override. Provide some
21531 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21532 	 * unless there is either multicast/broadcast support in the driver
21533 	 * or it is a pt-pt link.
21534 	 */
21535 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21536 		/* Meaningless to IP thus don't allow them to be set. */
21537 		ip1dbg(("ip_setname: EINVAL 1\n"));
21538 		return (EINVAL);
21539 	}
21540 
21541 	/*
21542 	 * If there's another ill already with the requested name, ensure
21543 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21544 	 * fuse together two unrelated ills, which will cause chaos.
21545 	 */
21546 	ipst = ill->ill_ipst;
21547 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21548 	    lifr->lifr_name, NULL);
21549 	if (phyi != NULL) {
21550 		ill_t *ill_mate = phyi->phyint_illv4;
21551 
21552 		if (ill_mate == NULL)
21553 			ill_mate = phyi->phyint_illv6;
21554 		ASSERT(ill_mate != NULL);
21555 
21556 		if (ill_mate->ill_media->ip_m_mac_type !=
21557 		    ill->ill_media->ip_m_mac_type) {
21558 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21559 			    "use the same ill name on differing media\n"));
21560 			return (EINVAL);
21561 		}
21562 	}
21563 
21564 	/*
21565 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21566 	 * ill_bcast_addr_length info.
21567 	 */
21568 	if (!ill->ill_needs_attach &&
21569 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21570 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21571 	    ill->ill_bcast_addr_length == 0)) {
21572 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21573 		ip1dbg(("ip_setname: EINVAL 2\n"));
21574 		return (EINVAL);
21575 	}
21576 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21577 	    ((lifr->lifr_flags & IFF_IPV6) ||
21578 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21579 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21580 		ip1dbg(("ip_setname: EINVAL 3\n"));
21581 		return (EINVAL);
21582 	}
21583 	if (lifr->lifr_flags & IFF_UP) {
21584 		/* Can only be set with SIOCSLIFFLAGS */
21585 		ip1dbg(("ip_setname: EINVAL 4\n"));
21586 		return (EINVAL);
21587 	}
21588 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21589 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21590 		ip1dbg(("ip_setname: EINVAL 5\n"));
21591 		return (EINVAL);
21592 	}
21593 	/*
21594 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21595 	 */
21596 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21597 	    !(lifr->lifr_flags & IFF_IPV6) &&
21598 	    !(ipif->ipif_isv6)) {
21599 		ip1dbg(("ip_setname: EINVAL 6\n"));
21600 		return (EINVAL);
21601 	}
21602 
21603 	/*
21604 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21605 	 * we have all the flags here. So, we assign rather than we OR.
21606 	 * We can't OR the flags here because we don't want to set
21607 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21608 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21609 	 * on lifr_flags value here.
21610 	 */
21611 	/*
21612 	 * This ill has not been inserted into the global list.
21613 	 * So we are still single threaded and don't need any lock
21614 	 */
21615 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21616 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21617 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21618 
21619 	/* We started off as V4. */
21620 	if (ill->ill_flags & ILLF_IPV6) {
21621 		ill->ill_phyint->phyint_illv6 = ill;
21622 		ill->ill_phyint->phyint_illv4 = NULL;
21623 	}
21624 
21625 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21626 }
21627 
21628 /* ARGSUSED */
21629 int
21630 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21631     ip_ioctl_cmd_t *ipip, void *if_req)
21632 {
21633 	/*
21634 	 * ill_phyint_reinit merged the v4 and v6 into a single
21635 	 * ipsq. Could also have become part of a ipmp group in the
21636 	 * process, and we might not have been able to complete the
21637 	 * slifname in ipif_set_values, if we could not become
21638 	 * exclusive.  If so restart it here
21639 	 */
21640 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21641 }
21642 
21643 /*
21644  * Return a pointer to the ipif which matches the index, IP version type and
21645  * zoneid.
21646  */
21647 ipif_t *
21648 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21649     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21650 {
21651 	ill_t	*ill;
21652 	ipif_t	*ipif = NULL;
21653 
21654 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21655 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21656 
21657 	if (err != NULL)
21658 		*err = 0;
21659 
21660 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21661 	if (ill != NULL) {
21662 		mutex_enter(&ill->ill_lock);
21663 		for (ipif = ill->ill_ipif; ipif != NULL;
21664 		    ipif = ipif->ipif_next) {
21665 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21666 			    zoneid == ipif->ipif_zoneid ||
21667 			    ipif->ipif_zoneid == ALL_ZONES)) {
21668 				ipif_refhold_locked(ipif);
21669 				break;
21670 			}
21671 		}
21672 		mutex_exit(&ill->ill_lock);
21673 		ill_refrele(ill);
21674 		if (ipif == NULL && err != NULL)
21675 			*err = ENXIO;
21676 	}
21677 	return (ipif);
21678 }
21679 
21680 typedef struct conn_change_s {
21681 	uint_t cc_old_ifindex;
21682 	uint_t cc_new_ifindex;
21683 } conn_change_t;
21684 
21685 /*
21686  * ipcl_walk function for changing interface index.
21687  */
21688 static void
21689 conn_change_ifindex(conn_t *connp, caddr_t arg)
21690 {
21691 	conn_change_t *connc;
21692 	uint_t old_ifindex;
21693 	uint_t new_ifindex;
21694 	int i;
21695 	ilg_t *ilg;
21696 
21697 	connc = (conn_change_t *)arg;
21698 	old_ifindex = connc->cc_old_ifindex;
21699 	new_ifindex = connc->cc_new_ifindex;
21700 
21701 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21702 		connp->conn_orig_bound_ifindex = new_ifindex;
21703 
21704 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21705 		connp->conn_orig_multicast_ifindex = new_ifindex;
21706 
21707 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21708 		ilg = &connp->conn_ilg[i];
21709 		if (ilg->ilg_orig_ifindex == old_ifindex)
21710 			ilg->ilg_orig_ifindex = new_ifindex;
21711 	}
21712 }
21713 
21714 /*
21715  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21716  * to new_index if it matches the old_index.
21717  *
21718  * Failovers typically happen within a group of ills. But somebody
21719  * can remove an ill from the group after a failover happened. If
21720  * we are setting the ifindex after this, we potentially need to
21721  * look at all the ills rather than just the ones in the group.
21722  * We cut down the work by looking at matching ill_net_types
21723  * and ill_types as we could not possibly grouped them together.
21724  */
21725 static void
21726 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21727 {
21728 	ill_t *ill;
21729 	ipif_t *ipif;
21730 	uint_t old_ifindex;
21731 	uint_t new_ifindex;
21732 	ilm_t *ilm;
21733 	ill_walk_context_t ctx;
21734 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21735 
21736 	old_ifindex = connc->cc_old_ifindex;
21737 	new_ifindex = connc->cc_new_ifindex;
21738 
21739 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21740 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21741 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21742 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21743 		    (ill_orig->ill_type != ill->ill_type)) {
21744 			continue;
21745 		}
21746 		for (ipif = ill->ill_ipif; ipif != NULL;
21747 		    ipif = ipif->ipif_next) {
21748 			if (ipif->ipif_orig_ifindex == old_ifindex)
21749 				ipif->ipif_orig_ifindex = new_ifindex;
21750 		}
21751 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21752 			if (ilm->ilm_orig_ifindex == old_ifindex)
21753 				ilm->ilm_orig_ifindex = new_ifindex;
21754 		}
21755 	}
21756 	rw_exit(&ipst->ips_ill_g_lock);
21757 }
21758 
21759 /*
21760  * We first need to ensure that the new index is unique, and
21761  * then carry the change across both v4 and v6 ill representation
21762  * of the physical interface.
21763  */
21764 /* ARGSUSED */
21765 int
21766 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21767     ip_ioctl_cmd_t *ipip, void *ifreq)
21768 {
21769 	ill_t		*ill;
21770 	ill_t		*ill_other;
21771 	phyint_t	*phyi;
21772 	int		old_index;
21773 	conn_change_t	connc;
21774 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21775 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21776 	uint_t	index;
21777 	ill_t	*ill_v4;
21778 	ill_t	*ill_v6;
21779 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21780 
21781 	if (ipip->ipi_cmd_type == IF_CMD)
21782 		index = ifr->ifr_index;
21783 	else
21784 		index = lifr->lifr_index;
21785 
21786 	/*
21787 	 * Only allow on physical interface. Also, index zero is illegal.
21788 	 *
21789 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21790 	 *
21791 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21792 	 *    implies a possible failback might have to happen. As failback
21793 	 *    depends on the old index, we should fail setting the index.
21794 	 *
21795 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21796 	 *    any addresses or multicast memberships are failed over to
21797 	 *    a non-STANDBY interface. As failback depends on the old
21798 	 *    index, we should fail setting the index for this case also.
21799 	 *
21800 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21801 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21802 	 */
21803 	ill = ipif->ipif_ill;
21804 	phyi = ill->ill_phyint;
21805 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21806 	    ipif->ipif_id != 0 || index == 0) {
21807 		return (EINVAL);
21808 	}
21809 	old_index = phyi->phyint_ifindex;
21810 
21811 	/* If the index is not changing, no work to do */
21812 	if (old_index == index)
21813 		return (0);
21814 
21815 	/*
21816 	 * Use ill_lookup_on_ifindex to determine if the
21817 	 * new index is unused and if so allow the change.
21818 	 */
21819 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21820 	    ipst);
21821 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21822 	    ipst);
21823 	if (ill_v6 != NULL || ill_v4 != NULL) {
21824 		if (ill_v4 != NULL)
21825 			ill_refrele(ill_v4);
21826 		if (ill_v6 != NULL)
21827 			ill_refrele(ill_v6);
21828 		return (EBUSY);
21829 	}
21830 
21831 	/*
21832 	 * The new index is unused. Set it in the phyint.
21833 	 * Locate the other ill so that we can send a routing
21834 	 * sockets message.
21835 	 */
21836 	if (ill->ill_isv6) {
21837 		ill_other = phyi->phyint_illv4;
21838 	} else {
21839 		ill_other = phyi->phyint_illv6;
21840 	}
21841 
21842 	phyi->phyint_ifindex = index;
21843 
21844 	/* Update SCTP's ILL list */
21845 	sctp_ill_reindex(ill, old_index);
21846 
21847 	connc.cc_old_ifindex = old_index;
21848 	connc.cc_new_ifindex = index;
21849 	ip_change_ifindex(ill, &connc);
21850 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21851 
21852 	/* Send the routing sockets message */
21853 	ip_rts_ifmsg(ipif);
21854 	if (ill_other != NULL)
21855 		ip_rts_ifmsg(ill_other->ill_ipif);
21856 
21857 	return (0);
21858 }
21859 
21860 /* ARGSUSED */
21861 int
21862 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21863     ip_ioctl_cmd_t *ipip, void *ifreq)
21864 {
21865 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21866 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21867 
21868 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21869 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21870 	/* Get the interface index */
21871 	if (ipip->ipi_cmd_type == IF_CMD) {
21872 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21873 	} else {
21874 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21875 	}
21876 	return (0);
21877 }
21878 
21879 /* ARGSUSED */
21880 int
21881 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21882     ip_ioctl_cmd_t *ipip, void *ifreq)
21883 {
21884 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21885 
21886 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21887 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21888 	/* Get the interface zone */
21889 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21890 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21891 	return (0);
21892 }
21893 
21894 /*
21895  * Set the zoneid of an interface.
21896  */
21897 /* ARGSUSED */
21898 int
21899 ip_sioctl_slifzone(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 	int err = 0;
21904 	boolean_t need_up = B_FALSE;
21905 	zone_t *zptr;
21906 	zone_status_t status;
21907 	zoneid_t zoneid;
21908 
21909 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21910 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21911 		if (!is_system_labeled())
21912 			return (ENOTSUP);
21913 		zoneid = GLOBAL_ZONEID;
21914 	}
21915 
21916 	/* cannot assign instance zero to a non-global zone */
21917 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21918 		return (ENOTSUP);
21919 
21920 	/*
21921 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21922 	 * the event of a race with the zone shutdown processing, since IP
21923 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21924 	 * interface will be cleaned up even if the zone is shut down
21925 	 * immediately after the status check. If the interface can't be brought
21926 	 * down right away, and the zone is shut down before the restart
21927 	 * function is called, we resolve the possible races by rechecking the
21928 	 * zone status in the restart function.
21929 	 */
21930 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21931 		return (EINVAL);
21932 	status = zone_status_get(zptr);
21933 	zone_rele(zptr);
21934 
21935 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21936 		return (EINVAL);
21937 
21938 	if (ipif->ipif_flags & IPIF_UP) {
21939 		/*
21940 		 * If the interface is already marked up,
21941 		 * we call ipif_down which will take care
21942 		 * of ditching any IREs that have been set
21943 		 * up based on the old interface address.
21944 		 */
21945 		err = ipif_logical_down(ipif, q, mp);
21946 		if (err == EINPROGRESS)
21947 			return (err);
21948 		ipif_down_tail(ipif);
21949 		need_up = B_TRUE;
21950 	}
21951 
21952 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21953 	return (err);
21954 }
21955 
21956 static int
21957 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21958     queue_t *q, mblk_t *mp, boolean_t need_up)
21959 {
21960 	int	err = 0;
21961 	ip_stack_t	*ipst;
21962 
21963 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21964 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21965 
21966 	if (CONN_Q(q))
21967 		ipst = CONNQ_TO_IPST(q);
21968 	else
21969 		ipst = ILLQ_TO_IPST(q);
21970 
21971 	/*
21972 	 * For exclusive stacks we don't allow a different zoneid than
21973 	 * global.
21974 	 */
21975 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
21976 	    zoneid != GLOBAL_ZONEID)
21977 		return (EINVAL);
21978 
21979 	/* Set the new zone id. */
21980 	ipif->ipif_zoneid = zoneid;
21981 
21982 	/* Update sctp list */
21983 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
21984 
21985 	if (need_up) {
21986 		/*
21987 		 * Now bring the interface back up.  If this
21988 		 * is the only IPIF for the ILL, ipif_up
21989 		 * will have to re-bind to the device, so
21990 		 * we may get back EINPROGRESS, in which
21991 		 * case, this IOCTL will get completed in
21992 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
21993 		 */
21994 		err = ipif_up(ipif, q, mp);
21995 	}
21996 	return (err);
21997 }
21998 
21999 /* ARGSUSED */
22000 int
22001 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22002     ip_ioctl_cmd_t *ipip, void *if_req)
22003 {
22004 	struct lifreq *lifr = (struct lifreq *)if_req;
22005 	zoneid_t zoneid;
22006 	zone_t *zptr;
22007 	zone_status_t status;
22008 
22009 	ASSERT(ipif->ipif_id != 0);
22010 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22011 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22012 		zoneid = GLOBAL_ZONEID;
22013 
22014 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22015 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22016 
22017 	/*
22018 	 * We recheck the zone status to resolve the following race condition:
22019 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22020 	 * 2) hme0:1 is up and can't be brought down right away;
22021 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22022 	 * 3) zone "myzone" is halted; the zone status switches to
22023 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22024 	 * the interfaces to remove - hme0:1 is not returned because it's not
22025 	 * yet in "myzone", so it won't be removed;
22026 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22027 	 * status check here, we would have hme0:1 in "myzone" after it's been
22028 	 * destroyed.
22029 	 * Note that if the status check fails, we need to bring the interface
22030 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22031 	 * ipif_up_done[_v6]().
22032 	 */
22033 	status = ZONE_IS_UNINITIALIZED;
22034 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22035 		status = zone_status_get(zptr);
22036 		zone_rele(zptr);
22037 	}
22038 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22039 		if (ipif->ipif_isv6) {
22040 			(void) ipif_up_done_v6(ipif);
22041 		} else {
22042 			(void) ipif_up_done(ipif);
22043 		}
22044 		return (EINVAL);
22045 	}
22046 
22047 	ipif_down_tail(ipif);
22048 
22049 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22050 	    B_TRUE));
22051 }
22052 
22053 /* ARGSUSED */
22054 int
22055 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22056 	ip_ioctl_cmd_t *ipip, void *ifreq)
22057 {
22058 	struct lifreq	*lifr = ifreq;
22059 
22060 	ASSERT(q->q_next == NULL);
22061 	ASSERT(CONN_Q(q));
22062 
22063 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22064 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22065 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22066 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22067 
22068 	return (0);
22069 }
22070 
22071 
22072 /* Find the previous ILL in this usesrc group */
22073 static ill_t *
22074 ill_prev_usesrc(ill_t *uill)
22075 {
22076 	ill_t *ill;
22077 
22078 	for (ill = uill->ill_usesrc_grp_next;
22079 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22080 	    ill = ill->ill_usesrc_grp_next)
22081 		/* do nothing */;
22082 	return (ill);
22083 }
22084 
22085 /*
22086  * Release all members of the usesrc group. This routine is called
22087  * from ill_delete when the interface being unplumbed is the
22088  * group head.
22089  */
22090 static void
22091 ill_disband_usesrc_group(ill_t *uill)
22092 {
22093 	ill_t *next_ill, *tmp_ill;
22094 	ip_stack_t	*ipst = uill->ill_ipst;
22095 
22096 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22097 	next_ill = uill->ill_usesrc_grp_next;
22098 
22099 	do {
22100 		ASSERT(next_ill != NULL);
22101 		tmp_ill = next_ill->ill_usesrc_grp_next;
22102 		ASSERT(tmp_ill != NULL);
22103 		next_ill->ill_usesrc_grp_next = NULL;
22104 		next_ill->ill_usesrc_ifindex = 0;
22105 		next_ill = tmp_ill;
22106 	} while (next_ill->ill_usesrc_ifindex != 0);
22107 	uill->ill_usesrc_grp_next = NULL;
22108 }
22109 
22110 /*
22111  * Remove the client usesrc ILL from the list and relink to a new list
22112  */
22113 int
22114 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22115 {
22116 	ill_t *ill, *tmp_ill;
22117 	ip_stack_t	*ipst = ucill->ill_ipst;
22118 
22119 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22120 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22121 
22122 	/*
22123 	 * Check if the usesrc client ILL passed in is not already
22124 	 * in use as a usesrc ILL i.e one whose source address is
22125 	 * in use OR a usesrc ILL is not already in use as a usesrc
22126 	 * client ILL
22127 	 */
22128 	if ((ucill->ill_usesrc_ifindex == 0) ||
22129 	    (uill->ill_usesrc_ifindex != 0)) {
22130 		return (-1);
22131 	}
22132 
22133 	ill = ill_prev_usesrc(ucill);
22134 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22135 
22136 	/* Remove from the current list */
22137 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22138 		/* Only two elements in the list */
22139 		ASSERT(ill->ill_usesrc_ifindex == 0);
22140 		ill->ill_usesrc_grp_next = NULL;
22141 	} else {
22142 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22143 	}
22144 
22145 	if (ifindex == 0) {
22146 		ucill->ill_usesrc_ifindex = 0;
22147 		ucill->ill_usesrc_grp_next = NULL;
22148 		return (0);
22149 	}
22150 
22151 	ucill->ill_usesrc_ifindex = ifindex;
22152 	tmp_ill = uill->ill_usesrc_grp_next;
22153 	uill->ill_usesrc_grp_next = ucill;
22154 	ucill->ill_usesrc_grp_next =
22155 	    (tmp_ill != NULL) ? tmp_ill : uill;
22156 	return (0);
22157 }
22158 
22159 /*
22160  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22161  * ip.c for locking details.
22162  */
22163 /* ARGSUSED */
22164 int
22165 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22166     ip_ioctl_cmd_t *ipip, void *ifreq)
22167 {
22168 	struct lifreq *lifr = (struct lifreq *)ifreq;
22169 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22170 	    ill_flag_changed = B_FALSE;
22171 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22172 	int err = 0, ret;
22173 	uint_t ifindex;
22174 	phyint_t *us_phyint, *us_cli_phyint;
22175 	ipsq_t *ipsq = NULL;
22176 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22177 
22178 	ASSERT(IAM_WRITER_IPIF(ipif));
22179 	ASSERT(q->q_next == NULL);
22180 	ASSERT(CONN_Q(q));
22181 
22182 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22183 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22184 
22185 	ASSERT(us_cli_phyint != NULL);
22186 
22187 	/*
22188 	 * If the client ILL is being used for IPMP, abort.
22189 	 * Note, this can be done before ipsq_try_enter since we are already
22190 	 * exclusive on this ILL
22191 	 */
22192 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22193 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22194 		return (EINVAL);
22195 	}
22196 
22197 	ifindex = lifr->lifr_index;
22198 	if (ifindex == 0) {
22199 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22200 			/* non usesrc group interface, nothing to reset */
22201 			return (0);
22202 		}
22203 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22204 		/* valid reset request */
22205 		reset_flg = B_TRUE;
22206 	}
22207 
22208 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22209 	    ip_process_ioctl, &err, ipst);
22210 
22211 	if (usesrc_ill == NULL) {
22212 		return (err);
22213 	}
22214 
22215 	/*
22216 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22217 	 * group nor can either of the interfaces be used for standy. So
22218 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22219 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22220 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22221 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22222 	 * the usesrc_cli_ill
22223 	 */
22224 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22225 	    NEW_OP, B_TRUE);
22226 	if (ipsq == NULL) {
22227 		err = EINPROGRESS;
22228 		/* Operation enqueued on the ipsq of the usesrc ILL */
22229 		goto done;
22230 	}
22231 
22232 	/* Check if the usesrc_ill is used for IPMP */
22233 	us_phyint = usesrc_ill->ill_phyint;
22234 	if ((us_phyint->phyint_groupname != NULL) ||
22235 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22236 		err = EINVAL;
22237 		goto done;
22238 	}
22239 
22240 	/*
22241 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22242 	 * already a client then return EINVAL
22243 	 */
22244 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22245 		err = EINVAL;
22246 		goto done;
22247 	}
22248 
22249 	/*
22250 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22251 	 * be then this is a duplicate operation.
22252 	 */
22253 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22254 		err = 0;
22255 		goto done;
22256 	}
22257 
22258 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22259 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22260 	    usesrc_ill->ill_isv6));
22261 
22262 	/*
22263 	 * The next step ensures that no new ires will be created referencing
22264 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22265 	 * we go through an ire walk deleting all ire caches that reference
22266 	 * the client ill. New ires referencing the client ill that are added
22267 	 * to the ire table before the ILL_CHANGING flag is set, will be
22268 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22269 	 * the client ill while the ILL_CHANGING flag is set will be failed
22270 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22271 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22272 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22273 	 * belong to the same usesrc group.
22274 	 */
22275 	mutex_enter(&usesrc_cli_ill->ill_lock);
22276 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22277 	mutex_exit(&usesrc_cli_ill->ill_lock);
22278 	ill_flag_changed = B_TRUE;
22279 
22280 	if (ipif->ipif_isv6)
22281 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22282 		    ALL_ZONES, ipst);
22283 	else
22284 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22285 		    ALL_ZONES, ipst);
22286 
22287 	/*
22288 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22289 	 * and the ill_usesrc_ifindex fields
22290 	 */
22291 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22292 
22293 	if (reset_flg) {
22294 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22295 		if (ret != 0) {
22296 			err = EINVAL;
22297 		}
22298 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22299 		goto done;
22300 	}
22301 
22302 	/*
22303 	 * Four possibilities to consider:
22304 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22305 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22306 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22307 	 * 4. Both are part of their respective usesrc groups
22308 	 */
22309 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22310 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22311 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22312 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22313 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22314 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22315 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22316 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22317 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22318 		/* Insert at head of list */
22319 		usesrc_cli_ill->ill_usesrc_grp_next =
22320 		    usesrc_ill->ill_usesrc_grp_next;
22321 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22322 	} else {
22323 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22324 		    ifindex);
22325 		if (ret != 0)
22326 			err = EINVAL;
22327 	}
22328 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22329 
22330 done:
22331 	if (ill_flag_changed) {
22332 		mutex_enter(&usesrc_cli_ill->ill_lock);
22333 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22334 		mutex_exit(&usesrc_cli_ill->ill_lock);
22335 	}
22336 	if (ipsq != NULL)
22337 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22338 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22339 	ill_refrele(usesrc_ill);
22340 	return (err);
22341 }
22342 
22343 /*
22344  * comparison function used by avl.
22345  */
22346 static int
22347 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22348 {
22349 
22350 	uint_t index;
22351 
22352 	ASSERT(phyip != NULL && index_ptr != NULL);
22353 
22354 	index = *((uint_t *)index_ptr);
22355 	/*
22356 	 * let the phyint with the lowest index be on top.
22357 	 */
22358 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22359 		return (1);
22360 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22361 		return (-1);
22362 	return (0);
22363 }
22364 
22365 /*
22366  * comparison function used by avl.
22367  */
22368 static int
22369 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22370 {
22371 	ill_t *ill;
22372 	int res = 0;
22373 
22374 	ASSERT(phyip != NULL && name_ptr != NULL);
22375 
22376 	if (((phyint_t *)phyip)->phyint_illv4)
22377 		ill = ((phyint_t *)phyip)->phyint_illv4;
22378 	else
22379 		ill = ((phyint_t *)phyip)->phyint_illv6;
22380 	ASSERT(ill != NULL);
22381 
22382 	res = strcmp(ill->ill_name, (char *)name_ptr);
22383 	if (res > 0)
22384 		return (1);
22385 	else if (res < 0)
22386 		return (-1);
22387 	return (0);
22388 }
22389 /*
22390  * This function is called from ill_delete when the ill is being
22391  * unplumbed. We remove the reference from the phyint and we also
22392  * free the phyint when there are no more references to it.
22393  */
22394 static void
22395 ill_phyint_free(ill_t *ill)
22396 {
22397 	phyint_t *phyi;
22398 	phyint_t *next_phyint;
22399 	ipsq_t *cur_ipsq;
22400 	ip_stack_t	*ipst = ill->ill_ipst;
22401 
22402 	ASSERT(ill->ill_phyint != NULL);
22403 
22404 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22405 	phyi = ill->ill_phyint;
22406 	ill->ill_phyint = NULL;
22407 	/*
22408 	 * ill_init allocates a phyint always to store the copy
22409 	 * of flags relevant to phyint. At that point in time, we could
22410 	 * not assign the name and hence phyint_illv4/v6 could not be
22411 	 * initialized. Later in ipif_set_values, we assign the name to
22412 	 * the ill, at which point in time we assign phyint_illv4/v6.
22413 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22414 	 */
22415 	if (ill->ill_flags & ILLF_IPV6) {
22416 		phyi->phyint_illv6 = NULL;
22417 	} else {
22418 		phyi->phyint_illv4 = NULL;
22419 	}
22420 	/*
22421 	 * ipif_down removes it from the group when the last ipif goes
22422 	 * down.
22423 	 */
22424 	ASSERT(ill->ill_group == NULL);
22425 
22426 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22427 		return;
22428 
22429 	/*
22430 	 * Make sure this phyint was put in the list.
22431 	 */
22432 	if (phyi->phyint_ifindex > 0) {
22433 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22434 		    phyi);
22435 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22436 		    phyi);
22437 	}
22438 	/*
22439 	 * remove phyint from the ipsq list.
22440 	 */
22441 	cur_ipsq = phyi->phyint_ipsq;
22442 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22443 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22444 	} else {
22445 		next_phyint = cur_ipsq->ipsq_phyint_list;
22446 		while (next_phyint != NULL) {
22447 			if (next_phyint->phyint_ipsq_next == phyi) {
22448 				next_phyint->phyint_ipsq_next =
22449 				    phyi->phyint_ipsq_next;
22450 				break;
22451 			}
22452 			next_phyint = next_phyint->phyint_ipsq_next;
22453 		}
22454 		ASSERT(next_phyint != NULL);
22455 	}
22456 	IPSQ_DEC_REF(cur_ipsq, ipst);
22457 
22458 	if (phyi->phyint_groupname_len != 0) {
22459 		ASSERT(phyi->phyint_groupname != NULL);
22460 		mi_free(phyi->phyint_groupname);
22461 	}
22462 	mi_free(phyi);
22463 }
22464 
22465 /*
22466  * Attach the ill to the phyint structure which can be shared by both
22467  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22468  * function is called from ipif_set_values and ill_lookup_on_name (for
22469  * loopback) where we know the name of the ill. We lookup the ill and if
22470  * there is one present already with the name use that phyint. Otherwise
22471  * reuse the one allocated by ill_init.
22472  */
22473 static void
22474 ill_phyint_reinit(ill_t *ill)
22475 {
22476 	boolean_t isv6 = ill->ill_isv6;
22477 	phyint_t *phyi_old;
22478 	phyint_t *phyi;
22479 	avl_index_t where = 0;
22480 	ill_t	*ill_other = NULL;
22481 	ipsq_t	*ipsq;
22482 	ip_stack_t	*ipst = ill->ill_ipst;
22483 
22484 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22485 
22486 	phyi_old = ill->ill_phyint;
22487 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22488 	    phyi_old->phyint_illv6 == NULL));
22489 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22490 	    phyi_old->phyint_illv4 == NULL));
22491 	ASSERT(phyi_old->phyint_ifindex == 0);
22492 
22493 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22494 	    ill->ill_name, &where);
22495 
22496 	/*
22497 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22498 	 *    the global list of ills. So no other thread could have located
22499 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22500 	 * 2. Now locate the other protocol instance of this ill.
22501 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22502 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22503 	 *    of neither ill can change.
22504 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22505 	 *    other ill.
22506 	 * 5. Release all locks.
22507 	 */
22508 
22509 	/*
22510 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22511 	 * we are initializing IPv4.
22512 	 */
22513 	if (phyi != NULL) {
22514 		ill_other = (isv6) ? phyi->phyint_illv4 :
22515 		    phyi->phyint_illv6;
22516 		ASSERT(ill_other->ill_phyint != NULL);
22517 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22518 		    (!isv6 && ill_other->ill_isv6));
22519 		GRAB_ILL_LOCKS(ill, ill_other);
22520 		/*
22521 		 * We are potentially throwing away phyint_flags which
22522 		 * could be different from the one that we obtain from
22523 		 * ill_other->ill_phyint. But it is okay as we are assuming
22524 		 * that the state maintained within IP is correct.
22525 		 */
22526 		mutex_enter(&phyi->phyint_lock);
22527 		if (isv6) {
22528 			ASSERT(phyi->phyint_illv6 == NULL);
22529 			phyi->phyint_illv6 = ill;
22530 		} else {
22531 			ASSERT(phyi->phyint_illv4 == NULL);
22532 			phyi->phyint_illv4 = ill;
22533 		}
22534 		/*
22535 		 * This is a new ill, currently undergoing SLIFNAME
22536 		 * So we could not have joined an IPMP group until now.
22537 		 */
22538 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22539 		    phyi_old->phyint_groupname == NULL);
22540 
22541 		/*
22542 		 * This phyi_old is going away. Decref ipsq_refs and
22543 		 * assert it is zero. The ipsq itself will be freed in
22544 		 * ipsq_exit
22545 		 */
22546 		ipsq = phyi_old->phyint_ipsq;
22547 		IPSQ_DEC_REF(ipsq, ipst);
22548 		ASSERT(ipsq->ipsq_refs == 0);
22549 		/* Get the singleton phyint out of the ipsq list */
22550 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22551 		ipsq->ipsq_phyint_list = NULL;
22552 		phyi_old->phyint_illv4 = NULL;
22553 		phyi_old->phyint_illv6 = NULL;
22554 		mi_free(phyi_old);
22555 	} else {
22556 		mutex_enter(&ill->ill_lock);
22557 		/*
22558 		 * We don't need to acquire any lock, since
22559 		 * the ill is not yet visible globally  and we
22560 		 * have not yet released the ill_g_lock.
22561 		 */
22562 		phyi = phyi_old;
22563 		mutex_enter(&phyi->phyint_lock);
22564 		/* XXX We need a recovery strategy here. */
22565 		if (!phyint_assign_ifindex(phyi, ipst))
22566 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22567 
22568 		/* No IPMP group yet, thus the hook uses the ifindex */
22569 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22570 
22571 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22572 		    (void *)phyi, where);
22573 
22574 		(void) avl_find(&ipst->ips_phyint_g_list->
22575 		    phyint_list_avl_by_index,
22576 		    &phyi->phyint_ifindex, &where);
22577 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22578 		    (void *)phyi, where);
22579 	}
22580 
22581 	/*
22582 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22583 	 * pending mp is not affected because that is per ill basis.
22584 	 */
22585 	ill->ill_phyint = phyi;
22586 
22587 	/*
22588 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22589 	 * We do this here as when the first ipif was allocated,
22590 	 * ipif_allocate does not know the right interface index.
22591 	 */
22592 
22593 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22594 	/*
22595 	 * Now that the phyint's ifindex has been assigned, complete the
22596 	 * remaining
22597 	 */
22598 
22599 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22600 	if (ill->ill_isv6) {
22601 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22602 		    ill->ill_phyint->phyint_ifindex;
22603 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22604 	} else {
22605 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22606 	}
22607 
22608 	/*
22609 	 * Generate an event within the hooks framework to indicate that
22610 	 * a new interface has just been added to IP.  For this event to
22611 	 * be generated, the network interface must, at least, have an
22612 	 * ifindex assigned to it.
22613 	 *
22614 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22615 	 * that the ordering of delivered events to listeners matches the
22616 	 * order of them in the kernel.
22617 	 *
22618 	 * This function could be called from ill_lookup_on_name. In that case
22619 	 * the interface is loopback "lo", which will not generate a NIC event.
22620 	 */
22621 	if (ill->ill_name_length <= 2 ||
22622 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22623 		/*
22624 		 * Generate nic plumb event for ill_name even if
22625 		 * ipmp_hook_emulation is set. That avoids generating events
22626 		 * for the ill_names should ipmp_hook_emulation be turned on
22627 		 * later.
22628 		 */
22629 		ill_nic_info_plumb(ill, B_FALSE);
22630 	}
22631 	RELEASE_ILL_LOCKS(ill, ill_other);
22632 	mutex_exit(&phyi->phyint_lock);
22633 }
22634 
22635 /*
22636  * Allocate a NE_PLUMB nic info event and store in the ill.
22637  * If 'group' is set we do it for the group name, otherwise the ill name.
22638  * It will be sent when we leave the ipsq.
22639  */
22640 void
22641 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22642 {
22643 	phyint_t	*phyi = ill->ill_phyint;
22644 	char		*name;
22645 	int		namelen;
22646 
22647 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22648 
22649 	if (group) {
22650 		ASSERT(phyi->phyint_groupname_len != 0);
22651 		namelen = phyi->phyint_groupname_len;
22652 		name = phyi->phyint_groupname;
22653 	} else {
22654 		namelen = ill->ill_name_length;
22655 		name = ill->ill_name;
22656 	}
22657 
22658 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22659 }
22660 
22661 /*
22662  * Unhook the nic event message from the ill and enqueue it
22663  * into the nic event taskq.
22664  */
22665 void
22666 ill_nic_info_dispatch(ill_t *ill)
22667 {
22668 	hook_nic_event_t *info;
22669 
22670 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22671 
22672 	if ((info = ill->ill_nic_event_info) != NULL) {
22673 		if (ddi_taskq_dispatch(eventq_queue_nic,
22674 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22675 			ip2dbg(("ill_nic_info_dispatch: "
22676 			    "ddi_taskq_dispatch failed\n"));
22677 			if (info->hne_data != NULL)
22678 				kmem_free(info->hne_data, info->hne_datalen);
22679 			kmem_free(info, sizeof (hook_nic_event_t));
22680 		}
22681 		ill->ill_nic_event_info = NULL;
22682 	}
22683 }
22684 
22685 /*
22686  * Notify any downstream modules of the name of this interface.
22687  * An M_IOCTL is used even though we don't expect a successful reply.
22688  * Any reply message from the driver (presumably an M_IOCNAK) will
22689  * eventually get discarded somewhere upstream.  The message format is
22690  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22691  * to IP.
22692  */
22693 static void
22694 ip_ifname_notify(ill_t *ill, queue_t *q)
22695 {
22696 	mblk_t *mp1, *mp2;
22697 	struct iocblk *iocp;
22698 	struct lifreq *lifr;
22699 
22700 	mp1 = mkiocb(SIOCSLIFNAME);
22701 	if (mp1 == NULL)
22702 		return;
22703 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22704 	if (mp2 == NULL) {
22705 		freeb(mp1);
22706 		return;
22707 	}
22708 
22709 	mp1->b_cont = mp2;
22710 	iocp = (struct iocblk *)mp1->b_rptr;
22711 	iocp->ioc_count = sizeof (struct lifreq);
22712 
22713 	lifr = (struct lifreq *)mp2->b_rptr;
22714 	mp2->b_wptr += sizeof (struct lifreq);
22715 	bzero(lifr, sizeof (struct lifreq));
22716 
22717 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22718 	lifr->lifr_ppa = ill->ill_ppa;
22719 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22720 
22721 	putnext(q, mp1);
22722 }
22723 
22724 static int
22725 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22726 {
22727 	int err;
22728 	ip_stack_t	*ipst = ill->ill_ipst;
22729 
22730 	/* Set the obsolete NDD per-interface forwarding name. */
22731 	err = ill_set_ndd_name(ill);
22732 	if (err != 0) {
22733 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22734 		    err);
22735 	}
22736 
22737 	/* Tell downstream modules where they are. */
22738 	ip_ifname_notify(ill, q);
22739 
22740 	/*
22741 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22742 	 * Error cases are ENOMEM ...
22743 	 */
22744 	err = ill_dl_phys(ill, ipif, mp, q);
22745 
22746 	/*
22747 	 * If there is no IRE expiration timer running, get one started.
22748 	 * igmp and mld timers will be triggered by the first multicast
22749 	 */
22750 	if (ipst->ips_ip_ire_expire_id == 0) {
22751 		/*
22752 		 * acquire the lock and check again.
22753 		 */
22754 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22755 		if (ipst->ips_ip_ire_expire_id == 0) {
22756 			ipst->ips_ip_ire_expire_id = timeout(
22757 			    ip_trash_timer_expire, ipst,
22758 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22759 		}
22760 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22761 	}
22762 
22763 	if (ill->ill_isv6) {
22764 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22765 		if (ipst->ips_mld_slowtimeout_id == 0) {
22766 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22767 			    (void *)ipst,
22768 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22769 		}
22770 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22771 	} else {
22772 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22773 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22774 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22775 			    (void *)ipst,
22776 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22777 		}
22778 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22779 	}
22780 
22781 	return (err);
22782 }
22783 
22784 /*
22785  * Common routine for ppa and ifname setting. Should be called exclusive.
22786  *
22787  * Returns EINPROGRESS when mp has been consumed by queueing it on
22788  * ill_pending_mp and the ioctl will complete in ip_rput.
22789  *
22790  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22791  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22792  * For SLIFNAME, we pass these values back to the userland.
22793  */
22794 static int
22795 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22796 {
22797 	ill_t	*ill;
22798 	ipif_t	*ipif;
22799 	ipsq_t	*ipsq;
22800 	char	*ppa_ptr;
22801 	char	*old_ptr;
22802 	char	old_char;
22803 	int	error;
22804 	ip_stack_t	*ipst;
22805 
22806 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22807 	ASSERT(q->q_next != NULL);
22808 	ASSERT(interf_name != NULL);
22809 
22810 	ill = (ill_t *)q->q_ptr;
22811 	ipst = ill->ill_ipst;
22812 
22813 	ASSERT(ill->ill_ipst != NULL);
22814 	ASSERT(ill->ill_name[0] == '\0');
22815 	ASSERT(IAM_WRITER_ILL(ill));
22816 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22817 	ASSERT(ill->ill_ppa == UINT_MAX);
22818 
22819 	/* The ppa is sent down by ifconfig or is chosen */
22820 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22821 		return (EINVAL);
22822 	}
22823 
22824 	/*
22825 	 * make sure ppa passed in is same as ppa in the name.
22826 	 * This check is not made when ppa == UINT_MAX in that case ppa
22827 	 * in the name could be anything. System will choose a ppa and
22828 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22829 	 */
22830 	if (*new_ppa_ptr != UINT_MAX) {
22831 		/* stoi changes the pointer */
22832 		old_ptr = ppa_ptr;
22833 		/*
22834 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22835 		 * (they don't have an externally visible ppa).  We assign one
22836 		 * here so that we can manage the interface.  Note that in
22837 		 * the past this value was always 0 for DLPI 1 drivers.
22838 		 */
22839 		if (*new_ppa_ptr == 0)
22840 			*new_ppa_ptr = stoi(&old_ptr);
22841 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22842 			return (EINVAL);
22843 	}
22844 	/*
22845 	 * terminate string before ppa
22846 	 * save char at that location.
22847 	 */
22848 	old_char = ppa_ptr[0];
22849 	ppa_ptr[0] = '\0';
22850 
22851 	ill->ill_ppa = *new_ppa_ptr;
22852 	/*
22853 	 * Finish as much work now as possible before calling ill_glist_insert
22854 	 * which makes the ill globally visible and also merges it with the
22855 	 * other protocol instance of this phyint. The remaining work is
22856 	 * done after entering the ipsq which may happen sometime later.
22857 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22858 	 */
22859 	ipif = ill->ill_ipif;
22860 
22861 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22862 	ipif_assign_seqid(ipif);
22863 
22864 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22865 		ill->ill_flags |= ILLF_IPV4;
22866 
22867 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22868 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22869 
22870 	if (ill->ill_flags & ILLF_IPV6) {
22871 
22872 		ill->ill_isv6 = B_TRUE;
22873 		if (ill->ill_rq != NULL) {
22874 			ill->ill_rq->q_qinfo = &iprinitv6;
22875 			ill->ill_wq->q_qinfo = &ipwinitv6;
22876 		}
22877 
22878 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22879 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22880 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22881 		ipif->ipif_v6subnet = ipv6_all_zeros;
22882 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22883 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22884 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22885 		/*
22886 		 * point-to-point or Non-mulicast capable
22887 		 * interfaces won't do NUD unless explicitly
22888 		 * configured to do so.
22889 		 */
22890 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22891 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22892 			ill->ill_flags |= ILLF_NONUD;
22893 		}
22894 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22895 		if (ill->ill_flags & ILLF_NOARP) {
22896 			/*
22897 			 * Note: xresolv interfaces will eventually need
22898 			 * NOARP set here as well, but that will require
22899 			 * those external resolvers to have some
22900 			 * knowledge of that flag and act appropriately.
22901 			 * Not to be changed at present.
22902 			 */
22903 			ill->ill_flags &= ~ILLF_NOARP;
22904 		}
22905 		/*
22906 		 * Set the ILLF_ROUTER flag according to the global
22907 		 * IPv6 forwarding policy.
22908 		 */
22909 		if (ipst->ips_ipv6_forward != 0)
22910 			ill->ill_flags |= ILLF_ROUTER;
22911 	} else if (ill->ill_flags & ILLF_IPV4) {
22912 		ill->ill_isv6 = B_FALSE;
22913 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22914 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22915 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22916 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22917 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22918 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22919 		/*
22920 		 * Set the ILLF_ROUTER flag according to the global
22921 		 * IPv4 forwarding policy.
22922 		 */
22923 		if (ipst->ips_ip_g_forward != 0)
22924 			ill->ill_flags |= ILLF_ROUTER;
22925 	}
22926 
22927 	ASSERT(ill->ill_phyint != NULL);
22928 
22929 	/*
22930 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22931 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22932 	 */
22933 	if (!ill_allocate_mibs(ill))
22934 		return (ENOMEM);
22935 
22936 	/*
22937 	 * Pick a default sap until we get the DL_INFO_ACK back from
22938 	 * the driver.
22939 	 */
22940 	if (ill->ill_sap == 0) {
22941 		if (ill->ill_isv6)
22942 			ill->ill_sap  = IP6_DL_SAP;
22943 		else
22944 			ill->ill_sap  = IP_DL_SAP;
22945 	}
22946 
22947 	ill->ill_ifname_pending = 1;
22948 	ill->ill_ifname_pending_err = 0;
22949 
22950 	ill_refhold(ill);
22951 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
22952 	if ((error = ill_glist_insert(ill, interf_name,
22953 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22954 		ill->ill_ppa = UINT_MAX;
22955 		ill->ill_name[0] = '\0';
22956 		/*
22957 		 * undo null termination done above.
22958 		 */
22959 		ppa_ptr[0] = old_char;
22960 		rw_exit(&ipst->ips_ill_g_lock);
22961 		ill_refrele(ill);
22962 		return (error);
22963 	}
22964 
22965 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
22966 
22967 	/*
22968 	 * When we return the buffer pointed to by interf_name should contain
22969 	 * the same name as in ill_name.
22970 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
22971 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
22972 	 * so copy full name and update the ppa ptr.
22973 	 * When ppa passed in != UINT_MAX all values are correct just undo
22974 	 * null termination, this saves a bcopy.
22975 	 */
22976 	if (*new_ppa_ptr == UINT_MAX) {
22977 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
22978 		*new_ppa_ptr = ill->ill_ppa;
22979 	} else {
22980 		/*
22981 		 * undo null termination done above.
22982 		 */
22983 		ppa_ptr[0] = old_char;
22984 	}
22985 
22986 	/* Let SCTP know about this ILL */
22987 	sctp_update_ill(ill, SCTP_ILL_INSERT);
22988 
22989 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
22990 	    B_TRUE);
22991 
22992 	rw_exit(&ipst->ips_ill_g_lock);
22993 	ill_refrele(ill);
22994 	if (ipsq == NULL)
22995 		return (EINPROGRESS);
22996 
22997 	/*
22998 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
22999 	 */
23000 	if (ipsq->ipsq_current_ipif == NULL)
23001 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23002 	else
23003 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23004 
23005 	error = ipif_set_values_tail(ill, ipif, mp, q);
23006 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23007 	if (error != 0 && error != EINPROGRESS) {
23008 		/*
23009 		 * restore previous values
23010 		 */
23011 		ill->ill_isv6 = B_FALSE;
23012 	}
23013 	return (error);
23014 }
23015 
23016 
23017 void
23018 ipif_init(ip_stack_t *ipst)
23019 {
23020 	hrtime_t hrt;
23021 	int i;
23022 
23023 	/*
23024 	 * Can't call drv_getparm here as it is too early in the boot.
23025 	 * As we use ipif_src_random just for picking a different
23026 	 * source address everytime, this need not be really random.
23027 	 */
23028 	hrt = gethrtime();
23029 	ipst->ips_ipif_src_random =
23030 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23031 
23032 	for (i = 0; i < MAX_G_HEADS; i++) {
23033 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23034 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23035 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23036 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23037 	}
23038 
23039 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23040 	    ill_phyint_compare_index,
23041 	    sizeof (phyint_t),
23042 	    offsetof(struct phyint, phyint_avl_by_index));
23043 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23044 	    ill_phyint_compare_name,
23045 	    sizeof (phyint_t),
23046 	    offsetof(struct phyint, phyint_avl_by_name));
23047 }
23048 
23049 /*
23050  * Lookup the ipif corresponding to the onlink destination address. For
23051  * point-to-point interfaces, it matches with remote endpoint destination
23052  * address. For point-to-multipoint interfaces it only tries to match the
23053  * destination with the interface's subnet address. The longest, most specific
23054  * match is found to take care of such rare network configurations like -
23055  * le0: 129.146.1.1/16
23056  * le1: 129.146.2.2/24
23057  * It is used only by SO_DONTROUTE at the moment.
23058  */
23059 ipif_t *
23060 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23061 {
23062 	ipif_t	*ipif, *best_ipif;
23063 	ill_t	*ill;
23064 	ill_walk_context_t ctx;
23065 
23066 	ASSERT(zoneid != ALL_ZONES);
23067 	best_ipif = NULL;
23068 
23069 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23070 	ill = ILL_START_WALK_V4(&ctx, ipst);
23071 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23072 		mutex_enter(&ill->ill_lock);
23073 		for (ipif = ill->ill_ipif; ipif != NULL;
23074 		    ipif = ipif->ipif_next) {
23075 			if (!IPIF_CAN_LOOKUP(ipif))
23076 				continue;
23077 			if (ipif->ipif_zoneid != zoneid &&
23078 			    ipif->ipif_zoneid != ALL_ZONES)
23079 				continue;
23080 			/*
23081 			 * Point-to-point case. Look for exact match with
23082 			 * destination address.
23083 			 */
23084 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23085 				if (ipif->ipif_pp_dst_addr == addr) {
23086 					ipif_refhold_locked(ipif);
23087 					mutex_exit(&ill->ill_lock);
23088 					rw_exit(&ipst->ips_ill_g_lock);
23089 					if (best_ipif != NULL)
23090 						ipif_refrele(best_ipif);
23091 					return (ipif);
23092 				}
23093 			} else if (ipif->ipif_subnet == (addr &
23094 			    ipif->ipif_net_mask)) {
23095 				/*
23096 				 * Point-to-multipoint case. Looping through to
23097 				 * find the most specific match. If there are
23098 				 * multiple best match ipif's then prefer ipif's
23099 				 * that are UP. If there is only one best match
23100 				 * ipif and it is DOWN we must still return it.
23101 				 */
23102 				if ((best_ipif == NULL) ||
23103 				    (ipif->ipif_net_mask >
23104 				    best_ipif->ipif_net_mask) ||
23105 				    ((ipif->ipif_net_mask ==
23106 				    best_ipif->ipif_net_mask) &&
23107 				    ((ipif->ipif_flags & IPIF_UP) &&
23108 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23109 					ipif_refhold_locked(ipif);
23110 					mutex_exit(&ill->ill_lock);
23111 					rw_exit(&ipst->ips_ill_g_lock);
23112 					if (best_ipif != NULL)
23113 						ipif_refrele(best_ipif);
23114 					best_ipif = ipif;
23115 					rw_enter(&ipst->ips_ill_g_lock,
23116 					    RW_READER);
23117 					mutex_enter(&ill->ill_lock);
23118 				}
23119 			}
23120 		}
23121 		mutex_exit(&ill->ill_lock);
23122 	}
23123 	rw_exit(&ipst->ips_ill_g_lock);
23124 	return (best_ipif);
23125 }
23126 
23127 
23128 /*
23129  * Save enough information so that we can recreate the IRE if
23130  * the interface goes down and then up.
23131  */
23132 static void
23133 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23134 {
23135 	mblk_t	*save_mp;
23136 
23137 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23138 	if (save_mp != NULL) {
23139 		ifrt_t	*ifrt;
23140 
23141 		save_mp->b_wptr += sizeof (ifrt_t);
23142 		ifrt = (ifrt_t *)save_mp->b_rptr;
23143 		bzero(ifrt, sizeof (ifrt_t));
23144 		ifrt->ifrt_type = ire->ire_type;
23145 		ifrt->ifrt_addr = ire->ire_addr;
23146 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23147 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23148 		ifrt->ifrt_mask = ire->ire_mask;
23149 		ifrt->ifrt_flags = ire->ire_flags;
23150 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23151 		mutex_enter(&ipif->ipif_saved_ire_lock);
23152 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23153 		ipif->ipif_saved_ire_mp = save_mp;
23154 		ipif->ipif_saved_ire_cnt++;
23155 		mutex_exit(&ipif->ipif_saved_ire_lock);
23156 	}
23157 }
23158 
23159 
23160 static void
23161 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23162 {
23163 	mblk_t	**mpp;
23164 	mblk_t	*mp;
23165 	ifrt_t	*ifrt;
23166 
23167 	/* Remove from ipif_saved_ire_mp list if it is there */
23168 	mutex_enter(&ipif->ipif_saved_ire_lock);
23169 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23170 	    mpp = &(*mpp)->b_cont) {
23171 		/*
23172 		 * On a given ipif, the triple of address, gateway and
23173 		 * mask is unique for each saved IRE (in the case of
23174 		 * ordinary interface routes, the gateway address is
23175 		 * all-zeroes).
23176 		 */
23177 		mp = *mpp;
23178 		ifrt = (ifrt_t *)mp->b_rptr;
23179 		if (ifrt->ifrt_addr == ire->ire_addr &&
23180 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23181 		    ifrt->ifrt_mask == ire->ire_mask) {
23182 			*mpp = mp->b_cont;
23183 			ipif->ipif_saved_ire_cnt--;
23184 			freeb(mp);
23185 			break;
23186 		}
23187 	}
23188 	mutex_exit(&ipif->ipif_saved_ire_lock);
23189 }
23190 
23191 
23192 /*
23193  * IP multirouting broadcast routes handling
23194  * Append CGTP broadcast IREs to regular ones created
23195  * at ifconfig time.
23196  */
23197 static void
23198 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23199 {
23200 	ire_t *ire_prim;
23201 
23202 	ASSERT(ire != NULL);
23203 	ASSERT(ire_dst != NULL);
23204 
23205 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23206 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23207 	if (ire_prim != NULL) {
23208 		/*
23209 		 * We are in the special case of broadcasts for
23210 		 * CGTP. We add an IRE_BROADCAST that holds
23211 		 * the RTF_MULTIRT flag, the destination
23212 		 * address of ire_dst and the low level
23213 		 * info of ire_prim. In other words, CGTP
23214 		 * broadcast is added to the redundant ipif.
23215 		 */
23216 		ipif_t *ipif_prim;
23217 		ire_t  *bcast_ire;
23218 
23219 		ipif_prim = ire_prim->ire_ipif;
23220 
23221 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23222 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23223 		    (void *)ire_dst, (void *)ire_prim,
23224 		    (void *)ipif_prim));
23225 
23226 		bcast_ire = ire_create(
23227 		    (uchar_t *)&ire->ire_addr,
23228 		    (uchar_t *)&ip_g_all_ones,
23229 		    (uchar_t *)&ire_dst->ire_src_addr,
23230 		    (uchar_t *)&ire->ire_gateway_addr,
23231 		    &ipif_prim->ipif_mtu,
23232 		    NULL,
23233 		    ipif_prim->ipif_rq,
23234 		    ipif_prim->ipif_wq,
23235 		    IRE_BROADCAST,
23236 		    ipif_prim,
23237 		    0,
23238 		    0,
23239 		    0,
23240 		    ire->ire_flags,
23241 		    &ire_uinfo_null,
23242 		    NULL,
23243 		    NULL,
23244 		    ipst);
23245 
23246 		if (bcast_ire != NULL) {
23247 
23248 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23249 			    B_FALSE) == 0) {
23250 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23251 				    "added bcast_ire %p\n",
23252 				    (void *)bcast_ire));
23253 
23254 				ipif_save_ire(bcast_ire->ire_ipif,
23255 				    bcast_ire);
23256 				ire_refrele(bcast_ire);
23257 			}
23258 		}
23259 		ire_refrele(ire_prim);
23260 	}
23261 }
23262 
23263 
23264 /*
23265  * IP multirouting broadcast routes handling
23266  * Remove the broadcast ire
23267  */
23268 static void
23269 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23270 {
23271 	ire_t *ire_dst;
23272 
23273 	ASSERT(ire != NULL);
23274 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23275 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23276 	if (ire_dst != NULL) {
23277 		ire_t *ire_prim;
23278 
23279 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23280 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23281 		if (ire_prim != NULL) {
23282 			ipif_t *ipif_prim;
23283 			ire_t  *bcast_ire;
23284 
23285 			ipif_prim = ire_prim->ire_ipif;
23286 
23287 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23288 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23289 			    (void *)ire_dst, (void *)ire_prim,
23290 			    (void *)ipif_prim));
23291 
23292 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23293 			    ire->ire_gateway_addr,
23294 			    IRE_BROADCAST,
23295 			    ipif_prim, ALL_ZONES,
23296 			    NULL,
23297 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23298 			    MATCH_IRE_MASK, ipst);
23299 
23300 			if (bcast_ire != NULL) {
23301 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23302 				    "looked up bcast_ire %p\n",
23303 				    (void *)bcast_ire));
23304 				ipif_remove_ire(bcast_ire->ire_ipif,
23305 				    bcast_ire);
23306 				ire_delete(bcast_ire);
23307 				ire_refrele(bcast_ire);
23308 			}
23309 			ire_refrele(ire_prim);
23310 		}
23311 		ire_refrele(ire_dst);
23312 	}
23313 }
23314 
23315 /*
23316  * IPsec hardware acceleration capabilities related functions.
23317  */
23318 
23319 /*
23320  * Free a per-ill IPsec capabilities structure.
23321  */
23322 static void
23323 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23324 {
23325 	if (capab->auth_hw_algs != NULL)
23326 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23327 	if (capab->encr_hw_algs != NULL)
23328 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23329 	if (capab->encr_algparm != NULL)
23330 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23331 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23332 }
23333 
23334 /*
23335  * Allocate a new per-ill IPsec capabilities structure. This structure
23336  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23337  * an array which specifies, for each algorithm, whether this algorithm
23338  * is supported by the ill or not.
23339  */
23340 static ill_ipsec_capab_t *
23341 ill_ipsec_capab_alloc(void)
23342 {
23343 	ill_ipsec_capab_t *capab;
23344 	uint_t nelems;
23345 
23346 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23347 	if (capab == NULL)
23348 		return (NULL);
23349 
23350 	/* we need one bit per algorithm */
23351 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23352 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23353 
23354 	/* allocate memory to store algorithm flags */
23355 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23356 	if (capab->encr_hw_algs == NULL)
23357 		goto nomem;
23358 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23359 	if (capab->auth_hw_algs == NULL)
23360 		goto nomem;
23361 	/*
23362 	 * Leave encr_algparm NULL for now since we won't need it half
23363 	 * the time
23364 	 */
23365 	return (capab);
23366 
23367 nomem:
23368 	ill_ipsec_capab_free(capab);
23369 	return (NULL);
23370 }
23371 
23372 /*
23373  * Resize capability array.  Since we're exclusive, this is OK.
23374  */
23375 static boolean_t
23376 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23377 {
23378 	ipsec_capab_algparm_t *nalp, *oalp;
23379 	uint32_t olen, nlen;
23380 
23381 	oalp = capab->encr_algparm;
23382 	olen = capab->encr_algparm_size;
23383 
23384 	if (oalp != NULL) {
23385 		if (algid < capab->encr_algparm_end)
23386 			return (B_TRUE);
23387 	}
23388 
23389 	nlen = (algid + 1) * sizeof (*nalp);
23390 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23391 	if (nalp == NULL)
23392 		return (B_FALSE);
23393 
23394 	if (oalp != NULL) {
23395 		bcopy(oalp, nalp, olen);
23396 		kmem_free(oalp, olen);
23397 	}
23398 	capab->encr_algparm = nalp;
23399 	capab->encr_algparm_size = nlen;
23400 	capab->encr_algparm_end = algid + 1;
23401 
23402 	return (B_TRUE);
23403 }
23404 
23405 /*
23406  * Compare the capabilities of the specified ill with the protocol
23407  * and algorithms specified by the SA passed as argument.
23408  * If they match, returns B_TRUE, B_FALSE if they do not match.
23409  *
23410  * The ill can be passed as a pointer to it, or by specifying its index
23411  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23412  *
23413  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23414  * packet is eligible for hardware acceleration, and by
23415  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23416  * to a particular ill.
23417  */
23418 boolean_t
23419 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23420     ipsa_t *sa, netstack_t *ns)
23421 {
23422 	boolean_t sa_isv6;
23423 	uint_t algid;
23424 	struct ill_ipsec_capab_s *cpp;
23425 	boolean_t need_refrele = B_FALSE;
23426 	ip_stack_t	*ipst = ns->netstack_ip;
23427 
23428 	if (ill == NULL) {
23429 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23430 		    NULL, NULL, NULL, ipst);
23431 		if (ill == NULL) {
23432 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23433 			return (B_FALSE);
23434 		}
23435 		need_refrele = B_TRUE;
23436 	}
23437 
23438 	/*
23439 	 * Use the address length specified by the SA to determine
23440 	 * if it corresponds to a IPv6 address, and fail the matching
23441 	 * if the isv6 flag passed as argument does not match.
23442 	 * Note: this check is used for SADB capability checking before
23443 	 * sending SA information to an ill.
23444 	 */
23445 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23446 	if (sa_isv6 != ill_isv6)
23447 		/* protocol mismatch */
23448 		goto done;
23449 
23450 	/*
23451 	 * Check if the ill supports the protocol, algorithm(s) and
23452 	 * key size(s) specified by the SA, and get the pointers to
23453 	 * the algorithms supported by the ill.
23454 	 */
23455 	switch (sa->ipsa_type) {
23456 
23457 	case SADB_SATYPE_ESP:
23458 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23459 			/* ill does not support ESP acceleration */
23460 			goto done;
23461 		cpp = ill->ill_ipsec_capab_esp;
23462 		algid = sa->ipsa_auth_alg;
23463 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23464 			goto done;
23465 		algid = sa->ipsa_encr_alg;
23466 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23467 			goto done;
23468 		if (algid < cpp->encr_algparm_end) {
23469 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23470 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23471 				goto done;
23472 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23473 				goto done;
23474 		}
23475 		break;
23476 
23477 	case SADB_SATYPE_AH:
23478 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23479 			/* ill does not support AH acceleration */
23480 			goto done;
23481 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23482 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23483 			goto done;
23484 		break;
23485 	}
23486 
23487 	if (need_refrele)
23488 		ill_refrele(ill);
23489 	return (B_TRUE);
23490 done:
23491 	if (need_refrele)
23492 		ill_refrele(ill);
23493 	return (B_FALSE);
23494 }
23495 
23496 
23497 /*
23498  * Add a new ill to the list of IPsec capable ills.
23499  * Called from ill_capability_ipsec_ack() when an ACK was received
23500  * indicating that IPsec hardware processing was enabled for an ill.
23501  *
23502  * ill must point to the ill for which acceleration was enabled.
23503  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23504  */
23505 static void
23506 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23507 {
23508 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23509 	uint_t sa_type;
23510 	uint_t ipproto;
23511 	ip_stack_t	*ipst = ill->ill_ipst;
23512 
23513 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23514 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23515 
23516 	switch (dl_cap) {
23517 	case DL_CAPAB_IPSEC_AH:
23518 		sa_type = SADB_SATYPE_AH;
23519 		ills = &ipst->ips_ipsec_capab_ills_ah;
23520 		ipproto = IPPROTO_AH;
23521 		break;
23522 	case DL_CAPAB_IPSEC_ESP:
23523 		sa_type = SADB_SATYPE_ESP;
23524 		ills = &ipst->ips_ipsec_capab_ills_esp;
23525 		ipproto = IPPROTO_ESP;
23526 		break;
23527 	}
23528 
23529 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23530 
23531 	/*
23532 	 * Add ill index to list of hardware accelerators. If
23533 	 * already in list, do nothing.
23534 	 */
23535 	for (cur_ill = *ills; cur_ill != NULL &&
23536 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23537 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23538 		;
23539 
23540 	if (cur_ill == NULL) {
23541 		/* if this is a new entry for this ill */
23542 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23543 		if (new_ill == NULL) {
23544 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23545 			return;
23546 		}
23547 
23548 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23549 		new_ill->ill_isv6 = ill->ill_isv6;
23550 		new_ill->next = *ills;
23551 		*ills = new_ill;
23552 	} else if (!sadb_resync) {
23553 		/* not resync'ing SADB and an entry exists for this ill */
23554 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23555 		return;
23556 	}
23557 
23558 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23559 
23560 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23561 		/*
23562 		 * IPsec module for protocol loaded, initiate dump
23563 		 * of the SADB to this ill.
23564 		 */
23565 		sadb_ill_download(ill, sa_type);
23566 }
23567 
23568 /*
23569  * Remove an ill from the list of IPsec capable ills.
23570  */
23571 static void
23572 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23573 {
23574 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23575 	ip_stack_t	*ipst = ill->ill_ipst;
23576 
23577 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23578 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23579 
23580 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23581 	    &ipst->ips_ipsec_capab_ills_esp;
23582 
23583 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23584 
23585 	prev_ill = NULL;
23586 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23587 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23588 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23589 		;
23590 	if (cur_ill == NULL) {
23591 		/* entry not found */
23592 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23593 		return;
23594 	}
23595 	if (prev_ill == NULL) {
23596 		/* entry at front of list */
23597 		*ills = NULL;
23598 	} else {
23599 		prev_ill->next = cur_ill->next;
23600 	}
23601 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23602 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23603 }
23604 
23605 /*
23606  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23607  * supporting the specified IPsec protocol acceleration.
23608  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23609  * We free the mblk and, if sa is non-null, release the held referece.
23610  */
23611 void
23612 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23613     netstack_t *ns)
23614 {
23615 	ipsec_capab_ill_t *ici, *cur_ici;
23616 	ill_t *ill;
23617 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23618 	ip_stack_t	*ipst = ns->netstack_ip;
23619 
23620 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23621 	    ipst->ips_ipsec_capab_ills_esp;
23622 
23623 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23624 
23625 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23626 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23627 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23628 
23629 		/*
23630 		 * Handle the case where the ill goes away while the SADB is
23631 		 * attempting to send messages.  If it's going away, it's
23632 		 * nuking its shadow SADB, so we don't care..
23633 		 */
23634 
23635 		if (ill == NULL)
23636 			continue;
23637 
23638 		if (sa != NULL) {
23639 			/*
23640 			 * Make sure capabilities match before
23641 			 * sending SA to ill.
23642 			 */
23643 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23644 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23645 				ill_refrele(ill);
23646 				continue;
23647 			}
23648 
23649 			mutex_enter(&sa->ipsa_lock);
23650 			sa->ipsa_flags |= IPSA_F_HW;
23651 			mutex_exit(&sa->ipsa_lock);
23652 		}
23653 
23654 		/*
23655 		 * Copy template message, and add it to the front
23656 		 * of the mblk ship list. We want to avoid holding
23657 		 * the ipsec_capab_ills_lock while sending the
23658 		 * message to the ills.
23659 		 *
23660 		 * The b_next and b_prev are temporarily used
23661 		 * to build a list of mblks to be sent down, and to
23662 		 * save the ill to which they must be sent.
23663 		 */
23664 		nmp = copymsg(mp);
23665 		if (nmp == NULL) {
23666 			ill_refrele(ill);
23667 			continue;
23668 		}
23669 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23670 		nmp->b_next = mp_ship_list;
23671 		mp_ship_list = nmp;
23672 		nmp->b_prev = (mblk_t *)ill;
23673 	}
23674 
23675 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23676 
23677 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23678 		/* restore the mblk to a sane state */
23679 		next_mp = nmp->b_next;
23680 		nmp->b_next = NULL;
23681 		ill = (ill_t *)nmp->b_prev;
23682 		nmp->b_prev = NULL;
23683 
23684 		ill_dlpi_send(ill, nmp);
23685 		ill_refrele(ill);
23686 	}
23687 
23688 	if (sa != NULL)
23689 		IPSA_REFRELE(sa);
23690 	freemsg(mp);
23691 }
23692 
23693 /*
23694  * Derive an interface id from the link layer address.
23695  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23696  */
23697 static boolean_t
23698 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23699 {
23700 	char		*addr;
23701 
23702 	if (phys_length != ETHERADDRL)
23703 		return (B_FALSE);
23704 
23705 	/* Form EUI-64 like address */
23706 	addr = (char *)&v6addr->s6_addr32[2];
23707 	bcopy((char *)phys_addr, addr, 3);
23708 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23709 	addr[3] = (char)0xff;
23710 	addr[4] = (char)0xfe;
23711 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23712 	return (B_TRUE);
23713 }
23714 
23715 /* ARGSUSED */
23716 static boolean_t
23717 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23718 {
23719 	return (B_FALSE);
23720 }
23721 
23722 /* ARGSUSED */
23723 static boolean_t
23724 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23725     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23726 {
23727 	/*
23728 	 * Multicast address mappings used over Ethernet/802.X.
23729 	 * This address is used as a base for mappings.
23730 	 */
23731 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23732 	    0x00, 0x00, 0x00};
23733 
23734 	/*
23735 	 * Extract low order 32 bits from IPv6 multicast address.
23736 	 * Or that into the link layer address, starting from the
23737 	 * second byte.
23738 	 */
23739 	*hw_start = 2;
23740 	v6_extract_mask->s6_addr32[0] = 0;
23741 	v6_extract_mask->s6_addr32[1] = 0;
23742 	v6_extract_mask->s6_addr32[2] = 0;
23743 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23744 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23745 	return (B_TRUE);
23746 }
23747 
23748 /*
23749  * Indicate by return value whether multicast is supported. If not,
23750  * this code should not touch/change any parameters.
23751  */
23752 /* ARGSUSED */
23753 static boolean_t
23754 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23755     uint32_t *hw_start, ipaddr_t *extract_mask)
23756 {
23757 	/*
23758 	 * Multicast address mappings used over Ethernet/802.X.
23759 	 * This address is used as a base for mappings.
23760 	 */
23761 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23762 	    0x00, 0x00, 0x00 };
23763 
23764 	if (phys_length != ETHERADDRL)
23765 		return (B_FALSE);
23766 
23767 	*extract_mask = htonl(0x007fffff);
23768 	*hw_start = 2;
23769 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23770 	return (B_TRUE);
23771 }
23772 
23773 /*
23774  * Derive IPoIB interface id from the link layer address.
23775  */
23776 static boolean_t
23777 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23778 {
23779 	char		*addr;
23780 
23781 	if (phys_length != 20)
23782 		return (B_FALSE);
23783 	addr = (char *)&v6addr->s6_addr32[2];
23784 	bcopy(phys_addr + 12, addr, 8);
23785 	/*
23786 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23787 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23788 	 * rules. In these cases, the IBA considers these GUIDs to be in
23789 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23790 	 * required; vendors are required not to assign global EUI-64's
23791 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23792 	 * of the interface identifier. Whether the GUID is in modified
23793 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23794 	 * bit set to 1.
23795 	 */
23796 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23797 	return (B_TRUE);
23798 }
23799 
23800 /*
23801  * Note on mapping from multicast IP addresses to IPoIB multicast link
23802  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23803  * The format of an IPoIB multicast address is:
23804  *
23805  *  4 byte QPN      Scope Sign.  Pkey
23806  * +--------------------------------------------+
23807  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23808  * +--------------------------------------------+
23809  *
23810  * The Scope and Pkey components are properties of the IBA port and
23811  * network interface. They can be ascertained from the broadcast address.
23812  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23813  */
23814 
23815 static boolean_t
23816 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23817     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23818 {
23819 	/*
23820 	 * Base IPoIB IPv6 multicast address used for mappings.
23821 	 * Does not contain the IBA scope/Pkey values.
23822 	 */
23823 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23824 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23825 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23826 
23827 	/*
23828 	 * Extract low order 80 bits from IPv6 multicast address.
23829 	 * Or that into the link layer address, starting from the
23830 	 * sixth byte.
23831 	 */
23832 	*hw_start = 6;
23833 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23834 
23835 	/*
23836 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23837 	 */
23838 	*(maddr + 5) = *(bphys_addr + 5);
23839 	*(maddr + 8) = *(bphys_addr + 8);
23840 	*(maddr + 9) = *(bphys_addr + 9);
23841 
23842 	v6_extract_mask->s6_addr32[0] = 0;
23843 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23844 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23845 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23846 	return (B_TRUE);
23847 }
23848 
23849 static boolean_t
23850 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23851     uint32_t *hw_start, ipaddr_t *extract_mask)
23852 {
23853 	/*
23854 	 * Base IPoIB IPv4 multicast address used for mappings.
23855 	 * Does not contain the IBA scope/Pkey values.
23856 	 */
23857 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23858 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23859 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23860 
23861 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23862 		return (B_FALSE);
23863 
23864 	/*
23865 	 * Extract low order 28 bits from IPv4 multicast address.
23866 	 * Or that into the link layer address, starting from the
23867 	 * sixteenth byte.
23868 	 */
23869 	*extract_mask = htonl(0x0fffffff);
23870 	*hw_start = 16;
23871 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23872 
23873 	/*
23874 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23875 	 */
23876 	*(maddr + 5) = *(bphys_addr + 5);
23877 	*(maddr + 8) = *(bphys_addr + 8);
23878 	*(maddr + 9) = *(bphys_addr + 9);
23879 	return (B_TRUE);
23880 }
23881 
23882 /*
23883  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23884  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23885  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23886  * the link-local address is preferred.
23887  */
23888 boolean_t
23889 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23890 {
23891 	ipif_t	*ipif;
23892 	ipif_t	*maybe_ipif = NULL;
23893 
23894 	mutex_enter(&ill->ill_lock);
23895 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23896 		mutex_exit(&ill->ill_lock);
23897 		if (ipifp != NULL)
23898 			*ipifp = NULL;
23899 		return (B_FALSE);
23900 	}
23901 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23902 		if (!IPIF_CAN_LOOKUP(ipif))
23903 			continue;
23904 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23905 		    ipif->ipif_zoneid != ALL_ZONES)
23906 			continue;
23907 		if ((ipif->ipif_flags & flags) != flags)
23908 			continue;
23909 
23910 		if (ipifp == NULL) {
23911 			mutex_exit(&ill->ill_lock);
23912 			ASSERT(maybe_ipif == NULL);
23913 			return (B_TRUE);
23914 		}
23915 		if (!ill->ill_isv6 ||
23916 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23917 			ipif_refhold_locked(ipif);
23918 			mutex_exit(&ill->ill_lock);
23919 			*ipifp = ipif;
23920 			return (B_TRUE);
23921 		}
23922 		if (maybe_ipif == NULL)
23923 			maybe_ipif = ipif;
23924 	}
23925 	if (ipifp != NULL) {
23926 		if (maybe_ipif != NULL)
23927 			ipif_refhold_locked(maybe_ipif);
23928 		*ipifp = maybe_ipif;
23929 	}
23930 	mutex_exit(&ill->ill_lock);
23931 	return (maybe_ipif != NULL);
23932 }
23933 
23934 /*
23935  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23936  */
23937 boolean_t
23938 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23939 {
23940 	ill_t *illg;
23941 	ip_stack_t	*ipst = ill->ill_ipst;
23942 
23943 	/*
23944 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23945 	 */
23946 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23947 		return (B_TRUE);
23948 	}
23949 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23950 	if (ill->ill_group == NULL) {
23951 		/* ill not in a group */
23952 		rw_exit(&ipst->ips_ill_g_lock);
23953 		return (B_FALSE);
23954 	}
23955 
23956 	/*
23957 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
23958 	 * group. We need to look for an ipif in the zone on all the ills in the
23959 	 * group.
23960 	 */
23961 	illg = ill->ill_group->illgrp_ill;
23962 	do {
23963 		/*
23964 		 * We don't call ipif_lookup_zoneid() on ill as we already know
23965 		 * that it's not there.
23966 		 */
23967 		if (illg != ill &&
23968 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
23969 			break;
23970 		}
23971 	} while ((illg = illg->ill_group_next) != NULL);
23972 	rw_exit(&ipst->ips_ill_g_lock);
23973 	return (illg != NULL);
23974 }
23975 
23976 /*
23977  * Check if this ill is only being used to send ICMP probes for IPMP
23978  */
23979 boolean_t
23980 ill_is_probeonly(ill_t *ill)
23981 {
23982 	/*
23983 	 * Check if the interface is FAILED, or INACTIVE
23984 	 */
23985 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
23986 		return (B_TRUE);
23987 
23988 	return (B_FALSE);
23989 }
23990 
23991 /*
23992  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
23993  * If a pointer to an ipif_t is returned then the caller will need to do
23994  * an ill_refrele().
23995  *
23996  * If there is no real interface which matches the ifindex, then it looks
23997  * for a group that has a matching index. In the case of a group match the
23998  * lifidx must be zero. We don't need emulate the logical interfaces
23999  * since IP Filter's use of netinfo doesn't use that.
24000  */
24001 ipif_t *
24002 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24003     ip_stack_t *ipst)
24004 {
24005 	ipif_t *ipif;
24006 	ill_t *ill;
24007 
24008 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24009 	    ipst);
24010 
24011 	if (ill == NULL) {
24012 		/* Fallback to group names only if hook_emulation set */
24013 		if (!ipst->ips_ipmp_hook_emulation)
24014 			return (NULL);
24015 
24016 		if (lifidx != 0)
24017 			return (NULL);
24018 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24019 		if (ill == NULL)
24020 			return (NULL);
24021 	}
24022 
24023 	mutex_enter(&ill->ill_lock);
24024 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24025 		mutex_exit(&ill->ill_lock);
24026 		ill_refrele(ill);
24027 		return (NULL);
24028 	}
24029 
24030 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24031 		if (!IPIF_CAN_LOOKUP(ipif))
24032 			continue;
24033 		if (lifidx == ipif->ipif_id) {
24034 			ipif_refhold_locked(ipif);
24035 			break;
24036 		}
24037 	}
24038 
24039 	mutex_exit(&ill->ill_lock);
24040 	ill_refrele(ill);
24041 	return (ipif);
24042 }
24043 
24044 /*
24045  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24046  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24047  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24048  * for details.
24049  */
24050 void
24051 ill_fastpath_flush(ill_t *ill)
24052 {
24053 	ip_stack_t *ipst = ill->ill_ipst;
24054 
24055 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24056 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24057 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24058 }
24059 
24060 /*
24061  * Set the physical address information for `ill' to the contents of the
24062  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24063  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24064  * EINPROGRESS will be returned.
24065  */
24066 int
24067 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24068 {
24069 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24070 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24071 
24072 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24073 
24074 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24075 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24076 		/* Changing DL_IPV6_TOKEN is not yet supported */
24077 		return (0);
24078 	}
24079 
24080 	/*
24081 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24082 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24083 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24084 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24085 	 */
24086 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24087 		freemsg(mp);
24088 		return (ENOMEM);
24089 	}
24090 
24091 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24092 
24093 	/*
24094 	 * If we can quiesce the ill, then set the address.  If not, then
24095 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24096 	 */
24097 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24098 	mutex_enter(&ill->ill_lock);
24099 	if (!ill_is_quiescent(ill)) {
24100 		/* call cannot fail since `conn_t *' argument is NULL */
24101 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24102 		    mp, ILL_DOWN);
24103 		mutex_exit(&ill->ill_lock);
24104 		return (EINPROGRESS);
24105 	}
24106 	mutex_exit(&ill->ill_lock);
24107 
24108 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24109 	return (0);
24110 }
24111 
24112 /*
24113  * Once the ill associated with `q' has quiesced, set its physical address
24114  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24115  * are passed (linked by b_cont), since we sometimes need to save two distinct
24116  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24117  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24118  * is quiesced, we know any stale IREs with the old address information have
24119  * already been removed, so we don't need to call ill_fastpath_flush().
24120  */
24121 /* ARGSUSED */
24122 static void
24123 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24124 {
24125 	ill_t		*ill = q->q_ptr;
24126 	mblk_t		*addrmp2 = unlinkb(addrmp);
24127 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24128 	uint_t		addrlen, addroff;
24129 
24130 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24131 
24132 	addroff	= dlindp->dl_addr_offset;
24133 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24134 
24135 	switch (dlindp->dl_data) {
24136 	case DL_IPV6_LINK_LAYER_ADDR:
24137 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24138 		freemsg(addrmp2);
24139 		break;
24140 
24141 	case DL_CURR_PHYS_ADDR:
24142 		freemsg(ill->ill_phys_addr_mp);
24143 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24144 		ill->ill_phys_addr_mp = addrmp;
24145 		ill->ill_phys_addr_length = addrlen;
24146 
24147 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24148 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24149 		else
24150 			freemsg(addrmp2);
24151 		break;
24152 	default:
24153 		ASSERT(0);
24154 	}
24155 
24156 	/*
24157 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24158 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24159 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24160 	 * brought up.
24161 	 */
24162 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24163 		ipsq_current_finish(ipsq);
24164 }
24165 
24166 /*
24167  * Helper routine for setting the ill_nd_lla fields.
24168  */
24169 void
24170 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24171 {
24172 	freemsg(ill->ill_nd_lla_mp);
24173 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24174 	ill->ill_nd_lla_mp = ndmp;
24175 	ill->ill_nd_lla_len = addrlen;
24176 }
24177 
24178 major_t IP_MAJ;
24179 #define	IP	"ip"
24180 
24181 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24182 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24183 
24184 /*
24185  * Issue REMOVEIF ioctls to have the loopback interfaces
24186  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24187  * the former going away when the user-level processes in the zone
24188  * are killed  * and the latter are cleaned up by the stream head
24189  * str_stack_shutdown callback that undoes all I_PLINKs.
24190  */
24191 void
24192 ip_loopback_cleanup(ip_stack_t *ipst)
24193 {
24194 	int error;
24195 	ldi_handle_t	lh = NULL;
24196 	ldi_ident_t	li = NULL;
24197 	int		rval;
24198 	cred_t		*cr;
24199 	struct strioctl iocb;
24200 	struct lifreq	lifreq;
24201 
24202 	IP_MAJ = ddi_name_to_major(IP);
24203 
24204 #ifdef NS_DEBUG
24205 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24206 	    ipst->ips_netstack->netstack_stackid);
24207 #endif
24208 
24209 	bzero(&lifreq, sizeof (lifreq));
24210 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24211 
24212 	error = ldi_ident_from_major(IP_MAJ, &li);
24213 	if (error) {
24214 #ifdef DEBUG
24215 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24216 		    error);
24217 #endif
24218 		return;
24219 	}
24220 
24221 	cr = zone_get_kcred(netstackid_to_zoneid(
24222 	    ipst->ips_netstack->netstack_stackid));
24223 	ASSERT(cr != NULL);
24224 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24225 	if (error) {
24226 #ifdef DEBUG
24227 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24228 		    error);
24229 #endif
24230 		goto out;
24231 	}
24232 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24233 	iocb.ic_timout = 15;
24234 	iocb.ic_len = sizeof (lifreq);
24235 	iocb.ic_dp = (char *)&lifreq;
24236 
24237 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24238 	/* LINTED - statement has no consequent */
24239 	if (error) {
24240 #ifdef NS_DEBUG
24241 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24242 		    "UDP6 error %d\n", error);
24243 #endif
24244 	}
24245 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24246 	lh = NULL;
24247 
24248 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24249 	if (error) {
24250 #ifdef NS_DEBUG
24251 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24252 		    error);
24253 #endif
24254 		goto out;
24255 	}
24256 
24257 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24258 	iocb.ic_timout = 15;
24259 	iocb.ic_len = sizeof (lifreq);
24260 	iocb.ic_dp = (char *)&lifreq;
24261 
24262 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24263 	/* LINTED - statement has no consequent */
24264 	if (error) {
24265 #ifdef NS_DEBUG
24266 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24267 		    "UDP error %d\n", error);
24268 #endif
24269 	}
24270 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24271 	lh = NULL;
24272 
24273 out:
24274 	/* Close layered handles */
24275 	if (lh)
24276 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24277 	if (li)
24278 		ldi_ident_release(li);
24279 
24280 	crfree(cr);
24281 }
24282 
24283 /*
24284  * This needs to be in-sync with nic_event_t definition
24285  */
24286 static const char *
24287 ill_hook_event2str(nic_event_t event)
24288 {
24289 	switch (event) {
24290 	case NE_PLUMB:
24291 		return ("PLUMB");
24292 	case NE_UNPLUMB:
24293 		return ("UNPLUMB");
24294 	case NE_UP:
24295 		return ("UP");
24296 	case NE_DOWN:
24297 		return ("DOWN");
24298 	case NE_ADDRESS_CHANGE:
24299 		return ("ADDRESS_CHANGE");
24300 	default:
24301 		return ("UNKNOWN");
24302 	}
24303 }
24304 
24305 static void
24306 ill_hook_event_destroy(ill_t *ill)
24307 {
24308 	hook_nic_event_t	*info;
24309 
24310 	if ((info = ill->ill_nic_event_info) != NULL) {
24311 		if (info->hne_data != NULL)
24312 			kmem_free(info->hne_data, info->hne_datalen);
24313 		kmem_free(info, sizeof (hook_nic_event_t));
24314 
24315 		ill->ill_nic_event_info = NULL;
24316 	}
24317 
24318 }
24319 
24320 boolean_t
24321 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24322     nic_event_data_t data, size_t datalen)
24323 {
24324 	ip_stack_t		*ipst = ill->ill_ipst;
24325 	hook_nic_event_t	*info;
24326 	const char		*str = NULL;
24327 
24328 	/* destroy nic event info if it exists */
24329 	if ((info = ill->ill_nic_event_info) != NULL) {
24330 		str = ill_hook_event2str(info->hne_event);
24331 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24332 		    "attached for %s\n", str, ill->ill_name));
24333 		ill_hook_event_destroy(ill);
24334 	}
24335 
24336 	/* create a new nic event info */
24337 	if ((info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP)) == NULL)
24338 		goto fail;
24339 
24340 	ill->ill_nic_event_info = info;
24341 
24342 	if (event == NE_UNPLUMB)
24343 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
24344 	else
24345 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24346 	info->hne_lif = lif;
24347 	info->hne_event = event;
24348 	info->hne_family = ill->ill_isv6 ?
24349 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24350 	info->hne_data = NULL;
24351 	info->hne_datalen = 0;
24352 
24353 	if (data != NULL && datalen != 0) {
24354 		info->hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24355 		if (info->hne_data != NULL) {
24356 			bcopy(data, info->hne_data, datalen);
24357 			info->hne_datalen = datalen;
24358 		} else {
24359 			ill_hook_event_destroy(ill);
24360 			goto fail;
24361 		}
24362 	}
24363 
24364 	return (B_TRUE);
24365 fail:
24366 	str = ill_hook_event2str(event);
24367 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24368 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24369 	return (B_FALSE);
24370 }
24371