xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision 15d9d0b528387242011cdcc6190c9e598cfe3a07)
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 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
135 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
136 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
137     mblk_t *mp, boolean_t need_up);
138 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139     mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
141     queue_t *q, mblk_t *mp, boolean_t need_up);
142 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
143     mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
145     mblk_t *mp);
146 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
147     queue_t *q, mblk_t *mp, boolean_t need_up);
148 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
149     int ioccmd, struct linkblk *li, boolean_t doconsist);
150 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
151 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
152 static void	ipsq_flush(ill_t *ill);
153 
154 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
155     queue_t *q, mblk_t *mp, boolean_t need_up);
156 static void	ipsq_delete(ipsq_t *);
157 
158 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
159 		    boolean_t initialize);
160 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
161 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
162 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
163 		    boolean_t isv6);
164 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
165 static void	ipif_delete_cache_ire(ire_t *, char *);
166 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
167 static void	ipif_free(ipif_t *ipif);
168 static void	ipif_free_tail(ipif_t *ipif);
169 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
170 static void	ipif_multicast_down(ipif_t *ipif);
171 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
172 static void	ipif_set_default(ipif_t *ipif);
173 static int	ipif_set_values(queue_t *q, mblk_t *mp,
174     char *interf_name, uint_t *ppa);
175 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
176     queue_t *q);
177 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
178     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
179     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
180 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
181 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
182 
183 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
184 static int	ill_arp_off(ill_t *ill);
185 static int	ill_arp_on(ill_t *ill);
186 static void	ill_delete_interface_type(ill_if_t *);
187 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
188 static void	ill_dl_down(ill_t *ill);
189 static void	ill_down(ill_t *ill);
190 static void	ill_downi(ire_t *ire, char *ill_arg);
191 static void	ill_free_mib(ill_t *ill);
192 static void	ill_glist_delete(ill_t *);
193 static boolean_t ill_has_usable_ipif(ill_t *);
194 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
195 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
196 static void	ill_phyint_free(ill_t *ill);
197 static void	ill_phyint_reinit(ill_t *ill);
198 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
199 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
200 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
201 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
202 static void	ill_stq_cache_delete(ire_t *, char *);
203 
204 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
207     in6_addr_t *);
208 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     ipaddr_t *);
210 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
211 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
212     in6_addr_t *);
213 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
214     ipaddr_t *);
215 
216 static void	ipif_save_ire(ipif_t *, ire_t *);
217 static void	ipif_remove_ire(ipif_t *, ire_t *);
218 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
219 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
220 
221 /*
222  * Per-ill IPsec capabilities management.
223  */
224 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
225 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
226 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
227 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
228 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
229 static void ill_capability_proto(ill_t *, int, mblk_t *);
230 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
231     boolean_t);
232 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
235 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
236 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
237 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
238 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
239 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
240     dl_capability_sub_t *);
241 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
242 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
243 static void ill_capability_lso_reset(ill_t *, mblk_t **);
244 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
245 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
246 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
247 static void	ill_capability_dls_disable(ill_t *);
248 
249 static void	illgrp_cache_delete(ire_t *, char *);
250 static void	illgrp_delete(ill_t *ill);
251 static void	illgrp_reset_schednext(ill_t *ill);
252 
253 static ill_t	*ill_prev_usesrc(ill_t *);
254 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
255 static void	ill_disband_usesrc_group(ill_t *);
256 
257 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
258 
259 #ifdef DEBUG
260 static	void	ill_trace_cleanup(const ill_t *);
261 static	void	ipif_trace_cleanup(const ipif_t *);
262 #endif
263 
264 /*
265  * if we go over the memory footprint limit more than once in this msec
266  * interval, we'll start pruning aggressively.
267  */
268 int ip_min_frag_prune_time = 0;
269 
270 /*
271  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
272  * and the IPsec DOI
273  */
274 #define	MAX_IPSEC_ALGS	256
275 
276 #define	BITSPERBYTE	8
277 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
278 
279 #define	IPSEC_ALG_ENABLE(algs, algid) \
280 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
281 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
282 
283 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
284 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
285 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
286 
287 typedef uint8_t ipsec_capab_elem_t;
288 
289 /*
290  * Per-algorithm parameters.  Note that at present, only encryption
291  * algorithms have variable keysize (IKE does not provide a way to negotiate
292  * auth algorithm keysize).
293  *
294  * All sizes here are in bits.
295  */
296 typedef struct
297 {
298 	uint16_t	minkeylen;
299 	uint16_t	maxkeylen;
300 } ipsec_capab_algparm_t;
301 
302 /*
303  * Per-ill capabilities.
304  */
305 struct ill_ipsec_capab_s {
306 	ipsec_capab_elem_t *encr_hw_algs;
307 	ipsec_capab_elem_t *auth_hw_algs;
308 	uint32_t algs_size;	/* size of _hw_algs in bytes */
309 	/* algorithm key lengths */
310 	ipsec_capab_algparm_t *encr_algparm;
311 	uint32_t encr_algparm_size;
312 	uint32_t encr_algparm_end;
313 };
314 
315 /*
316  * The field values are larger than strictly necessary for simple
317  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
318  */
319 static area_t	ip_area_template = {
320 	AR_ENTRY_ADD,			/* area_cmd */
321 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
322 					/* area_name_offset */
323 	/* area_name_length temporarily holds this structure length */
324 	sizeof (area_t),			/* area_name_length */
325 	IP_ARP_PROTO_TYPE,		/* area_proto */
326 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
327 	IP_ADDR_LEN,			/* area_proto_addr_length */
328 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
329 					/* area_proto_mask_offset */
330 	0,				/* area_flags */
331 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
332 					/* area_hw_addr_offset */
333 	/* Zero length hw_addr_length means 'use your idea of the address' */
334 	0				/* area_hw_addr_length */
335 };
336 
337 /*
338  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
339  * support
340  */
341 static area_t	ip6_area_template = {
342 	AR_ENTRY_ADD,			/* area_cmd */
343 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
344 					/* area_name_offset */
345 	/* area_name_length temporarily holds this structure length */
346 	sizeof (area_t),			/* area_name_length */
347 	IP_ARP_PROTO_TYPE,		/* area_proto */
348 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
349 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
350 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
351 					/* area_proto_mask_offset */
352 	0,				/* area_flags */
353 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
354 					/* area_hw_addr_offset */
355 	/* Zero length hw_addr_length means 'use your idea of the address' */
356 	0				/* area_hw_addr_length */
357 };
358 
359 static ared_t	ip_ared_template = {
360 	AR_ENTRY_DELETE,
361 	sizeof (ared_t) + IP_ADDR_LEN,
362 	sizeof (ared_t),
363 	IP_ARP_PROTO_TYPE,
364 	sizeof (ared_t),
365 	IP_ADDR_LEN
366 };
367 
368 static ared_t	ip6_ared_template = {
369 	AR_ENTRY_DELETE,
370 	sizeof (ared_t) + IPV6_ADDR_LEN,
371 	sizeof (ared_t),
372 	IP_ARP_PROTO_TYPE,
373 	sizeof (ared_t),
374 	IPV6_ADDR_LEN
375 };
376 
377 /*
378  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
379  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
380  * areq is used).
381  */
382 static areq_t	ip_areq_template = {
383 	AR_ENTRY_QUERY,			/* cmd */
384 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
385 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
386 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
387 	sizeof (areq_t),			/* target addr offset */
388 	IP_ADDR_LEN,			/* target addr_length */
389 	0,				/* flags */
390 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
391 	IP_ADDR_LEN,			/* sender addr length */
392 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
393 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
394 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
395 	/* anything else filled in by the code */
396 };
397 
398 static arc_t	ip_aru_template = {
399 	AR_INTERFACE_UP,
400 	sizeof (arc_t),		/* Name offset */
401 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
402 };
403 
404 static arc_t	ip_ard_template = {
405 	AR_INTERFACE_DOWN,
406 	sizeof (arc_t),		/* Name offset */
407 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
408 };
409 
410 static arc_t	ip_aron_template = {
411 	AR_INTERFACE_ON,
412 	sizeof (arc_t),		/* Name offset */
413 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
414 };
415 
416 static arc_t	ip_aroff_template = {
417 	AR_INTERFACE_OFF,
418 	sizeof (arc_t),		/* Name offset */
419 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
420 };
421 
422 
423 static arma_t	ip_arma_multi_template = {
424 	AR_MAPPING_ADD,
425 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
426 				/* Name offset */
427 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
428 	IP_ARP_PROTO_TYPE,
429 	sizeof (arma_t),			/* proto_addr_offset */
430 	IP_ADDR_LEN,				/* proto_addr_length */
431 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
432 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
433 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
434 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
435 	IP_MAX_HW_LEN,				/* hw_addr_length */
436 	0,					/* hw_mapping_start */
437 };
438 
439 static ipft_t	ip_ioctl_ftbl[] = {
440 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
441 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
444 		IPFT_F_NO_REPLY },
445 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
446 	{ 0 }
447 };
448 
449 /* Simple ICMP IP Header Template */
450 static ipha_t icmp_ipha = {
451 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
452 };
453 
454 /* Flag descriptors for ip_ipif_report */
455 static nv_t	ipif_nv_tbl[] = {
456 	{ IPIF_UP,		"UP" },
457 	{ IPIF_BROADCAST,	"BROADCAST" },
458 	{ ILLF_DEBUG,		"DEBUG" },
459 	{ PHYI_LOOPBACK,	"LOOPBACK" },
460 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
461 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
462 	{ PHYI_RUNNING,		"RUNNING" },
463 	{ ILLF_NOARP,		"NOARP" },
464 	{ PHYI_PROMISC,		"PROMISC" },
465 	{ PHYI_ALLMULTI,	"ALLMULTI" },
466 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
467 	{ ILLF_MULTICAST,	"MULTICAST" },
468 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
469 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
470 	{ IPIF_DHCPRUNNING,	"DHCP" },
471 	{ IPIF_PRIVATE,		"PRIVATE" },
472 	{ IPIF_NOXMIT,		"NOXMIT" },
473 	{ IPIF_NOLOCAL,		"NOLOCAL" },
474 	{ IPIF_DEPRECATED,	"DEPRECATED" },
475 	{ IPIF_PREFERRED,	"PREFERRED" },
476 	{ IPIF_TEMPORARY,	"TEMPORARY" },
477 	{ IPIF_ADDRCONF,	"ADDRCONF" },
478 	{ PHYI_VIRTUAL,		"VIRTUAL" },
479 	{ ILLF_ROUTER,		"ROUTER" },
480 	{ ILLF_NONUD,		"NONUD" },
481 	{ IPIF_ANYCAST,		"ANYCAST" },
482 	{ ILLF_NORTEXCH,	"NORTEXCH" },
483 	{ ILLF_IPV4,		"IPV4" },
484 	{ ILLF_IPV6,		"IPV6" },
485 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
486 	{ PHYI_FAILED,		"FAILED" },
487 	{ PHYI_STANDBY,		"STANDBY" },
488 	{ PHYI_INACTIVE,	"INACTIVE" },
489 	{ PHYI_OFFLINE,		"OFFLINE" },
490 };
491 
492 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
493 
494 static ip_m_t	ip_m_tbl[] = {
495 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_ether_v6intfid },
497 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_ether_v6intfid },
505 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
506 	    ip_ib_v6intfid },
507 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
508 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
509 	    ip_nodef_v6intfid }
510 };
511 
512 static ill_t	ill_null;		/* Empty ILL for init. */
513 char	ipif_loopback_name[] = "lo0";
514 static char *ipv4_forward_suffix = ":ip_forwarding";
515 static char *ipv6_forward_suffix = ":ip6_forwarding";
516 static	sin6_t	sin6_null;	/* Zero address for quick clears */
517 static	sin_t	sin_null;	/* Zero address for quick clears */
518 
519 /* When set search for unused ipif_seqid */
520 static ipif_t	ipif_zero;
521 
522 /*
523  * ppa arena is created after these many
524  * interfaces have been plumbed.
525  */
526 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
527 
528 /*
529  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
530  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
531  * set through platform specific code (Niagara/Ontario).
532  */
533 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
534 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
535 
536 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
537 
538 static uint_t
539 ipif_rand(ip_stack_t *ipst)
540 {
541 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
542 	    12345;
543 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
544 }
545 
546 /*
547  * Allocate per-interface mibs.
548  * Returns true if ok. False otherwise.
549  *  ipsq  may not yet be allocated (loopback case ).
550  */
551 static boolean_t
552 ill_allocate_mibs(ill_t *ill)
553 {
554 	/* Already allocated? */
555 	if (ill->ill_ip_mib != NULL) {
556 		if (ill->ill_isv6)
557 			ASSERT(ill->ill_icmp6_mib != NULL);
558 		return (B_TRUE);
559 	}
560 
561 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
562 	    KM_NOSLEEP);
563 	if (ill->ill_ip_mib == NULL) {
564 		return (B_FALSE);
565 	}
566 
567 	/* Setup static information */
568 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
569 	    sizeof (mib2_ipIfStatsEntry_t));
570 	if (ill->ill_isv6) {
571 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
573 		    sizeof (mib2_ipv6AddrEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
575 		    sizeof (mib2_ipv6RouteEntry_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
577 		    sizeof (mib2_ipv6NetToMediaEntry_t));
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
579 		    sizeof (ipv6_member_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
581 		    sizeof (ipv6_grpsrc_t));
582 	} else {
583 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
585 		    sizeof (mib2_ipAddrEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
587 		    sizeof (mib2_ipRouteEntry_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
589 		    sizeof (mib2_ipNetToMediaEntry_t));
590 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
591 		    sizeof (ip_member_t));
592 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
593 		    sizeof (ip_grpsrc_t));
594 
595 		/*
596 		 * For a v4 ill, we are done at this point, because per ill
597 		 * icmp mibs are only used for v6.
598 		 */
599 		return (B_TRUE);
600 	}
601 
602 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
603 	    KM_NOSLEEP);
604 	if (ill->ill_icmp6_mib == NULL) {
605 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
606 		ill->ill_ip_mib = NULL;
607 		return (B_FALSE);
608 	}
609 	/* static icmp info */
610 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
611 	    sizeof (mib2_ipv6IfIcmpEntry_t);
612 	/*
613 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
614 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
615 	 * -> ill_phyint_reinit
616 	 */
617 	return (B_TRUE);
618 }
619 
620 /*
621  * Common code for preparation of ARP commands.  Two points to remember:
622  * 	1) The ill_name is tacked on at the end of the allocated space so
623  *	   the templates name_offset field must contain the total space
624  *	   to allocate less the name length.
625  *
626  *	2) The templates name_length field should contain the *template*
627  *	   length.  We use it as a parameter to bcopy() and then write
628  *	   the real ill_name_length into the name_length field of the copy.
629  * (Always called as writer.)
630  */
631 mblk_t *
632 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
633 {
634 	arc_t	*arc = (arc_t *)template;
635 	char	*cp;
636 	int	len;
637 	mblk_t	*mp;
638 	uint_t	name_length = ill->ill_name_length;
639 	uint_t	template_len = arc->arc_name_length;
640 
641 	len = arc->arc_name_offset + name_length;
642 	mp = allocb(len, BPRI_HI);
643 	if (mp == NULL)
644 		return (NULL);
645 	cp = (char *)mp->b_rptr;
646 	mp->b_wptr = (uchar_t *)&cp[len];
647 	if (template_len)
648 		bcopy(template, cp, template_len);
649 	if (len > template_len)
650 		bzero(&cp[template_len], len - template_len);
651 	mp->b_datap->db_type = M_PROTO;
652 
653 	arc = (arc_t *)cp;
654 	arc->arc_name_length = name_length;
655 	cp = (char *)arc + arc->arc_name_offset;
656 	bcopy(ill->ill_name, cp, name_length);
657 
658 	if (addr) {
659 		area_t	*area = (area_t *)mp->b_rptr;
660 
661 		cp = (char *)area + area->area_proto_addr_offset;
662 		bcopy(addr, cp, area->area_proto_addr_length);
663 		if (area->area_cmd == AR_ENTRY_ADD) {
664 			cp = (char *)area;
665 			len = area->area_proto_addr_length;
666 			if (area->area_proto_mask_offset)
667 				cp += area->area_proto_mask_offset;
668 			else
669 				cp += area->area_proto_addr_offset + len;
670 			while (len-- > 0)
671 				*cp++ = (char)~0;
672 		}
673 	}
674 	return (mp);
675 }
676 
677 mblk_t *
678 ipif_area_alloc(ipif_t *ipif)
679 {
680 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
681 	    (char *)&ipif->ipif_lcl_addr));
682 }
683 
684 mblk_t *
685 ipif_ared_alloc(ipif_t *ipif)
686 {
687 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
688 	    (char *)&ipif->ipif_lcl_addr));
689 }
690 
691 mblk_t *
692 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
693 {
694 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
695 	    (char *)&addr));
696 }
697 
698 /*
699  * Completely vaporize a lower level tap and all associated interfaces.
700  * ill_delete is called only out of ip_close when the device control
701  * stream is being closed.
702  */
703 void
704 ill_delete(ill_t *ill)
705 {
706 	ipif_t	*ipif;
707 	ill_t	*prev_ill;
708 	ip_stack_t	*ipst = ill->ill_ipst;
709 
710 	/*
711 	 * ill_delete may be forcibly entering the ipsq. The previous
712 	 * ioctl may not have completed and may need to be aborted.
713 	 * ipsq_flush takes care of it. If we don't need to enter the
714 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
715 	 * ill_delete_tail is sufficient.
716 	 */
717 	ipsq_flush(ill);
718 
719 	/*
720 	 * Nuke all interfaces.  ipif_free will take down the interface,
721 	 * remove it from the list, and free the data structure.
722 	 * Walk down the ipif list and remove the logical interfaces
723 	 * first before removing the main ipif. We can't unplumb
724 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
725 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
726 	 * POINTOPOINT.
727 	 *
728 	 * If ill_ipif was not properly initialized (i.e low on memory),
729 	 * then no interfaces to clean up. In this case just clean up the
730 	 * ill.
731 	 */
732 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
733 		ipif_free(ipif);
734 
735 	/*
736 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
737 	 * So nobody can be using this mp now. Free the mp allocated for
738 	 * honoring ILLF_NOARP
739 	 */
740 	freemsg(ill->ill_arp_on_mp);
741 	ill->ill_arp_on_mp = NULL;
742 
743 	/* Clean up msgs on pending upcalls for mrouted */
744 	reset_mrt_ill(ill);
745 
746 	/*
747 	 * ipif_free -> reset_conn_ipif will remove all multicast
748 	 * references for IPv4. For IPv6, we need to do it here as
749 	 * it points only at ills.
750 	 */
751 	reset_conn_ill(ill);
752 
753 	/*
754 	 * ill_down will arrange to blow off any IRE's dependent on this
755 	 * ILL, and shut down fragmentation reassembly.
756 	 */
757 	ill_down(ill);
758 
759 	/* Let SCTP know, so that it can remove this from its list. */
760 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
761 
762 	/*
763 	 * If an address on this ILL is being used as a source address then
764 	 * clear out the pointers in other ILLs that point to this ILL.
765 	 */
766 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
767 	if (ill->ill_usesrc_grp_next != NULL) {
768 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
769 			ill_disband_usesrc_group(ill);
770 		} else {	/* consumer of the usesrc ILL */
771 			prev_ill = ill_prev_usesrc(ill);
772 			prev_ill->ill_usesrc_grp_next =
773 			    ill->ill_usesrc_grp_next;
774 		}
775 	}
776 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
777 }
778 
779 static void
780 ipif_non_duplicate(ipif_t *ipif)
781 {
782 	ill_t *ill = ipif->ipif_ill;
783 	mutex_enter(&ill->ill_lock);
784 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
785 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
786 		ASSERT(ill->ill_ipif_dup_count > 0);
787 		ill->ill_ipif_dup_count--;
788 	}
789 	mutex_exit(&ill->ill_lock);
790 }
791 
792 /*
793  * ill_delete_tail is called from ip_modclose after all references
794  * to the closing ill are gone. The wait is done in ip_modclose
795  */
796 void
797 ill_delete_tail(ill_t *ill)
798 {
799 	mblk_t	**mpp;
800 	ipif_t	*ipif;
801 	ip_stack_t	*ipst = ill->ill_ipst;
802 
803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
804 		ipif_non_duplicate(ipif);
805 		ipif_down_tail(ipif);
806 	}
807 
808 	ASSERT(ill->ill_ipif_dup_count == 0 &&
809 	    ill->ill_arp_down_mp == NULL &&
810 	    ill->ill_arp_del_mapping_mp == NULL);
811 
812 	/*
813 	 * If polling capability is enabled (which signifies direct
814 	 * upcall into IP and driver has ill saved as a handle),
815 	 * we need to make sure that unbind has completed before we
816 	 * let the ill disappear and driver no longer has any reference
817 	 * to this ill.
818 	 */
819 	mutex_enter(&ill->ill_lock);
820 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
821 		cv_wait(&ill->ill_cv, &ill->ill_lock);
822 	mutex_exit(&ill->ill_lock);
823 
824 	/*
825 	 * Clean up polling and soft ring capabilities
826 	 */
827 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
828 		ill_capability_dls_disable(ill);
829 
830 	if (ill->ill_net_type != IRE_LOOPBACK)
831 		qprocsoff(ill->ill_rq);
832 
833 	/*
834 	 * We do an ipsq_flush once again now. New messages could have
835 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
836 	 * could also have landed up if an ioctl thread had looked up
837 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
838 	 * enqueued the ioctl when we did the ipsq_flush last time.
839 	 */
840 	ipsq_flush(ill);
841 
842 	/*
843 	 * Free capabilities.
844 	 */
845 	if (ill->ill_ipsec_capab_ah != NULL) {
846 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
847 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
848 		ill->ill_ipsec_capab_ah = NULL;
849 	}
850 
851 	if (ill->ill_ipsec_capab_esp != NULL) {
852 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
853 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
854 		ill->ill_ipsec_capab_esp = NULL;
855 	}
856 
857 	if (ill->ill_mdt_capab != NULL) {
858 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
859 		ill->ill_mdt_capab = NULL;
860 	}
861 
862 	if (ill->ill_hcksum_capab != NULL) {
863 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
864 		ill->ill_hcksum_capab = NULL;
865 	}
866 
867 	if (ill->ill_zerocopy_capab != NULL) {
868 		kmem_free(ill->ill_zerocopy_capab,
869 		    sizeof (ill_zerocopy_capab_t));
870 		ill->ill_zerocopy_capab = NULL;
871 	}
872 
873 	if (ill->ill_lso_capab != NULL) {
874 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
875 		ill->ill_lso_capab = NULL;
876 	}
877 
878 	if (ill->ill_dls_capab != NULL) {
879 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
880 		ill->ill_dls_capab->ill_unbind_conn = NULL;
881 		kmem_free(ill->ill_dls_capab,
882 		    sizeof (ill_dls_capab_t) +
883 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
884 		ill->ill_dls_capab = NULL;
885 	}
886 
887 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
888 
889 	while (ill->ill_ipif != NULL)
890 		ipif_free_tail(ill->ill_ipif);
891 
892 	/*
893 	 * We have removed all references to ilm from conn and the ones joined
894 	 * within the kernel.
895 	 *
896 	 * We don't walk conns, mrts and ires because
897 	 *
898 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
899 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
900 	 *    ill references.
901 	 */
902 	ASSERT(ilm_walk_ill(ill) == 0);
903 	/*
904 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
905 	 * could free the phyint. No more reference to the phyint after this
906 	 * point.
907 	 */
908 	(void) ill_glist_delete(ill);
909 
910 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
911 	if (ill->ill_ndd_name != NULL)
912 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
913 	rw_exit(&ipst->ips_ip_g_nd_lock);
914 
915 
916 	if (ill->ill_frag_ptr != NULL) {
917 		uint_t count;
918 
919 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
920 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
921 		}
922 		mi_free(ill->ill_frag_ptr);
923 		ill->ill_frag_ptr = NULL;
924 		ill->ill_frag_hash_tbl = NULL;
925 	}
926 
927 	freemsg(ill->ill_nd_lla_mp);
928 	/* Free all retained control messages. */
929 	mpp = &ill->ill_first_mp_to_free;
930 	do {
931 		while (mpp[0]) {
932 			mblk_t  *mp;
933 			mblk_t  *mp1;
934 
935 			mp = mpp[0];
936 			mpp[0] = mp->b_next;
937 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
938 				mp1->b_next = NULL;
939 				mp1->b_prev = NULL;
940 			}
941 			freemsg(mp);
942 		}
943 	} while (mpp++ != &ill->ill_last_mp_to_free);
944 
945 	ill_free_mib(ill);
946 
947 #ifdef DEBUG
948 	ill_trace_cleanup(ill);
949 #endif
950 
951 	/* Drop refcnt here */
952 	netstack_rele(ill->ill_ipst->ips_netstack);
953 	ill->ill_ipst = NULL;
954 }
955 
956 static void
957 ill_free_mib(ill_t *ill)
958 {
959 	ip_stack_t *ipst = ill->ill_ipst;
960 
961 	/*
962 	 * MIB statistics must not be lost, so when an interface
963 	 * goes away the counter values will be added to the global
964 	 * MIBs.
965 	 */
966 	if (ill->ill_ip_mib != NULL) {
967 		if (ill->ill_isv6) {
968 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
969 			    ill->ill_ip_mib);
970 		} else {
971 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
972 			    ill->ill_ip_mib);
973 		}
974 
975 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
976 		ill->ill_ip_mib = NULL;
977 	}
978 	if (ill->ill_icmp6_mib != NULL) {
979 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
980 		    ill->ill_icmp6_mib);
981 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
982 		ill->ill_icmp6_mib = NULL;
983 	}
984 }
985 
986 /*
987  * Concatenate together a physical address and a sap.
988  *
989  * Sap_lengths are interpreted as follows:
990  *   sap_length == 0	==>	no sap
991  *   sap_length > 0	==>	sap is at the head of the dlpi address
992  *   sap_length < 0	==>	sap is at the tail of the dlpi address
993  */
994 static void
995 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
996     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
997 {
998 	uint16_t sap_addr = (uint16_t)sap_src;
999 
1000 	if (sap_length == 0) {
1001 		if (phys_src == NULL)
1002 			bzero(dst, phys_length);
1003 		else
1004 			bcopy(phys_src, dst, phys_length);
1005 	} else if (sap_length < 0) {
1006 		if (phys_src == NULL)
1007 			bzero(dst, phys_length);
1008 		else
1009 			bcopy(phys_src, dst, phys_length);
1010 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1011 	} else {
1012 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1013 		if (phys_src == NULL)
1014 			bzero((char *)dst + sap_length, phys_length);
1015 		else
1016 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1017 	}
1018 }
1019 
1020 /*
1021  * Generate a dl_unitdata_req mblk for the device and address given.
1022  * addr_length is the length of the physical portion of the address.
1023  * If addr is NULL include an all zero address of the specified length.
1024  * TRUE? In any case, addr_length is taken to be the entire length of the
1025  * dlpi address, including the absolute value of sap_length.
1026  */
1027 mblk_t *
1028 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1029 		t_scalar_t sap_length)
1030 {
1031 	dl_unitdata_req_t *dlur;
1032 	mblk_t	*mp;
1033 	t_scalar_t	abs_sap_length;		/* absolute value */
1034 
1035 	abs_sap_length = ABS(sap_length);
1036 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1037 	    DL_UNITDATA_REQ);
1038 	if (mp == NULL)
1039 		return (NULL);
1040 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1041 	/* HACK: accomodate incompatible DLPI drivers */
1042 	if (addr_length == 8)
1043 		addr_length = 6;
1044 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1045 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1046 	dlur->dl_priority.dl_min = 0;
1047 	dlur->dl_priority.dl_max = 0;
1048 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1049 	    (uchar_t *)&dlur[1]);
1050 	return (mp);
1051 }
1052 
1053 /*
1054  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1055  * Return an error if we already have 1 or more ioctls in progress.
1056  * This is used only for non-exclusive ioctls. Currently this is used
1057  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1058  * and thus need to use ipsq_pending_mp_add.
1059  */
1060 boolean_t
1061 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1062 {
1063 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1064 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1065 	/*
1066 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1067 	 */
1068 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1069 	    (add_mp->b_datap->db_type == M_IOCTL));
1070 
1071 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1072 	/*
1073 	 * Return error if the conn has started closing. The conn
1074 	 * could have finished cleaning up the pending mp list,
1075 	 * If so we should not add another mp to the list negating
1076 	 * the cleanup.
1077 	 */
1078 	if (connp->conn_state_flags & CONN_CLOSING)
1079 		return (B_FALSE);
1080 	/*
1081 	 * Add the pending mp to the head of the list, chained by b_next.
1082 	 * Note down the conn on which the ioctl request came, in b_prev.
1083 	 * This will be used to later get the conn, when we get a response
1084 	 * on the ill queue, from some other module (typically arp)
1085 	 */
1086 	add_mp->b_next = (void *)ill->ill_pending_mp;
1087 	add_mp->b_queue = CONNP_TO_WQ(connp);
1088 	ill->ill_pending_mp = add_mp;
1089 	if (connp != NULL)
1090 		connp->conn_oper_pending_ill = ill;
1091 	return (B_TRUE);
1092 }
1093 
1094 /*
1095  * Retrieve the ill_pending_mp and return it. We have to walk the list
1096  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1097  */
1098 mblk_t *
1099 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1100 {
1101 	mblk_t	*prev = NULL;
1102 	mblk_t	*curr = NULL;
1103 	uint_t	id;
1104 	conn_t	*connp;
1105 
1106 	/*
1107 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1108 	 * up the pending mp, but it does not know the ioc_id and
1109 	 * passes in a zero for it.
1110 	 */
1111 	mutex_enter(&ill->ill_lock);
1112 	if (ioc_id != 0)
1113 		*connpp = NULL;
1114 
1115 	/* Search the list for the appropriate ioctl based on ioc_id */
1116 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1117 	    prev = curr, curr = curr->b_next) {
1118 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1119 		connp = Q_TO_CONN(curr->b_queue);
1120 		/* Match based on the ioc_id or based on the conn */
1121 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1122 			break;
1123 	}
1124 
1125 	if (curr != NULL) {
1126 		/* Unlink the mblk from the pending mp list */
1127 		if (prev != NULL) {
1128 			prev->b_next = curr->b_next;
1129 		} else {
1130 			ASSERT(ill->ill_pending_mp == curr);
1131 			ill->ill_pending_mp = curr->b_next;
1132 		}
1133 
1134 		/*
1135 		 * conn refcnt must have been bumped up at the start of
1136 		 * the ioctl. So we can safely access the conn.
1137 		 */
1138 		ASSERT(CONN_Q(curr->b_queue));
1139 		*connpp = Q_TO_CONN(curr->b_queue);
1140 		curr->b_next = NULL;
1141 		curr->b_queue = NULL;
1142 	}
1143 
1144 	mutex_exit(&ill->ill_lock);
1145 
1146 	return (curr);
1147 }
1148 
1149 /*
1150  * Add the pending mp to the list. There can be only 1 pending mp
1151  * in the list. Any exclusive ioctl that needs to wait for a response
1152  * from another module or driver needs to use this function to set
1153  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1154  * the other module/driver. This is also used while waiting for the
1155  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1156  */
1157 boolean_t
1158 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1159     int waitfor)
1160 {
1161 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1162 
1163 	ASSERT(IAM_WRITER_IPIF(ipif));
1164 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1165 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1166 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1167 	/*
1168 	 * The caller may be using a different ipif than the one passed into
1169 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1170 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1171 	 * that `ipsq_current_ipif == ipif'.
1172 	 */
1173 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1174 
1175 	/*
1176 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1177 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1178 	 */
1179 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1180 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1181 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1182 
1183 	if (connp != NULL) {
1184 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1185 		/*
1186 		 * Return error if the conn has started closing. The conn
1187 		 * could have finished cleaning up the pending mp list,
1188 		 * If so we should not add another mp to the list negating
1189 		 * the cleanup.
1190 		 */
1191 		if (connp->conn_state_flags & CONN_CLOSING)
1192 			return (B_FALSE);
1193 	}
1194 	mutex_enter(&ipsq->ipsq_lock);
1195 	ipsq->ipsq_pending_ipif = ipif;
1196 	/*
1197 	 * Note down the queue in b_queue. This will be returned by
1198 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1199 	 * the processing
1200 	 */
1201 	add_mp->b_next = NULL;
1202 	add_mp->b_queue = q;
1203 	ipsq->ipsq_pending_mp = add_mp;
1204 	ipsq->ipsq_waitfor = waitfor;
1205 
1206 	if (connp != NULL)
1207 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1208 	mutex_exit(&ipsq->ipsq_lock);
1209 	return (B_TRUE);
1210 }
1211 
1212 /*
1213  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1214  * queued in the list.
1215  */
1216 mblk_t *
1217 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1218 {
1219 	mblk_t	*curr = NULL;
1220 
1221 	mutex_enter(&ipsq->ipsq_lock);
1222 	*connpp = NULL;
1223 	if (ipsq->ipsq_pending_mp == NULL) {
1224 		mutex_exit(&ipsq->ipsq_lock);
1225 		return (NULL);
1226 	}
1227 
1228 	/* There can be only 1 such excl message */
1229 	curr = ipsq->ipsq_pending_mp;
1230 	ASSERT(curr != NULL && curr->b_next == NULL);
1231 	ipsq->ipsq_pending_ipif = NULL;
1232 	ipsq->ipsq_pending_mp = NULL;
1233 	ipsq->ipsq_waitfor = 0;
1234 	mutex_exit(&ipsq->ipsq_lock);
1235 
1236 	if (CONN_Q(curr->b_queue)) {
1237 		/*
1238 		 * This mp did a refhold on the conn, at the start of the ioctl.
1239 		 * So we can safely return a pointer to the conn to the caller.
1240 		 */
1241 		*connpp = Q_TO_CONN(curr->b_queue);
1242 	} else {
1243 		*connpp = NULL;
1244 	}
1245 	curr->b_next = NULL;
1246 	curr->b_prev = NULL;
1247 	return (curr);
1248 }
1249 
1250 /*
1251  * Cleanup the ioctl mp queued in ipsq_pending_mp
1252  * - Called in the ill_delete path
1253  * - Called in the M_ERROR or M_HANGUP path on the ill.
1254  * - Called in the conn close path.
1255  */
1256 boolean_t
1257 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1258 {
1259 	mblk_t	*mp;
1260 	ipsq_t	*ipsq;
1261 	queue_t	*q;
1262 	ipif_t	*ipif;
1263 
1264 	ASSERT(IAM_WRITER_ILL(ill));
1265 	ipsq = ill->ill_phyint->phyint_ipsq;
1266 	mutex_enter(&ipsq->ipsq_lock);
1267 	/*
1268 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1269 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1270 	 * even if it is meant for another ill, since we have to enqueue
1271 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1272 	 * If connp is non-null we are called from the conn close path.
1273 	 */
1274 	mp = ipsq->ipsq_pending_mp;
1275 	if (mp == NULL || (connp != NULL &&
1276 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1277 		mutex_exit(&ipsq->ipsq_lock);
1278 		return (B_FALSE);
1279 	}
1280 	/* Now remove from the ipsq_pending_mp */
1281 	ipsq->ipsq_pending_mp = NULL;
1282 	q = mp->b_queue;
1283 	mp->b_next = NULL;
1284 	mp->b_prev = NULL;
1285 	mp->b_queue = NULL;
1286 
1287 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1288 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1289 	if (ill->ill_move_in_progress) {
1290 		ILL_CLEAR_MOVE(ill);
1291 	} else if (ill->ill_up_ipifs) {
1292 		ill_group_cleanup(ill);
1293 	}
1294 
1295 	ipif = ipsq->ipsq_pending_ipif;
1296 	ipsq->ipsq_pending_ipif = NULL;
1297 	ipsq->ipsq_waitfor = 0;
1298 	ipsq->ipsq_current_ipif = NULL;
1299 	ipsq->ipsq_current_ioctl = 0;
1300 	mutex_exit(&ipsq->ipsq_lock);
1301 
1302 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1303 		if (connp == NULL) {
1304 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1305 		} else {
1306 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1307 			mutex_enter(&ipif->ipif_ill->ill_lock);
1308 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1309 			mutex_exit(&ipif->ipif_ill->ill_lock);
1310 		}
1311 	} else {
1312 		/*
1313 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1314 		 * be just inet_freemsg. we have to restart it
1315 		 * otherwise the thread will be stuck.
1316 		 */
1317 		inet_freemsg(mp);
1318 	}
1319 	return (B_TRUE);
1320 }
1321 
1322 /*
1323  * The ill is closing. Cleanup all the pending mps. Called exclusively
1324  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1325  * knows this ill, and hence nobody can add an mp to this list
1326  */
1327 static void
1328 ill_pending_mp_cleanup(ill_t *ill)
1329 {
1330 	mblk_t	*mp;
1331 	queue_t	*q;
1332 
1333 	ASSERT(IAM_WRITER_ILL(ill));
1334 
1335 	mutex_enter(&ill->ill_lock);
1336 	/*
1337 	 * Every mp on the pending mp list originating from an ioctl
1338 	 * added 1 to the conn refcnt, at the start of the ioctl.
1339 	 * So bump it down now.  See comments in ip_wput_nondata()
1340 	 */
1341 	while (ill->ill_pending_mp != NULL) {
1342 		mp = ill->ill_pending_mp;
1343 		ill->ill_pending_mp = mp->b_next;
1344 		mutex_exit(&ill->ill_lock);
1345 
1346 		q = mp->b_queue;
1347 		ASSERT(CONN_Q(q));
1348 		mp->b_next = NULL;
1349 		mp->b_prev = NULL;
1350 		mp->b_queue = NULL;
1351 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1352 		mutex_enter(&ill->ill_lock);
1353 	}
1354 	ill->ill_pending_ipif = NULL;
1355 
1356 	mutex_exit(&ill->ill_lock);
1357 }
1358 
1359 /*
1360  * Called in the conn close path and ill delete path
1361  */
1362 static void
1363 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1364 {
1365 	ipsq_t	*ipsq;
1366 	mblk_t	*prev;
1367 	mblk_t	*curr;
1368 	mblk_t	*next;
1369 	queue_t	*q;
1370 	mblk_t	*tmp_list = NULL;
1371 
1372 	ASSERT(IAM_WRITER_ILL(ill));
1373 	if (connp != NULL)
1374 		q = CONNP_TO_WQ(connp);
1375 	else
1376 		q = ill->ill_wq;
1377 
1378 	ipsq = ill->ill_phyint->phyint_ipsq;
1379 	/*
1380 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1381 	 * In the case of ioctl from a conn, there can be only 1 mp
1382 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1383 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1384 	 * ioctls meant for this ill form conn's are not flushed. They will
1385 	 * be processed during ipsq_exit and will not find the ill and will
1386 	 * return error.
1387 	 */
1388 	mutex_enter(&ipsq->ipsq_lock);
1389 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1390 	    curr = next) {
1391 		next = curr->b_next;
1392 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1393 			/* Unlink the mblk from the pending mp list */
1394 			if (prev != NULL) {
1395 				prev->b_next = curr->b_next;
1396 			} else {
1397 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1398 				ipsq->ipsq_xopq_mphead = curr->b_next;
1399 			}
1400 			if (ipsq->ipsq_xopq_mptail == curr)
1401 				ipsq->ipsq_xopq_mptail = prev;
1402 			/*
1403 			 * Create a temporary list and release the ipsq lock
1404 			 * New elements are added to the head of the tmp_list
1405 			 */
1406 			curr->b_next = tmp_list;
1407 			tmp_list = curr;
1408 		} else {
1409 			prev = curr;
1410 		}
1411 	}
1412 	mutex_exit(&ipsq->ipsq_lock);
1413 
1414 	while (tmp_list != NULL) {
1415 		curr = tmp_list;
1416 		tmp_list = curr->b_next;
1417 		curr->b_next = NULL;
1418 		curr->b_prev = NULL;
1419 		curr->b_queue = NULL;
1420 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1421 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1422 			    CONN_CLOSE : NO_COPYOUT, NULL);
1423 		} else {
1424 			/*
1425 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1426 			 * this can't be just inet_freemsg. we have to
1427 			 * restart it otherwise the thread will be stuck.
1428 			 */
1429 			inet_freemsg(curr);
1430 		}
1431 	}
1432 }
1433 
1434 /*
1435  * This conn has started closing. Cleanup any pending ioctl from this conn.
1436  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1437  */
1438 void
1439 conn_ioctl_cleanup(conn_t *connp)
1440 {
1441 	mblk_t *curr;
1442 	ipsq_t	*ipsq;
1443 	ill_t	*ill;
1444 	boolean_t refheld;
1445 
1446 	/*
1447 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1448 	 * ioctl has not yet started, the mp is pending in the list headed by
1449 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1450 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1451 	 * is currently executing now the mp is not queued anywhere but
1452 	 * conn_oper_pending_ill is null. The conn close will wait
1453 	 * till the conn_ref drops to zero.
1454 	 */
1455 	mutex_enter(&connp->conn_lock);
1456 	ill = connp->conn_oper_pending_ill;
1457 	if (ill == NULL) {
1458 		mutex_exit(&connp->conn_lock);
1459 		return;
1460 	}
1461 
1462 	curr = ill_pending_mp_get(ill, &connp, 0);
1463 	if (curr != NULL) {
1464 		mutex_exit(&connp->conn_lock);
1465 		CONN_DEC_REF(connp);
1466 		inet_freemsg(curr);
1467 		return;
1468 	}
1469 	/*
1470 	 * We may not be able to refhold the ill if the ill/ipif
1471 	 * is changing. But we need to make sure that the ill will
1472 	 * not vanish. So we just bump up the ill_waiter count.
1473 	 */
1474 	refheld = ill_waiter_inc(ill);
1475 	mutex_exit(&connp->conn_lock);
1476 	if (refheld) {
1477 		if (ipsq_enter(ill, B_TRUE)) {
1478 			ill_waiter_dcr(ill);
1479 			/*
1480 			 * Check whether this ioctl has started and is
1481 			 * pending now in ipsq_pending_mp. If it is not
1482 			 * found there then check whether this ioctl has
1483 			 * not even started and is in the ipsq_xopq list.
1484 			 */
1485 			if (!ipsq_pending_mp_cleanup(ill, connp))
1486 				ipsq_xopq_mp_cleanup(ill, connp);
1487 			ipsq = ill->ill_phyint->phyint_ipsq;
1488 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1489 			return;
1490 		}
1491 	}
1492 
1493 	/*
1494 	 * The ill is also closing and we could not bump up the
1495 	 * ill_waiter_count or we could not enter the ipsq. Leave
1496 	 * the cleanup to ill_delete
1497 	 */
1498 	mutex_enter(&connp->conn_lock);
1499 	while (connp->conn_oper_pending_ill != NULL)
1500 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1501 	mutex_exit(&connp->conn_lock);
1502 	if (refheld)
1503 		ill_waiter_dcr(ill);
1504 }
1505 
1506 /*
1507  * ipcl_walk function for cleaning up conn_*_ill fields.
1508  */
1509 static void
1510 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1511 {
1512 	ill_t	*ill = (ill_t *)arg;
1513 	ire_t	*ire;
1514 
1515 	mutex_enter(&connp->conn_lock);
1516 	if (connp->conn_multicast_ill == ill) {
1517 		/* Revert to late binding */
1518 		connp->conn_multicast_ill = NULL;
1519 		connp->conn_orig_multicast_ifindex = 0;
1520 	}
1521 	if (connp->conn_incoming_ill == ill)
1522 		connp->conn_incoming_ill = NULL;
1523 	if (connp->conn_outgoing_ill == ill)
1524 		connp->conn_outgoing_ill = NULL;
1525 	if (connp->conn_outgoing_pill == ill)
1526 		connp->conn_outgoing_pill = NULL;
1527 	if (connp->conn_nofailover_ill == ill)
1528 		connp->conn_nofailover_ill = NULL;
1529 	if (connp->conn_dhcpinit_ill == ill) {
1530 		connp->conn_dhcpinit_ill = NULL;
1531 		ASSERT(ill->ill_dhcpinit != 0);
1532 		atomic_dec_32(&ill->ill_dhcpinit);
1533 	}
1534 	if (connp->conn_ire_cache != NULL) {
1535 		ire = connp->conn_ire_cache;
1536 		/*
1537 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1538 		 * interface X and ipif coming from interface Y, if interface
1539 		 * X and Y are part of the same IPMPgroup. Thus whenever
1540 		 * interface X goes down, remove all references to it by
1541 		 * checking both on ire_ipif and ire_stq.
1542 		 */
1543 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1544 		    (ire->ire_type == IRE_CACHE &&
1545 		    ire->ire_stq == ill->ill_wq)) {
1546 			connp->conn_ire_cache = NULL;
1547 			mutex_exit(&connp->conn_lock);
1548 			ire_refrele_notr(ire);
1549 			return;
1550 		}
1551 	}
1552 	mutex_exit(&connp->conn_lock);
1553 
1554 }
1555 
1556 /* ARGSUSED */
1557 void
1558 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1559 {
1560 	ill_t	*ill = q->q_ptr;
1561 	ipif_t	*ipif;
1562 
1563 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1564 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1565 		ipif_non_duplicate(ipif);
1566 		ipif_down_tail(ipif);
1567 	}
1568 	freemsg(mp);
1569 	ipsq_current_finish(ipsq);
1570 }
1571 
1572 /*
1573  * ill_down_start is called when we want to down this ill and bring it up again
1574  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1575  * all interfaces, but don't tear down any plumbing.
1576  */
1577 boolean_t
1578 ill_down_start(queue_t *q, mblk_t *mp)
1579 {
1580 	ill_t	*ill = q->q_ptr;
1581 	ipif_t	*ipif;
1582 
1583 	ASSERT(IAM_WRITER_ILL(ill));
1584 
1585 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1586 		(void) ipif_down(ipif, NULL, NULL);
1587 
1588 	ill_down(ill);
1589 
1590 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1591 
1592 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1593 
1594 	/*
1595 	 * Atomically test and add the pending mp if references are active.
1596 	 */
1597 	mutex_enter(&ill->ill_lock);
1598 	if (!ill_is_quiescent(ill)) {
1599 		/* call cannot fail since `conn_t *' argument is NULL */
1600 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1601 		    mp, ILL_DOWN);
1602 		mutex_exit(&ill->ill_lock);
1603 		return (B_FALSE);
1604 	}
1605 	mutex_exit(&ill->ill_lock);
1606 	return (B_TRUE);
1607 }
1608 
1609 static void
1610 ill_down(ill_t *ill)
1611 {
1612 	ip_stack_t	*ipst = ill->ill_ipst;
1613 
1614 	/* Blow off any IREs dependent on this ILL. */
1615 	ire_walk(ill_downi, (char *)ill, ipst);
1616 
1617 	/* Remove any conn_*_ill depending on this ill */
1618 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1619 
1620 	if (ill->ill_group != NULL) {
1621 		illgrp_delete(ill);
1622 	}
1623 }
1624 
1625 /*
1626  * ire_walk routine used to delete every IRE that depends on queues
1627  * associated with 'ill'.  (Always called as writer.)
1628  */
1629 static void
1630 ill_downi(ire_t *ire, char *ill_arg)
1631 {
1632 	ill_t	*ill = (ill_t *)ill_arg;
1633 
1634 	/*
1635 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1636 	 * interface X and ipif coming from interface Y, if interface
1637 	 * X and Y are part of the same IPMP group. Thus whenever interface
1638 	 * X goes down, remove all references to it by checking both
1639 	 * on ire_ipif and ire_stq.
1640 	 */
1641 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1642 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1643 		ire_delete(ire);
1644 	}
1645 }
1646 
1647 /*
1648  * Remove ire/nce from the fastpath list.
1649  */
1650 void
1651 ill_fastpath_nack(ill_t *ill)
1652 {
1653 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1654 }
1655 
1656 /* Consume an M_IOCACK of the fastpath probe. */
1657 void
1658 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1659 {
1660 	mblk_t	*mp1 = mp;
1661 
1662 	/*
1663 	 * If this was the first attempt turn on the fastpath probing.
1664 	 */
1665 	mutex_enter(&ill->ill_lock);
1666 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1667 		ill->ill_dlpi_fastpath_state = IDS_OK;
1668 	mutex_exit(&ill->ill_lock);
1669 
1670 	/* Free the M_IOCACK mblk, hold on to the data */
1671 	mp = mp->b_cont;
1672 	freeb(mp1);
1673 	if (mp == NULL)
1674 		return;
1675 	if (mp->b_cont != NULL) {
1676 		/*
1677 		 * Update all IRE's or NCE's that are waiting for
1678 		 * fastpath update.
1679 		 */
1680 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1681 		mp1 = mp->b_cont;
1682 		freeb(mp);
1683 		mp = mp1;
1684 	} else {
1685 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1686 	}
1687 
1688 	freeb(mp);
1689 }
1690 
1691 /*
1692  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1693  * The data portion of the request is a dl_unitdata_req_t template for
1694  * what we would send downstream in the absence of a fastpath confirmation.
1695  */
1696 int
1697 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1698 {
1699 	struct iocblk	*ioc;
1700 	mblk_t	*mp;
1701 
1702 	if (dlur_mp == NULL)
1703 		return (EINVAL);
1704 
1705 	mutex_enter(&ill->ill_lock);
1706 	switch (ill->ill_dlpi_fastpath_state) {
1707 	case IDS_FAILED:
1708 		/*
1709 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1710 		 * support it.
1711 		 */
1712 		mutex_exit(&ill->ill_lock);
1713 		return (ENOTSUP);
1714 	case IDS_UNKNOWN:
1715 		/* This is the first probe */
1716 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1717 		break;
1718 	default:
1719 		break;
1720 	}
1721 	mutex_exit(&ill->ill_lock);
1722 
1723 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1724 		return (EAGAIN);
1725 
1726 	mp->b_cont = copyb(dlur_mp);
1727 	if (mp->b_cont == NULL) {
1728 		freeb(mp);
1729 		return (EAGAIN);
1730 	}
1731 
1732 	ioc = (struct iocblk *)mp->b_rptr;
1733 	ioc->ioc_count = msgdsize(mp->b_cont);
1734 
1735 	putnext(ill->ill_wq, mp);
1736 	return (0);
1737 }
1738 
1739 void
1740 ill_capability_probe(ill_t *ill)
1741 {
1742 	/*
1743 	 * Do so only if capabilities are still unknown.
1744 	 */
1745 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1746 		return;
1747 
1748 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1749 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1750 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1751 }
1752 
1753 void
1754 ill_capability_reset(ill_t *ill)
1755 {
1756 	mblk_t *sc_mp = NULL;
1757 	mblk_t *tmp;
1758 
1759 	/*
1760 	 * Note here that we reset the state to UNKNOWN, and later send
1761 	 * down the DL_CAPABILITY_REQ without first setting the state to
1762 	 * INPROGRESS.  We do this in order to distinguish the
1763 	 * DL_CAPABILITY_ACK response which may come back in response to
1764 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1765 	 * also handle the case where the driver doesn't send us back
1766 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1767 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1768 	 * features are turned off until the state reaches IDS_OK.
1769 	 */
1770 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1771 	ill->ill_capab_reneg = B_FALSE;
1772 
1773 	/*
1774 	 * Disable sub-capabilities and request a list of sub-capability
1775 	 * messages which will be sent down to the driver.  Each handler
1776 	 * allocates the corresponding dl_capability_sub_t inside an
1777 	 * mblk, and links it to the existing sc_mp mblk, or return it
1778 	 * as sc_mp if it's the first sub-capability (the passed in
1779 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1780 	 * sc_mp will be pulled-up, before passing it downstream.
1781 	 */
1782 	ill_capability_mdt_reset(ill, &sc_mp);
1783 	ill_capability_hcksum_reset(ill, &sc_mp);
1784 	ill_capability_zerocopy_reset(ill, &sc_mp);
1785 	ill_capability_ipsec_reset(ill, &sc_mp);
1786 	ill_capability_dls_reset(ill, &sc_mp);
1787 	ill_capability_lso_reset(ill, &sc_mp);
1788 
1789 	/* Nothing to send down in order to disable the capabilities? */
1790 	if (sc_mp == NULL)
1791 		return;
1792 
1793 	tmp = msgpullup(sc_mp, -1);
1794 	freemsg(sc_mp);
1795 	if ((sc_mp = tmp) == NULL) {
1796 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1797 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1798 		return;
1799 	}
1800 
1801 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1802 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1803 }
1804 
1805 /*
1806  * Request or set new-style hardware capabilities supported by DLS provider.
1807  */
1808 static void
1809 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1810 {
1811 	mblk_t *mp;
1812 	dl_capability_req_t *capb;
1813 	size_t size = 0;
1814 	uint8_t *ptr;
1815 
1816 	if (reqp != NULL)
1817 		size = MBLKL(reqp);
1818 
1819 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1820 	if (mp == NULL) {
1821 		freemsg(reqp);
1822 		return;
1823 	}
1824 	ptr = mp->b_rptr;
1825 
1826 	capb = (dl_capability_req_t *)ptr;
1827 	ptr += sizeof (dl_capability_req_t);
1828 
1829 	if (reqp != NULL) {
1830 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1831 		capb->dl_sub_length = size;
1832 		bcopy(reqp->b_rptr, ptr, size);
1833 		ptr += size;
1834 		mp->b_cont = reqp->b_cont;
1835 		freeb(reqp);
1836 	}
1837 	ASSERT(ptr == mp->b_wptr);
1838 
1839 	ill_dlpi_send(ill, mp);
1840 }
1841 
1842 static void
1843 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1844 {
1845 	dl_capab_id_t *id_ic;
1846 	uint_t sub_dl_cap = outers->dl_cap;
1847 	dl_capability_sub_t *inners;
1848 	uint8_t *capend;
1849 
1850 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1851 
1852 	/*
1853 	 * Note: range checks here are not absolutely sufficient to
1854 	 * make us robust against malformed messages sent by drivers;
1855 	 * this is in keeping with the rest of IP's dlpi handling.
1856 	 * (Remember, it's coming from something else in the kernel
1857 	 * address space)
1858 	 */
1859 
1860 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1861 	if (capend > mp->b_wptr) {
1862 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1863 		    "malformed sub-capability too long for mblk");
1864 		return;
1865 	}
1866 
1867 	id_ic = (dl_capab_id_t *)(outers + 1);
1868 
1869 	if (outers->dl_length < sizeof (*id_ic) ||
1870 	    (inners = &id_ic->id_subcap,
1871 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1872 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1873 		    "encapsulated capab type %d too long for mblk",
1874 		    inners->dl_cap);
1875 		return;
1876 	}
1877 
1878 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1879 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1880 		    "isn't as expected; pass-thru module(s) detected, "
1881 		    "discarding capability\n", inners->dl_cap));
1882 		return;
1883 	}
1884 
1885 	/* Process the encapsulated sub-capability */
1886 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1887 }
1888 
1889 /*
1890  * Process Multidata Transmit capability negotiation ack received from a
1891  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1892  * DL_CAPABILITY_ACK message.
1893  */
1894 static void
1895 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1896 {
1897 	mblk_t *nmp = NULL;
1898 	dl_capability_req_t *oc;
1899 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1900 	ill_mdt_capab_t **ill_mdt_capab;
1901 	uint_t sub_dl_cap = isub->dl_cap;
1902 	uint8_t *capend;
1903 
1904 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1905 
1906 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1907 
1908 	/*
1909 	 * Note: range checks here are not absolutely sufficient to
1910 	 * make us robust against malformed messages sent by drivers;
1911 	 * this is in keeping with the rest of IP's dlpi handling.
1912 	 * (Remember, it's coming from something else in the kernel
1913 	 * address space)
1914 	 */
1915 
1916 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1917 	if (capend > mp->b_wptr) {
1918 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1919 		    "malformed sub-capability too long for mblk");
1920 		return;
1921 	}
1922 
1923 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1924 
1925 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1926 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1927 		    "unsupported MDT sub-capability (version %d, expected %d)",
1928 		    mdt_ic->mdt_version, MDT_VERSION_2);
1929 		return;
1930 	}
1931 
1932 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1933 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1934 		    "capability isn't as expected; pass-thru module(s) "
1935 		    "detected, discarding capability\n"));
1936 		return;
1937 	}
1938 
1939 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1940 
1941 		if (*ill_mdt_capab == NULL) {
1942 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1943 			    KM_NOSLEEP);
1944 
1945 			if (*ill_mdt_capab == NULL) {
1946 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1947 				    "could not enable MDT version %d "
1948 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1949 				    ill->ill_name);
1950 				return;
1951 			}
1952 		}
1953 
1954 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1955 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1956 		    "header spaces, %d max pld bufs, %d span limit)\n",
1957 		    ill->ill_name, MDT_VERSION_2,
1958 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1959 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1960 
1961 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1962 		(*ill_mdt_capab)->ill_mdt_on = 1;
1963 		/*
1964 		 * Round the following values to the nearest 32-bit; ULP
1965 		 * may further adjust them to accomodate for additional
1966 		 * protocol headers.  We pass these values to ULP during
1967 		 * bind time.
1968 		 */
1969 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1970 		    roundup(mdt_ic->mdt_hdr_head, 4);
1971 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1972 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1973 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1974 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1975 
1976 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1977 	} else {
1978 		uint_t size;
1979 		uchar_t *rptr;
1980 
1981 		size = sizeof (dl_capability_req_t) +
1982 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1983 
1984 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1985 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1986 			    "could not enable MDT for %s (ENOMEM)\n",
1987 			    ill->ill_name);
1988 			return;
1989 		}
1990 
1991 		rptr = nmp->b_rptr;
1992 		/* initialize dl_capability_req_t */
1993 		oc = (dl_capability_req_t *)nmp->b_rptr;
1994 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1995 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1996 		    sizeof (dl_capab_mdt_t);
1997 		nmp->b_rptr += sizeof (dl_capability_req_t);
1998 
1999 		/* initialize dl_capability_sub_t */
2000 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2001 		nmp->b_rptr += sizeof (*isub);
2002 
2003 		/* initialize dl_capab_mdt_t */
2004 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2005 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2006 
2007 		nmp->b_rptr = rptr;
2008 
2009 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2010 		    "to enable MDT version %d\n", ill->ill_name,
2011 		    MDT_VERSION_2));
2012 
2013 		/* set ENABLE flag */
2014 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2015 
2016 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2017 		ill_dlpi_send(ill, nmp);
2018 	}
2019 }
2020 
2021 static void
2022 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2023 {
2024 	mblk_t *mp;
2025 	dl_capab_mdt_t *mdt_subcap;
2026 	dl_capability_sub_t *dl_subcap;
2027 	int size;
2028 
2029 	if (!ILL_MDT_CAPABLE(ill))
2030 		return;
2031 
2032 	ASSERT(ill->ill_mdt_capab != NULL);
2033 	/*
2034 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2035 	 * structure since it's possible that another thread is still
2036 	 * referring to it.  The structure only gets deallocated when
2037 	 * we destroy the ill.
2038 	 */
2039 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2040 
2041 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2042 
2043 	mp = allocb(size, BPRI_HI);
2044 	if (mp == NULL) {
2045 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2046 		    "request to disable MDT\n"));
2047 		return;
2048 	}
2049 
2050 	mp->b_wptr = mp->b_rptr + size;
2051 
2052 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2053 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2054 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2055 
2056 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2057 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2058 	mdt_subcap->mdt_flags = 0;
2059 	mdt_subcap->mdt_hdr_head = 0;
2060 	mdt_subcap->mdt_hdr_tail = 0;
2061 
2062 	if (*sc_mp != NULL)
2063 		linkb(*sc_mp, mp);
2064 	else
2065 		*sc_mp = mp;
2066 }
2067 
2068 /*
2069  * Send a DL_NOTIFY_REQ to the specified ill to enable
2070  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2071  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2072  * acceleration.
2073  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2074  */
2075 static boolean_t
2076 ill_enable_promisc_notify(ill_t *ill)
2077 {
2078 	mblk_t *mp;
2079 	dl_notify_req_t *req;
2080 
2081 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2082 
2083 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2084 	if (mp == NULL)
2085 		return (B_FALSE);
2086 
2087 	req = (dl_notify_req_t *)mp->b_rptr;
2088 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2089 	    DL_NOTE_PROMISC_OFF_PHYS;
2090 
2091 	ill_dlpi_send(ill, mp);
2092 
2093 	return (B_TRUE);
2094 }
2095 
2096 
2097 /*
2098  * Allocate an IPsec capability request which will be filled by our
2099  * caller to turn on support for one or more algorithms.
2100  */
2101 static mblk_t *
2102 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2103 {
2104 	mblk_t *nmp;
2105 	dl_capability_req_t	*ocap;
2106 	dl_capab_ipsec_t	*ocip;
2107 	dl_capab_ipsec_t	*icip;
2108 	uint8_t			*ptr;
2109 	icip = (dl_capab_ipsec_t *)(isub + 1);
2110 
2111 	/*
2112 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2113 	 * PROMISC_ON/OFF notification from the provider. We need to
2114 	 * do this before enabling the algorithms to avoid leakage of
2115 	 * cleartext packets.
2116 	 */
2117 
2118 	if (!ill_enable_promisc_notify(ill))
2119 		return (NULL);
2120 
2121 	/*
2122 	 * Allocate new mblk which will contain a new capability
2123 	 * request to enable the capabilities.
2124 	 */
2125 
2126 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2127 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2128 	if (nmp == NULL)
2129 		return (NULL);
2130 
2131 	ptr = nmp->b_rptr;
2132 
2133 	/* initialize dl_capability_req_t */
2134 	ocap = (dl_capability_req_t *)ptr;
2135 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2136 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2137 	ptr += sizeof (dl_capability_req_t);
2138 
2139 	/* initialize dl_capability_sub_t */
2140 	bcopy(isub, ptr, sizeof (*isub));
2141 	ptr += sizeof (*isub);
2142 
2143 	/* initialize dl_capab_ipsec_t */
2144 	ocip = (dl_capab_ipsec_t *)ptr;
2145 	bcopy(icip, ocip, sizeof (*icip));
2146 
2147 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2148 	return (nmp);
2149 }
2150 
2151 /*
2152  * Process an IPsec capability negotiation ack received from a DLS Provider.
2153  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2154  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2155  */
2156 static void
2157 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2158 {
2159 	dl_capab_ipsec_t	*icip;
2160 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2161 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2162 	uint_t cipher, nciphers;
2163 	mblk_t *nmp;
2164 	uint_t alg_len;
2165 	boolean_t need_sadb_dump;
2166 	uint_t sub_dl_cap = isub->dl_cap;
2167 	ill_ipsec_capab_t **ill_capab;
2168 	uint64_t ill_capab_flag;
2169 	uint8_t *capend, *ciphend;
2170 	boolean_t sadb_resync;
2171 
2172 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2173 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2174 
2175 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2176 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2177 		ill_capab_flag = ILL_CAPAB_AH;
2178 	} else {
2179 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2180 		ill_capab_flag = ILL_CAPAB_ESP;
2181 	}
2182 
2183 	/*
2184 	 * If the ill capability structure exists, then this incoming
2185 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2186 	 * If this is so, then we'd need to resynchronize the SADB
2187 	 * after re-enabling the offloaded ciphers.
2188 	 */
2189 	sadb_resync = (*ill_capab != NULL);
2190 
2191 	/*
2192 	 * Note: range checks here are not absolutely sufficient to
2193 	 * make us robust against malformed messages sent by drivers;
2194 	 * this is in keeping with the rest of IP's dlpi handling.
2195 	 * (Remember, it's coming from something else in the kernel
2196 	 * address space)
2197 	 */
2198 
2199 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2200 	if (capend > mp->b_wptr) {
2201 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2202 		    "malformed sub-capability too long for mblk");
2203 		return;
2204 	}
2205 
2206 	/*
2207 	 * There are two types of acks we process here:
2208 	 * 1. acks in reply to a (first form) generic capability req
2209 	 *    (no ENABLE flag set)
2210 	 * 2. acks in reply to a ENABLE capability req.
2211 	 *    (ENABLE flag set)
2212 	 *
2213 	 * We process the subcapability passed as argument as follows:
2214 	 * 1 do initializations
2215 	 *   1.1 initialize nmp = NULL
2216 	 *   1.2 set need_sadb_dump to B_FALSE
2217 	 * 2 for each cipher in subcapability:
2218 	 *   2.1 if ENABLE flag is set:
2219 	 *	2.1.1 update per-ill ipsec capabilities info
2220 	 *	2.1.2 set need_sadb_dump to B_TRUE
2221 	 *   2.2 if ENABLE flag is not set:
2222 	 *	2.2.1 if nmp is NULL:
2223 	 *		2.2.1.1 allocate and initialize nmp
2224 	 *		2.2.1.2 init current pos in nmp
2225 	 *	2.2.2 copy current cipher to current pos in nmp
2226 	 *	2.2.3 set ENABLE flag in nmp
2227 	 *	2.2.4 update current pos
2228 	 * 3 if nmp is not equal to NULL, send enable request
2229 	 *   3.1 send capability request
2230 	 * 4 if need_sadb_dump is B_TRUE
2231 	 *   4.1 enable promiscuous on/off notifications
2232 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2233 	 *	AH or ESP SA's to interface.
2234 	 */
2235 
2236 	nmp = NULL;
2237 	oalg = NULL;
2238 	need_sadb_dump = B_FALSE;
2239 	icip = (dl_capab_ipsec_t *)(isub + 1);
2240 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2241 
2242 	nciphers = icip->cip_nciphers;
2243 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2244 
2245 	if (ciphend > capend) {
2246 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2247 		    "too many ciphers for sub-capability len");
2248 		return;
2249 	}
2250 
2251 	for (cipher = 0; cipher < nciphers; cipher++) {
2252 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2253 
2254 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2255 			/*
2256 			 * TBD: when we provide a way to disable capabilities
2257 			 * from above, need to manage the request-pending state
2258 			 * and fail if we were not expecting this ACK.
2259 			 */
2260 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2261 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2262 
2263 			/*
2264 			 * Update IPsec capabilities for this ill
2265 			 */
2266 
2267 			if (*ill_capab == NULL) {
2268 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2269 				    ("ill_capability_ipsec_ack: "
2270 				    "allocating ipsec_capab for ill\n"));
2271 				*ill_capab = ill_ipsec_capab_alloc();
2272 
2273 				if (*ill_capab == NULL) {
2274 					cmn_err(CE_WARN,
2275 					    "ill_capability_ipsec_ack: "
2276 					    "could not enable IPsec Hardware "
2277 					    "acceleration for %s (ENOMEM)\n",
2278 					    ill->ill_name);
2279 					return;
2280 				}
2281 			}
2282 
2283 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2284 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2285 
2286 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2287 				cmn_err(CE_WARN,
2288 				    "ill_capability_ipsec_ack: "
2289 				    "malformed IPsec algorithm id %d",
2290 				    ialg->alg_prim);
2291 				continue;
2292 			}
2293 
2294 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2295 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2296 				    ialg->alg_prim);
2297 			} else {
2298 				ipsec_capab_algparm_t *alp;
2299 
2300 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2301 				    ialg->alg_prim);
2302 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2303 				    ialg->alg_prim)) {
2304 					cmn_err(CE_WARN,
2305 					    "ill_capability_ipsec_ack: "
2306 					    "no space for IPsec alg id %d",
2307 					    ialg->alg_prim);
2308 					continue;
2309 				}
2310 				alp = &((*ill_capab)->encr_algparm[
2311 				    ialg->alg_prim]);
2312 				alp->minkeylen = ialg->alg_minbits;
2313 				alp->maxkeylen = ialg->alg_maxbits;
2314 			}
2315 			ill->ill_capabilities |= ill_capab_flag;
2316 			/*
2317 			 * indicate that a capability was enabled, which
2318 			 * will be used below to kick off a SADB dump
2319 			 * to the ill.
2320 			 */
2321 			need_sadb_dump = B_TRUE;
2322 		} else {
2323 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2324 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2325 			    ialg->alg_prim));
2326 
2327 			if (nmp == NULL) {
2328 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2329 				if (nmp == NULL) {
2330 					/*
2331 					 * Sending the PROMISC_ON/OFF
2332 					 * notification request failed.
2333 					 * We cannot enable the algorithms
2334 					 * since the Provider will not
2335 					 * notify IP of promiscous mode
2336 					 * changes, which could lead
2337 					 * to leakage of packets.
2338 					 */
2339 					cmn_err(CE_WARN,
2340 					    "ill_capability_ipsec_ack: "
2341 					    "could not enable IPsec Hardware "
2342 					    "acceleration for %s (ENOMEM)\n",
2343 					    ill->ill_name);
2344 					return;
2345 				}
2346 				/* ptr to current output alg specifier */
2347 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2348 			}
2349 
2350 			/*
2351 			 * Copy current alg specifier, set ENABLE
2352 			 * flag, and advance to next output alg.
2353 			 * For now we enable all IPsec capabilities.
2354 			 */
2355 			ASSERT(oalg != NULL);
2356 			bcopy(ialg, oalg, alg_len);
2357 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2358 			nmp->b_wptr += alg_len;
2359 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2360 		}
2361 
2362 		/* move to next input algorithm specifier */
2363 		ialg = (dl_capab_ipsec_alg_t *)
2364 		    ((char *)ialg + alg_len);
2365 	}
2366 
2367 	if (nmp != NULL)
2368 		/*
2369 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2370 		 * IPsec hardware acceleration.
2371 		 */
2372 		ill_dlpi_send(ill, nmp);
2373 
2374 	if (need_sadb_dump)
2375 		/*
2376 		 * An acknowledgement corresponding to a request to
2377 		 * enable acceleration was received, notify SADB.
2378 		 */
2379 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2380 }
2381 
2382 /*
2383  * Given an mblk with enough space in it, create sub-capability entries for
2384  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2385  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2386  * in preparation for the reset the DL_CAPABILITY_REQ message.
2387  */
2388 static void
2389 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2390     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2391 {
2392 	dl_capab_ipsec_t *oipsec;
2393 	dl_capab_ipsec_alg_t *oalg;
2394 	dl_capability_sub_t *dl_subcap;
2395 	int i, k;
2396 
2397 	ASSERT(nciphers > 0);
2398 	ASSERT(ill_cap != NULL);
2399 	ASSERT(mp != NULL);
2400 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2401 
2402 	/* dl_capability_sub_t for "stype" */
2403 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2404 	dl_subcap->dl_cap = stype;
2405 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2406 	mp->b_wptr += sizeof (dl_capability_sub_t);
2407 
2408 	/* dl_capab_ipsec_t for "stype" */
2409 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2410 	oipsec->cip_version = 1;
2411 	oipsec->cip_nciphers = nciphers;
2412 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2413 
2414 	/* create entries for "stype" AUTH ciphers */
2415 	for (i = 0; i < ill_cap->algs_size; i++) {
2416 		for (k = 0; k < BITSPERBYTE; k++) {
2417 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2418 				continue;
2419 
2420 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2421 			bzero((void *)oalg, sizeof (*oalg));
2422 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2423 			oalg->alg_prim = k + (BITSPERBYTE * i);
2424 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2425 		}
2426 	}
2427 	/* create entries for "stype" ENCR ciphers */
2428 	for (i = 0; i < ill_cap->algs_size; i++) {
2429 		for (k = 0; k < BITSPERBYTE; k++) {
2430 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2431 				continue;
2432 
2433 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2434 			bzero((void *)oalg, sizeof (*oalg));
2435 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2436 			oalg->alg_prim = k + (BITSPERBYTE * i);
2437 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2438 		}
2439 	}
2440 }
2441 
2442 /*
2443  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2444  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2445  * POPC instruction, but our macro is more flexible for an arbitrary length
2446  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2447  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2448  * stays that way, we can reduce the number of iterations required.
2449  */
2450 #define	COUNT_1S(val, sum) {					\
2451 	uint8_t x = val & 0xff;					\
2452 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2453 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2454 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2455 }
2456 
2457 /* ARGSUSED */
2458 static void
2459 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2460 {
2461 	mblk_t *mp;
2462 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2463 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2464 	uint64_t ill_capabilities = ill->ill_capabilities;
2465 	int ah_cnt = 0, esp_cnt = 0;
2466 	int ah_len = 0, esp_len = 0;
2467 	int i, size = 0;
2468 
2469 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2470 		return;
2471 
2472 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2473 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2474 
2475 	/* Find out the number of ciphers for AH */
2476 	if (cap_ah != NULL) {
2477 		for (i = 0; i < cap_ah->algs_size; i++) {
2478 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2479 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2480 		}
2481 		if (ah_cnt > 0) {
2482 			size += sizeof (dl_capability_sub_t) +
2483 			    sizeof (dl_capab_ipsec_t);
2484 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2485 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2486 			size += ah_len;
2487 		}
2488 	}
2489 
2490 	/* Find out the number of ciphers for ESP */
2491 	if (cap_esp != NULL) {
2492 		for (i = 0; i < cap_esp->algs_size; i++) {
2493 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2494 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2495 		}
2496 		if (esp_cnt > 0) {
2497 			size += sizeof (dl_capability_sub_t) +
2498 			    sizeof (dl_capab_ipsec_t);
2499 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2500 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2501 			size += esp_len;
2502 		}
2503 	}
2504 
2505 	if (size == 0) {
2506 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2507 		    "there's nothing to reset\n"));
2508 		return;
2509 	}
2510 
2511 	mp = allocb(size, BPRI_HI);
2512 	if (mp == NULL) {
2513 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2514 		    "request to disable IPSEC Hardware Acceleration\n"));
2515 		return;
2516 	}
2517 
2518 	/*
2519 	 * Clear the capability flags for IPsec HA but retain the ill
2520 	 * capability structures since it's possible that another thread
2521 	 * is still referring to them.  The structures only get deallocated
2522 	 * when we destroy the ill.
2523 	 *
2524 	 * Various places check the flags to see if the ill is capable of
2525 	 * hardware acceleration, and by clearing them we ensure that new
2526 	 * outbound IPsec packets are sent down encrypted.
2527 	 */
2528 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2529 
2530 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2531 	if (ah_cnt > 0) {
2532 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2533 		    cap_ah, mp);
2534 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2535 	}
2536 
2537 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2538 	if (esp_cnt > 0) {
2539 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2540 		    cap_esp, mp);
2541 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2542 	}
2543 
2544 	/*
2545 	 * At this point we've composed a bunch of sub-capabilities to be
2546 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2547 	 * by the caller.  Upon receiving this reset message, the driver
2548 	 * must stop inbound decryption (by destroying all inbound SAs)
2549 	 * and let the corresponding packets come in encrypted.
2550 	 */
2551 
2552 	if (*sc_mp != NULL)
2553 		linkb(*sc_mp, mp);
2554 	else
2555 		*sc_mp = mp;
2556 }
2557 
2558 static void
2559 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2560     boolean_t encapsulated)
2561 {
2562 	boolean_t legacy = B_FALSE;
2563 
2564 	/*
2565 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2566 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2567 	 * instructed the driver to disable its advertised capabilities,
2568 	 * so there's no point in accepting any response at this moment.
2569 	 */
2570 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2571 		return;
2572 
2573 	/*
2574 	 * Note that only the following two sub-capabilities may be
2575 	 * considered as "legacy", since their original definitions
2576 	 * do not incorporate the dl_mid_t module ID token, and hence
2577 	 * may require the use of the wrapper sub-capability.
2578 	 */
2579 	switch (subp->dl_cap) {
2580 	case DL_CAPAB_IPSEC_AH:
2581 	case DL_CAPAB_IPSEC_ESP:
2582 		legacy = B_TRUE;
2583 		break;
2584 	}
2585 
2586 	/*
2587 	 * For legacy sub-capabilities which don't incorporate a queue_t
2588 	 * pointer in their structures, discard them if we detect that
2589 	 * there are intermediate modules in between IP and the driver.
2590 	 */
2591 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2592 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2593 		    "%d discarded; %d module(s) present below IP\n",
2594 		    subp->dl_cap, ill->ill_lmod_cnt));
2595 		return;
2596 	}
2597 
2598 	switch (subp->dl_cap) {
2599 	case DL_CAPAB_IPSEC_AH:
2600 	case DL_CAPAB_IPSEC_ESP:
2601 		ill_capability_ipsec_ack(ill, mp, subp);
2602 		break;
2603 	case DL_CAPAB_MDT:
2604 		ill_capability_mdt_ack(ill, mp, subp);
2605 		break;
2606 	case DL_CAPAB_HCKSUM:
2607 		ill_capability_hcksum_ack(ill, mp, subp);
2608 		break;
2609 	case DL_CAPAB_ZEROCOPY:
2610 		ill_capability_zerocopy_ack(ill, mp, subp);
2611 		break;
2612 	case DL_CAPAB_POLL:
2613 		if (!SOFT_RINGS_ENABLED())
2614 			ill_capability_dls_ack(ill, mp, subp);
2615 		break;
2616 	case DL_CAPAB_SOFT_RING:
2617 		if (SOFT_RINGS_ENABLED())
2618 			ill_capability_dls_ack(ill, mp, subp);
2619 		break;
2620 	case DL_CAPAB_LSO:
2621 		ill_capability_lso_ack(ill, mp, subp);
2622 		break;
2623 	default:
2624 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2625 		    subp->dl_cap));
2626 	}
2627 }
2628 
2629 /*
2630  * As part of negotiating polling capability, the driver tells us
2631  * the default (or normal) blanking interval and packet threshold
2632  * (the receive timer fires if blanking interval is reached or
2633  * the packet threshold is reached).
2634  *
2635  * As part of manipulating the polling interval, we always use our
2636  * estimated interval (avg service time * number of packets queued
2637  * on the squeue) but we try to blank for a minimum of
2638  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2639  * packet threshold during this time. When we are not in polling mode
2640  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2641  * rr_min_blank_ratio but up the packet cnt by a ratio of
2642  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2643  * possible although for a shorter interval.
2644  */
2645 #define	RR_MAX_BLANK_RATIO	20
2646 #define	RR_MIN_BLANK_RATIO	10
2647 #define	RR_MAX_PKT_CNT_RATIO	3
2648 #define	RR_MIN_PKT_CNT_RATIO	3
2649 
2650 /*
2651  * These can be tuned via /etc/system.
2652  */
2653 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2654 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2655 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2656 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2657 
2658 static mac_resource_handle_t
2659 ill_ring_add(void *arg, mac_resource_t *mrp)
2660 {
2661 	ill_t			*ill = (ill_t *)arg;
2662 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2663 	ill_rx_ring_t		*rx_ring;
2664 	int			ip_rx_index;
2665 
2666 	ASSERT(mrp != NULL);
2667 	if (mrp->mr_type != MAC_RX_FIFO) {
2668 		return (NULL);
2669 	}
2670 	ASSERT(ill != NULL);
2671 	ASSERT(ill->ill_dls_capab != NULL);
2672 
2673 	mutex_enter(&ill->ill_lock);
2674 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2675 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2676 		ASSERT(rx_ring != NULL);
2677 
2678 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2679 			time_t normal_blank_time =
2680 			    mrfp->mrf_normal_blank_time;
2681 			uint_t normal_pkt_cnt =
2682 			    mrfp->mrf_normal_pkt_count;
2683 
2684 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2685 
2686 	rx_ring->rr_blank = mrfp->mrf_blank;
2687 	rx_ring->rr_handle = mrfp->mrf_arg;
2688 	rx_ring->rr_ill = ill;
2689 	rx_ring->rr_normal_blank_time = normal_blank_time;
2690 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2691 
2692 			rx_ring->rr_max_blank_time =
2693 			    normal_blank_time * rr_max_blank_ratio;
2694 			rx_ring->rr_min_blank_time =
2695 			    normal_blank_time * rr_min_blank_ratio;
2696 			rx_ring->rr_max_pkt_cnt =
2697 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2698 			rx_ring->rr_min_pkt_cnt =
2699 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2700 
2701 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2702 			mutex_exit(&ill->ill_lock);
2703 
2704 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2705 			    (int), ip_rx_index);
2706 			return ((mac_resource_handle_t)rx_ring);
2707 		}
2708 	}
2709 
2710 	/*
2711 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2712 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2713 	 * should be made configurable. Meanwhile it cause no panic because
2714 	 * driver will pass ip_input a NULL handle which will make
2715 	 * IP allocate the default squeue and Polling mode will not
2716 	 * be used for this ring.
2717 	 */
2718 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2719 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2720 
2721 	mutex_exit(&ill->ill_lock);
2722 	return (NULL);
2723 }
2724 
2725 static boolean_t
2726 ill_capability_dls_init(ill_t *ill)
2727 {
2728 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2729 	conn_t 			*connp;
2730 	size_t			sz;
2731 	ip_stack_t *ipst = ill->ill_ipst;
2732 
2733 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2734 		if (ill_dls == NULL) {
2735 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2736 			    "soft_ring enabled for ill=%s (%p) but data "
2737 			    "structs uninitialized\n", ill->ill_name,
2738 			    (void *)ill);
2739 		}
2740 		return (B_TRUE);
2741 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2742 		if (ill_dls == NULL) {
2743 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2744 			    "polling enabled for ill=%s (%p) but data "
2745 			    "structs uninitialized\n", ill->ill_name,
2746 			    (void *)ill);
2747 		}
2748 		return (B_TRUE);
2749 	}
2750 
2751 	if (ill_dls != NULL) {
2752 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2753 		/* Soft_Ring or polling is being re-enabled */
2754 
2755 		connp = ill_dls->ill_unbind_conn;
2756 		ASSERT(rx_ring != NULL);
2757 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2758 		bzero((void *)rx_ring,
2759 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2760 		ill_dls->ill_ring_tbl = rx_ring;
2761 		ill_dls->ill_unbind_conn = connp;
2762 		return (B_TRUE);
2763 	}
2764 
2765 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2766 	    ipst->ips_netstack)) == NULL)
2767 		return (B_FALSE);
2768 
2769 	sz = sizeof (ill_dls_capab_t);
2770 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2771 
2772 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2773 	if (ill_dls == NULL) {
2774 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2775 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2776 		    (void *)ill);
2777 		CONN_DEC_REF(connp);
2778 		return (B_FALSE);
2779 	}
2780 
2781 	/* Allocate space to hold ring table */
2782 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2783 	ill->ill_dls_capab = ill_dls;
2784 	ill_dls->ill_unbind_conn = connp;
2785 	return (B_TRUE);
2786 }
2787 
2788 /*
2789  * ill_capability_dls_disable: disable soft_ring and/or polling
2790  * capability. Since any of the rings might already be in use, need
2791  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2792  * direct calls if necessary.
2793  */
2794 static void
2795 ill_capability_dls_disable(ill_t *ill)
2796 {
2797 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2798 
2799 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2800 		ip_squeue_clean_all(ill);
2801 		ill_dls->ill_tx = NULL;
2802 		ill_dls->ill_tx_handle = NULL;
2803 		ill_dls->ill_dls_change_status = NULL;
2804 		ill_dls->ill_dls_bind = NULL;
2805 		ill_dls->ill_dls_unbind = NULL;
2806 	}
2807 
2808 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2809 }
2810 
2811 static void
2812 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2813     dl_capability_sub_t *isub)
2814 {
2815 	uint_t			size;
2816 	uchar_t			*rptr;
2817 	dl_capab_dls_t	dls, *odls;
2818 	ill_dls_capab_t	*ill_dls;
2819 	mblk_t			*nmp = NULL;
2820 	dl_capability_req_t	*ocap;
2821 	uint_t			sub_dl_cap = isub->dl_cap;
2822 
2823 	if (!ill_capability_dls_init(ill))
2824 		return;
2825 	ill_dls = ill->ill_dls_capab;
2826 
2827 	/* Copy locally to get the members aligned */
2828 	bcopy((void *)idls, (void *)&dls,
2829 	    sizeof (dl_capab_dls_t));
2830 
2831 	/* Get the tx function and handle from dld */
2832 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2833 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2834 
2835 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2836 		ill_dls->ill_dls_change_status =
2837 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2838 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2839 		ill_dls->ill_dls_unbind =
2840 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2841 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2842 	}
2843 
2844 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2845 	    isub->dl_length;
2846 
2847 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2848 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2849 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2850 		    ill->ill_name, (void *)ill);
2851 		return;
2852 	}
2853 
2854 	/* initialize dl_capability_req_t */
2855 	rptr = nmp->b_rptr;
2856 	ocap = (dl_capability_req_t *)rptr;
2857 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2858 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2859 	rptr += sizeof (dl_capability_req_t);
2860 
2861 	/* initialize dl_capability_sub_t */
2862 	bcopy(isub, rptr, sizeof (*isub));
2863 	rptr += sizeof (*isub);
2864 
2865 	odls = (dl_capab_dls_t *)rptr;
2866 	rptr += sizeof (dl_capab_dls_t);
2867 
2868 	/* initialize dl_capab_dls_t to be sent down */
2869 	dls.dls_rx_handle = (uintptr_t)ill;
2870 	dls.dls_rx = (uintptr_t)ip_input;
2871 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2872 
2873 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2874 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2875 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2876 		dls.dls_flags = SOFT_RING_ENABLE;
2877 	} else {
2878 		dls.dls_flags = POLL_ENABLE;
2879 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2880 		    "to enable polling\n", ill->ill_name));
2881 	}
2882 	bcopy((void *)&dls, (void *)odls,
2883 	    sizeof (dl_capab_dls_t));
2884 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2885 	/*
2886 	 * nmp points to a DL_CAPABILITY_REQ message to
2887 	 * enable either soft_ring or polling
2888 	 */
2889 	ill_dlpi_send(ill, nmp);
2890 }
2891 
2892 static void
2893 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2894 {
2895 	mblk_t *mp;
2896 	dl_capab_dls_t *idls;
2897 	dl_capability_sub_t *dl_subcap;
2898 	int size;
2899 
2900 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2901 		return;
2902 
2903 	ASSERT(ill->ill_dls_capab != NULL);
2904 
2905 	size = sizeof (*dl_subcap) + sizeof (*idls);
2906 
2907 	mp = allocb(size, BPRI_HI);
2908 	if (mp == NULL) {
2909 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2910 		    "request to disable soft_ring\n"));
2911 		return;
2912 	}
2913 
2914 	mp->b_wptr = mp->b_rptr + size;
2915 
2916 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2917 	dl_subcap->dl_length = sizeof (*idls);
2918 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2919 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2920 	else
2921 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2922 
2923 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2924 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2925 		idls->dls_flags = SOFT_RING_DISABLE;
2926 	else
2927 		idls->dls_flags = POLL_DISABLE;
2928 
2929 	if (*sc_mp != NULL)
2930 		linkb(*sc_mp, mp);
2931 	else
2932 		*sc_mp = mp;
2933 }
2934 
2935 /*
2936  * Process a soft_ring/poll capability negotiation ack received
2937  * from a DLS Provider.isub must point to the sub-capability
2938  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2939  */
2940 static void
2941 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2942 {
2943 	dl_capab_dls_t		*idls;
2944 	uint_t			sub_dl_cap = isub->dl_cap;
2945 	uint8_t			*capend;
2946 
2947 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2948 	    sub_dl_cap == DL_CAPAB_POLL);
2949 
2950 	if (ill->ill_isv6)
2951 		return;
2952 
2953 	/*
2954 	 * Note: range checks here are not absolutely sufficient to
2955 	 * make us robust against malformed messages sent by drivers;
2956 	 * this is in keeping with the rest of IP's dlpi handling.
2957 	 * (Remember, it's coming from something else in the kernel
2958 	 * address space)
2959 	 */
2960 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2961 	if (capend > mp->b_wptr) {
2962 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2963 		    "malformed sub-capability too long for mblk");
2964 		return;
2965 	}
2966 
2967 	/*
2968 	 * There are two types of acks we process here:
2969 	 * 1. acks in reply to a (first form) generic capability req
2970 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2971 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2972 	 *    capability req.
2973 	 */
2974 	idls = (dl_capab_dls_t *)(isub + 1);
2975 
2976 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2977 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2978 		    "capability isn't as expected; pass-thru "
2979 		    "module(s) detected, discarding capability\n"));
2980 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2981 			/*
2982 			 * This is a capability renegotitation case.
2983 			 * The interface better be unusable at this
2984 			 * point other wise bad things will happen
2985 			 * if we disable direct calls on a running
2986 			 * and up interface.
2987 			 */
2988 			ill_capability_dls_disable(ill);
2989 		}
2990 		return;
2991 	}
2992 
2993 	switch (idls->dls_flags) {
2994 	default:
2995 		/* Disable if unknown flag */
2996 	case SOFT_RING_DISABLE:
2997 	case POLL_DISABLE:
2998 		ill_capability_dls_disable(ill);
2999 		break;
3000 	case SOFT_RING_CAPABLE:
3001 	case POLL_CAPABLE:
3002 		/*
3003 		 * If the capability was already enabled, its safe
3004 		 * to disable it first to get rid of stale information
3005 		 * and then start enabling it again.
3006 		 */
3007 		ill_capability_dls_disable(ill);
3008 		ill_capability_dls_capable(ill, idls, isub);
3009 		break;
3010 	case SOFT_RING_ENABLE:
3011 	case POLL_ENABLE:
3012 		mutex_enter(&ill->ill_lock);
3013 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3014 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3015 			ASSERT(ill->ill_dls_capab != NULL);
3016 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3017 		}
3018 		if (sub_dl_cap == DL_CAPAB_POLL &&
3019 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3020 			ASSERT(ill->ill_dls_capab != NULL);
3021 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3022 			ip1dbg(("ill_capability_dls_ack: interface %s "
3023 			    "has enabled polling\n", ill->ill_name));
3024 		}
3025 		mutex_exit(&ill->ill_lock);
3026 		break;
3027 	}
3028 }
3029 
3030 /*
3031  * Process a hardware checksum offload capability negotiation ack received
3032  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3033  * of a DL_CAPABILITY_ACK message.
3034  */
3035 static void
3036 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3037 {
3038 	dl_capability_req_t	*ocap;
3039 	dl_capab_hcksum_t	*ihck, *ohck;
3040 	ill_hcksum_capab_t	**ill_hcksum;
3041 	mblk_t			*nmp = NULL;
3042 	uint_t			sub_dl_cap = isub->dl_cap;
3043 	uint8_t			*capend;
3044 
3045 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3046 
3047 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3048 
3049 	/*
3050 	 * Note: range checks here are not absolutely sufficient to
3051 	 * make us robust against malformed messages sent by drivers;
3052 	 * this is in keeping with the rest of IP's dlpi handling.
3053 	 * (Remember, it's coming from something else in the kernel
3054 	 * address space)
3055 	 */
3056 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3057 	if (capend > mp->b_wptr) {
3058 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3059 		    "malformed sub-capability too long for mblk");
3060 		return;
3061 	}
3062 
3063 	/*
3064 	 * There are two types of acks we process here:
3065 	 * 1. acks in reply to a (first form) generic capability req
3066 	 *    (no ENABLE flag set)
3067 	 * 2. acks in reply to a ENABLE capability req.
3068 	 *    (ENABLE flag set)
3069 	 */
3070 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3071 
3072 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3073 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3074 		    "unsupported hardware checksum "
3075 		    "sub-capability (version %d, expected %d)",
3076 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3077 		return;
3078 	}
3079 
3080 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3081 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3082 		    "checksum capability isn't as expected; pass-thru "
3083 		    "module(s) detected, discarding capability\n"));
3084 		return;
3085 	}
3086 
3087 #define	CURR_HCKSUM_CAPAB				\
3088 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3089 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3090 
3091 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3092 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3093 		/* do ENABLE processing */
3094 		if (*ill_hcksum == NULL) {
3095 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3096 			    KM_NOSLEEP);
3097 
3098 			if (*ill_hcksum == NULL) {
3099 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3100 				    "could not enable hcksum version %d "
3101 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3102 				    ill->ill_name);
3103 				return;
3104 			}
3105 		}
3106 
3107 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3108 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3109 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3110 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3111 		    "has enabled hardware checksumming\n ",
3112 		    ill->ill_name));
3113 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3114 		/*
3115 		 * Enabling hardware checksum offload
3116 		 * Currently IP supports {TCP,UDP}/IPv4
3117 		 * partial and full cksum offload and
3118 		 * IPv4 header checksum offload.
3119 		 * Allocate new mblk which will
3120 		 * contain a new capability request
3121 		 * to enable hardware checksum offload.
3122 		 */
3123 		uint_t	size;
3124 		uchar_t	*rptr;
3125 
3126 		size = sizeof (dl_capability_req_t) +
3127 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3128 
3129 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3130 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3131 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3132 			    ill->ill_name);
3133 			return;
3134 		}
3135 
3136 		rptr = nmp->b_rptr;
3137 		/* initialize dl_capability_req_t */
3138 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3139 		ocap->dl_sub_offset =
3140 		    sizeof (dl_capability_req_t);
3141 		ocap->dl_sub_length =
3142 		    sizeof (dl_capability_sub_t) +
3143 		    isub->dl_length;
3144 		nmp->b_rptr += sizeof (dl_capability_req_t);
3145 
3146 		/* initialize dl_capability_sub_t */
3147 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3148 		nmp->b_rptr += sizeof (*isub);
3149 
3150 		/* initialize dl_capab_hcksum_t */
3151 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3152 		bcopy(ihck, ohck, sizeof (*ihck));
3153 
3154 		nmp->b_rptr = rptr;
3155 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3156 
3157 		/* Set ENABLE flag */
3158 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3159 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3160 
3161 		/*
3162 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3163 		 * hardware checksum acceleration.
3164 		 */
3165 		ill_dlpi_send(ill, nmp);
3166 	} else {
3167 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3168 		    "advertised %x hardware checksum capability flags\n",
3169 		    ill->ill_name, ihck->hcksum_txflags));
3170 	}
3171 }
3172 
3173 static void
3174 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3175 {
3176 	mblk_t *mp;
3177 	dl_capab_hcksum_t *hck_subcap;
3178 	dl_capability_sub_t *dl_subcap;
3179 	int size;
3180 
3181 	if (!ILL_HCKSUM_CAPABLE(ill))
3182 		return;
3183 
3184 	ASSERT(ill->ill_hcksum_capab != NULL);
3185 	/*
3186 	 * Clear the capability flag for hardware checksum offload but
3187 	 * retain the ill_hcksum_capab structure since it's possible that
3188 	 * another thread is still referring to it.  The structure only
3189 	 * gets deallocated when we destroy the ill.
3190 	 */
3191 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3192 
3193 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3194 
3195 	mp = allocb(size, BPRI_HI);
3196 	if (mp == NULL) {
3197 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3198 		    "request to disable hardware checksum offload\n"));
3199 		return;
3200 	}
3201 
3202 	mp->b_wptr = mp->b_rptr + size;
3203 
3204 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3205 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3206 	dl_subcap->dl_length = sizeof (*hck_subcap);
3207 
3208 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3209 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3210 	hck_subcap->hcksum_txflags = 0;
3211 
3212 	if (*sc_mp != NULL)
3213 		linkb(*sc_mp, mp);
3214 	else
3215 		*sc_mp = mp;
3216 }
3217 
3218 static void
3219 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3220 {
3221 	mblk_t *nmp = NULL;
3222 	dl_capability_req_t *oc;
3223 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3224 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3225 	uint_t sub_dl_cap = isub->dl_cap;
3226 	uint8_t *capend;
3227 
3228 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3229 
3230 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3231 
3232 	/*
3233 	 * Note: range checks here are not absolutely sufficient to
3234 	 * make us robust against malformed messages sent by drivers;
3235 	 * this is in keeping with the rest of IP's dlpi handling.
3236 	 * (Remember, it's coming from something else in the kernel
3237 	 * address space)
3238 	 */
3239 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3240 	if (capend > mp->b_wptr) {
3241 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3242 		    "malformed sub-capability too long for mblk");
3243 		return;
3244 	}
3245 
3246 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3247 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3248 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3249 		    "unsupported ZEROCOPY sub-capability (version %d, "
3250 		    "expected %d)", zc_ic->zerocopy_version,
3251 		    ZEROCOPY_VERSION_1);
3252 		return;
3253 	}
3254 
3255 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3256 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3257 		    "capability isn't as expected; pass-thru module(s) "
3258 		    "detected, discarding capability\n"));
3259 		return;
3260 	}
3261 
3262 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3263 		if (*ill_zerocopy_capab == NULL) {
3264 			*ill_zerocopy_capab =
3265 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3266 			    KM_NOSLEEP);
3267 
3268 			if (*ill_zerocopy_capab == NULL) {
3269 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3270 				    "could not enable Zero-copy version %d "
3271 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3272 				    ill->ill_name);
3273 				return;
3274 			}
3275 		}
3276 
3277 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3278 		    "supports Zero-copy version %d\n", ill->ill_name,
3279 		    ZEROCOPY_VERSION_1));
3280 
3281 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3282 		    zc_ic->zerocopy_version;
3283 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3284 		    zc_ic->zerocopy_flags;
3285 
3286 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3287 	} else {
3288 		uint_t size;
3289 		uchar_t *rptr;
3290 
3291 		size = sizeof (dl_capability_req_t) +
3292 		    sizeof (dl_capability_sub_t) +
3293 		    sizeof (dl_capab_zerocopy_t);
3294 
3295 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3296 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3297 			    "could not enable zerocopy for %s (ENOMEM)\n",
3298 			    ill->ill_name);
3299 			return;
3300 		}
3301 
3302 		rptr = nmp->b_rptr;
3303 		/* initialize dl_capability_req_t */
3304 		oc = (dl_capability_req_t *)rptr;
3305 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3306 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3307 		    sizeof (dl_capab_zerocopy_t);
3308 		rptr += sizeof (dl_capability_req_t);
3309 
3310 		/* initialize dl_capability_sub_t */
3311 		bcopy(isub, rptr, sizeof (*isub));
3312 		rptr += sizeof (*isub);
3313 
3314 		/* initialize dl_capab_zerocopy_t */
3315 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3316 		*zc_oc = *zc_ic;
3317 
3318 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3319 		    "to enable zero-copy version %d\n", ill->ill_name,
3320 		    ZEROCOPY_VERSION_1));
3321 
3322 		/* set VMSAFE_MEM flag */
3323 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3324 
3325 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3326 		ill_dlpi_send(ill, nmp);
3327 	}
3328 }
3329 
3330 static void
3331 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3332 {
3333 	mblk_t *mp;
3334 	dl_capab_zerocopy_t *zerocopy_subcap;
3335 	dl_capability_sub_t *dl_subcap;
3336 	int size;
3337 
3338 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3339 		return;
3340 
3341 	ASSERT(ill->ill_zerocopy_capab != NULL);
3342 	/*
3343 	 * Clear the capability flag for Zero-copy but retain the
3344 	 * ill_zerocopy_capab structure since it's possible that another
3345 	 * thread is still referring to it.  The structure only gets
3346 	 * deallocated when we destroy the ill.
3347 	 */
3348 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3349 
3350 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3351 
3352 	mp = allocb(size, BPRI_HI);
3353 	if (mp == NULL) {
3354 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3355 		    "request to disable Zero-copy\n"));
3356 		return;
3357 	}
3358 
3359 	mp->b_wptr = mp->b_rptr + size;
3360 
3361 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3362 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3363 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3364 
3365 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3366 	zerocopy_subcap->zerocopy_version =
3367 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3368 	zerocopy_subcap->zerocopy_flags = 0;
3369 
3370 	if (*sc_mp != NULL)
3371 		linkb(*sc_mp, mp);
3372 	else
3373 		*sc_mp = mp;
3374 }
3375 
3376 /*
3377  * Process Large Segment Offload capability negotiation ack received from a
3378  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3379  * DL_CAPABILITY_ACK message.
3380  */
3381 static void
3382 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3383 {
3384 	mblk_t *nmp = NULL;
3385 	dl_capability_req_t *oc;
3386 	dl_capab_lso_t *lso_ic, *lso_oc;
3387 	ill_lso_capab_t **ill_lso_capab;
3388 	uint_t sub_dl_cap = isub->dl_cap;
3389 	uint8_t *capend;
3390 
3391 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3392 
3393 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3394 
3395 	/*
3396 	 * Note: range checks here are not absolutely sufficient to
3397 	 * make us robust against malformed messages sent by drivers;
3398 	 * this is in keeping with the rest of IP's dlpi handling.
3399 	 * (Remember, it's coming from something else in the kernel
3400 	 * address space)
3401 	 */
3402 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3403 	if (capend > mp->b_wptr) {
3404 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3405 		    "malformed sub-capability too long for mblk");
3406 		return;
3407 	}
3408 
3409 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3410 
3411 	if (lso_ic->lso_version != LSO_VERSION_1) {
3412 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3413 		    "unsupported LSO sub-capability (version %d, expected %d)",
3414 		    lso_ic->lso_version, LSO_VERSION_1);
3415 		return;
3416 	}
3417 
3418 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3419 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3420 		    "capability isn't as expected; pass-thru module(s) "
3421 		    "detected, discarding capability\n"));
3422 		return;
3423 	}
3424 
3425 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3426 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3427 		if (*ill_lso_capab == NULL) {
3428 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3429 			    KM_NOSLEEP);
3430 
3431 			if (*ill_lso_capab == NULL) {
3432 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3433 				    "could not enable LSO version %d "
3434 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3435 				    ill->ill_name);
3436 				return;
3437 			}
3438 		}
3439 
3440 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3441 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3442 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3443 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3444 
3445 		ip1dbg(("ill_capability_lso_ack: interface %s "
3446 		    "has enabled LSO\n ", ill->ill_name));
3447 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3448 		uint_t size;
3449 		uchar_t *rptr;
3450 
3451 		size = sizeof (dl_capability_req_t) +
3452 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3453 
3454 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3455 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3456 			    "could not enable LSO for %s (ENOMEM)\n",
3457 			    ill->ill_name);
3458 			return;
3459 		}
3460 
3461 		rptr = nmp->b_rptr;
3462 		/* initialize dl_capability_req_t */
3463 		oc = (dl_capability_req_t *)nmp->b_rptr;
3464 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3465 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3466 		    sizeof (dl_capab_lso_t);
3467 		nmp->b_rptr += sizeof (dl_capability_req_t);
3468 
3469 		/* initialize dl_capability_sub_t */
3470 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3471 		nmp->b_rptr += sizeof (*isub);
3472 
3473 		/* initialize dl_capab_lso_t */
3474 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3475 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3476 
3477 		nmp->b_rptr = rptr;
3478 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3479 
3480 		/* set ENABLE flag */
3481 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3482 
3483 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3484 		ill_dlpi_send(ill, nmp);
3485 	} else {
3486 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3487 		    "advertised %x LSO capability flags\n",
3488 		    ill->ill_name, lso_ic->lso_flags));
3489 	}
3490 }
3491 
3492 
3493 static void
3494 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3495 {
3496 	mblk_t *mp;
3497 	dl_capab_lso_t *lso_subcap;
3498 	dl_capability_sub_t *dl_subcap;
3499 	int size;
3500 
3501 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3502 		return;
3503 
3504 	ASSERT(ill->ill_lso_capab != NULL);
3505 	/*
3506 	 * Clear the capability flag for LSO but retain the
3507 	 * ill_lso_capab structure since it's possible that another
3508 	 * thread is still referring to it.  The structure only gets
3509 	 * deallocated when we destroy the ill.
3510 	 */
3511 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3512 
3513 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3514 
3515 	mp = allocb(size, BPRI_HI);
3516 	if (mp == NULL) {
3517 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3518 		    "request to disable LSO\n"));
3519 		return;
3520 	}
3521 
3522 	mp->b_wptr = mp->b_rptr + size;
3523 
3524 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3525 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3526 	dl_subcap->dl_length = sizeof (*lso_subcap);
3527 
3528 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3529 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3530 	lso_subcap->lso_flags = 0;
3531 
3532 	if (*sc_mp != NULL)
3533 		linkb(*sc_mp, mp);
3534 	else
3535 		*sc_mp = mp;
3536 }
3537 
3538 /*
3539  * Consume a new-style hardware capabilities negotiation ack.
3540  * Called from ip_rput_dlpi_writer().
3541  */
3542 void
3543 ill_capability_ack(ill_t *ill, mblk_t *mp)
3544 {
3545 	dl_capability_ack_t *capp;
3546 	dl_capability_sub_t *subp, *endp;
3547 
3548 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3549 		ill->ill_dlpi_capab_state = IDS_OK;
3550 
3551 	capp = (dl_capability_ack_t *)mp->b_rptr;
3552 
3553 	if (capp->dl_sub_length == 0)
3554 		/* no new-style capabilities */
3555 		return;
3556 
3557 	/* make sure the driver supplied correct dl_sub_length */
3558 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3559 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3560 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3561 		return;
3562 	}
3563 
3564 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3565 	/*
3566 	 * There are sub-capabilities. Process the ones we know about.
3567 	 * Loop until we don't have room for another sub-cap header..
3568 	 */
3569 	for (subp = SC(capp, capp->dl_sub_offset),
3570 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3571 	    subp <= endp;
3572 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3573 
3574 		switch (subp->dl_cap) {
3575 		case DL_CAPAB_ID_WRAPPER:
3576 			ill_capability_id_ack(ill, mp, subp);
3577 			break;
3578 		default:
3579 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3580 			break;
3581 		}
3582 	}
3583 #undef SC
3584 }
3585 
3586 /*
3587  * This routine is called to scan the fragmentation reassembly table for
3588  * the specified ILL for any packets that are starting to smell.
3589  * dead_interval is the maximum time in seconds that will be tolerated.  It
3590  * will either be the value specified in ip_g_frag_timeout, or zero if the
3591  * ILL is shutting down and it is time to blow everything off.
3592  *
3593  * It returns the number of seconds (as a time_t) that the next frag timer
3594  * should be scheduled for, 0 meaning that the timer doesn't need to be
3595  * re-started.  Note that the method of calculating next_timeout isn't
3596  * entirely accurate since time will flow between the time we grab
3597  * current_time and the time we schedule the next timeout.  This isn't a
3598  * big problem since this is the timer for sending an ICMP reassembly time
3599  * exceeded messages, and it doesn't have to be exactly accurate.
3600  *
3601  * This function is
3602  * sometimes called as writer, although this is not required.
3603  */
3604 time_t
3605 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3606 {
3607 	ipfb_t	*ipfb;
3608 	ipfb_t	*endp;
3609 	ipf_t	*ipf;
3610 	ipf_t	*ipfnext;
3611 	mblk_t	*mp;
3612 	time_t	current_time = gethrestime_sec();
3613 	time_t	next_timeout = 0;
3614 	uint32_t	hdr_length;
3615 	mblk_t	*send_icmp_head;
3616 	mblk_t	*send_icmp_head_v6;
3617 	zoneid_t zoneid;
3618 	ip_stack_t *ipst = ill->ill_ipst;
3619 
3620 	ipfb = ill->ill_frag_hash_tbl;
3621 	if (ipfb == NULL)
3622 		return (B_FALSE);
3623 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3624 	/* Walk the frag hash table. */
3625 	for (; ipfb < endp; ipfb++) {
3626 		send_icmp_head = NULL;
3627 		send_icmp_head_v6 = NULL;
3628 		mutex_enter(&ipfb->ipfb_lock);
3629 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3630 			time_t frag_time = current_time - ipf->ipf_timestamp;
3631 			time_t frag_timeout;
3632 
3633 			if (frag_time < dead_interval) {
3634 				/*
3635 				 * There are some outstanding fragments
3636 				 * that will timeout later.  Make note of
3637 				 * the time so that we can reschedule the
3638 				 * next timeout appropriately.
3639 				 */
3640 				frag_timeout = dead_interval - frag_time;
3641 				if (next_timeout == 0 ||
3642 				    frag_timeout < next_timeout) {
3643 					next_timeout = frag_timeout;
3644 				}
3645 				break;
3646 			}
3647 			/* Time's up.  Get it out of here. */
3648 			hdr_length = ipf->ipf_nf_hdr_len;
3649 			ipfnext = ipf->ipf_hash_next;
3650 			if (ipfnext)
3651 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3652 			*ipf->ipf_ptphn = ipfnext;
3653 			mp = ipf->ipf_mp->b_cont;
3654 			for (; mp; mp = mp->b_cont) {
3655 				/* Extra points for neatness. */
3656 				IP_REASS_SET_START(mp, 0);
3657 				IP_REASS_SET_END(mp, 0);
3658 			}
3659 			mp = ipf->ipf_mp->b_cont;
3660 			ill->ill_frag_count -= ipf->ipf_count;
3661 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3662 			ipfb->ipfb_count -= ipf->ipf_count;
3663 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3664 			ipfb->ipfb_frag_pkts--;
3665 			/*
3666 			 * We do not send any icmp message from here because
3667 			 * we currently are holding the ipfb_lock for this
3668 			 * hash chain. If we try and send any icmp messages
3669 			 * from here we may end up via a put back into ip
3670 			 * trying to get the same lock, causing a recursive
3671 			 * mutex panic. Instead we build a list and send all
3672 			 * the icmp messages after we have dropped the lock.
3673 			 */
3674 			if (ill->ill_isv6) {
3675 				if (hdr_length != 0) {
3676 					mp->b_next = send_icmp_head_v6;
3677 					send_icmp_head_v6 = mp;
3678 				} else {
3679 					freemsg(mp);
3680 				}
3681 			} else {
3682 				if (hdr_length != 0) {
3683 					mp->b_next = send_icmp_head;
3684 					send_icmp_head = mp;
3685 				} else {
3686 					freemsg(mp);
3687 				}
3688 			}
3689 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3690 			freeb(ipf->ipf_mp);
3691 		}
3692 		mutex_exit(&ipfb->ipfb_lock);
3693 		/*
3694 		 * Now need to send any icmp messages that we delayed from
3695 		 * above.
3696 		 */
3697 		while (send_icmp_head_v6 != NULL) {
3698 			ip6_t *ip6h;
3699 
3700 			mp = send_icmp_head_v6;
3701 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3702 			mp->b_next = NULL;
3703 			if (mp->b_datap->db_type == M_CTL)
3704 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3705 			else
3706 				ip6h = (ip6_t *)mp->b_rptr;
3707 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3708 			    ill, ipst);
3709 			if (zoneid == ALL_ZONES) {
3710 				freemsg(mp);
3711 			} else {
3712 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3713 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3714 				    B_FALSE, zoneid, ipst);
3715 			}
3716 		}
3717 		while (send_icmp_head != NULL) {
3718 			ipaddr_t dst;
3719 
3720 			mp = send_icmp_head;
3721 			send_icmp_head = send_icmp_head->b_next;
3722 			mp->b_next = NULL;
3723 
3724 			if (mp->b_datap->db_type == M_CTL)
3725 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3726 			else
3727 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3728 
3729 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3730 			if (zoneid == ALL_ZONES) {
3731 				freemsg(mp);
3732 			} else {
3733 				icmp_time_exceeded(ill->ill_wq, mp,
3734 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3735 				    ipst);
3736 			}
3737 		}
3738 	}
3739 	/*
3740 	 * A non-dying ILL will use the return value to decide whether to
3741 	 * restart the frag timer, and for how long.
3742 	 */
3743 	return (next_timeout);
3744 }
3745 
3746 /*
3747  * This routine is called when the approximate count of mblk memory used
3748  * for the specified ILL has exceeded max_count.
3749  */
3750 void
3751 ill_frag_prune(ill_t *ill, uint_t max_count)
3752 {
3753 	ipfb_t	*ipfb;
3754 	ipf_t	*ipf;
3755 	size_t	count;
3756 
3757 	/*
3758 	 * If we are here within ip_min_frag_prune_time msecs remove
3759 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3760 	 * ill_frag_free_num_pkts.
3761 	 */
3762 	mutex_enter(&ill->ill_lock);
3763 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3764 	    (ip_min_frag_prune_time != 0 ?
3765 	    ip_min_frag_prune_time : msec_per_tick)) {
3766 
3767 		ill->ill_frag_free_num_pkts++;
3768 
3769 	} else {
3770 		ill->ill_frag_free_num_pkts = 0;
3771 	}
3772 	ill->ill_last_frag_clean_time = lbolt;
3773 	mutex_exit(&ill->ill_lock);
3774 
3775 	/*
3776 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3777 	 */
3778 	if (ill->ill_frag_free_num_pkts != 0) {
3779 		int ix;
3780 
3781 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3782 			ipfb = &ill->ill_frag_hash_tbl[ix];
3783 			mutex_enter(&ipfb->ipfb_lock);
3784 			if (ipfb->ipfb_ipf != NULL) {
3785 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3786 				    ill->ill_frag_free_num_pkts);
3787 			}
3788 			mutex_exit(&ipfb->ipfb_lock);
3789 		}
3790 	}
3791 	/*
3792 	 * While the reassembly list for this ILL is too big, prune a fragment
3793 	 * queue by age, oldest first.  Note that the per ILL count is
3794 	 * approximate, while the per frag hash bucket counts are accurate.
3795 	 */
3796 	while (ill->ill_frag_count > max_count) {
3797 		int	ix;
3798 		ipfb_t	*oipfb = NULL;
3799 		uint_t	oldest = UINT_MAX;
3800 
3801 		count = 0;
3802 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3803 			ipfb = &ill->ill_frag_hash_tbl[ix];
3804 			mutex_enter(&ipfb->ipfb_lock);
3805 			ipf = ipfb->ipfb_ipf;
3806 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3807 				oldest = ipf->ipf_gen;
3808 				oipfb = ipfb;
3809 			}
3810 			count += ipfb->ipfb_count;
3811 			mutex_exit(&ipfb->ipfb_lock);
3812 		}
3813 		/* Refresh the per ILL count */
3814 		ill->ill_frag_count = count;
3815 		if (oipfb == NULL) {
3816 			ill->ill_frag_count = 0;
3817 			break;
3818 		}
3819 		if (count <= max_count)
3820 			return;	/* Somebody beat us to it, nothing to do */
3821 		mutex_enter(&oipfb->ipfb_lock);
3822 		ipf = oipfb->ipfb_ipf;
3823 		if (ipf != NULL) {
3824 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3825 		}
3826 		mutex_exit(&oipfb->ipfb_lock);
3827 	}
3828 }
3829 
3830 /*
3831  * free 'free_cnt' fragmented packets starting at ipf.
3832  */
3833 void
3834 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3835 {
3836 	size_t	count;
3837 	mblk_t	*mp;
3838 	mblk_t	*tmp;
3839 	ipf_t **ipfp = ipf->ipf_ptphn;
3840 
3841 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3842 	ASSERT(ipfp != NULL);
3843 	ASSERT(ipf != NULL);
3844 
3845 	while (ipf != NULL && free_cnt-- > 0) {
3846 		count = ipf->ipf_count;
3847 		mp = ipf->ipf_mp;
3848 		ipf = ipf->ipf_hash_next;
3849 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3850 			IP_REASS_SET_START(tmp, 0);
3851 			IP_REASS_SET_END(tmp, 0);
3852 		}
3853 		ill->ill_frag_count -= count;
3854 		ASSERT(ipfb->ipfb_count >= count);
3855 		ipfb->ipfb_count -= count;
3856 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3857 		ipfb->ipfb_frag_pkts--;
3858 		freemsg(mp);
3859 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3860 	}
3861 
3862 	if (ipf)
3863 		ipf->ipf_ptphn = ipfp;
3864 	ipfp[0] = ipf;
3865 }
3866 
3867 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3868 	"obsolete and may be removed in a future release of Solaris.  Use " \
3869 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3870 
3871 /*
3872  * For obsolete per-interface forwarding configuration;
3873  * called in response to ND_GET.
3874  */
3875 /* ARGSUSED */
3876 static int
3877 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3878 {
3879 	ill_t *ill = (ill_t *)cp;
3880 
3881 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3882 
3883 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3884 	return (0);
3885 }
3886 
3887 /*
3888  * For obsolete per-interface forwarding configuration;
3889  * called in response to ND_SET.
3890  */
3891 /* ARGSUSED */
3892 static int
3893 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3894     cred_t *ioc_cr)
3895 {
3896 	long value;
3897 	int retval;
3898 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3899 
3900 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3901 
3902 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3903 	    value < 0 || value > 1) {
3904 		return (EINVAL);
3905 	}
3906 
3907 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3908 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3909 	rw_exit(&ipst->ips_ill_g_lock);
3910 	return (retval);
3911 }
3912 
3913 /*
3914  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3915  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3916  * up RTS_IFINFO routing socket messages for each interface whose flags we
3917  * change.
3918  */
3919 int
3920 ill_forward_set(ill_t *ill, boolean_t enable)
3921 {
3922 	ill_group_t *illgrp;
3923 	ip_stack_t	*ipst = ill->ill_ipst;
3924 
3925 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3926 
3927 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3928 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3929 		return (0);
3930 
3931 	if (IS_LOOPBACK(ill))
3932 		return (EINVAL);
3933 
3934 	/*
3935 	 * If the ill is in an IPMP group, set the forwarding policy on all
3936 	 * members of the group to the same value.
3937 	 */
3938 	illgrp = ill->ill_group;
3939 	if (illgrp != NULL) {
3940 		ill_t *tmp_ill;
3941 
3942 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3943 		    tmp_ill = tmp_ill->ill_group_next) {
3944 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3945 			    (enable ? "Enabling" : "Disabling"),
3946 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3947 			    tmp_ill->ill_name));
3948 			mutex_enter(&tmp_ill->ill_lock);
3949 			if (enable)
3950 				tmp_ill->ill_flags |= ILLF_ROUTER;
3951 			else
3952 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3953 			mutex_exit(&tmp_ill->ill_lock);
3954 			if (tmp_ill->ill_isv6)
3955 				ill_set_nce_router_flags(tmp_ill, enable);
3956 			/* Notify routing socket listeners of this change. */
3957 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3958 		}
3959 	} else {
3960 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3961 		    (enable ? "Enabling" : "Disabling"),
3962 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3963 		mutex_enter(&ill->ill_lock);
3964 		if (enable)
3965 			ill->ill_flags |= ILLF_ROUTER;
3966 		else
3967 			ill->ill_flags &= ~ILLF_ROUTER;
3968 		mutex_exit(&ill->ill_lock);
3969 		if (ill->ill_isv6)
3970 			ill_set_nce_router_flags(ill, enable);
3971 		/* Notify routing socket listeners of this change. */
3972 		ip_rts_ifmsg(ill->ill_ipif);
3973 	}
3974 
3975 	return (0);
3976 }
3977 
3978 /*
3979  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3980  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3981  * set or clear.
3982  */
3983 static void
3984 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3985 {
3986 	ipif_t *ipif;
3987 	nce_t *nce;
3988 
3989 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3990 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3991 		if (nce != NULL) {
3992 			mutex_enter(&nce->nce_lock);
3993 			if (enable)
3994 				nce->nce_flags |= NCE_F_ISROUTER;
3995 			else
3996 				nce->nce_flags &= ~NCE_F_ISROUTER;
3997 			mutex_exit(&nce->nce_lock);
3998 			NCE_REFRELE(nce);
3999 		}
4000 	}
4001 }
4002 
4003 /*
4004  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4005  * for this ill.  Make sure the v6/v4 question has been answered about this
4006  * ill.  The creation of this ndd variable is only for backwards compatibility.
4007  * The preferred way to control per-interface IP forwarding is through the
4008  * ILLF_ROUTER interface flag.
4009  */
4010 static int
4011 ill_set_ndd_name(ill_t *ill)
4012 {
4013 	char *suffix;
4014 	ip_stack_t	*ipst = ill->ill_ipst;
4015 
4016 	ASSERT(IAM_WRITER_ILL(ill));
4017 
4018 	if (ill->ill_isv6)
4019 		suffix = ipv6_forward_suffix;
4020 	else
4021 		suffix = ipv4_forward_suffix;
4022 
4023 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4024 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4025 	/*
4026 	 * Copies over the '\0'.
4027 	 * Note that strlen(suffix) is always bounded.
4028 	 */
4029 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4030 	    strlen(suffix) + 1);
4031 
4032 	/*
4033 	 * Use of the nd table requires holding the reader lock.
4034 	 * Modifying the nd table thru nd_load/nd_unload requires
4035 	 * the writer lock.
4036 	 */
4037 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4038 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4039 	    nd_ill_forward_set, (caddr_t)ill)) {
4040 		/*
4041 		 * If the nd_load failed, it only meant that it could not
4042 		 * allocate a new bunch of room for further NDD expansion.
4043 		 * Because of that, the ill_ndd_name will be set to 0, and
4044 		 * this interface is at the mercy of the global ip_forwarding
4045 		 * variable.
4046 		 */
4047 		rw_exit(&ipst->ips_ip_g_nd_lock);
4048 		ill->ill_ndd_name = NULL;
4049 		return (ENOMEM);
4050 	}
4051 	rw_exit(&ipst->ips_ip_g_nd_lock);
4052 	return (0);
4053 }
4054 
4055 /*
4056  * Intializes the context structure and returns the first ill in the list
4057  * cuurently start_list and end_list can have values:
4058  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4059  * IP_V4_G_HEAD		Traverse IPV4 list only.
4060  * IP_V6_G_HEAD		Traverse IPV6 list only.
4061  */
4062 
4063 /*
4064  * We don't check for CONDEMNED ills here. Caller must do that if
4065  * necessary under the ill lock.
4066  */
4067 ill_t *
4068 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4069     ip_stack_t *ipst)
4070 {
4071 	ill_if_t *ifp;
4072 	ill_t *ill;
4073 	avl_tree_t *avl_tree;
4074 
4075 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4076 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4077 
4078 	/*
4079 	 * setup the lists to search
4080 	 */
4081 	if (end_list != MAX_G_HEADS) {
4082 		ctx->ctx_current_list = start_list;
4083 		ctx->ctx_last_list = end_list;
4084 	} else {
4085 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4086 		ctx->ctx_current_list = 0;
4087 	}
4088 
4089 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4090 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4091 		if (ifp != (ill_if_t *)
4092 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4093 			avl_tree = &ifp->illif_avl_by_ppa;
4094 			ill = avl_first(avl_tree);
4095 			/*
4096 			 * ill is guaranteed to be non NULL or ifp should have
4097 			 * not existed.
4098 			 */
4099 			ASSERT(ill != NULL);
4100 			return (ill);
4101 		}
4102 		ctx->ctx_current_list++;
4103 	}
4104 
4105 	return (NULL);
4106 }
4107 
4108 /*
4109  * returns the next ill in the list. ill_first() must have been called
4110  * before calling ill_next() or bad things will happen.
4111  */
4112 
4113 /*
4114  * We don't check for CONDEMNED ills here. Caller must do that if
4115  * necessary under the ill lock.
4116  */
4117 ill_t *
4118 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4119 {
4120 	ill_if_t *ifp;
4121 	ill_t *ill;
4122 	ip_stack_t	*ipst = lastill->ill_ipst;
4123 
4124 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4125 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4126 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4127 	    AVL_AFTER)) != NULL) {
4128 		return (ill);
4129 	}
4130 
4131 	/* goto next ill_ifp in the list. */
4132 	ifp = lastill->ill_ifptr->illif_next;
4133 
4134 	/* make sure not at end of circular list */
4135 	while (ifp ==
4136 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4137 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4138 			return (NULL);
4139 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4140 	}
4141 
4142 	return (avl_first(&ifp->illif_avl_by_ppa));
4143 }
4144 
4145 /*
4146  * Check interface name for correct format which is name+ppa.
4147  * name can contain characters and digits, the right most digits
4148  * make up the ppa number. use of octal is not allowed, name must contain
4149  * a ppa, return pointer to the start of ppa.
4150  * In case of error return NULL.
4151  */
4152 static char *
4153 ill_get_ppa_ptr(char *name)
4154 {
4155 	int namelen = mi_strlen(name);
4156 
4157 	int len = namelen;
4158 
4159 	name += len;
4160 	while (len > 0) {
4161 		name--;
4162 		if (*name < '0' || *name > '9')
4163 			break;
4164 		len--;
4165 	}
4166 
4167 	/* empty string, all digits, or no trailing digits */
4168 	if (len == 0 || len == (int)namelen)
4169 		return (NULL);
4170 
4171 	name++;
4172 	/* check for attempted use of octal */
4173 	if (*name == '0' && len != (int)namelen - 1)
4174 		return (NULL);
4175 	return (name);
4176 }
4177 
4178 /*
4179  * use avl tree to locate the ill.
4180  */
4181 static ill_t *
4182 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4183     ipsq_func_t func, int *error, ip_stack_t *ipst)
4184 {
4185 	char *ppa_ptr = NULL;
4186 	int len;
4187 	uint_t ppa;
4188 	ill_t *ill = NULL;
4189 	ill_if_t *ifp;
4190 	int list;
4191 	ipsq_t *ipsq;
4192 
4193 	if (error != NULL)
4194 		*error = 0;
4195 
4196 	/*
4197 	 * get ppa ptr
4198 	 */
4199 	if (isv6)
4200 		list = IP_V6_G_HEAD;
4201 	else
4202 		list = IP_V4_G_HEAD;
4203 
4204 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4205 		if (error != NULL)
4206 			*error = ENXIO;
4207 		return (NULL);
4208 	}
4209 
4210 	len = ppa_ptr - name + 1;
4211 
4212 	ppa = stoi(&ppa_ptr);
4213 
4214 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4215 
4216 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4217 		/*
4218 		 * match is done on len - 1 as the name is not null
4219 		 * terminated it contains ppa in addition to the interface
4220 		 * name.
4221 		 */
4222 		if ((ifp->illif_name_len == len) &&
4223 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4224 			break;
4225 		} else {
4226 			ifp = ifp->illif_next;
4227 		}
4228 	}
4229 
4230 
4231 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4232 		/*
4233 		 * Even the interface type does not exist.
4234 		 */
4235 		if (error != NULL)
4236 			*error = ENXIO;
4237 		return (NULL);
4238 	}
4239 
4240 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4241 	if (ill != NULL) {
4242 		/*
4243 		 * The block comment at the start of ipif_down
4244 		 * explains the use of the macros used below
4245 		 */
4246 		GRAB_CONN_LOCK(q);
4247 		mutex_enter(&ill->ill_lock);
4248 		if (ILL_CAN_LOOKUP(ill)) {
4249 			ill_refhold_locked(ill);
4250 			mutex_exit(&ill->ill_lock);
4251 			RELEASE_CONN_LOCK(q);
4252 			return (ill);
4253 		} else if (ILL_CAN_WAIT(ill, q)) {
4254 			ipsq = ill->ill_phyint->phyint_ipsq;
4255 			mutex_enter(&ipsq->ipsq_lock);
4256 			mutex_exit(&ill->ill_lock);
4257 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4258 			mutex_exit(&ipsq->ipsq_lock);
4259 			RELEASE_CONN_LOCK(q);
4260 			if (error != NULL)
4261 				*error = EINPROGRESS;
4262 			return (NULL);
4263 		}
4264 		mutex_exit(&ill->ill_lock);
4265 		RELEASE_CONN_LOCK(q);
4266 	}
4267 	if (error != NULL)
4268 		*error = ENXIO;
4269 	return (NULL);
4270 }
4271 
4272 /*
4273  * comparison function for use with avl.
4274  */
4275 static int
4276 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4277 {
4278 	uint_t ppa;
4279 	uint_t ill_ppa;
4280 
4281 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4282 
4283 	ppa = *((uint_t *)ppa_ptr);
4284 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4285 	/*
4286 	 * We want the ill with the lowest ppa to be on the
4287 	 * top.
4288 	 */
4289 	if (ill_ppa < ppa)
4290 		return (1);
4291 	if (ill_ppa > ppa)
4292 		return (-1);
4293 	return (0);
4294 }
4295 
4296 /*
4297  * remove an interface type from the global list.
4298  */
4299 static void
4300 ill_delete_interface_type(ill_if_t *interface)
4301 {
4302 	ASSERT(interface != NULL);
4303 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4304 
4305 	avl_destroy(&interface->illif_avl_by_ppa);
4306 	if (interface->illif_ppa_arena != NULL)
4307 		vmem_destroy(interface->illif_ppa_arena);
4308 
4309 	remque(interface);
4310 
4311 	mi_free(interface);
4312 }
4313 
4314 /*
4315  * remove ill from the global list.
4316  */
4317 static void
4318 ill_glist_delete(ill_t *ill)
4319 {
4320 	hook_nic_event_t *info;
4321 	ip_stack_t	*ipst;
4322 
4323 	if (ill == NULL)
4324 		return;
4325 	ipst = ill->ill_ipst;
4326 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4327 
4328 	/*
4329 	 * If the ill was never inserted into the AVL tree
4330 	 * we skip the if branch.
4331 	 */
4332 	if (ill->ill_ifptr != NULL) {
4333 		/*
4334 		 * remove from AVL tree and free ppa number
4335 		 */
4336 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4337 
4338 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4339 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4340 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4341 		}
4342 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4343 			ill_delete_interface_type(ill->ill_ifptr);
4344 		}
4345 
4346 		/*
4347 		 * Indicate ill is no longer in the list.
4348 		 */
4349 		ill->ill_ifptr = NULL;
4350 		ill->ill_name_length = 0;
4351 		ill->ill_name[0] = '\0';
4352 		ill->ill_ppa = UINT_MAX;
4353 	}
4354 
4355 	/*
4356 	 * Run the unplumb hook after the NIC has disappeared from being
4357 	 * visible so that attempts to revalidate its existance will fail.
4358 	 *
4359 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4360 	 * that the ordering of delivered events to listeners matches the
4361 	 * order of them in the kernel.
4362 	 */
4363 	info = ill->ill_nic_event_info;
4364 	if (info != NULL && info->hne_event == NE_DOWN) {
4365 		mutex_enter(&ill->ill_lock);
4366 		ill_nic_info_dispatch(ill);
4367 		mutex_exit(&ill->ill_lock);
4368 	}
4369 
4370 	/* Generate NE_UNPLUMB event for ill_name. */
4371 	(void) ill_hook_event_create(ill, 0, NE_UNPLUMB, ill->ill_name,
4372 	    ill->ill_name_length);
4373 
4374 	ill_phyint_free(ill);
4375 	rw_exit(&ipst->ips_ill_g_lock);
4376 }
4377 
4378 /*
4379  * allocate a ppa, if the number of plumbed interfaces of this type are
4380  * less than ill_no_arena do a linear search to find a unused ppa.
4381  * When the number goes beyond ill_no_arena switch to using an arena.
4382  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4383  * is the return value for an error condition, so allocation starts at one
4384  * and is decremented by one.
4385  */
4386 static int
4387 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4388 {
4389 	ill_t *tmp_ill;
4390 	uint_t start, end;
4391 	int ppa;
4392 
4393 	if (ifp->illif_ppa_arena == NULL &&
4394 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4395 		/*
4396 		 * Create an arena.
4397 		 */
4398 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4399 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4400 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4401 			/* allocate what has already been assigned */
4402 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4403 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4404 		    tmp_ill, AVL_AFTER)) {
4405 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4406 			    1,		/* size */
4407 			    1,		/* align/quantum */
4408 			    0,		/* phase */
4409 			    0,		/* nocross */
4410 			    /* minaddr */
4411 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4412 			    /* maxaddr */
4413 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4414 			    VM_NOSLEEP|VM_FIRSTFIT);
4415 			if (ppa == 0) {
4416 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4417 				    " failed while switching"));
4418 				vmem_destroy(ifp->illif_ppa_arena);
4419 				ifp->illif_ppa_arena = NULL;
4420 				break;
4421 			}
4422 		}
4423 	}
4424 
4425 	if (ifp->illif_ppa_arena != NULL) {
4426 		if (ill->ill_ppa == UINT_MAX) {
4427 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4428 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4429 			if (ppa == 0)
4430 				return (EAGAIN);
4431 			ill->ill_ppa = --ppa;
4432 		} else {
4433 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4434 			    1, 		/* size */
4435 			    1, 		/* align/quantum */
4436 			    0, 		/* phase */
4437 			    0, 		/* nocross */
4438 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4439 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4440 			    VM_NOSLEEP|VM_FIRSTFIT);
4441 			/*
4442 			 * Most likely the allocation failed because
4443 			 * the requested ppa was in use.
4444 			 */
4445 			if (ppa == 0)
4446 				return (EEXIST);
4447 		}
4448 		return (0);
4449 	}
4450 
4451 	/*
4452 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4453 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4454 	 */
4455 	if (ill->ill_ppa == UINT_MAX) {
4456 		end = UINT_MAX - 1;
4457 		start = 0;
4458 	} else {
4459 		end = start = ill->ill_ppa;
4460 	}
4461 
4462 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4463 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4464 		if (start++ >= end) {
4465 			if (ill->ill_ppa == UINT_MAX)
4466 				return (EAGAIN);
4467 			else
4468 				return (EEXIST);
4469 		}
4470 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4471 	}
4472 	ill->ill_ppa = start;
4473 	return (0);
4474 }
4475 
4476 /*
4477  * Insert ill into the list of configured ill's. Once this function completes,
4478  * the ill is globally visible and is available through lookups. More precisely
4479  * this happens after the caller drops the ill_g_lock.
4480  */
4481 static int
4482 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4483 {
4484 	ill_if_t *ill_interface;
4485 	avl_index_t where = 0;
4486 	int error;
4487 	int name_length;
4488 	int index;
4489 	boolean_t check_length = B_FALSE;
4490 	ip_stack_t	*ipst = ill->ill_ipst;
4491 
4492 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4493 
4494 	name_length = mi_strlen(name) + 1;
4495 
4496 	if (isv6)
4497 		index = IP_V6_G_HEAD;
4498 	else
4499 		index = IP_V4_G_HEAD;
4500 
4501 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4502 	/*
4503 	 * Search for interface type based on name
4504 	 */
4505 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4506 		if ((ill_interface->illif_name_len == name_length) &&
4507 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4508 			break;
4509 		}
4510 		ill_interface = ill_interface->illif_next;
4511 	}
4512 
4513 	/*
4514 	 * Interface type not found, create one.
4515 	 */
4516 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4517 
4518 		ill_g_head_t ghead;
4519 
4520 		/*
4521 		 * allocate ill_if_t structure
4522 		 */
4523 
4524 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4525 		if (ill_interface == NULL) {
4526 			return (ENOMEM);
4527 		}
4528 
4529 
4530 
4531 		(void) strcpy(ill_interface->illif_name, name);
4532 		ill_interface->illif_name_len = name_length;
4533 
4534 		avl_create(&ill_interface->illif_avl_by_ppa,
4535 		    ill_compare_ppa, sizeof (ill_t),
4536 		    offsetof(struct ill_s, ill_avl_byppa));
4537 
4538 		/*
4539 		 * link the structure in the back to maintain order
4540 		 * of configuration for ifconfig output.
4541 		 */
4542 		ghead = ipst->ips_ill_g_heads[index];
4543 		insque(ill_interface, ghead.ill_g_list_tail);
4544 
4545 	}
4546 
4547 	if (ill->ill_ppa == UINT_MAX)
4548 		check_length = B_TRUE;
4549 
4550 	error = ill_alloc_ppa(ill_interface, ill);
4551 	if (error != 0) {
4552 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4553 			ill_delete_interface_type(ill->ill_ifptr);
4554 		return (error);
4555 	}
4556 
4557 	/*
4558 	 * When the ppa is choosen by the system, check that there is
4559 	 * enough space to insert ppa. if a specific ppa was passed in this
4560 	 * check is not required as the interface name passed in will have
4561 	 * the right ppa in it.
4562 	 */
4563 	if (check_length) {
4564 		/*
4565 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4566 		 */
4567 		char buf[sizeof (uint_t) * 3];
4568 
4569 		/*
4570 		 * convert ppa to string to calculate the amount of space
4571 		 * required for it in the name.
4572 		 */
4573 		numtos(ill->ill_ppa, buf);
4574 
4575 		/* Do we have enough space to insert ppa ? */
4576 
4577 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4578 			/* Free ppa and interface type struct */
4579 			if (ill_interface->illif_ppa_arena != NULL) {
4580 				vmem_free(ill_interface->illif_ppa_arena,
4581 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4582 			}
4583 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4584 			    0) {
4585 				ill_delete_interface_type(ill->ill_ifptr);
4586 			}
4587 
4588 			return (EINVAL);
4589 		}
4590 	}
4591 
4592 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4593 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4594 
4595 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4596 	    &where);
4597 	ill->ill_ifptr = ill_interface;
4598 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4599 
4600 	ill_phyint_reinit(ill);
4601 	return (0);
4602 }
4603 
4604 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4605 static boolean_t
4606 ipsq_init(ill_t *ill)
4607 {
4608 	ipsq_t  *ipsq;
4609 
4610 	/* Init the ipsq and impicitly enter as writer */
4611 	ill->ill_phyint->phyint_ipsq =
4612 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4613 	if (ill->ill_phyint->phyint_ipsq == NULL)
4614 		return (B_FALSE);
4615 	ipsq = ill->ill_phyint->phyint_ipsq;
4616 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4617 	ill->ill_phyint->phyint_ipsq_next = NULL;
4618 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4619 	ipsq->ipsq_refs = 1;
4620 	ipsq->ipsq_writer = curthread;
4621 	ipsq->ipsq_reentry_cnt = 1;
4622 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4623 #ifdef DEBUG
4624 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4625 	    IPSQ_STACK_DEPTH);
4626 #endif
4627 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4628 	return (B_TRUE);
4629 }
4630 
4631 /*
4632  * ill_init is called by ip_open when a device control stream is opened.
4633  * It does a few initializations, and shoots a DL_INFO_REQ message down
4634  * to the driver.  The response is later picked up in ip_rput_dlpi and
4635  * used to set up default mechanisms for talking to the driver.  (Always
4636  * called as writer.)
4637  *
4638  * If this function returns error, ip_open will call ip_close which in
4639  * turn will call ill_delete to clean up any memory allocated here that
4640  * is not yet freed.
4641  */
4642 int
4643 ill_init(queue_t *q, ill_t *ill)
4644 {
4645 	int	count;
4646 	dl_info_req_t	*dlir;
4647 	mblk_t	*info_mp;
4648 	uchar_t *frag_ptr;
4649 
4650 	/*
4651 	 * The ill is initialized to zero by mi_alloc*(). In addition
4652 	 * some fields already contain valid values, initialized in
4653 	 * ip_open(), before we reach here.
4654 	 */
4655 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4656 
4657 	ill->ill_rq = q;
4658 	ill->ill_wq = WR(q);
4659 
4660 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4661 	    BPRI_HI);
4662 	if (info_mp == NULL)
4663 		return (ENOMEM);
4664 
4665 	/*
4666 	 * Allocate sufficient space to contain our fragment hash table and
4667 	 * the device name.
4668 	 */
4669 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4670 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4671 	if (frag_ptr == NULL) {
4672 		freemsg(info_mp);
4673 		return (ENOMEM);
4674 	}
4675 	ill->ill_frag_ptr = frag_ptr;
4676 	ill->ill_frag_free_num_pkts = 0;
4677 	ill->ill_last_frag_clean_time = 0;
4678 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4679 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4680 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4681 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4682 		    NULL, MUTEX_DEFAULT, NULL);
4683 	}
4684 
4685 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4686 	if (ill->ill_phyint == NULL) {
4687 		freemsg(info_mp);
4688 		mi_free(frag_ptr);
4689 		return (ENOMEM);
4690 	}
4691 
4692 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4693 	/*
4694 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4695 	 * at this point because of the following reason. If we can't
4696 	 * enter the ipsq at some point and cv_wait, the writer that
4697 	 * wakes us up tries to locate us using the list of all phyints
4698 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4699 	 * If we don't set it now, we risk a missed wakeup.
4700 	 */
4701 	ill->ill_phyint->phyint_illv4 = ill;
4702 	ill->ill_ppa = UINT_MAX;
4703 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4704 
4705 	if (!ipsq_init(ill)) {
4706 		freemsg(info_mp);
4707 		mi_free(frag_ptr);
4708 		mi_free(ill->ill_phyint);
4709 		return (ENOMEM);
4710 	}
4711 
4712 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4713 
4714 
4715 	/* Frag queue limit stuff */
4716 	ill->ill_frag_count = 0;
4717 	ill->ill_ipf_gen = 0;
4718 
4719 	ill->ill_global_timer = INFINITY;
4720 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4721 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4722 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4723 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4724 
4725 	/*
4726 	 * Initialize IPv6 configuration variables.  The IP module is always
4727 	 * opened as an IPv4 module.  Instead tracking down the cases where
4728 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4729 	 * here for convenience, this has no effect until the ill is set to do
4730 	 * IPv6.
4731 	 */
4732 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4733 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4734 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4735 	ill->ill_max_buf = ND_MAX_Q;
4736 	ill->ill_refcnt = 0;
4737 
4738 	/* Send down the Info Request to the driver. */
4739 	info_mp->b_datap->db_type = M_PCPROTO;
4740 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4741 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4742 	dlir->dl_primitive = DL_INFO_REQ;
4743 
4744 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4745 
4746 	qprocson(q);
4747 	ill_dlpi_send(ill, info_mp);
4748 
4749 	return (0);
4750 }
4751 
4752 /*
4753  * ill_dls_info
4754  * creates datalink socket info from the device.
4755  */
4756 int
4757 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4758 {
4759 	size_t	len;
4760 	ill_t	*ill = ipif->ipif_ill;
4761 
4762 	sdl->sdl_family = AF_LINK;
4763 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4764 	sdl->sdl_type = ill->ill_type;
4765 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4766 	len = strlen(sdl->sdl_data);
4767 	ASSERT(len < 256);
4768 	sdl->sdl_nlen = (uchar_t)len;
4769 	sdl->sdl_alen = ill->ill_phys_addr_length;
4770 	sdl->sdl_slen = 0;
4771 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4772 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4773 
4774 	return (sizeof (struct sockaddr_dl));
4775 }
4776 
4777 /*
4778  * ill_xarp_info
4779  * creates xarp info from the device.
4780  */
4781 static int
4782 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4783 {
4784 	sdl->sdl_family = AF_LINK;
4785 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4786 	sdl->sdl_type = ill->ill_type;
4787 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4788 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4789 	sdl->sdl_alen = ill->ill_phys_addr_length;
4790 	sdl->sdl_slen = 0;
4791 	return (sdl->sdl_nlen);
4792 }
4793 
4794 static int
4795 loopback_kstat_update(kstat_t *ksp, int rw)
4796 {
4797 	kstat_named_t *kn;
4798 	netstackid_t	stackid;
4799 	netstack_t	*ns;
4800 	ip_stack_t	*ipst;
4801 
4802 	if (ksp == NULL || ksp->ks_data == NULL)
4803 		return (EIO);
4804 
4805 	if (rw == KSTAT_WRITE)
4806 		return (EACCES);
4807 
4808 	kn = KSTAT_NAMED_PTR(ksp);
4809 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4810 
4811 	ns = netstack_find_by_stackid(stackid);
4812 	if (ns == NULL)
4813 		return (-1);
4814 
4815 	ipst = ns->netstack_ip;
4816 	if (ipst == NULL) {
4817 		netstack_rele(ns);
4818 		return (-1);
4819 	}
4820 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4821 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4822 	netstack_rele(ns);
4823 	return (0);
4824 }
4825 
4826 
4827 /*
4828  * Has ifindex been plumbed already.
4829  * Compares both phyint_ifindex and phyint_group_ifindex.
4830  */
4831 static boolean_t
4832 phyint_exists(uint_t index, ip_stack_t *ipst)
4833 {
4834 	phyint_t *phyi;
4835 
4836 	ASSERT(index != 0);
4837 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4838 	/*
4839 	 * Indexes are stored in the phyint - a common structure
4840 	 * to both IPv4 and IPv6.
4841 	 */
4842 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4843 	for (; phyi != NULL;
4844 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4845 	    phyi, AVL_AFTER)) {
4846 		if (phyi->phyint_ifindex == index ||
4847 		    phyi->phyint_group_ifindex == index)
4848 			return (B_TRUE);
4849 	}
4850 	return (B_FALSE);
4851 }
4852 
4853 /* Pick a unique ifindex */
4854 boolean_t
4855 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4856 {
4857 	uint_t starting_index;
4858 
4859 	if (!ipst->ips_ill_index_wrap) {
4860 		*indexp = ipst->ips_ill_index++;
4861 		if (ipst->ips_ill_index == 0) {
4862 			/* Reached the uint_t limit Next time wrap  */
4863 			ipst->ips_ill_index_wrap = B_TRUE;
4864 		}
4865 		return (B_TRUE);
4866 	}
4867 
4868 	/*
4869 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4870 	 * at this point and don't want to call any function that attempts
4871 	 * to get the lock again.
4872 	 */
4873 	starting_index = ipst->ips_ill_index++;
4874 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4875 		if (ipst->ips_ill_index != 0 &&
4876 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4877 			/* found unused index - use it */
4878 			*indexp = ipst->ips_ill_index;
4879 			return (B_TRUE);
4880 		}
4881 	}
4882 
4883 	/*
4884 	 * all interface indicies are inuse.
4885 	 */
4886 	return (B_FALSE);
4887 }
4888 
4889 /*
4890  * Assign a unique interface index for the phyint.
4891  */
4892 static boolean_t
4893 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4894 {
4895 	ASSERT(phyi->phyint_ifindex == 0);
4896 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4897 }
4898 
4899 /*
4900  * Return a pointer to the ill which matches the supplied name.  Note that
4901  * the ill name length includes the null termination character.  (May be
4902  * called as writer.)
4903  * If do_alloc and the interface is "lo0" it will be automatically created.
4904  * Cannot bump up reference on condemned ills. So dup detect can't be done
4905  * using this func.
4906  */
4907 ill_t *
4908 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4909     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4910     ip_stack_t *ipst)
4911 {
4912 	ill_t	*ill;
4913 	ipif_t	*ipif;
4914 	kstat_named_t	*kn;
4915 	boolean_t isloopback;
4916 	ipsq_t *old_ipsq;
4917 	in6_addr_t ov6addr;
4918 
4919 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4920 
4921 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4922 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4923 	rw_exit(&ipst->ips_ill_g_lock);
4924 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4925 		return (ill);
4926 
4927 	/*
4928 	 * Couldn't find it.  Does this happen to be a lookup for the
4929 	 * loopback device and are we allowed to allocate it?
4930 	 */
4931 	if (!isloopback || !do_alloc)
4932 		return (NULL);
4933 
4934 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4935 
4936 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4937 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4938 		rw_exit(&ipst->ips_ill_g_lock);
4939 		return (ill);
4940 	}
4941 
4942 	/* Create the loopback device on demand */
4943 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4944 	    sizeof (ipif_loopback_name), BPRI_MED));
4945 	if (ill == NULL)
4946 		goto done;
4947 
4948 	*ill = ill_null;
4949 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4950 	ill->ill_ipst = ipst;
4951 	netstack_hold(ipst->ips_netstack);
4952 	/*
4953 	 * For exclusive stacks we set the zoneid to zero
4954 	 * to make IP operate as if in the global zone.
4955 	 */
4956 	ill->ill_zoneid = GLOBAL_ZONEID;
4957 
4958 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4959 	if (ill->ill_phyint == NULL)
4960 		goto done;
4961 
4962 	if (isv6)
4963 		ill->ill_phyint->phyint_illv6 = ill;
4964 	else
4965 		ill->ill_phyint->phyint_illv4 = ill;
4966 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4967 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4968 	/* Add room for tcp+ip headers */
4969 	if (isv6) {
4970 		ill->ill_isv6 = B_TRUE;
4971 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4972 	} else {
4973 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4974 	}
4975 	if (!ill_allocate_mibs(ill))
4976 		goto done;
4977 	ill->ill_max_mtu = ill->ill_max_frag;
4978 	/*
4979 	 * ipif_loopback_name can't be pointed at directly because its used
4980 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4981 	 * from the glist, ill_glist_delete() sets the first character of
4982 	 * ill_name to '\0'.
4983 	 */
4984 	ill->ill_name = (char *)ill + sizeof (*ill);
4985 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4986 	ill->ill_name_length = sizeof (ipif_loopback_name);
4987 	/* Set ill_name_set for ill_phyint_reinit to work properly */
4988 
4989 	ill->ill_global_timer = INFINITY;
4990 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4991 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4992 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4993 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4994 
4995 	/* No resolver here. */
4996 	ill->ill_net_type = IRE_LOOPBACK;
4997 
4998 	/* Initialize the ipsq */
4999 	if (!ipsq_init(ill))
5000 		goto done;
5001 
5002 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5003 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5004 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5005 #ifdef DEBUG
5006 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5007 #endif
5008 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5009 	if (ipif == NULL)
5010 		goto done;
5011 
5012 	ill->ill_flags = ILLF_MULTICAST;
5013 
5014 	ov6addr = ipif->ipif_v6lcl_addr;
5015 	/* Set up default loopback address and mask. */
5016 	if (!isv6) {
5017 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5018 
5019 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5020 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5021 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5022 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5023 		    ipif->ipif_v6subnet);
5024 		ill->ill_flags |= ILLF_IPV4;
5025 	} else {
5026 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5027 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5028 		ipif->ipif_v6net_mask = ipv6_all_ones;
5029 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5030 		    ipif->ipif_v6subnet);
5031 		ill->ill_flags |= ILLF_IPV6;
5032 	}
5033 
5034 	/*
5035 	 * Chain us in at the end of the ill list. hold the ill
5036 	 * before we make it globally visible. 1 for the lookup.
5037 	 */
5038 	ill->ill_refcnt = 0;
5039 	ill_refhold(ill);
5040 
5041 	ill->ill_frag_count = 0;
5042 	ill->ill_frag_free_num_pkts = 0;
5043 	ill->ill_last_frag_clean_time = 0;
5044 
5045 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5046 
5047 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5048 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5049 
5050 	/* Let SCTP know so that it can add this to its list */
5051 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5052 
5053 	/*
5054 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5055 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5056 	 * requires to be after ill_glist_insert() since we need the
5057 	 * ill_index set. Pass on ipv6_loopback as the old address.
5058 	 */
5059 	sctp_update_ipif_addr(ipif, ov6addr);
5060 
5061 	/*
5062 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5063 	 */
5064 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5065 		/* Loopback ills aren't in any IPMP group */
5066 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5067 		ipsq_delete(old_ipsq);
5068 	}
5069 
5070 	/*
5071 	 * Delay this till the ipif is allocated as ipif_allocate
5072 	 * de-references ill_phyint for getting the ifindex. We
5073 	 * can't do this before ipif_allocate because ill_phyint_reinit
5074 	 * -> phyint_assign_ifindex expects ipif to be present.
5075 	 */
5076 	mutex_enter(&ill->ill_phyint->phyint_lock);
5077 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5078 	mutex_exit(&ill->ill_phyint->phyint_lock);
5079 
5080 	if (ipst->ips_loopback_ksp == NULL) {
5081 		/* Export loopback interface statistics */
5082 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5083 		    ipif_loopback_name, "net",
5084 		    KSTAT_TYPE_NAMED, 2, 0,
5085 		    ipst->ips_netstack->netstack_stackid);
5086 		if (ipst->ips_loopback_ksp != NULL) {
5087 			ipst->ips_loopback_ksp->ks_update =
5088 			    loopback_kstat_update;
5089 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5090 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5091 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5092 			ipst->ips_loopback_ksp->ks_private =
5093 			    (void *)(uintptr_t)ipst->ips_netstack->
5094 			    netstack_stackid;
5095 			kstat_install(ipst->ips_loopback_ksp);
5096 		}
5097 	}
5098 
5099 	if (error != NULL)
5100 		*error = 0;
5101 	*did_alloc = B_TRUE;
5102 	rw_exit(&ipst->ips_ill_g_lock);
5103 	return (ill);
5104 done:
5105 	if (ill != NULL) {
5106 		if (ill->ill_phyint != NULL) {
5107 			ipsq_t	*ipsq;
5108 
5109 			ipsq = ill->ill_phyint->phyint_ipsq;
5110 			if (ipsq != NULL) {
5111 				ipsq->ipsq_ipst = NULL;
5112 				kmem_free(ipsq, sizeof (ipsq_t));
5113 			}
5114 			mi_free(ill->ill_phyint);
5115 		}
5116 		ill_free_mib(ill);
5117 		if (ill->ill_ipst != NULL)
5118 			netstack_rele(ill->ill_ipst->ips_netstack);
5119 		mi_free(ill);
5120 	}
5121 	rw_exit(&ipst->ips_ill_g_lock);
5122 	if (error != NULL)
5123 		*error = ENOMEM;
5124 	return (NULL);
5125 }
5126 
5127 /*
5128  * For IPP calls - use the ip_stack_t for global stack.
5129  */
5130 ill_t *
5131 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5132     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5133 {
5134 	ip_stack_t	*ipst;
5135 	ill_t		*ill;
5136 
5137 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5138 	if (ipst == NULL) {
5139 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5140 		return (NULL);
5141 	}
5142 
5143 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5144 	netstack_rele(ipst->ips_netstack);
5145 	return (ill);
5146 }
5147 
5148 /*
5149  * Return a pointer to the ill which matches the index and IP version type.
5150  */
5151 ill_t *
5152 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5153     ipsq_func_t func, int *err, ip_stack_t *ipst)
5154 {
5155 	ill_t	*ill;
5156 	ipsq_t  *ipsq;
5157 	phyint_t *phyi;
5158 
5159 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5160 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5161 
5162 	if (err != NULL)
5163 		*err = 0;
5164 
5165 	/*
5166 	 * Indexes are stored in the phyint - a common structure
5167 	 * to both IPv4 and IPv6.
5168 	 */
5169 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5170 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5171 	    (void *) &index, NULL);
5172 	if (phyi != NULL) {
5173 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5174 		if (ill != NULL) {
5175 			/*
5176 			 * The block comment at the start of ipif_down
5177 			 * explains the use of the macros used below
5178 			 */
5179 			GRAB_CONN_LOCK(q);
5180 			mutex_enter(&ill->ill_lock);
5181 			if (ILL_CAN_LOOKUP(ill)) {
5182 				ill_refhold_locked(ill);
5183 				mutex_exit(&ill->ill_lock);
5184 				RELEASE_CONN_LOCK(q);
5185 				rw_exit(&ipst->ips_ill_g_lock);
5186 				return (ill);
5187 			} else if (ILL_CAN_WAIT(ill, q)) {
5188 				ipsq = ill->ill_phyint->phyint_ipsq;
5189 				mutex_enter(&ipsq->ipsq_lock);
5190 				rw_exit(&ipst->ips_ill_g_lock);
5191 				mutex_exit(&ill->ill_lock);
5192 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5193 				mutex_exit(&ipsq->ipsq_lock);
5194 				RELEASE_CONN_LOCK(q);
5195 				if (err != NULL)
5196 					*err = EINPROGRESS;
5197 				return (NULL);
5198 			}
5199 			RELEASE_CONN_LOCK(q);
5200 			mutex_exit(&ill->ill_lock);
5201 		}
5202 	}
5203 	rw_exit(&ipst->ips_ill_g_lock);
5204 	if (err != NULL)
5205 		*err = ENXIO;
5206 	return (NULL);
5207 }
5208 
5209 /*
5210  * Return the ifindex next in sequence after the passed in ifindex.
5211  * If there is no next ifindex for the given protocol, return 0.
5212  */
5213 uint_t
5214 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5215 {
5216 	phyint_t *phyi;
5217 	phyint_t *phyi_initial;
5218 	uint_t   ifindex;
5219 
5220 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5221 
5222 	if (index == 0) {
5223 		phyi = avl_first(
5224 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5225 	} else {
5226 		phyi = phyi_initial = avl_find(
5227 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5228 		    (void *) &index, NULL);
5229 	}
5230 
5231 	for (; phyi != NULL;
5232 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5233 	    phyi, AVL_AFTER)) {
5234 		/*
5235 		 * If we're not returning the first interface in the tree
5236 		 * and we still haven't moved past the phyint_t that
5237 		 * corresponds to index, avl_walk needs to be called again
5238 		 */
5239 		if (!((index != 0) && (phyi == phyi_initial))) {
5240 			if (isv6) {
5241 				if ((phyi->phyint_illv6) &&
5242 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5243 				    (phyi->phyint_illv6->ill_isv6 == 1))
5244 					break;
5245 			} else {
5246 				if ((phyi->phyint_illv4) &&
5247 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5248 				    (phyi->phyint_illv4->ill_isv6 == 0))
5249 					break;
5250 			}
5251 		}
5252 	}
5253 
5254 	rw_exit(&ipst->ips_ill_g_lock);
5255 
5256 	if (phyi != NULL)
5257 		ifindex = phyi->phyint_ifindex;
5258 	else
5259 		ifindex = 0;
5260 
5261 	return (ifindex);
5262 }
5263 
5264 
5265 /*
5266  * Return the ifindex for the named interface.
5267  * If there is no next ifindex for the interface, return 0.
5268  */
5269 uint_t
5270 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5271 {
5272 	phyint_t	*phyi;
5273 	avl_index_t	where = 0;
5274 	uint_t		ifindex;
5275 
5276 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5277 
5278 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5279 	    name, &where)) == NULL) {
5280 		rw_exit(&ipst->ips_ill_g_lock);
5281 		return (0);
5282 	}
5283 
5284 	ifindex = phyi->phyint_ifindex;
5285 
5286 	rw_exit(&ipst->ips_ill_g_lock);
5287 
5288 	return (ifindex);
5289 }
5290 
5291 
5292 /*
5293  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5294  * that gives a running thread a reference to the ill. This reference must be
5295  * released by the thread when it is done accessing the ill and related
5296  * objects. ill_refcnt can not be used to account for static references
5297  * such as other structures pointing to an ill. Callers must generally
5298  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5299  * or be sure that the ill is not being deleted or changing state before
5300  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5301  * ill won't change any of its critical state such as address, netmask etc.
5302  */
5303 void
5304 ill_refhold(ill_t *ill)
5305 {
5306 	mutex_enter(&ill->ill_lock);
5307 	ill->ill_refcnt++;
5308 	ILL_TRACE_REF(ill);
5309 	mutex_exit(&ill->ill_lock);
5310 }
5311 
5312 void
5313 ill_refhold_locked(ill_t *ill)
5314 {
5315 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5316 	ill->ill_refcnt++;
5317 	ILL_TRACE_REF(ill);
5318 }
5319 
5320 int
5321 ill_check_and_refhold(ill_t *ill)
5322 {
5323 	mutex_enter(&ill->ill_lock);
5324 	if (ILL_CAN_LOOKUP(ill)) {
5325 		ill_refhold_locked(ill);
5326 		mutex_exit(&ill->ill_lock);
5327 		return (0);
5328 	}
5329 	mutex_exit(&ill->ill_lock);
5330 	return (ILL_LOOKUP_FAILED);
5331 }
5332 
5333 /*
5334  * Must not be called while holding any locks. Otherwise if this is
5335  * the last reference to be released, there is a chance of recursive mutex
5336  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5337  * to restart an ioctl.
5338  */
5339 void
5340 ill_refrele(ill_t *ill)
5341 {
5342 	mutex_enter(&ill->ill_lock);
5343 	ASSERT(ill->ill_refcnt != 0);
5344 	ill->ill_refcnt--;
5345 	ILL_UNTRACE_REF(ill);
5346 	if (ill->ill_refcnt != 0) {
5347 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5348 		mutex_exit(&ill->ill_lock);
5349 		return;
5350 	}
5351 
5352 	/* Drops the ill_lock */
5353 	ipif_ill_refrele_tail(ill);
5354 }
5355 
5356 /*
5357  * Obtain a weak reference count on the ill. This reference ensures the
5358  * ill won't be freed, but the ill may change any of its critical state
5359  * such as netmask, address etc. Returns an error if the ill has started
5360  * closing.
5361  */
5362 boolean_t
5363 ill_waiter_inc(ill_t *ill)
5364 {
5365 	mutex_enter(&ill->ill_lock);
5366 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5367 		mutex_exit(&ill->ill_lock);
5368 		return (B_FALSE);
5369 	}
5370 	ill->ill_waiters++;
5371 	mutex_exit(&ill->ill_lock);
5372 	return (B_TRUE);
5373 }
5374 
5375 void
5376 ill_waiter_dcr(ill_t *ill)
5377 {
5378 	mutex_enter(&ill->ill_lock);
5379 	ill->ill_waiters--;
5380 	if (ill->ill_waiters == 0)
5381 		cv_broadcast(&ill->ill_cv);
5382 	mutex_exit(&ill->ill_lock);
5383 }
5384 
5385 /*
5386  * Named Dispatch routine to produce a formatted report on all ILLs.
5387  * This report is accessed by using the ndd utility to "get" ND variable
5388  * "ip_ill_status".
5389  */
5390 /* ARGSUSED */
5391 int
5392 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5393 {
5394 	ill_t		*ill;
5395 	ill_walk_context_t ctx;
5396 	ip_stack_t	*ipst;
5397 
5398 	ipst = CONNQ_TO_IPST(q);
5399 
5400 	(void) mi_mpprintf(mp,
5401 	    "ILL      " MI_COL_HDRPAD_STR
5402 	/*   01234567[89ABCDEF] */
5403 	    "rq       " MI_COL_HDRPAD_STR
5404 	/*   01234567[89ABCDEF] */
5405 	    "wq       " MI_COL_HDRPAD_STR
5406 	/*   01234567[89ABCDEF] */
5407 	    "upcnt mxfrg err name");
5408 	/*   12345 12345 123 xxxxxxxx  */
5409 
5410 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5411 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5412 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5413 		(void) mi_mpprintf(mp,
5414 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5415 		    "%05u %05u %03d %s",
5416 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5417 		    ill->ill_ipif_up_count,
5418 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5419 	}
5420 	rw_exit(&ipst->ips_ill_g_lock);
5421 
5422 	return (0);
5423 }
5424 
5425 /*
5426  * Named Dispatch routine to produce a formatted report on all IPIFs.
5427  * This report is accessed by using the ndd utility to "get" ND variable
5428  * "ip_ipif_status".
5429  */
5430 /* ARGSUSED */
5431 int
5432 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5433 {
5434 	char	buf1[INET6_ADDRSTRLEN];
5435 	char	buf2[INET6_ADDRSTRLEN];
5436 	char	buf3[INET6_ADDRSTRLEN];
5437 	char	buf4[INET6_ADDRSTRLEN];
5438 	char	buf5[INET6_ADDRSTRLEN];
5439 	char	buf6[INET6_ADDRSTRLEN];
5440 	char	buf[LIFNAMSIZ];
5441 	ill_t	*ill;
5442 	ipif_t	*ipif;
5443 	nv_t	*nvp;
5444 	uint64_t flags;
5445 	zoneid_t zoneid;
5446 	ill_walk_context_t ctx;
5447 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5448 
5449 	(void) mi_mpprintf(mp,
5450 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5451 	    "\tlocal address\n"
5452 	    "\tsrc address\n"
5453 	    "\tsubnet\n"
5454 	    "\tmask\n"
5455 	    "\tbroadcast\n"
5456 	    "\tp-p-dst");
5457 
5458 	ASSERT(q->q_next == NULL);
5459 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5460 
5461 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5462 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5463 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5464 		for (ipif = ill->ill_ipif; ipif != NULL;
5465 		    ipif = ipif->ipif_next) {
5466 			if (zoneid != GLOBAL_ZONEID &&
5467 			    zoneid != ipif->ipif_zoneid &&
5468 			    ipif->ipif_zoneid != ALL_ZONES)
5469 				continue;
5470 
5471 			ipif_get_name(ipif, buf, sizeof (buf));
5472 			(void) mi_mpprintf(mp,
5473 			    MI_COL_PTRFMT_STR
5474 			    "%04u %05u %u/%u/%u %s %d",
5475 			    (void *)ipif,
5476 			    ipif->ipif_metric, ipif->ipif_mtu,
5477 			    ipif->ipif_ib_pkt_count,
5478 			    ipif->ipif_ob_pkt_count,
5479 			    ipif->ipif_fo_pkt_count,
5480 			    buf,
5481 			    ipif->ipif_zoneid);
5482 
5483 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5484 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5485 
5486 		/* Tack on text strings for any flags. */
5487 		nvp = ipif_nv_tbl;
5488 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5489 			if (nvp->nv_value & flags)
5490 				(void) mi_mpprintf_nr(mp, " %s",
5491 				    nvp->nv_name);
5492 		}
5493 		(void) mi_mpprintf(mp,
5494 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5495 		    inet_ntop(AF_INET6,
5496 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5497 		    inet_ntop(AF_INET6,
5498 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5499 		    inet_ntop(AF_INET6,
5500 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5501 		    inet_ntop(AF_INET6,
5502 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5503 		    inet_ntop(AF_INET6,
5504 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5505 		    inet_ntop(AF_INET6,
5506 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5507 		}
5508 	}
5509 	rw_exit(&ipst->ips_ill_g_lock);
5510 	return (0);
5511 }
5512 
5513 /*
5514  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5515  * driver.  We construct best guess defaults for lower level information that
5516  * we need.  If an interface is brought up without injection of any overriding
5517  * information from outside, we have to be ready to go with these defaults.
5518  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5519  * we primarely want the dl_provider_style.
5520  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5521  * at which point we assume the other part of the information is valid.
5522  */
5523 void
5524 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5525 {
5526 	uchar_t		*brdcst_addr;
5527 	uint_t		brdcst_addr_length, phys_addr_length;
5528 	t_scalar_t	sap_length;
5529 	dl_info_ack_t	*dlia;
5530 	ip_m_t		*ipm;
5531 	dl_qos_cl_sel1_t *sel1;
5532 
5533 	ASSERT(IAM_WRITER_ILL(ill));
5534 
5535 	/*
5536 	 * Till the ill is fully up ILL_CHANGING will be set and
5537 	 * the ill is not globally visible. So no need for a lock.
5538 	 */
5539 	dlia = (dl_info_ack_t *)mp->b_rptr;
5540 	ill->ill_mactype = dlia->dl_mac_type;
5541 
5542 	ipm = ip_m_lookup(dlia->dl_mac_type);
5543 	if (ipm == NULL) {
5544 		ipm = ip_m_lookup(DL_OTHER);
5545 		ASSERT(ipm != NULL);
5546 	}
5547 	ill->ill_media = ipm;
5548 
5549 	/*
5550 	 * When the new DLPI stuff is ready we'll pull lengths
5551 	 * from dlia.
5552 	 */
5553 	if (dlia->dl_version == DL_VERSION_2) {
5554 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5555 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5556 		    brdcst_addr_length);
5557 		if (brdcst_addr == NULL) {
5558 			brdcst_addr_length = 0;
5559 		}
5560 		sap_length = dlia->dl_sap_length;
5561 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5562 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5563 		    brdcst_addr_length, sap_length, phys_addr_length));
5564 	} else {
5565 		brdcst_addr_length = 6;
5566 		brdcst_addr = ip_six_byte_all_ones;
5567 		sap_length = -2;
5568 		phys_addr_length = brdcst_addr_length;
5569 	}
5570 
5571 	ill->ill_bcast_addr_length = brdcst_addr_length;
5572 	ill->ill_phys_addr_length = phys_addr_length;
5573 	ill->ill_sap_length = sap_length;
5574 	ill->ill_max_frag = dlia->dl_max_sdu;
5575 	ill->ill_max_mtu = ill->ill_max_frag;
5576 
5577 	ill->ill_type = ipm->ip_m_type;
5578 
5579 	if (!ill->ill_dlpi_style_set) {
5580 		if (dlia->dl_provider_style == DL_STYLE2)
5581 			ill->ill_needs_attach = 1;
5582 
5583 		/*
5584 		 * Allocate the first ipif on this ill. We don't delay it
5585 		 * further as ioctl handling assumes atleast one ipif to
5586 		 * be present.
5587 		 *
5588 		 * At this point we don't know whether the ill is v4 or v6.
5589 		 * We will know this whan the SIOCSLIFNAME happens and
5590 		 * the correct value for ill_isv6 will be assigned in
5591 		 * ipif_set_values(). We need to hold the ill lock and
5592 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5593 		 * the wakeup.
5594 		 */
5595 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5596 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5597 		mutex_enter(&ill->ill_lock);
5598 		ASSERT(ill->ill_dlpi_style_set == 0);
5599 		ill->ill_dlpi_style_set = 1;
5600 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5601 		cv_broadcast(&ill->ill_cv);
5602 		mutex_exit(&ill->ill_lock);
5603 		freemsg(mp);
5604 		return;
5605 	}
5606 	ASSERT(ill->ill_ipif != NULL);
5607 	/*
5608 	 * We know whether it is IPv4 or IPv6 now, as this is the
5609 	 * second DL_INFO_ACK we are recieving in response to the
5610 	 * DL_INFO_REQ sent in ipif_set_values.
5611 	 */
5612 	if (ill->ill_isv6)
5613 		ill->ill_sap = IP6_DL_SAP;
5614 	else
5615 		ill->ill_sap = IP_DL_SAP;
5616 	/*
5617 	 * Set ipif_mtu which is used to set the IRE's
5618 	 * ire_max_frag value. The driver could have sent
5619 	 * a different mtu from what it sent last time. No
5620 	 * need to call ipif_mtu_change because IREs have
5621 	 * not yet been created.
5622 	 */
5623 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5624 	/*
5625 	 * Clear all the flags that were set based on ill_bcast_addr_length
5626 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5627 	 * changed now and we need to re-evaluate.
5628 	 */
5629 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5630 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5631 
5632 	/*
5633 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5634 	 * changed now.
5635 	 */
5636 	if (ill->ill_bcast_addr_length == 0) {
5637 		if (ill->ill_resolver_mp != NULL)
5638 			freemsg(ill->ill_resolver_mp);
5639 		if (ill->ill_bcast_mp != NULL)
5640 			freemsg(ill->ill_bcast_mp);
5641 		if (ill->ill_flags & ILLF_XRESOLV)
5642 			ill->ill_net_type = IRE_IF_RESOLVER;
5643 		else
5644 			ill->ill_net_type = IRE_IF_NORESOLVER;
5645 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5646 		    ill->ill_phys_addr_length,
5647 		    ill->ill_sap,
5648 		    ill->ill_sap_length);
5649 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5650 
5651 		if (ill->ill_isv6)
5652 			/*
5653 			 * Note: xresolv interfaces will eventually need NOARP
5654 			 * set here as well, but that will require those
5655 			 * external resolvers to have some knowledge of
5656 			 * that flag and act appropriately. Not to be changed
5657 			 * at present.
5658 			 */
5659 			ill->ill_flags |= ILLF_NONUD;
5660 		else
5661 			ill->ill_flags |= ILLF_NOARP;
5662 
5663 		if (ill->ill_phys_addr_length == 0) {
5664 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5665 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5666 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5667 			} else {
5668 				/* pt-pt supports multicast. */
5669 				ill->ill_flags |= ILLF_MULTICAST;
5670 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5671 			}
5672 		}
5673 	} else {
5674 		ill->ill_net_type = IRE_IF_RESOLVER;
5675 		if (ill->ill_bcast_mp != NULL)
5676 			freemsg(ill->ill_bcast_mp);
5677 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5678 		    ill->ill_bcast_addr_length, ill->ill_sap,
5679 		    ill->ill_sap_length);
5680 		/*
5681 		 * Later detect lack of DLPI driver multicast
5682 		 * capability by catching DL_ENABMULTI errors in
5683 		 * ip_rput_dlpi.
5684 		 */
5685 		ill->ill_flags |= ILLF_MULTICAST;
5686 		if (!ill->ill_isv6)
5687 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5688 	}
5689 	/* By default an interface does not support any CoS marking */
5690 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5691 
5692 	/*
5693 	 * If we get QoS information in DL_INFO_ACK, the device supports
5694 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5695 	 */
5696 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5697 	    dlia->dl_qos_length);
5698 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5699 		ill->ill_flags |= ILLF_COS_ENABLED;
5700 	}
5701 
5702 	/* Clear any previous error indication. */
5703 	ill->ill_error = 0;
5704 	freemsg(mp);
5705 }
5706 
5707 /*
5708  * Perform various checks to verify that an address would make sense as a
5709  * local, remote, or subnet interface address.
5710  */
5711 static boolean_t
5712 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5713 {
5714 	ipaddr_t	net_mask;
5715 
5716 	/*
5717 	 * Don't allow all zeroes, or all ones, but allow
5718 	 * all ones netmask.
5719 	 */
5720 	if ((net_mask = ip_net_mask(addr)) == 0)
5721 		return (B_FALSE);
5722 	/* A given netmask overrides the "guess" netmask */
5723 	if (subnet_mask != 0)
5724 		net_mask = subnet_mask;
5725 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5726 	    (addr == (addr | ~net_mask)))) {
5727 		return (B_FALSE);
5728 	}
5729 
5730 	/*
5731 	 * Even if the netmask is all ones, we do not allow address to be
5732 	 * 255.255.255.255
5733 	 */
5734 	if (addr == INADDR_BROADCAST)
5735 		return (B_FALSE);
5736 
5737 	if (CLASSD(addr))
5738 		return (B_FALSE);
5739 
5740 	return (B_TRUE);
5741 }
5742 
5743 #define	V6_IPIF_LINKLOCAL(p)	\
5744 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5745 
5746 /*
5747  * Compare two given ipifs and check if the second one is better than
5748  * the first one using the order of preference (not taking deprecated
5749  * into acount) specified in ipif_lookup_multicast().
5750  */
5751 static boolean_t
5752 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5753 {
5754 	/* Check the least preferred first. */
5755 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5756 		/* If both ipifs are the same, use the first one. */
5757 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5758 			return (B_FALSE);
5759 		else
5760 			return (B_TRUE);
5761 	}
5762 
5763 	/* For IPv6, check for link local address. */
5764 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5765 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5766 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5767 			/* The second one is equal or less preferred. */
5768 			return (B_FALSE);
5769 		} else {
5770 			return (B_TRUE);
5771 		}
5772 	}
5773 
5774 	/* Then check for point to point interface. */
5775 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5776 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5777 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5778 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5779 			return (B_FALSE);
5780 		} else {
5781 			return (B_TRUE);
5782 		}
5783 	}
5784 
5785 	/* old_ipif is a normal interface, so no need to use the new one. */
5786 	return (B_FALSE);
5787 }
5788 
5789 /*
5790  * Find any non-virtual, not condemned, and up multicast capable interface
5791  * given an IP instance and zoneid.  Order of preference is:
5792  *
5793  * 1. normal
5794  * 1.1 normal, but deprecated
5795  * 2. point to point
5796  * 2.1 point to point, but deprecated
5797  * 3. link local
5798  * 3.1 link local, but deprecated
5799  * 4. loopback.
5800  */
5801 ipif_t *
5802 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5803 {
5804 	ill_t			*ill;
5805 	ill_walk_context_t	ctx;
5806 	ipif_t			*ipif;
5807 	ipif_t			*saved_ipif = NULL;
5808 	ipif_t			*dep_ipif = NULL;
5809 
5810 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5811 	if (isv6)
5812 		ill = ILL_START_WALK_V6(&ctx, ipst);
5813 	else
5814 		ill = ILL_START_WALK_V4(&ctx, ipst);
5815 
5816 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5817 		mutex_enter(&ill->ill_lock);
5818 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5819 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5820 			mutex_exit(&ill->ill_lock);
5821 			continue;
5822 		}
5823 		for (ipif = ill->ill_ipif; ipif != NULL;
5824 		    ipif = ipif->ipif_next) {
5825 			if (zoneid != ipif->ipif_zoneid &&
5826 			    zoneid != ALL_ZONES &&
5827 			    ipif->ipif_zoneid != ALL_ZONES) {
5828 				continue;
5829 			}
5830 			if (!(ipif->ipif_flags & IPIF_UP) ||
5831 			    !IPIF_CAN_LOOKUP(ipif)) {
5832 				continue;
5833 			}
5834 
5835 			/*
5836 			 * Found one candidate.  If it is deprecated,
5837 			 * remember it in dep_ipif.  If it is not deprecated,
5838 			 * remember it in saved_ipif.
5839 			 */
5840 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5841 				if (dep_ipif == NULL) {
5842 					dep_ipif = ipif;
5843 				} else if (ipif_comp_multi(dep_ipif, ipif,
5844 				    isv6)) {
5845 					/*
5846 					 * If the previous dep_ipif does not
5847 					 * belong to the same ill, we've done
5848 					 * a ipif_refhold() on it.  So we need
5849 					 * to release it.
5850 					 */
5851 					if (dep_ipif->ipif_ill != ill)
5852 						ipif_refrele(dep_ipif);
5853 					dep_ipif = ipif;
5854 				}
5855 				continue;
5856 			}
5857 			if (saved_ipif == NULL) {
5858 				saved_ipif = ipif;
5859 			} else {
5860 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5861 					if (saved_ipif->ipif_ill != ill)
5862 						ipif_refrele(saved_ipif);
5863 					saved_ipif = ipif;
5864 				}
5865 			}
5866 		}
5867 		/*
5868 		 * Before going to the next ill, do a ipif_refhold() on the
5869 		 * saved ones.
5870 		 */
5871 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5872 			ipif_refhold_locked(saved_ipif);
5873 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5874 			ipif_refhold_locked(dep_ipif);
5875 		mutex_exit(&ill->ill_lock);
5876 	}
5877 	rw_exit(&ipst->ips_ill_g_lock);
5878 
5879 	/*
5880 	 * If we have only the saved_ipif, return it.  But if we have both
5881 	 * saved_ipif and dep_ipif, check to see which one is better.
5882 	 */
5883 	if (saved_ipif != NULL) {
5884 		if (dep_ipif != NULL) {
5885 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5886 				ipif_refrele(saved_ipif);
5887 				return (dep_ipif);
5888 			} else {
5889 				ipif_refrele(dep_ipif);
5890 				return (saved_ipif);
5891 			}
5892 		}
5893 		return (saved_ipif);
5894 	} else {
5895 		return (dep_ipif);
5896 	}
5897 }
5898 
5899 /*
5900  * This function is called when an application does not specify an interface
5901  * to be used for multicast traffic (joining a group/sending data).  It
5902  * calls ire_lookup_multi() to look for an interface route for the
5903  * specified multicast group.  Doing this allows the administrator to add
5904  * prefix routes for multicast to indicate which interface to be used for
5905  * multicast traffic in the above scenario.  The route could be for all
5906  * multicast (224.0/4), for a single multicast group (a /32 route) or
5907  * anything in between.  If there is no such multicast route, we just find
5908  * any multicast capable interface and return it.  The returned ipif
5909  * is refhold'ed.
5910  */
5911 ipif_t *
5912 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5913 {
5914 	ire_t			*ire;
5915 	ipif_t			*ipif;
5916 
5917 	ire = ire_lookup_multi(group, zoneid, ipst);
5918 	if (ire != NULL) {
5919 		ipif = ire->ire_ipif;
5920 		ipif_refhold(ipif);
5921 		ire_refrele(ire);
5922 		return (ipif);
5923 	}
5924 
5925 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5926 }
5927 
5928 /*
5929  * Look for an ipif with the specified interface address and destination.
5930  * The destination address is used only for matching point-to-point interfaces.
5931  */
5932 ipif_t *
5933 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5934     ipsq_func_t func, int *error, ip_stack_t *ipst)
5935 {
5936 	ipif_t	*ipif;
5937 	ill_t	*ill;
5938 	ill_walk_context_t ctx;
5939 	ipsq_t	*ipsq;
5940 
5941 	if (error != NULL)
5942 		*error = 0;
5943 
5944 	/*
5945 	 * First match all the point-to-point interfaces
5946 	 * before looking at non-point-to-point interfaces.
5947 	 * This is done to avoid returning non-point-to-point
5948 	 * ipif instead of unnumbered point-to-point ipif.
5949 	 */
5950 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5951 	ill = ILL_START_WALK_V4(&ctx, ipst);
5952 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5953 		GRAB_CONN_LOCK(q);
5954 		mutex_enter(&ill->ill_lock);
5955 		for (ipif = ill->ill_ipif; ipif != NULL;
5956 		    ipif = ipif->ipif_next) {
5957 			/* Allow the ipif to be down */
5958 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5959 			    (ipif->ipif_lcl_addr == if_addr) &&
5960 			    (ipif->ipif_pp_dst_addr == dst)) {
5961 				/*
5962 				 * The block comment at the start of ipif_down
5963 				 * explains the use of the macros used below
5964 				 */
5965 				if (IPIF_CAN_LOOKUP(ipif)) {
5966 					ipif_refhold_locked(ipif);
5967 					mutex_exit(&ill->ill_lock);
5968 					RELEASE_CONN_LOCK(q);
5969 					rw_exit(&ipst->ips_ill_g_lock);
5970 					return (ipif);
5971 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5972 					ipsq = ill->ill_phyint->phyint_ipsq;
5973 					mutex_enter(&ipsq->ipsq_lock);
5974 					mutex_exit(&ill->ill_lock);
5975 					rw_exit(&ipst->ips_ill_g_lock);
5976 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5977 					    ill);
5978 					mutex_exit(&ipsq->ipsq_lock);
5979 					RELEASE_CONN_LOCK(q);
5980 					if (error != NULL)
5981 						*error = EINPROGRESS;
5982 					return (NULL);
5983 				}
5984 			}
5985 		}
5986 		mutex_exit(&ill->ill_lock);
5987 		RELEASE_CONN_LOCK(q);
5988 	}
5989 	rw_exit(&ipst->ips_ill_g_lock);
5990 
5991 	/* lookup the ipif based on interface address */
5992 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5993 	    ipst);
5994 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5995 	return (ipif);
5996 }
5997 
5998 /*
5999  * Look for an ipif with the specified address. For point-point links
6000  * we look for matches on either the destination address and the local
6001  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6002  * is set.
6003  * Matches on a specific ill if match_ill is set.
6004  */
6005 ipif_t *
6006 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6007     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6008 {
6009 	ipif_t  *ipif;
6010 	ill_t   *ill;
6011 	boolean_t ptp = B_FALSE;
6012 	ipsq_t	*ipsq;
6013 	ill_walk_context_t	ctx;
6014 
6015 	if (error != NULL)
6016 		*error = 0;
6017 
6018 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6019 	/*
6020 	 * Repeat twice, first based on local addresses and
6021 	 * next time for pointopoint.
6022 	 */
6023 repeat:
6024 	ill = ILL_START_WALK_V4(&ctx, ipst);
6025 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6026 		if (match_ill != NULL && ill != match_ill) {
6027 			continue;
6028 		}
6029 		GRAB_CONN_LOCK(q);
6030 		mutex_enter(&ill->ill_lock);
6031 		for (ipif = ill->ill_ipif; ipif != NULL;
6032 		    ipif = ipif->ipif_next) {
6033 			if (zoneid != ALL_ZONES &&
6034 			    zoneid != ipif->ipif_zoneid &&
6035 			    ipif->ipif_zoneid != ALL_ZONES)
6036 				continue;
6037 			/* Allow the ipif to be down */
6038 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6039 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6040 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6041 			    (ipif->ipif_pp_dst_addr == addr))) {
6042 				/*
6043 				 * The block comment at the start of ipif_down
6044 				 * explains the use of the macros used below
6045 				 */
6046 				if (IPIF_CAN_LOOKUP(ipif)) {
6047 					ipif_refhold_locked(ipif);
6048 					mutex_exit(&ill->ill_lock);
6049 					RELEASE_CONN_LOCK(q);
6050 					rw_exit(&ipst->ips_ill_g_lock);
6051 					return (ipif);
6052 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6053 					ipsq = ill->ill_phyint->phyint_ipsq;
6054 					mutex_enter(&ipsq->ipsq_lock);
6055 					mutex_exit(&ill->ill_lock);
6056 					rw_exit(&ipst->ips_ill_g_lock);
6057 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6058 					    ill);
6059 					mutex_exit(&ipsq->ipsq_lock);
6060 					RELEASE_CONN_LOCK(q);
6061 					if (error != NULL)
6062 						*error = EINPROGRESS;
6063 					return (NULL);
6064 				}
6065 			}
6066 		}
6067 		mutex_exit(&ill->ill_lock);
6068 		RELEASE_CONN_LOCK(q);
6069 	}
6070 
6071 	/* If we already did the ptp case, then we are done */
6072 	if (ptp) {
6073 		rw_exit(&ipst->ips_ill_g_lock);
6074 		if (error != NULL)
6075 			*error = ENXIO;
6076 		return (NULL);
6077 	}
6078 	ptp = B_TRUE;
6079 	goto repeat;
6080 }
6081 
6082 /*
6083  * Look for an ipif with the specified address. For point-point links
6084  * we look for matches on either the destination address and the local
6085  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6086  * is set.
6087  * Matches on a specific ill if match_ill is set.
6088  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6089  */
6090 zoneid_t
6091 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6092 {
6093 	zoneid_t zoneid;
6094 	ipif_t  *ipif;
6095 	ill_t   *ill;
6096 	boolean_t ptp = B_FALSE;
6097 	ill_walk_context_t	ctx;
6098 
6099 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6100 	/*
6101 	 * Repeat twice, first based on local addresses and
6102 	 * next time for pointopoint.
6103 	 */
6104 repeat:
6105 	ill = ILL_START_WALK_V4(&ctx, ipst);
6106 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6107 		if (match_ill != NULL && ill != match_ill) {
6108 			continue;
6109 		}
6110 		mutex_enter(&ill->ill_lock);
6111 		for (ipif = ill->ill_ipif; ipif != NULL;
6112 		    ipif = ipif->ipif_next) {
6113 			/* Allow the ipif to be down */
6114 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6115 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6116 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6117 			    (ipif->ipif_pp_dst_addr == addr)) &&
6118 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6119 				zoneid = ipif->ipif_zoneid;
6120 				mutex_exit(&ill->ill_lock);
6121 				rw_exit(&ipst->ips_ill_g_lock);
6122 				/*
6123 				 * If ipif_zoneid was ALL_ZONES then we have
6124 				 * a trusted extensions shared IP address.
6125 				 * In that case GLOBAL_ZONEID works to send.
6126 				 */
6127 				if (zoneid == ALL_ZONES)
6128 					zoneid = GLOBAL_ZONEID;
6129 				return (zoneid);
6130 			}
6131 		}
6132 		mutex_exit(&ill->ill_lock);
6133 	}
6134 
6135 	/* If we already did the ptp case, then we are done */
6136 	if (ptp) {
6137 		rw_exit(&ipst->ips_ill_g_lock);
6138 		return (ALL_ZONES);
6139 	}
6140 	ptp = B_TRUE;
6141 	goto repeat;
6142 }
6143 
6144 /*
6145  * Look for an ipif that matches the specified remote address i.e. the
6146  * ipif that would receive the specified packet.
6147  * First look for directly connected interfaces and then do a recursive
6148  * IRE lookup and pick the first ipif corresponding to the source address in the
6149  * ire.
6150  * Returns: held ipif
6151  */
6152 ipif_t *
6153 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6154 {
6155 	ipif_t	*ipif;
6156 	ire_t	*ire;
6157 	ip_stack_t	*ipst = ill->ill_ipst;
6158 
6159 	ASSERT(!ill->ill_isv6);
6160 
6161 	/*
6162 	 * Someone could be changing this ipif currently or change it
6163 	 * after we return this. Thus  a few packets could use the old
6164 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6165 	 * will atomically be updated or cleaned up with the new value
6166 	 * Thus we don't need a lock to check the flags or other attrs below.
6167 	 */
6168 	mutex_enter(&ill->ill_lock);
6169 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6170 		if (!IPIF_CAN_LOOKUP(ipif))
6171 			continue;
6172 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6173 		    ipif->ipif_zoneid != ALL_ZONES)
6174 			continue;
6175 		/* Allow the ipif to be down */
6176 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6177 			if ((ipif->ipif_pp_dst_addr == addr) ||
6178 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6179 			    ipif->ipif_lcl_addr == addr)) {
6180 				ipif_refhold_locked(ipif);
6181 				mutex_exit(&ill->ill_lock);
6182 				return (ipif);
6183 			}
6184 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6185 			ipif_refhold_locked(ipif);
6186 			mutex_exit(&ill->ill_lock);
6187 			return (ipif);
6188 		}
6189 	}
6190 	mutex_exit(&ill->ill_lock);
6191 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6192 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6193 	if (ire != NULL) {
6194 		/*
6195 		 * The callers of this function wants to know the
6196 		 * interface on which they have to send the replies
6197 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6198 		 * derived from different ills, we really don't care
6199 		 * what we return here.
6200 		 */
6201 		ipif = ire->ire_ipif;
6202 		if (ipif != NULL) {
6203 			ipif_refhold(ipif);
6204 			ire_refrele(ire);
6205 			return (ipif);
6206 		}
6207 		ire_refrele(ire);
6208 	}
6209 	/* Pick the first interface */
6210 	ipif = ipif_get_next_ipif(NULL, ill);
6211 	return (ipif);
6212 }
6213 
6214 /*
6215  * This func does not prevent refcnt from increasing. But if
6216  * the caller has taken steps to that effect, then this func
6217  * can be used to determine whether the ill has become quiescent
6218  */
6219 boolean_t
6220 ill_is_quiescent(ill_t *ill)
6221 {
6222 	ipif_t	*ipif;
6223 
6224 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6225 
6226 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6227 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6228 			return (B_FALSE);
6229 		}
6230 	}
6231 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6232 	    ill->ill_nce_cnt != 0) {
6233 		return (B_FALSE);
6234 	}
6235 	return (B_TRUE);
6236 }
6237 
6238 /*
6239  * This func does not prevent refcnt from increasing. But if
6240  * the caller has taken steps to that effect, then this func
6241  * can be used to determine whether the ipif has become quiescent
6242  */
6243 static boolean_t
6244 ipif_is_quiescent(ipif_t *ipif)
6245 {
6246 	ill_t *ill;
6247 
6248 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6249 
6250 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6251 		return (B_FALSE);
6252 	}
6253 
6254 	ill = ipif->ipif_ill;
6255 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6256 	    ill->ill_logical_down) {
6257 		return (B_TRUE);
6258 	}
6259 
6260 	/* This is the last ipif going down or being deleted on this ill */
6261 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6262 		return (B_FALSE);
6263 	}
6264 
6265 	return (B_TRUE);
6266 }
6267 
6268 /*
6269  * This func does not prevent refcnt from increasing. But if
6270  * the caller has taken steps to that effect, then this func
6271  * can be used to determine whether the ipifs marked with IPIF_MOVING
6272  * have become quiescent and can be moved in a failover/failback.
6273  */
6274 static ipif_t *
6275 ill_quiescent_to_move(ill_t *ill)
6276 {
6277 	ipif_t  *ipif;
6278 
6279 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6280 
6281 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6282 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6283 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6284 				return (ipif);
6285 			}
6286 		}
6287 	}
6288 	return (NULL);
6289 }
6290 
6291 /*
6292  * The ipif/ill/ire has been refreled. Do the tail processing.
6293  * Determine if the ipif or ill in question has become quiescent and if so
6294  * wakeup close and/or restart any queued pending ioctl that is waiting
6295  * for the ipif_down (or ill_down)
6296  */
6297 void
6298 ipif_ill_refrele_tail(ill_t *ill)
6299 {
6300 	mblk_t	*mp;
6301 	conn_t	*connp;
6302 	ipsq_t	*ipsq;
6303 	ipif_t	*ipif;
6304 	dl_notify_ind_t *dlindp;
6305 
6306 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6307 
6308 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6309 	    ill_is_quiescent(ill)) {
6310 		/* ill_close may be waiting */
6311 		cv_broadcast(&ill->ill_cv);
6312 	}
6313 
6314 	/* ipsq can't change because ill_lock  is held */
6315 	ipsq = ill->ill_phyint->phyint_ipsq;
6316 	if (ipsq->ipsq_waitfor == 0) {
6317 		/* Not waiting for anything, just return. */
6318 		mutex_exit(&ill->ill_lock);
6319 		return;
6320 	}
6321 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6322 	    ipsq->ipsq_pending_ipif != NULL);
6323 	/*
6324 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6325 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6326 	 * be zero for restarting an ioctl that ends up downing the ill.
6327 	 */
6328 	ipif = ipsq->ipsq_pending_ipif;
6329 	if (ipif->ipif_ill != ill) {
6330 		/* The ioctl is pending on some other ill. */
6331 		mutex_exit(&ill->ill_lock);
6332 		return;
6333 	}
6334 
6335 	switch (ipsq->ipsq_waitfor) {
6336 	case IPIF_DOWN:
6337 	case IPIF_FREE:
6338 		if (!ipif_is_quiescent(ipif)) {
6339 			mutex_exit(&ill->ill_lock);
6340 			return;
6341 		}
6342 		break;
6343 
6344 	case ILL_DOWN:
6345 	case ILL_FREE:
6346 		/*
6347 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6348 		 * waits synchronously in ip_close, and no message is queued in
6349 		 * ipsq_pending_mp at all in this case
6350 		 */
6351 		if (!ill_is_quiescent(ill)) {
6352 			mutex_exit(&ill->ill_lock);
6353 			return;
6354 		}
6355 
6356 		break;
6357 
6358 	case ILL_MOVE_OK:
6359 		if (ill_quiescent_to_move(ill) != NULL) {
6360 			mutex_exit(&ill->ill_lock);
6361 			return;
6362 		}
6363 
6364 		break;
6365 	default:
6366 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6367 		    (void *)ipsq, ipsq->ipsq_waitfor);
6368 	}
6369 
6370 	/*
6371 	 * Incr refcnt for the qwriter_ip call below which
6372 	 * does a refrele
6373 	 */
6374 	ill_refhold_locked(ill);
6375 	mutex_exit(&ill->ill_lock);
6376 
6377 	mp = ipsq_pending_mp_get(ipsq, &connp);
6378 	ASSERT(mp != NULL);
6379 
6380 	/*
6381 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6382 	 * we can only get here when the current operation decides it
6383 	 * it needs to quiesce via ipsq_pending_mp_add().
6384 	 */
6385 	switch (mp->b_datap->db_type) {
6386 	case M_PCPROTO:
6387 	case M_PROTO:
6388 		/*
6389 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6390 		 */
6391 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6392 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6393 
6394 		switch (dlindp->dl_notification) {
6395 		case DL_NOTE_PHYS_ADDR:
6396 			qwriter_ip(ill, ill->ill_rq, mp,
6397 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6398 			return;
6399 		default:
6400 			ASSERT(0);
6401 		}
6402 		break;
6403 
6404 	case M_ERROR:
6405 	case M_HANGUP:
6406 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6407 		    B_TRUE);
6408 		return;
6409 
6410 	case M_IOCTL:
6411 	case M_IOCDATA:
6412 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6413 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6414 		return;
6415 
6416 	default:
6417 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6418 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6419 	}
6420 }
6421 
6422 #ifdef DEBUG
6423 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6424 static void
6425 th_trace_rrecord(th_trace_t *th_trace)
6426 {
6427 	tr_buf_t *tr_buf;
6428 	uint_t lastref;
6429 
6430 	lastref = th_trace->th_trace_lastref;
6431 	lastref++;
6432 	if (lastref == TR_BUF_MAX)
6433 		lastref = 0;
6434 	th_trace->th_trace_lastref = lastref;
6435 	tr_buf = &th_trace->th_trbuf[lastref];
6436 	tr_buf->tr_time = lbolt;
6437 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6438 }
6439 
6440 static void
6441 th_trace_free(void *value)
6442 {
6443 	th_trace_t *th_trace = value;
6444 
6445 	ASSERT(th_trace->th_refcnt == 0);
6446 	kmem_free(th_trace, sizeof (*th_trace));
6447 }
6448 
6449 /*
6450  * Find or create the per-thread hash table used to track object references.
6451  * The ipst argument is NULL if we shouldn't allocate.
6452  *
6453  * Accesses per-thread data, so there's no need to lock here.
6454  */
6455 static mod_hash_t *
6456 th_trace_gethash(ip_stack_t *ipst)
6457 {
6458 	th_hash_t *thh;
6459 
6460 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6461 		mod_hash_t *mh;
6462 		char name[256];
6463 		size_t objsize, rshift;
6464 		int retv;
6465 
6466 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6467 			return (NULL);
6468 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6469 
6470 		/*
6471 		 * We use mod_hash_create_extended here rather than the more
6472 		 * obvious mod_hash_create_ptrhash because the latter has a
6473 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6474 		 * block.
6475 		 */
6476 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6477 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6478 		rshift = highbit(objsize);
6479 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6480 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6481 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6482 		if (mh == NULL) {
6483 			kmem_free(thh, sizeof (*thh));
6484 			return (NULL);
6485 		}
6486 		thh->thh_hash = mh;
6487 		thh->thh_ipst = ipst;
6488 		/*
6489 		 * We trace ills, ipifs, ires, and nces.  All of these are
6490 		 * per-IP-stack, so the lock on the thread list is as well.
6491 		 */
6492 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6493 		list_insert_tail(&ip_thread_list, thh);
6494 		rw_exit(&ip_thread_rwlock);
6495 		retv = tsd_set(ip_thread_data, thh);
6496 		ASSERT(retv == 0);
6497 	}
6498 	return (thh != NULL ? thh->thh_hash : NULL);
6499 }
6500 
6501 boolean_t
6502 th_trace_ref(const void *obj, ip_stack_t *ipst)
6503 {
6504 	th_trace_t *th_trace;
6505 	mod_hash_t *mh;
6506 	mod_hash_val_t val;
6507 
6508 	if ((mh = th_trace_gethash(ipst)) == NULL)
6509 		return (B_FALSE);
6510 
6511 	/*
6512 	 * Attempt to locate the trace buffer for this obj and thread.
6513 	 * If it does not exist, then allocate a new trace buffer and
6514 	 * insert into the hash.
6515 	 */
6516 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6517 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6518 		if (th_trace == NULL)
6519 			return (B_FALSE);
6520 
6521 		th_trace->th_id = curthread;
6522 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6523 		    (mod_hash_val_t)th_trace) != 0) {
6524 			kmem_free(th_trace, sizeof (th_trace_t));
6525 			return (B_FALSE);
6526 		}
6527 	} else {
6528 		th_trace = (th_trace_t *)val;
6529 	}
6530 
6531 	ASSERT(th_trace->th_refcnt >= 0 &&
6532 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6533 
6534 	th_trace->th_refcnt++;
6535 	th_trace_rrecord(th_trace);
6536 	return (B_TRUE);
6537 }
6538 
6539 /*
6540  * For the purpose of tracing a reference release, we assume that global
6541  * tracing is always on and that the same thread initiated the reference hold
6542  * is releasing.
6543  */
6544 void
6545 th_trace_unref(const void *obj)
6546 {
6547 	int retv;
6548 	mod_hash_t *mh;
6549 	th_trace_t *th_trace;
6550 	mod_hash_val_t val;
6551 
6552 	mh = th_trace_gethash(NULL);
6553 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6554 	ASSERT(retv == 0);
6555 	th_trace = (th_trace_t *)val;
6556 
6557 	ASSERT(th_trace->th_refcnt > 0);
6558 	th_trace->th_refcnt--;
6559 	th_trace_rrecord(th_trace);
6560 }
6561 
6562 /*
6563  * If tracing has been disabled, then we assume that the reference counts are
6564  * now useless, and we clear them out before destroying the entries.
6565  */
6566 void
6567 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6568 {
6569 	th_hash_t	*thh;
6570 	mod_hash_t	*mh;
6571 	mod_hash_val_t	val;
6572 	th_trace_t	*th_trace;
6573 	int		retv;
6574 
6575 	rw_enter(&ip_thread_rwlock, RW_READER);
6576 	for (thh = list_head(&ip_thread_list); thh != NULL;
6577 	    thh = list_next(&ip_thread_list, thh)) {
6578 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6579 		    &val) == 0) {
6580 			th_trace = (th_trace_t *)val;
6581 			if (trace_disable)
6582 				th_trace->th_refcnt = 0;
6583 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6584 			ASSERT(retv == 0);
6585 		}
6586 	}
6587 	rw_exit(&ip_thread_rwlock);
6588 }
6589 
6590 void
6591 ipif_trace_ref(ipif_t *ipif)
6592 {
6593 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6594 
6595 	if (ipif->ipif_trace_disable)
6596 		return;
6597 
6598 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6599 		ipif->ipif_trace_disable = B_TRUE;
6600 		ipif_trace_cleanup(ipif);
6601 	}
6602 }
6603 
6604 void
6605 ipif_untrace_ref(ipif_t *ipif)
6606 {
6607 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6608 
6609 	if (!ipif->ipif_trace_disable)
6610 		th_trace_unref(ipif);
6611 }
6612 
6613 void
6614 ill_trace_ref(ill_t *ill)
6615 {
6616 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6617 
6618 	if (ill->ill_trace_disable)
6619 		return;
6620 
6621 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6622 		ill->ill_trace_disable = B_TRUE;
6623 		ill_trace_cleanup(ill);
6624 	}
6625 }
6626 
6627 void
6628 ill_untrace_ref(ill_t *ill)
6629 {
6630 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6631 
6632 	if (!ill->ill_trace_disable)
6633 		th_trace_unref(ill);
6634 }
6635 
6636 /*
6637  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6638  * failure, ipif_trace_disable is set.
6639  */
6640 static void
6641 ipif_trace_cleanup(const ipif_t *ipif)
6642 {
6643 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6644 }
6645 
6646 /*
6647  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6648  * failure, ill_trace_disable is set.
6649  */
6650 static void
6651 ill_trace_cleanup(const ill_t *ill)
6652 {
6653 	th_trace_cleanup(ill, ill->ill_trace_disable);
6654 }
6655 #endif /* DEBUG */
6656 
6657 void
6658 ipif_refhold_locked(ipif_t *ipif)
6659 {
6660 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6661 	ipif->ipif_refcnt++;
6662 	IPIF_TRACE_REF(ipif);
6663 }
6664 
6665 void
6666 ipif_refhold(ipif_t *ipif)
6667 {
6668 	ill_t	*ill;
6669 
6670 	ill = ipif->ipif_ill;
6671 	mutex_enter(&ill->ill_lock);
6672 	ipif->ipif_refcnt++;
6673 	IPIF_TRACE_REF(ipif);
6674 	mutex_exit(&ill->ill_lock);
6675 }
6676 
6677 /*
6678  * Must not be called while holding any locks. Otherwise if this is
6679  * the last reference to be released there is a chance of recursive mutex
6680  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6681  * to restart an ioctl.
6682  */
6683 void
6684 ipif_refrele(ipif_t *ipif)
6685 {
6686 	ill_t	*ill;
6687 
6688 	ill = ipif->ipif_ill;
6689 
6690 	mutex_enter(&ill->ill_lock);
6691 	ASSERT(ipif->ipif_refcnt != 0);
6692 	ipif->ipif_refcnt--;
6693 	IPIF_UNTRACE_REF(ipif);
6694 	if (ipif->ipif_refcnt != 0) {
6695 		mutex_exit(&ill->ill_lock);
6696 		return;
6697 	}
6698 
6699 	/* Drops the ill_lock */
6700 	ipif_ill_refrele_tail(ill);
6701 }
6702 
6703 ipif_t *
6704 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6705 {
6706 	ipif_t	*ipif;
6707 
6708 	mutex_enter(&ill->ill_lock);
6709 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6710 	    ipif != NULL; ipif = ipif->ipif_next) {
6711 		if (!IPIF_CAN_LOOKUP(ipif))
6712 			continue;
6713 		ipif_refhold_locked(ipif);
6714 		mutex_exit(&ill->ill_lock);
6715 		return (ipif);
6716 	}
6717 	mutex_exit(&ill->ill_lock);
6718 	return (NULL);
6719 }
6720 
6721 /*
6722  * TODO: make this table extendible at run time
6723  * Return a pointer to the mac type info for 'mac_type'
6724  */
6725 static ip_m_t *
6726 ip_m_lookup(t_uscalar_t mac_type)
6727 {
6728 	ip_m_t	*ipm;
6729 
6730 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6731 		if (ipm->ip_m_mac_type == mac_type)
6732 			return (ipm);
6733 	return (NULL);
6734 }
6735 
6736 /*
6737  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6738  * ipif_arg is passed in to associate it with the correct interface.
6739  * We may need to restart this operation if the ipif cannot be looked up
6740  * due to an exclusive operation that is currently in progress. The restart
6741  * entry point is specified by 'func'
6742  */
6743 int
6744 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6745     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6746     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6747     struct rtsa_s *sp, ip_stack_t *ipst)
6748 {
6749 	ire_t	*ire;
6750 	ire_t	*gw_ire = NULL;
6751 	ipif_t	*ipif = NULL;
6752 	boolean_t ipif_refheld = B_FALSE;
6753 	uint_t	type;
6754 	int	match_flags = MATCH_IRE_TYPE;
6755 	int	error;
6756 	tsol_gc_t *gc = NULL;
6757 	tsol_gcgrp_t *gcgrp = NULL;
6758 	boolean_t gcgrp_xtraref = B_FALSE;
6759 
6760 	ip1dbg(("ip_rt_add:"));
6761 
6762 	if (ire_arg != NULL)
6763 		*ire_arg = NULL;
6764 
6765 	/*
6766 	 * If this is the case of RTF_HOST being set, then we set the netmask
6767 	 * to all ones (regardless if one was supplied).
6768 	 */
6769 	if (flags & RTF_HOST)
6770 		mask = IP_HOST_MASK;
6771 
6772 	/*
6773 	 * Prevent routes with a zero gateway from being created (since
6774 	 * interfaces can currently be plumbed and brought up no assigned
6775 	 * address).
6776 	 */
6777 	if (gw_addr == 0)
6778 		return (ENETUNREACH);
6779 	/*
6780 	 * Get the ipif, if any, corresponding to the gw_addr
6781 	 */
6782 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6783 	    ipst);
6784 	if (ipif != NULL) {
6785 		if (IS_VNI(ipif->ipif_ill)) {
6786 			ipif_refrele(ipif);
6787 			return (EINVAL);
6788 		}
6789 		ipif_refheld = B_TRUE;
6790 	} else if (error == EINPROGRESS) {
6791 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6792 		return (EINPROGRESS);
6793 	} else {
6794 		error = 0;
6795 	}
6796 
6797 	if (ipif != NULL) {
6798 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6799 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6800 	} else {
6801 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6802 	}
6803 
6804 	/*
6805 	 * GateD will attempt to create routes with a loopback interface
6806 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6807 	 * these routes to be added, but create them as interface routes
6808 	 * since the gateway is an interface address.
6809 	 */
6810 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6811 		flags &= ~RTF_GATEWAY;
6812 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6813 		    mask == IP_HOST_MASK) {
6814 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6815 			    ALL_ZONES, NULL, match_flags, ipst);
6816 			if (ire != NULL) {
6817 				ire_refrele(ire);
6818 				if (ipif_refheld)
6819 					ipif_refrele(ipif);
6820 				return (EEXIST);
6821 			}
6822 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6823 			    "for 0x%x\n", (void *)ipif,
6824 			    ipif->ipif_ire_type,
6825 			    ntohl(ipif->ipif_lcl_addr)));
6826 			ire = ire_create(
6827 			    (uchar_t *)&dst_addr,	/* dest address */
6828 			    (uchar_t *)&mask,		/* mask */
6829 			    (uchar_t *)&ipif->ipif_src_addr,
6830 			    NULL,			/* no gateway */
6831 			    &ipif->ipif_mtu,
6832 			    NULL,
6833 			    ipif->ipif_rq,		/* recv-from queue */
6834 			    NULL,			/* no send-to queue */
6835 			    ipif->ipif_ire_type,	/* LOOPBACK */
6836 			    ipif,
6837 			    0,
6838 			    0,
6839 			    0,
6840 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6841 			    RTF_PRIVATE : 0,
6842 			    &ire_uinfo_null,
6843 			    NULL,
6844 			    NULL,
6845 			    ipst);
6846 
6847 			if (ire == NULL) {
6848 				if (ipif_refheld)
6849 					ipif_refrele(ipif);
6850 				return (ENOMEM);
6851 			}
6852 			error = ire_add(&ire, q, mp, func, B_FALSE);
6853 			if (error == 0)
6854 				goto save_ire;
6855 			if (ipif_refheld)
6856 				ipif_refrele(ipif);
6857 			return (error);
6858 
6859 		}
6860 	}
6861 
6862 	/*
6863 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6864 	 * and the gateway address provided is one of the system's interface
6865 	 * addresses.  By using the routing socket interface and supplying an
6866 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6867 	 * specifying an interface route to be created is available which uses
6868 	 * the interface index that specifies the outgoing interface rather than
6869 	 * the address of an outgoing interface (which may not be able to
6870 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6871 	 * flag, routes can be specified which not only specify the next-hop to
6872 	 * be used when routing to a certain prefix, but also which outgoing
6873 	 * interface should be used.
6874 	 *
6875 	 * Previously, interfaces would have unique addresses assigned to them
6876 	 * and so the address assigned to a particular interface could be used
6877 	 * to identify a particular interface.  One exception to this was the
6878 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6879 	 *
6880 	 * With the advent of IPv6 and its link-local addresses, this
6881 	 * restriction was relaxed and interfaces could share addresses between
6882 	 * themselves.  In fact, typically all of the link-local interfaces on
6883 	 * an IPv6 node or router will have the same link-local address.  In
6884 	 * order to differentiate between these interfaces, the use of an
6885 	 * interface index is necessary and this index can be carried inside a
6886 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6887 	 * of using the interface index, however, is that all of the ipif's that
6888 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6889 	 * cannot be used to differentiate between ipif's (or logical
6890 	 * interfaces) that belong to the same ill (physical interface).
6891 	 *
6892 	 * For example, in the following case involving IPv4 interfaces and
6893 	 * logical interfaces
6894 	 *
6895 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6896 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6897 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6898 	 *
6899 	 * the ipif's corresponding to each of these interface routes can be
6900 	 * uniquely identified by the "gateway" (actually interface address).
6901 	 *
6902 	 * In this case involving multiple IPv6 default routes to a particular
6903 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6904 	 * default route is of interest:
6905 	 *
6906 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6907 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6908 	 */
6909 
6910 	/* RTF_GATEWAY not set */
6911 	if (!(flags & RTF_GATEWAY)) {
6912 		queue_t	*stq;
6913 
6914 		if (sp != NULL) {
6915 			ip2dbg(("ip_rt_add: gateway security attributes "
6916 			    "cannot be set with interface route\n"));
6917 			if (ipif_refheld)
6918 				ipif_refrele(ipif);
6919 			return (EINVAL);
6920 		}
6921 
6922 		/*
6923 		 * As the interface index specified with the RTA_IFP sockaddr is
6924 		 * the same for all ipif's off of an ill, the matching logic
6925 		 * below uses MATCH_IRE_ILL if such an index was specified.
6926 		 * This means that routes sharing the same prefix when added
6927 		 * using a RTA_IFP sockaddr must have distinct interface
6928 		 * indices (namely, they must be on distinct ill's).
6929 		 *
6930 		 * On the other hand, since the gateway address will usually be
6931 		 * different for each ipif on the system, the matching logic
6932 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6933 		 * route.  This means that interface routes for the same prefix
6934 		 * can be created if they belong to distinct ipif's and if a
6935 		 * RTA_IFP sockaddr is not present.
6936 		 */
6937 		if (ipif_arg != NULL) {
6938 			if (ipif_refheld)  {
6939 				ipif_refrele(ipif);
6940 				ipif_refheld = B_FALSE;
6941 			}
6942 			ipif = ipif_arg;
6943 			match_flags |= MATCH_IRE_ILL;
6944 		} else {
6945 			/*
6946 			 * Check the ipif corresponding to the gw_addr
6947 			 */
6948 			if (ipif == NULL)
6949 				return (ENETUNREACH);
6950 			match_flags |= MATCH_IRE_IPIF;
6951 		}
6952 		ASSERT(ipif != NULL);
6953 
6954 		/*
6955 		 * We check for an existing entry at this point.
6956 		 *
6957 		 * Since a netmask isn't passed in via the ioctl interface
6958 		 * (SIOCADDRT), we don't check for a matching netmask in that
6959 		 * case.
6960 		 */
6961 		if (!ioctl_msg)
6962 			match_flags |= MATCH_IRE_MASK;
6963 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6964 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6965 		if (ire != NULL) {
6966 			ire_refrele(ire);
6967 			if (ipif_refheld)
6968 				ipif_refrele(ipif);
6969 			return (EEXIST);
6970 		}
6971 
6972 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6973 		    ? ipif->ipif_rq : ipif->ipif_wq;
6974 
6975 		/*
6976 		 * Create a copy of the IRE_LOOPBACK,
6977 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6978 		 * the modified address and netmask.
6979 		 */
6980 		ire = ire_create(
6981 		    (uchar_t *)&dst_addr,
6982 		    (uint8_t *)&mask,
6983 		    (uint8_t *)&ipif->ipif_src_addr,
6984 		    NULL,
6985 		    &ipif->ipif_mtu,
6986 		    NULL,
6987 		    NULL,
6988 		    stq,
6989 		    ipif->ipif_net_type,
6990 		    ipif,
6991 		    0,
6992 		    0,
6993 		    0,
6994 		    flags,
6995 		    &ire_uinfo_null,
6996 		    NULL,
6997 		    NULL,
6998 		    ipst);
6999 		if (ire == NULL) {
7000 			if (ipif_refheld)
7001 				ipif_refrele(ipif);
7002 			return (ENOMEM);
7003 		}
7004 
7005 		/*
7006 		 * Some software (for example, GateD and Sun Cluster) attempts
7007 		 * to create (what amount to) IRE_PREFIX routes with the
7008 		 * loopback address as the gateway.  This is primarily done to
7009 		 * set up prefixes with the RTF_REJECT flag set (for example,
7010 		 * when generating aggregate routes.)
7011 		 *
7012 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7013 		 * IRE_LOOPBACK, then we map the request into a
7014 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7015 		 * these interface routes, by definition, can only be that.
7016 		 *
7017 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7018 		 * routine, but rather using ire_create() directly.
7019 		 *
7020 		 */
7021 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7022 			ire->ire_type = IRE_IF_NORESOLVER;
7023 			ire->ire_flags |= RTF_BLACKHOLE;
7024 		}
7025 
7026 		error = ire_add(&ire, q, mp, func, B_FALSE);
7027 		if (error == 0)
7028 			goto save_ire;
7029 
7030 		/*
7031 		 * In the result of failure, ire_add() will have already
7032 		 * deleted the ire in question, so there is no need to
7033 		 * do that here.
7034 		 */
7035 		if (ipif_refheld)
7036 			ipif_refrele(ipif);
7037 		return (error);
7038 	}
7039 	if (ipif_refheld) {
7040 		ipif_refrele(ipif);
7041 		ipif_refheld = B_FALSE;
7042 	}
7043 
7044 	/*
7045 	 * Get an interface IRE for the specified gateway.
7046 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7047 	 * gateway, it is currently unreachable and we fail the request
7048 	 * accordingly.
7049 	 */
7050 	ipif = ipif_arg;
7051 	if (ipif_arg != NULL)
7052 		match_flags |= MATCH_IRE_ILL;
7053 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7054 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7055 	if (gw_ire == NULL)
7056 		return (ENETUNREACH);
7057 
7058 	/*
7059 	 * We create one of three types of IREs as a result of this request
7060 	 * based on the netmask.  A netmask of all ones (which is automatically
7061 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7062 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7063 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7064 	 * destination prefix.
7065 	 */
7066 	if (mask == IP_HOST_MASK)
7067 		type = IRE_HOST;
7068 	else if (mask == 0)
7069 		type = IRE_DEFAULT;
7070 	else
7071 		type = IRE_PREFIX;
7072 
7073 	/* check for a duplicate entry */
7074 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7075 	    NULL, ALL_ZONES, 0, NULL,
7076 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7077 	if (ire != NULL) {
7078 		ire_refrele(gw_ire);
7079 		ire_refrele(ire);
7080 		return (EEXIST);
7081 	}
7082 
7083 	/* Security attribute exists */
7084 	if (sp != NULL) {
7085 		tsol_gcgrp_addr_t ga;
7086 
7087 		/* find or create the gateway credentials group */
7088 		ga.ga_af = AF_INET;
7089 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7090 
7091 		/* we hold reference to it upon success */
7092 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7093 		if (gcgrp == NULL) {
7094 			ire_refrele(gw_ire);
7095 			return (ENOMEM);
7096 		}
7097 
7098 		/*
7099 		 * Create and add the security attribute to the group; a
7100 		 * reference to the group is made upon allocating a new
7101 		 * entry successfully.  If it finds an already-existing
7102 		 * entry for the security attribute in the group, it simply
7103 		 * returns it and no new reference is made to the group.
7104 		 */
7105 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7106 		if (gc == NULL) {
7107 			/* release reference held by gcgrp_lookup */
7108 			GCGRP_REFRELE(gcgrp);
7109 			ire_refrele(gw_ire);
7110 			return (ENOMEM);
7111 		}
7112 	}
7113 
7114 	/* Create the IRE. */
7115 	ire = ire_create(
7116 	    (uchar_t *)&dst_addr,		/* dest address */
7117 	    (uchar_t *)&mask,			/* mask */
7118 	    /* src address assigned by the caller? */
7119 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7120 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7121 	    (uchar_t *)&gw_addr,		/* gateway address */
7122 	    &gw_ire->ire_max_frag,
7123 	    NULL,				/* no src nce */
7124 	    NULL,				/* no recv-from queue */
7125 	    NULL,				/* no send-to queue */
7126 	    (ushort_t)type,			/* IRE type */
7127 	    ipif_arg,
7128 	    0,
7129 	    0,
7130 	    0,
7131 	    flags,
7132 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7133 	    gc,					/* security attribute */
7134 	    NULL,
7135 	    ipst);
7136 
7137 	/*
7138 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7139 	 * reference to the 'gcgrp'. We can now release the extra reference
7140 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7141 	 */
7142 	if (gcgrp_xtraref)
7143 		GCGRP_REFRELE(gcgrp);
7144 	if (ire == NULL) {
7145 		if (gc != NULL)
7146 			GC_REFRELE(gc);
7147 		ire_refrele(gw_ire);
7148 		return (ENOMEM);
7149 	}
7150 
7151 	/*
7152 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7153 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7154 	 */
7155 
7156 	/* Add the new IRE. */
7157 	error = ire_add(&ire, q, mp, func, B_FALSE);
7158 	if (error != 0) {
7159 		/*
7160 		 * In the result of failure, ire_add() will have already
7161 		 * deleted the ire in question, so there is no need to
7162 		 * do that here.
7163 		 */
7164 		ire_refrele(gw_ire);
7165 		return (error);
7166 	}
7167 
7168 	if (flags & RTF_MULTIRT) {
7169 		/*
7170 		 * Invoke the CGTP (multirouting) filtering module
7171 		 * to add the dst address in the filtering database.
7172 		 * Replicated inbound packets coming from that address
7173 		 * will be filtered to discard the duplicates.
7174 		 * It is not necessary to call the CGTP filter hook
7175 		 * when the dst address is a broadcast or multicast,
7176 		 * because an IP source address cannot be a broadcast
7177 		 * or a multicast.
7178 		 */
7179 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7180 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7181 		if (ire_dst != NULL) {
7182 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7183 			ire_refrele(ire_dst);
7184 			goto save_ire;
7185 		}
7186 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7187 		    !CLASSD(ire->ire_addr)) {
7188 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7189 			    ipst->ips_netstack->netstack_stackid,
7190 			    ire->ire_addr,
7191 			    ire->ire_gateway_addr,
7192 			    ire->ire_src_addr,
7193 			    gw_ire->ire_src_addr);
7194 			if (res != 0) {
7195 				ire_refrele(gw_ire);
7196 				ire_delete(ire);
7197 				return (res);
7198 			}
7199 		}
7200 	}
7201 
7202 	/*
7203 	 * Now that the prefix IRE entry has been created, delete any
7204 	 * existing gateway IRE cache entries as well as any IRE caches
7205 	 * using the gateway, and force them to be created through
7206 	 * ip_newroute.
7207 	 */
7208 	if (gc != NULL) {
7209 		ASSERT(gcgrp != NULL);
7210 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7211 	}
7212 
7213 save_ire:
7214 	if (gw_ire != NULL) {
7215 		ire_refrele(gw_ire);
7216 	}
7217 	if (ipif != NULL) {
7218 		/*
7219 		 * Save enough information so that we can recreate the IRE if
7220 		 * the interface goes down and then up.  The metrics associated
7221 		 * with the route will be saved as well when rts_setmetrics() is
7222 		 * called after the IRE has been created.  In the case where
7223 		 * memory cannot be allocated, none of this information will be
7224 		 * saved.
7225 		 */
7226 		ipif_save_ire(ipif, ire);
7227 	}
7228 	if (ioctl_msg)
7229 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7230 	if (ire_arg != NULL) {
7231 		/*
7232 		 * Store the ire that was successfully added into where ire_arg
7233 		 * points to so that callers don't have to look it up
7234 		 * themselves (but they are responsible for ire_refrele()ing
7235 		 * the ire when they are finished with it).
7236 		 */
7237 		*ire_arg = ire;
7238 	} else {
7239 		ire_refrele(ire);		/* Held in ire_add */
7240 	}
7241 	if (ipif_refheld)
7242 		ipif_refrele(ipif);
7243 	return (0);
7244 }
7245 
7246 /*
7247  * ip_rt_delete is called to delete an IPv4 route.
7248  * ipif_arg is passed in to associate it with the correct interface.
7249  * We may need to restart this operation if the ipif cannot be looked up
7250  * due to an exclusive operation that is currently in progress. The restart
7251  * entry point is specified by 'func'
7252  */
7253 /* ARGSUSED4 */
7254 int
7255 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7256     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7257     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7258 {
7259 	ire_t	*ire = NULL;
7260 	ipif_t	*ipif;
7261 	boolean_t ipif_refheld = B_FALSE;
7262 	uint_t	type;
7263 	uint_t	match_flags = MATCH_IRE_TYPE;
7264 	int	err = 0;
7265 
7266 	ip1dbg(("ip_rt_delete:"));
7267 	/*
7268 	 * If this is the case of RTF_HOST being set, then we set the netmask
7269 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7270 	 */
7271 	if (flags & RTF_HOST) {
7272 		mask = IP_HOST_MASK;
7273 		match_flags |= MATCH_IRE_MASK;
7274 	} else if (rtm_addrs & RTA_NETMASK) {
7275 		match_flags |= MATCH_IRE_MASK;
7276 	}
7277 
7278 	/*
7279 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7280 	 * we check if the gateway address is one of our interfaces first,
7281 	 * and fall back on RTF_GATEWAY routes.
7282 	 *
7283 	 * This makes it possible to delete an original
7284 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7285 	 *
7286 	 * As the interface index specified with the RTA_IFP sockaddr is the
7287 	 * same for all ipif's off of an ill, the matching logic below uses
7288 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7289 	 * sharing the same prefix and interface index as the the route
7290 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7291 	 * is specified in the request.
7292 	 *
7293 	 * On the other hand, since the gateway address will usually be
7294 	 * different for each ipif on the system, the matching logic
7295 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7296 	 * route.  This means that interface routes for the same prefix can be
7297 	 * uniquely identified if they belong to distinct ipif's and if a
7298 	 * RTA_IFP sockaddr is not present.
7299 	 *
7300 	 * For more detail on specifying routes by gateway address and by
7301 	 * interface index, see the comments in ip_rt_add().
7302 	 */
7303 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7304 	    ipst);
7305 	if (ipif != NULL)
7306 		ipif_refheld = B_TRUE;
7307 	else if (err == EINPROGRESS)
7308 		return (err);
7309 	else
7310 		err = 0;
7311 	if (ipif != NULL) {
7312 		if (ipif_arg != NULL) {
7313 			if (ipif_refheld) {
7314 				ipif_refrele(ipif);
7315 				ipif_refheld = B_FALSE;
7316 			}
7317 			ipif = ipif_arg;
7318 			match_flags |= MATCH_IRE_ILL;
7319 		} else {
7320 			match_flags |= MATCH_IRE_IPIF;
7321 		}
7322 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7323 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7324 			    ALL_ZONES, NULL, match_flags, ipst);
7325 		}
7326 		if (ire == NULL) {
7327 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7328 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7329 			    match_flags, ipst);
7330 		}
7331 	}
7332 
7333 	if (ire == NULL) {
7334 		/*
7335 		 * At this point, the gateway address is not one of our own
7336 		 * addresses or a matching interface route was not found.  We
7337 		 * set the IRE type to lookup based on whether
7338 		 * this is a host route, a default route or just a prefix.
7339 		 *
7340 		 * If an ipif_arg was passed in, then the lookup is based on an
7341 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7342 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7343 		 * set as the route being looked up is not a traditional
7344 		 * interface route.
7345 		 */
7346 		match_flags &= ~MATCH_IRE_IPIF;
7347 		match_flags |= MATCH_IRE_GW;
7348 		if (ipif_arg != NULL)
7349 			match_flags |= MATCH_IRE_ILL;
7350 		if (mask == IP_HOST_MASK)
7351 			type = IRE_HOST;
7352 		else if (mask == 0)
7353 			type = IRE_DEFAULT;
7354 		else
7355 			type = IRE_PREFIX;
7356 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7357 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7358 	}
7359 
7360 	if (ipif_refheld)
7361 		ipif_refrele(ipif);
7362 
7363 	/* ipif is not refheld anymore */
7364 	if (ire == NULL)
7365 		return (ESRCH);
7366 
7367 	if (ire->ire_flags & RTF_MULTIRT) {
7368 		/*
7369 		 * Invoke the CGTP (multirouting) filtering module
7370 		 * to remove the dst address from the filtering database.
7371 		 * Packets coming from that address will no longer be
7372 		 * filtered to remove duplicates.
7373 		 */
7374 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7375 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7376 			    ipst->ips_netstack->netstack_stackid,
7377 			    ire->ire_addr, ire->ire_gateway_addr);
7378 		}
7379 		ip_cgtp_bcast_delete(ire, ipst);
7380 	}
7381 
7382 	ipif = ire->ire_ipif;
7383 	if (ipif != NULL)
7384 		ipif_remove_ire(ipif, ire);
7385 	if (ioctl_msg)
7386 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7387 	ire_delete(ire);
7388 	ire_refrele(ire);
7389 	return (err);
7390 }
7391 
7392 /*
7393  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7394  */
7395 /* ARGSUSED */
7396 int
7397 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7398     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7399 {
7400 	ipaddr_t dst_addr;
7401 	ipaddr_t gw_addr;
7402 	ipaddr_t mask;
7403 	int error = 0;
7404 	mblk_t *mp1;
7405 	struct rtentry *rt;
7406 	ipif_t *ipif = NULL;
7407 	ip_stack_t	*ipst;
7408 
7409 	ASSERT(q->q_next == NULL);
7410 	ipst = CONNQ_TO_IPST(q);
7411 
7412 	ip1dbg(("ip_siocaddrt:"));
7413 	/* Existence of mp1 verified in ip_wput_nondata */
7414 	mp1 = mp->b_cont->b_cont;
7415 	rt = (struct rtentry *)mp1->b_rptr;
7416 
7417 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7418 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7419 
7420 	/*
7421 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7422 	 * to a particular host address.  In this case, we set the netmask to
7423 	 * all ones for the particular destination address.  Otherwise,
7424 	 * determine the netmask to be used based on dst_addr and the interfaces
7425 	 * in use.
7426 	 */
7427 	if (rt->rt_flags & RTF_HOST) {
7428 		mask = IP_HOST_MASK;
7429 	} else {
7430 		/*
7431 		 * Note that ip_subnet_mask returns a zero mask in the case of
7432 		 * default (an all-zeroes address).
7433 		 */
7434 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7435 	}
7436 
7437 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7438 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7439 	if (ipif != NULL)
7440 		ipif_refrele(ipif);
7441 	return (error);
7442 }
7443 
7444 /*
7445  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7446  */
7447 /* ARGSUSED */
7448 int
7449 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7450     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7451 {
7452 	ipaddr_t dst_addr;
7453 	ipaddr_t gw_addr;
7454 	ipaddr_t mask;
7455 	int error;
7456 	mblk_t *mp1;
7457 	struct rtentry *rt;
7458 	ipif_t *ipif = NULL;
7459 	ip_stack_t	*ipst;
7460 
7461 	ASSERT(q->q_next == NULL);
7462 	ipst = CONNQ_TO_IPST(q);
7463 
7464 	ip1dbg(("ip_siocdelrt:"));
7465 	/* Existence of mp1 verified in ip_wput_nondata */
7466 	mp1 = mp->b_cont->b_cont;
7467 	rt = (struct rtentry *)mp1->b_rptr;
7468 
7469 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7470 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7471 
7472 	/*
7473 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7474 	 * to a particular host address.  In this case, we set the netmask to
7475 	 * all ones for the particular destination address.  Otherwise,
7476 	 * determine the netmask to be used based on dst_addr and the interfaces
7477 	 * in use.
7478 	 */
7479 	if (rt->rt_flags & RTF_HOST) {
7480 		mask = IP_HOST_MASK;
7481 	} else {
7482 		/*
7483 		 * Note that ip_subnet_mask returns a zero mask in the case of
7484 		 * default (an all-zeroes address).
7485 		 */
7486 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7487 	}
7488 
7489 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7490 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7491 	    mp, ip_process_ioctl, ipst);
7492 	if (ipif != NULL)
7493 		ipif_refrele(ipif);
7494 	return (error);
7495 }
7496 
7497 /*
7498  * Enqueue the mp onto the ipsq, chained by b_next.
7499  * b_prev stores the function to be executed later, and b_queue the queue
7500  * where this mp originated.
7501  */
7502 void
7503 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7504     ill_t *pending_ill)
7505 {
7506 	conn_t	*connp = NULL;
7507 
7508 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7509 	ASSERT(func != NULL);
7510 
7511 	mp->b_queue = q;
7512 	mp->b_prev = (void *)func;
7513 	mp->b_next = NULL;
7514 
7515 	switch (type) {
7516 	case CUR_OP:
7517 		if (ipsq->ipsq_mptail != NULL) {
7518 			ASSERT(ipsq->ipsq_mphead != NULL);
7519 			ipsq->ipsq_mptail->b_next = mp;
7520 		} else {
7521 			ASSERT(ipsq->ipsq_mphead == NULL);
7522 			ipsq->ipsq_mphead = mp;
7523 		}
7524 		ipsq->ipsq_mptail = mp;
7525 		break;
7526 
7527 	case NEW_OP:
7528 		if (ipsq->ipsq_xopq_mptail != NULL) {
7529 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7530 			ipsq->ipsq_xopq_mptail->b_next = mp;
7531 		} else {
7532 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7533 			ipsq->ipsq_xopq_mphead = mp;
7534 		}
7535 		ipsq->ipsq_xopq_mptail = mp;
7536 		break;
7537 	default:
7538 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7539 	}
7540 
7541 	if (CONN_Q(q) && pending_ill != NULL) {
7542 		connp = Q_TO_CONN(q);
7543 
7544 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7545 		connp->conn_oper_pending_ill = pending_ill;
7546 	}
7547 }
7548 
7549 /*
7550  * Return the mp at the head of the ipsq. After emptying the ipsq
7551  * look at the next ioctl, if this ioctl is complete. Otherwise
7552  * return, we will resume when we complete the current ioctl.
7553  * The current ioctl will wait till it gets a response from the
7554  * driver below.
7555  */
7556 static mblk_t *
7557 ipsq_dq(ipsq_t *ipsq)
7558 {
7559 	mblk_t	*mp;
7560 
7561 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7562 
7563 	mp = ipsq->ipsq_mphead;
7564 	if (mp != NULL) {
7565 		ipsq->ipsq_mphead = mp->b_next;
7566 		if (ipsq->ipsq_mphead == NULL)
7567 			ipsq->ipsq_mptail = NULL;
7568 		mp->b_next = NULL;
7569 		return (mp);
7570 	}
7571 	if (ipsq->ipsq_current_ipif != NULL)
7572 		return (NULL);
7573 	mp = ipsq->ipsq_xopq_mphead;
7574 	if (mp != NULL) {
7575 		ipsq->ipsq_xopq_mphead = mp->b_next;
7576 		if (ipsq->ipsq_xopq_mphead == NULL)
7577 			ipsq->ipsq_xopq_mptail = NULL;
7578 		mp->b_next = NULL;
7579 		return (mp);
7580 	}
7581 	return (NULL);
7582 }
7583 
7584 /*
7585  * Enter the ipsq corresponding to ill, by waiting synchronously till
7586  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7587  * will have to drain completely before ipsq_enter returns success.
7588  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7589  * and the ipsq_exit logic will start the next enqueued ioctl after
7590  * completion of the current ioctl. If 'force' is used, we don't wait
7591  * for the enqueued ioctls. This is needed when a conn_close wants to
7592  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7593  * of an ill can also use this option. But we dont' use it currently.
7594  */
7595 #define	ENTER_SQ_WAIT_TICKS 100
7596 boolean_t
7597 ipsq_enter(ill_t *ill, boolean_t force)
7598 {
7599 	ipsq_t	*ipsq;
7600 	boolean_t waited_enough = B_FALSE;
7601 
7602 	/*
7603 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7604 	 * Since the <ill-ipsq> assocs could change while we wait for the
7605 	 * writer, it is easier to wait on a fixed global rather than try to
7606 	 * cv_wait on a changing ipsq.
7607 	 */
7608 	mutex_enter(&ill->ill_lock);
7609 	for (;;) {
7610 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7611 			mutex_exit(&ill->ill_lock);
7612 			return (B_FALSE);
7613 		}
7614 
7615 		ipsq = ill->ill_phyint->phyint_ipsq;
7616 		mutex_enter(&ipsq->ipsq_lock);
7617 		if (ipsq->ipsq_writer == NULL &&
7618 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7619 			break;
7620 		} else if (ipsq->ipsq_writer != NULL) {
7621 			mutex_exit(&ipsq->ipsq_lock);
7622 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7623 		} else {
7624 			mutex_exit(&ipsq->ipsq_lock);
7625 			if (force) {
7626 				(void) cv_timedwait(&ill->ill_cv,
7627 				    &ill->ill_lock,
7628 				    lbolt + ENTER_SQ_WAIT_TICKS);
7629 				waited_enough = B_TRUE;
7630 				continue;
7631 			} else {
7632 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7633 			}
7634 		}
7635 	}
7636 
7637 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7638 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7639 	ipsq->ipsq_writer = curthread;
7640 	ipsq->ipsq_reentry_cnt++;
7641 #ifdef DEBUG
7642 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7643 #endif
7644 	mutex_exit(&ipsq->ipsq_lock);
7645 	mutex_exit(&ill->ill_lock);
7646 	return (B_TRUE);
7647 }
7648 
7649 /*
7650  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7651  * certain critical operations like plumbing (i.e. most set ioctls),
7652  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7653  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7654  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7655  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7656  * threads executing in the ipsq. Responses from the driver pertain to the
7657  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7658  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7659  *
7660  * If a thread does not want to reenter the ipsq when it is already writer,
7661  * it must make sure that the specified reentry point to be called later
7662  * when the ipsq is empty, nor any code path starting from the specified reentry
7663  * point must never ever try to enter the ipsq again. Otherwise it can lead
7664  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7665  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7666  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7667  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7668  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7669  * ioctl if the current ioctl has completed. If the current ioctl is still
7670  * in progress it simply returns. The current ioctl could be waiting for
7671  * a response from another module (arp_ or the driver or could be waiting for
7672  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7673  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7674  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7675  * ipsq_current_ipif is clear which happens only on ioctl completion.
7676  */
7677 
7678 /*
7679  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7680  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7681  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7682  * completion.
7683  */
7684 ipsq_t *
7685 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7686     ipsq_func_t func, int type, boolean_t reentry_ok)
7687 {
7688 	ipsq_t	*ipsq;
7689 
7690 	/* Only 1 of ipif or ill can be specified */
7691 	ASSERT((ipif != NULL) ^ (ill != NULL));
7692 	if (ipif != NULL)
7693 		ill = ipif->ipif_ill;
7694 
7695 	/*
7696 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7697 	 * ipsq of an ill can't change when ill_lock is held.
7698 	 */
7699 	GRAB_CONN_LOCK(q);
7700 	mutex_enter(&ill->ill_lock);
7701 	ipsq = ill->ill_phyint->phyint_ipsq;
7702 	mutex_enter(&ipsq->ipsq_lock);
7703 
7704 	/*
7705 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7706 	 *    (Note: If the caller does not specify reentry_ok then neither
7707 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7708 	 *    again. Otherwise it can lead to an infinite loop
7709 	 * 2. Enter the ipsq if there is no current writer and this attempted
7710 	 *    entry is part of the current ioctl or operation
7711 	 * 3. Enter the ipsq if there is no current writer and this is a new
7712 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7713 	 *    empty and there is no ioctl (or operation) currently in progress
7714 	 */
7715 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7716 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7717 	    ipsq->ipsq_current_ipif == NULL))) ||
7718 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7719 		/* Success. */
7720 		ipsq->ipsq_reentry_cnt++;
7721 		ipsq->ipsq_writer = curthread;
7722 		mutex_exit(&ipsq->ipsq_lock);
7723 		mutex_exit(&ill->ill_lock);
7724 		RELEASE_CONN_LOCK(q);
7725 #ifdef DEBUG
7726 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7727 		    IPSQ_STACK_DEPTH);
7728 #endif
7729 		return (ipsq);
7730 	}
7731 
7732 	ipsq_enq(ipsq, q, mp, func, type, ill);
7733 
7734 	mutex_exit(&ipsq->ipsq_lock);
7735 	mutex_exit(&ill->ill_lock);
7736 	RELEASE_CONN_LOCK(q);
7737 	return (NULL);
7738 }
7739 
7740 /*
7741  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7742  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7743  * cannot be entered, the mp is queued for completion.
7744  */
7745 void
7746 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7747     boolean_t reentry_ok)
7748 {
7749 	ipsq_t	*ipsq;
7750 
7751 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7752 
7753 	/*
7754 	 * Drop the caller's refhold on the ill.  This is safe since we either
7755 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7756 	 * IPSQ, in which case we return without accessing ill anymore.  This
7757 	 * is needed because func needs to see the correct refcount.
7758 	 * e.g. removeif can work only then.
7759 	 */
7760 	ill_refrele(ill);
7761 	if (ipsq != NULL) {
7762 		(*func)(ipsq, q, mp, NULL);
7763 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7764 	}
7765 }
7766 
7767 /*
7768  * If there are more than ILL_GRP_CNT ills in a group,
7769  * we use kmem alloc'd buffers, else use the stack
7770  */
7771 #define	ILL_GRP_CNT	14
7772 /*
7773  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7774  * Called by a thread that is currently exclusive on this ipsq.
7775  */
7776 void
7777 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7778 {
7779 	queue_t	*q;
7780 	mblk_t	*mp;
7781 	ipsq_func_t	func;
7782 	int	next;
7783 	ill_t	**ill_list = NULL;
7784 	size_t	ill_list_size = 0;
7785 	int	cnt = 0;
7786 	boolean_t need_ipsq_free = B_FALSE;
7787 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7788 
7789 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7790 	mutex_enter(&ipsq->ipsq_lock);
7791 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7792 	if (ipsq->ipsq_reentry_cnt != 1) {
7793 		ipsq->ipsq_reentry_cnt--;
7794 		mutex_exit(&ipsq->ipsq_lock);
7795 		return;
7796 	}
7797 
7798 	mp = ipsq_dq(ipsq);
7799 	while (mp != NULL) {
7800 again:
7801 		mutex_exit(&ipsq->ipsq_lock);
7802 		func = (ipsq_func_t)mp->b_prev;
7803 		q = (queue_t *)mp->b_queue;
7804 		mp->b_prev = NULL;
7805 		mp->b_queue = NULL;
7806 
7807 		/*
7808 		 * If 'q' is an conn queue, it is valid, since we did a
7809 		 * a refhold on the connp, at the start of the ioctl.
7810 		 * If 'q' is an ill queue, it is valid, since close of an
7811 		 * ill will clean up the 'ipsq'.
7812 		 */
7813 		(*func)(ipsq, q, mp, NULL);
7814 
7815 		mutex_enter(&ipsq->ipsq_lock);
7816 		mp = ipsq_dq(ipsq);
7817 	}
7818 
7819 	mutex_exit(&ipsq->ipsq_lock);
7820 
7821 	/*
7822 	 * Need to grab the locks in the right order. Need to
7823 	 * atomically check (under ipsq_lock) that there are no
7824 	 * messages before relinquishing the ipsq. Also need to
7825 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7826 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7827 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7828 	 * to grab ill_g_lock as writer.
7829 	 */
7830 	rw_enter(&ipst->ips_ill_g_lock,
7831 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7832 
7833 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7834 	if (ipsq->ipsq_refs != 0) {
7835 		/* At most 2 ills v4/v6 per phyint */
7836 		cnt = ipsq->ipsq_refs << 1;
7837 		ill_list_size = cnt * sizeof (ill_t *);
7838 		/*
7839 		 * If memory allocation fails, we will do the split
7840 		 * the next time ipsq_exit is called for whatever reason.
7841 		 * As long as the ipsq_split flag is set the need to
7842 		 * split is remembered.
7843 		 */
7844 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7845 		if (ill_list != NULL)
7846 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7847 	}
7848 	mutex_enter(&ipsq->ipsq_lock);
7849 	mp = ipsq_dq(ipsq);
7850 	if (mp != NULL) {
7851 		/* oops, some message has landed up, we can't get out */
7852 		if (ill_list != NULL)
7853 			ill_unlock_ills(ill_list, cnt);
7854 		rw_exit(&ipst->ips_ill_g_lock);
7855 		if (ill_list != NULL)
7856 			kmem_free(ill_list, ill_list_size);
7857 		ill_list = NULL;
7858 		ill_list_size = 0;
7859 		cnt = 0;
7860 		goto again;
7861 	}
7862 
7863 	/*
7864 	 * Split only if no ioctl is pending and if memory alloc succeeded
7865 	 * above.
7866 	 */
7867 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7868 	    ill_list != NULL) {
7869 		/*
7870 		 * No new ill can join this ipsq since we are holding the
7871 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7872 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7873 		 * If so we will retry on the next ipsq_exit.
7874 		 */
7875 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7876 	}
7877 
7878 	/*
7879 	 * We are holding the ipsq lock, hence no new messages can
7880 	 * land up on the ipsq, and there are no messages currently.
7881 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7882 	 * atomically while holding ill locks.
7883 	 */
7884 	ipsq->ipsq_writer = NULL;
7885 	ipsq->ipsq_reentry_cnt--;
7886 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7887 #ifdef DEBUG
7888 	ipsq->ipsq_depth = 0;
7889 #endif
7890 	mutex_exit(&ipsq->ipsq_lock);
7891 	/*
7892 	 * For IPMP this should wake up all ills in this ipsq.
7893 	 * We need to hold the ill_lock while waking up waiters to
7894 	 * avoid missed wakeups. But there is no need to acquire all
7895 	 * the ill locks and then wakeup. If we have not acquired all
7896 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7897 	 * wakes up ills one at a time after getting the right ill_lock
7898 	 */
7899 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7900 	if (ill_list != NULL)
7901 		ill_unlock_ills(ill_list, cnt);
7902 	if (ipsq->ipsq_refs == 0)
7903 		need_ipsq_free = B_TRUE;
7904 	rw_exit(&ipst->ips_ill_g_lock);
7905 	if (ill_list != 0)
7906 		kmem_free(ill_list, ill_list_size);
7907 
7908 	if (need_ipsq_free) {
7909 		/*
7910 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7911 		 * looked up. ipsq can be looked up only thru ill or phyint
7912 		 * and there are no ills/phyint on this ipsq.
7913 		 */
7914 		ipsq_delete(ipsq);
7915 	}
7916 	/*
7917 	 * Now start any igmp or mld timers that could not be started
7918 	 * while inside the ipsq. The timers can't be started while inside
7919 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7920 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7921 	 * there could be a deadlock since the timeout handlers
7922 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7923 	 * wait in ipsq_enter() trying to get the ipsq.
7924 	 *
7925 	 * However there is one exception to the above. If this thread is
7926 	 * itself the igmp/mld timeout handler thread, then we don't want
7927 	 * to start any new timer until the current handler is done. The
7928 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7929 	 * all others pass B_TRUE.
7930 	 */
7931 	if (start_igmp_timer) {
7932 		mutex_enter(&ipst->ips_igmp_timer_lock);
7933 		next = ipst->ips_igmp_deferred_next;
7934 		ipst->ips_igmp_deferred_next = INFINITY;
7935 		mutex_exit(&ipst->ips_igmp_timer_lock);
7936 
7937 		if (next != INFINITY)
7938 			igmp_start_timers(next, ipst);
7939 	}
7940 
7941 	if (start_mld_timer) {
7942 		mutex_enter(&ipst->ips_mld_timer_lock);
7943 		next = ipst->ips_mld_deferred_next;
7944 		ipst->ips_mld_deferred_next = INFINITY;
7945 		mutex_exit(&ipst->ips_mld_timer_lock);
7946 
7947 		if (next != INFINITY)
7948 			mld_start_timers(next, ipst);
7949 	}
7950 }
7951 
7952 /*
7953  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7954  * and `ioccmd'.
7955  */
7956 void
7957 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7958 {
7959 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7960 
7961 	mutex_enter(&ipsq->ipsq_lock);
7962 	ASSERT(ipsq->ipsq_current_ipif == NULL);
7963 	ASSERT(ipsq->ipsq_current_ioctl == 0);
7964 	ipsq->ipsq_current_ipif = ipif;
7965 	ipsq->ipsq_current_ioctl = ioccmd;
7966 	mutex_exit(&ipsq->ipsq_lock);
7967 }
7968 
7969 /*
7970  * Finish the current exclusive operation on `ipsq'.  Note that other
7971  * operations will not be able to proceed until an ipsq_exit() is done.
7972  */
7973 void
7974 ipsq_current_finish(ipsq_t *ipsq)
7975 {
7976 	ipif_t *ipif = ipsq->ipsq_current_ipif;
7977 
7978 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7979 
7980 	/*
7981 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
7982 	 * (but we're careful to never set IPIF_CHANGING in that case).
7983 	 */
7984 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
7985 		mutex_enter(&ipif->ipif_ill->ill_lock);
7986 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
7987 
7988 		/* Send any queued event */
7989 		ill_nic_info_dispatch(ipif->ipif_ill);
7990 		mutex_exit(&ipif->ipif_ill->ill_lock);
7991 	}
7992 
7993 	mutex_enter(&ipsq->ipsq_lock);
7994 	ASSERT(ipsq->ipsq_current_ipif != NULL);
7995 	ipsq->ipsq_current_ipif = NULL;
7996 	ipsq->ipsq_current_ioctl = 0;
7997 	mutex_exit(&ipsq->ipsq_lock);
7998 }
7999 
8000 /*
8001  * The ill is closing. Flush all messages on the ipsq that originated
8002  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8003  * for this ill since ipsq_enter could not have entered until then.
8004  * New messages can't be queued since the CONDEMNED flag is set.
8005  */
8006 static void
8007 ipsq_flush(ill_t *ill)
8008 {
8009 	queue_t	*q;
8010 	mblk_t	*prev;
8011 	mblk_t	*mp;
8012 	mblk_t	*mp_next;
8013 	ipsq_t	*ipsq;
8014 
8015 	ASSERT(IAM_WRITER_ILL(ill));
8016 	ipsq = ill->ill_phyint->phyint_ipsq;
8017 	/*
8018 	 * Flush any messages sent up by the driver.
8019 	 */
8020 	mutex_enter(&ipsq->ipsq_lock);
8021 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8022 		mp_next = mp->b_next;
8023 		q = mp->b_queue;
8024 		if (q == ill->ill_rq || q == ill->ill_wq) {
8025 			/* Remove the mp from the ipsq */
8026 			if (prev == NULL)
8027 				ipsq->ipsq_mphead = mp->b_next;
8028 			else
8029 				prev->b_next = mp->b_next;
8030 			if (ipsq->ipsq_mptail == mp) {
8031 				ASSERT(mp_next == NULL);
8032 				ipsq->ipsq_mptail = prev;
8033 			}
8034 			inet_freemsg(mp);
8035 		} else {
8036 			prev = mp;
8037 		}
8038 	}
8039 	mutex_exit(&ipsq->ipsq_lock);
8040 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8041 	ipsq_xopq_mp_cleanup(ill, NULL);
8042 	ill_pending_mp_cleanup(ill);
8043 }
8044 
8045 /* ARGSUSED */
8046 int
8047 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8048     ip_ioctl_cmd_t *ipip, void *ifreq)
8049 {
8050 	ill_t	*ill;
8051 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8052 	boolean_t isv6;
8053 	conn_t	*connp;
8054 	ip_stack_t	*ipst;
8055 
8056 	connp = Q_TO_CONN(q);
8057 	ipst = connp->conn_netstack->netstack_ip;
8058 	isv6 = connp->conn_af_isv6;
8059 	/*
8060 	 * Set original index.
8061 	 * Failover and failback move logical interfaces
8062 	 * from one physical interface to another.  The
8063 	 * original index indicates the parent of a logical
8064 	 * interface, in other words, the physical interface
8065 	 * the logical interface will be moved back to on
8066 	 * failback.
8067 	 */
8068 
8069 	/*
8070 	 * Don't allow the original index to be changed
8071 	 * for non-failover addresses, autoconfigured
8072 	 * addresses, or IPv6 link local addresses.
8073 	 */
8074 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8075 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8076 		return (EINVAL);
8077 	}
8078 	/*
8079 	 * The new original index must be in use by some
8080 	 * physical interface.
8081 	 */
8082 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8083 	    NULL, NULL, ipst);
8084 	if (ill == NULL)
8085 		return (ENXIO);
8086 	ill_refrele(ill);
8087 
8088 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8089 	/*
8090 	 * When this ipif gets failed back, don't
8091 	 * preserve the original id, as it is no
8092 	 * longer applicable.
8093 	 */
8094 	ipif->ipif_orig_ipifid = 0;
8095 	/*
8096 	 * For IPv4, change the original index of any
8097 	 * multicast addresses associated with the
8098 	 * ipif to the new value.
8099 	 */
8100 	if (!isv6) {
8101 		ilm_t *ilm;
8102 
8103 		mutex_enter(&ipif->ipif_ill->ill_lock);
8104 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8105 		    ilm = ilm->ilm_next) {
8106 			if (ilm->ilm_ipif == ipif) {
8107 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8108 			}
8109 		}
8110 		mutex_exit(&ipif->ipif_ill->ill_lock);
8111 	}
8112 	return (0);
8113 }
8114 
8115 /* ARGSUSED */
8116 int
8117 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8118     ip_ioctl_cmd_t *ipip, void *ifreq)
8119 {
8120 	struct lifreq *lifr = (struct lifreq *)ifreq;
8121 
8122 	/*
8123 	 * Get the original interface index i.e the one
8124 	 * before FAILOVER if it ever happened.
8125 	 */
8126 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8127 	return (0);
8128 }
8129 
8130 /*
8131  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8132  * refhold and return the associated ipif
8133  */
8134 /* ARGSUSED */
8135 int
8136 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8137     cmd_info_t *ci, ipsq_func_t func)
8138 {
8139 	boolean_t exists;
8140 	struct iftun_req *ta;
8141 	ipif_t	*ipif;
8142 	ill_t	*ill;
8143 	boolean_t isv6;
8144 	mblk_t	*mp1;
8145 	int	error;
8146 	conn_t	*connp;
8147 	ip_stack_t	*ipst;
8148 
8149 	/* Existence verified in ip_wput_nondata */
8150 	mp1 = mp->b_cont->b_cont;
8151 	ta = (struct iftun_req *)mp1->b_rptr;
8152 	/*
8153 	 * Null terminate the string to protect against buffer
8154 	 * overrun. String was generated by user code and may not
8155 	 * be trusted.
8156 	 */
8157 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8158 
8159 	connp = Q_TO_CONN(q);
8160 	isv6 = connp->conn_af_isv6;
8161 	ipst = connp->conn_netstack->netstack_ip;
8162 
8163 	/* Disallows implicit create */
8164 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8165 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8166 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8167 	if (ipif == NULL)
8168 		return (error);
8169 
8170 	if (ipif->ipif_id != 0) {
8171 		/*
8172 		 * We really don't want to set/get tunnel parameters
8173 		 * on virtual tunnel interfaces.  Only allow the
8174 		 * base tunnel to do these.
8175 		 */
8176 		ipif_refrele(ipif);
8177 		return (EINVAL);
8178 	}
8179 
8180 	/*
8181 	 * Send down to tunnel mod for ioctl processing.
8182 	 * Will finish ioctl in ip_rput_other().
8183 	 */
8184 	ill = ipif->ipif_ill;
8185 	if (ill->ill_net_type == IRE_LOOPBACK) {
8186 		ipif_refrele(ipif);
8187 		return (EOPNOTSUPP);
8188 	}
8189 
8190 	if (ill->ill_wq == NULL) {
8191 		ipif_refrele(ipif);
8192 		return (ENXIO);
8193 	}
8194 	/*
8195 	 * Mark the ioctl as coming from an IPv6 interface for
8196 	 * tun's convenience.
8197 	 */
8198 	if (ill->ill_isv6)
8199 		ta->ifta_flags |= 0x80000000;
8200 	ci->ci_ipif = ipif;
8201 	return (0);
8202 }
8203 
8204 /*
8205  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8206  * and return the associated ipif.
8207  * Return value:
8208  *	Non zero: An error has occurred. ci may not be filled out.
8209  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8210  *	a held ipif in ci.ci_ipif.
8211  */
8212 int
8213 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8214     cmd_info_t *ci, ipsq_func_t func)
8215 {
8216 	sin_t		*sin;
8217 	sin6_t		*sin6;
8218 	char		*name;
8219 	struct ifreq    *ifr;
8220 	struct lifreq    *lifr;
8221 	ipif_t		*ipif = NULL;
8222 	ill_t		*ill;
8223 	conn_t		*connp;
8224 	boolean_t	isv6;
8225 	boolean_t	exists;
8226 	int		err;
8227 	mblk_t		*mp1;
8228 	zoneid_t	zoneid;
8229 	ip_stack_t	*ipst;
8230 
8231 	if (q->q_next != NULL) {
8232 		ill = (ill_t *)q->q_ptr;
8233 		isv6 = ill->ill_isv6;
8234 		connp = NULL;
8235 		zoneid = ALL_ZONES;
8236 		ipst = ill->ill_ipst;
8237 	} else {
8238 		ill = NULL;
8239 		connp = Q_TO_CONN(q);
8240 		isv6 = connp->conn_af_isv6;
8241 		zoneid = connp->conn_zoneid;
8242 		if (zoneid == GLOBAL_ZONEID) {
8243 			/* global zone can access ipifs in all zones */
8244 			zoneid = ALL_ZONES;
8245 		}
8246 		ipst = connp->conn_netstack->netstack_ip;
8247 	}
8248 
8249 	/* Has been checked in ip_wput_nondata */
8250 	mp1 = mp->b_cont->b_cont;
8251 
8252 	if (ipip->ipi_cmd_type == IF_CMD) {
8253 		/* This a old style SIOC[GS]IF* command */
8254 		ifr = (struct ifreq *)mp1->b_rptr;
8255 		/*
8256 		 * Null terminate the string to protect against buffer
8257 		 * overrun. String was generated by user code and may not
8258 		 * be trusted.
8259 		 */
8260 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8261 		sin = (sin_t *)&ifr->ifr_addr;
8262 		name = ifr->ifr_name;
8263 		ci->ci_sin = sin;
8264 		ci->ci_sin6 = NULL;
8265 		ci->ci_lifr = (struct lifreq *)ifr;
8266 	} else {
8267 		/* This a new style SIOC[GS]LIF* command */
8268 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8269 		lifr = (struct lifreq *)mp1->b_rptr;
8270 		/*
8271 		 * Null terminate the string to protect against buffer
8272 		 * overrun. String was generated by user code and may not
8273 		 * be trusted.
8274 		 */
8275 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8276 		name = lifr->lifr_name;
8277 		sin = (sin_t *)&lifr->lifr_addr;
8278 		sin6 = (sin6_t *)&lifr->lifr_addr;
8279 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8280 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8281 			    LIFNAMSIZ);
8282 		}
8283 		ci->ci_sin = sin;
8284 		ci->ci_sin6 = sin6;
8285 		ci->ci_lifr = lifr;
8286 	}
8287 
8288 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8289 		/*
8290 		 * The ioctl will be failed if the ioctl comes down
8291 		 * an conn stream
8292 		 */
8293 		if (ill == NULL) {
8294 			/*
8295 			 * Not an ill queue, return EINVAL same as the
8296 			 * old error code.
8297 			 */
8298 			return (ENXIO);
8299 		}
8300 		ipif = ill->ill_ipif;
8301 		ipif_refhold(ipif);
8302 	} else {
8303 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8304 		    &exists, isv6, zoneid,
8305 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8306 		    ipst);
8307 		if (ipif == NULL) {
8308 			if (err == EINPROGRESS)
8309 				return (err);
8310 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8311 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8312 				/*
8313 				 * Need to try both v4 and v6 since this
8314 				 * ioctl can come down either v4 or v6
8315 				 * socket. The lifreq.lifr_family passed
8316 				 * down by this ioctl is AF_UNSPEC.
8317 				 */
8318 				ipif = ipif_lookup_on_name(name,
8319 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8320 				    zoneid, (connp == NULL) ? q :
8321 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8322 				if (err == EINPROGRESS)
8323 					return (err);
8324 			}
8325 			err = 0;	/* Ensure we don't use it below */
8326 		}
8327 	}
8328 
8329 	/*
8330 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8331 	 */
8332 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8333 		ipif_refrele(ipif);
8334 		return (ENXIO);
8335 	}
8336 
8337 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8338 	    name[0] == '\0') {
8339 		/*
8340 		 * Handle a or a SIOC?IF* with a null name
8341 		 * during plumb (on the ill queue before the I_PLINK).
8342 		 */
8343 		ipif = ill->ill_ipif;
8344 		ipif_refhold(ipif);
8345 	}
8346 
8347 	if (ipif == NULL)
8348 		return (ENXIO);
8349 
8350 	/*
8351 	 * Allow only GET operations if this ipif has been created
8352 	 * temporarily due to a MOVE operation.
8353 	 */
8354 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8355 		ipif_refrele(ipif);
8356 		return (EINVAL);
8357 	}
8358 
8359 	ci->ci_ipif = ipif;
8360 	return (0);
8361 }
8362 
8363 /*
8364  * Return the total number of ipifs.
8365  */
8366 static uint_t
8367 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8368 {
8369 	uint_t numifs = 0;
8370 	ill_t	*ill;
8371 	ill_walk_context_t	ctx;
8372 	ipif_t	*ipif;
8373 
8374 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8375 	ill = ILL_START_WALK_V4(&ctx, ipst);
8376 
8377 	while (ill != NULL) {
8378 		for (ipif = ill->ill_ipif; ipif != NULL;
8379 		    ipif = ipif->ipif_next) {
8380 			if (ipif->ipif_zoneid == zoneid ||
8381 			    ipif->ipif_zoneid == ALL_ZONES)
8382 				numifs++;
8383 		}
8384 		ill = ill_next(&ctx, ill);
8385 	}
8386 	rw_exit(&ipst->ips_ill_g_lock);
8387 	return (numifs);
8388 }
8389 
8390 /*
8391  * Return the total number of ipifs.
8392  */
8393 static uint_t
8394 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8395 {
8396 	uint_t numifs = 0;
8397 	ill_t	*ill;
8398 	ipif_t	*ipif;
8399 	ill_walk_context_t	ctx;
8400 
8401 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8402 
8403 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8404 	if (family == AF_INET)
8405 		ill = ILL_START_WALK_V4(&ctx, ipst);
8406 	else if (family == AF_INET6)
8407 		ill = ILL_START_WALK_V6(&ctx, ipst);
8408 	else
8409 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8410 
8411 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8412 		for (ipif = ill->ill_ipif; ipif != NULL;
8413 		    ipif = ipif->ipif_next) {
8414 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8415 			    !(lifn_flags & LIFC_NOXMIT))
8416 				continue;
8417 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8418 			    !(lifn_flags & LIFC_TEMPORARY))
8419 				continue;
8420 			if (((ipif->ipif_flags &
8421 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8422 			    IPIF_DEPRECATED)) ||
8423 			    IS_LOOPBACK(ill) ||
8424 			    !(ipif->ipif_flags & IPIF_UP)) &&
8425 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8426 				continue;
8427 
8428 			if (zoneid != ipif->ipif_zoneid &&
8429 			    ipif->ipif_zoneid != ALL_ZONES &&
8430 			    (zoneid != GLOBAL_ZONEID ||
8431 			    !(lifn_flags & LIFC_ALLZONES)))
8432 				continue;
8433 
8434 			numifs++;
8435 		}
8436 	}
8437 	rw_exit(&ipst->ips_ill_g_lock);
8438 	return (numifs);
8439 }
8440 
8441 uint_t
8442 ip_get_lifsrcofnum(ill_t *ill)
8443 {
8444 	uint_t numifs = 0;
8445 	ill_t	*ill_head = ill;
8446 	ip_stack_t	*ipst = ill->ill_ipst;
8447 
8448 	/*
8449 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8450 	 * other thread may be trying to relink the ILLs in this usesrc group
8451 	 * and adjusting the ill_usesrc_grp_next pointers
8452 	 */
8453 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8454 	if ((ill->ill_usesrc_ifindex == 0) &&
8455 	    (ill->ill_usesrc_grp_next != NULL)) {
8456 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8457 		    ill = ill->ill_usesrc_grp_next)
8458 			numifs++;
8459 	}
8460 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8461 
8462 	return (numifs);
8463 }
8464 
8465 /* Null values are passed in for ipif, sin, and ifreq */
8466 /* ARGSUSED */
8467 int
8468 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8469     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8470 {
8471 	int *nump;
8472 	conn_t *connp = Q_TO_CONN(q);
8473 
8474 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8475 
8476 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8477 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8478 
8479 	*nump = ip_get_numifs(connp->conn_zoneid,
8480 	    connp->conn_netstack->netstack_ip);
8481 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8482 	return (0);
8483 }
8484 
8485 /* Null values are passed in for ipif, sin, and ifreq */
8486 /* ARGSUSED */
8487 int
8488 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8489     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8490 {
8491 	struct lifnum *lifn;
8492 	mblk_t	*mp1;
8493 	conn_t *connp = Q_TO_CONN(q);
8494 
8495 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8496 
8497 	/* Existence checked in ip_wput_nondata */
8498 	mp1 = mp->b_cont->b_cont;
8499 
8500 	lifn = (struct lifnum *)mp1->b_rptr;
8501 	switch (lifn->lifn_family) {
8502 	case AF_UNSPEC:
8503 	case AF_INET:
8504 	case AF_INET6:
8505 		break;
8506 	default:
8507 		return (EAFNOSUPPORT);
8508 	}
8509 
8510 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8511 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8512 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8513 	return (0);
8514 }
8515 
8516 /* ARGSUSED */
8517 int
8518 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8519     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8520 {
8521 	STRUCT_HANDLE(ifconf, ifc);
8522 	mblk_t *mp1;
8523 	struct iocblk *iocp;
8524 	struct ifreq *ifr;
8525 	ill_walk_context_t	ctx;
8526 	ill_t	*ill;
8527 	ipif_t	*ipif;
8528 	struct sockaddr_in *sin;
8529 	int32_t	ifclen;
8530 	zoneid_t zoneid;
8531 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8532 
8533 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8534 
8535 	ip1dbg(("ip_sioctl_get_ifconf"));
8536 	/* Existence verified in ip_wput_nondata */
8537 	mp1 = mp->b_cont->b_cont;
8538 	iocp = (struct iocblk *)mp->b_rptr;
8539 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8540 
8541 	/*
8542 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8543 	 * the user buffer address and length into which the list of struct
8544 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8545 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8546 	 * the SIOCGIFCONF operation was redefined to simply provide
8547 	 * a large output buffer into which we are supposed to jam the ifreq
8548 	 * array.  The same ioctl command code was used, despite the fact that
8549 	 * both the applications and the kernel code had to change, thus making
8550 	 * it impossible to support both interfaces.
8551 	 *
8552 	 * For reasons not good enough to try to explain, the following
8553 	 * algorithm is used for deciding what to do with one of these:
8554 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8555 	 * form with the output buffer coming down as the continuation message.
8556 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8557 	 * and we have to copy in the ifconf structure to find out how big the
8558 	 * output buffer is and where to copy out to.  Sure no problem...
8559 	 *
8560 	 */
8561 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8562 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8563 		int numifs = 0;
8564 		size_t ifc_bufsize;
8565 
8566 		/*
8567 		 * Must be (better be!) continuation of a TRANSPARENT
8568 		 * IOCTL.  We just copied in the ifconf structure.
8569 		 */
8570 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8571 		    (struct ifconf *)mp1->b_rptr);
8572 
8573 		/*
8574 		 * Allocate a buffer to hold requested information.
8575 		 *
8576 		 * If ifc_len is larger than what is needed, we only
8577 		 * allocate what we will use.
8578 		 *
8579 		 * If ifc_len is smaller than what is needed, return
8580 		 * EINVAL.
8581 		 *
8582 		 * XXX: the ill_t structure can hava 2 counters, for
8583 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8584 		 * number of interfaces for a device, so we don't need
8585 		 * to count them here...
8586 		 */
8587 		numifs = ip_get_numifs(zoneid, ipst);
8588 
8589 		ifclen = STRUCT_FGET(ifc, ifc_len);
8590 		ifc_bufsize = numifs * sizeof (struct ifreq);
8591 		if (ifc_bufsize > ifclen) {
8592 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8593 				/* old behaviour */
8594 				return (EINVAL);
8595 			} else {
8596 				ifc_bufsize = ifclen;
8597 			}
8598 		}
8599 
8600 		mp1 = mi_copyout_alloc(q, mp,
8601 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8602 		if (mp1 == NULL)
8603 			return (ENOMEM);
8604 
8605 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8606 	}
8607 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8608 	/*
8609 	 * the SIOCGIFCONF ioctl only knows about
8610 	 * IPv4 addresses, so don't try to tell
8611 	 * it about interfaces with IPv6-only
8612 	 * addresses. (Last parm 'isv6' is B_FALSE)
8613 	 */
8614 
8615 	ifr = (struct ifreq *)mp1->b_rptr;
8616 
8617 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8618 	ill = ILL_START_WALK_V4(&ctx, ipst);
8619 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8620 		for (ipif = ill->ill_ipif; ipif != NULL;
8621 		    ipif = ipif->ipif_next) {
8622 			if (zoneid != ipif->ipif_zoneid &&
8623 			    ipif->ipif_zoneid != ALL_ZONES)
8624 				continue;
8625 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8626 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8627 					/* old behaviour */
8628 					rw_exit(&ipst->ips_ill_g_lock);
8629 					return (EINVAL);
8630 				} else {
8631 					goto if_copydone;
8632 				}
8633 			}
8634 			ipif_get_name(ipif, ifr->ifr_name,
8635 			    sizeof (ifr->ifr_name));
8636 			sin = (sin_t *)&ifr->ifr_addr;
8637 			*sin = sin_null;
8638 			sin->sin_family = AF_INET;
8639 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8640 			ifr++;
8641 		}
8642 	}
8643 if_copydone:
8644 	rw_exit(&ipst->ips_ill_g_lock);
8645 	mp1->b_wptr = (uchar_t *)ifr;
8646 
8647 	if (STRUCT_BUF(ifc) != NULL) {
8648 		STRUCT_FSET(ifc, ifc_len,
8649 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8650 	}
8651 	return (0);
8652 }
8653 
8654 /*
8655  * Get the interfaces using the address hosted on the interface passed in,
8656  * as a source adddress
8657  */
8658 /* ARGSUSED */
8659 int
8660 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8661     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8662 {
8663 	mblk_t *mp1;
8664 	ill_t	*ill, *ill_head;
8665 	ipif_t	*ipif, *orig_ipif;
8666 	int	numlifs = 0;
8667 	size_t	lifs_bufsize, lifsmaxlen;
8668 	struct	lifreq *lifr;
8669 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8670 	uint_t	ifindex;
8671 	zoneid_t zoneid;
8672 	int err = 0;
8673 	boolean_t isv6 = B_FALSE;
8674 	struct	sockaddr_in	*sin;
8675 	struct	sockaddr_in6	*sin6;
8676 	STRUCT_HANDLE(lifsrcof, lifs);
8677 	ip_stack_t		*ipst;
8678 
8679 	ipst = CONNQ_TO_IPST(q);
8680 
8681 	ASSERT(q->q_next == NULL);
8682 
8683 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8684 
8685 	/* Existence verified in ip_wput_nondata */
8686 	mp1 = mp->b_cont->b_cont;
8687 
8688 	/*
8689 	 * Must be (better be!) continuation of a TRANSPARENT
8690 	 * IOCTL.  We just copied in the lifsrcof structure.
8691 	 */
8692 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8693 	    (struct lifsrcof *)mp1->b_rptr);
8694 
8695 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8696 		return (EINVAL);
8697 
8698 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8699 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8700 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8701 	    ip_process_ioctl, &err, ipst);
8702 	if (ipif == NULL) {
8703 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8704 		    ifindex));
8705 		return (err);
8706 	}
8707 
8708 
8709 	/* Allocate a buffer to hold requested information */
8710 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8711 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8712 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8713 	/* The actual size needed is always returned in lifs_len */
8714 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8715 
8716 	/* If the amount we need is more than what is passed in, abort */
8717 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8718 		ipif_refrele(ipif);
8719 		return (0);
8720 	}
8721 
8722 	mp1 = mi_copyout_alloc(q, mp,
8723 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8724 	if (mp1 == NULL) {
8725 		ipif_refrele(ipif);
8726 		return (ENOMEM);
8727 	}
8728 
8729 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8730 	bzero(mp1->b_rptr, lifs_bufsize);
8731 
8732 	lifr = (struct lifreq *)mp1->b_rptr;
8733 
8734 	ill = ill_head = ipif->ipif_ill;
8735 	orig_ipif = ipif;
8736 
8737 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8738 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8739 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8740 
8741 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8742 	for (; (ill != NULL) && (ill != ill_head);
8743 	    ill = ill->ill_usesrc_grp_next) {
8744 
8745 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8746 			break;
8747 
8748 		ipif = ill->ill_ipif;
8749 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8750 		if (ipif->ipif_isv6) {
8751 			sin6 = (sin6_t *)&lifr->lifr_addr;
8752 			*sin6 = sin6_null;
8753 			sin6->sin6_family = AF_INET6;
8754 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8755 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8756 			    &ipif->ipif_v6net_mask);
8757 		} else {
8758 			sin = (sin_t *)&lifr->lifr_addr;
8759 			*sin = sin_null;
8760 			sin->sin_family = AF_INET;
8761 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8762 			lifr->lifr_addrlen = ip_mask_to_plen(
8763 			    ipif->ipif_net_mask);
8764 		}
8765 		lifr++;
8766 	}
8767 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8768 	rw_exit(&ipst->ips_ill_g_lock);
8769 	ipif_refrele(orig_ipif);
8770 	mp1->b_wptr = (uchar_t *)lifr;
8771 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8772 
8773 	return (0);
8774 }
8775 
8776 /* ARGSUSED */
8777 int
8778 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8779     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8780 {
8781 	mblk_t *mp1;
8782 	int	list;
8783 	ill_t	*ill;
8784 	ipif_t	*ipif;
8785 	int	flags;
8786 	int	numlifs = 0;
8787 	size_t	lifc_bufsize;
8788 	struct	lifreq *lifr;
8789 	sa_family_t	family;
8790 	struct	sockaddr_in	*sin;
8791 	struct	sockaddr_in6	*sin6;
8792 	ill_walk_context_t	ctx;
8793 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8794 	int32_t	lifclen;
8795 	zoneid_t zoneid;
8796 	STRUCT_HANDLE(lifconf, lifc);
8797 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8798 
8799 	ip1dbg(("ip_sioctl_get_lifconf"));
8800 
8801 	ASSERT(q->q_next == NULL);
8802 
8803 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8804 
8805 	/* Existence verified in ip_wput_nondata */
8806 	mp1 = mp->b_cont->b_cont;
8807 
8808 	/*
8809 	 * An extended version of SIOCGIFCONF that takes an
8810 	 * additional address family and flags field.
8811 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8812 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8813 	 * interfaces are omitted.
8814 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8815 	 * unless LIFC_TEMPORARY is specified.
8816 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8817 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8818 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8819 	 * has priority over LIFC_NOXMIT.
8820 	 */
8821 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8822 
8823 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8824 		return (EINVAL);
8825 
8826 	/*
8827 	 * Must be (better be!) continuation of a TRANSPARENT
8828 	 * IOCTL.  We just copied in the lifconf structure.
8829 	 */
8830 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8831 
8832 	family = STRUCT_FGET(lifc, lifc_family);
8833 	flags = STRUCT_FGET(lifc, lifc_flags);
8834 
8835 	switch (family) {
8836 	case AF_UNSPEC:
8837 		/*
8838 		 * walk all ILL's.
8839 		 */
8840 		list = MAX_G_HEADS;
8841 		break;
8842 	case AF_INET:
8843 		/*
8844 		 * walk only IPV4 ILL's.
8845 		 */
8846 		list = IP_V4_G_HEAD;
8847 		break;
8848 	case AF_INET6:
8849 		/*
8850 		 * walk only IPV6 ILL's.
8851 		 */
8852 		list = IP_V6_G_HEAD;
8853 		break;
8854 	default:
8855 		return (EAFNOSUPPORT);
8856 	}
8857 
8858 	/*
8859 	 * Allocate a buffer to hold requested information.
8860 	 *
8861 	 * If lifc_len is larger than what is needed, we only
8862 	 * allocate what we will use.
8863 	 *
8864 	 * If lifc_len is smaller than what is needed, return
8865 	 * EINVAL.
8866 	 */
8867 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8868 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8869 	lifclen = STRUCT_FGET(lifc, lifc_len);
8870 	if (lifc_bufsize > lifclen) {
8871 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8872 			return (EINVAL);
8873 		else
8874 			lifc_bufsize = lifclen;
8875 	}
8876 
8877 	mp1 = mi_copyout_alloc(q, mp,
8878 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8879 	if (mp1 == NULL)
8880 		return (ENOMEM);
8881 
8882 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8883 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8884 
8885 	lifr = (struct lifreq *)mp1->b_rptr;
8886 
8887 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8888 	ill = ill_first(list, list, &ctx, ipst);
8889 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8890 		for (ipif = ill->ill_ipif; ipif != NULL;
8891 		    ipif = ipif->ipif_next) {
8892 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8893 			    !(flags & LIFC_NOXMIT))
8894 				continue;
8895 
8896 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8897 			    !(flags & LIFC_TEMPORARY))
8898 				continue;
8899 
8900 			if (((ipif->ipif_flags &
8901 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8902 			    IPIF_DEPRECATED)) ||
8903 			    IS_LOOPBACK(ill) ||
8904 			    !(ipif->ipif_flags & IPIF_UP)) &&
8905 			    (flags & LIFC_EXTERNAL_SOURCE))
8906 				continue;
8907 
8908 			if (zoneid != ipif->ipif_zoneid &&
8909 			    ipif->ipif_zoneid != ALL_ZONES &&
8910 			    (zoneid != GLOBAL_ZONEID ||
8911 			    !(flags & LIFC_ALLZONES)))
8912 				continue;
8913 
8914 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8915 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8916 					rw_exit(&ipst->ips_ill_g_lock);
8917 					return (EINVAL);
8918 				} else {
8919 					goto lif_copydone;
8920 				}
8921 			}
8922 
8923 			ipif_get_name(ipif, lifr->lifr_name,
8924 			    sizeof (lifr->lifr_name));
8925 			if (ipif->ipif_isv6) {
8926 				sin6 = (sin6_t *)&lifr->lifr_addr;
8927 				*sin6 = sin6_null;
8928 				sin6->sin6_family = AF_INET6;
8929 				sin6->sin6_addr =
8930 				    ipif->ipif_v6lcl_addr;
8931 				lifr->lifr_addrlen =
8932 				    ip_mask_to_plen_v6(
8933 				    &ipif->ipif_v6net_mask);
8934 			} else {
8935 				sin = (sin_t *)&lifr->lifr_addr;
8936 				*sin = sin_null;
8937 				sin->sin_family = AF_INET;
8938 				sin->sin_addr.s_addr =
8939 				    ipif->ipif_lcl_addr;
8940 				lifr->lifr_addrlen =
8941 				    ip_mask_to_plen(
8942 				    ipif->ipif_net_mask);
8943 			}
8944 			lifr++;
8945 		}
8946 	}
8947 lif_copydone:
8948 	rw_exit(&ipst->ips_ill_g_lock);
8949 
8950 	mp1->b_wptr = (uchar_t *)lifr;
8951 	if (STRUCT_BUF(lifc) != NULL) {
8952 		STRUCT_FSET(lifc, lifc_len,
8953 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8954 	}
8955 	return (0);
8956 }
8957 
8958 /* ARGSUSED */
8959 int
8960 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8961     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8962 {
8963 	ip_stack_t	*ipst;
8964 
8965 	if (q->q_next == NULL)
8966 		ipst = CONNQ_TO_IPST(q);
8967 	else
8968 		ipst = ILLQ_TO_IPST(q);
8969 
8970 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8971 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8972 	return (0);
8973 }
8974 
8975 static void
8976 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8977 {
8978 	ip6_asp_t *table;
8979 	size_t table_size;
8980 	mblk_t *data_mp;
8981 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8982 	ip_stack_t	*ipst;
8983 
8984 	if (q->q_next == NULL)
8985 		ipst = CONNQ_TO_IPST(q);
8986 	else
8987 		ipst = ILLQ_TO_IPST(q);
8988 
8989 	/* These two ioctls are I_STR only */
8990 	if (iocp->ioc_count == TRANSPARENT) {
8991 		miocnak(q, mp, 0, EINVAL);
8992 		return;
8993 	}
8994 
8995 	data_mp = mp->b_cont;
8996 	if (data_mp == NULL) {
8997 		/* The user passed us a NULL argument */
8998 		table = NULL;
8999 		table_size = iocp->ioc_count;
9000 	} else {
9001 		/*
9002 		 * The user provided a table.  The stream head
9003 		 * may have copied in the user data in chunks,
9004 		 * so make sure everything is pulled up
9005 		 * properly.
9006 		 */
9007 		if (MBLKL(data_mp) < iocp->ioc_count) {
9008 			mblk_t *new_data_mp;
9009 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9010 			    NULL) {
9011 				miocnak(q, mp, 0, ENOMEM);
9012 				return;
9013 			}
9014 			freemsg(data_mp);
9015 			data_mp = new_data_mp;
9016 			mp->b_cont = data_mp;
9017 		}
9018 		table = (ip6_asp_t *)data_mp->b_rptr;
9019 		table_size = iocp->ioc_count;
9020 	}
9021 
9022 	switch (iocp->ioc_cmd) {
9023 	case SIOCGIP6ADDRPOLICY:
9024 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9025 		if (iocp->ioc_rval == -1)
9026 			iocp->ioc_error = EINVAL;
9027 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9028 		else if (table != NULL &&
9029 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9030 			ip6_asp_t *src = table;
9031 			ip6_asp32_t *dst = (void *)table;
9032 			int count = table_size / sizeof (ip6_asp_t);
9033 			int i;
9034 
9035 			/*
9036 			 * We need to do an in-place shrink of the array
9037 			 * to match the alignment attributes of the
9038 			 * 32-bit ABI looking at it.
9039 			 */
9040 			/* LINTED: logical expression always true: op "||" */
9041 			ASSERT(sizeof (*src) > sizeof (*dst));
9042 			for (i = 1; i < count; i++)
9043 				bcopy(src + i, dst + i, sizeof (*dst));
9044 		}
9045 #endif
9046 		break;
9047 
9048 	case SIOCSIP6ADDRPOLICY:
9049 		ASSERT(mp->b_prev == NULL);
9050 		mp->b_prev = (void *)q;
9051 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9052 		/*
9053 		 * We pass in the datamodel here so that the ip6_asp_replace()
9054 		 * routine can handle converting from 32-bit to native formats
9055 		 * where necessary.
9056 		 *
9057 		 * A better way to handle this might be to convert the inbound
9058 		 * data structure here, and hang it off a new 'mp'; thus the
9059 		 * ip6_asp_replace() logic would always be dealing with native
9060 		 * format data structures..
9061 		 *
9062 		 * (An even simpler way to handle these ioctls is to just
9063 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9064 		 * and just recompile everything that depends on it.)
9065 		 */
9066 #endif
9067 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9068 		    iocp->ioc_flag & IOC_MODELS);
9069 		return;
9070 	}
9071 
9072 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9073 	qreply(q, mp);
9074 }
9075 
9076 static void
9077 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9078 {
9079 	mblk_t 		*data_mp;
9080 	struct dstinforeq	*dir;
9081 	uint8_t		*end, *cur;
9082 	in6_addr_t	*daddr, *saddr;
9083 	ipaddr_t	v4daddr;
9084 	ire_t		*ire;
9085 	char		*slabel, *dlabel;
9086 	boolean_t	isipv4;
9087 	int		match_ire;
9088 	ill_t		*dst_ill;
9089 	ipif_t		*src_ipif, *ire_ipif;
9090 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9091 	zoneid_t	zoneid;
9092 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9093 
9094 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9095 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9096 
9097 	/*
9098 	 * This ioctl is I_STR only, and must have a
9099 	 * data mblk following the M_IOCTL mblk.
9100 	 */
9101 	data_mp = mp->b_cont;
9102 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9103 		miocnak(q, mp, 0, EINVAL);
9104 		return;
9105 	}
9106 
9107 	if (MBLKL(data_mp) < iocp->ioc_count) {
9108 		mblk_t *new_data_mp;
9109 
9110 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9111 			miocnak(q, mp, 0, ENOMEM);
9112 			return;
9113 		}
9114 		freemsg(data_mp);
9115 		data_mp = new_data_mp;
9116 		mp->b_cont = data_mp;
9117 	}
9118 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9119 
9120 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9121 	    end - cur >= sizeof (struct dstinforeq);
9122 	    cur += sizeof (struct dstinforeq)) {
9123 		dir = (struct dstinforeq *)cur;
9124 		daddr = &dir->dir_daddr;
9125 		saddr = &dir->dir_saddr;
9126 
9127 		/*
9128 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9129 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9130 		 * and ipif_select_source[_v6]() do not.
9131 		 */
9132 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9133 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9134 
9135 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9136 		if (isipv4) {
9137 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9138 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9139 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9140 		} else {
9141 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9142 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9143 		}
9144 		if (ire == NULL) {
9145 			dir->dir_dreachable = 0;
9146 
9147 			/* move on to next dst addr */
9148 			continue;
9149 		}
9150 		dir->dir_dreachable = 1;
9151 
9152 		ire_ipif = ire->ire_ipif;
9153 		if (ire_ipif == NULL)
9154 			goto next_dst;
9155 
9156 		/*
9157 		 * We expect to get back an interface ire or a
9158 		 * gateway ire cache entry.  For both types, the
9159 		 * output interface is ire_ipif->ipif_ill.
9160 		 */
9161 		dst_ill = ire_ipif->ipif_ill;
9162 		dir->dir_dmactype = dst_ill->ill_mactype;
9163 
9164 		if (isipv4) {
9165 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9166 		} else {
9167 			src_ipif = ipif_select_source_v6(dst_ill,
9168 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9169 			    zoneid);
9170 		}
9171 		if (src_ipif == NULL)
9172 			goto next_dst;
9173 
9174 		*saddr = src_ipif->ipif_v6lcl_addr;
9175 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9176 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9177 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9178 		dir->dir_sdeprecated =
9179 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9180 		ipif_refrele(src_ipif);
9181 next_dst:
9182 		ire_refrele(ire);
9183 	}
9184 	miocack(q, mp, iocp->ioc_count, 0);
9185 }
9186 
9187 
9188 /*
9189  * Check if this is an address assigned to this machine.
9190  * Skips interfaces that are down by using ire checks.
9191  * Translates mapped addresses to v4 addresses and then
9192  * treats them as such, returning true if the v4 address
9193  * associated with this mapped address is configured.
9194  * Note: Applications will have to be careful what they do
9195  * with the response; use of mapped addresses limits
9196  * what can be done with the socket, especially with
9197  * respect to socket options and ioctls - neither IPv4
9198  * options nor IPv6 sticky options/ancillary data options
9199  * may be used.
9200  */
9201 /* ARGSUSED */
9202 int
9203 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9204     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9205 {
9206 	struct sioc_addrreq *sia;
9207 	sin_t *sin;
9208 	ire_t *ire;
9209 	mblk_t *mp1;
9210 	zoneid_t zoneid;
9211 	ip_stack_t	*ipst;
9212 
9213 	ip1dbg(("ip_sioctl_tmyaddr"));
9214 
9215 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9216 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9217 	ipst = CONNQ_TO_IPST(q);
9218 
9219 	/* Existence verified in ip_wput_nondata */
9220 	mp1 = mp->b_cont->b_cont;
9221 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9222 	sin = (sin_t *)&sia->sa_addr;
9223 	switch (sin->sin_family) {
9224 	case AF_INET6: {
9225 		sin6_t *sin6 = (sin6_t *)sin;
9226 
9227 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9228 			ipaddr_t v4_addr;
9229 
9230 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9231 			    v4_addr);
9232 			ire = ire_ctable_lookup(v4_addr, 0,
9233 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9234 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9235 		} else {
9236 			in6_addr_t v6addr;
9237 
9238 			v6addr = sin6->sin6_addr;
9239 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9240 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9241 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9242 		}
9243 		break;
9244 	}
9245 	case AF_INET: {
9246 		ipaddr_t v4addr;
9247 
9248 		v4addr = sin->sin_addr.s_addr;
9249 		ire = ire_ctable_lookup(v4addr, 0,
9250 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9251 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9252 		break;
9253 	}
9254 	default:
9255 		return (EAFNOSUPPORT);
9256 	}
9257 	if (ire != NULL) {
9258 		sia->sa_res = 1;
9259 		ire_refrele(ire);
9260 	} else {
9261 		sia->sa_res = 0;
9262 	}
9263 	return (0);
9264 }
9265 
9266 /*
9267  * Check if this is an address assigned on-link i.e. neighbor,
9268  * and makes sure it's reachable from the current zone.
9269  * Returns true for my addresses as well.
9270  * Translates mapped addresses to v4 addresses and then
9271  * treats them as such, returning true if the v4 address
9272  * associated with this mapped address is configured.
9273  * Note: Applications will have to be careful what they do
9274  * with the response; use of mapped addresses limits
9275  * what can be done with the socket, especially with
9276  * respect to socket options and ioctls - neither IPv4
9277  * options nor IPv6 sticky options/ancillary data options
9278  * may be used.
9279  */
9280 /* ARGSUSED */
9281 int
9282 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9283     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9284 {
9285 	struct sioc_addrreq *sia;
9286 	sin_t *sin;
9287 	mblk_t	*mp1;
9288 	ire_t *ire = NULL;
9289 	zoneid_t zoneid;
9290 	ip_stack_t	*ipst;
9291 
9292 	ip1dbg(("ip_sioctl_tonlink"));
9293 
9294 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9295 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9296 	ipst = CONNQ_TO_IPST(q);
9297 
9298 	/* Existence verified in ip_wput_nondata */
9299 	mp1 = mp->b_cont->b_cont;
9300 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9301 	sin = (sin_t *)&sia->sa_addr;
9302 
9303 	/*
9304 	 * Match addresses with a zero gateway field to avoid
9305 	 * routes going through a router.
9306 	 * Exclude broadcast and multicast addresses.
9307 	 */
9308 	switch (sin->sin_family) {
9309 	case AF_INET6: {
9310 		sin6_t *sin6 = (sin6_t *)sin;
9311 
9312 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9313 			ipaddr_t v4_addr;
9314 
9315 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9316 			    v4_addr);
9317 			if (!CLASSD(v4_addr)) {
9318 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9319 				    NULL, NULL, zoneid, NULL,
9320 				    MATCH_IRE_GW, ipst);
9321 			}
9322 		} else {
9323 			in6_addr_t v6addr;
9324 			in6_addr_t v6gw;
9325 
9326 			v6addr = sin6->sin6_addr;
9327 			v6gw = ipv6_all_zeros;
9328 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9329 				ire = ire_route_lookup_v6(&v6addr, 0,
9330 				    &v6gw, 0, NULL, NULL, zoneid,
9331 				    NULL, MATCH_IRE_GW, ipst);
9332 			}
9333 		}
9334 		break;
9335 	}
9336 	case AF_INET: {
9337 		ipaddr_t v4addr;
9338 
9339 		v4addr = sin->sin_addr.s_addr;
9340 		if (!CLASSD(v4addr)) {
9341 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9342 			    NULL, NULL, zoneid, NULL,
9343 			    MATCH_IRE_GW, ipst);
9344 		}
9345 		break;
9346 	}
9347 	default:
9348 		return (EAFNOSUPPORT);
9349 	}
9350 	sia->sa_res = 0;
9351 	if (ire != NULL) {
9352 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9353 		    IRE_LOCAL|IRE_LOOPBACK)) {
9354 			sia->sa_res = 1;
9355 		}
9356 		ire_refrele(ire);
9357 	}
9358 	return (0);
9359 }
9360 
9361 /*
9362  * TBD: implement when kernel maintaines a list of site prefixes.
9363  */
9364 /* ARGSUSED */
9365 int
9366 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9367     ip_ioctl_cmd_t *ipip, void *ifreq)
9368 {
9369 	return (ENXIO);
9370 }
9371 
9372 /* ARGSUSED */
9373 int
9374 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9375     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9376 {
9377 	ill_t  		*ill;
9378 	mblk_t		*mp1;
9379 	conn_t		*connp;
9380 	boolean_t	success;
9381 
9382 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9383 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9384 	/* ioctl comes down on an conn */
9385 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9386 	connp = Q_TO_CONN(q);
9387 
9388 	mp->b_datap->db_type = M_IOCTL;
9389 
9390 	/*
9391 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9392 	 * The original mp contains contaminated b_next values due to 'mi',
9393 	 * which is needed to do the mi_copy_done. Unfortunately if we
9394 	 * send down the original mblk itself and if we are popped due to an
9395 	 * an unplumb before the response comes back from tunnel,
9396 	 * the streamhead (which does a freemsg) will see this contaminated
9397 	 * message and the assertion in freemsg about non-null b_next/b_prev
9398 	 * will panic a DEBUG kernel.
9399 	 */
9400 	mp1 = copymsg(mp);
9401 	if (mp1 == NULL)
9402 		return (ENOMEM);
9403 
9404 	ill = ipif->ipif_ill;
9405 	mutex_enter(&connp->conn_lock);
9406 	mutex_enter(&ill->ill_lock);
9407 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9408 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9409 		    mp, 0);
9410 	} else {
9411 		success = ill_pending_mp_add(ill, connp, mp);
9412 	}
9413 	mutex_exit(&ill->ill_lock);
9414 	mutex_exit(&connp->conn_lock);
9415 
9416 	if (success) {
9417 		ip1dbg(("sending down tunparam request "));
9418 		putnext(ill->ill_wq, mp1);
9419 		return (EINPROGRESS);
9420 	} else {
9421 		/* The conn has started closing */
9422 		freemsg(mp1);
9423 		return (EINTR);
9424 	}
9425 }
9426 
9427 /*
9428  * ARP IOCTLs.
9429  * How does IP get in the business of fronting ARP configuration/queries?
9430  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9431  * are by tradition passed in through a datagram socket.  That lands in IP.
9432  * As it happens, this is just as well since the interface is quite crude in
9433  * that it passes in no information about protocol or hardware types, or
9434  * interface association.  After making the protocol assumption, IP is in
9435  * the position to look up the name of the ILL, which ARP will need, and
9436  * format a request that can be handled by ARP.  The request is passed up
9437  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9438  * back a response.  ARP supports its own set of more general IOCTLs, in
9439  * case anyone is interested.
9440  */
9441 /* ARGSUSED */
9442 int
9443 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9444     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9445 {
9446 	mblk_t *mp1;
9447 	mblk_t *mp2;
9448 	mblk_t *pending_mp;
9449 	ipaddr_t ipaddr;
9450 	area_t *area;
9451 	struct iocblk *iocp;
9452 	conn_t *connp;
9453 	struct arpreq *ar;
9454 	struct xarpreq *xar;
9455 	int flags, alength;
9456 	char *lladdr;
9457 	ip_stack_t	*ipst;
9458 	ill_t *ill = ipif->ipif_ill;
9459 	boolean_t if_arp_ioctl = B_FALSE;
9460 
9461 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9462 	connp = Q_TO_CONN(q);
9463 	ipst = connp->conn_netstack->netstack_ip;
9464 
9465 	if (ipip->ipi_cmd_type == XARP_CMD) {
9466 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9467 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9468 		ar = NULL;
9469 
9470 		flags = xar->xarp_flags;
9471 		lladdr = LLADDR(&xar->xarp_ha);
9472 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9473 		/*
9474 		 * Validate against user's link layer address length
9475 		 * input and name and addr length limits.
9476 		 */
9477 		alength = ill->ill_phys_addr_length;
9478 		if (ipip->ipi_cmd == SIOCSXARP) {
9479 			if (alength != xar->xarp_ha.sdl_alen ||
9480 			    (alength + xar->xarp_ha.sdl_nlen >
9481 			    sizeof (xar->xarp_ha.sdl_data)))
9482 				return (EINVAL);
9483 		}
9484 	} else {
9485 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9486 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9487 		xar = NULL;
9488 
9489 		flags = ar->arp_flags;
9490 		lladdr = ar->arp_ha.sa_data;
9491 		/*
9492 		 * Theoretically, the sa_family could tell us what link
9493 		 * layer type this operation is trying to deal with. By
9494 		 * common usage AF_UNSPEC means ethernet. We'll assume
9495 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9496 		 * for now. Our new SIOC*XARP ioctls can be used more
9497 		 * generally.
9498 		 *
9499 		 * If the underlying media happens to have a non 6 byte
9500 		 * address, arp module will fail set/get, but the del
9501 		 * operation will succeed.
9502 		 */
9503 		alength = 6;
9504 		if ((ipip->ipi_cmd != SIOCDARP) &&
9505 		    (alength != ill->ill_phys_addr_length)) {
9506 			return (EINVAL);
9507 		}
9508 	}
9509 
9510 	/*
9511 	 * We are going to pass up to ARP a packet chain that looks
9512 	 * like:
9513 	 *
9514 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9515 	 *
9516 	 * Get a copy of the original IOCTL mblk to head the chain,
9517 	 * to be sent up (in mp1). Also get another copy to store
9518 	 * in the ill_pending_mp list, for matching the response
9519 	 * when it comes back from ARP.
9520 	 */
9521 	mp1 = copyb(mp);
9522 	pending_mp = copymsg(mp);
9523 	if (mp1 == NULL || pending_mp == NULL) {
9524 		if (mp1 != NULL)
9525 			freeb(mp1);
9526 		if (pending_mp != NULL)
9527 			inet_freemsg(pending_mp);
9528 		return (ENOMEM);
9529 	}
9530 
9531 	ipaddr = sin->sin_addr.s_addr;
9532 
9533 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9534 	    (caddr_t)&ipaddr);
9535 	if (mp2 == NULL) {
9536 		freeb(mp1);
9537 		inet_freemsg(pending_mp);
9538 		return (ENOMEM);
9539 	}
9540 	/* Put together the chain. */
9541 	mp1->b_cont = mp2;
9542 	mp1->b_datap->db_type = M_IOCTL;
9543 	mp2->b_cont = mp;
9544 	mp2->b_datap->db_type = M_DATA;
9545 
9546 	iocp = (struct iocblk *)mp1->b_rptr;
9547 
9548 	/*
9549 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9550 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9551 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9552 	 * ioc_count field; set ioc_count to be correct.
9553 	 */
9554 	iocp->ioc_count = MBLKL(mp1->b_cont);
9555 
9556 	/*
9557 	 * Set the proper command in the ARP message.
9558 	 * Convert the SIOC{G|S|D}ARP calls into our
9559 	 * AR_ENTRY_xxx calls.
9560 	 */
9561 	area = (area_t *)mp2->b_rptr;
9562 	switch (iocp->ioc_cmd) {
9563 	case SIOCDARP:
9564 	case SIOCDXARP:
9565 		/*
9566 		 * We defer deleting the corresponding IRE until
9567 		 * we return from arp.
9568 		 */
9569 		area->area_cmd = AR_ENTRY_DELETE;
9570 		area->area_proto_mask_offset = 0;
9571 		break;
9572 	case SIOCGARP:
9573 	case SIOCGXARP:
9574 		area->area_cmd = AR_ENTRY_SQUERY;
9575 		area->area_proto_mask_offset = 0;
9576 		break;
9577 	case SIOCSARP:
9578 	case SIOCSXARP:
9579 		/*
9580 		 * Delete the corresponding ire to make sure IP will
9581 		 * pick up any change from arp.
9582 		 */
9583 		if (!if_arp_ioctl) {
9584 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9585 		} else {
9586 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9587 			if (ipif != NULL) {
9588 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9589 				    ipst);
9590 				ipif_refrele(ipif);
9591 			}
9592 		}
9593 		break;
9594 	}
9595 	iocp->ioc_cmd = area->area_cmd;
9596 
9597 	/*
9598 	 * Fill in the rest of the ARP operation fields.
9599 	 */
9600 	area->area_hw_addr_length = alength;
9601 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9602 
9603 	/* Translate the flags. */
9604 	if (flags & ATF_PERM)
9605 		area->area_flags |= ACE_F_PERMANENT;
9606 	if (flags & ATF_PUBL)
9607 		area->area_flags |= ACE_F_PUBLISH;
9608 	if (flags & ATF_AUTHORITY)
9609 		area->area_flags |= ACE_F_AUTHORITY;
9610 
9611 	/*
9612 	 * Before sending 'mp' to ARP, we have to clear the b_next
9613 	 * and b_prev. Otherwise if STREAMS encounters such a message
9614 	 * in freemsg(), (because ARP can close any time) it can cause
9615 	 * a panic. But mi code needs the b_next and b_prev values of
9616 	 * mp->b_cont, to complete the ioctl. So we store it here
9617 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9618 	 * when the response comes down from ARP.
9619 	 */
9620 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9621 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9622 	mp->b_cont->b_next = NULL;
9623 	mp->b_cont->b_prev = NULL;
9624 
9625 	mutex_enter(&connp->conn_lock);
9626 	mutex_enter(&ill->ill_lock);
9627 	/* conn has not yet started closing, hence this can't fail */
9628 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9629 	mutex_exit(&ill->ill_lock);
9630 	mutex_exit(&connp->conn_lock);
9631 
9632 	/*
9633 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9634 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9635 	 */
9636 	putnext(ill->ill_rq, mp1);
9637 	return (EINPROGRESS);
9638 }
9639 
9640 /*
9641  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9642  * the associated sin and refhold and return the associated ipif via `ci'.
9643  */
9644 int
9645 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9646     cmd_info_t *ci, ipsq_func_t func)
9647 {
9648 	mblk_t	*mp1;
9649 	int	err;
9650 	sin_t	*sin;
9651 	conn_t	*connp;
9652 	ipif_t	*ipif;
9653 	ire_t	*ire = NULL;
9654 	ill_t	*ill = NULL;
9655 	boolean_t exists;
9656 	ip_stack_t *ipst;
9657 	struct arpreq *ar;
9658 	struct xarpreq *xar;
9659 	struct sockaddr_dl *sdl;
9660 
9661 	/* ioctl comes down on a conn */
9662 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9663 	connp = Q_TO_CONN(q);
9664 	if (connp->conn_af_isv6)
9665 		return (ENXIO);
9666 
9667 	ipst = connp->conn_netstack->netstack_ip;
9668 
9669 	/* Verified in ip_wput_nondata */
9670 	mp1 = mp->b_cont->b_cont;
9671 
9672 	if (ipip->ipi_cmd_type == XARP_CMD) {
9673 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9674 		xar = (struct xarpreq *)mp1->b_rptr;
9675 		sin = (sin_t *)&xar->xarp_pa;
9676 		sdl = &xar->xarp_ha;
9677 
9678 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9679 			return (ENXIO);
9680 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9681 			return (EINVAL);
9682 	} else {
9683 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9684 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9685 		ar = (struct arpreq *)mp1->b_rptr;
9686 		sin = (sin_t *)&ar->arp_pa;
9687 	}
9688 
9689 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9690 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9691 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9692 		    mp, func, &err, ipst);
9693 		if (ipif == NULL)
9694 			return (err);
9695 		if (ipif->ipif_id != 0 ||
9696 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9697 			ipif_refrele(ipif);
9698 			return (ENXIO);
9699 		}
9700 	} else {
9701 		/*
9702 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9703 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9704 		 * case, a simple forwarding table lookup will return the
9705 		 * IRE_IF_RESOLVER for the first interface in the group, which
9706 		 * might not be the interface on which the requested IP
9707 		 * address was resolved due to the ill selection algorithm
9708 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9709 		 * lookup first: if the IRE cache entry for the IP address is
9710 		 * still there, it will contain the ill pointer for the right
9711 		 * interface, so we use that. If the cache entry has been
9712 		 * flushed, we fall back to the forwarding table lookup. This
9713 		 * should be rare enough since IRE cache entries have a longer
9714 		 * life expectancy than ARP cache entries.
9715 		 */
9716 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9717 		    ipst);
9718 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9719 		    ((ill = ire_to_ill(ire)) == NULL) ||
9720 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9721 			if (ire != NULL)
9722 				ire_refrele(ire);
9723 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9724 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9725 			    NULL, MATCH_IRE_TYPE, ipst);
9726 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9727 
9728 				if (ire != NULL)
9729 					ire_refrele(ire);
9730 				return (ENXIO);
9731 			}
9732 		}
9733 		ASSERT(ire != NULL && ill != NULL);
9734 		ipif = ill->ill_ipif;
9735 		ipif_refhold(ipif);
9736 		ire_refrele(ire);
9737 	}
9738 	ci->ci_sin = sin;
9739 	ci->ci_ipif = ipif;
9740 	return (0);
9741 }
9742 
9743 /*
9744  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9745  * atomically set/clear the muxids. Also complete the ioctl by acking or
9746  * naking it.  Note that the code is structured such that the link type,
9747  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9748  * its clones use the persistent link, while pppd(1M) and perhaps many
9749  * other daemons may use non-persistent link.  When combined with some
9750  * ill_t states, linking and unlinking lower streams may be used as
9751  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9752  */
9753 /* ARGSUSED */
9754 void
9755 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9756 {
9757 	mblk_t		*mp1, *mp2;
9758 	struct linkblk	*li;
9759 	struct ipmx_s	*ipmxp;
9760 	ill_t		*ill;
9761 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9762 	int		err = 0;
9763 	boolean_t	entered_ipsq = B_FALSE;
9764 	boolean_t	islink;
9765 	ip_stack_t	*ipst;
9766 
9767 	if (CONN_Q(q))
9768 		ipst = CONNQ_TO_IPST(q);
9769 	else
9770 		ipst = ILLQ_TO_IPST(q);
9771 
9772 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9773 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9774 
9775 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9776 
9777 	mp1 = mp->b_cont;	/* This is the linkblk info */
9778 	li = (struct linkblk *)mp1->b_rptr;
9779 
9780 	/*
9781 	 * ARP has added this special mblk, and the utility is asking us
9782 	 * to perform consistency checks, and also atomically set the
9783 	 * muxid. Ifconfig is an example.  It achieves this by using
9784 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9785 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9786 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9787 	 * and other comments in this routine for more details.
9788 	 */
9789 	mp2 = mp1->b_cont;	/* This is added by ARP */
9790 
9791 	/*
9792 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9793 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9794 	 * get the special mblk above.  For backward compatibility, we
9795 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9796 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9797 	 * not atomic, and can leave the streams unplumbable if the utility
9798 	 * is interrupted before it does the SIOCSLIFMUXID.
9799 	 */
9800 	if (mp2 == NULL) {
9801 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9802 		if (err == EINPROGRESS)
9803 			return;
9804 		goto done;
9805 	}
9806 
9807 	/*
9808 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9809 	 * ARP has appended this last mblk to tell us whether the lower stream
9810 	 * is an arp-dev stream or an IP module stream.
9811 	 */
9812 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9813 	if (ipmxp->ipmx_arpdev_stream) {
9814 		/*
9815 		 * The lower stream is the arp-dev stream.
9816 		 */
9817 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9818 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9819 		if (ill == NULL) {
9820 			if (err == EINPROGRESS)
9821 				return;
9822 			err = EINVAL;
9823 			goto done;
9824 		}
9825 
9826 		if (ipsq == NULL) {
9827 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9828 			    NEW_OP, B_TRUE);
9829 			if (ipsq == NULL) {
9830 				ill_refrele(ill);
9831 				return;
9832 			}
9833 			entered_ipsq = B_TRUE;
9834 		}
9835 		ASSERT(IAM_WRITER_ILL(ill));
9836 		ill_refrele(ill);
9837 
9838 		/*
9839 		 * To ensure consistency between IP and ARP, the following
9840 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9841 		 * This is because the muxid's are stored in the IP stream on
9842 		 * the ill.
9843 		 *
9844 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9845 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9846 		 * not yet plinked, and it also checks that the corresponding
9847 		 * IP stream is already plinked.
9848 		 *
9849 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9850 		 * punlinking the IP stream. IP does not allow punlink of the
9851 		 * IP stream unless the arp stream has been punlinked.
9852 		 */
9853 		if ((islink &&
9854 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9855 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9856 			err = EINVAL;
9857 			goto done;
9858 		}
9859 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9860 	} else {
9861 		/*
9862 		 * The lower stream is probably an IP module stream.  Do
9863 		 * consistency checking.
9864 		 */
9865 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9866 		if (err == EINPROGRESS)
9867 			return;
9868 	}
9869 done:
9870 	if (err == 0)
9871 		miocack(q, mp, 0, 0);
9872 	else
9873 		miocnak(q, mp, 0, err);
9874 
9875 	/* Conn was refheld in ip_sioctl_copyin_setup */
9876 	if (CONN_Q(q))
9877 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9878 	if (entered_ipsq)
9879 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9880 }
9881 
9882 /*
9883  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9884  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9885  * module stream).  If `doconsist' is set, then do the extended consistency
9886  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9887  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9888  * an error code on failure.
9889  */
9890 static int
9891 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9892     struct linkblk *li, boolean_t doconsist)
9893 {
9894 	ill_t  		*ill;
9895 	queue_t		*ipwq, *dwq;
9896 	const char	*name;
9897 	struct qinit	*qinfo;
9898 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9899 	boolean_t	entered_ipsq = B_FALSE;
9900 
9901 	/*
9902 	 * Walk the lower stream to verify it's the IP module stream.
9903 	 * The IP module is identified by its name, wput function,
9904 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9905 	 * (li->l_qbot) will not vanish until this ioctl completes.
9906 	 */
9907 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9908 		qinfo = ipwq->q_qinfo;
9909 		name = qinfo->qi_minfo->mi_idname;
9910 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9911 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9912 			break;
9913 		}
9914 	}
9915 
9916 	/*
9917 	 * If this isn't an IP module stream, bail.
9918 	 */
9919 	if (ipwq == NULL)
9920 		return (0);
9921 
9922 	ill = ipwq->q_ptr;
9923 	ASSERT(ill != NULL);
9924 
9925 	if (ipsq == NULL) {
9926 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9927 		    NEW_OP, B_TRUE);
9928 		if (ipsq == NULL)
9929 			return (EINPROGRESS);
9930 		entered_ipsq = B_TRUE;
9931 	}
9932 	ASSERT(IAM_WRITER_ILL(ill));
9933 
9934 	if (doconsist) {
9935 		/*
9936 		 * Consistency checking requires that I_{P}LINK occurs
9937 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9938 		 * occurs prior to clearing ill_arp_muxid.
9939 		 */
9940 		if ((islink && ill->ill_ip_muxid != 0) ||
9941 		    (!islink && ill->ill_arp_muxid != 0)) {
9942 			if (entered_ipsq)
9943 				ipsq_exit(ipsq, B_TRUE, B_TRUE);
9944 			return (EINVAL);
9945 		}
9946 	}
9947 
9948 	/*
9949 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9950 	 * the read queue of the module immediately below IP in the ill.
9951 	 * These are used during the capability negotiation below.
9952 	 */
9953 	ill->ill_lmod_rq = NULL;
9954 	ill->ill_lmod_cnt = 0;
9955 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9956 		ill->ill_lmod_rq = RD(dwq);
9957 		for (; dwq != NULL; dwq = dwq->q_next)
9958 			ill->ill_lmod_cnt++;
9959 	}
9960 
9961 	if (doconsist)
9962 		ill->ill_ip_muxid = islink ? li->l_index : 0;
9963 
9964 	/*
9965 	 * If there's at least one up ipif on this ill, then we're bound to
9966 	 * the underlying driver via DLPI.  In that case, renegotiate
9967 	 * capabilities to account for any possible change in modules
9968 	 * interposed between IP and the driver.
9969 	 */
9970 	if (ill->ill_ipif_up_count > 0) {
9971 		if (islink)
9972 			ill_capability_probe(ill);
9973 		else
9974 			ill_capability_reset(ill);
9975 	}
9976 
9977 	if (entered_ipsq)
9978 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9979 
9980 	return (0);
9981 }
9982 
9983 /*
9984  * Search the ioctl command in the ioctl tables and return a pointer
9985  * to the ioctl command information. The ioctl command tables are
9986  * static and fully populated at compile time.
9987  */
9988 ip_ioctl_cmd_t *
9989 ip_sioctl_lookup(int ioc_cmd)
9990 {
9991 	int index;
9992 	ip_ioctl_cmd_t *ipip;
9993 	ip_ioctl_cmd_t *ipip_end;
9994 
9995 	if (ioc_cmd == IPI_DONTCARE)
9996 		return (NULL);
9997 
9998 	/*
9999 	 * Do a 2 step search. First search the indexed table
10000 	 * based on the least significant byte of the ioctl cmd.
10001 	 * If we don't find a match, then search the misc table
10002 	 * serially.
10003 	 */
10004 	index = ioc_cmd & 0xFF;
10005 	if (index < ip_ndx_ioctl_count) {
10006 		ipip = &ip_ndx_ioctl_table[index];
10007 		if (ipip->ipi_cmd == ioc_cmd) {
10008 			/* Found a match in the ndx table */
10009 			return (ipip);
10010 		}
10011 	}
10012 
10013 	/* Search the misc table */
10014 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10015 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10016 		if (ipip->ipi_cmd == ioc_cmd)
10017 			/* Found a match in the misc table */
10018 			return (ipip);
10019 	}
10020 
10021 	return (NULL);
10022 }
10023 
10024 /*
10025  * Wrapper function for resuming deferred ioctl processing
10026  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10027  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10028  */
10029 /* ARGSUSED */
10030 void
10031 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10032     void *dummy_arg)
10033 {
10034 	ip_sioctl_copyin_setup(q, mp);
10035 }
10036 
10037 /*
10038  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10039  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10040  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10041  * We establish here the size of the block to be copied in.  mi_copyin
10042  * arranges for this to happen, an processing continues in ip_wput with
10043  * an M_IOCDATA message.
10044  */
10045 void
10046 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10047 {
10048 	int	copyin_size;
10049 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10050 	ip_ioctl_cmd_t *ipip;
10051 	cred_t *cr;
10052 	ip_stack_t	*ipst;
10053 
10054 	if (CONN_Q(q))
10055 		ipst = CONNQ_TO_IPST(q);
10056 	else
10057 		ipst = ILLQ_TO_IPST(q);
10058 
10059 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10060 	if (ipip == NULL) {
10061 		/*
10062 		 * The ioctl is not one we understand or own.
10063 		 * Pass it along to be processed down stream,
10064 		 * if this is a module instance of IP, else nak
10065 		 * the ioctl.
10066 		 */
10067 		if (q->q_next == NULL) {
10068 			goto nak;
10069 		} else {
10070 			putnext(q, mp);
10071 			return;
10072 		}
10073 	}
10074 
10075 	/*
10076 	 * If this is deferred, then we will do all the checks when we
10077 	 * come back.
10078 	 */
10079 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10080 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10081 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10082 		return;
10083 	}
10084 
10085 	/*
10086 	 * Only allow a very small subset of IP ioctls on this stream if
10087 	 * IP is a module and not a driver. Allowing ioctls to be processed
10088 	 * in this case may cause assert failures or data corruption.
10089 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10090 	 * ioctls allowed on an IP module stream, after which this stream
10091 	 * normally becomes a multiplexor (at which time the stream head
10092 	 * will fail all ioctls).
10093 	 */
10094 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10095 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10096 			/*
10097 			 * Pass common Streams ioctls which the IP
10098 			 * module does not own or consume along to
10099 			 * be processed down stream.
10100 			 */
10101 			putnext(q, mp);
10102 			return;
10103 		} else {
10104 			goto nak;
10105 		}
10106 	}
10107 
10108 	/* Make sure we have ioctl data to process. */
10109 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10110 		goto nak;
10111 
10112 	/*
10113 	 * Prefer dblk credential over ioctl credential; some synthesized
10114 	 * ioctls have kcred set because there's no way to crhold()
10115 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10116 	 * the framework; the caller of ioctl needs to hold the reference
10117 	 * for the duration of the call).
10118 	 */
10119 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10120 
10121 	/* Make sure normal users don't send down privileged ioctls */
10122 	if ((ipip->ipi_flags & IPI_PRIV) &&
10123 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10124 		/* We checked the privilege earlier but log it here */
10125 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10126 		return;
10127 	}
10128 
10129 	/*
10130 	 * The ioctl command tables can only encode fixed length
10131 	 * ioctl data. If the length is variable, the table will
10132 	 * encode the length as zero. Such special cases are handled
10133 	 * below in the switch.
10134 	 */
10135 	if (ipip->ipi_copyin_size != 0) {
10136 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10137 		return;
10138 	}
10139 
10140 	switch (iocp->ioc_cmd) {
10141 	case O_SIOCGIFCONF:
10142 	case SIOCGIFCONF:
10143 		/*
10144 		 * This IOCTL is hilarious.  See comments in
10145 		 * ip_sioctl_get_ifconf for the story.
10146 		 */
10147 		if (iocp->ioc_count == TRANSPARENT)
10148 			copyin_size = SIZEOF_STRUCT(ifconf,
10149 			    iocp->ioc_flag);
10150 		else
10151 			copyin_size = iocp->ioc_count;
10152 		mi_copyin(q, mp, NULL, copyin_size);
10153 		return;
10154 
10155 	case O_SIOCGLIFCONF:
10156 	case SIOCGLIFCONF:
10157 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10158 		mi_copyin(q, mp, NULL, copyin_size);
10159 		return;
10160 
10161 	case SIOCGLIFSRCOF:
10162 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10163 		mi_copyin(q, mp, NULL, copyin_size);
10164 		return;
10165 	case SIOCGIP6ADDRPOLICY:
10166 		ip_sioctl_ip6addrpolicy(q, mp);
10167 		ip6_asp_table_refrele(ipst);
10168 		return;
10169 
10170 	case SIOCSIP6ADDRPOLICY:
10171 		ip_sioctl_ip6addrpolicy(q, mp);
10172 		return;
10173 
10174 	case SIOCGDSTINFO:
10175 		ip_sioctl_dstinfo(q, mp);
10176 		ip6_asp_table_refrele(ipst);
10177 		return;
10178 
10179 	case I_PLINK:
10180 	case I_PUNLINK:
10181 	case I_LINK:
10182 	case I_UNLINK:
10183 		/*
10184 		 * We treat non-persistent link similarly as the persistent
10185 		 * link case, in terms of plumbing/unplumbing, as well as
10186 		 * dynamic re-plumbing events indicator.  See comments
10187 		 * in ip_sioctl_plink() for more.
10188 		 *
10189 		 * Request can be enqueued in the 'ipsq' while waiting
10190 		 * to become exclusive. So bump up the conn ref.
10191 		 */
10192 		if (CONN_Q(q))
10193 			CONN_INC_REF(Q_TO_CONN(q));
10194 		ip_sioctl_plink(NULL, q, mp, NULL);
10195 		return;
10196 
10197 	case ND_GET:
10198 	case ND_SET:
10199 		/*
10200 		 * Use of the nd table requires holding the reader lock.
10201 		 * Modifying the nd table thru nd_load/nd_unload requires
10202 		 * the writer lock.
10203 		 */
10204 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10205 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10206 			rw_exit(&ipst->ips_ip_g_nd_lock);
10207 
10208 			if (iocp->ioc_error)
10209 				iocp->ioc_count = 0;
10210 			mp->b_datap->db_type = M_IOCACK;
10211 			qreply(q, mp);
10212 			return;
10213 		}
10214 		rw_exit(&ipst->ips_ip_g_nd_lock);
10215 		/*
10216 		 * We don't understand this subioctl of ND_GET / ND_SET.
10217 		 * Maybe intended for some driver / module below us
10218 		 */
10219 		if (q->q_next) {
10220 			putnext(q, mp);
10221 		} else {
10222 			iocp->ioc_error = ENOENT;
10223 			mp->b_datap->db_type = M_IOCNAK;
10224 			iocp->ioc_count = 0;
10225 			qreply(q, mp);
10226 		}
10227 		return;
10228 
10229 	case IP_IOCTL:
10230 		ip_wput_ioctl(q, mp);
10231 		return;
10232 	default:
10233 		cmn_err(CE_PANIC, "should not happen ");
10234 	}
10235 nak:
10236 	if (mp->b_cont != NULL) {
10237 		freemsg(mp->b_cont);
10238 		mp->b_cont = NULL;
10239 	}
10240 	iocp->ioc_error = EINVAL;
10241 	mp->b_datap->db_type = M_IOCNAK;
10242 	iocp->ioc_count = 0;
10243 	qreply(q, mp);
10244 }
10245 
10246 /* ip_wput hands off ARP IOCTL responses to us */
10247 void
10248 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10249 {
10250 	struct arpreq *ar;
10251 	struct xarpreq *xar;
10252 	area_t	*area;
10253 	mblk_t	*area_mp;
10254 	struct iocblk *iocp;
10255 	mblk_t	*orig_ioc_mp, *tmp;
10256 	struct iocblk	*orig_iocp;
10257 	ill_t *ill;
10258 	conn_t *connp = NULL;
10259 	uint_t ioc_id;
10260 	mblk_t *pending_mp;
10261 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10262 	int *flagsp;
10263 	char *storage = NULL;
10264 	sin_t *sin;
10265 	ipaddr_t addr;
10266 	int err;
10267 	ip_stack_t *ipst;
10268 
10269 	ill = q->q_ptr;
10270 	ASSERT(ill != NULL);
10271 	ipst = ill->ill_ipst;
10272 
10273 	/*
10274 	 * We should get back from ARP a packet chain that looks like:
10275 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10276 	 */
10277 	if (!(area_mp = mp->b_cont) ||
10278 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10279 	    !(orig_ioc_mp = area_mp->b_cont) ||
10280 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10281 		freemsg(mp);
10282 		return;
10283 	}
10284 
10285 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10286 
10287 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10288 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10289 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10290 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10291 		x_arp_ioctl = B_TRUE;
10292 		xar = (struct xarpreq *)tmp->b_rptr;
10293 		sin = (sin_t *)&xar->xarp_pa;
10294 		flagsp = &xar->xarp_flags;
10295 		storage = xar->xarp_ha.sdl_data;
10296 		if (xar->xarp_ha.sdl_nlen != 0)
10297 			ifx_arp_ioctl = B_TRUE;
10298 	} else {
10299 		ar = (struct arpreq *)tmp->b_rptr;
10300 		sin = (sin_t *)&ar->arp_pa;
10301 		flagsp = &ar->arp_flags;
10302 		storage = ar->arp_ha.sa_data;
10303 	}
10304 
10305 	iocp = (struct iocblk *)mp->b_rptr;
10306 
10307 	/*
10308 	 * Pick out the originating queue based on the ioc_id.
10309 	 */
10310 	ioc_id = iocp->ioc_id;
10311 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10312 	if (pending_mp == NULL) {
10313 		ASSERT(connp == NULL);
10314 		inet_freemsg(mp);
10315 		return;
10316 	}
10317 	ASSERT(connp != NULL);
10318 	q = CONNP_TO_WQ(connp);
10319 
10320 	/* Uncouple the internally generated IOCTL from the original one */
10321 	area = (area_t *)area_mp->b_rptr;
10322 	area_mp->b_cont = NULL;
10323 
10324 	/*
10325 	 * Restore the b_next and b_prev used by mi code. This is needed
10326 	 * to complete the ioctl using mi* functions. We stored them in
10327 	 * the pending mp prior to sending the request to ARP.
10328 	 */
10329 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10330 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10331 	inet_freemsg(pending_mp);
10332 
10333 	/*
10334 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10335 	 * Catch the case where there is an IRE_CACHE by no entry in the
10336 	 * arp table.
10337 	 */
10338 	addr = sin->sin_addr.s_addr;
10339 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10340 		ire_t			*ire;
10341 		dl_unitdata_req_t	*dlup;
10342 		mblk_t			*llmp;
10343 		int			addr_len;
10344 		ill_t			*ipsqill = NULL;
10345 
10346 		if (ifx_arp_ioctl) {
10347 			/*
10348 			 * There's no need to lookup the ill, since
10349 			 * we've already done that when we started
10350 			 * processing the ioctl and sent the message
10351 			 * to ARP on that ill.  So use the ill that
10352 			 * is stored in q->q_ptr.
10353 			 */
10354 			ipsqill = ill;
10355 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10356 			    ipsqill->ill_ipif, ALL_ZONES,
10357 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10358 		} else {
10359 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10360 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10361 			if (ire != NULL)
10362 				ipsqill = ire_to_ill(ire);
10363 		}
10364 
10365 		if ((x_arp_ioctl) && (ipsqill != NULL))
10366 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10367 
10368 		if (ire != NULL) {
10369 			/*
10370 			 * Since the ire obtained from cachetable is used for
10371 			 * mac addr copying below, treat an incomplete ire as if
10372 			 * as if we never found it.
10373 			 */
10374 			if (ire->ire_nce != NULL &&
10375 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10376 				ire_refrele(ire);
10377 				ire = NULL;
10378 				ipsqill = NULL;
10379 				goto errack;
10380 			}
10381 			*flagsp = ATF_INUSE;
10382 			llmp = (ire->ire_nce != NULL ?
10383 			    ire->ire_nce->nce_res_mp : NULL);
10384 			if (llmp != NULL && ipsqill != NULL) {
10385 				uchar_t *macaddr;
10386 
10387 				addr_len = ipsqill->ill_phys_addr_length;
10388 				if (x_arp_ioctl && ((addr_len +
10389 				    ipsqill->ill_name_length) >
10390 				    sizeof (xar->xarp_ha.sdl_data))) {
10391 					ire_refrele(ire);
10392 					freemsg(mp);
10393 					ip_ioctl_finish(q, orig_ioc_mp,
10394 					    EINVAL, NO_COPYOUT, NULL);
10395 					return;
10396 				}
10397 				*flagsp |= ATF_COM;
10398 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10399 				if (ipsqill->ill_sap_length < 0)
10400 					macaddr = llmp->b_rptr +
10401 					    dlup->dl_dest_addr_offset;
10402 				else
10403 					macaddr = llmp->b_rptr +
10404 					    dlup->dl_dest_addr_offset +
10405 					    ipsqill->ill_sap_length;
10406 				/*
10407 				 * For SIOCGARP, MAC address length
10408 				 * validation has already been done
10409 				 * before the ioctl was issued to ARP to
10410 				 * allow it to progress only on 6 byte
10411 				 * addressable (ethernet like) media. Thus
10412 				 * the mac address copying can not overwrite
10413 				 * the sa_data area below.
10414 				 */
10415 				bcopy(macaddr, storage, addr_len);
10416 			}
10417 			/* Ditch the internal IOCTL. */
10418 			freemsg(mp);
10419 			ire_refrele(ire);
10420 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10421 			return;
10422 		}
10423 	}
10424 
10425 	/*
10426 	 * Delete the coresponding IRE_CACHE if any.
10427 	 * Reset the error if there was one (in case there was no entry
10428 	 * in arp.)
10429 	 */
10430 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10431 		ipif_t *ipintf = NULL;
10432 
10433 		if (ifx_arp_ioctl) {
10434 			/*
10435 			 * There's no need to lookup the ill, since
10436 			 * we've already done that when we started
10437 			 * processing the ioctl and sent the message
10438 			 * to ARP on that ill.  So use the ill that
10439 			 * is stored in q->q_ptr.
10440 			 */
10441 			ipintf = ill->ill_ipif;
10442 		}
10443 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10444 			/*
10445 			 * The address in "addr" may be an entry for a
10446 			 * router. If that's true, then any off-net
10447 			 * IRE_CACHE entries that go through the router
10448 			 * with address "addr" must be clobbered. Use
10449 			 * ire_walk to achieve this goal.
10450 			 */
10451 			if (ifx_arp_ioctl)
10452 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10453 				    ire_delete_cache_gw, (char *)&addr, ill);
10454 			else
10455 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10456 				    ALL_ZONES, ipst);
10457 			iocp->ioc_error = 0;
10458 		}
10459 	}
10460 errack:
10461 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10462 		err = iocp->ioc_error;
10463 		freemsg(mp);
10464 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10465 		return;
10466 	}
10467 
10468 	/*
10469 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10470 	 * the area_t into the struct {x}arpreq.
10471 	 */
10472 	if (x_arp_ioctl) {
10473 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10474 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10475 		    sizeof (xar->xarp_ha.sdl_data)) {
10476 			freemsg(mp);
10477 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10478 			    NULL);
10479 			return;
10480 		}
10481 	}
10482 	*flagsp = ATF_INUSE;
10483 	if (area->area_flags & ACE_F_PERMANENT)
10484 		*flagsp |= ATF_PERM;
10485 	if (area->area_flags & ACE_F_PUBLISH)
10486 		*flagsp |= ATF_PUBL;
10487 	if (area->area_flags & ACE_F_AUTHORITY)
10488 		*flagsp |= ATF_AUTHORITY;
10489 	if (area->area_hw_addr_length != 0) {
10490 		*flagsp |= ATF_COM;
10491 		/*
10492 		 * For SIOCGARP, MAC address length validation has
10493 		 * already been done before the ioctl was issued to ARP
10494 		 * to allow it to progress only on 6 byte addressable
10495 		 * (ethernet like) media. Thus the mac address copying
10496 		 * can not overwrite the sa_data area below.
10497 		 */
10498 		bcopy((char *)area + area->area_hw_addr_offset,
10499 		    storage, area->area_hw_addr_length);
10500 	}
10501 
10502 	/* Ditch the internal IOCTL. */
10503 	freemsg(mp);
10504 	/* Complete the original. */
10505 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10506 }
10507 
10508 /*
10509  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10510  * interface) create the next available logical interface for this
10511  * physical interface.
10512  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10513  * ipif with the specified name.
10514  *
10515  * If the address family is not AF_UNSPEC then set the address as well.
10516  *
10517  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10518  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10519  *
10520  * Executed as a writer on the ill or ill group.
10521  * So no lock is needed to traverse the ipif chain, or examine the
10522  * phyint flags.
10523  */
10524 /* ARGSUSED */
10525 int
10526 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10527     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10528 {
10529 	mblk_t	*mp1;
10530 	struct lifreq *lifr;
10531 	boolean_t	isv6;
10532 	boolean_t	exists;
10533 	char 	*name;
10534 	char	*endp;
10535 	char	*cp;
10536 	int	namelen;
10537 	ipif_t	*ipif;
10538 	long	id;
10539 	ipsq_t	*ipsq;
10540 	ill_t	*ill;
10541 	sin_t	*sin;
10542 	int	err = 0;
10543 	boolean_t found_sep = B_FALSE;
10544 	conn_t	*connp;
10545 	zoneid_t zoneid;
10546 	int	orig_ifindex = 0;
10547 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10548 
10549 	ASSERT(q->q_next == NULL);
10550 	ip1dbg(("ip_sioctl_addif\n"));
10551 	/* Existence of mp1 has been checked in ip_wput_nondata */
10552 	mp1 = mp->b_cont->b_cont;
10553 	/*
10554 	 * Null terminate the string to protect against buffer
10555 	 * overrun. String was generated by user code and may not
10556 	 * be trusted.
10557 	 */
10558 	lifr = (struct lifreq *)mp1->b_rptr;
10559 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10560 	name = lifr->lifr_name;
10561 	ASSERT(CONN_Q(q));
10562 	connp = Q_TO_CONN(q);
10563 	isv6 = connp->conn_af_isv6;
10564 	zoneid = connp->conn_zoneid;
10565 	namelen = mi_strlen(name);
10566 	if (namelen == 0)
10567 		return (EINVAL);
10568 
10569 	exists = B_FALSE;
10570 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10571 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10572 		/*
10573 		 * Allow creating lo0 using SIOCLIFADDIF.
10574 		 * can't be any other writer thread. So can pass null below
10575 		 * for the last 4 args to ipif_lookup_name.
10576 		 */
10577 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10578 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10579 		/* Prevent any further action */
10580 		if (ipif == NULL) {
10581 			return (ENOBUFS);
10582 		} else if (!exists) {
10583 			/* We created the ipif now and as writer */
10584 			ipif_refrele(ipif);
10585 			return (0);
10586 		} else {
10587 			ill = ipif->ipif_ill;
10588 			ill_refhold(ill);
10589 			ipif_refrele(ipif);
10590 		}
10591 	} else {
10592 		/* Look for a colon in the name. */
10593 		endp = &name[namelen];
10594 		for (cp = endp; --cp > name; ) {
10595 			if (*cp == IPIF_SEPARATOR_CHAR) {
10596 				found_sep = B_TRUE;
10597 				/*
10598 				 * Reject any non-decimal aliases for plumbing
10599 				 * of logical interfaces. Aliases with leading
10600 				 * zeroes are also rejected as they introduce
10601 				 * ambiguity in the naming of the interfaces.
10602 				 * Comparing with "0" takes care of all such
10603 				 * cases.
10604 				 */
10605 				if ((strncmp("0", cp+1, 1)) == 0)
10606 					return (EINVAL);
10607 
10608 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10609 				    id <= 0 || *endp != '\0') {
10610 					return (EINVAL);
10611 				}
10612 				*cp = '\0';
10613 				break;
10614 			}
10615 		}
10616 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10617 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10618 		if (found_sep)
10619 			*cp = IPIF_SEPARATOR_CHAR;
10620 		if (ill == NULL)
10621 			return (err);
10622 	}
10623 
10624 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10625 	    B_TRUE);
10626 
10627 	/*
10628 	 * Release the refhold due to the lookup, now that we are excl
10629 	 * or we are just returning
10630 	 */
10631 	ill_refrele(ill);
10632 
10633 	if (ipsq == NULL)
10634 		return (EINPROGRESS);
10635 
10636 	/*
10637 	 * If the interface is failed, inactive or offlined, look for a working
10638 	 * interface in the ill group and create the ipif there. If we can't
10639 	 * find a good interface, create the ipif anyway so that in.mpathd can
10640 	 * move it to the first repaired interface.
10641 	 */
10642 	if ((ill->ill_phyint->phyint_flags &
10643 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10644 	    ill->ill_phyint->phyint_groupname_len != 0) {
10645 		phyint_t *phyi;
10646 		char *groupname = ill->ill_phyint->phyint_groupname;
10647 
10648 		/*
10649 		 * We're looking for a working interface, but it doesn't matter
10650 		 * if it's up or down; so instead of following the group lists,
10651 		 * we look at each physical interface and compare the groupname.
10652 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10653 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10654 		 * Otherwise we create the ipif on the failed interface.
10655 		 */
10656 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10657 		phyi = avl_first(&ipst->ips_phyint_g_list->
10658 		    phyint_list_avl_by_index);
10659 		for (; phyi != NULL;
10660 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10661 		    phyint_list_avl_by_index,
10662 		    phyi, AVL_AFTER)) {
10663 			if (phyi->phyint_groupname_len == 0)
10664 				continue;
10665 			ASSERT(phyi->phyint_groupname != NULL);
10666 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10667 			    !(phyi->phyint_flags &
10668 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10669 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10670 			    (phyi->phyint_illv4 != NULL))) {
10671 				break;
10672 			}
10673 		}
10674 		rw_exit(&ipst->ips_ill_g_lock);
10675 
10676 		if (phyi != NULL) {
10677 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10678 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10679 			    phyi->phyint_illv4);
10680 		}
10681 	}
10682 
10683 	/*
10684 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10685 	 * before or after us.
10686 	 */
10687 	ASSERT(IAM_WRITER_ILL(ill));
10688 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10689 
10690 	if (found_sep && orig_ifindex == 0) {
10691 		/* Now see if there is an IPIF with this unit number. */
10692 		for (ipif = ill->ill_ipif; ipif != NULL;
10693 		    ipif = ipif->ipif_next) {
10694 			if (ipif->ipif_id == id) {
10695 				err = EEXIST;
10696 				goto done;
10697 			}
10698 		}
10699 	}
10700 
10701 	/*
10702 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10703 	 * of lo0. We never come here when we plumb lo0:0. It
10704 	 * happens in ipif_lookup_on_name.
10705 	 * The specified unit number is ignored when we create the ipif on a
10706 	 * different interface. However, we save it in ipif_orig_ipifid below so
10707 	 * that the ipif fails back to the right position.
10708 	 */
10709 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10710 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10711 		err = ENOBUFS;
10712 		goto done;
10713 	}
10714 
10715 	/* Return created name with ioctl */
10716 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10717 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10718 	ip1dbg(("created %s\n", lifr->lifr_name));
10719 
10720 	/* Set address */
10721 	sin = (sin_t *)&lifr->lifr_addr;
10722 	if (sin->sin_family != AF_UNSPEC) {
10723 		err = ip_sioctl_addr(ipif, sin, q, mp,
10724 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10725 	}
10726 
10727 	/* Set ifindex and unit number for failback */
10728 	if (err == 0 && orig_ifindex != 0) {
10729 		ipif->ipif_orig_ifindex = orig_ifindex;
10730 		if (found_sep) {
10731 			ipif->ipif_orig_ipifid = id;
10732 		}
10733 	}
10734 
10735 done:
10736 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10737 	return (err);
10738 }
10739 
10740 /*
10741  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10742  * interface) delete it based on the IP address (on this physical interface).
10743  * Otherwise delete it based on the ipif_id.
10744  * Also, special handling to allow a removeif of lo0.
10745  */
10746 /* ARGSUSED */
10747 int
10748 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10749     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10750 {
10751 	conn_t		*connp;
10752 	ill_t		*ill = ipif->ipif_ill;
10753 	boolean_t	 success;
10754 	ip_stack_t	*ipst;
10755 
10756 	ipst = CONNQ_TO_IPST(q);
10757 
10758 	ASSERT(q->q_next == NULL);
10759 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10760 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10761 	ASSERT(IAM_WRITER_IPIF(ipif));
10762 
10763 	connp = Q_TO_CONN(q);
10764 	/*
10765 	 * Special case for unplumbing lo0 (the loopback physical interface).
10766 	 * If unplumbing lo0, the incoming address structure has been
10767 	 * initialized to all zeros. When unplumbing lo0, all its logical
10768 	 * interfaces must be removed too.
10769 	 *
10770 	 * Note that this interface may be called to remove a specific
10771 	 * loopback logical interface (eg, lo0:1). But in that case
10772 	 * ipif->ipif_id != 0 so that the code path for that case is the
10773 	 * same as any other interface (meaning it skips the code directly
10774 	 * below).
10775 	 */
10776 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10777 		if (sin->sin_family == AF_UNSPEC &&
10778 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10779 			/*
10780 			 * Mark it condemned. No new ref. will be made to ill.
10781 			 */
10782 			mutex_enter(&ill->ill_lock);
10783 			ill->ill_state_flags |= ILL_CONDEMNED;
10784 			for (ipif = ill->ill_ipif; ipif != NULL;
10785 			    ipif = ipif->ipif_next) {
10786 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10787 			}
10788 			mutex_exit(&ill->ill_lock);
10789 
10790 			ipif = ill->ill_ipif;
10791 			/* unplumb the loopback interface */
10792 			ill_delete(ill);
10793 			mutex_enter(&connp->conn_lock);
10794 			mutex_enter(&ill->ill_lock);
10795 			ASSERT(ill->ill_group == NULL);
10796 
10797 			/* Are any references to this ill active */
10798 			if (ill_is_quiescent(ill)) {
10799 				mutex_exit(&ill->ill_lock);
10800 				mutex_exit(&connp->conn_lock);
10801 				ill_delete_tail(ill);
10802 				mutex_enter(&ill->ill_lock);
10803 				ill_nic_info_dispatch(ill);
10804 				mutex_exit(&ill->ill_lock);
10805 				mi_free(ill);
10806 				return (0);
10807 			}
10808 			success = ipsq_pending_mp_add(connp, ipif,
10809 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10810 			mutex_exit(&connp->conn_lock);
10811 			mutex_exit(&ill->ill_lock);
10812 			if (success)
10813 				return (EINPROGRESS);
10814 			else
10815 				return (EINTR);
10816 		}
10817 	}
10818 
10819 	/*
10820 	 * We are exclusive on the ipsq, so an ill move will be serialized
10821 	 * before or after us.
10822 	 */
10823 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10824 
10825 	if (ipif->ipif_id == 0) {
10826 		/* Find based on address */
10827 		if (ipif->ipif_isv6) {
10828 			sin6_t *sin6;
10829 
10830 			if (sin->sin_family != AF_INET6)
10831 				return (EAFNOSUPPORT);
10832 
10833 			sin6 = (sin6_t *)sin;
10834 			/* We are a writer, so we should be able to lookup */
10835 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10836 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10837 			if (ipif == NULL) {
10838 				/*
10839 				 * Maybe the address in on another interface in
10840 				 * the same IPMP group? We check this below.
10841 				 */
10842 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10843 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10844 				    ipst);
10845 			}
10846 		} else {
10847 			ipaddr_t addr;
10848 
10849 			if (sin->sin_family != AF_INET)
10850 				return (EAFNOSUPPORT);
10851 
10852 			addr = sin->sin_addr.s_addr;
10853 			/* We are a writer, so we should be able to lookup */
10854 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10855 			    NULL, NULL, NULL, ipst);
10856 			if (ipif == NULL) {
10857 				/*
10858 				 * Maybe the address in on another interface in
10859 				 * the same IPMP group? We check this below.
10860 				 */
10861 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10862 				    NULL, NULL, NULL, NULL, ipst);
10863 			}
10864 		}
10865 		if (ipif == NULL) {
10866 			return (EADDRNOTAVAIL);
10867 		}
10868 		/*
10869 		 * When the address to be removed is hosted on a different
10870 		 * interface, we check if the interface is in the same IPMP
10871 		 * group as the specified one; if so we proceed with the
10872 		 * removal.
10873 		 * ill->ill_group is NULL when the ill is down, so we have to
10874 		 * compare the group names instead.
10875 		 */
10876 		if (ipif->ipif_ill != ill &&
10877 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10878 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10879 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10880 		    ill->ill_phyint->phyint_groupname) != 0)) {
10881 			ipif_refrele(ipif);
10882 			return (EADDRNOTAVAIL);
10883 		}
10884 
10885 		/* This is a writer */
10886 		ipif_refrele(ipif);
10887 	}
10888 
10889 	/*
10890 	 * Can not delete instance zero since it is tied to the ill.
10891 	 */
10892 	if (ipif->ipif_id == 0)
10893 		return (EBUSY);
10894 
10895 	mutex_enter(&ill->ill_lock);
10896 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10897 	mutex_exit(&ill->ill_lock);
10898 
10899 	ipif_free(ipif);
10900 
10901 	mutex_enter(&connp->conn_lock);
10902 	mutex_enter(&ill->ill_lock);
10903 
10904 	/* Are any references to this ipif active */
10905 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10906 		mutex_exit(&ill->ill_lock);
10907 		mutex_exit(&connp->conn_lock);
10908 		ipif_non_duplicate(ipif);
10909 		ipif_down_tail(ipif);
10910 		ipif_free_tail(ipif);
10911 		return (0);
10912 	}
10913 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10914 	    IPIF_FREE);
10915 	mutex_exit(&ill->ill_lock);
10916 	mutex_exit(&connp->conn_lock);
10917 	if (success)
10918 		return (EINPROGRESS);
10919 	else
10920 		return (EINTR);
10921 }
10922 
10923 /*
10924  * Restart the removeif ioctl. The refcnt has gone down to 0.
10925  * The ipif is already condemned. So can't find it thru lookups.
10926  */
10927 /* ARGSUSED */
10928 int
10929 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10930     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10931 {
10932 	ill_t *ill = ipif->ipif_ill;
10933 
10934 	ASSERT(IAM_WRITER_IPIF(ipif));
10935 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10936 
10937 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10938 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10939 
10940 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10941 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10942 		ill_delete_tail(ill);
10943 		mutex_enter(&ill->ill_lock);
10944 		ill_nic_info_dispatch(ill);
10945 		mutex_exit(&ill->ill_lock);
10946 		mi_free(ill);
10947 		return (0);
10948 	}
10949 
10950 	ipif_non_duplicate(ipif);
10951 	ipif_down_tail(ipif);
10952 	ipif_free_tail(ipif);
10953 
10954 	ILL_UNMARK_CHANGING(ill);
10955 	return (0);
10956 }
10957 
10958 /*
10959  * Set the local interface address.
10960  * Allow an address of all zero when the interface is down.
10961  */
10962 /* ARGSUSED */
10963 int
10964 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10965     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10966 {
10967 	int err = 0;
10968 	in6_addr_t v6addr;
10969 	boolean_t need_up = B_FALSE;
10970 
10971 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10972 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10973 
10974 	ASSERT(IAM_WRITER_IPIF(ipif));
10975 
10976 	if (ipif->ipif_isv6) {
10977 		sin6_t *sin6;
10978 		ill_t *ill;
10979 		phyint_t *phyi;
10980 
10981 		if (sin->sin_family != AF_INET6)
10982 			return (EAFNOSUPPORT);
10983 
10984 		sin6 = (sin6_t *)sin;
10985 		v6addr = sin6->sin6_addr;
10986 		ill = ipif->ipif_ill;
10987 		phyi = ill->ill_phyint;
10988 
10989 		/*
10990 		 * Enforce that true multicast interfaces have a link-local
10991 		 * address for logical unit 0.
10992 		 */
10993 		if (ipif->ipif_id == 0 &&
10994 		    (ill->ill_flags & ILLF_MULTICAST) &&
10995 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10996 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10997 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10998 			return (EADDRNOTAVAIL);
10999 		}
11000 
11001 		/*
11002 		 * up interfaces shouldn't have the unspecified address
11003 		 * unless they also have the IPIF_NOLOCAL flags set and
11004 		 * have a subnet assigned.
11005 		 */
11006 		if ((ipif->ipif_flags & IPIF_UP) &&
11007 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11008 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11009 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11010 			return (EADDRNOTAVAIL);
11011 		}
11012 
11013 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11014 			return (EADDRNOTAVAIL);
11015 	} else {
11016 		ipaddr_t addr;
11017 
11018 		if (sin->sin_family != AF_INET)
11019 			return (EAFNOSUPPORT);
11020 
11021 		addr = sin->sin_addr.s_addr;
11022 
11023 		/* Allow 0 as the local address. */
11024 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11025 			return (EADDRNOTAVAIL);
11026 
11027 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11028 	}
11029 
11030 
11031 	/*
11032 	 * Even if there is no change we redo things just to rerun
11033 	 * ipif_set_default.
11034 	 */
11035 	if (ipif->ipif_flags & IPIF_UP) {
11036 		/*
11037 		 * Setting a new local address, make sure
11038 		 * we have net and subnet bcast ire's for
11039 		 * the old address if we need them.
11040 		 */
11041 		if (!ipif->ipif_isv6)
11042 			ipif_check_bcast_ires(ipif);
11043 		/*
11044 		 * If the interface is already marked up,
11045 		 * we call ipif_down which will take care
11046 		 * of ditching any IREs that have been set
11047 		 * up based on the old interface address.
11048 		 */
11049 		err = ipif_logical_down(ipif, q, mp);
11050 		if (err == EINPROGRESS)
11051 			return (err);
11052 		ipif_down_tail(ipif);
11053 		need_up = 1;
11054 	}
11055 
11056 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11057 	return (err);
11058 }
11059 
11060 int
11061 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11062     boolean_t need_up)
11063 {
11064 	in6_addr_t v6addr;
11065 	in6_addr_t ov6addr;
11066 	ipaddr_t addr;
11067 	sin6_t	*sin6;
11068 	int	sinlen;
11069 	int	err = 0;
11070 	ill_t	*ill = ipif->ipif_ill;
11071 	boolean_t need_dl_down;
11072 	boolean_t need_arp_down;
11073 	struct iocblk *iocp;
11074 
11075 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11076 
11077 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11078 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11079 	ASSERT(IAM_WRITER_IPIF(ipif));
11080 
11081 	/* Must cancel any pending timer before taking the ill_lock */
11082 	if (ipif->ipif_recovery_id != 0)
11083 		(void) untimeout(ipif->ipif_recovery_id);
11084 	ipif->ipif_recovery_id = 0;
11085 
11086 	if (ipif->ipif_isv6) {
11087 		sin6 = (sin6_t *)sin;
11088 		v6addr = sin6->sin6_addr;
11089 		sinlen = sizeof (struct sockaddr_in6);
11090 	} else {
11091 		addr = sin->sin_addr.s_addr;
11092 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11093 		sinlen = sizeof (struct sockaddr_in);
11094 	}
11095 	mutex_enter(&ill->ill_lock);
11096 	ov6addr = ipif->ipif_v6lcl_addr;
11097 	ipif->ipif_v6lcl_addr = v6addr;
11098 	sctp_update_ipif_addr(ipif, ov6addr);
11099 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11100 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11101 	} else {
11102 		ipif->ipif_v6src_addr = v6addr;
11103 	}
11104 	ipif->ipif_addr_ready = 0;
11105 
11106 	/*
11107 	 * If the interface was previously marked as a duplicate, then since
11108 	 * we've now got a "new" address, it should no longer be considered a
11109 	 * duplicate -- even if the "new" address is the same as the old one.
11110 	 * Note that if all ipifs are down, we may have a pending ARP down
11111 	 * event to handle.  This is because we want to recover from duplicates
11112 	 * and thus delay tearing down ARP until the duplicates have been
11113 	 * removed or disabled.
11114 	 */
11115 	need_dl_down = need_arp_down = B_FALSE;
11116 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11117 		need_arp_down = !need_up;
11118 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11119 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11120 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11121 			need_dl_down = B_TRUE;
11122 		}
11123 	}
11124 
11125 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11126 	    !ill->ill_is_6to4tun) {
11127 		queue_t *wqp = ill->ill_wq;
11128 
11129 		/*
11130 		 * The local address of this interface is a 6to4 address,
11131 		 * check if this interface is in fact a 6to4 tunnel or just
11132 		 * an interface configured with a 6to4 address.  We are only
11133 		 * interested in the former.
11134 		 */
11135 		if (wqp != NULL) {
11136 			while ((wqp->q_next != NULL) &&
11137 			    (wqp->q_next->q_qinfo != NULL) &&
11138 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11139 
11140 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11141 				    == TUN6TO4_MODID) {
11142 					/* set for use in IP */
11143 					ill->ill_is_6to4tun = 1;
11144 					break;
11145 				}
11146 				wqp = wqp->q_next;
11147 			}
11148 		}
11149 	}
11150 
11151 	ipif_set_default(ipif);
11152 
11153 	/*
11154 	 * When publishing an interface address change event, we only notify
11155 	 * the event listeners of the new address.  It is assumed that if they
11156 	 * actively care about the addresses assigned that they will have
11157 	 * already discovered the previous address assigned (if there was one.)
11158 	 *
11159 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11160 	 */
11161 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11162 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11163 		    NE_ADDRESS_CHANGE, sin, sinlen);
11164 	}
11165 
11166 	mutex_exit(&ill->ill_lock);
11167 
11168 	if (need_up) {
11169 		/*
11170 		 * Now bring the interface back up.  If this
11171 		 * is the only IPIF for the ILL, ipif_up
11172 		 * will have to re-bind to the device, so
11173 		 * we may get back EINPROGRESS, in which
11174 		 * case, this IOCTL will get completed in
11175 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11176 		 */
11177 		err = ipif_up(ipif, q, mp);
11178 	}
11179 
11180 	if (need_dl_down)
11181 		ill_dl_down(ill);
11182 	if (need_arp_down)
11183 		ipif_arp_down(ipif);
11184 
11185 	return (err);
11186 }
11187 
11188 
11189 /*
11190  * Restart entry point to restart the address set operation after the
11191  * refcounts have dropped to zero.
11192  */
11193 /* ARGSUSED */
11194 int
11195 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11196     ip_ioctl_cmd_t *ipip, void *ifreq)
11197 {
11198 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11199 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11200 	ASSERT(IAM_WRITER_IPIF(ipif));
11201 	ipif_down_tail(ipif);
11202 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11203 }
11204 
11205 /* ARGSUSED */
11206 int
11207 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11208     ip_ioctl_cmd_t *ipip, void *if_req)
11209 {
11210 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11211 	struct lifreq *lifr = (struct lifreq *)if_req;
11212 
11213 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11214 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11215 	/*
11216 	 * The net mask and address can't change since we have a
11217 	 * reference to the ipif. So no lock is necessary.
11218 	 */
11219 	if (ipif->ipif_isv6) {
11220 		*sin6 = sin6_null;
11221 		sin6->sin6_family = AF_INET6;
11222 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11223 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11224 		lifr->lifr_addrlen =
11225 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11226 	} else {
11227 		*sin = sin_null;
11228 		sin->sin_family = AF_INET;
11229 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11230 		if (ipip->ipi_cmd_type == LIF_CMD) {
11231 			lifr->lifr_addrlen =
11232 			    ip_mask_to_plen(ipif->ipif_net_mask);
11233 		}
11234 	}
11235 	return (0);
11236 }
11237 
11238 /*
11239  * Set the destination address for a pt-pt interface.
11240  */
11241 /* ARGSUSED */
11242 int
11243 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11244     ip_ioctl_cmd_t *ipip, void *if_req)
11245 {
11246 	int err = 0;
11247 	in6_addr_t v6addr;
11248 	boolean_t need_up = B_FALSE;
11249 
11250 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11251 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11252 	ASSERT(IAM_WRITER_IPIF(ipif));
11253 
11254 	if (ipif->ipif_isv6) {
11255 		sin6_t *sin6;
11256 
11257 		if (sin->sin_family != AF_INET6)
11258 			return (EAFNOSUPPORT);
11259 
11260 		sin6 = (sin6_t *)sin;
11261 		v6addr = sin6->sin6_addr;
11262 
11263 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11264 			return (EADDRNOTAVAIL);
11265 	} else {
11266 		ipaddr_t addr;
11267 
11268 		if (sin->sin_family != AF_INET)
11269 			return (EAFNOSUPPORT);
11270 
11271 		addr = sin->sin_addr.s_addr;
11272 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11273 			return (EADDRNOTAVAIL);
11274 
11275 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11276 	}
11277 
11278 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11279 		return (0);	/* No change */
11280 
11281 	if (ipif->ipif_flags & IPIF_UP) {
11282 		/*
11283 		 * If the interface is already marked up,
11284 		 * we call ipif_down which will take care
11285 		 * of ditching any IREs that have been set
11286 		 * up based on the old pp dst address.
11287 		 */
11288 		err = ipif_logical_down(ipif, q, mp);
11289 		if (err == EINPROGRESS)
11290 			return (err);
11291 		ipif_down_tail(ipif);
11292 		need_up = B_TRUE;
11293 	}
11294 	/*
11295 	 * could return EINPROGRESS. If so ioctl will complete in
11296 	 * ip_rput_dlpi_writer
11297 	 */
11298 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11299 	return (err);
11300 }
11301 
11302 static int
11303 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11304     boolean_t need_up)
11305 {
11306 	in6_addr_t v6addr;
11307 	ill_t	*ill = ipif->ipif_ill;
11308 	int	err = 0;
11309 	boolean_t need_dl_down;
11310 	boolean_t need_arp_down;
11311 
11312 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11313 	    ipif->ipif_id, (void *)ipif));
11314 
11315 	/* Must cancel any pending timer before taking the ill_lock */
11316 	if (ipif->ipif_recovery_id != 0)
11317 		(void) untimeout(ipif->ipif_recovery_id);
11318 	ipif->ipif_recovery_id = 0;
11319 
11320 	if (ipif->ipif_isv6) {
11321 		sin6_t *sin6;
11322 
11323 		sin6 = (sin6_t *)sin;
11324 		v6addr = sin6->sin6_addr;
11325 	} else {
11326 		ipaddr_t addr;
11327 
11328 		addr = sin->sin_addr.s_addr;
11329 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11330 	}
11331 	mutex_enter(&ill->ill_lock);
11332 	/* Set point to point destination address. */
11333 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11334 		/*
11335 		 * Allow this as a means of creating logical
11336 		 * pt-pt interfaces on top of e.g. an Ethernet.
11337 		 * XXX Undocumented HACK for testing.
11338 		 * pt-pt interfaces are created with NUD disabled.
11339 		 */
11340 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11341 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11342 		if (ipif->ipif_isv6)
11343 			ill->ill_flags |= ILLF_NONUD;
11344 	}
11345 
11346 	/*
11347 	 * If the interface was previously marked as a duplicate, then since
11348 	 * we've now got a "new" address, it should no longer be considered a
11349 	 * duplicate -- even if the "new" address is the same as the old one.
11350 	 * Note that if all ipifs are down, we may have a pending ARP down
11351 	 * event to handle.
11352 	 */
11353 	need_dl_down = need_arp_down = B_FALSE;
11354 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11355 		need_arp_down = !need_up;
11356 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11357 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11358 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11359 			need_dl_down = B_TRUE;
11360 		}
11361 	}
11362 
11363 	/* Set the new address. */
11364 	ipif->ipif_v6pp_dst_addr = v6addr;
11365 	/* Make sure subnet tracks pp_dst */
11366 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11367 	mutex_exit(&ill->ill_lock);
11368 
11369 	if (need_up) {
11370 		/*
11371 		 * Now bring the interface back up.  If this
11372 		 * is the only IPIF for the ILL, ipif_up
11373 		 * will have to re-bind to the device, so
11374 		 * we may get back EINPROGRESS, in which
11375 		 * case, this IOCTL will get completed in
11376 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11377 		 */
11378 		err = ipif_up(ipif, q, mp);
11379 	}
11380 
11381 	if (need_dl_down)
11382 		ill_dl_down(ill);
11383 
11384 	if (need_arp_down)
11385 		ipif_arp_down(ipif);
11386 	return (err);
11387 }
11388 
11389 /*
11390  * Restart entry point to restart the dstaddress set operation after the
11391  * refcounts have dropped to zero.
11392  */
11393 /* ARGSUSED */
11394 int
11395 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11396     ip_ioctl_cmd_t *ipip, void *ifreq)
11397 {
11398 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11399 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11400 	ipif_down_tail(ipif);
11401 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11402 }
11403 
11404 /* ARGSUSED */
11405 int
11406 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11407     ip_ioctl_cmd_t *ipip, void *if_req)
11408 {
11409 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11410 
11411 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11412 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11413 	/*
11414 	 * Get point to point destination address. The addresses can't
11415 	 * change since we hold a reference to the ipif.
11416 	 */
11417 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11418 		return (EADDRNOTAVAIL);
11419 
11420 	if (ipif->ipif_isv6) {
11421 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11422 		*sin6 = sin6_null;
11423 		sin6->sin6_family = AF_INET6;
11424 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11425 	} else {
11426 		*sin = sin_null;
11427 		sin->sin_family = AF_INET;
11428 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11429 	}
11430 	return (0);
11431 }
11432 
11433 /*
11434  * part of ipmp, make this func return the active/inactive state and
11435  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11436  */
11437 /*
11438  * This function either sets or clears the IFF_INACTIVE flag.
11439  *
11440  * As long as there are some addresses or multicast memberships on the
11441  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11442  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11443  * will be used for outbound packets.
11444  *
11445  * Caller needs to verify the validity of setting IFF_INACTIVE.
11446  */
11447 static void
11448 phyint_inactive(phyint_t *phyi)
11449 {
11450 	ill_t *ill_v4;
11451 	ill_t *ill_v6;
11452 	ipif_t *ipif;
11453 	ilm_t *ilm;
11454 
11455 	ill_v4 = phyi->phyint_illv4;
11456 	ill_v6 = phyi->phyint_illv6;
11457 
11458 	/*
11459 	 * No need for a lock while traversing the list since iam
11460 	 * a writer
11461 	 */
11462 	if (ill_v4 != NULL) {
11463 		ASSERT(IAM_WRITER_ILL(ill_v4));
11464 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11465 		    ipif = ipif->ipif_next) {
11466 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11467 				mutex_enter(&phyi->phyint_lock);
11468 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11469 				mutex_exit(&phyi->phyint_lock);
11470 				return;
11471 			}
11472 		}
11473 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11474 		    ilm = ilm->ilm_next) {
11475 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11476 				mutex_enter(&phyi->phyint_lock);
11477 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11478 				mutex_exit(&phyi->phyint_lock);
11479 				return;
11480 			}
11481 		}
11482 	}
11483 	if (ill_v6 != NULL) {
11484 		ill_v6 = phyi->phyint_illv6;
11485 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11486 		    ipif = ipif->ipif_next) {
11487 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11488 				mutex_enter(&phyi->phyint_lock);
11489 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11490 				mutex_exit(&phyi->phyint_lock);
11491 				return;
11492 			}
11493 		}
11494 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11495 		    ilm = ilm->ilm_next) {
11496 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11497 				mutex_enter(&phyi->phyint_lock);
11498 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11499 				mutex_exit(&phyi->phyint_lock);
11500 				return;
11501 			}
11502 		}
11503 	}
11504 	mutex_enter(&phyi->phyint_lock);
11505 	phyi->phyint_flags |= PHYI_INACTIVE;
11506 	mutex_exit(&phyi->phyint_lock);
11507 }
11508 
11509 /*
11510  * This function is called only when the phyint flags change. Currently
11511  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11512  * that we can select a good ill.
11513  */
11514 static void
11515 ip_redo_nomination(phyint_t *phyi)
11516 {
11517 	ill_t *ill_v4;
11518 
11519 	ill_v4 = phyi->phyint_illv4;
11520 
11521 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11522 		ASSERT(IAM_WRITER_ILL(ill_v4));
11523 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11524 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11525 	}
11526 }
11527 
11528 /*
11529  * Heuristic to check if ill is INACTIVE.
11530  * Checks if ill has an ipif with an usable ip address.
11531  *
11532  * Return values:
11533  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11534  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11535  */
11536 static boolean_t
11537 ill_is_inactive(ill_t *ill)
11538 {
11539 	ipif_t *ipif;
11540 
11541 	/* Check whether it is in an IPMP group */
11542 	if (ill->ill_phyint->phyint_groupname == NULL)
11543 		return (B_FALSE);
11544 
11545 	if (ill->ill_ipif_up_count == 0)
11546 		return (B_TRUE);
11547 
11548 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11549 		uint64_t flags = ipif->ipif_flags;
11550 
11551 		/*
11552 		 * This ipif is usable if it is IPIF_UP and not a
11553 		 * dedicated test address.  A dedicated test address
11554 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11555 		 * (note in particular that V6 test addresses are
11556 		 * link-local data addresses and thus are marked
11557 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11558 		 */
11559 		if ((flags & IPIF_UP) &&
11560 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11561 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11562 			return (B_FALSE);
11563 	}
11564 	return (B_TRUE);
11565 }
11566 
11567 /*
11568  * Set interface flags.
11569  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11570  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11571  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11572  *
11573  * NOTE : We really don't enforce that ipif_id zero should be used
11574  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11575  *	  is because applications generally does SICGLIFFLAGS and
11576  *	  ORs in the new flags (that affects the logical) and does a
11577  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11578  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11579  *	  flags that will be turned on is correct with respect to
11580  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11581  */
11582 /* ARGSUSED */
11583 int
11584 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11585     ip_ioctl_cmd_t *ipip, void *if_req)
11586 {
11587 	uint64_t turn_on;
11588 	uint64_t turn_off;
11589 	int	err;
11590 	boolean_t need_up = B_FALSE;
11591 	phyint_t *phyi;
11592 	ill_t *ill;
11593 	uint64_t intf_flags;
11594 	boolean_t phyint_flags_modified = B_FALSE;
11595 	uint64_t flags;
11596 	struct ifreq *ifr;
11597 	struct lifreq *lifr;
11598 	boolean_t set_linklocal = B_FALSE;
11599 	boolean_t zero_source = B_FALSE;
11600 	ip_stack_t *ipst;
11601 
11602 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11603 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11604 
11605 	ASSERT(IAM_WRITER_IPIF(ipif));
11606 
11607 	ill = ipif->ipif_ill;
11608 	phyi = ill->ill_phyint;
11609 	ipst = ill->ill_ipst;
11610 
11611 	if (ipip->ipi_cmd_type == IF_CMD) {
11612 		ifr = (struct ifreq *)if_req;
11613 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11614 	} else {
11615 		lifr = (struct lifreq *)if_req;
11616 		flags = lifr->lifr_flags;
11617 	}
11618 
11619 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11620 
11621 	/*
11622 	 * Has the flags been set correctly till now ?
11623 	 */
11624 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11625 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11626 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11627 	/*
11628 	 * Compare the new flags to the old, and partition
11629 	 * into those coming on and those going off.
11630 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11631 	 */
11632 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11633 		flags |= intf_flags & ~0xFFFF;
11634 
11635 	/*
11636 	 * First check which bits will change and then which will
11637 	 * go on and off
11638 	 */
11639 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11640 	if (!turn_on)
11641 		return (0);	/* No change */
11642 
11643 	turn_off = intf_flags & turn_on;
11644 	turn_on ^= turn_off;
11645 	err = 0;
11646 
11647 	/*
11648 	 * Don't allow any bits belonging to the logical interface
11649 	 * to be set or cleared on the replacement ipif that was
11650 	 * created temporarily during a MOVE.
11651 	 */
11652 	if (ipif->ipif_replace_zero &&
11653 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11654 		return (EINVAL);
11655 	}
11656 
11657 	/*
11658 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11659 	 * IPv6 interfaces.
11660 	 */
11661 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11662 		return (EINVAL);
11663 
11664 	/*
11665 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11666 	 */
11667 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11668 		return (EINVAL);
11669 
11670 	/*
11671 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11672 	 * interfaces.  It makes no sense in that context.
11673 	 */
11674 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11675 		return (EINVAL);
11676 
11677 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11678 		zero_source = B_TRUE;
11679 
11680 	/*
11681 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11682 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11683 	 * If the link local address isn't set, and can be set, it will get
11684 	 * set later on in this function.
11685 	 */
11686 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11687 	    (flags & IFF_UP) && !zero_source &&
11688 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11689 		if (ipif_cant_setlinklocal(ipif))
11690 			return (EINVAL);
11691 		set_linklocal = B_TRUE;
11692 	}
11693 
11694 	/*
11695 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11696 	 * same time. No need to grab ill_g_usesrc_lock here, see
11697 	 * synchronization notes in ip.c
11698 	 */
11699 	if (turn_on & PHYI_STANDBY &&
11700 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11701 		return (EINVAL);
11702 	}
11703 
11704 	/*
11705 	 * If we modify physical interface flags, we'll potentially need to
11706 	 * send up two routing socket messages for the changes (one for the
11707 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11708 	 */
11709 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11710 		phyint_flags_modified = B_TRUE;
11711 
11712 	/*
11713 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11714 	 * we need to flush the IRE_CACHES belonging to this ill.
11715 	 * We handle this case here without doing the DOWN/UP dance
11716 	 * like it is done for other flags. If some other flags are
11717 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11718 	 * below will handle it by bringing it down and then
11719 	 * bringing it UP.
11720 	 */
11721 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11722 		ill_t *ill_v4, *ill_v6;
11723 
11724 		ill_v4 = phyi->phyint_illv4;
11725 		ill_v6 = phyi->phyint_illv6;
11726 
11727 		/*
11728 		 * First set the INACTIVE flag if needed. Then delete the ires.
11729 		 * ire_add will atomically prevent creating new IRE_CACHEs
11730 		 * unless hidden flag is set.
11731 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11732 		 */
11733 		if ((turn_on & PHYI_FAILED) &&
11734 		    ((intf_flags & PHYI_STANDBY) ||
11735 		    !ipst->ips_ipmp_enable_failback)) {
11736 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11737 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11738 		}
11739 		if ((turn_off & PHYI_FAILED) &&
11740 		    ((intf_flags & PHYI_STANDBY) ||
11741 		    (!ipst->ips_ipmp_enable_failback &&
11742 		    ill_is_inactive(ill)))) {
11743 			phyint_inactive(phyi);
11744 		}
11745 
11746 		if (turn_on & PHYI_STANDBY) {
11747 			/*
11748 			 * We implicitly set INACTIVE only when STANDBY is set.
11749 			 * INACTIVE is also set on non-STANDBY phyint when user
11750 			 * disables FAILBACK using configuration file.
11751 			 * Do not allow STANDBY to be set on such INACTIVE
11752 			 * phyint
11753 			 */
11754 			if (phyi->phyint_flags & PHYI_INACTIVE)
11755 				return (EINVAL);
11756 			if (!(phyi->phyint_flags & PHYI_FAILED))
11757 				phyint_inactive(phyi);
11758 		}
11759 		if (turn_off & PHYI_STANDBY) {
11760 			if (ipst->ips_ipmp_enable_failback) {
11761 				/*
11762 				 * Reset PHYI_INACTIVE.
11763 				 */
11764 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11765 			} else if (ill_is_inactive(ill) &&
11766 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11767 				/*
11768 				 * Need to set INACTIVE, when user sets
11769 				 * STANDBY on a non-STANDBY phyint and
11770 				 * later resets STANDBY
11771 				 */
11772 				phyint_inactive(phyi);
11773 			}
11774 		}
11775 		/*
11776 		 * We should always send up a message so that the
11777 		 * daemons come to know of it. Note that the zeroth
11778 		 * interface can be down and the check below for IPIF_UP
11779 		 * will not make sense as we are actually setting
11780 		 * a phyint flag here. We assume that the ipif used
11781 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11782 		 * send up any message for non-zero ipifs).
11783 		 */
11784 		phyint_flags_modified = B_TRUE;
11785 
11786 		if (ill_v4 != NULL) {
11787 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11788 			    IRE_CACHE, ill_stq_cache_delete,
11789 			    (char *)ill_v4, ill_v4);
11790 			illgrp_reset_schednext(ill_v4);
11791 		}
11792 		if (ill_v6 != NULL) {
11793 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11794 			    IRE_CACHE, ill_stq_cache_delete,
11795 			    (char *)ill_v6, ill_v6);
11796 			illgrp_reset_schednext(ill_v6);
11797 		}
11798 	}
11799 
11800 	/*
11801 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11802 	 * status of the interface and, if the interface is part of an IPMP
11803 	 * group, all other interfaces that are part of the same IPMP
11804 	 * group.
11805 	 */
11806 	if ((turn_on | turn_off) & ILLF_ROUTER)
11807 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11808 
11809 	/*
11810 	 * If the interface is not UP and we are not going to
11811 	 * bring it UP, record the flags and return. When the
11812 	 * interface comes UP later, the right actions will be
11813 	 * taken.
11814 	 */
11815 	if (!(ipif->ipif_flags & IPIF_UP) &&
11816 	    !(turn_on & IPIF_UP)) {
11817 		/* Record new flags in their respective places. */
11818 		mutex_enter(&ill->ill_lock);
11819 		mutex_enter(&ill->ill_phyint->phyint_lock);
11820 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11821 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11822 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11823 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11824 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11825 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11826 		mutex_exit(&ill->ill_lock);
11827 		mutex_exit(&ill->ill_phyint->phyint_lock);
11828 
11829 		/*
11830 		 * We do the broadcast and nomination here rather
11831 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11832 		 * the case of FAILBACK from INACTIVE standby to the
11833 		 * interface that has been repaired, PHYI_FAILED has not
11834 		 * been cleared yet. If there are only two interfaces in
11835 		 * that group, all we have is a FAILED and INACTIVE
11836 		 * interface. If we do the nomination soon after a failback,
11837 		 * the broadcast nomination code would select the
11838 		 * INACTIVE interface for receiving broadcasts as FAILED is
11839 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11840 		 * receive broadcast packets, we need to redo nomination
11841 		 * when the FAILED is cleared here. Thus, in general we
11842 		 * always do the nomination here for FAILED, STANDBY
11843 		 * and OFFLINE.
11844 		 */
11845 		if (((turn_on | turn_off) &
11846 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11847 			ip_redo_nomination(phyi);
11848 		}
11849 		if (phyint_flags_modified) {
11850 			if (phyi->phyint_illv4 != NULL) {
11851 				ip_rts_ifmsg(phyi->phyint_illv4->
11852 				    ill_ipif);
11853 			}
11854 			if (phyi->phyint_illv6 != NULL) {
11855 				ip_rts_ifmsg(phyi->phyint_illv6->
11856 				    ill_ipif);
11857 			}
11858 		}
11859 		return (0);
11860 	} else if (set_linklocal || zero_source) {
11861 		mutex_enter(&ill->ill_lock);
11862 		if (set_linklocal)
11863 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11864 		if (zero_source)
11865 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11866 		mutex_exit(&ill->ill_lock);
11867 	}
11868 
11869 	/*
11870 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11871 	 * or point-to-point interfaces with an unspecified destination. We do
11872 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11873 	 * have a subnet assigned, which is how in.ndpd currently manages its
11874 	 * onlink prefix list when no addresses are configured with those
11875 	 * prefixes.
11876 	 */
11877 	if (ipif->ipif_isv6 &&
11878 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11879 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11880 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11881 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11882 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11883 		return (EINVAL);
11884 	}
11885 
11886 	/*
11887 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11888 	 * from being brought up.
11889 	 */
11890 	if (!ipif->ipif_isv6 &&
11891 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11892 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11893 		return (EINVAL);
11894 	}
11895 
11896 	/*
11897 	 * The only flag changes that we currently take specific action on
11898 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11899 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11900 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11901 	 * the flags and bringing it back up again.
11902 	 */
11903 	if ((turn_on|turn_off) &
11904 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11905 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11906 		/*
11907 		 * Taking this ipif down, make sure we have
11908 		 * valid net and subnet bcast ire's for other
11909 		 * logical interfaces, if we need them.
11910 		 */
11911 		if (!ipif->ipif_isv6)
11912 			ipif_check_bcast_ires(ipif);
11913 
11914 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11915 		    !(turn_off & IPIF_UP)) {
11916 			need_up = B_TRUE;
11917 			if (ipif->ipif_flags & IPIF_UP)
11918 				ill->ill_logical_down = 1;
11919 			turn_on &= ~IPIF_UP;
11920 		}
11921 		err = ipif_down(ipif, q, mp);
11922 		ip1dbg(("ipif_down returns %d err ", err));
11923 		if (err == EINPROGRESS)
11924 			return (err);
11925 		ipif_down_tail(ipif);
11926 	}
11927 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11928 }
11929 
11930 static int
11931 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11932     boolean_t need_up)
11933 {
11934 	ill_t	*ill;
11935 	phyint_t *phyi;
11936 	uint64_t turn_on;
11937 	uint64_t turn_off;
11938 	uint64_t intf_flags;
11939 	boolean_t phyint_flags_modified = B_FALSE;
11940 	int	err = 0;
11941 	boolean_t set_linklocal = B_FALSE;
11942 	boolean_t zero_source = B_FALSE;
11943 
11944 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11945 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11946 
11947 	ASSERT(IAM_WRITER_IPIF(ipif));
11948 
11949 	ill = ipif->ipif_ill;
11950 	phyi = ill->ill_phyint;
11951 
11952 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11953 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11954 
11955 	turn_off = intf_flags & turn_on;
11956 	turn_on ^= turn_off;
11957 
11958 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11959 		phyint_flags_modified = B_TRUE;
11960 
11961 	/*
11962 	 * Now we change the flags. Track current value of
11963 	 * other flags in their respective places.
11964 	 */
11965 	mutex_enter(&ill->ill_lock);
11966 	mutex_enter(&phyi->phyint_lock);
11967 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11968 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11969 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11970 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11971 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11972 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11973 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11974 		set_linklocal = B_TRUE;
11975 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11976 	}
11977 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11978 		zero_source = B_TRUE;
11979 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11980 	}
11981 	mutex_exit(&ill->ill_lock);
11982 	mutex_exit(&phyi->phyint_lock);
11983 
11984 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11985 		ip_redo_nomination(phyi);
11986 
11987 	if (set_linklocal)
11988 		(void) ipif_setlinklocal(ipif);
11989 
11990 	if (zero_source)
11991 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11992 	else
11993 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11994 
11995 	if (need_up) {
11996 		/*
11997 		 * XXX ipif_up really does not know whether a phyint flags
11998 		 * was modified or not. So, it sends up information on
11999 		 * only one routing sockets message. As we don't bring up
12000 		 * the interface and also set STANDBY/FAILED simultaneously
12001 		 * it should be okay.
12002 		 */
12003 		err = ipif_up(ipif, q, mp);
12004 	} else {
12005 		/*
12006 		 * Make sure routing socket sees all changes to the flags.
12007 		 * ipif_up_done* handles this when we use ipif_up.
12008 		 */
12009 		if (phyint_flags_modified) {
12010 			if (phyi->phyint_illv4 != NULL) {
12011 				ip_rts_ifmsg(phyi->phyint_illv4->
12012 				    ill_ipif);
12013 			}
12014 			if (phyi->phyint_illv6 != NULL) {
12015 				ip_rts_ifmsg(phyi->phyint_illv6->
12016 				    ill_ipif);
12017 			}
12018 		} else {
12019 			ip_rts_ifmsg(ipif);
12020 		}
12021 		/*
12022 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12023 		 * this in need_up case.
12024 		 */
12025 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12026 	}
12027 	return (err);
12028 }
12029 
12030 /*
12031  * Restart entry point to restart the flags restart operation after the
12032  * refcounts have dropped to zero.
12033  */
12034 /* ARGSUSED */
12035 int
12036 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12037     ip_ioctl_cmd_t *ipip, void *if_req)
12038 {
12039 	int	err;
12040 	struct ifreq *ifr = (struct ifreq *)if_req;
12041 	struct lifreq *lifr = (struct lifreq *)if_req;
12042 
12043 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12044 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12045 
12046 	ipif_down_tail(ipif);
12047 	if (ipip->ipi_cmd_type == IF_CMD) {
12048 		/*
12049 		 * Since ip_sioctl_flags expects an int and ifr_flags
12050 		 * is a short we need to cast ifr_flags into an int
12051 		 * to avoid having sign extension cause bits to get
12052 		 * set that should not be.
12053 		 */
12054 		err = ip_sioctl_flags_tail(ipif,
12055 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12056 		    q, mp, B_TRUE);
12057 	} else {
12058 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12059 		    q, mp, B_TRUE);
12060 	}
12061 	return (err);
12062 }
12063 
12064 /*
12065  * Can operate on either a module or a driver queue.
12066  */
12067 /* ARGSUSED */
12068 int
12069 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12070     ip_ioctl_cmd_t *ipip, void *if_req)
12071 {
12072 	/*
12073 	 * Has the flags been set correctly till now ?
12074 	 */
12075 	ill_t *ill = ipif->ipif_ill;
12076 	phyint_t *phyi = ill->ill_phyint;
12077 
12078 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12079 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12080 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12081 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12082 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12083 
12084 	/*
12085 	 * Need a lock since some flags can be set even when there are
12086 	 * references to the ipif.
12087 	 */
12088 	mutex_enter(&ill->ill_lock);
12089 	if (ipip->ipi_cmd_type == IF_CMD) {
12090 		struct ifreq *ifr = (struct ifreq *)if_req;
12091 
12092 		/* Get interface flags (low 16 only). */
12093 		ifr->ifr_flags = ((ipif->ipif_flags |
12094 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12095 	} else {
12096 		struct lifreq *lifr = (struct lifreq *)if_req;
12097 
12098 		/* Get interface flags. */
12099 		lifr->lifr_flags = ipif->ipif_flags |
12100 		    ill->ill_flags | phyi->phyint_flags;
12101 	}
12102 	mutex_exit(&ill->ill_lock);
12103 	return (0);
12104 }
12105 
12106 /* ARGSUSED */
12107 int
12108 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12109     ip_ioctl_cmd_t *ipip, void *if_req)
12110 {
12111 	int mtu;
12112 	int ip_min_mtu;
12113 	struct ifreq	*ifr;
12114 	struct lifreq *lifr;
12115 	ire_t	*ire;
12116 	ip_stack_t *ipst;
12117 
12118 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12119 	    ipif->ipif_id, (void *)ipif));
12120 	if (ipip->ipi_cmd_type == IF_CMD) {
12121 		ifr = (struct ifreq *)if_req;
12122 		mtu = ifr->ifr_metric;
12123 	} else {
12124 		lifr = (struct lifreq *)if_req;
12125 		mtu = lifr->lifr_mtu;
12126 	}
12127 
12128 	if (ipif->ipif_isv6)
12129 		ip_min_mtu = IPV6_MIN_MTU;
12130 	else
12131 		ip_min_mtu = IP_MIN_MTU;
12132 
12133 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12134 		return (EINVAL);
12135 
12136 	/*
12137 	 * Change the MTU size in all relevant ire's.
12138 	 * Mtu change Vs. new ire creation - protocol below.
12139 	 * First change ipif_mtu and the ire_max_frag of the
12140 	 * interface ire. Then do an ire walk and change the
12141 	 * ire_max_frag of all affected ires. During ire_add
12142 	 * under the bucket lock, set the ire_max_frag of the
12143 	 * new ire being created from the ipif/ire from which
12144 	 * it is being derived. If an mtu change happens after
12145 	 * the ire is added, the new ire will be cleaned up.
12146 	 * Conversely if the mtu change happens before the ire
12147 	 * is added, ire_add will see the new value of the mtu.
12148 	 */
12149 	ipif->ipif_mtu = mtu;
12150 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12151 
12152 	if (ipif->ipif_isv6)
12153 		ire = ipif_to_ire_v6(ipif);
12154 	else
12155 		ire = ipif_to_ire(ipif);
12156 	if (ire != NULL) {
12157 		ire->ire_max_frag = ipif->ipif_mtu;
12158 		ire_refrele(ire);
12159 	}
12160 	ipst = ipif->ipif_ill->ill_ipst;
12161 	if (ipif->ipif_flags & IPIF_UP) {
12162 		if (ipif->ipif_isv6)
12163 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12164 			    ipst);
12165 		else
12166 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12167 			    ipst);
12168 	}
12169 	/* Update the MTU in SCTP's list */
12170 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12171 	return (0);
12172 }
12173 
12174 /* Get interface MTU. */
12175 /* ARGSUSED */
12176 int
12177 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12178 	ip_ioctl_cmd_t *ipip, void *if_req)
12179 {
12180 	struct ifreq	*ifr;
12181 	struct lifreq	*lifr;
12182 
12183 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12184 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12185 	if (ipip->ipi_cmd_type == IF_CMD) {
12186 		ifr = (struct ifreq *)if_req;
12187 		ifr->ifr_metric = ipif->ipif_mtu;
12188 	} else {
12189 		lifr = (struct lifreq *)if_req;
12190 		lifr->lifr_mtu = ipif->ipif_mtu;
12191 	}
12192 	return (0);
12193 }
12194 
12195 /* Set interface broadcast address. */
12196 /* ARGSUSED2 */
12197 int
12198 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12199 	ip_ioctl_cmd_t *ipip, void *if_req)
12200 {
12201 	ipaddr_t addr;
12202 	ire_t	*ire;
12203 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12204 
12205 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12206 	    ipif->ipif_id));
12207 
12208 	ASSERT(IAM_WRITER_IPIF(ipif));
12209 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12210 		return (EADDRNOTAVAIL);
12211 
12212 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12213 
12214 	if (sin->sin_family != AF_INET)
12215 		return (EAFNOSUPPORT);
12216 
12217 	addr = sin->sin_addr.s_addr;
12218 	if (ipif->ipif_flags & IPIF_UP) {
12219 		/*
12220 		 * If we are already up, make sure the new
12221 		 * broadcast address makes sense.  If it does,
12222 		 * there should be an IRE for it already.
12223 		 * Don't match on ipif, only on the ill
12224 		 * since we are sharing these now. Don't use
12225 		 * MATCH_IRE_ILL_GROUP as we are looking for
12226 		 * the broadcast ire on this ill and each ill
12227 		 * in the group has its own broadcast ire.
12228 		 */
12229 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12230 		    ipif, ALL_ZONES, NULL,
12231 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12232 		if (ire == NULL) {
12233 			return (EINVAL);
12234 		} else {
12235 			ire_refrele(ire);
12236 		}
12237 	}
12238 	/*
12239 	 * Changing the broadcast addr for this ipif.
12240 	 * Make sure we have valid net and subnet bcast
12241 	 * ire's for other logical interfaces, if needed.
12242 	 */
12243 	if (addr != ipif->ipif_brd_addr)
12244 		ipif_check_bcast_ires(ipif);
12245 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12246 	return (0);
12247 }
12248 
12249 /* Get interface broadcast address. */
12250 /* ARGSUSED */
12251 int
12252 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12253     ip_ioctl_cmd_t *ipip, void *if_req)
12254 {
12255 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12256 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12257 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12258 		return (EADDRNOTAVAIL);
12259 
12260 	/* IPIF_BROADCAST not possible with IPv6 */
12261 	ASSERT(!ipif->ipif_isv6);
12262 	*sin = sin_null;
12263 	sin->sin_family = AF_INET;
12264 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12265 	return (0);
12266 }
12267 
12268 /*
12269  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12270  */
12271 /* ARGSUSED */
12272 int
12273 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12274     ip_ioctl_cmd_t *ipip, void *if_req)
12275 {
12276 	int err = 0;
12277 	in6_addr_t v6mask;
12278 
12279 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12280 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12281 
12282 	ASSERT(IAM_WRITER_IPIF(ipif));
12283 
12284 	if (ipif->ipif_isv6) {
12285 		sin6_t *sin6;
12286 
12287 		if (sin->sin_family != AF_INET6)
12288 			return (EAFNOSUPPORT);
12289 
12290 		sin6 = (sin6_t *)sin;
12291 		v6mask = sin6->sin6_addr;
12292 	} else {
12293 		ipaddr_t mask;
12294 
12295 		if (sin->sin_family != AF_INET)
12296 			return (EAFNOSUPPORT);
12297 
12298 		mask = sin->sin_addr.s_addr;
12299 		V4MASK_TO_V6(mask, v6mask);
12300 	}
12301 
12302 	/*
12303 	 * No big deal if the interface isn't already up, or the mask
12304 	 * isn't really changing, or this is pt-pt.
12305 	 */
12306 	if (!(ipif->ipif_flags & IPIF_UP) ||
12307 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12308 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12309 		ipif->ipif_v6net_mask = v6mask;
12310 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12311 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12312 			    ipif->ipif_v6net_mask,
12313 			    ipif->ipif_v6subnet);
12314 		}
12315 		return (0);
12316 	}
12317 	/*
12318 	 * Make sure we have valid net and subnet broadcast ire's
12319 	 * for the old netmask, if needed by other logical interfaces.
12320 	 */
12321 	if (!ipif->ipif_isv6)
12322 		ipif_check_bcast_ires(ipif);
12323 
12324 	err = ipif_logical_down(ipif, q, mp);
12325 	if (err == EINPROGRESS)
12326 		return (err);
12327 	ipif_down_tail(ipif);
12328 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12329 	return (err);
12330 }
12331 
12332 static int
12333 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12334 {
12335 	in6_addr_t v6mask;
12336 	int err = 0;
12337 
12338 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12339 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12340 
12341 	if (ipif->ipif_isv6) {
12342 		sin6_t *sin6;
12343 
12344 		sin6 = (sin6_t *)sin;
12345 		v6mask = sin6->sin6_addr;
12346 	} else {
12347 		ipaddr_t mask;
12348 
12349 		mask = sin->sin_addr.s_addr;
12350 		V4MASK_TO_V6(mask, v6mask);
12351 	}
12352 
12353 	ipif->ipif_v6net_mask = v6mask;
12354 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12355 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12356 		    ipif->ipif_v6subnet);
12357 	}
12358 	err = ipif_up(ipif, q, mp);
12359 
12360 	if (err == 0 || err == EINPROGRESS) {
12361 		/*
12362 		 * The interface must be DL_BOUND if this packet has to
12363 		 * go out on the wire. Since we only go through a logical
12364 		 * down and are bound with the driver during an internal
12365 		 * down/up that is satisfied.
12366 		 */
12367 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12368 			/* Potentially broadcast an address mask reply. */
12369 			ipif_mask_reply(ipif);
12370 		}
12371 	}
12372 	return (err);
12373 }
12374 
12375 /* ARGSUSED */
12376 int
12377 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12378     ip_ioctl_cmd_t *ipip, void *if_req)
12379 {
12380 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12381 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12382 	ipif_down_tail(ipif);
12383 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12384 }
12385 
12386 /* Get interface net mask. */
12387 /* ARGSUSED */
12388 int
12389 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12390     ip_ioctl_cmd_t *ipip, void *if_req)
12391 {
12392 	struct lifreq *lifr = (struct lifreq *)if_req;
12393 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12394 
12395 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12396 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12397 
12398 	/*
12399 	 * net mask can't change since we have a reference to the ipif.
12400 	 */
12401 	if (ipif->ipif_isv6) {
12402 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12403 		*sin6 = sin6_null;
12404 		sin6->sin6_family = AF_INET6;
12405 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12406 		lifr->lifr_addrlen =
12407 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12408 	} else {
12409 		*sin = sin_null;
12410 		sin->sin_family = AF_INET;
12411 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12412 		if (ipip->ipi_cmd_type == LIF_CMD) {
12413 			lifr->lifr_addrlen =
12414 			    ip_mask_to_plen(ipif->ipif_net_mask);
12415 		}
12416 	}
12417 	return (0);
12418 }
12419 
12420 /* ARGSUSED */
12421 int
12422 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12423     ip_ioctl_cmd_t *ipip, void *if_req)
12424 {
12425 
12426 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12427 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12428 	/*
12429 	 * Set interface metric.  We don't use this for
12430 	 * anything but we keep track of it in case it is
12431 	 * important to routing applications or such.
12432 	 */
12433 	if (ipip->ipi_cmd_type == IF_CMD) {
12434 		struct ifreq    *ifr;
12435 
12436 		ifr = (struct ifreq *)if_req;
12437 		ipif->ipif_metric = ifr->ifr_metric;
12438 	} else {
12439 		struct lifreq   *lifr;
12440 
12441 		lifr = (struct lifreq *)if_req;
12442 		ipif->ipif_metric = lifr->lifr_metric;
12443 	}
12444 	return (0);
12445 }
12446 
12447 
12448 /* ARGSUSED */
12449 int
12450 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12451     ip_ioctl_cmd_t *ipip, void *if_req)
12452 {
12453 
12454 	/* Get interface metric. */
12455 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12456 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12457 	if (ipip->ipi_cmd_type == IF_CMD) {
12458 		struct ifreq    *ifr;
12459 
12460 		ifr = (struct ifreq *)if_req;
12461 		ifr->ifr_metric = ipif->ipif_metric;
12462 	} else {
12463 		struct lifreq   *lifr;
12464 
12465 		lifr = (struct lifreq *)if_req;
12466 		lifr->lifr_metric = ipif->ipif_metric;
12467 	}
12468 
12469 	return (0);
12470 }
12471 
12472 /* ARGSUSED */
12473 int
12474 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12475     ip_ioctl_cmd_t *ipip, void *if_req)
12476 {
12477 
12478 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12479 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12480 	/*
12481 	 * Set the muxid returned from I_PLINK.
12482 	 */
12483 	if (ipip->ipi_cmd_type == IF_CMD) {
12484 		struct ifreq *ifr = (struct ifreq *)if_req;
12485 
12486 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12487 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12488 	} else {
12489 		struct lifreq *lifr = (struct lifreq *)if_req;
12490 
12491 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12492 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12493 	}
12494 	return (0);
12495 }
12496 
12497 /* ARGSUSED */
12498 int
12499 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12500     ip_ioctl_cmd_t *ipip, void *if_req)
12501 {
12502 
12503 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12504 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12505 	/*
12506 	 * Get the muxid saved in ill for I_PUNLINK.
12507 	 */
12508 	if (ipip->ipi_cmd_type == IF_CMD) {
12509 		struct ifreq *ifr = (struct ifreq *)if_req;
12510 
12511 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12512 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12513 	} else {
12514 		struct lifreq *lifr = (struct lifreq *)if_req;
12515 
12516 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12517 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12518 	}
12519 	return (0);
12520 }
12521 
12522 /*
12523  * Set the subnet prefix. Does not modify the broadcast address.
12524  */
12525 /* ARGSUSED */
12526 int
12527 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12528     ip_ioctl_cmd_t *ipip, void *if_req)
12529 {
12530 	int err = 0;
12531 	in6_addr_t v6addr;
12532 	in6_addr_t v6mask;
12533 	boolean_t need_up = B_FALSE;
12534 	int addrlen;
12535 
12536 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12537 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12538 
12539 	ASSERT(IAM_WRITER_IPIF(ipif));
12540 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12541 
12542 	if (ipif->ipif_isv6) {
12543 		sin6_t *sin6;
12544 
12545 		if (sin->sin_family != AF_INET6)
12546 			return (EAFNOSUPPORT);
12547 
12548 		sin6 = (sin6_t *)sin;
12549 		v6addr = sin6->sin6_addr;
12550 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12551 			return (EADDRNOTAVAIL);
12552 	} else {
12553 		ipaddr_t addr;
12554 
12555 		if (sin->sin_family != AF_INET)
12556 			return (EAFNOSUPPORT);
12557 
12558 		addr = sin->sin_addr.s_addr;
12559 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12560 			return (EADDRNOTAVAIL);
12561 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12562 		/* Add 96 bits */
12563 		addrlen += IPV6_ABITS - IP_ABITS;
12564 	}
12565 
12566 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12567 		return (EINVAL);
12568 
12569 	/* Check if bits in the address is set past the mask */
12570 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12571 		return (EINVAL);
12572 
12573 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12574 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12575 		return (0);	/* No change */
12576 
12577 	if (ipif->ipif_flags & IPIF_UP) {
12578 		/*
12579 		 * If the interface is already marked up,
12580 		 * we call ipif_down which will take care
12581 		 * of ditching any IREs that have been set
12582 		 * up based on the old interface address.
12583 		 */
12584 		err = ipif_logical_down(ipif, q, mp);
12585 		if (err == EINPROGRESS)
12586 			return (err);
12587 		ipif_down_tail(ipif);
12588 		need_up = B_TRUE;
12589 	}
12590 
12591 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12592 	return (err);
12593 }
12594 
12595 static int
12596 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12597     queue_t *q, mblk_t *mp, boolean_t need_up)
12598 {
12599 	ill_t	*ill = ipif->ipif_ill;
12600 	int	err = 0;
12601 
12602 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12603 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12604 
12605 	/* Set the new address. */
12606 	mutex_enter(&ill->ill_lock);
12607 	ipif->ipif_v6net_mask = v6mask;
12608 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12609 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12610 		    ipif->ipif_v6subnet);
12611 	}
12612 	mutex_exit(&ill->ill_lock);
12613 
12614 	if (need_up) {
12615 		/*
12616 		 * Now bring the interface back up.  If this
12617 		 * is the only IPIF for the ILL, ipif_up
12618 		 * will have to re-bind to the device, so
12619 		 * we may get back EINPROGRESS, in which
12620 		 * case, this IOCTL will get completed in
12621 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12622 		 */
12623 		err = ipif_up(ipif, q, mp);
12624 		if (err == EINPROGRESS)
12625 			return (err);
12626 	}
12627 	return (err);
12628 }
12629 
12630 /* ARGSUSED */
12631 int
12632 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12633     ip_ioctl_cmd_t *ipip, void *if_req)
12634 {
12635 	int	addrlen;
12636 	in6_addr_t v6addr;
12637 	in6_addr_t v6mask;
12638 	struct lifreq *lifr = (struct lifreq *)if_req;
12639 
12640 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12641 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12642 	ipif_down_tail(ipif);
12643 
12644 	addrlen = lifr->lifr_addrlen;
12645 	if (ipif->ipif_isv6) {
12646 		sin6_t *sin6;
12647 
12648 		sin6 = (sin6_t *)sin;
12649 		v6addr = sin6->sin6_addr;
12650 	} else {
12651 		ipaddr_t addr;
12652 
12653 		addr = sin->sin_addr.s_addr;
12654 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12655 		addrlen += IPV6_ABITS - IP_ABITS;
12656 	}
12657 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12658 
12659 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12660 }
12661 
12662 /* ARGSUSED */
12663 int
12664 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12665     ip_ioctl_cmd_t *ipip, void *if_req)
12666 {
12667 	struct lifreq *lifr = (struct lifreq *)if_req;
12668 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12669 
12670 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12671 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12672 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12673 
12674 	if (ipif->ipif_isv6) {
12675 		*sin6 = sin6_null;
12676 		sin6->sin6_family = AF_INET6;
12677 		sin6->sin6_addr = ipif->ipif_v6subnet;
12678 		lifr->lifr_addrlen =
12679 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12680 	} else {
12681 		*sin = sin_null;
12682 		sin->sin_family = AF_INET;
12683 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12684 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12685 	}
12686 	return (0);
12687 }
12688 
12689 /*
12690  * Set the IPv6 address token.
12691  */
12692 /* ARGSUSED */
12693 int
12694 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12695     ip_ioctl_cmd_t *ipi, void *if_req)
12696 {
12697 	ill_t *ill = ipif->ipif_ill;
12698 	int err;
12699 	in6_addr_t v6addr;
12700 	in6_addr_t v6mask;
12701 	boolean_t need_up = B_FALSE;
12702 	int i;
12703 	sin6_t *sin6 = (sin6_t *)sin;
12704 	struct lifreq *lifr = (struct lifreq *)if_req;
12705 	int addrlen;
12706 
12707 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12708 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12709 	ASSERT(IAM_WRITER_IPIF(ipif));
12710 
12711 	addrlen = lifr->lifr_addrlen;
12712 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12713 	if (ipif->ipif_id != 0)
12714 		return (EINVAL);
12715 
12716 	if (!ipif->ipif_isv6)
12717 		return (EINVAL);
12718 
12719 	if (addrlen > IPV6_ABITS)
12720 		return (EINVAL);
12721 
12722 	v6addr = sin6->sin6_addr;
12723 
12724 	/*
12725 	 * The length of the token is the length from the end.  To get
12726 	 * the proper mask for this, compute the mask of the bits not
12727 	 * in the token; ie. the prefix, and then xor to get the mask.
12728 	 */
12729 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12730 		return (EINVAL);
12731 	for (i = 0; i < 4; i++) {
12732 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12733 	}
12734 
12735 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12736 	    ill->ill_token_length == addrlen)
12737 		return (0);	/* No change */
12738 
12739 	if (ipif->ipif_flags & IPIF_UP) {
12740 		err = ipif_logical_down(ipif, q, mp);
12741 		if (err == EINPROGRESS)
12742 			return (err);
12743 		ipif_down_tail(ipif);
12744 		need_up = B_TRUE;
12745 	}
12746 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12747 	return (err);
12748 }
12749 
12750 static int
12751 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12752     mblk_t *mp, boolean_t need_up)
12753 {
12754 	in6_addr_t v6addr;
12755 	in6_addr_t v6mask;
12756 	ill_t	*ill = ipif->ipif_ill;
12757 	int	i;
12758 	int	err = 0;
12759 
12760 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12761 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12762 	v6addr = sin6->sin6_addr;
12763 	/*
12764 	 * The length of the token is the length from the end.  To get
12765 	 * the proper mask for this, compute the mask of the bits not
12766 	 * in the token; ie. the prefix, and then xor to get the mask.
12767 	 */
12768 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12769 	for (i = 0; i < 4; i++)
12770 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12771 
12772 	mutex_enter(&ill->ill_lock);
12773 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12774 	ill->ill_token_length = addrlen;
12775 	mutex_exit(&ill->ill_lock);
12776 
12777 	if (need_up) {
12778 		/*
12779 		 * Now bring the interface back up.  If this
12780 		 * is the only IPIF for the ILL, ipif_up
12781 		 * will have to re-bind to the device, so
12782 		 * we may get back EINPROGRESS, in which
12783 		 * case, this IOCTL will get completed in
12784 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12785 		 */
12786 		err = ipif_up(ipif, q, mp);
12787 		if (err == EINPROGRESS)
12788 			return (err);
12789 	}
12790 	return (err);
12791 }
12792 
12793 /* ARGSUSED */
12794 int
12795 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12796     ip_ioctl_cmd_t *ipi, void *if_req)
12797 {
12798 	ill_t *ill;
12799 	sin6_t *sin6 = (sin6_t *)sin;
12800 	struct lifreq *lifr = (struct lifreq *)if_req;
12801 
12802 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12803 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12804 	if (ipif->ipif_id != 0)
12805 		return (EINVAL);
12806 
12807 	ill = ipif->ipif_ill;
12808 	if (!ill->ill_isv6)
12809 		return (ENXIO);
12810 
12811 	*sin6 = sin6_null;
12812 	sin6->sin6_family = AF_INET6;
12813 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12814 	sin6->sin6_addr = ill->ill_token;
12815 	lifr->lifr_addrlen = ill->ill_token_length;
12816 	return (0);
12817 }
12818 
12819 /*
12820  * Set (hardware) link specific information that might override
12821  * what was acquired through the DL_INFO_ACK.
12822  * The logic is as follows.
12823  *
12824  * become exclusive
12825  * set CHANGING flag
12826  * change mtu on affected IREs
12827  * clear CHANGING flag
12828  *
12829  * An ire add that occurs before the CHANGING flag is set will have its mtu
12830  * changed by the ip_sioctl_lnkinfo.
12831  *
12832  * During the time the CHANGING flag is set, no new ires will be added to the
12833  * bucket, and ire add will fail (due the CHANGING flag).
12834  *
12835  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12836  * before it is added to the bucket.
12837  *
12838  * Obviously only 1 thread can set the CHANGING flag and we need to become
12839  * exclusive to set the flag.
12840  */
12841 /* ARGSUSED */
12842 int
12843 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12844     ip_ioctl_cmd_t *ipi, void *if_req)
12845 {
12846 	ill_t		*ill = ipif->ipif_ill;
12847 	ipif_t		*nipif;
12848 	int		ip_min_mtu;
12849 	boolean_t	mtu_walk = B_FALSE;
12850 	struct lifreq	*lifr = (struct lifreq *)if_req;
12851 	lif_ifinfo_req_t *lir;
12852 	ire_t		*ire;
12853 
12854 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12855 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12856 	lir = &lifr->lifr_ifinfo;
12857 	ASSERT(IAM_WRITER_IPIF(ipif));
12858 
12859 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12860 	if (ipif->ipif_id != 0)
12861 		return (EINVAL);
12862 
12863 	/* Set interface MTU. */
12864 	if (ipif->ipif_isv6)
12865 		ip_min_mtu = IPV6_MIN_MTU;
12866 	else
12867 		ip_min_mtu = IP_MIN_MTU;
12868 
12869 	/*
12870 	 * Verify values before we set anything. Allow zero to
12871 	 * mean unspecified.
12872 	 */
12873 	if (lir->lir_maxmtu != 0 &&
12874 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12875 	    lir->lir_maxmtu < ip_min_mtu))
12876 		return (EINVAL);
12877 	if (lir->lir_reachtime != 0 &&
12878 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12879 		return (EINVAL);
12880 	if (lir->lir_reachretrans != 0 &&
12881 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12882 		return (EINVAL);
12883 
12884 	mutex_enter(&ill->ill_lock);
12885 	ill->ill_state_flags |= ILL_CHANGING;
12886 	for (nipif = ill->ill_ipif; nipif != NULL;
12887 	    nipif = nipif->ipif_next) {
12888 		nipif->ipif_state_flags |= IPIF_CHANGING;
12889 	}
12890 
12891 	mutex_exit(&ill->ill_lock);
12892 
12893 	if (lir->lir_maxmtu != 0) {
12894 		ill->ill_max_mtu = lir->lir_maxmtu;
12895 		ill->ill_mtu_userspecified = 1;
12896 		mtu_walk = B_TRUE;
12897 	}
12898 
12899 	if (lir->lir_reachtime != 0)
12900 		ill->ill_reachable_time = lir->lir_reachtime;
12901 
12902 	if (lir->lir_reachretrans != 0)
12903 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12904 
12905 	ill->ill_max_hops = lir->lir_maxhops;
12906 
12907 	ill->ill_max_buf = ND_MAX_Q;
12908 
12909 	if (mtu_walk) {
12910 		/*
12911 		 * Set the MTU on all ipifs associated with this ill except
12912 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12913 		 */
12914 		for (nipif = ill->ill_ipif; nipif != NULL;
12915 		    nipif = nipif->ipif_next) {
12916 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12917 				continue;
12918 
12919 			nipif->ipif_mtu = ill->ill_max_mtu;
12920 
12921 			if (!(nipif->ipif_flags & IPIF_UP))
12922 				continue;
12923 
12924 			if (nipif->ipif_isv6)
12925 				ire = ipif_to_ire_v6(nipif);
12926 			else
12927 				ire = ipif_to_ire(nipif);
12928 			if (ire != NULL) {
12929 				ire->ire_max_frag = ipif->ipif_mtu;
12930 				ire_refrele(ire);
12931 			}
12932 			if (ill->ill_isv6) {
12933 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12934 				    ipif_mtu_change, (char *)nipif,
12935 				    ill);
12936 			} else {
12937 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12938 				    ipif_mtu_change, (char *)nipif,
12939 				    ill);
12940 			}
12941 		}
12942 	}
12943 
12944 	mutex_enter(&ill->ill_lock);
12945 	for (nipif = ill->ill_ipif; nipif != NULL;
12946 	    nipif = nipif->ipif_next) {
12947 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12948 	}
12949 	ILL_UNMARK_CHANGING(ill);
12950 	mutex_exit(&ill->ill_lock);
12951 
12952 	return (0);
12953 }
12954 
12955 /* ARGSUSED */
12956 int
12957 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12958     ip_ioctl_cmd_t *ipi, void *if_req)
12959 {
12960 	struct lif_ifinfo_req *lir;
12961 	ill_t *ill = ipif->ipif_ill;
12962 
12963 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12964 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12965 	if (ipif->ipif_id != 0)
12966 		return (EINVAL);
12967 
12968 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12969 	lir->lir_maxhops = ill->ill_max_hops;
12970 	lir->lir_reachtime = ill->ill_reachable_time;
12971 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12972 	lir->lir_maxmtu = ill->ill_max_mtu;
12973 
12974 	return (0);
12975 }
12976 
12977 /*
12978  * Return best guess as to the subnet mask for the specified address.
12979  * Based on the subnet masks for all the configured interfaces.
12980  *
12981  * We end up returning a zero mask in the case of default, multicast or
12982  * experimental.
12983  */
12984 static ipaddr_t
12985 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12986 {
12987 	ipaddr_t net_mask;
12988 	ill_t	*ill;
12989 	ipif_t	*ipif;
12990 	ill_walk_context_t ctx;
12991 	ipif_t	*fallback_ipif = NULL;
12992 
12993 	net_mask = ip_net_mask(addr);
12994 	if (net_mask == 0) {
12995 		*ipifp = NULL;
12996 		return (0);
12997 	}
12998 
12999 	/* Let's check to see if this is maybe a local subnet route. */
13000 	/* this function only applies to IPv4 interfaces */
13001 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13002 	ill = ILL_START_WALK_V4(&ctx, ipst);
13003 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13004 		mutex_enter(&ill->ill_lock);
13005 		for (ipif = ill->ill_ipif; ipif != NULL;
13006 		    ipif = ipif->ipif_next) {
13007 			if (!IPIF_CAN_LOOKUP(ipif))
13008 				continue;
13009 			if (!(ipif->ipif_flags & IPIF_UP))
13010 				continue;
13011 			if ((ipif->ipif_subnet & net_mask) ==
13012 			    (addr & net_mask)) {
13013 				/*
13014 				 * Don't trust pt-pt interfaces if there are
13015 				 * other interfaces.
13016 				 */
13017 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13018 					if (fallback_ipif == NULL) {
13019 						ipif_refhold_locked(ipif);
13020 						fallback_ipif = ipif;
13021 					}
13022 					continue;
13023 				}
13024 
13025 				/*
13026 				 * Fine. Just assume the same net mask as the
13027 				 * directly attached subnet interface is using.
13028 				 */
13029 				ipif_refhold_locked(ipif);
13030 				mutex_exit(&ill->ill_lock);
13031 				rw_exit(&ipst->ips_ill_g_lock);
13032 				if (fallback_ipif != NULL)
13033 					ipif_refrele(fallback_ipif);
13034 				*ipifp = ipif;
13035 				return (ipif->ipif_net_mask);
13036 			}
13037 		}
13038 		mutex_exit(&ill->ill_lock);
13039 	}
13040 	rw_exit(&ipst->ips_ill_g_lock);
13041 
13042 	*ipifp = fallback_ipif;
13043 	return ((fallback_ipif != NULL) ?
13044 	    fallback_ipif->ipif_net_mask : net_mask);
13045 }
13046 
13047 /*
13048  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13049  */
13050 static void
13051 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13052 {
13053 	IOCP	iocp;
13054 	ipft_t	*ipft;
13055 	ipllc_t	*ipllc;
13056 	mblk_t	*mp1;
13057 	cred_t	*cr;
13058 	int	error = 0;
13059 	conn_t	*connp;
13060 
13061 	ip1dbg(("ip_wput_ioctl"));
13062 	iocp = (IOCP)mp->b_rptr;
13063 	mp1 = mp->b_cont;
13064 	if (mp1 == NULL) {
13065 		iocp->ioc_error = EINVAL;
13066 		mp->b_datap->db_type = M_IOCNAK;
13067 		iocp->ioc_count = 0;
13068 		qreply(q, mp);
13069 		return;
13070 	}
13071 
13072 	/*
13073 	 * These IOCTLs provide various control capabilities to
13074 	 * upstream agents such as ULPs and processes.	There
13075 	 * are currently two such IOCTLs implemented.  They
13076 	 * are used by TCP to provide update information for
13077 	 * existing IREs and to forcibly delete an IRE for a
13078 	 * host that is not responding, thereby forcing an
13079 	 * attempt at a new route.
13080 	 */
13081 	iocp->ioc_error = EINVAL;
13082 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13083 		goto done;
13084 
13085 	ipllc = (ipllc_t *)mp1->b_rptr;
13086 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13087 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13088 			break;
13089 	}
13090 	/*
13091 	 * prefer credential from mblk over ioctl;
13092 	 * see ip_sioctl_copyin_setup
13093 	 */
13094 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13095 
13096 	/*
13097 	 * Refhold the conn in case the request gets queued up in some lookup
13098 	 */
13099 	ASSERT(CONN_Q(q));
13100 	connp = Q_TO_CONN(q);
13101 	CONN_INC_REF(connp);
13102 	if (ipft->ipft_pfi &&
13103 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13104 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13105 		error = (*ipft->ipft_pfi)(q,
13106 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13107 	}
13108 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13109 		/*
13110 		 * CONN_OPER_PENDING_DONE happens in the function called
13111 		 * through ipft_pfi above.
13112 		 */
13113 		return;
13114 	}
13115 
13116 	CONN_OPER_PENDING_DONE(connp);
13117 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13118 		freemsg(mp);
13119 		return;
13120 	}
13121 	iocp->ioc_error = error;
13122 
13123 done:
13124 	mp->b_datap->db_type = M_IOCACK;
13125 	if (iocp->ioc_error)
13126 		iocp->ioc_count = 0;
13127 	qreply(q, mp);
13128 }
13129 
13130 /*
13131  * Lookup an ipif using the sequence id (ipif_seqid)
13132  */
13133 ipif_t *
13134 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13135 {
13136 	ipif_t *ipif;
13137 
13138 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13139 
13140 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13141 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13142 			return (ipif);
13143 	}
13144 	return (NULL);
13145 }
13146 
13147 /*
13148  * Assign a unique id for the ipif. This is used later when we send
13149  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13150  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13151  * IRE is added, we verify that ipif has not disappeared.
13152  */
13153 
13154 static void
13155 ipif_assign_seqid(ipif_t *ipif)
13156 {
13157 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13158 
13159 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13160 }
13161 
13162 /*
13163  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13164  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13165  * be inserted into the first space available in the list. The value of
13166  * ipif_id will then be set to the appropriate value for its position.
13167  */
13168 static int
13169 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13170 {
13171 	ill_t *ill;
13172 	ipif_t *tipif;
13173 	ipif_t **tipifp;
13174 	int id;
13175 	ip_stack_t	*ipst;
13176 
13177 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13178 	    IAM_WRITER_IPIF(ipif));
13179 
13180 	ill = ipif->ipif_ill;
13181 	ASSERT(ill != NULL);
13182 	ipst = ill->ill_ipst;
13183 
13184 	/*
13185 	 * In the case of lo0:0 we already hold the ill_g_lock.
13186 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13187 	 * ipif_insert. Another such caller is ipif_move.
13188 	 */
13189 	if (acquire_g_lock)
13190 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13191 	if (acquire_ill_lock)
13192 		mutex_enter(&ill->ill_lock);
13193 	id = ipif->ipif_id;
13194 	tipifp = &(ill->ill_ipif);
13195 	if (id == -1) {	/* need to find a real id */
13196 		id = 0;
13197 		while ((tipif = *tipifp) != NULL) {
13198 			ASSERT(tipif->ipif_id >= id);
13199 			if (tipif->ipif_id != id)
13200 				break; /* non-consecutive id */
13201 			id++;
13202 			tipifp = &(tipif->ipif_next);
13203 		}
13204 		/* limit number of logical interfaces */
13205 		if (id >= ipst->ips_ip_addrs_per_if) {
13206 			if (acquire_ill_lock)
13207 				mutex_exit(&ill->ill_lock);
13208 			if (acquire_g_lock)
13209 				rw_exit(&ipst->ips_ill_g_lock);
13210 			return (-1);
13211 		}
13212 		ipif->ipif_id = id; /* assign new id */
13213 	} else if (id < ipst->ips_ip_addrs_per_if) {
13214 		/* we have a real id; insert ipif in the right place */
13215 		while ((tipif = *tipifp) != NULL) {
13216 			ASSERT(tipif->ipif_id != id);
13217 			if (tipif->ipif_id > id)
13218 				break; /* found correct location */
13219 			tipifp = &(tipif->ipif_next);
13220 		}
13221 	} else {
13222 		if (acquire_ill_lock)
13223 			mutex_exit(&ill->ill_lock);
13224 		if (acquire_g_lock)
13225 			rw_exit(&ipst->ips_ill_g_lock);
13226 		return (-1);
13227 	}
13228 
13229 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13230 
13231 	ipif->ipif_next = tipif;
13232 	*tipifp = ipif;
13233 	if (acquire_ill_lock)
13234 		mutex_exit(&ill->ill_lock);
13235 	if (acquire_g_lock)
13236 		rw_exit(&ipst->ips_ill_g_lock);
13237 	return (0);
13238 }
13239 
13240 static void
13241 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13242 {
13243 	ipif_t	**ipifp;
13244 	ill_t	*ill = ipif->ipif_ill;
13245 
13246 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13247 	if (acquire_ill_lock)
13248 		mutex_enter(&ill->ill_lock);
13249 	else
13250 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13251 
13252 	ipifp = &ill->ill_ipif;
13253 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13254 		if (*ipifp == ipif) {
13255 			*ipifp = ipif->ipif_next;
13256 			break;
13257 		}
13258 	}
13259 
13260 	if (acquire_ill_lock)
13261 		mutex_exit(&ill->ill_lock);
13262 }
13263 
13264 /*
13265  * Allocate and initialize a new interface control structure.  (Always
13266  * called as writer.)
13267  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13268  * is not part of the global linked list of ills. ipif_seqid is unique
13269  * in the system and to preserve the uniqueness, it is assigned only
13270  * when ill becomes part of the global list. At that point ill will
13271  * have a name. If it doesn't get assigned here, it will get assigned
13272  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13273  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13274  * the interface flags or any other information from the DL_INFO_ACK for
13275  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13276  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13277  * second DL_INFO_ACK comes in from the driver.
13278  */
13279 static ipif_t *
13280 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13281 {
13282 	ipif_t	*ipif;
13283 	phyint_t *phyi;
13284 
13285 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13286 	    ill->ill_name, id, (void *)ill));
13287 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13288 
13289 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13290 		return (NULL);
13291 	*ipif = ipif_zero;	/* start clean */
13292 
13293 	ipif->ipif_ill = ill;
13294 	ipif->ipif_id = id;	/* could be -1 */
13295 	/*
13296 	 * Inherit the zoneid from the ill; for the shared stack instance
13297 	 * this is always the global zone
13298 	 */
13299 	ipif->ipif_zoneid = ill->ill_zoneid;
13300 
13301 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13302 
13303 	ipif->ipif_refcnt = 0;
13304 	ipif->ipif_saved_ire_cnt = 0;
13305 
13306 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13307 		mi_free(ipif);
13308 		return (NULL);
13309 	}
13310 	/* -1 id should have been replaced by real id */
13311 	id = ipif->ipif_id;
13312 	ASSERT(id >= 0);
13313 
13314 	if (ill->ill_name[0] != '\0')
13315 		ipif_assign_seqid(ipif);
13316 
13317 	/*
13318 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13319 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13320 	 * ioctl sets ipif_orig_ipifid to zero.
13321 	 */
13322 	ipif->ipif_orig_ipifid = id;
13323 
13324 	/*
13325 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13326 	 * The ipif is still not up and can't be looked up until the
13327 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13328 	 */
13329 	mutex_enter(&ill->ill_lock);
13330 	mutex_enter(&ill->ill_phyint->phyint_lock);
13331 	/*
13332 	 * Set the running flag when logical interface zero is created.
13333 	 * For subsequent logical interfaces, a DLPI link down
13334 	 * notification message may have cleared the running flag to
13335 	 * indicate the link is down, so we shouldn't just blindly set it.
13336 	 */
13337 	if (id == 0)
13338 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13339 	ipif->ipif_ire_type = ire_type;
13340 	phyi = ill->ill_phyint;
13341 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13342 
13343 	if (ipif->ipif_isv6) {
13344 		ill->ill_flags |= ILLF_IPV6;
13345 	} else {
13346 		ipaddr_t inaddr_any = INADDR_ANY;
13347 
13348 		ill->ill_flags |= ILLF_IPV4;
13349 
13350 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13351 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13352 		    &ipif->ipif_v6lcl_addr);
13353 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13354 		    &ipif->ipif_v6src_addr);
13355 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13356 		    &ipif->ipif_v6subnet);
13357 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13358 		    &ipif->ipif_v6net_mask);
13359 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13360 		    &ipif->ipif_v6brd_addr);
13361 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13362 		    &ipif->ipif_v6pp_dst_addr);
13363 	}
13364 
13365 	/*
13366 	 * Don't set the interface flags etc. now, will do it in
13367 	 * ip_ll_subnet_defaults.
13368 	 */
13369 	if (!initialize) {
13370 		mutex_exit(&ill->ill_lock);
13371 		mutex_exit(&ill->ill_phyint->phyint_lock);
13372 		return (ipif);
13373 	}
13374 	ipif->ipif_mtu = ill->ill_max_mtu;
13375 
13376 	if (ill->ill_bcast_addr_length != 0) {
13377 		/*
13378 		 * Later detect lack of DLPI driver multicast
13379 		 * capability by catching DL_ENABMULTI errors in
13380 		 * ip_rput_dlpi.
13381 		 */
13382 		ill->ill_flags |= ILLF_MULTICAST;
13383 		if (!ipif->ipif_isv6)
13384 			ipif->ipif_flags |= IPIF_BROADCAST;
13385 	} else {
13386 		if (ill->ill_net_type != IRE_LOOPBACK) {
13387 			if (ipif->ipif_isv6)
13388 				/*
13389 				 * Note: xresolv interfaces will eventually need
13390 				 * NOARP set here as well, but that will require
13391 				 * those external resolvers to have some
13392 				 * knowledge of that flag and act appropriately.
13393 				 * Not to be changed at present.
13394 				 */
13395 				ill->ill_flags |= ILLF_NONUD;
13396 			else
13397 				ill->ill_flags |= ILLF_NOARP;
13398 		}
13399 		if (ill->ill_phys_addr_length == 0) {
13400 			if (ill->ill_media &&
13401 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13402 				ipif->ipif_flags |= IPIF_NOXMIT;
13403 				phyi->phyint_flags |= PHYI_VIRTUAL;
13404 			} else {
13405 				/* pt-pt supports multicast. */
13406 				ill->ill_flags |= ILLF_MULTICAST;
13407 				if (ill->ill_net_type == IRE_LOOPBACK) {
13408 					phyi->phyint_flags |=
13409 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13410 				} else {
13411 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13412 				}
13413 			}
13414 		}
13415 	}
13416 	mutex_exit(&ill->ill_lock);
13417 	mutex_exit(&ill->ill_phyint->phyint_lock);
13418 	return (ipif);
13419 }
13420 
13421 /*
13422  * If appropriate, send a message up to the resolver delete the entry
13423  * for the address of this interface which is going out of business.
13424  * (Always called as writer).
13425  *
13426  * NOTE : We need to check for NULL mps as some of the fields are
13427  *	  initialized only for some interface types. See ipif_resolver_up()
13428  *	  for details.
13429  */
13430 void
13431 ipif_arp_down(ipif_t *ipif)
13432 {
13433 	mblk_t	*mp;
13434 	ill_t	*ill = ipif->ipif_ill;
13435 
13436 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13437 	ASSERT(IAM_WRITER_IPIF(ipif));
13438 
13439 	/* Delete the mapping for the local address */
13440 	mp = ipif->ipif_arp_del_mp;
13441 	if (mp != NULL) {
13442 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13443 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13444 		putnext(ill->ill_rq, mp);
13445 		ipif->ipif_arp_del_mp = NULL;
13446 	}
13447 
13448 	/*
13449 	 * If this is the last ipif that is going down and there are no
13450 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13451 	 * clean up ARP completely.
13452 	 */
13453 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13454 
13455 		/* Send up AR_INTERFACE_DOWN message */
13456 		mp = ill->ill_arp_down_mp;
13457 		if (mp != NULL) {
13458 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13459 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13460 			    ipif->ipif_id));
13461 			putnext(ill->ill_rq, mp);
13462 			ill->ill_arp_down_mp = NULL;
13463 		}
13464 
13465 		/* Tell ARP to delete the multicast mappings */
13466 		mp = ill->ill_arp_del_mapping_mp;
13467 		if (mp != NULL) {
13468 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13469 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13470 			    ipif->ipif_id));
13471 			putnext(ill->ill_rq, mp);
13472 			ill->ill_arp_del_mapping_mp = NULL;
13473 		}
13474 	}
13475 }
13476 
13477 /*
13478  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13479  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13480  * that it wants the add_mp allocated in this function to be returned
13481  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13482  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13483  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13484  * as it does a ipif_arp_down after calling this function - which will
13485  * remove what we add here.
13486  *
13487  * Returns -1 on failures and 0 on success.
13488  */
13489 int
13490 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13491 {
13492 	mblk_t	*del_mp = NULL;
13493 	mblk_t *add_mp = NULL;
13494 	mblk_t *mp;
13495 	ill_t	*ill = ipif->ipif_ill;
13496 	phyint_t *phyi = ill->ill_phyint;
13497 	ipaddr_t addr, mask, extract_mask = 0;
13498 	arma_t	*arma;
13499 	uint8_t *maddr, *bphys_addr;
13500 	uint32_t hw_start;
13501 	dl_unitdata_req_t *dlur;
13502 
13503 	ASSERT(IAM_WRITER_IPIF(ipif));
13504 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13505 		return (0);
13506 
13507 	/*
13508 	 * Delete the existing mapping from ARP. Normally ipif_down
13509 	 * -> ipif_arp_down should send this up to ARP. The only
13510 	 * reason we would find this when we are switching from
13511 	 * Multicast to Broadcast where we did not do a down.
13512 	 */
13513 	mp = ill->ill_arp_del_mapping_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, ipif->ipif_id));
13517 		putnext(ill->ill_rq, mp);
13518 		ill->ill_arp_del_mapping_mp = NULL;
13519 	}
13520 
13521 	if (arp_add_mapping_mp != NULL)
13522 		*arp_add_mapping_mp = NULL;
13523 
13524 	/*
13525 	 * Check that the address is not to long for the constant
13526 	 * length reserved in the template arma_t.
13527 	 */
13528 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13529 		return (-1);
13530 
13531 	/* Add mapping mblk */
13532 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13533 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13534 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13535 	    (caddr_t)&addr);
13536 	if (add_mp == NULL)
13537 		return (-1);
13538 	arma = (arma_t *)add_mp->b_rptr;
13539 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13540 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13541 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13542 
13543 	/*
13544 	 * Determine the broadcast address.
13545 	 */
13546 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13547 	if (ill->ill_sap_length < 0)
13548 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13549 	else
13550 		bphys_addr = (uchar_t *)dlur +
13551 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13552 	/*
13553 	 * Check PHYI_MULTI_BCAST and length of physical
13554 	 * address to determine if we use the mapping or the
13555 	 * broadcast address.
13556 	 */
13557 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13558 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13559 		    bphys_addr, maddr, &hw_start, &extract_mask))
13560 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13561 
13562 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13563 	    (ill->ill_flags & ILLF_MULTICAST)) {
13564 		/* Make sure this will not match the "exact" entry. */
13565 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13566 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13567 		    (caddr_t)&addr);
13568 		if (del_mp == NULL) {
13569 			freemsg(add_mp);
13570 			return (-1);
13571 		}
13572 		bcopy(&extract_mask, (char *)arma +
13573 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13574 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13575 			/* Use link-layer broadcast address for MULTI_BCAST */
13576 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13577 			ip2dbg(("ipif_arp_setup_multicast: adding"
13578 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13579 		} else {
13580 			arma->arma_hw_mapping_start = hw_start;
13581 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13582 			    " ARP setup for %s\n", ill->ill_name));
13583 		}
13584 	} else {
13585 		freemsg(add_mp);
13586 		ASSERT(del_mp == NULL);
13587 		/* It is neither MULTICAST nor MULTI_BCAST */
13588 		return (0);
13589 	}
13590 	ASSERT(add_mp != NULL && del_mp != NULL);
13591 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13592 	ill->ill_arp_del_mapping_mp = del_mp;
13593 	if (arp_add_mapping_mp != NULL) {
13594 		/* The caller just wants the mblks allocated */
13595 		*arp_add_mapping_mp = add_mp;
13596 	} else {
13597 		/* The caller wants us to send it to arp */
13598 		putnext(ill->ill_rq, add_mp);
13599 	}
13600 	return (0);
13601 }
13602 
13603 /*
13604  * Get the resolver set up for a new interface address.
13605  * (Always called as writer.)
13606  * Called both for IPv4 and IPv6 interfaces,
13607  * though it only sets up the resolver for v6
13608  * if it's an xresolv interface (one using an external resolver).
13609  * Honors ILLF_NOARP.
13610  * The enumerated value res_act is used to tune the behavior.
13611  * If set to Res_act_initial, then we set up all the resolver
13612  * structures for a new interface.  If set to Res_act_move, then
13613  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13614  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13615  * asynchronous hardware address change notification.  If set to
13616  * Res_act_defend, then we tell ARP that it needs to send a single
13617  * gratuitous message in defense of the address.
13618  * Returns error on failure.
13619  */
13620 int
13621 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13622 {
13623 	caddr_t	addr;
13624 	mblk_t	*arp_up_mp = NULL;
13625 	mblk_t	*arp_down_mp = NULL;
13626 	mblk_t	*arp_add_mp = NULL;
13627 	mblk_t	*arp_del_mp = NULL;
13628 	mblk_t	*arp_add_mapping_mp = NULL;
13629 	mblk_t	*arp_del_mapping_mp = NULL;
13630 	ill_t	*ill = ipif->ipif_ill;
13631 	uchar_t	*area_p = NULL;
13632 	uchar_t	*ared_p = NULL;
13633 	int	err = ENOMEM;
13634 	boolean_t was_dup;
13635 
13636 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13637 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13638 	ASSERT(IAM_WRITER_IPIF(ipif));
13639 
13640 	was_dup = B_FALSE;
13641 	if (res_act == Res_act_initial) {
13642 		ipif->ipif_addr_ready = 0;
13643 		/*
13644 		 * We're bringing an interface up here.  There's no way that we
13645 		 * should need to shut down ARP now.
13646 		 */
13647 		mutex_enter(&ill->ill_lock);
13648 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13649 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13650 			ill->ill_ipif_dup_count--;
13651 			was_dup = B_TRUE;
13652 		}
13653 		mutex_exit(&ill->ill_lock);
13654 	}
13655 	if (ipif->ipif_recovery_id != 0)
13656 		(void) untimeout(ipif->ipif_recovery_id);
13657 	ipif->ipif_recovery_id = 0;
13658 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13659 		ipif->ipif_addr_ready = 1;
13660 		return (0);
13661 	}
13662 	/* NDP will set the ipif_addr_ready flag when it's ready */
13663 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13664 		return (0);
13665 
13666 	if (ill->ill_isv6) {
13667 		/*
13668 		 * External resolver for IPv6
13669 		 */
13670 		ASSERT(res_act == Res_act_initial);
13671 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13672 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13673 			area_p = (uchar_t *)&ip6_area_template;
13674 			ared_p = (uchar_t *)&ip6_ared_template;
13675 		}
13676 	} else {
13677 		/*
13678 		 * IPv4 arp case. If the ARP stream has already started
13679 		 * closing, fail this request for ARP bringup. Else
13680 		 * record the fact that an ARP bringup is pending.
13681 		 */
13682 		mutex_enter(&ill->ill_lock);
13683 		if (ill->ill_arp_closing) {
13684 			mutex_exit(&ill->ill_lock);
13685 			err = EINVAL;
13686 			goto failed;
13687 		} else {
13688 			if (ill->ill_ipif_up_count == 0 &&
13689 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13690 				ill->ill_arp_bringup_pending = 1;
13691 			mutex_exit(&ill->ill_lock);
13692 		}
13693 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13694 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13695 			area_p = (uchar_t *)&ip_area_template;
13696 			ared_p = (uchar_t *)&ip_ared_template;
13697 		}
13698 	}
13699 
13700 	/*
13701 	 * Add an entry for the local address in ARP only if it
13702 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13703 	 */
13704 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13705 		area_t *area;
13706 
13707 		/* Now ask ARP to publish our address. */
13708 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13709 		if (arp_add_mp == NULL)
13710 			goto failed;
13711 		area = (area_t *)arp_add_mp->b_rptr;
13712 		if (res_act != Res_act_initial) {
13713 			/*
13714 			 * Copy the new hardware address and length into
13715 			 * arp_add_mp to be sent to ARP.
13716 			 */
13717 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13718 			bcopy(ill->ill_phys_addr,
13719 			    ((char *)area + area->area_hw_addr_offset),
13720 			    area->area_hw_addr_length);
13721 		}
13722 
13723 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13724 		    ACE_F_MYADDR;
13725 
13726 		if (res_act == Res_act_defend) {
13727 			area->area_flags |= ACE_F_DEFEND;
13728 			/*
13729 			 * If we're just defending our address now, then
13730 			 * there's no need to set up ARP multicast mappings.
13731 			 * The publish command is enough.
13732 			 */
13733 			goto done;
13734 		}
13735 
13736 		if (res_act != Res_act_initial)
13737 			goto arp_setup_multicast;
13738 
13739 		/*
13740 		 * Allocate an ARP deletion message so we know we can tell ARP
13741 		 * when the interface goes down.
13742 		 */
13743 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13744 		if (arp_del_mp == NULL)
13745 			goto failed;
13746 
13747 	} else {
13748 		if (res_act != Res_act_initial)
13749 			goto done;
13750 	}
13751 	/*
13752 	 * Need to bring up ARP or setup multicast mapping only
13753 	 * when the first interface is coming UP.
13754 	 */
13755 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13756 	    was_dup) {
13757 		goto done;
13758 	}
13759 
13760 	/*
13761 	 * Allocate an ARP down message (to be saved) and an ARP up
13762 	 * message.
13763 	 */
13764 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13765 	if (arp_down_mp == NULL)
13766 		goto failed;
13767 
13768 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13769 	if (arp_up_mp == NULL)
13770 		goto failed;
13771 
13772 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13773 		goto done;
13774 
13775 arp_setup_multicast:
13776 	/*
13777 	 * Setup the multicast mappings. This function initializes
13778 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13779 	 * IPv6.
13780 	 */
13781 	if (!ill->ill_isv6) {
13782 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13783 		if (err != 0)
13784 			goto failed;
13785 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13786 		ASSERT(arp_add_mapping_mp != NULL);
13787 	}
13788 
13789 done:
13790 	if (arp_del_mp != NULL) {
13791 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13792 		ipif->ipif_arp_del_mp = arp_del_mp;
13793 	}
13794 	if (arp_down_mp != NULL) {
13795 		ASSERT(ill->ill_arp_down_mp == NULL);
13796 		ill->ill_arp_down_mp = arp_down_mp;
13797 	}
13798 	if (arp_del_mapping_mp != NULL) {
13799 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13800 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13801 	}
13802 	if (arp_up_mp != NULL) {
13803 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13804 		    ill->ill_name, ipif->ipif_id));
13805 		putnext(ill->ill_rq, arp_up_mp);
13806 	}
13807 	if (arp_add_mp != NULL) {
13808 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13809 		    ill->ill_name, ipif->ipif_id));
13810 		/*
13811 		 * If it's an extended ARP implementation, then we'll wait to
13812 		 * hear that DAD has finished before using the interface.
13813 		 */
13814 		if (!ill->ill_arp_extend)
13815 			ipif->ipif_addr_ready = 1;
13816 		putnext(ill->ill_rq, arp_add_mp);
13817 	} else {
13818 		ipif->ipif_addr_ready = 1;
13819 	}
13820 	if (arp_add_mapping_mp != NULL) {
13821 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13822 		    ill->ill_name, ipif->ipif_id));
13823 		putnext(ill->ill_rq, arp_add_mapping_mp);
13824 	}
13825 	if (res_act != Res_act_initial)
13826 		return (0);
13827 
13828 	if (ill->ill_flags & ILLF_NOARP)
13829 		err = ill_arp_off(ill);
13830 	else
13831 		err = ill_arp_on(ill);
13832 	if (err != 0) {
13833 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13834 		freemsg(ipif->ipif_arp_del_mp);
13835 		freemsg(ill->ill_arp_down_mp);
13836 		freemsg(ill->ill_arp_del_mapping_mp);
13837 		ipif->ipif_arp_del_mp = NULL;
13838 		ill->ill_arp_down_mp = NULL;
13839 		ill->ill_arp_del_mapping_mp = NULL;
13840 		return (err);
13841 	}
13842 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13843 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13844 
13845 failed:
13846 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13847 	freemsg(arp_add_mp);
13848 	freemsg(arp_del_mp);
13849 	freemsg(arp_add_mapping_mp);
13850 	freemsg(arp_up_mp);
13851 	freemsg(arp_down_mp);
13852 	ill->ill_arp_bringup_pending = 0;
13853 	return (err);
13854 }
13855 
13856 /*
13857  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13858  * just gone back up.
13859  */
13860 static void
13861 ipif_arp_start_dad(ipif_t *ipif)
13862 {
13863 	ill_t *ill = ipif->ipif_ill;
13864 	mblk_t *arp_add_mp;
13865 	area_t *area;
13866 
13867 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13868 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13869 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13870 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13871 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13872 		/*
13873 		 * If we can't contact ARP for some reason, that's not really a
13874 		 * problem.  Just send out the routing socket notification that
13875 		 * DAD completion would have done, and continue.
13876 		 */
13877 		ipif_mask_reply(ipif);
13878 		ip_rts_ifmsg(ipif);
13879 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13880 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13881 		ipif->ipif_addr_ready = 1;
13882 		return;
13883 	}
13884 
13885 	/* Setting the 'unverified' flag restarts DAD */
13886 	area = (area_t *)arp_add_mp->b_rptr;
13887 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13888 	    ACE_F_UNVERIFIED;
13889 	putnext(ill->ill_rq, arp_add_mp);
13890 }
13891 
13892 static void
13893 ipif_ndp_start_dad(ipif_t *ipif)
13894 {
13895 	nce_t *nce;
13896 
13897 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13898 	if (nce == NULL)
13899 		return;
13900 
13901 	if (!ndp_restart_dad(nce)) {
13902 		/*
13903 		 * If we can't restart DAD for some reason, that's not really a
13904 		 * problem.  Just send out the routing socket notification that
13905 		 * DAD completion would have done, and continue.
13906 		 */
13907 		ip_rts_ifmsg(ipif);
13908 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13909 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13910 		ipif->ipif_addr_ready = 1;
13911 	}
13912 	NCE_REFRELE(nce);
13913 }
13914 
13915 /*
13916  * Restart duplicate address detection on all interfaces on the given ill.
13917  *
13918  * This is called when an interface transitions from down to up
13919  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13920  *
13921  * Note that since the underlying physical link has transitioned, we must cause
13922  * at least one routing socket message to be sent here, either via DAD
13923  * completion or just by default on the first ipif.  (If we don't do this, then
13924  * in.mpathd will see long delays when doing link-based failure recovery.)
13925  */
13926 void
13927 ill_restart_dad(ill_t *ill, boolean_t went_up)
13928 {
13929 	ipif_t *ipif;
13930 
13931 	if (ill == NULL)
13932 		return;
13933 
13934 	/*
13935 	 * If layer two doesn't support duplicate address detection, then just
13936 	 * send the routing socket message now and be done with it.
13937 	 */
13938 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13939 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13940 		ip_rts_ifmsg(ill->ill_ipif);
13941 		return;
13942 	}
13943 
13944 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13945 		if (went_up) {
13946 			if (ipif->ipif_flags & IPIF_UP) {
13947 				if (ill->ill_isv6)
13948 					ipif_ndp_start_dad(ipif);
13949 				else
13950 					ipif_arp_start_dad(ipif);
13951 			} else if (ill->ill_isv6 &&
13952 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13953 				/*
13954 				 * For IPv4, the ARP module itself will
13955 				 * automatically start the DAD process when it
13956 				 * sees DL_NOTE_LINK_UP.  We respond to the
13957 				 * AR_CN_READY at the completion of that task.
13958 				 * For IPv6, we must kick off the bring-up
13959 				 * process now.
13960 				 */
13961 				ndp_do_recovery(ipif);
13962 			} else {
13963 				/*
13964 				 * Unfortunately, the first ipif is "special"
13965 				 * and represents the underlying ill in the
13966 				 * routing socket messages.  Thus, when this
13967 				 * one ipif is down, we must still notify so
13968 				 * that the user knows the IFF_RUNNING status
13969 				 * change.  (If the first ipif is up, then
13970 				 * we'll handle eventual routing socket
13971 				 * notification via DAD completion.)
13972 				 */
13973 				if (ipif == ill->ill_ipif)
13974 					ip_rts_ifmsg(ill->ill_ipif);
13975 			}
13976 		} else {
13977 			/*
13978 			 * After link down, we'll need to send a new routing
13979 			 * message when the link comes back, so clear
13980 			 * ipif_addr_ready.
13981 			 */
13982 			ipif->ipif_addr_ready = 0;
13983 		}
13984 	}
13985 
13986 	/*
13987 	 * If we've torn down links, then notify the user right away.
13988 	 */
13989 	if (!went_up)
13990 		ip_rts_ifmsg(ill->ill_ipif);
13991 }
13992 
13993 /*
13994  * Wakeup all threads waiting to enter the ipsq, and sleeping
13995  * on any of the ills in this ipsq. The ill_lock of the ill
13996  * must be held so that waiters don't miss wakeups
13997  */
13998 static void
13999 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14000 {
14001 	phyint_t *phyint;
14002 
14003 	phyint = ipsq->ipsq_phyint_list;
14004 	while (phyint != NULL) {
14005 		if (phyint->phyint_illv4) {
14006 			if (!caller_holds_lock)
14007 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14008 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14009 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14010 			if (!caller_holds_lock)
14011 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14012 		}
14013 		if (phyint->phyint_illv6) {
14014 			if (!caller_holds_lock)
14015 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14016 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14017 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14018 			if (!caller_holds_lock)
14019 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14020 		}
14021 		phyint = phyint->phyint_ipsq_next;
14022 	}
14023 }
14024 
14025 static ipsq_t *
14026 ipsq_create(char *groupname, ip_stack_t *ipst)
14027 {
14028 	ipsq_t	*ipsq;
14029 
14030 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14031 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14032 	if (ipsq == NULL) {
14033 		return (NULL);
14034 	}
14035 
14036 	if (groupname != NULL)
14037 		(void) strcpy(ipsq->ipsq_name, groupname);
14038 	else
14039 		ipsq->ipsq_name[0] = '\0';
14040 
14041 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14042 	ipsq->ipsq_flags |= IPSQ_GROUP;
14043 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14044 	ipst->ips_ipsq_g_head = ipsq;
14045 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14046 	return (ipsq);
14047 }
14048 
14049 /*
14050  * Return an ipsq correspoding to the groupname. If 'create' is true
14051  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14052  * uniquely with an IPMP group. However during IPMP groupname operations,
14053  * multiple IPMP groups may be associated with a single ipsq. But no
14054  * IPMP group can be associated with more than 1 ipsq at any time.
14055  * For example
14056  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14057  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14058  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14059  *
14060  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14061  * status shown below during the execution of the above command.
14062  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14063  *
14064  * After the completion of the above groupname command we return to the stable
14065  * state shown below.
14066  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14067  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14068  *
14069  * Because of the above, we don't search based on the ipsq_name since that
14070  * would miss the correct ipsq during certain windows as shown above.
14071  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14072  * natural state.
14073  */
14074 static ipsq_t *
14075 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14076     ip_stack_t *ipst)
14077 {
14078 	ipsq_t	*ipsq;
14079 	int	group_len;
14080 	phyint_t *phyint;
14081 
14082 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14083 
14084 	group_len = strlen(groupname);
14085 	ASSERT(group_len != 0);
14086 	group_len++;
14087 
14088 	for (ipsq = ipst->ips_ipsq_g_head;
14089 	    ipsq != NULL;
14090 	    ipsq = ipsq->ipsq_next) {
14091 		/*
14092 		 * When an ipsq is being split, and ill_split_ipsq
14093 		 * calls this function, we exclude it from being considered.
14094 		 */
14095 		if (ipsq == exclude_ipsq)
14096 			continue;
14097 
14098 		/*
14099 		 * Compare against the ipsq_name. The groupname change happens
14100 		 * in 2 phases. The 1st phase merges the from group into
14101 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14102 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14103 		 * ipsq_name. At this point the phyint_groupname has not been
14104 		 * updated.
14105 		 */
14106 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14107 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14108 			/*
14109 			 * Verify that an ipmp groupname is exactly
14110 			 * part of 1 ipsq and is not found in any other
14111 			 * ipsq.
14112 			 */
14113 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14114 			    NULL);
14115 			return (ipsq);
14116 		}
14117 
14118 		/*
14119 		 * Comparison against ipsq_name alone is not sufficient.
14120 		 * In the case when groups are currently being
14121 		 * merged, the ipsq could hold other IPMP groups temporarily.
14122 		 * so we walk the phyint list and compare against the
14123 		 * phyint_groupname as well.
14124 		 */
14125 		phyint = ipsq->ipsq_phyint_list;
14126 		while (phyint != NULL) {
14127 			if ((group_len == phyint->phyint_groupname_len) &&
14128 			    (bcmp(phyint->phyint_groupname, groupname,
14129 			    group_len) == 0)) {
14130 				/*
14131 				 * Verify that an ipmp groupname is exactly
14132 				 * part of 1 ipsq and is not found in any other
14133 				 * ipsq.
14134 				 */
14135 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14136 				    ipst) == NULL);
14137 				return (ipsq);
14138 			}
14139 			phyint = phyint->phyint_ipsq_next;
14140 		}
14141 	}
14142 	if (create)
14143 		ipsq = ipsq_create(groupname, ipst);
14144 	return (ipsq);
14145 }
14146 
14147 static void
14148 ipsq_delete(ipsq_t *ipsq)
14149 {
14150 	ipsq_t *nipsq;
14151 	ipsq_t *pipsq = NULL;
14152 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14153 
14154 	/*
14155 	 * We don't hold the ipsq lock, but we are sure no new
14156 	 * messages can land up, since the ipsq_refs is zero.
14157 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14158 	 * is associated with this ipsq. (Lookups are based on ill_name
14159 	 * or phyint_groupname)
14160 	 */
14161 	ASSERT(ipsq->ipsq_refs == 0);
14162 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14163 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14164 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14165 		/*
14166 		 * This is not the ipsq of an IPMP group.
14167 		 */
14168 		ipsq->ipsq_ipst = NULL;
14169 		kmem_free(ipsq, sizeof (ipsq_t));
14170 		return;
14171 	}
14172 
14173 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14174 
14175 	/*
14176 	 * Locate the ipsq  before we can remove it from
14177 	 * the singly linked list of ipsq's.
14178 	 */
14179 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14180 	    nipsq = nipsq->ipsq_next) {
14181 		if (nipsq == ipsq) {
14182 			break;
14183 		}
14184 		pipsq = nipsq;
14185 	}
14186 
14187 	ASSERT(nipsq == ipsq);
14188 
14189 	/* unlink ipsq from the list */
14190 	if (pipsq != NULL)
14191 		pipsq->ipsq_next = ipsq->ipsq_next;
14192 	else
14193 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14194 	ipsq->ipsq_ipst = NULL;
14195 	kmem_free(ipsq, sizeof (ipsq_t));
14196 	rw_exit(&ipst->ips_ill_g_lock);
14197 }
14198 
14199 static void
14200 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14201     queue_t *q)
14202 {
14203 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14204 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14205 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14206 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14207 	ASSERT(current_mp != NULL);
14208 
14209 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14210 	    NEW_OP, NULL);
14211 
14212 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14213 	    new_ipsq->ipsq_xopq_mphead != NULL);
14214 
14215 	/*
14216 	 * move from old ipsq to the new ipsq.
14217 	 */
14218 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14219 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14220 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14221 
14222 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14223 }
14224 
14225 void
14226 ill_group_cleanup(ill_t *ill)
14227 {
14228 	ill_t *ill_v4;
14229 	ill_t *ill_v6;
14230 	ipif_t *ipif;
14231 
14232 	ill_v4 = ill->ill_phyint->phyint_illv4;
14233 	ill_v6 = ill->ill_phyint->phyint_illv6;
14234 
14235 	if (ill_v4 != NULL) {
14236 		mutex_enter(&ill_v4->ill_lock);
14237 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14238 		    ipif = ipif->ipif_next) {
14239 			IPIF_UNMARK_MOVING(ipif);
14240 		}
14241 		ill_v4->ill_up_ipifs = B_FALSE;
14242 		mutex_exit(&ill_v4->ill_lock);
14243 	}
14244 
14245 	if (ill_v6 != NULL) {
14246 		mutex_enter(&ill_v6->ill_lock);
14247 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14248 		    ipif = ipif->ipif_next) {
14249 			IPIF_UNMARK_MOVING(ipif);
14250 		}
14251 		ill_v6->ill_up_ipifs = B_FALSE;
14252 		mutex_exit(&ill_v6->ill_lock);
14253 	}
14254 }
14255 /*
14256  * This function is called when an ill has had a change in its group status
14257  * to bring up all the ipifs that were up before the change.
14258  */
14259 int
14260 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14261 {
14262 	ipif_t *ipif;
14263 	ill_t *ill_v4;
14264 	ill_t *ill_v6;
14265 	ill_t *from_ill;
14266 	int err = 0;
14267 
14268 
14269 	ASSERT(IAM_WRITER_ILL(ill));
14270 
14271 	/*
14272 	 * Except for ipif_state_flags and ill_state_flags the other
14273 	 * fields of the ipif/ill that are modified below are protected
14274 	 * implicitly since we are a writer. We would have tried to down
14275 	 * even an ipif that was already down, in ill_down_ipifs. So we
14276 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14277 	 */
14278 	ill_v4 = ill->ill_phyint->phyint_illv4;
14279 	ill_v6 = ill->ill_phyint->phyint_illv6;
14280 	if (ill_v4 != NULL) {
14281 		ill_v4->ill_up_ipifs = B_TRUE;
14282 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14283 		    ipif = ipif->ipif_next) {
14284 			mutex_enter(&ill_v4->ill_lock);
14285 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14286 			IPIF_UNMARK_MOVING(ipif);
14287 			mutex_exit(&ill_v4->ill_lock);
14288 			if (ipif->ipif_was_up) {
14289 				if (!(ipif->ipif_flags & IPIF_UP))
14290 					err = ipif_up(ipif, q, mp);
14291 				ipif->ipif_was_up = B_FALSE;
14292 				if (err != 0) {
14293 					/*
14294 					 * Can there be any other error ?
14295 					 */
14296 					ASSERT(err == EINPROGRESS);
14297 					return (err);
14298 				}
14299 			}
14300 		}
14301 		mutex_enter(&ill_v4->ill_lock);
14302 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14303 		mutex_exit(&ill_v4->ill_lock);
14304 		ill_v4->ill_up_ipifs = B_FALSE;
14305 		if (ill_v4->ill_move_in_progress) {
14306 			ASSERT(ill_v4->ill_move_peer != NULL);
14307 			ill_v4->ill_move_in_progress = B_FALSE;
14308 			from_ill = ill_v4->ill_move_peer;
14309 			from_ill->ill_move_in_progress = B_FALSE;
14310 			from_ill->ill_move_peer = NULL;
14311 			mutex_enter(&from_ill->ill_lock);
14312 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14313 			mutex_exit(&from_ill->ill_lock);
14314 			if (ill_v6 == NULL) {
14315 				if (from_ill->ill_phyint->phyint_flags &
14316 				    PHYI_STANDBY) {
14317 					phyint_inactive(from_ill->ill_phyint);
14318 				}
14319 				if (ill_v4->ill_phyint->phyint_flags &
14320 				    PHYI_STANDBY) {
14321 					phyint_inactive(ill_v4->ill_phyint);
14322 				}
14323 			}
14324 			ill_v4->ill_move_peer = NULL;
14325 		}
14326 	}
14327 
14328 	if (ill_v6 != NULL) {
14329 		ill_v6->ill_up_ipifs = B_TRUE;
14330 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14331 		    ipif = ipif->ipif_next) {
14332 			mutex_enter(&ill_v6->ill_lock);
14333 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14334 			IPIF_UNMARK_MOVING(ipif);
14335 			mutex_exit(&ill_v6->ill_lock);
14336 			if (ipif->ipif_was_up) {
14337 				if (!(ipif->ipif_flags & IPIF_UP))
14338 					err = ipif_up(ipif, q, mp);
14339 				ipif->ipif_was_up = B_FALSE;
14340 				if (err != 0) {
14341 					/*
14342 					 * Can there be any other error ?
14343 					 */
14344 					ASSERT(err == EINPROGRESS);
14345 					return (err);
14346 				}
14347 			}
14348 		}
14349 		mutex_enter(&ill_v6->ill_lock);
14350 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14351 		mutex_exit(&ill_v6->ill_lock);
14352 		ill_v6->ill_up_ipifs = B_FALSE;
14353 		if (ill_v6->ill_move_in_progress) {
14354 			ASSERT(ill_v6->ill_move_peer != NULL);
14355 			ill_v6->ill_move_in_progress = B_FALSE;
14356 			from_ill = ill_v6->ill_move_peer;
14357 			from_ill->ill_move_in_progress = B_FALSE;
14358 			from_ill->ill_move_peer = NULL;
14359 			mutex_enter(&from_ill->ill_lock);
14360 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14361 			mutex_exit(&from_ill->ill_lock);
14362 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14363 				phyint_inactive(from_ill->ill_phyint);
14364 			}
14365 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14366 				phyint_inactive(ill_v6->ill_phyint);
14367 			}
14368 			ill_v6->ill_move_peer = NULL;
14369 		}
14370 	}
14371 	return (0);
14372 }
14373 
14374 /*
14375  * bring down all the approriate ipifs.
14376  */
14377 /* ARGSUSED */
14378 static void
14379 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14380 {
14381 	ipif_t *ipif;
14382 
14383 	ASSERT(IAM_WRITER_ILL(ill));
14384 
14385 	/*
14386 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14387 	 * are modified below are protected implicitly since we are a writer
14388 	 */
14389 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14390 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14391 			continue;
14392 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14393 			/*
14394 			 * We go through the ipif_down logic even if the ipif
14395 			 * is already down, since routes can be added based
14396 			 * on down ipifs. Going through ipif_down once again
14397 			 * will delete any IREs created based on these routes.
14398 			 */
14399 			if (ipif->ipif_flags & IPIF_UP)
14400 				ipif->ipif_was_up = B_TRUE;
14401 			/*
14402 			 * If called with chk_nofailover true ipif is moving.
14403 			 */
14404 			mutex_enter(&ill->ill_lock);
14405 			if (chk_nofailover) {
14406 				ipif->ipif_state_flags |=
14407 				    IPIF_MOVING | IPIF_CHANGING;
14408 			} else {
14409 				ipif->ipif_state_flags |= IPIF_CHANGING;
14410 			}
14411 			mutex_exit(&ill->ill_lock);
14412 			/*
14413 			 * Need to re-create net/subnet bcast ires if
14414 			 * they are dependent on ipif.
14415 			 */
14416 			if (!ipif->ipif_isv6)
14417 				ipif_check_bcast_ires(ipif);
14418 			(void) ipif_logical_down(ipif, NULL, NULL);
14419 			ipif_non_duplicate(ipif);
14420 			ipif_down_tail(ipif);
14421 		}
14422 	}
14423 }
14424 
14425 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14426 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14427 	(ipsq)->ipsq_refs++;				\
14428 }
14429 
14430 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14431 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14432 	(ipsq)->ipsq_refs--;				\
14433 	if ((ipsq)->ipsq_refs == 0)				\
14434 		(ipsq)->ipsq_name[0] = '\0'; 		\
14435 }
14436 
14437 /*
14438  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14439  * new_ipsq.
14440  */
14441 static void
14442 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14443 {
14444 	phyint_t *phyint;
14445 	phyint_t *next_phyint;
14446 
14447 	/*
14448 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14449 	 * writer and the ill_lock of the ill in question. Also the dest
14450 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14451 	 */
14452 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14453 
14454 	phyint = cur_ipsq->ipsq_phyint_list;
14455 	cur_ipsq->ipsq_phyint_list = NULL;
14456 	while (phyint != NULL) {
14457 		next_phyint = phyint->phyint_ipsq_next;
14458 		IPSQ_DEC_REF(cur_ipsq, ipst);
14459 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14460 		new_ipsq->ipsq_phyint_list = phyint;
14461 		IPSQ_INC_REF(new_ipsq, ipst);
14462 		phyint->phyint_ipsq = new_ipsq;
14463 		phyint = next_phyint;
14464 	}
14465 }
14466 
14467 #define	SPLIT_SUCCESS		0
14468 #define	SPLIT_NOT_NEEDED	1
14469 #define	SPLIT_FAILED		2
14470 
14471 int
14472 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14473     ip_stack_t *ipst)
14474 {
14475 	ipsq_t *newipsq = NULL;
14476 
14477 	/*
14478 	 * Assertions denote pre-requisites for changing the ipsq of
14479 	 * a phyint
14480 	 */
14481 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14482 	/*
14483 	 * <ill-phyint> assocs can't change while ill_g_lock
14484 	 * is held as writer. See ill_phyint_reinit()
14485 	 */
14486 	ASSERT(phyint->phyint_illv4 == NULL ||
14487 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14488 	ASSERT(phyint->phyint_illv6 == NULL ||
14489 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14490 
14491 	if ((phyint->phyint_groupname_len !=
14492 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14493 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14494 	    phyint->phyint_groupname_len) != 0)) {
14495 		/*
14496 		 * Once we fail in creating a new ipsq due to memory shortage,
14497 		 * don't attempt to create new ipsq again, based on another
14498 		 * phyint, since we want all phyints belonging to an IPMP group
14499 		 * to be in the same ipsq even in the event of mem alloc fails.
14500 		 */
14501 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14502 		    cur_ipsq, ipst);
14503 		if (newipsq == NULL) {
14504 			/* Memory allocation failure */
14505 			return (SPLIT_FAILED);
14506 		} else {
14507 			/* ipsq_refs protected by ill_g_lock (writer) */
14508 			IPSQ_DEC_REF(cur_ipsq, ipst);
14509 			phyint->phyint_ipsq = newipsq;
14510 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14511 			newipsq->ipsq_phyint_list = phyint;
14512 			IPSQ_INC_REF(newipsq, ipst);
14513 			return (SPLIT_SUCCESS);
14514 		}
14515 	}
14516 	return (SPLIT_NOT_NEEDED);
14517 }
14518 
14519 /*
14520  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14521  * to do this split
14522  */
14523 static int
14524 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14525 {
14526 	ipsq_t *newipsq;
14527 
14528 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14529 	/*
14530 	 * <ill-phyint> assocs can't change while ill_g_lock
14531 	 * is held as writer. See ill_phyint_reinit()
14532 	 */
14533 
14534 	ASSERT(phyint->phyint_illv4 == NULL ||
14535 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14536 	ASSERT(phyint->phyint_illv6 == NULL ||
14537 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14538 
14539 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14540 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14541 		/*
14542 		 * ipsq_init failed due to no memory
14543 		 * caller will use the same ipsq
14544 		 */
14545 		return (SPLIT_FAILED);
14546 	}
14547 
14548 	/* ipsq_ref is protected by ill_g_lock (writer) */
14549 	IPSQ_DEC_REF(cur_ipsq, ipst);
14550 
14551 	/*
14552 	 * This is a new ipsq that is unknown to the world.
14553 	 * So we don't need to hold ipsq_lock,
14554 	 */
14555 	newipsq = phyint->phyint_ipsq;
14556 	newipsq->ipsq_writer = NULL;
14557 	newipsq->ipsq_reentry_cnt--;
14558 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14559 #ifdef DEBUG
14560 	newipsq->ipsq_depth = 0;
14561 #endif
14562 
14563 	return (SPLIT_SUCCESS);
14564 }
14565 
14566 /*
14567  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14568  * ipsq's representing their individual groups or themselves. Return
14569  * whether split needs to be retried again later.
14570  */
14571 static boolean_t
14572 ill_split_ipsq(ipsq_t *cur_ipsq)
14573 {
14574 	phyint_t *phyint;
14575 	phyint_t *next_phyint;
14576 	int	error;
14577 	boolean_t need_retry = B_FALSE;
14578 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14579 
14580 	phyint = cur_ipsq->ipsq_phyint_list;
14581 	cur_ipsq->ipsq_phyint_list = NULL;
14582 	while (phyint != NULL) {
14583 		next_phyint = phyint->phyint_ipsq_next;
14584 		/*
14585 		 * 'created' will tell us whether the callee actually
14586 		 * created an ipsq. Lack of memory may force the callee
14587 		 * to return without creating an ipsq.
14588 		 */
14589 		if (phyint->phyint_groupname == NULL) {
14590 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14591 		} else {
14592 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14593 			    need_retry, ipst);
14594 		}
14595 
14596 		switch (error) {
14597 		case SPLIT_FAILED:
14598 			need_retry = B_TRUE;
14599 			/* FALLTHRU */
14600 		case SPLIT_NOT_NEEDED:
14601 			/*
14602 			 * Keep it on the list.
14603 			 */
14604 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14605 			cur_ipsq->ipsq_phyint_list = phyint;
14606 			break;
14607 		case SPLIT_SUCCESS:
14608 			break;
14609 		default:
14610 			ASSERT(0);
14611 		}
14612 
14613 		phyint = next_phyint;
14614 	}
14615 	return (need_retry);
14616 }
14617 
14618 /*
14619  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14620  * and return the ills in the list. This list will be
14621  * needed to unlock all the ills later on by the caller.
14622  * The <ill-ipsq> associations could change between the
14623  * lock and unlock. Hence the unlock can't traverse the
14624  * ipsq to get the list of ills.
14625  */
14626 static int
14627 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14628 {
14629 	int	cnt = 0;
14630 	phyint_t	*phyint;
14631 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14632 
14633 	/*
14634 	 * The caller holds ill_g_lock to ensure that the ill memberships
14635 	 * of the ipsq don't change
14636 	 */
14637 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14638 
14639 	phyint = ipsq->ipsq_phyint_list;
14640 	while (phyint != NULL) {
14641 		if (phyint->phyint_illv4 != NULL) {
14642 			ASSERT(cnt < list_max);
14643 			list[cnt++] = phyint->phyint_illv4;
14644 		}
14645 		if (phyint->phyint_illv6 != NULL) {
14646 			ASSERT(cnt < list_max);
14647 			list[cnt++] = phyint->phyint_illv6;
14648 		}
14649 		phyint = phyint->phyint_ipsq_next;
14650 	}
14651 	ill_lock_ills(list, cnt);
14652 	return (cnt);
14653 }
14654 
14655 void
14656 ill_lock_ills(ill_t **list, int cnt)
14657 {
14658 	int	i;
14659 
14660 	if (cnt > 1) {
14661 		boolean_t try_again;
14662 		do {
14663 			try_again = B_FALSE;
14664 			for (i = 0; i < cnt - 1; i++) {
14665 				if (list[i] < list[i + 1]) {
14666 					ill_t	*tmp;
14667 
14668 					/* swap the elements */
14669 					tmp = list[i];
14670 					list[i] = list[i + 1];
14671 					list[i + 1] = tmp;
14672 					try_again = B_TRUE;
14673 				}
14674 			}
14675 		} while (try_again);
14676 	}
14677 
14678 	for (i = 0; i < cnt; i++) {
14679 		if (i == 0) {
14680 			if (list[i] != NULL)
14681 				mutex_enter(&list[i]->ill_lock);
14682 			else
14683 				return;
14684 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14685 			mutex_enter(&list[i]->ill_lock);
14686 		}
14687 	}
14688 }
14689 
14690 void
14691 ill_unlock_ills(ill_t **list, int cnt)
14692 {
14693 	int	i;
14694 
14695 	for (i = 0; i < cnt; i++) {
14696 		if ((i == 0) && (list[i] != NULL)) {
14697 			mutex_exit(&list[i]->ill_lock);
14698 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14699 			mutex_exit(&list[i]->ill_lock);
14700 		}
14701 	}
14702 }
14703 
14704 /*
14705  * Merge all the ills from 1 ipsq group into another ipsq group.
14706  * The source ipsq group is specified by the ipsq associated with
14707  * 'from_ill'. The destination ipsq group is specified by the ipsq
14708  * associated with 'to_ill' or 'groupname' respectively.
14709  * Note that ipsq itself does not have a reference count mechanism
14710  * and functions don't look up an ipsq and pass it around. Instead
14711  * functions pass around an ill or groupname, and the ipsq is looked
14712  * up from the ill or groupname and the required operation performed
14713  * atomically with the lookup on the ipsq.
14714  */
14715 static int
14716 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14717     queue_t *q)
14718 {
14719 	ipsq_t *old_ipsq;
14720 	ipsq_t *new_ipsq;
14721 	ill_t	**ill_list;
14722 	int	cnt;
14723 	size_t	ill_list_size;
14724 	boolean_t became_writer_on_new_sq = B_FALSE;
14725 	ip_stack_t	*ipst = from_ill->ill_ipst;
14726 
14727 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14728 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14729 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14730 
14731 	/*
14732 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14733 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14734 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14735 	 */
14736 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14737 
14738 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14739 	if (groupname != NULL)
14740 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14741 	else {
14742 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14743 	}
14744 
14745 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14746 
14747 	/*
14748 	 * both groups are on the same ipsq.
14749 	 */
14750 	if (old_ipsq == new_ipsq) {
14751 		rw_exit(&ipst->ips_ill_g_lock);
14752 		return (0);
14753 	}
14754 
14755 	cnt = old_ipsq->ipsq_refs << 1;
14756 	ill_list_size = cnt * sizeof (ill_t *);
14757 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14758 	if (ill_list == NULL) {
14759 		rw_exit(&ipst->ips_ill_g_lock);
14760 		return (ENOMEM);
14761 	}
14762 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14763 
14764 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14765 	mutex_enter(&new_ipsq->ipsq_lock);
14766 	if ((new_ipsq->ipsq_writer == NULL &&
14767 	    new_ipsq->ipsq_current_ipif == NULL) ||
14768 	    (new_ipsq->ipsq_writer == curthread)) {
14769 		new_ipsq->ipsq_writer = curthread;
14770 		new_ipsq->ipsq_reentry_cnt++;
14771 		became_writer_on_new_sq = B_TRUE;
14772 	}
14773 
14774 	/*
14775 	 * We are holding ill_g_lock as writer and all the ill locks of
14776 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14777 	 * message can land up on the old ipsq even though we don't hold the
14778 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14779 	 */
14780 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14781 
14782 	/*
14783 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14784 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14785 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14786 	 */
14787 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14788 
14789 	/*
14790 	 * Mark the new ipsq as needing a split since it is currently
14791 	 * being shared by more than 1 IPMP group. The split will
14792 	 * occur at the end of ipsq_exit
14793 	 */
14794 	new_ipsq->ipsq_split = B_TRUE;
14795 
14796 	/* Now release all the locks */
14797 	mutex_exit(&new_ipsq->ipsq_lock);
14798 	ill_unlock_ills(ill_list, cnt);
14799 	rw_exit(&ipst->ips_ill_g_lock);
14800 
14801 	kmem_free(ill_list, ill_list_size);
14802 
14803 	/*
14804 	 * If we succeeded in becoming writer on the new ipsq, then
14805 	 * drain the new ipsq and start processing  all enqueued messages
14806 	 * including the current ioctl we are processing which is either
14807 	 * a set groupname or failover/failback.
14808 	 */
14809 	if (became_writer_on_new_sq)
14810 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14811 
14812 	/*
14813 	 * syncq has been changed and all the messages have been moved.
14814 	 */
14815 	mutex_enter(&old_ipsq->ipsq_lock);
14816 	old_ipsq->ipsq_current_ipif = NULL;
14817 	old_ipsq->ipsq_current_ioctl = 0;
14818 	mutex_exit(&old_ipsq->ipsq_lock);
14819 	return (EINPROGRESS);
14820 }
14821 
14822 /*
14823  * Delete and add the loopback copy and non-loopback copy of
14824  * the BROADCAST ire corresponding to ill and addr. Used to
14825  * group broadcast ires together when ill becomes part of
14826  * a group.
14827  *
14828  * This function is also called when ill is leaving the group
14829  * so that the ires belonging to the group gets re-grouped.
14830  */
14831 static void
14832 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14833 {
14834 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14835 	ire_t **ire_ptpn = &ire_head;
14836 	ip_stack_t	*ipst = ill->ill_ipst;
14837 
14838 	/*
14839 	 * The loopback and non-loopback IREs are inserted in the order in which
14840 	 * they're found, on the basis that they are correctly ordered (loopback
14841 	 * first).
14842 	 */
14843 	for (;;) {
14844 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14845 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14846 		if (ire == NULL)
14847 			break;
14848 
14849 		/*
14850 		 * we are passing in KM_SLEEP because it is not easy to
14851 		 * go back to a sane state in case of memory failure.
14852 		 */
14853 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14854 		ASSERT(nire != NULL);
14855 		bzero(nire, sizeof (ire_t));
14856 		/*
14857 		 * Don't use ire_max_frag directly since we don't
14858 		 * hold on to 'ire' until we add the new ire 'nire' and
14859 		 * we don't want the new ire to have a dangling reference
14860 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14861 		 * be in sync with the ipif_mtu of the associate ipif.
14862 		 * For eg. this happens as a result of SIOCSLIFNAME,
14863 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14864 		 * the driver. A change in ire_max_frag triggered as
14865 		 * as a result of path mtu discovery, or due to an
14866 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14867 		 * route change -mtu command does not apply to broadcast ires.
14868 		 *
14869 		 * XXX We need a recovery strategy here if ire_init fails
14870 		 */
14871 		if (ire_init(nire,
14872 		    (uchar_t *)&ire->ire_addr,
14873 		    (uchar_t *)&ire->ire_mask,
14874 		    (uchar_t *)&ire->ire_src_addr,
14875 		    (uchar_t *)&ire->ire_gateway_addr,
14876 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14877 		    &ire->ire_ipif->ipif_mtu,
14878 		    ire->ire_nce,
14879 		    ire->ire_rfq,
14880 		    ire->ire_stq,
14881 		    ire->ire_type,
14882 		    ire->ire_ipif,
14883 		    ire->ire_cmask,
14884 		    ire->ire_phandle,
14885 		    ire->ire_ihandle,
14886 		    ire->ire_flags,
14887 		    &ire->ire_uinfo,
14888 		    NULL,
14889 		    NULL,
14890 		    ipst) == NULL) {
14891 			cmn_err(CE_PANIC, "ire_init() failed");
14892 		}
14893 		ire_delete(ire);
14894 		ire_refrele(ire);
14895 
14896 		/*
14897 		 * The newly created IREs are inserted at the tail of the list
14898 		 * starting with ire_head. As we've just allocated them no one
14899 		 * knows about them so it's safe.
14900 		 */
14901 		*ire_ptpn = nire;
14902 		ire_ptpn = &nire->ire_next;
14903 	}
14904 
14905 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14906 		int error;
14907 		ire_t *oire;
14908 		/* unlink the IRE from our list before calling ire_add() */
14909 		nire_next = nire->ire_next;
14910 		nire->ire_next = NULL;
14911 
14912 		/* ire_add adds the ire at the right place in the list */
14913 		oire = nire;
14914 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14915 		ASSERT(error == 0);
14916 		ASSERT(oire == nire);
14917 		ire_refrele(nire);	/* Held in ire_add */
14918 	}
14919 }
14920 
14921 /*
14922  * This function is usually called when an ill is inserted in
14923  * a group and all the ipifs are already UP. As all the ipifs
14924  * are already UP, the broadcast ires have already been created
14925  * and been inserted. But, ire_add_v4 would not have grouped properly.
14926  * We need to re-group for the benefit of ip_wput_ire which
14927  * expects BROADCAST ires to be grouped properly to avoid sending
14928  * more than one copy of the broadcast packet per group.
14929  *
14930  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14931  *	  because when ipif_up_done ends up calling this, ires have
14932  *        already been added before illgrp_insert i.e before ill_group
14933  *	  has been initialized.
14934  */
14935 static void
14936 ill_group_bcast_for_xmit(ill_t *ill)
14937 {
14938 	ill_group_t *illgrp;
14939 	ipif_t *ipif;
14940 	ipaddr_t addr;
14941 	ipaddr_t net_mask;
14942 	ipaddr_t subnet_netmask;
14943 
14944 	illgrp = ill->ill_group;
14945 
14946 	/*
14947 	 * This function is called even when an ill is deleted from
14948 	 * the group. Hence, illgrp could be null.
14949 	 */
14950 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14951 		return;
14952 
14953 	/*
14954 	 * Delete all the BROADCAST ires matching this ill and add
14955 	 * them back. This time, ire_add_v4 should take care of
14956 	 * grouping them with others because ill is part of the
14957 	 * group.
14958 	 */
14959 	ill_bcast_delete_and_add(ill, 0);
14960 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
14961 
14962 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14963 
14964 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14965 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14966 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14967 		} else {
14968 			net_mask = htonl(IN_CLASSA_NET);
14969 		}
14970 		addr = net_mask & ipif->ipif_subnet;
14971 		ill_bcast_delete_and_add(ill, addr);
14972 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
14973 
14974 		subnet_netmask = ipif->ipif_net_mask;
14975 		addr = ipif->ipif_subnet;
14976 		ill_bcast_delete_and_add(ill, addr);
14977 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
14978 	}
14979 }
14980 
14981 /*
14982  * This function is called from illgrp_delete when ill is being deleted
14983  * from the group.
14984  *
14985  * As ill is not there in the group anymore, any address belonging
14986  * to this ill should be cleared of IRE_MARK_NORECV.
14987  */
14988 static void
14989 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
14990 {
14991 	ire_t *ire;
14992 	irb_t *irb;
14993 	ip_stack_t	*ipst = ill->ill_ipst;
14994 
14995 	ASSERT(ill->ill_group == NULL);
14996 
14997 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14998 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14999 
15000 	if (ire != NULL) {
15001 		/*
15002 		 * IPMP and plumbing operations are serialized on the ipsq, so
15003 		 * no one will insert or delete a broadcast ire under our feet.
15004 		 */
15005 		irb = ire->ire_bucket;
15006 		rw_enter(&irb->irb_lock, RW_READER);
15007 		ire_refrele(ire);
15008 
15009 		for (; ire != NULL; ire = ire->ire_next) {
15010 			if (ire->ire_addr != addr)
15011 				break;
15012 			if (ire_to_ill(ire) != ill)
15013 				continue;
15014 
15015 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15016 			ire->ire_marks &= ~IRE_MARK_NORECV;
15017 		}
15018 		rw_exit(&irb->irb_lock);
15019 	}
15020 }
15021 
15022 /*
15023  * This function must be called only after the broadcast ires
15024  * have been grouped together. For a given address addr, nominate
15025  * only one of the ires whose interface is not FAILED or OFFLINE.
15026  *
15027  * This is also called when an ipif goes down, so that we can nominate
15028  * a different ire with the same address for receiving.
15029  */
15030 static void
15031 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15032 {
15033 	irb_t *irb;
15034 	ire_t *ire;
15035 	ire_t *ire1;
15036 	ire_t *save_ire;
15037 	ire_t **irep = NULL;
15038 	boolean_t first = B_TRUE;
15039 	ire_t *clear_ire = NULL;
15040 	ire_t *start_ire = NULL;
15041 	ire_t	*new_lb_ire;
15042 	ire_t	*new_nlb_ire;
15043 	boolean_t new_lb_ire_used = B_FALSE;
15044 	boolean_t new_nlb_ire_used = B_FALSE;
15045 	uint64_t match_flags;
15046 	uint64_t phyi_flags;
15047 	boolean_t fallback = B_FALSE;
15048 	uint_t	max_frag;
15049 
15050 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15051 	    NULL, MATCH_IRE_TYPE, ipst);
15052 	/*
15053 	 * We may not be able to find some ires if a previous
15054 	 * ire_create failed. This happens when an ipif goes
15055 	 * down and we are unable to create BROADCAST ires due
15056 	 * to memory failure. Thus, we have to check for NULL
15057 	 * below. This should handle the case for LOOPBACK,
15058 	 * POINTOPOINT and interfaces with some POINTOPOINT
15059 	 * logicals for which there are no BROADCAST ires.
15060 	 */
15061 	if (ire == NULL)
15062 		return;
15063 	/*
15064 	 * Currently IRE_BROADCASTS are deleted when an ipif
15065 	 * goes down which runs exclusively. Thus, setting
15066 	 * IRE_MARK_RCVD should not race with ire_delete marking
15067 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15068 	 * be consistent with other parts of the code that walks
15069 	 * a given bucket.
15070 	 */
15071 	save_ire = ire;
15072 	irb = ire->ire_bucket;
15073 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15074 	if (new_lb_ire == NULL) {
15075 		ire_refrele(ire);
15076 		return;
15077 	}
15078 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15079 	if (new_nlb_ire == NULL) {
15080 		ire_refrele(ire);
15081 		kmem_cache_free(ire_cache, new_lb_ire);
15082 		return;
15083 	}
15084 	IRB_REFHOLD(irb);
15085 	rw_enter(&irb->irb_lock, RW_WRITER);
15086 	/*
15087 	 * Get to the first ire matching the address and the
15088 	 * group. If the address does not match we are done
15089 	 * as we could not find the IRE. If the address matches
15090 	 * we should get to the first one matching the group.
15091 	 */
15092 	while (ire != NULL) {
15093 		if (ire->ire_addr != addr ||
15094 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15095 			break;
15096 		}
15097 		ire = ire->ire_next;
15098 	}
15099 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15100 	start_ire = ire;
15101 redo:
15102 	while (ire != NULL && ire->ire_addr == addr &&
15103 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15104 		/*
15105 		 * The first ire for any address within a group
15106 		 * should always be the one with IRE_MARK_NORECV cleared
15107 		 * so that ip_wput_ire can avoid searching for one.
15108 		 * Note down the insertion point which will be used
15109 		 * later.
15110 		 */
15111 		if (first && (irep == NULL))
15112 			irep = ire->ire_ptpn;
15113 		/*
15114 		 * PHYI_FAILED is set when the interface fails.
15115 		 * This interface might have become good, but the
15116 		 * daemon has not yet detected. We should still
15117 		 * not receive on this. PHYI_OFFLINE should never
15118 		 * be picked as this has been offlined and soon
15119 		 * be removed.
15120 		 */
15121 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15122 		if (phyi_flags & PHYI_OFFLINE) {
15123 			ire->ire_marks |= IRE_MARK_NORECV;
15124 			ire = ire->ire_next;
15125 			continue;
15126 		}
15127 		if (phyi_flags & match_flags) {
15128 			ire->ire_marks |= IRE_MARK_NORECV;
15129 			ire = ire->ire_next;
15130 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15131 			    PHYI_INACTIVE) {
15132 				fallback = B_TRUE;
15133 			}
15134 			continue;
15135 		}
15136 		if (first) {
15137 			/*
15138 			 * We will move this to the front of the list later
15139 			 * on.
15140 			 */
15141 			clear_ire = ire;
15142 			ire->ire_marks &= ~IRE_MARK_NORECV;
15143 		} else {
15144 			ire->ire_marks |= IRE_MARK_NORECV;
15145 		}
15146 		first = B_FALSE;
15147 		ire = ire->ire_next;
15148 	}
15149 	/*
15150 	 * If we never nominated anybody, try nominating at least
15151 	 * an INACTIVE, if we found one. Do it only once though.
15152 	 */
15153 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15154 	    fallback) {
15155 		match_flags = PHYI_FAILED;
15156 		ire = start_ire;
15157 		irep = NULL;
15158 		goto redo;
15159 	}
15160 	ire_refrele(save_ire);
15161 
15162 	/*
15163 	 * irep non-NULL indicates that we entered the while loop
15164 	 * above. If clear_ire is at the insertion point, we don't
15165 	 * have to do anything. clear_ire will be NULL if all the
15166 	 * interfaces are failed.
15167 	 *
15168 	 * We cannot unlink and reinsert the ire at the right place
15169 	 * in the list since there can be other walkers of this bucket.
15170 	 * Instead we delete and recreate the ire
15171 	 */
15172 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15173 		ire_t *clear_ire_stq = NULL;
15174 
15175 		bzero(new_lb_ire, sizeof (ire_t));
15176 		/* XXX We need a recovery strategy here. */
15177 		if (ire_init(new_lb_ire,
15178 		    (uchar_t *)&clear_ire->ire_addr,
15179 		    (uchar_t *)&clear_ire->ire_mask,
15180 		    (uchar_t *)&clear_ire->ire_src_addr,
15181 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15182 		    &clear_ire->ire_max_frag,
15183 		    NULL, /* let ire_nce_init derive the resolver info */
15184 		    clear_ire->ire_rfq,
15185 		    clear_ire->ire_stq,
15186 		    clear_ire->ire_type,
15187 		    clear_ire->ire_ipif,
15188 		    clear_ire->ire_cmask,
15189 		    clear_ire->ire_phandle,
15190 		    clear_ire->ire_ihandle,
15191 		    clear_ire->ire_flags,
15192 		    &clear_ire->ire_uinfo,
15193 		    NULL,
15194 		    NULL,
15195 		    ipst) == NULL)
15196 			cmn_err(CE_PANIC, "ire_init() failed");
15197 		if (clear_ire->ire_stq == NULL) {
15198 			ire_t *ire_next = clear_ire->ire_next;
15199 			if (ire_next != NULL &&
15200 			    ire_next->ire_stq != NULL &&
15201 			    ire_next->ire_addr == clear_ire->ire_addr &&
15202 			    ire_next->ire_ipif->ipif_ill ==
15203 			    clear_ire->ire_ipif->ipif_ill) {
15204 				clear_ire_stq = ire_next;
15205 
15206 				bzero(new_nlb_ire, sizeof (ire_t));
15207 				/* XXX We need a recovery strategy here. */
15208 				if (ire_init(new_nlb_ire,
15209 				    (uchar_t *)&clear_ire_stq->ire_addr,
15210 				    (uchar_t *)&clear_ire_stq->ire_mask,
15211 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15212 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15213 				    &clear_ire_stq->ire_max_frag,
15214 				    NULL,
15215 				    clear_ire_stq->ire_rfq,
15216 				    clear_ire_stq->ire_stq,
15217 				    clear_ire_stq->ire_type,
15218 				    clear_ire_stq->ire_ipif,
15219 				    clear_ire_stq->ire_cmask,
15220 				    clear_ire_stq->ire_phandle,
15221 				    clear_ire_stq->ire_ihandle,
15222 				    clear_ire_stq->ire_flags,
15223 				    &clear_ire_stq->ire_uinfo,
15224 				    NULL,
15225 				    NULL,
15226 				    ipst) == NULL)
15227 					cmn_err(CE_PANIC, "ire_init() failed");
15228 			}
15229 		}
15230 
15231 		/*
15232 		 * Delete the ire. We can't call ire_delete() since
15233 		 * we are holding the bucket lock. We can't release the
15234 		 * bucket lock since we can't allow irep to change. So just
15235 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15236 		 * ire from the list and do the refrele.
15237 		 */
15238 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15239 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15240 
15241 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15242 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15243 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15244 		}
15245 
15246 		/*
15247 		 * Also take care of otherfields like ib/ob pkt count
15248 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15249 		 */
15250 
15251 		/* Set the max_frag before adding the ire */
15252 		max_frag = *new_lb_ire->ire_max_fragp;
15253 		new_lb_ire->ire_max_fragp = NULL;
15254 		new_lb_ire->ire_max_frag = max_frag;
15255 
15256 		/* Add the new ire's. Insert at *irep */
15257 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15258 		ire1 = *irep;
15259 		if (ire1 != NULL)
15260 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15261 		new_lb_ire->ire_next = ire1;
15262 		/* Link the new one in. */
15263 		new_lb_ire->ire_ptpn = irep;
15264 		membar_producer();
15265 		*irep = new_lb_ire;
15266 		new_lb_ire_used = B_TRUE;
15267 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15268 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15269 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15270 
15271 		if (clear_ire_stq != NULL) {
15272 			/* Set the max_frag before adding the ire */
15273 			max_frag = *new_nlb_ire->ire_max_fragp;
15274 			new_nlb_ire->ire_max_fragp = NULL;
15275 			new_nlb_ire->ire_max_frag = max_frag;
15276 
15277 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15278 			irep = &new_lb_ire->ire_next;
15279 			/* Add the new ire. Insert at *irep */
15280 			ire1 = *irep;
15281 			if (ire1 != NULL)
15282 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15283 			new_nlb_ire->ire_next = ire1;
15284 			/* Link the new one in. */
15285 			new_nlb_ire->ire_ptpn = irep;
15286 			membar_producer();
15287 			*irep = new_nlb_ire;
15288 			new_nlb_ire_used = B_TRUE;
15289 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15290 			    ire_stats_inserted);
15291 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15292 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15293 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15294 		}
15295 	}
15296 	rw_exit(&irb->irb_lock);
15297 	if (!new_lb_ire_used)
15298 		kmem_cache_free(ire_cache, new_lb_ire);
15299 	if (!new_nlb_ire_used)
15300 		kmem_cache_free(ire_cache, new_nlb_ire);
15301 	IRB_REFRELE(irb);
15302 }
15303 
15304 /*
15305  * Whenever an ipif goes down we have to renominate a different
15306  * broadcast ire to receive. Whenever an ipif comes up, we need
15307  * to make sure that we have only one nominated to receive.
15308  */
15309 static void
15310 ipif_renominate_bcast(ipif_t *ipif)
15311 {
15312 	ill_t *ill = ipif->ipif_ill;
15313 	ipaddr_t subnet_addr;
15314 	ipaddr_t net_addr;
15315 	ipaddr_t net_mask = 0;
15316 	ipaddr_t subnet_netmask;
15317 	ipaddr_t addr;
15318 	ill_group_t *illgrp;
15319 	ip_stack_t	*ipst = ill->ill_ipst;
15320 
15321 	illgrp = ill->ill_group;
15322 	/*
15323 	 * If this is the last ipif going down, it might take
15324 	 * the ill out of the group. In that case ipif_down ->
15325 	 * illgrp_delete takes care of doing the nomination.
15326 	 * ipif_down does not call for this case.
15327 	 */
15328 	ASSERT(illgrp != NULL);
15329 
15330 	/* There could not have been any ires associated with this */
15331 	if (ipif->ipif_subnet == 0)
15332 		return;
15333 
15334 	ill_mark_bcast(illgrp, 0, ipst);
15335 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15336 
15337 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15338 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15339 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15340 	} else {
15341 		net_mask = htonl(IN_CLASSA_NET);
15342 	}
15343 	addr = net_mask & ipif->ipif_subnet;
15344 	ill_mark_bcast(illgrp, addr, ipst);
15345 
15346 	net_addr = ~net_mask | addr;
15347 	ill_mark_bcast(illgrp, net_addr, ipst);
15348 
15349 	subnet_netmask = ipif->ipif_net_mask;
15350 	addr = ipif->ipif_subnet;
15351 	ill_mark_bcast(illgrp, addr, ipst);
15352 
15353 	subnet_addr = ~subnet_netmask | addr;
15354 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15355 }
15356 
15357 /*
15358  * Whenever we form or delete ill groups, we need to nominate one set of
15359  * BROADCAST ires for receiving in the group.
15360  *
15361  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15362  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15363  *    for ill_ipif_up_count to be non-zero. This is the only case where
15364  *    ill_ipif_up_count is zero and we would still find the ires.
15365  *
15366  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15367  *    ipif is UP and we just have to do the nomination.
15368  *
15369  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15370  *    from the group. So, we have to do the nomination.
15371  *
15372  * Because of (3), there could be just one ill in the group. But we have
15373  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15374  * Thus, this function does not optimize when there is only one ill as
15375  * it is not correct for (3).
15376  */
15377 static void
15378 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15379 {
15380 	ill_t *ill;
15381 	ipif_t *ipif;
15382 	ipaddr_t subnet_addr;
15383 	ipaddr_t prev_subnet_addr = 0;
15384 	ipaddr_t net_addr;
15385 	ipaddr_t prev_net_addr = 0;
15386 	ipaddr_t net_mask = 0;
15387 	ipaddr_t subnet_netmask;
15388 	ipaddr_t addr;
15389 	ip_stack_t	*ipst;
15390 
15391 	/*
15392 	 * When the last memeber is leaving, there is nothing to
15393 	 * nominate.
15394 	 */
15395 	if (illgrp->illgrp_ill_count == 0) {
15396 		ASSERT(illgrp->illgrp_ill == NULL);
15397 		return;
15398 	}
15399 
15400 	ill = illgrp->illgrp_ill;
15401 	ASSERT(!ill->ill_isv6);
15402 	ipst = ill->ill_ipst;
15403 	/*
15404 	 * We assume that ires with same address and belonging to the
15405 	 * same group, has been grouped together. Nominating a *single*
15406 	 * ill in the group for sending and receiving broadcast is done
15407 	 * by making sure that the first BROADCAST ire (which will be
15408 	 * the one returned by ire_ctable_lookup for ip_rput and the
15409 	 * one that will be used in ip_wput_ire) will be the one that
15410 	 * will not have IRE_MARK_NORECV set.
15411 	 *
15412 	 * 1) ip_rput checks and discards packets received on ires marked
15413 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15414 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15415 	 *    first ire in the group for every broadcast address in the group.
15416 	 *    ip_rput will accept packets only on the first ire i.e only
15417 	 *    one copy of the ill.
15418 	 *
15419 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15420 	 *    packet for the whole group. It needs to send out on the ill
15421 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15422 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15423 	 *    the copy echoed back on other port where the ire is not marked
15424 	 *    with IRE_MARK_NORECV.
15425 	 *
15426 	 * Note that we just need to have the first IRE either loopback or
15427 	 * non-loopback (either of them may not exist if ire_create failed
15428 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15429 	 * always hit the first one and hence will always accept one copy.
15430 	 *
15431 	 * We have a broadcast ire per ill for all the unique prefixes
15432 	 * hosted on that ill. As we don't have a way of knowing the
15433 	 * unique prefixes on a given ill and hence in the whole group,
15434 	 * we just call ill_mark_bcast on all the prefixes that exist
15435 	 * in the group. For the common case of one prefix, the code
15436 	 * below optimizes by remebering the last address used for
15437 	 * markng. In the case of multiple prefixes, this will still
15438 	 * optimize depending the order of prefixes.
15439 	 *
15440 	 * The only unique address across the whole group is 0.0.0.0 and
15441 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15442 	 * the first ire in the bucket for receiving and disables the
15443 	 * others.
15444 	 */
15445 	ill_mark_bcast(illgrp, 0, ipst);
15446 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15447 	for (; ill != NULL; ill = ill->ill_group_next) {
15448 
15449 		for (ipif = ill->ill_ipif; ipif != NULL;
15450 		    ipif = ipif->ipif_next) {
15451 
15452 			if (!(ipif->ipif_flags & IPIF_UP) ||
15453 			    ipif->ipif_subnet == 0) {
15454 				continue;
15455 			}
15456 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15457 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15458 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15459 			} else {
15460 				net_mask = htonl(IN_CLASSA_NET);
15461 			}
15462 			addr = net_mask & ipif->ipif_subnet;
15463 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15464 				ill_mark_bcast(illgrp, addr, ipst);
15465 				net_addr = ~net_mask | addr;
15466 				ill_mark_bcast(illgrp, net_addr, ipst);
15467 			}
15468 			prev_net_addr = addr;
15469 
15470 			subnet_netmask = ipif->ipif_net_mask;
15471 			addr = ipif->ipif_subnet;
15472 			if (prev_subnet_addr == 0 ||
15473 			    prev_subnet_addr != addr) {
15474 				ill_mark_bcast(illgrp, addr, ipst);
15475 				subnet_addr = ~subnet_netmask | addr;
15476 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15477 			}
15478 			prev_subnet_addr = addr;
15479 		}
15480 	}
15481 }
15482 
15483 /*
15484  * This function is called while forming ill groups.
15485  *
15486  * Currently, we handle only allmulti groups. We want to join
15487  * allmulti on only one of the ills in the groups. In future,
15488  * when we have link aggregation, we may have to join normal
15489  * multicast groups on multiple ills as switch does inbound load
15490  * balancing. Following are the functions that calls this
15491  * function :
15492  *
15493  * 1) ill_recover_multicast : Interface is coming back UP.
15494  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15495  *    will call ill_recover_multicast to recover all the multicast
15496  *    groups. We need to make sure that only one member is joined
15497  *    in the ill group.
15498  *
15499  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15500  *    Somebody is joining allmulti. We need to make sure that only one
15501  *    member is joined in the group.
15502  *
15503  * 3) illgrp_insert : If allmulti has already joined, we need to make
15504  *    sure that only one member is joined in the group.
15505  *
15506  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15507  *    allmulti who we have nominated. We need to pick someother ill.
15508  *
15509  * 5) illgrp_delete : The ill we nominated is leaving the group,
15510  *    we need to pick a new ill to join the group.
15511  *
15512  * For (1), (2), (5) - we just have to check whether there is
15513  * a good ill joined in the group. If we could not find any ills
15514  * joined the group, we should join.
15515  *
15516  * For (4), the one that was nominated to receive, left the group.
15517  * There could be nobody joined in the group when this function is
15518  * called.
15519  *
15520  * For (3) - we need to explicitly check whether there are multiple
15521  * ills joined in the group.
15522  *
15523  * For simplicity, we don't differentiate any of the above cases. We
15524  * just leave the group if it is joined on any of them and join on
15525  * the first good ill.
15526  */
15527 int
15528 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15529 {
15530 	ilm_t *ilm;
15531 	ill_t *ill;
15532 	ill_t *fallback_inactive_ill = NULL;
15533 	ill_t *fallback_failed_ill = NULL;
15534 	int ret = 0;
15535 
15536 	/*
15537 	 * Leave the allmulti on all the ills and start fresh.
15538 	 */
15539 	for (ill = illgrp->illgrp_ill; ill != NULL;
15540 	    ill = ill->ill_group_next) {
15541 		if (ill->ill_join_allmulti)
15542 			(void) ip_leave_allmulti(ill->ill_ipif);
15543 	}
15544 
15545 	/*
15546 	 * Choose a good ill. Fallback to inactive or failed if
15547 	 * none available. We need to fallback to FAILED in the
15548 	 * case where we have 2 interfaces in a group - where
15549 	 * one of them is failed and another is a good one and
15550 	 * the good one (not marked inactive) is leaving the group.
15551 	 */
15552 	ret = 0;
15553 	for (ill = illgrp->illgrp_ill; ill != NULL;
15554 	    ill = ill->ill_group_next) {
15555 		/* Never pick an offline interface */
15556 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15557 			continue;
15558 
15559 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15560 			fallback_failed_ill = ill;
15561 			continue;
15562 		}
15563 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15564 			fallback_inactive_ill = ill;
15565 			continue;
15566 		}
15567 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15568 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15569 				ret = ip_join_allmulti(ill->ill_ipif);
15570 				/*
15571 				 * ip_join_allmulti can fail because of memory
15572 				 * failures. So, make sure we join at least
15573 				 * on one ill.
15574 				 */
15575 				if (ill->ill_join_allmulti)
15576 					return (0);
15577 			}
15578 		}
15579 	}
15580 	if (ret != 0) {
15581 		/*
15582 		 * If we tried nominating above and failed to do so,
15583 		 * return error. We might have tried multiple times.
15584 		 * But, return the latest error.
15585 		 */
15586 		return (ret);
15587 	}
15588 	if ((ill = fallback_inactive_ill) != NULL) {
15589 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15590 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15591 				ret = ip_join_allmulti(ill->ill_ipif);
15592 				return (ret);
15593 			}
15594 		}
15595 	} else if ((ill = fallback_failed_ill) != NULL) {
15596 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15597 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15598 				ret = ip_join_allmulti(ill->ill_ipif);
15599 				return (ret);
15600 			}
15601 		}
15602 	}
15603 	return (0);
15604 }
15605 
15606 /*
15607  * This function is called from illgrp_delete after it is
15608  * deleted from the group to reschedule responsibilities
15609  * to a different ill.
15610  */
15611 static void
15612 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15613 {
15614 	ilm_t	*ilm;
15615 	ipif_t	*ipif;
15616 	ipaddr_t subnet_addr;
15617 	ipaddr_t net_addr;
15618 	ipaddr_t net_mask = 0;
15619 	ipaddr_t subnet_netmask;
15620 	ipaddr_t addr;
15621 	ip_stack_t *ipst = ill->ill_ipst;
15622 
15623 	ASSERT(ill->ill_group == NULL);
15624 	/*
15625 	 * Broadcast Responsibility:
15626 	 *
15627 	 * 1. If this ill has been nominated for receiving broadcast
15628 	 * packets, we need to find a new one. Before we find a new
15629 	 * one, we need to re-group the ires that are part of this new
15630 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15631 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15632 	 * thing for us.
15633 	 *
15634 	 * 2. If this ill was not nominated for receiving broadcast
15635 	 * packets, we need to clear the IRE_MARK_NORECV flag
15636 	 * so that we continue to send up broadcast packets.
15637 	 */
15638 	if (!ill->ill_isv6) {
15639 		/*
15640 		 * Case 1 above : No optimization here. Just redo the
15641 		 * nomination.
15642 		 */
15643 		ill_group_bcast_for_xmit(ill);
15644 		ill_nominate_bcast_rcv(illgrp);
15645 
15646 		/*
15647 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15648 		 */
15649 		ill_clear_bcast_mark(ill, 0);
15650 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15651 
15652 		for (ipif = ill->ill_ipif; ipif != NULL;
15653 		    ipif = ipif->ipif_next) {
15654 
15655 			if (!(ipif->ipif_flags & IPIF_UP) ||
15656 			    ipif->ipif_subnet == 0) {
15657 				continue;
15658 			}
15659 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15660 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15661 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15662 			} else {
15663 				net_mask = htonl(IN_CLASSA_NET);
15664 			}
15665 			addr = net_mask & ipif->ipif_subnet;
15666 			ill_clear_bcast_mark(ill, addr);
15667 
15668 			net_addr = ~net_mask | addr;
15669 			ill_clear_bcast_mark(ill, net_addr);
15670 
15671 			subnet_netmask = ipif->ipif_net_mask;
15672 			addr = ipif->ipif_subnet;
15673 			ill_clear_bcast_mark(ill, addr);
15674 
15675 			subnet_addr = ~subnet_netmask | addr;
15676 			ill_clear_bcast_mark(ill, subnet_addr);
15677 		}
15678 	}
15679 
15680 	/*
15681 	 * Multicast Responsibility.
15682 	 *
15683 	 * If we have joined allmulti on this one, find a new member
15684 	 * in the group to join allmulti. As this ill is already part
15685 	 * of allmulti, we don't have to join on this one.
15686 	 *
15687 	 * If we have not joined allmulti on this one, there is no
15688 	 * responsibility to handoff. But we need to take new
15689 	 * responsibility i.e, join allmulti on this one if we need
15690 	 * to.
15691 	 */
15692 	if (ill->ill_join_allmulti) {
15693 		(void) ill_nominate_mcast_rcv(illgrp);
15694 	} else {
15695 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15696 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15697 				(void) ip_join_allmulti(ill->ill_ipif);
15698 				break;
15699 			}
15700 		}
15701 	}
15702 
15703 	/*
15704 	 * We intentionally do the flushing of IRE_CACHES only matching
15705 	 * on the ill and not on groups. Note that we are already deleted
15706 	 * from the group.
15707 	 *
15708 	 * This will make sure that all IRE_CACHES whose stq is pointing
15709 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15710 	 * deleted and IRE_CACHES that are not pointing at this ill will
15711 	 * be left alone.
15712 	 */
15713 	if (ill->ill_isv6) {
15714 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15715 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15716 	} else {
15717 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15718 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15719 	}
15720 
15721 	/*
15722 	 * Some conn may have cached one of the IREs deleted above. By removing
15723 	 * the ire reference, we clean up the extra reference to the ill held in
15724 	 * ire->ire_stq.
15725 	 */
15726 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15727 
15728 	/*
15729 	 * Re-do source address selection for all the members in the
15730 	 * group, if they borrowed source address from one of the ipifs
15731 	 * in this ill.
15732 	 */
15733 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15734 		if (ill->ill_isv6) {
15735 			ipif_update_other_ipifs_v6(ipif, illgrp);
15736 		} else {
15737 			ipif_update_other_ipifs(ipif, illgrp);
15738 		}
15739 	}
15740 }
15741 
15742 /*
15743  * Delete the ill from the group. The caller makes sure that it is
15744  * in a group and it okay to delete from the group. So, we always
15745  * delete here.
15746  */
15747 static void
15748 illgrp_delete(ill_t *ill)
15749 {
15750 	ill_group_t *illgrp;
15751 	ill_group_t *tmpg;
15752 	ill_t *tmp_ill;
15753 	ip_stack_t	*ipst = ill->ill_ipst;
15754 
15755 	/*
15756 	 * Reset illgrp_ill_schednext if it was pointing at us.
15757 	 * We need to do this before we set ill_group to NULL.
15758 	 */
15759 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15760 	mutex_enter(&ill->ill_lock);
15761 
15762 	illgrp_reset_schednext(ill);
15763 
15764 	illgrp = ill->ill_group;
15765 
15766 	/* Delete the ill from illgrp. */
15767 	if (illgrp->illgrp_ill == ill) {
15768 		illgrp->illgrp_ill = ill->ill_group_next;
15769 	} else {
15770 		tmp_ill = illgrp->illgrp_ill;
15771 		while (tmp_ill->ill_group_next != ill) {
15772 			tmp_ill = tmp_ill->ill_group_next;
15773 			ASSERT(tmp_ill != NULL);
15774 		}
15775 		tmp_ill->ill_group_next = ill->ill_group_next;
15776 	}
15777 	ill->ill_group = NULL;
15778 	ill->ill_group_next = NULL;
15779 
15780 	illgrp->illgrp_ill_count--;
15781 	mutex_exit(&ill->ill_lock);
15782 	rw_exit(&ipst->ips_ill_g_lock);
15783 
15784 	/*
15785 	 * As this ill is leaving the group, we need to hand off
15786 	 * the responsibilities to the other ills in the group, if
15787 	 * this ill had some responsibilities.
15788 	 */
15789 
15790 	ill_handoff_responsibility(ill, illgrp);
15791 
15792 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15793 
15794 	if (illgrp->illgrp_ill_count == 0) {
15795 
15796 		ASSERT(illgrp->illgrp_ill == NULL);
15797 		if (ill->ill_isv6) {
15798 			if (illgrp == ipst->ips_illgrp_head_v6) {
15799 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15800 			} else {
15801 				tmpg = ipst->ips_illgrp_head_v6;
15802 				while (tmpg->illgrp_next != illgrp) {
15803 					tmpg = tmpg->illgrp_next;
15804 					ASSERT(tmpg != NULL);
15805 				}
15806 				tmpg->illgrp_next = illgrp->illgrp_next;
15807 			}
15808 		} else {
15809 			if (illgrp == ipst->ips_illgrp_head_v4) {
15810 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15811 			} else {
15812 				tmpg = ipst->ips_illgrp_head_v4;
15813 				while (tmpg->illgrp_next != illgrp) {
15814 					tmpg = tmpg->illgrp_next;
15815 					ASSERT(tmpg != NULL);
15816 				}
15817 				tmpg->illgrp_next = illgrp->illgrp_next;
15818 			}
15819 		}
15820 		mutex_destroy(&illgrp->illgrp_lock);
15821 		mi_free(illgrp);
15822 	}
15823 	rw_exit(&ipst->ips_ill_g_lock);
15824 
15825 	/*
15826 	 * Even though the ill is out of the group its not necessary
15827 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15828 	 * We will split the ipsq when phyint_groupname is set to NULL.
15829 	 */
15830 
15831 	/*
15832 	 * Send a routing sockets message if we are deleting from
15833 	 * groups with names.
15834 	 */
15835 	if (ill->ill_phyint->phyint_groupname_len != 0)
15836 		ip_rts_ifmsg(ill->ill_ipif);
15837 }
15838 
15839 /*
15840  * Re-do source address selection. This is normally called when
15841  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15842  * ipif comes up.
15843  */
15844 void
15845 ill_update_source_selection(ill_t *ill)
15846 {
15847 	ipif_t *ipif;
15848 
15849 	ASSERT(IAM_WRITER_ILL(ill));
15850 
15851 	if (ill->ill_group != NULL)
15852 		ill = ill->ill_group->illgrp_ill;
15853 
15854 	for (; ill != NULL; ill = ill->ill_group_next) {
15855 		for (ipif = ill->ill_ipif; ipif != NULL;
15856 		    ipif = ipif->ipif_next) {
15857 			if (ill->ill_isv6)
15858 				ipif_recreate_interface_routes_v6(NULL, ipif);
15859 			else
15860 				ipif_recreate_interface_routes(NULL, ipif);
15861 		}
15862 	}
15863 }
15864 
15865 /*
15866  * Insert ill in a group headed by illgrp_head. The caller can either
15867  * pass a groupname in which case we search for a group with the
15868  * same name to insert in or pass a group to insert in. This function
15869  * would only search groups with names.
15870  *
15871  * NOTE : The caller should make sure that there is at least one ipif
15872  *	  UP on this ill so that illgrp_scheduler can pick this ill
15873  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15874  *	  already sent a DL_UNBIND to the driver and we don't want to
15875  *	  send anymore packets. We don't assert for ipif_up_count
15876  *	  to be greater than zero, because ipif_up_done wants to call
15877  *	  this function before bumping up the ipif_up_count. See
15878  *	  ipif_up_done() for details.
15879  */
15880 int
15881 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15882     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15883 {
15884 	ill_group_t *illgrp;
15885 	ill_t *prev_ill;
15886 	phyint_t *phyi;
15887 	ip_stack_t	*ipst = ill->ill_ipst;
15888 
15889 	ASSERT(ill->ill_group == NULL);
15890 
15891 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15892 	mutex_enter(&ill->ill_lock);
15893 
15894 	if (groupname != NULL) {
15895 		/*
15896 		 * Look for a group with a matching groupname to insert.
15897 		 */
15898 		for (illgrp = *illgrp_head; illgrp != NULL;
15899 		    illgrp = illgrp->illgrp_next) {
15900 
15901 			ill_t *tmp_ill;
15902 
15903 			/*
15904 			 * If we have an ill_group_t in the list which has
15905 			 * no ill_t assigned then we must be in the process of
15906 			 * removing this group. We skip this as illgrp_delete()
15907 			 * will remove it from the list.
15908 			 */
15909 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15910 				ASSERT(illgrp->illgrp_ill_count == 0);
15911 				continue;
15912 			}
15913 
15914 			ASSERT(tmp_ill->ill_phyint != NULL);
15915 			phyi = tmp_ill->ill_phyint;
15916 			/*
15917 			 * Look at groups which has names only.
15918 			 */
15919 			if (phyi->phyint_groupname_len == 0)
15920 				continue;
15921 			/*
15922 			 * Names are stored in the phyint common to both
15923 			 * IPv4 and IPv6.
15924 			 */
15925 			if (mi_strcmp(phyi->phyint_groupname,
15926 			    groupname) == 0) {
15927 				break;
15928 			}
15929 		}
15930 	} else {
15931 		/*
15932 		 * If the caller passes in a NULL "grp_to_insert", we
15933 		 * allocate one below and insert this singleton.
15934 		 */
15935 		illgrp = grp_to_insert;
15936 	}
15937 
15938 	ill->ill_group_next = NULL;
15939 
15940 	if (illgrp == NULL) {
15941 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15942 		if (illgrp == NULL) {
15943 			return (ENOMEM);
15944 		}
15945 		illgrp->illgrp_next = *illgrp_head;
15946 		*illgrp_head = illgrp;
15947 		illgrp->illgrp_ill = ill;
15948 		illgrp->illgrp_ill_count = 1;
15949 		ill->ill_group = illgrp;
15950 		/*
15951 		 * Used in illgrp_scheduler to protect multiple threads
15952 		 * from traversing the list.
15953 		 */
15954 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
15955 	} else {
15956 		ASSERT(ill->ill_net_type ==
15957 		    illgrp->illgrp_ill->ill_net_type);
15958 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
15959 
15960 		/* Insert ill at tail of this group */
15961 		prev_ill = illgrp->illgrp_ill;
15962 		while (prev_ill->ill_group_next != NULL)
15963 			prev_ill = prev_ill->ill_group_next;
15964 		prev_ill->ill_group_next = ill;
15965 		ill->ill_group = illgrp;
15966 		illgrp->illgrp_ill_count++;
15967 		/*
15968 		 * Inherit group properties. Currently only forwarding
15969 		 * is the property we try to keep the same with all the
15970 		 * ills. When there are more, we will abstract this into
15971 		 * a function.
15972 		 */
15973 		ill->ill_flags &= ~ILLF_ROUTER;
15974 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
15975 	}
15976 	mutex_exit(&ill->ill_lock);
15977 	rw_exit(&ipst->ips_ill_g_lock);
15978 
15979 	/*
15980 	 * 1) When ipif_up_done() calls this function, ipif_up_count
15981 	 *    may be zero as it has not yet been bumped. But the ires
15982 	 *    have already been added. So, we do the nomination here
15983 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
15984 	 *    for ill_ipif_up_count != 0. Thus we don't check for
15985 	 *    ill_ipif_up_count here while nominating broadcast ires for
15986 	 *    receive.
15987 	 *
15988 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
15989 	 *    to group them properly as ire_add() has already happened
15990 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
15991 	 *    case, we need to do it here anyway.
15992 	 */
15993 	if (!ill->ill_isv6) {
15994 		ill_group_bcast_for_xmit(ill);
15995 		ill_nominate_bcast_rcv(illgrp);
15996 	}
15997 
15998 	if (!ipif_is_coming_up) {
15999 		/*
16000 		 * When ipif_up_done() calls this function, the multicast
16001 		 * groups have not been joined yet. So, there is no point in
16002 		 * nomination. ip_join_allmulti will handle groups when
16003 		 * ill_recover_multicast is called from ipif_up_done() later.
16004 		 */
16005 		(void) ill_nominate_mcast_rcv(illgrp);
16006 		/*
16007 		 * ipif_up_done calls ill_update_source_selection
16008 		 * anyway. Moreover, we don't want to re-create
16009 		 * interface routes while ipif_up_done() still has reference
16010 		 * to them. Refer to ipif_up_done() for more details.
16011 		 */
16012 		ill_update_source_selection(ill);
16013 	}
16014 
16015 	/*
16016 	 * Send a routing sockets message if we are inserting into
16017 	 * groups with names.
16018 	 */
16019 	if (groupname != NULL)
16020 		ip_rts_ifmsg(ill->ill_ipif);
16021 	return (0);
16022 }
16023 
16024 /*
16025  * Return the first phyint matching the groupname. There could
16026  * be more than one when there are ill groups.
16027  *
16028  * If 'usable' is set, then we exclude ones that are marked with any of
16029  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16030  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16031  * emulation of ipmp.
16032  */
16033 phyint_t *
16034 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16035 {
16036 	phyint_t *phyi;
16037 
16038 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16039 	/*
16040 	 * Group names are stored in the phyint - a common structure
16041 	 * to both IPv4 and IPv6.
16042 	 */
16043 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16044 	for (; phyi != NULL;
16045 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16046 	    phyi, AVL_AFTER)) {
16047 		if (phyi->phyint_groupname_len == 0)
16048 			continue;
16049 		/*
16050 		 * Skip the ones that should not be used since the callers
16051 		 * sometime use this for sending packets.
16052 		 */
16053 		if (usable && (phyi->phyint_flags &
16054 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16055 			continue;
16056 
16057 		ASSERT(phyi->phyint_groupname != NULL);
16058 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16059 			return (phyi);
16060 	}
16061 	return (NULL);
16062 }
16063 
16064 
16065 /*
16066  * Return the first usable phyint matching the group index. By 'usable'
16067  * we exclude ones that are marked ununsable with any of
16068  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16069  *
16070  * Used only for the ipmp/netinfo emulation of ipmp.
16071  */
16072 phyint_t *
16073 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16074 {
16075 	phyint_t *phyi;
16076 
16077 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16078 
16079 	if (!ipst->ips_ipmp_hook_emulation)
16080 		return (NULL);
16081 
16082 	/*
16083 	 * Group indicies are stored in the phyint - a common structure
16084 	 * to both IPv4 and IPv6.
16085 	 */
16086 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16087 	for (; phyi != NULL;
16088 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16089 	    phyi, AVL_AFTER)) {
16090 		/* Ignore the ones that do not have a group */
16091 		if (phyi->phyint_groupname_len == 0)
16092 			continue;
16093 
16094 		ASSERT(phyi->phyint_group_ifindex != 0);
16095 		/*
16096 		 * Skip the ones that should not be used since the callers
16097 		 * sometime use this for sending packets.
16098 		 */
16099 		if (phyi->phyint_flags &
16100 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16101 			continue;
16102 		if (phyi->phyint_group_ifindex == group_ifindex)
16103 			return (phyi);
16104 	}
16105 	return (NULL);
16106 }
16107 
16108 
16109 /*
16110  * MT notes on creation and deletion of IPMP groups
16111  *
16112  * Creation and deletion of IPMP groups introduce the need to merge or
16113  * split the associated serialization objects i.e the ipsq's. Normally all
16114  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16115  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16116  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16117  * is a need to change the <ill-ipsq> association and we have to operate on both
16118  * the source and destination IPMP groups. For eg. attempting to set the
16119  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16120  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16121  * source or destination IPMP group are mapped to a single ipsq for executing
16122  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16123  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16124  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16125  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16126  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16127  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16128  *
16129  * In the above example the ioctl handling code locates the current ipsq of hme0
16130  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16131  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16132  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16133  * the destination ipsq. If the destination ipsq is not busy, it also enters
16134  * the destination ipsq exclusively. Now the actual groupname setting operation
16135  * can proceed. If the destination ipsq is busy, the operation is enqueued
16136  * on the destination (merged) ipsq and will be handled in the unwind from
16137  * ipsq_exit.
16138  *
16139  * To prevent other threads accessing the ill while the group name change is
16140  * in progres, we bring down the ipifs which also removes the ill from the
16141  * group. The group is changed in phyint and when the first ipif on the ill
16142  * is brought up, the ill is inserted into the right IPMP group by
16143  * illgrp_insert.
16144  */
16145 /* ARGSUSED */
16146 int
16147 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16148     ip_ioctl_cmd_t *ipip, void *ifreq)
16149 {
16150 	int i;
16151 	char *tmp;
16152 	int namelen;
16153 	ill_t *ill = ipif->ipif_ill;
16154 	ill_t *ill_v4, *ill_v6;
16155 	int err = 0;
16156 	phyint_t *phyi;
16157 	phyint_t *phyi_tmp;
16158 	struct lifreq *lifr;
16159 	mblk_t	*mp1;
16160 	char *groupname;
16161 	ipsq_t *ipsq;
16162 	ip_stack_t	*ipst = ill->ill_ipst;
16163 
16164 	ASSERT(IAM_WRITER_IPIF(ipif));
16165 
16166 	/* Existance verified in ip_wput_nondata */
16167 	mp1 = mp->b_cont->b_cont;
16168 	lifr = (struct lifreq *)mp1->b_rptr;
16169 	groupname = lifr->lifr_groupname;
16170 
16171 	if (ipif->ipif_id != 0)
16172 		return (EINVAL);
16173 
16174 	phyi = ill->ill_phyint;
16175 	ASSERT(phyi != NULL);
16176 
16177 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16178 		return (EINVAL);
16179 
16180 	tmp = groupname;
16181 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16182 		;
16183 
16184 	if (i == LIFNAMSIZ) {
16185 		/* no null termination */
16186 		return (EINVAL);
16187 	}
16188 
16189 	/*
16190 	 * Calculate the namelen exclusive of the null
16191 	 * termination character.
16192 	 */
16193 	namelen = tmp - groupname;
16194 
16195 	ill_v4 = phyi->phyint_illv4;
16196 	ill_v6 = phyi->phyint_illv6;
16197 
16198 	/*
16199 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16200 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16201 	 * synchronization notes in ip.c
16202 	 */
16203 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16204 		return (EINVAL);
16205 	}
16206 
16207 	/*
16208 	 * mark the ill as changing.
16209 	 * this should queue all new requests on the syncq.
16210 	 */
16211 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16212 
16213 	if (ill_v4 != NULL)
16214 		ill_v4->ill_state_flags |= ILL_CHANGING;
16215 	if (ill_v6 != NULL)
16216 		ill_v6->ill_state_flags |= ILL_CHANGING;
16217 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16218 
16219 	if (namelen == 0) {
16220 		/*
16221 		 * Null string means remove this interface from the
16222 		 * existing group.
16223 		 */
16224 		if (phyi->phyint_groupname_len == 0) {
16225 			/*
16226 			 * Never was in a group.
16227 			 */
16228 			err = 0;
16229 			goto done;
16230 		}
16231 
16232 		/*
16233 		 * IPv4 or IPv6 may be temporarily out of the group when all
16234 		 * the ipifs are down. Thus, we need to check for ill_group to
16235 		 * be non-NULL.
16236 		 */
16237 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16238 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16239 			mutex_enter(&ill_v4->ill_lock);
16240 			if (!ill_is_quiescent(ill_v4)) {
16241 				/*
16242 				 * ipsq_pending_mp_add will not fail since
16243 				 * connp is NULL
16244 				 */
16245 				(void) ipsq_pending_mp_add(NULL,
16246 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16247 				mutex_exit(&ill_v4->ill_lock);
16248 				err = EINPROGRESS;
16249 				goto done;
16250 			}
16251 			mutex_exit(&ill_v4->ill_lock);
16252 		}
16253 
16254 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16255 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16256 			mutex_enter(&ill_v6->ill_lock);
16257 			if (!ill_is_quiescent(ill_v6)) {
16258 				(void) ipsq_pending_mp_add(NULL,
16259 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16260 				mutex_exit(&ill_v6->ill_lock);
16261 				err = EINPROGRESS;
16262 				goto done;
16263 			}
16264 			mutex_exit(&ill_v6->ill_lock);
16265 		}
16266 
16267 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16268 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16269 		mutex_enter(&phyi->phyint_lock);
16270 		ASSERT(phyi->phyint_groupname != NULL);
16271 		mi_free(phyi->phyint_groupname);
16272 		phyi->phyint_groupname = NULL;
16273 		phyi->phyint_groupname_len = 0;
16274 
16275 		/* Restore the ifindex used to be the per interface one */
16276 		phyi->phyint_group_ifindex = 0;
16277 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16278 		mutex_exit(&phyi->phyint_lock);
16279 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16280 		rw_exit(&ipst->ips_ill_g_lock);
16281 		err = ill_up_ipifs(ill, q, mp);
16282 
16283 		/*
16284 		 * set the split flag so that the ipsq can be split
16285 		 */
16286 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16287 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16288 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16289 
16290 	} else {
16291 		if (phyi->phyint_groupname_len != 0) {
16292 			ASSERT(phyi->phyint_groupname != NULL);
16293 			/* Are we inserting in the same group ? */
16294 			if (mi_strcmp(groupname,
16295 			    phyi->phyint_groupname) == 0) {
16296 				err = 0;
16297 				goto done;
16298 			}
16299 		}
16300 
16301 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16302 		/*
16303 		 * Merge ipsq for the group's.
16304 		 * This check is here as multiple groups/ills might be
16305 		 * sharing the same ipsq.
16306 		 * If we have to merege than the operation is restarted
16307 		 * on the new ipsq.
16308 		 */
16309 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16310 		if (phyi->phyint_ipsq != ipsq) {
16311 			rw_exit(&ipst->ips_ill_g_lock);
16312 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16313 			goto done;
16314 		}
16315 		/*
16316 		 * Running exclusive on new ipsq.
16317 		 */
16318 
16319 		ASSERT(ipsq != NULL);
16320 		ASSERT(ipsq->ipsq_writer == curthread);
16321 
16322 		/*
16323 		 * Check whether the ill_type and ill_net_type matches before
16324 		 * we allocate any memory so that the cleanup is easier.
16325 		 *
16326 		 * We can't group dissimilar ones as we can't load spread
16327 		 * packets across the group because of potential link-level
16328 		 * header differences.
16329 		 */
16330 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16331 		if (phyi_tmp != NULL) {
16332 			if ((ill_v4 != NULL &&
16333 			    phyi_tmp->phyint_illv4 != NULL) &&
16334 			    ((ill_v4->ill_net_type !=
16335 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16336 			    (ill_v4->ill_type !=
16337 			    phyi_tmp->phyint_illv4->ill_type))) {
16338 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16339 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16340 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16341 				rw_exit(&ipst->ips_ill_g_lock);
16342 				return (EINVAL);
16343 			}
16344 			if ((ill_v6 != NULL &&
16345 			    phyi_tmp->phyint_illv6 != NULL) &&
16346 			    ((ill_v6->ill_net_type !=
16347 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16348 			    (ill_v6->ill_type !=
16349 			    phyi_tmp->phyint_illv6->ill_type))) {
16350 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16351 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16352 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16353 				rw_exit(&ipst->ips_ill_g_lock);
16354 				return (EINVAL);
16355 			}
16356 		}
16357 
16358 		rw_exit(&ipst->ips_ill_g_lock);
16359 
16360 		/*
16361 		 * bring down all v4 ipifs.
16362 		 */
16363 		if (ill_v4 != NULL) {
16364 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16365 		}
16366 
16367 		/*
16368 		 * bring down all v6 ipifs.
16369 		 */
16370 		if (ill_v6 != NULL) {
16371 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16372 		}
16373 
16374 		/*
16375 		 * make sure all ipifs are down and there are no active
16376 		 * references. Call to ipsq_pending_mp_add will not fail
16377 		 * since connp is NULL.
16378 		 */
16379 		if (ill_v4 != NULL) {
16380 			mutex_enter(&ill_v4->ill_lock);
16381 			if (!ill_is_quiescent(ill_v4)) {
16382 				(void) ipsq_pending_mp_add(NULL,
16383 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16384 				mutex_exit(&ill_v4->ill_lock);
16385 				err = EINPROGRESS;
16386 				goto done;
16387 			}
16388 			mutex_exit(&ill_v4->ill_lock);
16389 		}
16390 
16391 		if (ill_v6 != NULL) {
16392 			mutex_enter(&ill_v6->ill_lock);
16393 			if (!ill_is_quiescent(ill_v6)) {
16394 				(void) ipsq_pending_mp_add(NULL,
16395 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16396 				mutex_exit(&ill_v6->ill_lock);
16397 				err = EINPROGRESS;
16398 				goto done;
16399 			}
16400 			mutex_exit(&ill_v6->ill_lock);
16401 		}
16402 
16403 		/*
16404 		 * allocate including space for null terminator
16405 		 * before we insert.
16406 		 */
16407 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16408 		if (tmp == NULL)
16409 			return (ENOMEM);
16410 
16411 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16412 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16413 		mutex_enter(&phyi->phyint_lock);
16414 		if (phyi->phyint_groupname_len != 0) {
16415 			ASSERT(phyi->phyint_groupname != NULL);
16416 			mi_free(phyi->phyint_groupname);
16417 		}
16418 
16419 		/*
16420 		 * setup the new group name.
16421 		 */
16422 		phyi->phyint_groupname = tmp;
16423 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16424 		phyi->phyint_groupname_len = namelen + 1;
16425 
16426 		if (ipst->ips_ipmp_hook_emulation) {
16427 			/*
16428 			 * If the group already exists we use the existing
16429 			 * group_ifindex, otherwise we pick a new index here.
16430 			 */
16431 			if (phyi_tmp != NULL) {
16432 				phyi->phyint_group_ifindex =
16433 				    phyi_tmp->phyint_group_ifindex;
16434 			} else {
16435 				/* XXX We need a recovery strategy here. */
16436 				if (!ip_assign_ifindex(
16437 				    &phyi->phyint_group_ifindex, ipst))
16438 					cmn_err(CE_PANIC,
16439 					    "ip_assign_ifindex() failed");
16440 			}
16441 		}
16442 		/*
16443 		 * Select whether the netinfo and hook use the per-interface
16444 		 * or per-group ifindex.
16445 		 */
16446 		if (ipst->ips_ipmp_hook_emulation)
16447 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16448 		else
16449 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16450 
16451 		if (ipst->ips_ipmp_hook_emulation &&
16452 		    phyi_tmp != NULL) {
16453 			/* First phyint in group - group PLUMB event */
16454 			ill_nic_info_plumb(ill, B_TRUE);
16455 		}
16456 		mutex_exit(&phyi->phyint_lock);
16457 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16458 		rw_exit(&ipst->ips_ill_g_lock);
16459 
16460 		err = ill_up_ipifs(ill, q, mp);
16461 	}
16462 
16463 done:
16464 	/*
16465 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16466 	 */
16467 	if (err != EINPROGRESS) {
16468 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16469 		if (ill_v4 != NULL)
16470 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16471 		if (ill_v6 != NULL)
16472 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16473 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16474 	}
16475 	return (err);
16476 }
16477 
16478 /* ARGSUSED */
16479 int
16480 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16481     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16482 {
16483 	ill_t *ill;
16484 	phyint_t *phyi;
16485 	struct lifreq *lifr;
16486 	mblk_t	*mp1;
16487 
16488 	/* Existence verified in ip_wput_nondata */
16489 	mp1 = mp->b_cont->b_cont;
16490 	lifr = (struct lifreq *)mp1->b_rptr;
16491 	ill = ipif->ipif_ill;
16492 	phyi = ill->ill_phyint;
16493 
16494 	lifr->lifr_groupname[0] = '\0';
16495 	/*
16496 	 * ill_group may be null if all the interfaces
16497 	 * are down. But still, the phyint should always
16498 	 * hold the name.
16499 	 */
16500 	if (phyi->phyint_groupname_len != 0) {
16501 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16502 		    phyi->phyint_groupname_len);
16503 	}
16504 
16505 	return (0);
16506 }
16507 
16508 
16509 typedef struct conn_move_s {
16510 	ill_t	*cm_from_ill;
16511 	ill_t	*cm_to_ill;
16512 	int	cm_ifindex;
16513 } conn_move_t;
16514 
16515 /*
16516  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16517  */
16518 static void
16519 conn_move(conn_t *connp, caddr_t arg)
16520 {
16521 	conn_move_t *connm;
16522 	int ifindex;
16523 	int i;
16524 	ill_t *from_ill;
16525 	ill_t *to_ill;
16526 	ilg_t *ilg;
16527 	ilm_t *ret_ilm;
16528 
16529 	connm = (conn_move_t *)arg;
16530 	ifindex = connm->cm_ifindex;
16531 	from_ill = connm->cm_from_ill;
16532 	to_ill = connm->cm_to_ill;
16533 
16534 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16535 
16536 	/* All multicast fields protected by conn_lock */
16537 	mutex_enter(&connp->conn_lock);
16538 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16539 	if ((connp->conn_outgoing_ill == from_ill) &&
16540 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16541 		connp->conn_outgoing_ill = to_ill;
16542 		connp->conn_incoming_ill = to_ill;
16543 	}
16544 
16545 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16546 
16547 	if ((connp->conn_multicast_ill == from_ill) &&
16548 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16549 		connp->conn_multicast_ill = connm->cm_to_ill;
16550 	}
16551 
16552 	/*
16553 	 * Change the ilg_ill to point to the new one. This assumes
16554 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16555 	 * has been told to receive packets on this interface.
16556 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16557 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16558 	 * some ilms may not have moved. We check to see whether
16559 	 * the ilms have moved to to_ill. We can't check on from_ill
16560 	 * as in the process of moving, we could have split an ilm
16561 	 * in to two - which has the same orig_ifindex and v6group.
16562 	 *
16563 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16564 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16565 	 */
16566 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16567 		ilg = &connp->conn_ilg[i];
16568 		if ((ilg->ilg_ill == from_ill) &&
16569 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16570 			/* ifindex != 0 indicates failback */
16571 			if (ifindex != 0) {
16572 				connp->conn_ilg[i].ilg_ill = to_ill;
16573 				continue;
16574 			}
16575 
16576 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16577 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16578 			    connp->conn_zoneid);
16579 
16580 			if (ret_ilm != NULL)
16581 				connp->conn_ilg[i].ilg_ill = to_ill;
16582 		}
16583 	}
16584 	mutex_exit(&connp->conn_lock);
16585 }
16586 
16587 static void
16588 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16589 {
16590 	conn_move_t connm;
16591 	ip_stack_t	*ipst = from_ill->ill_ipst;
16592 
16593 	connm.cm_from_ill = from_ill;
16594 	connm.cm_to_ill = to_ill;
16595 	connm.cm_ifindex = ifindex;
16596 
16597 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16598 }
16599 
16600 /*
16601  * ilm has been moved from from_ill to to_ill.
16602  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16603  * appropriately.
16604  *
16605  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16606  *	  the code there de-references ipif_ill to get the ill to
16607  *	  send multicast requests. It does not work as ipif is on its
16608  *	  move and already moved when this function is called.
16609  *	  Thus, we need to use from_ill and to_ill send down multicast
16610  *	  requests.
16611  */
16612 static void
16613 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16614 {
16615 	ipif_t *ipif;
16616 	ilm_t *ilm;
16617 
16618 	/*
16619 	 * See whether we need to send down DL_ENABMULTI_REQ on
16620 	 * to_ill as ilm has just been added.
16621 	 */
16622 	ASSERT(IAM_WRITER_ILL(to_ill));
16623 	ASSERT(IAM_WRITER_ILL(from_ill));
16624 
16625 	ILM_WALKER_HOLD(to_ill);
16626 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16627 
16628 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16629 			continue;
16630 		/*
16631 		 * no locks held, ill/ipif cannot dissappear as long
16632 		 * as we are writer.
16633 		 */
16634 		ipif = to_ill->ill_ipif;
16635 		/*
16636 		 * No need to hold any lock as we are the writer and this
16637 		 * can only be changed by a writer.
16638 		 */
16639 		ilm->ilm_is_new = B_FALSE;
16640 
16641 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16642 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16643 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16644 			    "resolver\n"));
16645 			continue;		/* Must be IRE_IF_NORESOLVER */
16646 		}
16647 
16648 
16649 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16650 			ip1dbg(("ilm_send_multicast_reqs: "
16651 			    "to_ill MULTI_BCAST\n"));
16652 			goto from;
16653 		}
16654 
16655 		if (to_ill->ill_isv6)
16656 			mld_joingroup(ilm);
16657 		else
16658 			igmp_joingroup(ilm);
16659 
16660 		if (to_ill->ill_ipif_up_count == 0) {
16661 			/*
16662 			 * Nobody there. All multicast addresses will be
16663 			 * re-joined when we get the DL_BIND_ACK bringing the
16664 			 * interface up.
16665 			 */
16666 			ilm->ilm_notify_driver = B_FALSE;
16667 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16668 			goto from;
16669 		}
16670 
16671 		/*
16672 		 * For allmulti address, we want to join on only one interface.
16673 		 * Checking for ilm_numentries_v6 is not correct as you may
16674 		 * find an ilm with zero address on to_ill, but we may not
16675 		 * have nominated to_ill for receiving. Thus, if we have
16676 		 * nominated from_ill (ill_join_allmulti is set), nominate
16677 		 * only if to_ill is not already nominated (to_ill normally
16678 		 * should not have been nominated if "from_ill" has already
16679 		 * been nominated. As we don't prevent failovers from happening
16680 		 * across groups, we don't assert).
16681 		 */
16682 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16683 			/*
16684 			 * There is no need to hold ill locks as we are
16685 			 * writer on both ills and when ill_join_allmulti
16686 			 * is changed the thread is always a writer.
16687 			 */
16688 			if (from_ill->ill_join_allmulti &&
16689 			    !to_ill->ill_join_allmulti) {
16690 				(void) ip_join_allmulti(to_ill->ill_ipif);
16691 			}
16692 		} else if (ilm->ilm_notify_driver) {
16693 
16694 			/*
16695 			 * This is a newly moved ilm so we need to tell the
16696 			 * driver about the new group. There can be more than
16697 			 * one ilm's for the same group in the list each with a
16698 			 * different orig_ifindex. We have to inform the driver
16699 			 * once. In ilm_move_v[4,6] we only set the flag
16700 			 * ilm_notify_driver for the first ilm.
16701 			 */
16702 
16703 			(void) ip_ll_send_enabmulti_req(to_ill,
16704 			    &ilm->ilm_v6addr);
16705 		}
16706 
16707 		ilm->ilm_notify_driver = B_FALSE;
16708 
16709 		/*
16710 		 * See whether we need to send down DL_DISABMULTI_REQ on
16711 		 * from_ill as ilm has just been removed.
16712 		 */
16713 from:
16714 		ipif = from_ill->ill_ipif;
16715 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16716 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16717 			ip1dbg(("ilm_send_multicast_reqs: "
16718 			    "from_ill not resolver\n"));
16719 			continue;		/* Must be IRE_IF_NORESOLVER */
16720 		}
16721 
16722 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16723 			ip1dbg(("ilm_send_multicast_reqs: "
16724 			    "from_ill MULTI_BCAST\n"));
16725 			continue;
16726 		}
16727 
16728 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16729 			if (from_ill->ill_join_allmulti)
16730 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16731 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16732 			(void) ip_ll_send_disabmulti_req(from_ill,
16733 			    &ilm->ilm_v6addr);
16734 		}
16735 	}
16736 	ILM_WALKER_RELE(to_ill);
16737 }
16738 
16739 /*
16740  * This function is called when all multicast memberships needs
16741  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16742  * called only once unlike the IPv4 counterpart where it is called after
16743  * every logical interface is moved. The reason is due to multicast
16744  * memberships are joined using an interface address in IPv4 while in
16745  * IPv6, interface index is used.
16746  */
16747 static void
16748 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16749 {
16750 	ilm_t	*ilm;
16751 	ilm_t	*ilm_next;
16752 	ilm_t	*new_ilm;
16753 	ilm_t	**ilmp;
16754 	int	count;
16755 	char buf[INET6_ADDRSTRLEN];
16756 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16757 	ip_stack_t	*ipst = from_ill->ill_ipst;
16758 
16759 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16760 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16761 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16762 
16763 	if (ifindex == 0) {
16764 		/*
16765 		 * Form the solicited node mcast address which is used later.
16766 		 */
16767 		ipif_t *ipif;
16768 
16769 		ipif = from_ill->ill_ipif;
16770 		ASSERT(ipif->ipif_id == 0);
16771 
16772 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16773 	}
16774 
16775 	ilmp = &from_ill->ill_ilm;
16776 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16777 		ilm_next = ilm->ilm_next;
16778 
16779 		if (ilm->ilm_flags & ILM_DELETED) {
16780 			ilmp = &ilm->ilm_next;
16781 			continue;
16782 		}
16783 
16784 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16785 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16786 		ASSERT(ilm->ilm_orig_ifindex != 0);
16787 		if (ilm->ilm_orig_ifindex == ifindex) {
16788 			/*
16789 			 * We are failing back multicast memberships.
16790 			 * If the same ilm exists in to_ill, it means somebody
16791 			 * has joined the same group there e.g. ff02::1
16792 			 * is joined within the kernel when the interfaces
16793 			 * came UP.
16794 			 */
16795 			ASSERT(ilm->ilm_ipif == NULL);
16796 			if (new_ilm != NULL) {
16797 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16798 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16799 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16800 					new_ilm->ilm_is_new = B_TRUE;
16801 				}
16802 			} else {
16803 				/*
16804 				 * check if we can just move the ilm
16805 				 */
16806 				if (from_ill->ill_ilm_walker_cnt != 0) {
16807 					/*
16808 					 * We have walkers we cannot move
16809 					 * the ilm, so allocate a new ilm,
16810 					 * this (old) ilm will be marked
16811 					 * ILM_DELETED at the end of the loop
16812 					 * and will be freed when the
16813 					 * last walker exits.
16814 					 */
16815 					new_ilm = (ilm_t *)mi_zalloc
16816 					    (sizeof (ilm_t));
16817 					if (new_ilm == NULL) {
16818 						ip0dbg(("ilm_move_v6: "
16819 						    "FAILBACK of IPv6"
16820 						    " multicast address %s : "
16821 						    "from %s to"
16822 						    " %s failed : ENOMEM \n",
16823 						    inet_ntop(AF_INET6,
16824 						    &ilm->ilm_v6addr, buf,
16825 						    sizeof (buf)),
16826 						    from_ill->ill_name,
16827 						    to_ill->ill_name));
16828 
16829 							ilmp = &ilm->ilm_next;
16830 							continue;
16831 					}
16832 					*new_ilm = *ilm;
16833 					/*
16834 					 * we don't want new_ilm linked to
16835 					 * ilm's filter list.
16836 					 */
16837 					new_ilm->ilm_filter = NULL;
16838 				} else {
16839 					/*
16840 					 * No walkers we can move the ilm.
16841 					 * lets take it out of the list.
16842 					 */
16843 					*ilmp = ilm->ilm_next;
16844 					ilm->ilm_next = NULL;
16845 					new_ilm = ilm;
16846 				}
16847 
16848 				/*
16849 				 * if this is the first ilm for the group
16850 				 * set ilm_notify_driver so that we notify the
16851 				 * driver in ilm_send_multicast_reqs.
16852 				 */
16853 				if (ilm_lookup_ill_v6(to_ill,
16854 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16855 					new_ilm->ilm_notify_driver = B_TRUE;
16856 
16857 				new_ilm->ilm_ill = to_ill;
16858 				/* Add to the to_ill's list */
16859 				new_ilm->ilm_next = to_ill->ill_ilm;
16860 				to_ill->ill_ilm = new_ilm;
16861 				/*
16862 				 * set the flag so that mld_joingroup is
16863 				 * called in ilm_send_multicast_reqs().
16864 				 */
16865 				new_ilm->ilm_is_new = B_TRUE;
16866 			}
16867 			goto bottom;
16868 		} else if (ifindex != 0) {
16869 			/*
16870 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16871 			 * has not matched above, look at the next ilm.
16872 			 */
16873 			ilmp = &ilm->ilm_next;
16874 			continue;
16875 		}
16876 		/*
16877 		 * If we are here, it means ifindex is 0. Failover
16878 		 * everything.
16879 		 *
16880 		 * We need to handle solicited node mcast address
16881 		 * and all_nodes mcast address differently as they
16882 		 * are joined witin the kenrel (ipif_multicast_up)
16883 		 * and potentially from the userland. We are called
16884 		 * after the ipifs of from_ill has been moved.
16885 		 * If we still find ilms on ill with solicited node
16886 		 * mcast address or all_nodes mcast address, it must
16887 		 * belong to the UP interface that has not moved e.g.
16888 		 * ipif_id 0 with the link local prefix does not move.
16889 		 * We join this on the new ill accounting for all the
16890 		 * userland memberships so that applications don't
16891 		 * see any failure.
16892 		 *
16893 		 * We need to make sure that we account only for the
16894 		 * solicited node and all node multicast addresses
16895 		 * that was brought UP on these. In the case of
16896 		 * a failover from A to B, we might have ilms belonging
16897 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16898 		 * for the membership from the userland. If we are failing
16899 		 * over from B to C now, we will find the ones belonging
16900 		 * to A on B. These don't account for the ill_ipif_up_count.
16901 		 * They just move from B to C. The check below on
16902 		 * ilm_orig_ifindex ensures that.
16903 		 */
16904 		if ((ilm->ilm_orig_ifindex ==
16905 		    from_ill->ill_phyint->phyint_ifindex) &&
16906 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16907 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16908 		    &ilm->ilm_v6addr))) {
16909 			ASSERT(ilm->ilm_refcnt > 0);
16910 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16911 			/*
16912 			 * For indentation reasons, we are not using a
16913 			 * "else" here.
16914 			 */
16915 			if (count == 0) {
16916 				ilmp = &ilm->ilm_next;
16917 				continue;
16918 			}
16919 			ilm->ilm_refcnt -= count;
16920 			if (new_ilm != NULL) {
16921 				/*
16922 				 * Can find one with the same
16923 				 * ilm_orig_ifindex, if we are failing
16924 				 * over to a STANDBY. This happens
16925 				 * when somebody wants to join a group
16926 				 * on a STANDBY interface and we
16927 				 * internally join on a different one.
16928 				 * If we had joined on from_ill then, a
16929 				 * failover now will find a new ilm
16930 				 * with this index.
16931 				 */
16932 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16933 				    " new ilm on %s, group address %s\n",
16934 				    to_ill->ill_name,
16935 				    inet_ntop(AF_INET6,
16936 				    &ilm->ilm_v6addr, buf,
16937 				    sizeof (buf))));
16938 				new_ilm->ilm_refcnt += count;
16939 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16940 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16941 					new_ilm->ilm_is_new = B_TRUE;
16942 				}
16943 			} else {
16944 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16945 				if (new_ilm == NULL) {
16946 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
16947 					    " multicast address %s : from %s to"
16948 					    " %s failed : ENOMEM \n",
16949 					    inet_ntop(AF_INET6,
16950 					    &ilm->ilm_v6addr, buf,
16951 					    sizeof (buf)), from_ill->ill_name,
16952 					    to_ill->ill_name));
16953 					ilmp = &ilm->ilm_next;
16954 					continue;
16955 				}
16956 				*new_ilm = *ilm;
16957 				new_ilm->ilm_filter = NULL;
16958 				new_ilm->ilm_refcnt = count;
16959 				new_ilm->ilm_timer = INFINITY;
16960 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
16961 				new_ilm->ilm_is_new = B_TRUE;
16962 				/*
16963 				 * If the to_ill has not joined this
16964 				 * group we need to tell the driver in
16965 				 * ill_send_multicast_reqs.
16966 				 */
16967 				if (ilm_lookup_ill_v6(to_ill,
16968 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16969 					new_ilm->ilm_notify_driver = B_TRUE;
16970 
16971 				new_ilm->ilm_ill = to_ill;
16972 				/* Add to the to_ill's list */
16973 				new_ilm->ilm_next = to_ill->ill_ilm;
16974 				to_ill->ill_ilm = new_ilm;
16975 				ASSERT(new_ilm->ilm_ipif == NULL);
16976 			}
16977 			if (ilm->ilm_refcnt == 0) {
16978 				goto bottom;
16979 			} else {
16980 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16981 				CLEAR_SLIST(new_ilm->ilm_filter);
16982 				ilmp = &ilm->ilm_next;
16983 			}
16984 			continue;
16985 		} else {
16986 			/*
16987 			 * ifindex = 0 means, move everything pointing at
16988 			 * from_ill. We are doing this becuase ill has
16989 			 * either FAILED or became INACTIVE.
16990 			 *
16991 			 * As we would like to move things later back to
16992 			 * from_ill, we want to retain the identity of this
16993 			 * ilm. Thus, we don't blindly increment the reference
16994 			 * count on the ilms matching the address alone. We
16995 			 * need to match on the ilm_orig_index also. new_ilm
16996 			 * was obtained by matching ilm_orig_index also.
16997 			 */
16998 			if (new_ilm != NULL) {
16999 				/*
17000 				 * This is possible only if a previous restore
17001 				 * was incomplete i.e restore to
17002 				 * ilm_orig_ifindex left some ilms because
17003 				 * of some failures. Thus when we are failing
17004 				 * again, we might find our old friends there.
17005 				 */
17006 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17007 				    " on %s, group address %s\n",
17008 				    to_ill->ill_name,
17009 				    inet_ntop(AF_INET6,
17010 				    &ilm->ilm_v6addr, buf,
17011 				    sizeof (buf))));
17012 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17013 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17014 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17015 					new_ilm->ilm_is_new = B_TRUE;
17016 				}
17017 			} else {
17018 				if (from_ill->ill_ilm_walker_cnt != 0) {
17019 					new_ilm = (ilm_t *)
17020 					    mi_zalloc(sizeof (ilm_t));
17021 					if (new_ilm == NULL) {
17022 						ip0dbg(("ilm_move_v6: "
17023 						    "FAILOVER of IPv6"
17024 						    " multicast address %s : "
17025 						    "from %s to"
17026 						    " %s failed : ENOMEM \n",
17027 						    inet_ntop(AF_INET6,
17028 						    &ilm->ilm_v6addr, buf,
17029 						    sizeof (buf)),
17030 						    from_ill->ill_name,
17031 						    to_ill->ill_name));
17032 
17033 							ilmp = &ilm->ilm_next;
17034 							continue;
17035 					}
17036 					*new_ilm = *ilm;
17037 					new_ilm->ilm_filter = NULL;
17038 				} else {
17039 					*ilmp = ilm->ilm_next;
17040 					new_ilm = ilm;
17041 				}
17042 				/*
17043 				 * If the to_ill has not joined this
17044 				 * group we need to tell the driver in
17045 				 * ill_send_multicast_reqs.
17046 				 */
17047 				if (ilm_lookup_ill_v6(to_ill,
17048 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17049 					new_ilm->ilm_notify_driver = B_TRUE;
17050 
17051 				/* Add to the to_ill's list */
17052 				new_ilm->ilm_next = to_ill->ill_ilm;
17053 				to_ill->ill_ilm = new_ilm;
17054 				ASSERT(ilm->ilm_ipif == NULL);
17055 				new_ilm->ilm_ill = to_ill;
17056 				new_ilm->ilm_is_new = B_TRUE;
17057 			}
17058 
17059 		}
17060 
17061 bottom:
17062 		/*
17063 		 * Revert multicast filter state to (EXCLUDE, NULL).
17064 		 * new_ilm->ilm_is_new should already be set if needed.
17065 		 */
17066 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17067 		CLEAR_SLIST(new_ilm->ilm_filter);
17068 		/*
17069 		 * We allocated/got a new ilm, free the old one.
17070 		 */
17071 		if (new_ilm != ilm) {
17072 			if (from_ill->ill_ilm_walker_cnt == 0) {
17073 				*ilmp = ilm->ilm_next;
17074 				ilm->ilm_next = NULL;
17075 				FREE_SLIST(ilm->ilm_filter);
17076 				FREE_SLIST(ilm->ilm_pendsrcs);
17077 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17078 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17079 				mi_free((char *)ilm);
17080 			} else {
17081 				ilm->ilm_flags |= ILM_DELETED;
17082 				from_ill->ill_ilm_cleanup_reqd = 1;
17083 				ilmp = &ilm->ilm_next;
17084 			}
17085 		}
17086 	}
17087 }
17088 
17089 /*
17090  * Move all the multicast memberships to to_ill. Called when
17091  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17092  * different from IPv6 counterpart as multicast memberships are associated
17093  * with ills in IPv6. This function is called after every ipif is moved
17094  * unlike IPv6, where it is moved only once.
17095  */
17096 static void
17097 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17098 {
17099 	ilm_t	*ilm;
17100 	ilm_t	*ilm_next;
17101 	ilm_t	*new_ilm;
17102 	ilm_t	**ilmp;
17103 	ip_stack_t	*ipst = from_ill->ill_ipst;
17104 
17105 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17106 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17107 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17108 
17109 	ilmp = &from_ill->ill_ilm;
17110 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17111 		ilm_next = ilm->ilm_next;
17112 
17113 		if (ilm->ilm_flags & ILM_DELETED) {
17114 			ilmp = &ilm->ilm_next;
17115 			continue;
17116 		}
17117 
17118 		ASSERT(ilm->ilm_ipif != NULL);
17119 
17120 		if (ilm->ilm_ipif != ipif) {
17121 			ilmp = &ilm->ilm_next;
17122 			continue;
17123 		}
17124 
17125 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17126 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17127 			new_ilm = ilm_lookup_ipif(ipif,
17128 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17129 			if (new_ilm != NULL) {
17130 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17131 				/*
17132 				 * We still need to deal with the from_ill.
17133 				 */
17134 				new_ilm->ilm_is_new = B_TRUE;
17135 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17136 				CLEAR_SLIST(new_ilm->ilm_filter);
17137 				goto delete_ilm;
17138 			}
17139 			/*
17140 			 * If we could not find one e.g. ipif is
17141 			 * still down on to_ill, we add this ilm
17142 			 * on ill_new to preserve the reference
17143 			 * count.
17144 			 */
17145 		}
17146 		/*
17147 		 * When ipifs move, ilms always move with it
17148 		 * to the NEW ill. Thus we should never be
17149 		 * able to find ilm till we really move it here.
17150 		 */
17151 		ASSERT(ilm_lookup_ipif(ipif,
17152 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17153 
17154 		if (from_ill->ill_ilm_walker_cnt != 0) {
17155 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17156 			if (new_ilm == NULL) {
17157 				char buf[INET6_ADDRSTRLEN];
17158 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17159 				    " multicast address %s : "
17160 				    "from %s to"
17161 				    " %s failed : ENOMEM \n",
17162 				    inet_ntop(AF_INET,
17163 				    &ilm->ilm_v6addr, buf,
17164 				    sizeof (buf)),
17165 				    from_ill->ill_name,
17166 				    to_ill->ill_name));
17167 
17168 				ilmp = &ilm->ilm_next;
17169 				continue;
17170 			}
17171 			*new_ilm = *ilm;
17172 			/* We don't want new_ilm linked to ilm's filter list */
17173 			new_ilm->ilm_filter = NULL;
17174 		} else {
17175 			/* Remove from the list */
17176 			*ilmp = ilm->ilm_next;
17177 			new_ilm = ilm;
17178 		}
17179 
17180 		/*
17181 		 * If we have never joined this group on the to_ill
17182 		 * make sure we tell the driver.
17183 		 */
17184 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17185 		    ALL_ZONES) == NULL)
17186 			new_ilm->ilm_notify_driver = B_TRUE;
17187 
17188 		/* Add to the to_ill's list */
17189 		new_ilm->ilm_next = to_ill->ill_ilm;
17190 		to_ill->ill_ilm = new_ilm;
17191 		new_ilm->ilm_is_new = B_TRUE;
17192 
17193 		/*
17194 		 * Revert multicast filter state to (EXCLUDE, NULL)
17195 		 */
17196 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17197 		CLEAR_SLIST(new_ilm->ilm_filter);
17198 
17199 		/*
17200 		 * Delete only if we have allocated a new ilm.
17201 		 */
17202 		if (new_ilm != ilm) {
17203 delete_ilm:
17204 			if (from_ill->ill_ilm_walker_cnt == 0) {
17205 				/* Remove from the list */
17206 				*ilmp = ilm->ilm_next;
17207 				ilm->ilm_next = NULL;
17208 				FREE_SLIST(ilm->ilm_filter);
17209 				FREE_SLIST(ilm->ilm_pendsrcs);
17210 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17211 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17212 				mi_free((char *)ilm);
17213 			} else {
17214 				ilm->ilm_flags |= ILM_DELETED;
17215 				from_ill->ill_ilm_cleanup_reqd = 1;
17216 				ilmp = &ilm->ilm_next;
17217 			}
17218 		}
17219 	}
17220 }
17221 
17222 static uint_t
17223 ipif_get_id(ill_t *ill, uint_t id)
17224 {
17225 	uint_t	unit;
17226 	ipif_t	*tipif;
17227 	boolean_t found = B_FALSE;
17228 	ip_stack_t	*ipst = ill->ill_ipst;
17229 
17230 	/*
17231 	 * During failback, we want to go back to the same id
17232 	 * instead of the smallest id so that the original
17233 	 * configuration is maintained. id is non-zero in that
17234 	 * case.
17235 	 */
17236 	if (id != 0) {
17237 		/*
17238 		 * While failing back, if we still have an ipif with
17239 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17240 		 * as soon as we return from this function. It was
17241 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17242 		 * we can choose the smallest id. Thus we return zero
17243 		 * in that case ignoring the hint.
17244 		 */
17245 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17246 			return (0);
17247 		for (tipif = ill->ill_ipif; tipif != NULL;
17248 		    tipif = tipif->ipif_next) {
17249 			if (tipif->ipif_id == id) {
17250 				found = B_TRUE;
17251 				break;
17252 			}
17253 		}
17254 		/*
17255 		 * If somebody already plumbed another logical
17256 		 * with the same id, we won't be able to find it.
17257 		 */
17258 		if (!found)
17259 			return (id);
17260 	}
17261 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17262 		found = B_FALSE;
17263 		for (tipif = ill->ill_ipif; tipif != NULL;
17264 		    tipif = tipif->ipif_next) {
17265 			if (tipif->ipif_id == unit) {
17266 				found = B_TRUE;
17267 				break;
17268 			}
17269 		}
17270 		if (!found)
17271 			break;
17272 	}
17273 	return (unit);
17274 }
17275 
17276 /* ARGSUSED */
17277 static int
17278 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17279     ipif_t **rep_ipif_ptr)
17280 {
17281 	ill_t	*from_ill;
17282 	ipif_t	*rep_ipif;
17283 	uint_t	unit;
17284 	int err = 0;
17285 	ipif_t	*to_ipif;
17286 	struct iocblk	*iocp;
17287 	boolean_t failback_cmd;
17288 	boolean_t remove_ipif;
17289 	int	rc;
17290 	ip_stack_t	*ipst;
17291 
17292 	ASSERT(IAM_WRITER_ILL(to_ill));
17293 	ASSERT(IAM_WRITER_IPIF(ipif));
17294 
17295 	iocp = (struct iocblk *)mp->b_rptr;
17296 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17297 	remove_ipif = B_FALSE;
17298 
17299 	from_ill = ipif->ipif_ill;
17300 	ipst = from_ill->ill_ipst;
17301 
17302 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17303 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17304 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17305 
17306 	/*
17307 	 * Don't move LINK LOCAL addresses as they are tied to
17308 	 * physical interface.
17309 	 */
17310 	if (from_ill->ill_isv6 &&
17311 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17312 		ipif->ipif_was_up = B_FALSE;
17313 		IPIF_UNMARK_MOVING(ipif);
17314 		return (0);
17315 	}
17316 
17317 	/*
17318 	 * We set the ipif_id to maximum so that the search for
17319 	 * ipif_id will pick the lowest number i.e 0 in the
17320 	 * following 2 cases :
17321 	 *
17322 	 * 1) We have a replacement ipif at the head of to_ill.
17323 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17324 	 *    on to_ill and hence the MOVE might fail. We want to
17325 	 *    remove it only if we could move the ipif. Thus, by
17326 	 *    setting it to the MAX value, we make the search in
17327 	 *    ipif_get_id return the zeroth id.
17328 	 *
17329 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17330 	 *    we might just have a zero address plumbed on the ipif
17331 	 *    with zero id in the case of IPv4. We remove that while
17332 	 *    doing the failback. We want to remove it only if we
17333 	 *    could move the ipif. Thus, by setting it to the MAX
17334 	 *    value, we make the search in ipif_get_id return the
17335 	 *    zeroth id.
17336 	 *
17337 	 * Both (1) and (2) are done only when when we are moving
17338 	 * an ipif (either due to failover/failback) which originally
17339 	 * belonged to this interface i.e the ipif_orig_ifindex is
17340 	 * the same as to_ill's ifindex. This is needed so that
17341 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17342 	 * from B -> A (B is being removed from the group) and
17343 	 * FAILBACK from A -> B restores the original configuration.
17344 	 * Without the check for orig_ifindex, the second FAILOVER
17345 	 * could make the ipif belonging to B replace the A's zeroth
17346 	 * ipif and the subsequent failback re-creating the replacement
17347 	 * ipif again.
17348 	 *
17349 	 * NOTE : We created the replacement ipif when we did a
17350 	 * FAILOVER (See below). We could check for FAILBACK and
17351 	 * then look for replacement ipif to be removed. But we don't
17352 	 * want to do that because we wan't to allow the possibility
17353 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17354 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17355 	 * from B -> A.
17356 	 */
17357 	to_ipif = to_ill->ill_ipif;
17358 	if ((to_ill->ill_phyint->phyint_ifindex ==
17359 	    ipif->ipif_orig_ifindex) &&
17360 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17361 		ASSERT(to_ipif->ipif_id == 0);
17362 		remove_ipif = B_TRUE;
17363 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17364 	}
17365 	/*
17366 	 * Find the lowest logical unit number on the to_ill.
17367 	 * If we are failing back, try to get the original id
17368 	 * rather than the lowest one so that the original
17369 	 * configuration is maintained.
17370 	 *
17371 	 * XXX need a better scheme for this.
17372 	 */
17373 	if (failback_cmd) {
17374 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17375 	} else {
17376 		unit = ipif_get_id(to_ill, 0);
17377 	}
17378 
17379 	/* Reset back to zero in case we fail below */
17380 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17381 		to_ipif->ipif_id = 0;
17382 
17383 	if (unit == ipst->ips_ip_addrs_per_if) {
17384 		ipif->ipif_was_up = B_FALSE;
17385 		IPIF_UNMARK_MOVING(ipif);
17386 		return (EINVAL);
17387 	}
17388 
17389 	/*
17390 	 * ipif is ready to move from "from_ill" to "to_ill".
17391 	 *
17392 	 * 1) If we are moving ipif with id zero, create a
17393 	 *    replacement ipif for this ipif on from_ill. If this fails
17394 	 *    fail the MOVE operation.
17395 	 *
17396 	 * 2) Remove the replacement ipif on to_ill if any.
17397 	 *    We could remove the replacement ipif when we are moving
17398 	 *    the ipif with id zero. But what if somebody already
17399 	 *    unplumbed it ? Thus we always remove it if it is present.
17400 	 *    We want to do it only if we are sure we are going to
17401 	 *    move the ipif to to_ill which is why there are no
17402 	 *    returns due to error till ipif is linked to to_ill.
17403 	 *    Note that the first ipif that we failback will always
17404 	 *    be zero if it is present.
17405 	 */
17406 	if (ipif->ipif_id == 0) {
17407 		ipaddr_t inaddr_any = INADDR_ANY;
17408 
17409 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17410 		if (rep_ipif == NULL) {
17411 			ipif->ipif_was_up = B_FALSE;
17412 			IPIF_UNMARK_MOVING(ipif);
17413 			return (ENOMEM);
17414 		}
17415 		*rep_ipif = ipif_zero;
17416 		/*
17417 		 * Before we put the ipif on the list, store the addresses
17418 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17419 		 * assumes so. This logic is not any different from what
17420 		 * ipif_allocate does.
17421 		 */
17422 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17423 		    &rep_ipif->ipif_v6lcl_addr);
17424 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17425 		    &rep_ipif->ipif_v6src_addr);
17426 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17427 		    &rep_ipif->ipif_v6subnet);
17428 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17429 		    &rep_ipif->ipif_v6net_mask);
17430 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17431 		    &rep_ipif->ipif_v6brd_addr);
17432 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17433 		    &rep_ipif->ipif_v6pp_dst_addr);
17434 		/*
17435 		 * We mark IPIF_NOFAILOVER so that this can never
17436 		 * move.
17437 		 */
17438 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17439 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17440 		rep_ipif->ipif_replace_zero = B_TRUE;
17441 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17442 		    MUTEX_DEFAULT, NULL);
17443 		rep_ipif->ipif_id = 0;
17444 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17445 		rep_ipif->ipif_ill = from_ill;
17446 		rep_ipif->ipif_orig_ifindex =
17447 		    from_ill->ill_phyint->phyint_ifindex;
17448 		/* Insert at head */
17449 		rep_ipif->ipif_next = from_ill->ill_ipif;
17450 		from_ill->ill_ipif = rep_ipif;
17451 		/*
17452 		 * We don't really care to let apps know about
17453 		 * this interface.
17454 		 */
17455 	}
17456 
17457 	if (remove_ipif) {
17458 		/*
17459 		 * We set to a max value above for this case to get
17460 		 * id zero. ASSERT that we did get one.
17461 		 */
17462 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17463 		rep_ipif = to_ipif;
17464 		to_ill->ill_ipif = rep_ipif->ipif_next;
17465 		rep_ipif->ipif_next = NULL;
17466 		/*
17467 		 * If some apps scanned and find this interface,
17468 		 * it is time to let them know, so that they can
17469 		 * delete it.
17470 		 */
17471 
17472 		*rep_ipif_ptr = rep_ipif;
17473 	}
17474 
17475 	/* Get it out of the ILL interface list. */
17476 	ipif_remove(ipif, B_FALSE);
17477 
17478 	/* Assign the new ill */
17479 	ipif->ipif_ill = to_ill;
17480 	ipif->ipif_id = unit;
17481 	/* id has already been checked */
17482 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17483 	ASSERT(rc == 0);
17484 	/* Let SCTP update its list */
17485 	sctp_move_ipif(ipif, from_ill, to_ill);
17486 	/*
17487 	 * Handle the failover and failback of ipif_t between
17488 	 * ill_t that have differing maximum mtu values.
17489 	 */
17490 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17491 		if (ipif->ipif_saved_mtu == 0) {
17492 			/*
17493 			 * As this ipif_t is moving to an ill_t
17494 			 * that has a lower ill_max_mtu, its
17495 			 * ipif_mtu needs to be saved so it can
17496 			 * be restored during failback or during
17497 			 * failover to an ill_t which has a
17498 			 * higher ill_max_mtu.
17499 			 */
17500 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17501 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17502 		} else {
17503 			/*
17504 			 * The ipif_t is, once again, moving to
17505 			 * an ill_t that has a lower maximum mtu
17506 			 * value.
17507 			 */
17508 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17509 		}
17510 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17511 	    ipif->ipif_saved_mtu != 0) {
17512 		/*
17513 		 * The mtu of this ipif_t had to be reduced
17514 		 * during an earlier failover; this is an
17515 		 * opportunity for it to be increased (either as
17516 		 * part of another failover or a failback).
17517 		 */
17518 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17519 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17520 			ipif->ipif_saved_mtu = 0;
17521 		} else {
17522 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17523 		}
17524 	}
17525 
17526 	/*
17527 	 * We preserve all the other fields of the ipif including
17528 	 * ipif_saved_ire_mp. The routes that are saved here will
17529 	 * be recreated on the new interface and back on the old
17530 	 * interface when we move back.
17531 	 */
17532 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17533 
17534 	return (err);
17535 }
17536 
17537 static int
17538 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17539     int ifindex, ipif_t **rep_ipif_ptr)
17540 {
17541 	ipif_t *mipif;
17542 	ipif_t *ipif_next;
17543 	int err;
17544 
17545 	/*
17546 	 * We don't really try to MOVE back things if some of the
17547 	 * operations fail. The daemon will take care of moving again
17548 	 * later on.
17549 	 */
17550 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17551 		ipif_next = mipif->ipif_next;
17552 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17553 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17554 
17555 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17556 
17557 			/*
17558 			 * When the MOVE fails, it is the job of the
17559 			 * application to take care of this properly
17560 			 * i.e try again if it is ENOMEM.
17561 			 */
17562 			if (mipif->ipif_ill != from_ill) {
17563 				/*
17564 				 * ipif has moved.
17565 				 *
17566 				 * Move the multicast memberships associated
17567 				 * with this ipif to the new ill. For IPv6, we
17568 				 * do it once after all the ipifs are moved
17569 				 * (in ill_move) as they are not associated
17570 				 * with ipifs.
17571 				 *
17572 				 * We need to move the ilms as the ipif has
17573 				 * already been moved to a new ill even
17574 				 * in the case of errors. Neither
17575 				 * ilm_free(ipif) will find the ilm
17576 				 * when somebody unplumbs this ipif nor
17577 				 * ilm_delete(ilm) will be able to find the
17578 				 * ilm, if we don't move now.
17579 				 */
17580 				if (!from_ill->ill_isv6)
17581 					ilm_move_v4(from_ill, to_ill, mipif);
17582 			}
17583 
17584 			if (err != 0)
17585 				return (err);
17586 		}
17587 	}
17588 	return (0);
17589 }
17590 
17591 static int
17592 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17593 {
17594 	int ifindex;
17595 	int err;
17596 	struct iocblk	*iocp;
17597 	ipif_t	*ipif;
17598 	ipif_t *rep_ipif_ptr = NULL;
17599 	ipif_t	*from_ipif = NULL;
17600 	boolean_t check_rep_if = B_FALSE;
17601 	ip_stack_t	*ipst = from_ill->ill_ipst;
17602 
17603 	iocp = (struct iocblk *)mp->b_rptr;
17604 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17605 		/*
17606 		 * Move everything pointing at from_ill to to_ill.
17607 		 * We acheive this by passing in 0 as ifindex.
17608 		 */
17609 		ifindex = 0;
17610 	} else {
17611 		/*
17612 		 * Move everything pointing at from_ill whose original
17613 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17614 		 * We acheive this by passing in ifindex rather than 0.
17615 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17616 		 */
17617 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17618 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17619 	}
17620 
17621 	/*
17622 	 * Determine if there is at least one ipif that would move from
17623 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17624 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17625 	 * the move, in which case we need to quiesce the replacement ipif also.
17626 	 */
17627 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17628 	    from_ipif = from_ipif->ipif_next) {
17629 		if (((ifindex == 0) ||
17630 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17631 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17632 			check_rep_if = B_TRUE;
17633 			break;
17634 		}
17635 	}
17636 
17637 
17638 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17639 
17640 	GRAB_ILL_LOCKS(from_ill, to_ill);
17641 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17642 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17643 		    mp, ILL_MOVE_OK);
17644 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17645 		return (EINPROGRESS);
17646 	}
17647 
17648 	/* Check if the replacement ipif is quiescent to delete */
17649 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17650 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17651 		to_ill->ill_ipif->ipif_state_flags |=
17652 		    IPIF_MOVING | IPIF_CHANGING;
17653 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17654 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17655 			    mp, ILL_MOVE_OK);
17656 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17657 			return (EINPROGRESS);
17658 		}
17659 	}
17660 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17661 
17662 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17663 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17664 	GRAB_ILL_LOCKS(from_ill, to_ill);
17665 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17666 
17667 	/* ilm_move is done inside ipif_move for IPv4 */
17668 	if (err == 0 && from_ill->ill_isv6)
17669 		ilm_move_v6(from_ill, to_ill, ifindex);
17670 
17671 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17672 	rw_exit(&ipst->ips_ill_g_lock);
17673 
17674 	/*
17675 	 * send rts messages and multicast messages.
17676 	 */
17677 	if (rep_ipif_ptr != NULL) {
17678 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17679 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17680 			rep_ipif_ptr->ipif_recovery_id = 0;
17681 		}
17682 		ip_rts_ifmsg(rep_ipif_ptr);
17683 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17684 #ifdef DEBUG
17685 		ipif_trace_cleanup(rep_ipif_ptr);
17686 #endif
17687 		mi_free(rep_ipif_ptr);
17688 	}
17689 
17690 	conn_move_ill(from_ill, to_ill, ifindex);
17691 
17692 	return (err);
17693 }
17694 
17695 /*
17696  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17697  * Also checks for the validity of the arguments.
17698  * Note: We are already exclusive inside the from group.
17699  * It is upto the caller to release refcnt on the to_ill's.
17700  */
17701 static int
17702 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17703     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17704 {
17705 	int dst_index;
17706 	ipif_t *ipif_v4, *ipif_v6;
17707 	struct lifreq *lifr;
17708 	mblk_t *mp1;
17709 	boolean_t exists;
17710 	sin_t	*sin;
17711 	int	err = 0;
17712 	ip_stack_t	*ipst;
17713 
17714 	if (CONN_Q(q))
17715 		ipst = CONNQ_TO_IPST(q);
17716 	else
17717 		ipst = ILLQ_TO_IPST(q);
17718 
17719 
17720 	if ((mp1 = mp->b_cont) == NULL)
17721 		return (EPROTO);
17722 
17723 	if ((mp1 = mp1->b_cont) == NULL)
17724 		return (EPROTO);
17725 
17726 	lifr = (struct lifreq *)mp1->b_rptr;
17727 	sin = (sin_t *)&lifr->lifr_addr;
17728 
17729 	/*
17730 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17731 	 * specific operations.
17732 	 */
17733 	if (sin->sin_family != AF_UNSPEC)
17734 		return (EINVAL);
17735 
17736 	/*
17737 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17738 	 * NULLs for the last 4 args and we know the lookup won't fail
17739 	 * with EINPROGRESS.
17740 	 */
17741 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17742 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17743 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17744 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17745 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17746 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17747 
17748 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17749 		return (ENXIO);
17750 
17751 	if (ipif_v4 != NULL) {
17752 		ASSERT(ipif_v4->ipif_refcnt != 0);
17753 		if (ipif_v4->ipif_id != 0) {
17754 			err = EINVAL;
17755 			goto done;
17756 		}
17757 
17758 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17759 		*ill_from_v4 = ipif_v4->ipif_ill;
17760 	}
17761 
17762 	if (ipif_v6 != NULL) {
17763 		ASSERT(ipif_v6->ipif_refcnt != 0);
17764 		if (ipif_v6->ipif_id != 0) {
17765 			err = EINVAL;
17766 			goto done;
17767 		}
17768 
17769 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17770 		*ill_from_v6 = ipif_v6->ipif_ill;
17771 	}
17772 
17773 	err = 0;
17774 	dst_index = lifr->lifr_movetoindex;
17775 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17776 	    q, mp, ip_process_ioctl, &err, ipst);
17777 	if (err != 0) {
17778 		/*
17779 		 * There could be only v6.
17780 		 */
17781 		if (err != ENXIO)
17782 			goto done;
17783 		err = 0;
17784 	}
17785 
17786 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17787 	    q, mp, ip_process_ioctl, &err, ipst);
17788 	if (err != 0) {
17789 		if (err != ENXIO)
17790 			goto done;
17791 		if (*ill_to_v4 == NULL) {
17792 			err = ENXIO;
17793 			goto done;
17794 		}
17795 		err = 0;
17796 	}
17797 
17798 	/*
17799 	 * If we have something to MOVE i.e "from" not NULL,
17800 	 * "to" should be non-NULL.
17801 	 */
17802 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17803 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17804 		err = EINVAL;
17805 	}
17806 
17807 done:
17808 	if (ipif_v4 != NULL)
17809 		ipif_refrele(ipif_v4);
17810 	if (ipif_v6 != NULL)
17811 		ipif_refrele(ipif_v6);
17812 	return (err);
17813 }
17814 
17815 /*
17816  * FAILOVER and FAILBACK are modelled as MOVE operations.
17817  *
17818  * We don't check whether the MOVE is within the same group or
17819  * not, because this ioctl can be used as a generic mechanism
17820  * to failover from interface A to B, though things will function
17821  * only if they are really part of the same group. Moreover,
17822  * all ipifs may be down and hence temporarily out of the group.
17823  *
17824  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17825  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17826  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17827  * have been deleted and there are no active references. Once quiescent the
17828  * ipif's are moved and brought up on the new ill.
17829  *
17830  * Normally the source ill and destination ill belong to the same IPMP group
17831  * and hence the same ipsq_t. In the event they don't belong to the same
17832  * same group the two ipsq's are first merged into one ipsq - that of the
17833  * to_ill. The multicast memberships on the source and destination ill cannot
17834  * change during the move operation since multicast joins/leaves also have to
17835  * execute on the same ipsq and are hence serialized.
17836  */
17837 /* ARGSUSED */
17838 int
17839 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17840     ip_ioctl_cmd_t *ipip, void *ifreq)
17841 {
17842 	ill_t *ill_to_v4 = NULL;
17843 	ill_t *ill_to_v6 = NULL;
17844 	ill_t *ill_from_v4 = NULL;
17845 	ill_t *ill_from_v6 = NULL;
17846 	int err = 0;
17847 
17848 	/*
17849 	 * setup from and to ill's, we can get EINPROGRESS only for
17850 	 * to_ill's.
17851 	 */
17852 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17853 	    &ill_to_v4, &ill_to_v6);
17854 
17855 	if (err != 0) {
17856 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17857 		goto done;
17858 	}
17859 
17860 	/*
17861 	 * nothing to do.
17862 	 */
17863 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17864 		goto done;
17865 	}
17866 
17867 	/*
17868 	 * nothing to do.
17869 	 */
17870 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17871 		goto done;
17872 	}
17873 
17874 	/*
17875 	 * Mark the ill as changing.
17876 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17877 	 * in ill_up_ipifs in case of error they are cleared below.
17878 	 */
17879 
17880 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17881 	if (ill_from_v4 != NULL)
17882 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17883 	if (ill_from_v6 != NULL)
17884 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17885 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17886 
17887 	/*
17888 	 * Make sure that both src and dst are
17889 	 * in the same syncq group. If not make it happen.
17890 	 * We are not holding any locks because we are the writer
17891 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17892 	 * to protect to_ipsq against changing.
17893 	 */
17894 	if (ill_from_v4 != NULL) {
17895 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17896 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17897 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17898 			    NULL, mp, q);
17899 			goto err_ret;
17900 
17901 		}
17902 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17903 	} else {
17904 
17905 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17906 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17907 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17908 			    NULL, mp, q);
17909 			goto err_ret;
17910 
17911 		}
17912 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17913 	}
17914 
17915 	/*
17916 	 * Now that the ipsq's have been merged and we are the writer
17917 	 * lets mark to_ill as changing as well.
17918 	 */
17919 
17920 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17921 	if (ill_to_v4 != NULL)
17922 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17923 	if (ill_to_v6 != NULL)
17924 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17925 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17926 
17927 	/*
17928 	 * Its ok for us to proceed with the move even if
17929 	 * ill_pending_mp is non null on one of the from ill's as the reply
17930 	 * should not be looking at the ipif, it should only care about the
17931 	 * ill itself.
17932 	 */
17933 
17934 	/*
17935 	 * lets move ipv4 first.
17936 	 */
17937 	if (ill_from_v4 != NULL) {
17938 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
17939 		ill_from_v4->ill_move_in_progress = B_TRUE;
17940 		ill_to_v4->ill_move_in_progress = B_TRUE;
17941 		ill_to_v4->ill_move_peer = ill_from_v4;
17942 		ill_from_v4->ill_move_peer = ill_to_v4;
17943 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
17944 	}
17945 
17946 	/*
17947 	 * Now lets move ipv6.
17948 	 */
17949 	if (err == 0 && ill_from_v6 != NULL) {
17950 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
17951 		ill_from_v6->ill_move_in_progress = B_TRUE;
17952 		ill_to_v6->ill_move_in_progress = B_TRUE;
17953 		ill_to_v6->ill_move_peer = ill_from_v6;
17954 		ill_from_v6->ill_move_peer = ill_to_v6;
17955 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
17956 	}
17957 
17958 err_ret:
17959 	/*
17960 	 * EINPROGRESS means we are waiting for the ipif's that need to be
17961 	 * moved to become quiescent.
17962 	 */
17963 	if (err == EINPROGRESS) {
17964 		goto done;
17965 	}
17966 
17967 	/*
17968 	 * if err is set ill_up_ipifs will not be called
17969 	 * lets clear the flags.
17970 	 */
17971 
17972 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17973 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17974 	/*
17975 	 * Some of the clearing may be redundant. But it is simple
17976 	 * not making any extra checks.
17977 	 */
17978 	if (ill_from_v6 != NULL) {
17979 		ill_from_v6->ill_move_in_progress = B_FALSE;
17980 		ill_from_v6->ill_move_peer = NULL;
17981 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
17982 	}
17983 	if (ill_from_v4 != NULL) {
17984 		ill_from_v4->ill_move_in_progress = B_FALSE;
17985 		ill_from_v4->ill_move_peer = NULL;
17986 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
17987 	}
17988 	if (ill_to_v6 != NULL) {
17989 		ill_to_v6->ill_move_in_progress = B_FALSE;
17990 		ill_to_v6->ill_move_peer = NULL;
17991 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
17992 	}
17993 	if (ill_to_v4 != NULL) {
17994 		ill_to_v4->ill_move_in_progress = B_FALSE;
17995 		ill_to_v4->ill_move_peer = NULL;
17996 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
17997 	}
17998 
17999 	/*
18000 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18001 	 * Do this always to maintain proper state i.e even in case of errors.
18002 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18003 	 * we need not call on both v4 and v6 interfaces.
18004 	 */
18005 	if (ill_from_v4 != NULL) {
18006 		if ((ill_from_v4->ill_phyint->phyint_flags &
18007 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18008 			phyint_inactive(ill_from_v4->ill_phyint);
18009 		}
18010 	} else if (ill_from_v6 != NULL) {
18011 		if ((ill_from_v6->ill_phyint->phyint_flags &
18012 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18013 			phyint_inactive(ill_from_v6->ill_phyint);
18014 		}
18015 	}
18016 
18017 	if (ill_to_v4 != NULL) {
18018 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18019 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18020 		}
18021 	} else if (ill_to_v6 != NULL) {
18022 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18023 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18024 		}
18025 	}
18026 
18027 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18028 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18029 
18030 no_err:
18031 	/*
18032 	 * lets bring the interfaces up on the to_ill.
18033 	 */
18034 	if (err == 0) {
18035 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18036 		    q, mp);
18037 	}
18038 
18039 	if (err == 0) {
18040 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18041 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18042 
18043 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18044 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18045 	}
18046 done:
18047 
18048 	if (ill_to_v4 != NULL) {
18049 		ill_refrele(ill_to_v4);
18050 	}
18051 	if (ill_to_v6 != NULL) {
18052 		ill_refrele(ill_to_v6);
18053 	}
18054 
18055 	return (err);
18056 }
18057 
18058 static void
18059 ill_dl_down(ill_t *ill)
18060 {
18061 	/*
18062 	 * The ill is down; unbind but stay attached since we're still
18063 	 * associated with a PPA. If we have negotiated DLPI capabilites
18064 	 * with the data link service provider (IDS_OK) then reset them.
18065 	 * The interval between unbinding and rebinding is potentially
18066 	 * unbounded hence we cannot assume things will be the same.
18067 	 * The DLPI capabilities will be probed again when the data link
18068 	 * is brought up.
18069 	 */
18070 	mblk_t	*mp = ill->ill_unbind_mp;
18071 
18072 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18073 
18074 	ill->ill_unbind_mp = NULL;
18075 	if (mp != NULL) {
18076 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18077 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18078 		    ill->ill_name));
18079 		mutex_enter(&ill->ill_lock);
18080 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18081 		mutex_exit(&ill->ill_lock);
18082 		/*
18083 		 * Reset the capabilities if the negotiation is done or is
18084 		 * still in progress. Note that ill_capability_reset() will
18085 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18086 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18087 		 *
18088 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18089 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18090 		 * the capabilities renegotiation from happening.
18091 		 */
18092 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18093 			ill_capability_reset(ill);
18094 		ill->ill_capab_reneg = B_FALSE;
18095 
18096 		ill_dlpi_send(ill, mp);
18097 	}
18098 
18099 	/*
18100 	 * Toss all of our multicast memberships.  We could keep them, but
18101 	 * then we'd have to do bookkeeping of any joins and leaves performed
18102 	 * by the application while the the interface is down (we can't just
18103 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18104 	 * on a downed interface).
18105 	 */
18106 	ill_leave_multicast(ill);
18107 
18108 	mutex_enter(&ill->ill_lock);
18109 	ill->ill_dl_up = 0;
18110 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18111 	mutex_exit(&ill->ill_lock);
18112 }
18113 
18114 static void
18115 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18116 {
18117 	union DL_primitives *dlp;
18118 	t_uscalar_t prim;
18119 
18120 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18121 
18122 	dlp = (union DL_primitives *)mp->b_rptr;
18123 	prim = dlp->dl_primitive;
18124 
18125 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18126 	    dl_primstr(prim), prim, ill->ill_name));
18127 
18128 	switch (prim) {
18129 	case DL_PHYS_ADDR_REQ:
18130 	{
18131 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18132 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18133 		break;
18134 	}
18135 	case DL_BIND_REQ:
18136 		mutex_enter(&ill->ill_lock);
18137 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18138 		mutex_exit(&ill->ill_lock);
18139 		break;
18140 	}
18141 
18142 	/*
18143 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18144 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18145 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18146 	 */
18147 	mutex_enter(&ill->ill_lock);
18148 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18149 	    (prim == DL_UNBIND_REQ)) {
18150 		ill->ill_dlpi_pending = prim;
18151 	}
18152 	mutex_exit(&ill->ill_lock);
18153 
18154 	putnext(ill->ill_wq, mp);
18155 }
18156 
18157 /*
18158  * Helper function for ill_dlpi_send().
18159  */
18160 /* ARGSUSED */
18161 static void
18162 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18163 {
18164 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18165 }
18166 
18167 /*
18168  * Send a DLPI control message to the driver but make sure there
18169  * is only one outstanding message. Uses ill_dlpi_pending to tell
18170  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18171  * when an ACK or a NAK is received to process the next queued message.
18172  */
18173 void
18174 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18175 {
18176 	mblk_t **mpp;
18177 
18178 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18179 
18180 	/*
18181 	 * To ensure that any DLPI requests for current exclusive operation
18182 	 * are always completely sent before any DLPI messages for other
18183 	 * operations, require writer access before enqueuing.
18184 	 */
18185 	if (!IAM_WRITER_ILL(ill)) {
18186 		ill_refhold(ill);
18187 		/* qwriter_ip() does the ill_refrele() */
18188 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18189 		    NEW_OP, B_TRUE);
18190 		return;
18191 	}
18192 
18193 	mutex_enter(&ill->ill_lock);
18194 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18195 		/* Must queue message. Tail insertion */
18196 		mpp = &ill->ill_dlpi_deferred;
18197 		while (*mpp != NULL)
18198 			mpp = &((*mpp)->b_next);
18199 
18200 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18201 		    ill->ill_name));
18202 
18203 		*mpp = mp;
18204 		mutex_exit(&ill->ill_lock);
18205 		return;
18206 	}
18207 	mutex_exit(&ill->ill_lock);
18208 	ill_dlpi_dispatch(ill, mp);
18209 }
18210 
18211 /*
18212  * Send all deferred DLPI messages without waiting for their ACKs.
18213  */
18214 void
18215 ill_dlpi_send_deferred(ill_t *ill)
18216 {
18217 	mblk_t *mp, *nextmp;
18218 
18219 	/*
18220 	 * Clear ill_dlpi_pending so that the message is not queued in
18221 	 * ill_dlpi_send().
18222 	 */
18223 	mutex_enter(&ill->ill_lock);
18224 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18225 	mp = ill->ill_dlpi_deferred;
18226 	ill->ill_dlpi_deferred = NULL;
18227 	mutex_exit(&ill->ill_lock);
18228 
18229 	for (; mp != NULL; mp = nextmp) {
18230 		nextmp = mp->b_next;
18231 		mp->b_next = NULL;
18232 		ill_dlpi_send(ill, mp);
18233 	}
18234 }
18235 
18236 /*
18237  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18238  */
18239 boolean_t
18240 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18241 {
18242 	t_uscalar_t pending;
18243 
18244 	mutex_enter(&ill->ill_lock);
18245 	if (ill->ill_dlpi_pending == prim) {
18246 		mutex_exit(&ill->ill_lock);
18247 		return (B_TRUE);
18248 	}
18249 
18250 	/*
18251 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18252 	 * without waiting, so don't print any warnings in that case.
18253 	 */
18254 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18255 		mutex_exit(&ill->ill_lock);
18256 		return (B_FALSE);
18257 	}
18258 	pending = ill->ill_dlpi_pending;
18259 	mutex_exit(&ill->ill_lock);
18260 
18261 	if (pending == DL_PRIM_INVAL) {
18262 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18263 		    "received unsolicited ack for %s on %s\n",
18264 		    dl_primstr(prim), ill->ill_name);
18265 	} else {
18266 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18267 		    "received unexpected ack for %s on %s (expecting %s)\n",
18268 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18269 	}
18270 	return (B_FALSE);
18271 }
18272 
18273 /*
18274  * Called when an DLPI control message has been acked or nacked to
18275  * send down the next queued message (if any).
18276  */
18277 void
18278 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18279 {
18280 	mblk_t *mp;
18281 
18282 	ASSERT(IAM_WRITER_ILL(ill));
18283 	mutex_enter(&ill->ill_lock);
18284 
18285 	ASSERT(prim != DL_PRIM_INVAL);
18286 	ASSERT(ill->ill_dlpi_pending == prim);
18287 
18288 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18289 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18290 
18291 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18292 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18293 		cv_signal(&ill->ill_cv);
18294 		mutex_exit(&ill->ill_lock);
18295 		return;
18296 	}
18297 
18298 	ill->ill_dlpi_deferred = mp->b_next;
18299 	mp->b_next = NULL;
18300 	mutex_exit(&ill->ill_lock);
18301 
18302 	ill_dlpi_dispatch(ill, mp);
18303 }
18304 
18305 void
18306 conn_delete_ire(conn_t *connp, caddr_t arg)
18307 {
18308 	ipif_t	*ipif = (ipif_t *)arg;
18309 	ire_t	*ire;
18310 
18311 	/*
18312 	 * Look at the cached ires on conns which has pointers to ipifs.
18313 	 * We just call ire_refrele which clears up the reference
18314 	 * to ire. Called when a conn closes. Also called from ipif_free
18315 	 * to cleanup indirect references to the stale ipif via the cached ire.
18316 	 */
18317 	mutex_enter(&connp->conn_lock);
18318 	ire = connp->conn_ire_cache;
18319 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18320 		connp->conn_ire_cache = NULL;
18321 		mutex_exit(&connp->conn_lock);
18322 		IRE_REFRELE_NOTR(ire);
18323 		return;
18324 	}
18325 	mutex_exit(&connp->conn_lock);
18326 
18327 }
18328 
18329 /*
18330  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18331  * of IREs. Those IREs may have been previously cached in the conn structure.
18332  * This ipcl_walk() walker function releases all references to such IREs based
18333  * on the condemned flag.
18334  */
18335 /* ARGSUSED */
18336 void
18337 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18338 {
18339 	ire_t	*ire;
18340 
18341 	mutex_enter(&connp->conn_lock);
18342 	ire = connp->conn_ire_cache;
18343 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18344 		connp->conn_ire_cache = NULL;
18345 		mutex_exit(&connp->conn_lock);
18346 		IRE_REFRELE_NOTR(ire);
18347 		return;
18348 	}
18349 	mutex_exit(&connp->conn_lock);
18350 }
18351 
18352 /*
18353  * Take down a specific interface, but don't lose any information about it.
18354  * Also delete interface from its interface group (ifgrp).
18355  * (Always called as writer.)
18356  * This function goes through the down sequence even if the interface is
18357  * already down. There are 2 reasons.
18358  * a. Currently we permit interface routes that depend on down interfaces
18359  *    to be added. This behaviour itself is questionable. However it appears
18360  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18361  *    time. We go thru the cleanup in order to remove these routes.
18362  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18363  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18364  *    down, but we need to cleanup i.e. do ill_dl_down and
18365  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18366  *
18367  * IP-MT notes:
18368  *
18369  * Model of reference to interfaces.
18370  *
18371  * The following members in ipif_t track references to the ipif.
18372  *	int     ipif_refcnt;    Active reference count
18373  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18374  * The following members in ill_t track references to the ill.
18375  *	int             ill_refcnt;     active refcnt
18376  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18377  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18378  *
18379  * Reference to an ipif or ill can be obtained in any of the following ways.
18380  *
18381  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18382  * Pointers to ipif / ill from other data structures viz ire and conn.
18383  * Implicit reference to the ipif / ill by holding a reference to the ire.
18384  *
18385  * The ipif/ill lookup functions return a reference held ipif / ill.
18386  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18387  * This is a purely dynamic reference count associated with threads holding
18388  * references to the ipif / ill. Pointers from other structures do not
18389  * count towards this reference count.
18390  *
18391  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18392  * ipif/ill. This is incremented whenever a new ire is created referencing the
18393  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18394  * actually added to the ire hash table. The count is decremented in
18395  * ire_inactive where the ire is destroyed.
18396  *
18397  * nce's reference ill's thru nce_ill and the count of nce's associated with
18398  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18399  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18400  * table. Similarly it is decremented in ndp_inactive() where the nce
18401  * is destroyed.
18402  *
18403  * Flow of ioctls involving interface down/up
18404  *
18405  * The following is the sequence of an attempt to set some critical flags on an
18406  * up interface.
18407  * ip_sioctl_flags
18408  * ipif_down
18409  * wait for ipif to be quiescent
18410  * ipif_down_tail
18411  * ip_sioctl_flags_tail
18412  *
18413  * All set ioctls that involve down/up sequence would have a skeleton similar
18414  * to the above. All the *tail functions are called after the refcounts have
18415  * dropped to the appropriate values.
18416  *
18417  * The mechanism to quiesce an ipif is as follows.
18418  *
18419  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18420  * on the ipif. Callers either pass a flag requesting wait or the lookup
18421  *  functions will return NULL.
18422  *
18423  * Delete all ires referencing this ipif
18424  *
18425  * Any thread attempting to do an ipif_refhold on an ipif that has been
18426  * obtained thru a cached pointer will first make sure that
18427  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18428  * increment the refcount.
18429  *
18430  * The above guarantees that the ipif refcount will eventually come down to
18431  * zero and the ipif will quiesce, once all threads that currently hold a
18432  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18433  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18434  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18435  * drop to zero.
18436  *
18437  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18438  *
18439  * Threads trying to lookup an ipif or ill can pass a flag requesting
18440  * wait and restart if the ipif / ill cannot be looked up currently.
18441  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18442  * failure if the ipif is currently undergoing an exclusive operation, and
18443  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18444  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18445  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18446  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18447  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18448  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18449  * until we release the ipsq_lock, even though the the ill/ipif state flags
18450  * can change after we drop the ill_lock.
18451  *
18452  * An attempt to send out a packet using an ipif that is currently
18453  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18454  * operation and restart it later when the exclusive condition on the ipif ends.
18455  * This is an example of not passing the wait flag to the lookup functions. For
18456  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18457  * out a multicast packet on that ipif will fail while the ipif is
18458  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18459  * currently IPIF_CHANGING will also fail.
18460  */
18461 int
18462 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18463 {
18464 	ill_t		*ill = ipif->ipif_ill;
18465 	phyint_t	*phyi;
18466 	conn_t		*connp;
18467 	boolean_t	success;
18468 	boolean_t	ipif_was_up = B_FALSE;
18469 	ip_stack_t	*ipst = ill->ill_ipst;
18470 
18471 	ASSERT(IAM_WRITER_IPIF(ipif));
18472 
18473 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18474 
18475 	if (ipif->ipif_flags & IPIF_UP) {
18476 		mutex_enter(&ill->ill_lock);
18477 		ipif->ipif_flags &= ~IPIF_UP;
18478 		ASSERT(ill->ill_ipif_up_count > 0);
18479 		--ill->ill_ipif_up_count;
18480 		mutex_exit(&ill->ill_lock);
18481 		ipif_was_up = B_TRUE;
18482 		/* Update status in SCTP's list */
18483 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18484 	}
18485 
18486 	/*
18487 	 * Blow away memberships we established in ipif_multicast_up().
18488 	 */
18489 	ipif_multicast_down(ipif);
18490 
18491 	/*
18492 	 * Remove from the mapping for __sin6_src_id. We insert only
18493 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18494 	 * stored as mapped addresses, we need to check for mapped
18495 	 * INADDR_ANY also.
18496 	 */
18497 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18498 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18499 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18500 		int err;
18501 
18502 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18503 		    ipif->ipif_zoneid, ipst);
18504 		if (err != 0) {
18505 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18506 		}
18507 	}
18508 
18509 	/*
18510 	 * Before we delete the ill from the group (if any), we need
18511 	 * to make sure that we delete all the routes dependent on
18512 	 * this and also any ipifs dependent on this ipif for
18513 	 * source address. We need to do before we delete from
18514 	 * the group because
18515 	 *
18516 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18517 	 *
18518 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18519 	 *    for re-doing source address selection. Note that
18520 	 *    ipif_select_source[_v6] called from
18521 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18522 	 *    because we have already marked down here i.e cleared
18523 	 *    IPIF_UP.
18524 	 */
18525 	if (ipif->ipif_isv6) {
18526 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18527 		    ipst);
18528 	} else {
18529 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18530 		    ipst);
18531 	}
18532 
18533 	/*
18534 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18535 	 * ires have been deleted above. Otherwise a thread could end up
18536 	 * caching an ire in a conn after we have finished the cleanup of the
18537 	 * conn. The caching is done after making sure that the ire is not yet
18538 	 * condemned. Also documented in the block comment above ip_output
18539 	 */
18540 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18541 	/* Also, delete the ires cached in SCTP */
18542 	sctp_ire_cache_flush(ipif);
18543 
18544 	/*
18545 	 * Update any other ipifs which have used "our" local address as
18546 	 * a source address. This entails removing and recreating IRE_INTERFACE
18547 	 * entries for such ipifs.
18548 	 */
18549 	if (ipif->ipif_isv6)
18550 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18551 	else
18552 		ipif_update_other_ipifs(ipif, ill->ill_group);
18553 
18554 	if (ipif_was_up) {
18555 		/*
18556 		 * Check whether it is last ipif to leave this group.
18557 		 * If this is the last ipif to leave, we should remove
18558 		 * this ill from the group as ipif_select_source will not
18559 		 * be able to find any useful ipifs if this ill is selected
18560 		 * for load balancing.
18561 		 *
18562 		 * For nameless groups, we should call ifgrp_delete if this
18563 		 * belongs to some group. As this ipif is going down, we may
18564 		 * need to reconstruct groups.
18565 		 */
18566 		phyi = ill->ill_phyint;
18567 		/*
18568 		 * If the phyint_groupname_len is 0, it may or may not
18569 		 * be in the nameless group. If the phyint_groupname_len is
18570 		 * not 0, then this ill should be part of some group.
18571 		 * As we always insert this ill in the group if
18572 		 * phyint_groupname_len is not zero when the first ipif
18573 		 * comes up (in ipif_up_done), it should be in a group
18574 		 * when the namelen is not 0.
18575 		 *
18576 		 * NOTE : When we delete the ill from the group,it will
18577 		 * blow away all the IRE_CACHES pointing either at this ipif or
18578 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18579 		 * should be pointing at this ill.
18580 		 */
18581 		ASSERT(phyi->phyint_groupname_len == 0 ||
18582 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18583 
18584 		if (phyi->phyint_groupname_len != 0) {
18585 			if (ill->ill_ipif_up_count == 0)
18586 				illgrp_delete(ill);
18587 		}
18588 
18589 		/*
18590 		 * If we have deleted some of the broadcast ires associated
18591 		 * with this ipif, we need to re-nominate somebody else if
18592 		 * the ires that we deleted were the nominated ones.
18593 		 */
18594 		if (ill->ill_group != NULL && !ill->ill_isv6)
18595 			ipif_renominate_bcast(ipif);
18596 	}
18597 
18598 	/*
18599 	 * neighbor-discovery or arp entries for this interface.
18600 	 */
18601 	ipif_ndp_down(ipif);
18602 
18603 	/*
18604 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18605 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18606 	 * and ill_delete -> ipif_free -> ipif_down
18607 	 */
18608 	if (mp == NULL) {
18609 		ASSERT(q == NULL);
18610 		return (0);
18611 	}
18612 
18613 	if (CONN_Q(q)) {
18614 		connp = Q_TO_CONN(q);
18615 		mutex_enter(&connp->conn_lock);
18616 	} else {
18617 		connp = NULL;
18618 	}
18619 	mutex_enter(&ill->ill_lock);
18620 	/*
18621 	 * Are there any ire's pointing to this ipif that are still active ?
18622 	 * If this is the last ipif going down, are there any ire's pointing
18623 	 * to this ill that are still active ?
18624 	 */
18625 	if (ipif_is_quiescent(ipif)) {
18626 		mutex_exit(&ill->ill_lock);
18627 		if (connp != NULL)
18628 			mutex_exit(&connp->conn_lock);
18629 		return (0);
18630 	}
18631 
18632 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18633 	    ill->ill_name, (void *)ill));
18634 	/*
18635 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18636 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18637 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18638 	 */
18639 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18640 	if (!success) {
18641 		/* The conn is closing. So just return */
18642 		ASSERT(connp != NULL);
18643 		mutex_exit(&ill->ill_lock);
18644 		mutex_exit(&connp->conn_lock);
18645 		return (EINTR);
18646 	}
18647 
18648 	mutex_exit(&ill->ill_lock);
18649 	if (connp != NULL)
18650 		mutex_exit(&connp->conn_lock);
18651 	return (EINPROGRESS);
18652 }
18653 
18654 void
18655 ipif_down_tail(ipif_t *ipif)
18656 {
18657 	ill_t	*ill = ipif->ipif_ill;
18658 
18659 	/*
18660 	 * Skip any loopback interface (null wq).
18661 	 * If this is the last logical interface on the ill
18662 	 * have ill_dl_down tell the driver we are gone (unbind)
18663 	 * Note that lun 0 can ipif_down even though
18664 	 * there are other logical units that are up.
18665 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18666 	 */
18667 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18668 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18669 	    ill->ill_dl_up) {
18670 		ill_dl_down(ill);
18671 	}
18672 	ill->ill_logical_down = 0;
18673 
18674 	/*
18675 	 * Have to be after removing the routes in ipif_down_delete_ire.
18676 	 */
18677 	if (ipif->ipif_isv6) {
18678 		if (ill->ill_flags & ILLF_XRESOLV)
18679 			ipif_arp_down(ipif);
18680 	} else {
18681 		ipif_arp_down(ipif);
18682 	}
18683 
18684 	ip_rts_ifmsg(ipif);
18685 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18686 }
18687 
18688 /*
18689  * Bring interface logically down without bringing the physical interface
18690  * down e.g. when the netmask is changed. This avoids long lasting link
18691  * negotiations between an ethernet interface and a certain switches.
18692  */
18693 static int
18694 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18695 {
18696 	/*
18697 	 * The ill_logical_down flag is a transient flag. It is set here
18698 	 * and is cleared once the down has completed in ipif_down_tail.
18699 	 * This flag does not indicate whether the ill stream is in the
18700 	 * DL_BOUND state with the driver. Instead this flag is used by
18701 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18702 	 * the driver. The state of the ill stream i.e. whether it is
18703 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18704 	 */
18705 	ipif->ipif_ill->ill_logical_down = 1;
18706 	return (ipif_down(ipif, q, mp));
18707 }
18708 
18709 /*
18710  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18711  * If the usesrc client ILL is already part of a usesrc group or not,
18712  * in either case a ire_stq with the matching usesrc client ILL will
18713  * locate the IRE's that need to be deleted. We want IREs to be created
18714  * with the new source address.
18715  */
18716 static void
18717 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18718 {
18719 	ill_t	*ucill = (ill_t *)ill_arg;
18720 
18721 	ASSERT(IAM_WRITER_ILL(ucill));
18722 
18723 	if (ire->ire_stq == NULL)
18724 		return;
18725 
18726 	if ((ire->ire_type == IRE_CACHE) &&
18727 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18728 		ire_delete(ire);
18729 }
18730 
18731 /*
18732  * ire_walk routine to delete every IRE dependent on the interface
18733  * address that is going down.	(Always called as writer.)
18734  * Works for both v4 and v6.
18735  * In addition for checking for ire_ipif matches it also checks for
18736  * IRE_CACHE entries which have the same source address as the
18737  * disappearing ipif since ipif_select_source might have picked
18738  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18739  * care of any IRE_INTERFACE with the disappearing source address.
18740  */
18741 static void
18742 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18743 {
18744 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18745 	ill_t *ire_ill;
18746 	ill_t *ipif_ill;
18747 
18748 	ASSERT(IAM_WRITER_IPIF(ipif));
18749 	if (ire->ire_ipif == NULL)
18750 		return;
18751 
18752 	/*
18753 	 * For IPv4, we derive source addresses for an IRE from ipif's
18754 	 * belonging to the same IPMP group as the IRE's outgoing
18755 	 * interface.  If an IRE's outgoing interface isn't in the
18756 	 * same IPMP group as a particular ipif, then that ipif
18757 	 * couldn't have been used as a source address for this IRE.
18758 	 *
18759 	 * For IPv6, source addresses are only restricted to the IPMP group
18760 	 * if the IRE is for a link-local address or a multicast address.
18761 	 * Otherwise, source addresses for an IRE can be chosen from
18762 	 * interfaces other than the the outgoing interface for that IRE.
18763 	 *
18764 	 * For source address selection details, see ipif_select_source()
18765 	 * and ipif_select_source_v6().
18766 	 */
18767 	if (ire->ire_ipversion == IPV4_VERSION ||
18768 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18769 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18770 		ire_ill = ire->ire_ipif->ipif_ill;
18771 		ipif_ill = ipif->ipif_ill;
18772 
18773 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18774 			return;
18775 		}
18776 	}
18777 
18778 
18779 	if (ire->ire_ipif != ipif) {
18780 		/*
18781 		 * Look for a matching source address.
18782 		 */
18783 		if (ire->ire_type != IRE_CACHE)
18784 			return;
18785 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18786 			return;
18787 
18788 		if (ire->ire_ipversion == IPV4_VERSION) {
18789 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18790 				return;
18791 		} else {
18792 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18793 			    &ipif->ipif_v6lcl_addr))
18794 				return;
18795 		}
18796 		ire_delete(ire);
18797 		return;
18798 	}
18799 	/*
18800 	 * ire_delete() will do an ire_flush_cache which will delete
18801 	 * all ire_ipif matches
18802 	 */
18803 	ire_delete(ire);
18804 }
18805 
18806 /*
18807  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18808  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18809  * 2) when an interface is brought up or down (on that ill).
18810  * This ensures that the IRE_CACHE entries don't retain stale source
18811  * address selection results.
18812  */
18813 void
18814 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18815 {
18816 	ill_t	*ill = (ill_t *)ill_arg;
18817 	ill_t	*ipif_ill;
18818 
18819 	ASSERT(IAM_WRITER_ILL(ill));
18820 	/*
18821 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18822 	 * Hence this should be IRE_CACHE.
18823 	 */
18824 	ASSERT(ire->ire_type == IRE_CACHE);
18825 
18826 	/*
18827 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18828 	 * We are only interested in IRE_CACHES that has borrowed
18829 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18830 	 * for which we need to look at ire_ipif->ipif_ill match
18831 	 * with ill.
18832 	 */
18833 	ASSERT(ire->ire_ipif != NULL);
18834 	ipif_ill = ire->ire_ipif->ipif_ill;
18835 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18836 	    ipif_ill->ill_group == ill->ill_group)) {
18837 		ire_delete(ire);
18838 	}
18839 }
18840 
18841 /*
18842  * Delete all the ire whose stq references ill_arg.
18843  */
18844 static void
18845 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18846 {
18847 	ill_t	*ill = (ill_t *)ill_arg;
18848 	ill_t	*ire_ill;
18849 
18850 	ASSERT(IAM_WRITER_ILL(ill));
18851 	/*
18852 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18853 	 * Hence this should be IRE_CACHE.
18854 	 */
18855 	ASSERT(ire->ire_type == IRE_CACHE);
18856 
18857 	/*
18858 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18859 	 * matches ill. We are only interested in IRE_CACHES that
18860 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18861 	 * filtering here.
18862 	 */
18863 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18864 
18865 	if (ire_ill == ill)
18866 		ire_delete(ire);
18867 }
18868 
18869 /*
18870  * This is called when an ill leaves the group. We want to delete
18871  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18872  * pointing at ill.
18873  */
18874 static void
18875 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18876 {
18877 	ill_t	*ill = (ill_t *)ill_arg;
18878 
18879 	ASSERT(IAM_WRITER_ILL(ill));
18880 	ASSERT(ill->ill_group == NULL);
18881 	/*
18882 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18883 	 * Hence this should be IRE_CACHE.
18884 	 */
18885 	ASSERT(ire->ire_type == IRE_CACHE);
18886 	/*
18887 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18888 	 * matches ill. We are interested in both.
18889 	 */
18890 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18891 	    (ire->ire_ipif->ipif_ill == ill));
18892 
18893 	ire_delete(ire);
18894 }
18895 
18896 /*
18897  * Initiate deallocate of an IPIF. Always called as writer. Called by
18898  * ill_delete or ip_sioctl_removeif.
18899  */
18900 static void
18901 ipif_free(ipif_t *ipif)
18902 {
18903 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18904 
18905 	ASSERT(IAM_WRITER_IPIF(ipif));
18906 
18907 	if (ipif->ipif_recovery_id != 0)
18908 		(void) untimeout(ipif->ipif_recovery_id);
18909 	ipif->ipif_recovery_id = 0;
18910 
18911 	/* Remove conn references */
18912 	reset_conn_ipif(ipif);
18913 
18914 	/*
18915 	 * Make sure we have valid net and subnet broadcast ire's for the
18916 	 * other ipif's which share them with this ipif.
18917 	 */
18918 	if (!ipif->ipif_isv6)
18919 		ipif_check_bcast_ires(ipif);
18920 
18921 	/*
18922 	 * Take down the interface. We can be called either from ill_delete
18923 	 * or from ip_sioctl_removeif.
18924 	 */
18925 	(void) ipif_down(ipif, NULL, NULL);
18926 
18927 	/*
18928 	 * Now that the interface is down, there's no chance it can still
18929 	 * become a duplicate.  Cancel any timer that may have been set while
18930 	 * tearing down.
18931 	 */
18932 	if (ipif->ipif_recovery_id != 0)
18933 		(void) untimeout(ipif->ipif_recovery_id);
18934 	ipif->ipif_recovery_id = 0;
18935 
18936 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18937 	/* Remove pointers to this ill in the multicast routing tables */
18938 	reset_mrt_vif_ipif(ipif);
18939 	rw_exit(&ipst->ips_ill_g_lock);
18940 }
18941 
18942 /*
18943  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
18944  * also ill_move().
18945  */
18946 static void
18947 ipif_free_tail(ipif_t *ipif)
18948 {
18949 	mblk_t	*mp;
18950 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
18951 
18952 	/*
18953 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
18954 	 */
18955 	mutex_enter(&ipif->ipif_saved_ire_lock);
18956 	mp = ipif->ipif_saved_ire_mp;
18957 	ipif->ipif_saved_ire_mp = NULL;
18958 	mutex_exit(&ipif->ipif_saved_ire_lock);
18959 	freemsg(mp);
18960 
18961 	/*
18962 	 * Need to hold both ill_g_lock and ill_lock while
18963 	 * inserting or removing an ipif from the linked list
18964 	 * of ipifs hanging off the ill.
18965 	 */
18966 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18967 	/*
18968 	 * Remove all IPv4 multicast memberships on the interface now.
18969 	 * IPv6 is not handled here as the multicast memberships are
18970 	 * tied to the ill rather than the ipif.
18971 	 */
18972 	ilm_free(ipif);
18973 
18974 	/*
18975 	 * Since we held the ill_g_lock while doing the ilm_free above,
18976 	 * we can assert the ilms were really deleted and not just marked
18977 	 * ILM_DELETED.
18978 	 */
18979 	ASSERT(ilm_walk_ipif(ipif) == 0);
18980 
18981 #ifdef DEBUG
18982 	ipif_trace_cleanup(ipif);
18983 #endif
18984 
18985 	/* Ask SCTP to take it out of it list */
18986 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
18987 
18988 	/* Get it out of the ILL interface list. */
18989 	ipif_remove(ipif, B_TRUE);
18990 	rw_exit(&ipst->ips_ill_g_lock);
18991 
18992 	mutex_destroy(&ipif->ipif_saved_ire_lock);
18993 
18994 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
18995 	ASSERT(ipif->ipif_recovery_id == 0);
18996 
18997 	/* Free the memory. */
18998 	mi_free(ipif);
18999 }
19000 
19001 /*
19002  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19003  * is zero.
19004  */
19005 void
19006 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19007 {
19008 	char	lbuf[LIFNAMSIZ];
19009 	char	*name;
19010 	size_t	name_len;
19011 
19012 	buf[0] = '\0';
19013 	name = ipif->ipif_ill->ill_name;
19014 	name_len = ipif->ipif_ill->ill_name_length;
19015 	if (ipif->ipif_id != 0) {
19016 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19017 		    ipif->ipif_id);
19018 		name = lbuf;
19019 		name_len = mi_strlen(name) + 1;
19020 	}
19021 	len -= 1;
19022 	buf[len] = '\0';
19023 	len = MIN(len, name_len);
19024 	bcopy(name, buf, len);
19025 }
19026 
19027 /*
19028  * Find an IPIF based on the name passed in.  Names can be of the
19029  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19030  * The <phys> string can have forms like <dev><#> (e.g., le0),
19031  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19032  * When there is no colon, the implied unit id is zero. <phys> must
19033  * correspond to the name of an ILL.  (May be called as writer.)
19034  */
19035 static ipif_t *
19036 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19037     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19038     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19039 {
19040 	char	*cp;
19041 	char	*endp;
19042 	long	id;
19043 	ill_t	*ill;
19044 	ipif_t	*ipif;
19045 	uint_t	ire_type;
19046 	boolean_t did_alloc = B_FALSE;
19047 	ipsq_t	*ipsq;
19048 
19049 	if (error != NULL)
19050 		*error = 0;
19051 
19052 	/*
19053 	 * If the caller wants to us to create the ipif, make sure we have a
19054 	 * valid zoneid
19055 	 */
19056 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19057 
19058 	if (namelen == 0) {
19059 		if (error != NULL)
19060 			*error = ENXIO;
19061 		return (NULL);
19062 	}
19063 
19064 	*exists = B_FALSE;
19065 	/* Look for a colon in the name. */
19066 	endp = &name[namelen];
19067 	for (cp = endp; --cp > name; ) {
19068 		if (*cp == IPIF_SEPARATOR_CHAR)
19069 			break;
19070 	}
19071 
19072 	if (*cp == IPIF_SEPARATOR_CHAR) {
19073 		/*
19074 		 * Reject any non-decimal aliases for logical
19075 		 * interfaces. Aliases with leading zeroes
19076 		 * are also rejected as they introduce ambiguity
19077 		 * in the naming of the interfaces.
19078 		 * In order to confirm with existing semantics,
19079 		 * and to not break any programs/script relying
19080 		 * on that behaviour, if<0>:0 is considered to be
19081 		 * a valid interface.
19082 		 *
19083 		 * If alias has two or more digits and the first
19084 		 * is zero, fail.
19085 		 */
19086 		if (&cp[2] < endp && cp[1] == '0') {
19087 			if (error != NULL)
19088 				*error = EINVAL;
19089 			return (NULL);
19090 		}
19091 	}
19092 
19093 	if (cp <= name) {
19094 		cp = endp;
19095 	} else {
19096 		*cp = '\0';
19097 	}
19098 
19099 	/*
19100 	 * Look up the ILL, based on the portion of the name
19101 	 * before the slash. ill_lookup_on_name returns a held ill.
19102 	 * Temporary to check whether ill exists already. If so
19103 	 * ill_lookup_on_name will clear it.
19104 	 */
19105 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19106 	    q, mp, func, error, &did_alloc, ipst);
19107 	if (cp != endp)
19108 		*cp = IPIF_SEPARATOR_CHAR;
19109 	if (ill == NULL)
19110 		return (NULL);
19111 
19112 	/* Establish the unit number in the name. */
19113 	id = 0;
19114 	if (cp < endp && *endp == '\0') {
19115 		/* If there was a colon, the unit number follows. */
19116 		cp++;
19117 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19118 			ill_refrele(ill);
19119 			if (error != NULL)
19120 				*error = ENXIO;
19121 			return (NULL);
19122 		}
19123 	}
19124 
19125 	GRAB_CONN_LOCK(q);
19126 	mutex_enter(&ill->ill_lock);
19127 	/* Now see if there is an IPIF with this unit number. */
19128 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19129 		if (ipif->ipif_id == id) {
19130 			if (zoneid != ALL_ZONES &&
19131 			    zoneid != ipif->ipif_zoneid &&
19132 			    ipif->ipif_zoneid != ALL_ZONES) {
19133 				mutex_exit(&ill->ill_lock);
19134 				RELEASE_CONN_LOCK(q);
19135 				ill_refrele(ill);
19136 				if (error != NULL)
19137 					*error = ENXIO;
19138 				return (NULL);
19139 			}
19140 			/*
19141 			 * The block comment at the start of ipif_down
19142 			 * explains the use of the macros used below
19143 			 */
19144 			if (IPIF_CAN_LOOKUP(ipif)) {
19145 				ipif_refhold_locked(ipif);
19146 				mutex_exit(&ill->ill_lock);
19147 				if (!did_alloc)
19148 					*exists = B_TRUE;
19149 				/*
19150 				 * Drop locks before calling ill_refrele
19151 				 * since it can potentially call into
19152 				 * ipif_ill_refrele_tail which can end up
19153 				 * in trying to acquire any lock.
19154 				 */
19155 				RELEASE_CONN_LOCK(q);
19156 				ill_refrele(ill);
19157 				return (ipif);
19158 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19159 				ipsq = ill->ill_phyint->phyint_ipsq;
19160 				mutex_enter(&ipsq->ipsq_lock);
19161 				mutex_exit(&ill->ill_lock);
19162 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19163 				mutex_exit(&ipsq->ipsq_lock);
19164 				RELEASE_CONN_LOCK(q);
19165 				ill_refrele(ill);
19166 				if (error != NULL)
19167 					*error = EINPROGRESS;
19168 				return (NULL);
19169 			}
19170 		}
19171 	}
19172 	RELEASE_CONN_LOCK(q);
19173 
19174 	if (!do_alloc) {
19175 		mutex_exit(&ill->ill_lock);
19176 		ill_refrele(ill);
19177 		if (error != NULL)
19178 			*error = ENXIO;
19179 		return (NULL);
19180 	}
19181 
19182 	/*
19183 	 * If none found, atomically allocate and return a new one.
19184 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19185 	 * to support "receive only" use of lo0:1 etc. as is still done
19186 	 * below as an initial guess.
19187 	 * However, this is now likely to be overriden later in ipif_up_done()
19188 	 * when we know for sure what address has been configured on the
19189 	 * interface, since we might have more than one loopback interface
19190 	 * with a loopback address, e.g. in the case of zones, and all the
19191 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19192 	 */
19193 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19194 		ire_type = IRE_LOOPBACK;
19195 	else
19196 		ire_type = IRE_LOCAL;
19197 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19198 	if (ipif != NULL)
19199 		ipif_refhold_locked(ipif);
19200 	else if (error != NULL)
19201 		*error = ENOMEM;
19202 	mutex_exit(&ill->ill_lock);
19203 	ill_refrele(ill);
19204 	return (ipif);
19205 }
19206 
19207 /*
19208  * This routine is called whenever a new address comes up on an ipif.  If
19209  * we are configured to respond to address mask requests, then we are supposed
19210  * to broadcast an address mask reply at this time.  This routine is also
19211  * called if we are already up, but a netmask change is made.  This is legal
19212  * but might not make the system manager very popular.	(May be called
19213  * as writer.)
19214  */
19215 void
19216 ipif_mask_reply(ipif_t *ipif)
19217 {
19218 	icmph_t	*icmph;
19219 	ipha_t	*ipha;
19220 	mblk_t	*mp;
19221 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19222 
19223 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19224 
19225 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19226 		return;
19227 
19228 	/* ICMP mask reply is IPv4 only */
19229 	ASSERT(!ipif->ipif_isv6);
19230 	/* ICMP mask reply is not for a loopback interface */
19231 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19232 
19233 	mp = allocb(REPLY_LEN, BPRI_HI);
19234 	if (mp == NULL)
19235 		return;
19236 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19237 
19238 	ipha = (ipha_t *)mp->b_rptr;
19239 	bzero(ipha, REPLY_LEN);
19240 	*ipha = icmp_ipha;
19241 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19242 	ipha->ipha_src = ipif->ipif_src_addr;
19243 	ipha->ipha_dst = ipif->ipif_brd_addr;
19244 	ipha->ipha_length = htons(REPLY_LEN);
19245 	ipha->ipha_ident = 0;
19246 
19247 	icmph = (icmph_t *)&ipha[1];
19248 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19249 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19250 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19251 
19252 	put(ipif->ipif_wq, mp);
19253 
19254 #undef	REPLY_LEN
19255 }
19256 
19257 /*
19258  * When the mtu in the ipif changes, we call this routine through ire_walk
19259  * to update all the relevant IREs.
19260  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19261  */
19262 static void
19263 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19264 {
19265 	ipif_t *ipif = (ipif_t *)ipif_arg;
19266 
19267 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19268 		return;
19269 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19270 }
19271 
19272 /*
19273  * When the mtu in the ill changes, we call this routine through ire_walk
19274  * to update all the relevant IREs.
19275  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19276  */
19277 void
19278 ill_mtu_change(ire_t *ire, char *ill_arg)
19279 {
19280 	ill_t	*ill = (ill_t *)ill_arg;
19281 
19282 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19283 		return;
19284 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19285 }
19286 
19287 /*
19288  * Join the ipif specific multicast groups.
19289  * Must be called after a mapping has been set up in the resolver.  (Always
19290  * called as writer.)
19291  */
19292 void
19293 ipif_multicast_up(ipif_t *ipif)
19294 {
19295 	int err, index;
19296 	ill_t *ill;
19297 
19298 	ASSERT(IAM_WRITER_IPIF(ipif));
19299 
19300 	ill = ipif->ipif_ill;
19301 	index = ill->ill_phyint->phyint_ifindex;
19302 
19303 	ip1dbg(("ipif_multicast_up\n"));
19304 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19305 		return;
19306 
19307 	if (ipif->ipif_isv6) {
19308 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19309 			return;
19310 
19311 		/* Join the all hosts multicast address */
19312 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19313 		/*
19314 		 * Passing B_TRUE means we have to join the multicast
19315 		 * membership on this interface even though this is
19316 		 * FAILED. If we join on a different one in the group,
19317 		 * we will not be able to delete the membership later
19318 		 * as we currently don't track where we join when we
19319 		 * join within the kernel unlike applications where
19320 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19321 		 * for more on this.
19322 		 */
19323 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19324 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19325 		if (err != 0) {
19326 			ip0dbg(("ipif_multicast_up: "
19327 			    "all_hosts_mcast failed %d\n",
19328 			    err));
19329 			return;
19330 		}
19331 		/*
19332 		 * Enable multicast for the solicited node multicast address
19333 		 */
19334 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19335 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19336 
19337 			ipv6_multi.s6_addr32[3] |=
19338 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19339 
19340 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19341 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19342 			    NULL);
19343 			if (err != 0) {
19344 				ip0dbg(("ipif_multicast_up: solicited MC"
19345 				    " failed %d\n", err));
19346 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19347 				    ill, ill->ill_phyint->phyint_ifindex,
19348 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19349 				return;
19350 			}
19351 		}
19352 	} else {
19353 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19354 			return;
19355 
19356 		/* Join the all hosts multicast address */
19357 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19358 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19359 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19360 		if (err) {
19361 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19362 			return;
19363 		}
19364 	}
19365 	ipif->ipif_multicast_up = 1;
19366 }
19367 
19368 /*
19369  * Blow away any multicast groups that we joined in ipif_multicast_up().
19370  * (Explicit memberships are blown away in ill_leave_multicast() when the
19371  * ill is brought down.)
19372  */
19373 static void
19374 ipif_multicast_down(ipif_t *ipif)
19375 {
19376 	int err;
19377 
19378 	ASSERT(IAM_WRITER_IPIF(ipif));
19379 
19380 	ip1dbg(("ipif_multicast_down\n"));
19381 	if (!ipif->ipif_multicast_up)
19382 		return;
19383 
19384 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19385 
19386 	if (!ipif->ipif_isv6) {
19387 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19388 		    B_TRUE);
19389 		if (err != 0)
19390 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19391 
19392 		ipif->ipif_multicast_up = 0;
19393 		return;
19394 	}
19395 
19396 	/*
19397 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19398 	 * we should look for ilms on this ill rather than the ones that have
19399 	 * been failed over here.  They are here temporarily. As
19400 	 * ipif_multicast_up has joined on this ill, we should delete only
19401 	 * from this ill.
19402 	 */
19403 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19404 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19405 	    B_TRUE, B_TRUE);
19406 	if (err != 0) {
19407 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19408 		    err));
19409 	}
19410 	/*
19411 	 * Disable multicast for the solicited node multicast address
19412 	 */
19413 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19414 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19415 
19416 		ipv6_multi.s6_addr32[3] |=
19417 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19418 
19419 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19420 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19421 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19422 
19423 		if (err != 0) {
19424 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19425 			    err));
19426 		}
19427 	}
19428 
19429 	ipif->ipif_multicast_up = 0;
19430 }
19431 
19432 /*
19433  * Used when an interface comes up to recreate any extra routes on this
19434  * interface.
19435  */
19436 static ire_t **
19437 ipif_recover_ire(ipif_t *ipif)
19438 {
19439 	mblk_t	*mp;
19440 	ire_t	**ipif_saved_irep;
19441 	ire_t	**irep;
19442 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19443 
19444 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19445 	    ipif->ipif_id));
19446 
19447 	mutex_enter(&ipif->ipif_saved_ire_lock);
19448 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19449 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19450 	if (ipif_saved_irep == NULL) {
19451 		mutex_exit(&ipif->ipif_saved_ire_lock);
19452 		return (NULL);
19453 	}
19454 
19455 	irep = ipif_saved_irep;
19456 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19457 		ire_t		*ire;
19458 		queue_t		*rfq;
19459 		queue_t		*stq;
19460 		ifrt_t		*ifrt;
19461 		uchar_t		*src_addr;
19462 		uchar_t		*gateway_addr;
19463 		ushort_t	type;
19464 
19465 		/*
19466 		 * When the ire was initially created and then added in
19467 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19468 		 * in the case of a traditional interface route, or as one of
19469 		 * the IRE_OFFSUBNET types (with the exception of
19470 		 * IRE_HOST types ire which is created by icmp_redirect() and
19471 		 * which we don't need to save or recover).  In the case where
19472 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19473 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19474 		 * to satisfy software like GateD and Sun Cluster which creates
19475 		 * routes using the the loopback interface's address as a
19476 		 * gateway.
19477 		 *
19478 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19479 		 * ire_create() will be called in the same way here as
19480 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19481 		 * the route looks like a traditional interface route (where
19482 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19483 		 * the saved ifrt->ifrt_type.  This means that in the case where
19484 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19485 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19486 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19487 		 */
19488 		ifrt = (ifrt_t *)mp->b_rptr;
19489 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19490 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19491 			rfq = NULL;
19492 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19493 			    ? ipif->ipif_rq : ipif->ipif_wq;
19494 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19495 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19496 			    : (uint8_t *)&ipif->ipif_src_addr;
19497 			gateway_addr = NULL;
19498 			type = ipif->ipif_net_type;
19499 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19500 			/* Recover multiroute broadcast IRE. */
19501 			rfq = ipif->ipif_rq;
19502 			stq = ipif->ipif_wq;
19503 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19504 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19505 			    : (uint8_t *)&ipif->ipif_src_addr;
19506 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19507 			type = ifrt->ifrt_type;
19508 		} else {
19509 			rfq = NULL;
19510 			stq = NULL;
19511 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19512 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19513 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19514 			type = ifrt->ifrt_type;
19515 		}
19516 
19517 		/*
19518 		 * Create a copy of the IRE with the saved address and netmask.
19519 		 */
19520 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19521 		    "0x%x/0x%x\n",
19522 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19523 		    ntohl(ifrt->ifrt_addr),
19524 		    ntohl(ifrt->ifrt_mask)));
19525 		ire = ire_create(
19526 		    (uint8_t *)&ifrt->ifrt_addr,
19527 		    (uint8_t *)&ifrt->ifrt_mask,
19528 		    src_addr,
19529 		    gateway_addr,
19530 		    &ifrt->ifrt_max_frag,
19531 		    NULL,
19532 		    rfq,
19533 		    stq,
19534 		    type,
19535 		    ipif,
19536 		    0,
19537 		    0,
19538 		    0,
19539 		    ifrt->ifrt_flags,
19540 		    &ifrt->ifrt_iulp_info,
19541 		    NULL,
19542 		    NULL,
19543 		    ipst);
19544 
19545 		if (ire == NULL) {
19546 			mutex_exit(&ipif->ipif_saved_ire_lock);
19547 			kmem_free(ipif_saved_irep,
19548 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19549 			return (NULL);
19550 		}
19551 
19552 		/*
19553 		 * Some software (for example, GateD and Sun Cluster) attempts
19554 		 * to create (what amount to) IRE_PREFIX routes with the
19555 		 * loopback address as the gateway.  This is primarily done to
19556 		 * set up prefixes with the RTF_REJECT flag set (for example,
19557 		 * when generating aggregate routes.)
19558 		 *
19559 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19560 		 * IRE_LOOPBACK, then we map the request into a
19561 		 * IRE_IF_NORESOLVER.
19562 		 */
19563 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19564 			ire->ire_type = IRE_IF_NORESOLVER;
19565 		/*
19566 		 * ire held by ire_add, will be refreled' towards the
19567 		 * the end of ipif_up_done
19568 		 */
19569 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19570 		*irep = ire;
19571 		irep++;
19572 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19573 	}
19574 	mutex_exit(&ipif->ipif_saved_ire_lock);
19575 	return (ipif_saved_irep);
19576 }
19577 
19578 /*
19579  * Used to set the netmask and broadcast address to default values when the
19580  * interface is brought up.  (Always called as writer.)
19581  */
19582 static void
19583 ipif_set_default(ipif_t *ipif)
19584 {
19585 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19586 
19587 	if (!ipif->ipif_isv6) {
19588 		/*
19589 		 * Interface holds an IPv4 address. Default
19590 		 * mask is the natural netmask.
19591 		 */
19592 		if (!ipif->ipif_net_mask) {
19593 			ipaddr_t	v4mask;
19594 
19595 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19596 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19597 		}
19598 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19599 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19600 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19601 		} else {
19602 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19603 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19604 		}
19605 		/*
19606 		 * NOTE: SunOS 4.X does this even if the broadcast address
19607 		 * has been already set thus we do the same here.
19608 		 */
19609 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19610 			ipaddr_t	v4addr;
19611 
19612 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19613 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19614 		}
19615 	} else {
19616 		/*
19617 		 * Interface holds an IPv6-only address.  Default
19618 		 * mask is all-ones.
19619 		 */
19620 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19621 			ipif->ipif_v6net_mask = ipv6_all_ones;
19622 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19623 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19624 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19625 		} else {
19626 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19627 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19628 		}
19629 	}
19630 }
19631 
19632 /*
19633  * Return 0 if this address can be used as local address without causing
19634  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19635  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19636  * Special checks are needed to allow the same IPv6 link-local address
19637  * on different ills.
19638  * TODO: allowing the same site-local address on different ill's.
19639  */
19640 int
19641 ip_addr_availability_check(ipif_t *new_ipif)
19642 {
19643 	in6_addr_t our_v6addr;
19644 	ill_t *ill;
19645 	ipif_t *ipif;
19646 	ill_walk_context_t ctx;
19647 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19648 
19649 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19650 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19651 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19652 
19653 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19654 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19655 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19656 		return (0);
19657 
19658 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19659 
19660 	if (new_ipif->ipif_isv6)
19661 		ill = ILL_START_WALK_V6(&ctx, ipst);
19662 	else
19663 		ill = ILL_START_WALK_V4(&ctx, ipst);
19664 
19665 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19666 		for (ipif = ill->ill_ipif; ipif != NULL;
19667 		    ipif = ipif->ipif_next) {
19668 			if ((ipif == new_ipif) ||
19669 			    !(ipif->ipif_flags & IPIF_UP) ||
19670 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19671 				continue;
19672 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19673 			    &our_v6addr)) {
19674 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19675 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19676 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19677 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19678 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19679 				    new_ipif->ipif_ill != ill)
19680 					continue;
19681 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19682 				    new_ipif->ipif_ill != ill)
19683 					continue;
19684 				else if (new_ipif->ipif_zoneid !=
19685 				    ipif->ipif_zoneid &&
19686 				    ipif->ipif_zoneid != ALL_ZONES &&
19687 				    IS_LOOPBACK(ill))
19688 					continue;
19689 				else if (new_ipif->ipif_ill == ill)
19690 					return (EADDRINUSE);
19691 				else
19692 					return (EADDRNOTAVAIL);
19693 			}
19694 		}
19695 	}
19696 
19697 	return (0);
19698 }
19699 
19700 /*
19701  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19702  * IREs for the ipif.
19703  * When the routine returns EINPROGRESS then mp has been consumed and
19704  * the ioctl will be acked from ip_rput_dlpi.
19705  */
19706 static int
19707 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19708 {
19709 	ill_t	*ill = ipif->ipif_ill;
19710 	boolean_t isv6 = ipif->ipif_isv6;
19711 	int	err = 0;
19712 	boolean_t success;
19713 
19714 	ASSERT(IAM_WRITER_IPIF(ipif));
19715 
19716 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19717 
19718 	/* Shouldn't get here if it is already up. */
19719 	if (ipif->ipif_flags & IPIF_UP)
19720 		return (EALREADY);
19721 
19722 	/* Skip arp/ndp for any loopback interface. */
19723 	if (ill->ill_wq != NULL) {
19724 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19725 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19726 
19727 		if (!ill->ill_dl_up) {
19728 			/*
19729 			 * ill_dl_up is not yet set. i.e. we are yet to
19730 			 * DL_BIND with the driver and this is the first
19731 			 * logical interface on the ill to become "up".
19732 			 * Tell the driver to get going (via DL_BIND_REQ).
19733 			 * Note that changing "significant" IFF_ flags
19734 			 * address/netmask etc cause a down/up dance, but
19735 			 * does not cause an unbind (DL_UNBIND) with the driver
19736 			 */
19737 			return (ill_dl_up(ill, ipif, mp, q));
19738 		}
19739 
19740 		/*
19741 		 * ipif_resolver_up may end up sending an
19742 		 * AR_INTERFACE_UP message to ARP, which would, in
19743 		 * turn send a DLPI message to the driver. ioctls are
19744 		 * serialized and so we cannot send more than one
19745 		 * interface up message at a time. If ipif_resolver_up
19746 		 * does send an interface up message to ARP, we get
19747 		 * EINPROGRESS and we will complete in ip_arp_done.
19748 		 */
19749 
19750 		ASSERT(connp != NULL || !CONN_Q(q));
19751 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19752 		if (connp != NULL)
19753 			mutex_enter(&connp->conn_lock);
19754 		mutex_enter(&ill->ill_lock);
19755 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19756 		mutex_exit(&ill->ill_lock);
19757 		if (connp != NULL)
19758 			mutex_exit(&connp->conn_lock);
19759 		if (!success)
19760 			return (EINTR);
19761 
19762 		/*
19763 		 * Crank up IPv6 neighbor discovery
19764 		 * Unlike ARP, this should complete when
19765 		 * ipif_ndp_up returns. However, for
19766 		 * ILLF_XRESOLV interfaces we also send a
19767 		 * AR_INTERFACE_UP to the external resolver.
19768 		 * That ioctl will complete in ip_rput.
19769 		 */
19770 		if (isv6) {
19771 			err = ipif_ndp_up(ipif);
19772 			if (err != 0) {
19773 				if (err != EINPROGRESS)
19774 					mp = ipsq_pending_mp_get(ipsq, &connp);
19775 				return (err);
19776 			}
19777 		}
19778 		/* Now, ARP */
19779 		err = ipif_resolver_up(ipif, Res_act_initial);
19780 		if (err == EINPROGRESS) {
19781 			/* We will complete it in ip_arp_done */
19782 			return (err);
19783 		}
19784 		mp = ipsq_pending_mp_get(ipsq, &connp);
19785 		ASSERT(mp != NULL);
19786 		if (err != 0)
19787 			return (err);
19788 	} else {
19789 		/*
19790 		 * Interfaces without underlying hardware don't do duplicate
19791 		 * address detection.
19792 		 */
19793 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19794 		ipif->ipif_addr_ready = 1;
19795 	}
19796 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19797 }
19798 
19799 /*
19800  * Perform a bind for the physical device.
19801  * When the routine returns EINPROGRESS then mp has been consumed and
19802  * the ioctl will be acked from ip_rput_dlpi.
19803  * Allocate an unbind message and save it until ipif_down.
19804  */
19805 static int
19806 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19807 {
19808 	areq_t	*areq;
19809 	mblk_t	*areq_mp = NULL;
19810 	mblk_t	*bind_mp = NULL;
19811 	mblk_t	*unbind_mp = NULL;
19812 	conn_t	*connp;
19813 	boolean_t success;
19814 	uint16_t sap_addr;
19815 
19816 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19817 	ASSERT(IAM_WRITER_ILL(ill));
19818 	ASSERT(mp != NULL);
19819 
19820 	/* Create a resolver cookie for ARP */
19821 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19822 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19823 		if (areq_mp == NULL)
19824 			return (ENOMEM);
19825 
19826 		freemsg(ill->ill_resolver_mp);
19827 		ill->ill_resolver_mp = areq_mp;
19828 		areq = (areq_t *)areq_mp->b_rptr;
19829 		sap_addr = ill->ill_sap;
19830 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19831 	}
19832 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19833 	    DL_BIND_REQ);
19834 	if (bind_mp == NULL)
19835 		goto bad;
19836 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19837 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19838 
19839 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19840 	if (unbind_mp == NULL)
19841 		goto bad;
19842 
19843 	/*
19844 	 * Record state needed to complete this operation when the
19845 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19846 	 */
19847 	ASSERT(WR(q)->q_next == NULL);
19848 	connp = Q_TO_CONN(q);
19849 
19850 	mutex_enter(&connp->conn_lock);
19851 	mutex_enter(&ipif->ipif_ill->ill_lock);
19852 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19853 	mutex_exit(&ipif->ipif_ill->ill_lock);
19854 	mutex_exit(&connp->conn_lock);
19855 	if (!success)
19856 		goto bad;
19857 
19858 	/*
19859 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19860 	 * the interface goes down.
19861 	 */
19862 	ASSERT(ill->ill_unbind_mp == NULL);
19863 	ill->ill_unbind_mp = unbind_mp;
19864 
19865 	ill_dlpi_send(ill, bind_mp);
19866 	/* Send down link-layer capabilities probe if not already done. */
19867 	ill_capability_probe(ill);
19868 
19869 	/*
19870 	 * Sysid used to rely on the fact that netboots set domainname
19871 	 * and the like. Now that miniroot boots aren't strictly netboots
19872 	 * and miniroot network configuration is driven from userland
19873 	 * these things still need to be set. This situation can be detected
19874 	 * by comparing the interface being configured here to the one
19875 	 * dhcifname was set to reference by the boot loader. Once sysid is
19876 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19877 	 */
19878 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
19879 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
19880 	    (strlen(srpc_domain) == 0)) {
19881 		if (dhcpinit() != 0)
19882 			cmn_err(CE_WARN, "no cached dhcp response");
19883 	}
19884 
19885 	/*
19886 	 * This operation will complete in ip_rput_dlpi with either
19887 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19888 	 */
19889 	return (EINPROGRESS);
19890 bad:
19891 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19892 	/*
19893 	 * We don't have to check for possible removal from illgrp
19894 	 * as we have not yet inserted in illgrp. For groups
19895 	 * without names, this ipif is still not UP and hence
19896 	 * this could not have possibly had any influence in forming
19897 	 * groups.
19898 	 */
19899 
19900 	freemsg(bind_mp);
19901 	freemsg(unbind_mp);
19902 	return (ENOMEM);
19903 }
19904 
19905 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19906 
19907 /*
19908  * DLPI and ARP is up.
19909  * Create all the IREs associated with an interface bring up multicast.
19910  * Set the interface flag and finish other initialization
19911  * that potentially had to be differed to after DL_BIND_ACK.
19912  */
19913 int
19914 ipif_up_done(ipif_t *ipif)
19915 {
19916 	ire_t	*ire_array[20];
19917 	ire_t	**irep = ire_array;
19918 	ire_t	**irep1;
19919 	ipaddr_t net_mask = 0;
19920 	ipaddr_t subnet_mask, route_mask;
19921 	ill_t	*ill = ipif->ipif_ill;
19922 	queue_t	*stq;
19923 	ipif_t	 *src_ipif;
19924 	ipif_t   *tmp_ipif;
19925 	boolean_t	flush_ire_cache = B_TRUE;
19926 	int	err = 0;
19927 	phyint_t *phyi;
19928 	ire_t	**ipif_saved_irep = NULL;
19929 	int ipif_saved_ire_cnt;
19930 	int	cnt;
19931 	boolean_t	src_ipif_held = B_FALSE;
19932 	boolean_t	ire_added = B_FALSE;
19933 	boolean_t	loopback = B_FALSE;
19934 	ip_stack_t	*ipst = ill->ill_ipst;
19935 
19936 	ip1dbg(("ipif_up_done(%s:%u)\n",
19937 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
19938 	/* Check if this is a loopback interface */
19939 	if (ipif->ipif_ill->ill_wq == NULL)
19940 		loopback = B_TRUE;
19941 
19942 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19943 	/*
19944 	 * If all other interfaces for this ill are down or DEPRECATED,
19945 	 * or otherwise unsuitable for source address selection, remove
19946 	 * any IRE_CACHE entries for this ill to make sure source
19947 	 * address selection gets to take this new ipif into account.
19948 	 * No need to hold ill_lock while traversing the ipif list since
19949 	 * we are writer
19950 	 */
19951 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
19952 	    tmp_ipif = tmp_ipif->ipif_next) {
19953 		if (((tmp_ipif->ipif_flags &
19954 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
19955 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
19956 		    (tmp_ipif == ipif))
19957 			continue;
19958 		/* first useable pre-existing interface */
19959 		flush_ire_cache = B_FALSE;
19960 		break;
19961 	}
19962 	if (flush_ire_cache)
19963 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
19964 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
19965 
19966 	/*
19967 	 * Figure out which way the send-to queue should go.  Only
19968 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
19969 	 * should show up here.
19970 	 */
19971 	switch (ill->ill_net_type) {
19972 	case IRE_IF_RESOLVER:
19973 		stq = ill->ill_rq;
19974 		break;
19975 	case IRE_IF_NORESOLVER:
19976 	case IRE_LOOPBACK:
19977 		stq = ill->ill_wq;
19978 		break;
19979 	default:
19980 		return (EINVAL);
19981 	}
19982 
19983 	if (IS_LOOPBACK(ill)) {
19984 		/*
19985 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
19986 		 * ipif_lookup_on_name(), but in the case of zones we can have
19987 		 * several loopback addresses on lo0. So all the interfaces with
19988 		 * loopback addresses need to be marked IRE_LOOPBACK.
19989 		 */
19990 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
19991 		    htonl(INADDR_LOOPBACK))
19992 			ipif->ipif_ire_type = IRE_LOOPBACK;
19993 		else
19994 			ipif->ipif_ire_type = IRE_LOCAL;
19995 	}
19996 
19997 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
19998 		/*
19999 		 * Can't use our source address. Select a different
20000 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20001 		 */
20002 		src_ipif = ipif_select_source(ipif->ipif_ill,
20003 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20004 		if (src_ipif == NULL)
20005 			src_ipif = ipif;	/* Last resort */
20006 		else
20007 			src_ipif_held = B_TRUE;
20008 	} else {
20009 		src_ipif = ipif;
20010 	}
20011 
20012 	/* Create all the IREs associated with this interface */
20013 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20014 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20015 
20016 		/*
20017 		 * If we're on a labeled system then make sure that zone-
20018 		 * private addresses have proper remote host database entries.
20019 		 */
20020 		if (is_system_labeled() &&
20021 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20022 		    !tsol_check_interface_address(ipif))
20023 			return (EINVAL);
20024 
20025 		/* Register the source address for __sin6_src_id */
20026 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20027 		    ipif->ipif_zoneid, ipst);
20028 		if (err != 0) {
20029 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20030 			return (err);
20031 		}
20032 
20033 		/* If the interface address is set, create the local IRE. */
20034 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20035 		    (void *)ipif,
20036 		    ipif->ipif_ire_type,
20037 		    ntohl(ipif->ipif_lcl_addr)));
20038 		*irep++ = ire_create(
20039 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20040 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20041 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20042 		    NULL,				/* no gateway */
20043 		    &ip_loopback_mtuplus,		/* max frag size */
20044 		    NULL,
20045 		    ipif->ipif_rq,			/* recv-from queue */
20046 		    NULL,				/* no send-to queue */
20047 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20048 		    ipif,
20049 		    0,
20050 		    0,
20051 		    0,
20052 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20053 		    RTF_PRIVATE : 0,
20054 		    &ire_uinfo_null,
20055 		    NULL,
20056 		    NULL,
20057 		    ipst);
20058 	} else {
20059 		ip1dbg((
20060 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20061 		    ipif->ipif_ire_type,
20062 		    ntohl(ipif->ipif_lcl_addr),
20063 		    (uint_t)ipif->ipif_flags));
20064 	}
20065 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20066 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20067 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20068 	} else {
20069 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20070 	}
20071 
20072 	subnet_mask = ipif->ipif_net_mask;
20073 
20074 	/*
20075 	 * If mask was not specified, use natural netmask of
20076 	 * interface address. Also, store this mask back into the
20077 	 * ipif struct.
20078 	 */
20079 	if (subnet_mask == 0) {
20080 		subnet_mask = net_mask;
20081 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20082 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20083 		    ipif->ipif_v6subnet);
20084 	}
20085 
20086 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20087 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20088 	    ipif->ipif_subnet != INADDR_ANY) {
20089 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20090 
20091 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20092 			route_mask = IP_HOST_MASK;
20093 		} else {
20094 			route_mask = subnet_mask;
20095 		}
20096 
20097 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20098 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20099 		    (void *)ipif, (void *)ill,
20100 		    ill->ill_net_type,
20101 		    ntohl(ipif->ipif_subnet)));
20102 		*irep++ = ire_create(
20103 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20104 		    (uchar_t *)&route_mask,		/* mask */
20105 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20106 		    NULL,				/* no gateway */
20107 		    &ipif->ipif_mtu,			/* max frag */
20108 		    NULL,
20109 		    NULL,				/* no recv queue */
20110 		    stq,				/* send-to queue */
20111 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20112 		    ipif,
20113 		    0,
20114 		    0,
20115 		    0,
20116 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20117 		    &ire_uinfo_null,
20118 		    NULL,
20119 		    NULL,
20120 		    ipst);
20121 	}
20122 
20123 	/*
20124 	 * Create any necessary broadcast IREs.
20125 	 */
20126 	if (ipif->ipif_flags & IPIF_BROADCAST)
20127 		irep = ipif_create_bcast_ires(ipif, irep);
20128 
20129 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20130 
20131 	/* If an earlier ire_create failed, get out now */
20132 	for (irep1 = irep; irep1 > ire_array; ) {
20133 		irep1--;
20134 		if (*irep1 == NULL) {
20135 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20136 			err = ENOMEM;
20137 			goto bad;
20138 		}
20139 	}
20140 
20141 	/*
20142 	 * Need to atomically check for ip_addr_availablity_check
20143 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20144 	 * from group also.The ill_g_lock is grabbed as reader
20145 	 * just to make sure no new ills or new ipifs are being added
20146 	 * to the system while we are checking the uniqueness of addresses.
20147 	 */
20148 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20149 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20150 	/* Mark it up, and increment counters. */
20151 	ipif->ipif_flags |= IPIF_UP;
20152 	ill->ill_ipif_up_count++;
20153 	err = ip_addr_availability_check(ipif);
20154 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20155 	rw_exit(&ipst->ips_ill_g_lock);
20156 
20157 	if (err != 0) {
20158 		/*
20159 		 * Our address may already be up on the same ill. In this case,
20160 		 * the ARP entry for our ipif replaced the one for the other
20161 		 * ipif. So we don't want to delete it (otherwise the other ipif
20162 		 * would be unable to send packets).
20163 		 * ip_addr_availability_check() identifies this case for us and
20164 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20165 		 * which is the expected error code.
20166 		 */
20167 		if (err == EADDRINUSE) {
20168 			freemsg(ipif->ipif_arp_del_mp);
20169 			ipif->ipif_arp_del_mp = NULL;
20170 			err = EADDRNOTAVAIL;
20171 		}
20172 		ill->ill_ipif_up_count--;
20173 		ipif->ipif_flags &= ~IPIF_UP;
20174 		goto bad;
20175 	}
20176 
20177 	/*
20178 	 * Add in all newly created IREs.  ire_create_bcast() has
20179 	 * already checked for duplicates of the IRE_BROADCAST type.
20180 	 * We want to add before we call ifgrp_insert which wants
20181 	 * to know whether IRE_IF_RESOLVER exists or not.
20182 	 *
20183 	 * NOTE : We refrele the ire though we may branch to "bad"
20184 	 *	  later on where we do ire_delete. This is okay
20185 	 *	  because nobody can delete it as we are running
20186 	 *	  exclusively.
20187 	 */
20188 	for (irep1 = irep; irep1 > ire_array; ) {
20189 		irep1--;
20190 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20191 		/*
20192 		 * refheld by ire_add. refele towards the end of the func
20193 		 */
20194 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20195 	}
20196 	ire_added = B_TRUE;
20197 	/*
20198 	 * Form groups if possible.
20199 	 *
20200 	 * If we are supposed to be in a ill_group with a name, insert it
20201 	 * now as we know that at least one ipif is UP. Otherwise form
20202 	 * nameless groups.
20203 	 *
20204 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20205 	 * this ipif into the appropriate interface group, or create a
20206 	 * new one. If this is already in a nameless group, we try to form
20207 	 * a bigger group looking at other ills potentially sharing this
20208 	 * ipif's prefix.
20209 	 */
20210 	phyi = ill->ill_phyint;
20211 	if (phyi->phyint_groupname_len != 0) {
20212 		ASSERT(phyi->phyint_groupname != NULL);
20213 		if (ill->ill_ipif_up_count == 1) {
20214 			ASSERT(ill->ill_group == NULL);
20215 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20216 			    phyi->phyint_groupname, NULL, B_TRUE);
20217 			if (err != 0) {
20218 				ip1dbg(("ipif_up_done: illgrp allocation "
20219 				    "failed, error %d\n", err));
20220 				goto bad;
20221 			}
20222 		}
20223 		ASSERT(ill->ill_group != NULL);
20224 	}
20225 
20226 	/*
20227 	 * When this is part of group, we need to make sure that
20228 	 * any broadcast ires created because of this ipif coming
20229 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20230 	 * so that we don't receive duplicate broadcast packets.
20231 	 */
20232 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20233 		ipif_renominate_bcast(ipif);
20234 
20235 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20236 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20237 	ipif_saved_irep = ipif_recover_ire(ipif);
20238 
20239 	if (!loopback) {
20240 		/*
20241 		 * If the broadcast address has been set, make sure it makes
20242 		 * sense based on the interface address.
20243 		 * Only match on ill since we are sharing broadcast addresses.
20244 		 */
20245 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20246 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20247 			ire_t	*ire;
20248 
20249 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20250 			    IRE_BROADCAST, ipif, ALL_ZONES,
20251 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20252 
20253 			if (ire == NULL) {
20254 				/*
20255 				 * If there isn't a matching broadcast IRE,
20256 				 * revert to the default for this netmask.
20257 				 */
20258 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20259 				mutex_enter(&ipif->ipif_ill->ill_lock);
20260 				ipif_set_default(ipif);
20261 				mutex_exit(&ipif->ipif_ill->ill_lock);
20262 			} else {
20263 				ire_refrele(ire);
20264 			}
20265 		}
20266 
20267 	}
20268 
20269 	/* This is the first interface on this ill */
20270 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20271 		/*
20272 		 * Need to recover all multicast memberships in the driver.
20273 		 * This had to be deferred until we had attached.
20274 		 */
20275 		ill_recover_multicast(ill);
20276 	}
20277 	/* Join the allhosts multicast address */
20278 	ipif_multicast_up(ipif);
20279 
20280 	if (!loopback) {
20281 		/*
20282 		 * See whether anybody else would benefit from the
20283 		 * new ipif that we added. We call this always rather
20284 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20285 		 * ipif is for the benefit of illgrp_insert (done above)
20286 		 * which does not do source address selection as it does
20287 		 * not want to re-create interface routes that we are
20288 		 * having reference to it here.
20289 		 */
20290 		ill_update_source_selection(ill);
20291 	}
20292 
20293 	for (irep1 = irep; irep1 > ire_array; ) {
20294 		irep1--;
20295 		if (*irep1 != NULL) {
20296 			/* was held in ire_add */
20297 			ire_refrele(*irep1);
20298 		}
20299 	}
20300 
20301 	cnt = ipif_saved_ire_cnt;
20302 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20303 		if (*irep1 != NULL) {
20304 			/* was held in ire_add */
20305 			ire_refrele(*irep1);
20306 		}
20307 	}
20308 
20309 	if (!loopback && ipif->ipif_addr_ready) {
20310 		/* Broadcast an address mask reply. */
20311 		ipif_mask_reply(ipif);
20312 	}
20313 	if (ipif_saved_irep != NULL) {
20314 		kmem_free(ipif_saved_irep,
20315 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20316 	}
20317 	if (src_ipif_held)
20318 		ipif_refrele(src_ipif);
20319 
20320 	/*
20321 	 * This had to be deferred until we had bound.  Tell routing sockets and
20322 	 * others that this interface is up if it looks like the address has
20323 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20324 	 * duplicate address detection to do its thing.
20325 	 */
20326 	if (ipif->ipif_addr_ready) {
20327 		ip_rts_ifmsg(ipif);
20328 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20329 		/* Let SCTP update the status for this ipif */
20330 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20331 	}
20332 	return (0);
20333 
20334 bad:
20335 	ip1dbg(("ipif_up_done: FAILED \n"));
20336 	/*
20337 	 * We don't have to bother removing from ill groups because
20338 	 *
20339 	 * 1) For groups with names, we insert only when the first ipif
20340 	 *    comes up. In that case if it fails, it will not be in any
20341 	 *    group. So, we need not try to remove for that case.
20342 	 *
20343 	 * 2) For groups without names, either we tried to insert ipif_ill
20344 	 *    in a group as singleton or found some other group to become
20345 	 *    a bigger group. For the former, if it fails we don't have
20346 	 *    anything to do as ipif_ill is not in the group and for the
20347 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20348 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20349 	 */
20350 	while (irep > ire_array) {
20351 		irep--;
20352 		if (*irep != NULL) {
20353 			ire_delete(*irep);
20354 			if (ire_added)
20355 				ire_refrele(*irep);
20356 		}
20357 	}
20358 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20359 
20360 	if (ipif_saved_irep != NULL) {
20361 		kmem_free(ipif_saved_irep,
20362 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20363 	}
20364 	if (src_ipif_held)
20365 		ipif_refrele(src_ipif);
20366 
20367 	ipif_arp_down(ipif);
20368 	return (err);
20369 }
20370 
20371 /*
20372  * Turn off the ARP with the ILLF_NOARP flag.
20373  */
20374 static int
20375 ill_arp_off(ill_t *ill)
20376 {
20377 	mblk_t	*arp_off_mp = NULL;
20378 	mblk_t	*arp_on_mp = NULL;
20379 
20380 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20381 
20382 	ASSERT(IAM_WRITER_ILL(ill));
20383 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20384 
20385 	/*
20386 	 * If the on message is still around we've already done
20387 	 * an arp_off without doing an arp_on thus there is no
20388 	 * work needed.
20389 	 */
20390 	if (ill->ill_arp_on_mp != NULL)
20391 		return (0);
20392 
20393 	/*
20394 	 * Allocate an ARP on message (to be saved) and an ARP off message
20395 	 */
20396 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20397 	if (!arp_off_mp)
20398 		return (ENOMEM);
20399 
20400 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20401 	if (!arp_on_mp)
20402 		goto failed;
20403 
20404 	ASSERT(ill->ill_arp_on_mp == NULL);
20405 	ill->ill_arp_on_mp = arp_on_mp;
20406 
20407 	/* Send an AR_INTERFACE_OFF request */
20408 	putnext(ill->ill_rq, arp_off_mp);
20409 	return (0);
20410 failed:
20411 
20412 	if (arp_off_mp)
20413 		freemsg(arp_off_mp);
20414 	return (ENOMEM);
20415 }
20416 
20417 /*
20418  * Turn on ARP by turning off the ILLF_NOARP flag.
20419  */
20420 static int
20421 ill_arp_on(ill_t *ill)
20422 {
20423 	mblk_t	*mp;
20424 
20425 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20426 
20427 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20428 
20429 	ASSERT(IAM_WRITER_ILL(ill));
20430 	/*
20431 	 * Send an AR_INTERFACE_ON request if we have already done
20432 	 * an arp_off (which allocated the message).
20433 	 */
20434 	if (ill->ill_arp_on_mp != NULL) {
20435 		mp = ill->ill_arp_on_mp;
20436 		ill->ill_arp_on_mp = NULL;
20437 		putnext(ill->ill_rq, mp);
20438 	}
20439 	return (0);
20440 }
20441 
20442 /*
20443  * Called after either deleting ill from the group or when setting
20444  * FAILED or STANDBY on the interface.
20445  */
20446 static void
20447 illgrp_reset_schednext(ill_t *ill)
20448 {
20449 	ill_group_t *illgrp;
20450 	ill_t *save_ill;
20451 
20452 	ASSERT(IAM_WRITER_ILL(ill));
20453 	/*
20454 	 * When called from illgrp_delete, ill_group will be non-NULL.
20455 	 * But when called from ip_sioctl_flags, it could be NULL if
20456 	 * somebody is setting FAILED/INACTIVE on some interface which
20457 	 * is not part of a group.
20458 	 */
20459 	illgrp = ill->ill_group;
20460 	if (illgrp == NULL)
20461 		return;
20462 	if (illgrp->illgrp_ill_schednext != ill)
20463 		return;
20464 
20465 	illgrp->illgrp_ill_schednext = NULL;
20466 	save_ill = ill;
20467 	/*
20468 	 * Choose a good ill to be the next one for
20469 	 * outbound traffic. As the flags FAILED/STANDBY is
20470 	 * not yet marked when called from ip_sioctl_flags,
20471 	 * we check for ill separately.
20472 	 */
20473 	for (ill = illgrp->illgrp_ill; ill != NULL;
20474 	    ill = ill->ill_group_next) {
20475 		if ((ill != save_ill) &&
20476 		    !(ill->ill_phyint->phyint_flags &
20477 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20478 			illgrp->illgrp_ill_schednext = ill;
20479 			return;
20480 		}
20481 	}
20482 }
20483 
20484 /*
20485  * Given an ill, find the next ill in the group to be scheduled.
20486  * (This should be called by ip_newroute() before ire_create().)
20487  * The passed in ill may be pulled out of the group, after we have picked
20488  * up a different outgoing ill from the same group. However ire add will
20489  * atomically check this.
20490  */
20491 ill_t *
20492 illgrp_scheduler(ill_t *ill)
20493 {
20494 	ill_t *retill;
20495 	ill_group_t *illgrp;
20496 	int illcnt;
20497 	int i;
20498 	uint64_t flags;
20499 	ip_stack_t	*ipst = ill->ill_ipst;
20500 
20501 	/*
20502 	 * We don't use a lock to check for the ill_group. If this ill
20503 	 * is currently being inserted we may end up just returning this
20504 	 * ill itself. That is ok.
20505 	 */
20506 	if (ill->ill_group == NULL) {
20507 		ill_refhold(ill);
20508 		return (ill);
20509 	}
20510 
20511 	/*
20512 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20513 	 * a set of stable ills. No ill can be added or deleted or change
20514 	 * group while we hold the reader lock.
20515 	 */
20516 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20517 	if ((illgrp = ill->ill_group) == NULL) {
20518 		rw_exit(&ipst->ips_ill_g_lock);
20519 		ill_refhold(ill);
20520 		return (ill);
20521 	}
20522 
20523 	illcnt = illgrp->illgrp_ill_count;
20524 	mutex_enter(&illgrp->illgrp_lock);
20525 	retill = illgrp->illgrp_ill_schednext;
20526 
20527 	if (retill == NULL)
20528 		retill = illgrp->illgrp_ill;
20529 
20530 	/*
20531 	 * We do a circular search beginning at illgrp_ill_schednext
20532 	 * or illgrp_ill. We don't check the flags against the ill lock
20533 	 * since it can change anytime. The ire creation will be atomic
20534 	 * and will fail if the ill is FAILED or OFFLINE.
20535 	 */
20536 	for (i = 0; i < illcnt; i++) {
20537 		flags = retill->ill_phyint->phyint_flags;
20538 
20539 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20540 		    ILL_CAN_LOOKUP(retill)) {
20541 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20542 			ill_refhold(retill);
20543 			break;
20544 		}
20545 		retill = retill->ill_group_next;
20546 		if (retill == NULL)
20547 			retill = illgrp->illgrp_ill;
20548 	}
20549 	mutex_exit(&illgrp->illgrp_lock);
20550 	rw_exit(&ipst->ips_ill_g_lock);
20551 
20552 	return (i == illcnt ? NULL : retill);
20553 }
20554 
20555 /*
20556  * Checks for availbility of a usable source address (if there is one) when the
20557  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20558  * this selection is done regardless of the destination.
20559  */
20560 boolean_t
20561 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20562 {
20563 	uint_t	ifindex;
20564 	ipif_t	*ipif = NULL;
20565 	ill_t	*uill;
20566 	boolean_t isv6;
20567 	ip_stack_t	*ipst = ill->ill_ipst;
20568 
20569 	ASSERT(ill != NULL);
20570 
20571 	isv6 = ill->ill_isv6;
20572 	ifindex = ill->ill_usesrc_ifindex;
20573 	if (ifindex != 0) {
20574 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20575 		    NULL, ipst);
20576 		if (uill == NULL)
20577 			return (NULL);
20578 		mutex_enter(&uill->ill_lock);
20579 		for (ipif = uill->ill_ipif; ipif != NULL;
20580 		    ipif = ipif->ipif_next) {
20581 			if (!IPIF_CAN_LOOKUP(ipif))
20582 				continue;
20583 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20584 				continue;
20585 			if (!(ipif->ipif_flags & IPIF_UP))
20586 				continue;
20587 			if (ipif->ipif_zoneid != zoneid)
20588 				continue;
20589 			if ((isv6 &&
20590 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20591 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20592 				continue;
20593 			mutex_exit(&uill->ill_lock);
20594 			ill_refrele(uill);
20595 			return (B_TRUE);
20596 		}
20597 		mutex_exit(&uill->ill_lock);
20598 		ill_refrele(uill);
20599 	}
20600 	return (B_FALSE);
20601 }
20602 
20603 /*
20604  * Determine the best source address given a destination address and an ill.
20605  * Prefers non-deprecated over deprecated but will return a deprecated
20606  * address if there is no other choice. If there is a usable source address
20607  * on the interface pointed to by ill_usesrc_ifindex then that is given
20608  * first preference.
20609  *
20610  * Returns NULL if there is no suitable source address for the ill.
20611  * This only occurs when there is no valid source address for the ill.
20612  */
20613 ipif_t *
20614 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20615 {
20616 	ipif_t *ipif;
20617 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20618 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20619 	int index = 0;
20620 	boolean_t wrapped = B_FALSE;
20621 	boolean_t same_subnet_only = B_FALSE;
20622 	boolean_t ipif_same_found, ipif_other_found;
20623 	boolean_t specific_found;
20624 	ill_t	*till, *usill = NULL;
20625 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20626 	ip_stack_t	*ipst = ill->ill_ipst;
20627 
20628 	if (ill->ill_usesrc_ifindex != 0) {
20629 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20630 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20631 		if (usill != NULL)
20632 			ill = usill;	/* Select source from usesrc ILL */
20633 		else
20634 			return (NULL);
20635 	}
20636 
20637 	/*
20638 	 * If we're dealing with an unlabeled destination on a labeled system,
20639 	 * make sure that we ignore source addresses that are incompatible with
20640 	 * the destination's default label.  That destination's default label
20641 	 * must dominate the minimum label on the source address.
20642 	 */
20643 	dst_rhtp = NULL;
20644 	if (is_system_labeled()) {
20645 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20646 		if (dst_rhtp == NULL)
20647 			return (NULL);
20648 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20649 			TPC_RELE(dst_rhtp);
20650 			dst_rhtp = NULL;
20651 		}
20652 	}
20653 
20654 	/*
20655 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20656 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20657 	 * After selecting the right ipif, under ill_lock make sure ipif is
20658 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20659 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20660 	 * but not under a lock.
20661 	 */
20662 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20663 
20664 retry:
20665 	till = ill;
20666 	ipif_arr[0] = NULL;
20667 
20668 	if (till->ill_group != NULL)
20669 		till = till->ill_group->illgrp_ill;
20670 
20671 	/*
20672 	 * Choose one good source address from each ill across the group.
20673 	 * If possible choose a source address in the same subnet as
20674 	 * the destination address.
20675 	 *
20676 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20677 	 * This is okay because of the following.
20678 	 *
20679 	 *    If PHYI_FAILED is set and we still have non-deprecated
20680 	 *    addresses, it means the addresses have not yet been
20681 	 *    failed over to a different interface. We potentially
20682 	 *    select them to create IRE_CACHES, which will be later
20683 	 *    flushed when the addresses move over.
20684 	 *
20685 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20686 	 *    addresses, it means either the user has configured them
20687 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20688 	 *    been moved over. For the former, in.mpathd does a failover
20689 	 *    when the interface becomes INACTIVE and hence we should
20690 	 *    not find them. Once INACTIVE is set, we don't allow them
20691 	 *    to create logical interfaces anymore. For the latter, a
20692 	 *    flush will happen when INACTIVE is cleared which will
20693 	 *    flush the IRE_CACHES.
20694 	 *
20695 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20696 	 *    over soon. We potentially select them to create IRE_CACHEs,
20697 	 *    which will be later flushed when the addresses move over.
20698 	 *
20699 	 * NOTE : As ipif_select_source is called to borrow source address
20700 	 * for an ipif that is part of a group, source address selection
20701 	 * will be re-done whenever the group changes i.e either an
20702 	 * insertion/deletion in the group.
20703 	 *
20704 	 * Fill ipif_arr[] with source addresses, using these rules:
20705 	 *
20706 	 *	1. At most one source address from a given ill ends up
20707 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20708 	 *	   associated with a given ill ends up in ipif_arr[].
20709 	 *
20710 	 *	2. If there is at least one non-deprecated ipif in the
20711 	 *	   IPMP group with a source address on the same subnet as
20712 	 *	   our destination, then fill ipif_arr[] only with
20713 	 *	   source addresses on the same subnet as our destination.
20714 	 *	   Note that because of (1), only the first
20715 	 *	   non-deprecated ipif found with a source address
20716 	 *	   matching the destination ends up in ipif_arr[].
20717 	 *
20718 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20719 	 *	   addresses not in the same subnet as our destination.
20720 	 *	   Again, because of (1), only the first off-subnet source
20721 	 *	   address will be chosen.
20722 	 *
20723 	 *	4. If there are no non-deprecated ipifs, then just use
20724 	 *	   the source address associated with the last deprecated
20725 	 *	   one we find that happens to be on the same subnet,
20726 	 *	   otherwise the first one not in the same subnet.
20727 	 */
20728 	specific_found = B_FALSE;
20729 	for (; till != NULL; till = till->ill_group_next) {
20730 		ipif_same_found = B_FALSE;
20731 		ipif_other_found = B_FALSE;
20732 		for (ipif = till->ill_ipif; ipif != NULL;
20733 		    ipif = ipif->ipif_next) {
20734 			if (!IPIF_CAN_LOOKUP(ipif))
20735 				continue;
20736 			/* Always skip NOLOCAL and ANYCAST interfaces */
20737 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20738 				continue;
20739 			if (!(ipif->ipif_flags & IPIF_UP) ||
20740 			    !ipif->ipif_addr_ready)
20741 				continue;
20742 			if (ipif->ipif_zoneid != zoneid &&
20743 			    ipif->ipif_zoneid != ALL_ZONES)
20744 				continue;
20745 			/*
20746 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20747 			 * but are not valid as source addresses.
20748 			 */
20749 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20750 				continue;
20751 
20752 			/*
20753 			 * Check compatibility of local address for
20754 			 * destination's default label if we're on a labeled
20755 			 * system.  Incompatible addresses can't be used at
20756 			 * all.
20757 			 */
20758 			if (dst_rhtp != NULL) {
20759 				boolean_t incompat;
20760 
20761 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20762 				    IPV4_VERSION, B_FALSE);
20763 				if (src_rhtp == NULL)
20764 					continue;
20765 				incompat =
20766 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20767 				    src_rhtp->tpc_tp.tp_doi !=
20768 				    dst_rhtp->tpc_tp.tp_doi ||
20769 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20770 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20771 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20772 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20773 				TPC_RELE(src_rhtp);
20774 				if (incompat)
20775 					continue;
20776 			}
20777 
20778 			/*
20779 			 * We prefer not to use all all-zones addresses, if we
20780 			 * can avoid it, as they pose problems with unlabeled
20781 			 * destinations.
20782 			 */
20783 			if (ipif->ipif_zoneid != ALL_ZONES) {
20784 				if (!specific_found &&
20785 				    (!same_subnet_only ||
20786 				    (ipif->ipif_net_mask & dst) ==
20787 				    ipif->ipif_subnet)) {
20788 					index = 0;
20789 					specific_found = B_TRUE;
20790 					ipif_other_found = B_FALSE;
20791 				}
20792 			} else {
20793 				if (specific_found)
20794 					continue;
20795 			}
20796 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20797 				if (ipif_dep == NULL ||
20798 				    (ipif->ipif_net_mask & dst) ==
20799 				    ipif->ipif_subnet)
20800 					ipif_dep = ipif;
20801 				continue;
20802 			}
20803 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20804 				/* found a source address in the same subnet */
20805 				if (!same_subnet_only) {
20806 					same_subnet_only = B_TRUE;
20807 					index = 0;
20808 				}
20809 				ipif_same_found = B_TRUE;
20810 			} else {
20811 				if (same_subnet_only || ipif_other_found)
20812 					continue;
20813 				ipif_other_found = B_TRUE;
20814 			}
20815 			ipif_arr[index++] = ipif;
20816 			if (index == MAX_IPIF_SELECT_SOURCE) {
20817 				wrapped = B_TRUE;
20818 				index = 0;
20819 			}
20820 			if (ipif_same_found)
20821 				break;
20822 		}
20823 	}
20824 
20825 	if (ipif_arr[0] == NULL) {
20826 		ipif = ipif_dep;
20827 	} else {
20828 		if (wrapped)
20829 			index = MAX_IPIF_SELECT_SOURCE;
20830 		ipif = ipif_arr[ipif_rand(ipst) % index];
20831 		ASSERT(ipif != NULL);
20832 	}
20833 
20834 	if (ipif != NULL) {
20835 		mutex_enter(&ipif->ipif_ill->ill_lock);
20836 		if (!IPIF_CAN_LOOKUP(ipif)) {
20837 			mutex_exit(&ipif->ipif_ill->ill_lock);
20838 			goto retry;
20839 		}
20840 		ipif_refhold_locked(ipif);
20841 		mutex_exit(&ipif->ipif_ill->ill_lock);
20842 	}
20843 
20844 	rw_exit(&ipst->ips_ill_g_lock);
20845 	if (usill != NULL)
20846 		ill_refrele(usill);
20847 	if (dst_rhtp != NULL)
20848 		TPC_RELE(dst_rhtp);
20849 
20850 #ifdef DEBUG
20851 	if (ipif == NULL) {
20852 		char buf1[INET6_ADDRSTRLEN];
20853 
20854 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20855 		    ill->ill_name,
20856 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20857 	} else {
20858 		char buf1[INET6_ADDRSTRLEN];
20859 		char buf2[INET6_ADDRSTRLEN];
20860 
20861 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20862 		    ipif->ipif_ill->ill_name,
20863 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20864 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20865 		    buf2, sizeof (buf2))));
20866 	}
20867 #endif /* DEBUG */
20868 	return (ipif);
20869 }
20870 
20871 
20872 /*
20873  * If old_ipif is not NULL, see if ipif was derived from old
20874  * ipif and if so, recreate the interface route by re-doing
20875  * source address selection. This happens when ipif_down ->
20876  * ipif_update_other_ipifs calls us.
20877  *
20878  * If old_ipif is NULL, just redo the source address selection
20879  * if needed. This happens when illgrp_insert or ipif_up_done
20880  * calls us.
20881  */
20882 static void
20883 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20884 {
20885 	ire_t *ire;
20886 	ire_t *ipif_ire;
20887 	queue_t *stq;
20888 	ipif_t *nipif;
20889 	ill_t *ill;
20890 	boolean_t need_rele = B_FALSE;
20891 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20892 
20893 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20894 	ASSERT(IAM_WRITER_IPIF(ipif));
20895 
20896 	ill = ipif->ipif_ill;
20897 	if (!(ipif->ipif_flags &
20898 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20899 		/*
20900 		 * Can't possibly have borrowed the source
20901 		 * from old_ipif.
20902 		 */
20903 		return;
20904 	}
20905 
20906 	/*
20907 	 * Is there any work to be done? No work if the address
20908 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
20909 	 * ipif_select_source() does not borrow addresses from
20910 	 * NOLOCAL and ANYCAST interfaces).
20911 	 */
20912 	if ((old_ipif != NULL) &&
20913 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20914 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20915 	    (old_ipif->ipif_flags &
20916 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20917 		return;
20918 	}
20919 
20920 	/*
20921 	 * Perform the same checks as when creating the
20922 	 * IRE_INTERFACE in ipif_up_done.
20923 	 */
20924 	if (!(ipif->ipif_flags & IPIF_UP))
20925 		return;
20926 
20927 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
20928 	    (ipif->ipif_subnet == INADDR_ANY))
20929 		return;
20930 
20931 	ipif_ire = ipif_to_ire(ipif);
20932 	if (ipif_ire == NULL)
20933 		return;
20934 
20935 	/*
20936 	 * We know that ipif uses some other source for its
20937 	 * IRE_INTERFACE. Is it using the source of this
20938 	 * old_ipif?
20939 	 */
20940 	if (old_ipif != NULL &&
20941 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
20942 		ire_refrele(ipif_ire);
20943 		return;
20944 	}
20945 	if (ip_debug > 2) {
20946 		/* ip1dbg */
20947 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
20948 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
20949 	}
20950 
20951 	stq = ipif_ire->ire_stq;
20952 
20953 	/*
20954 	 * Can't use our source address. Select a different
20955 	 * source address for the IRE_INTERFACE.
20956 	 */
20957 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
20958 	if (nipif == NULL) {
20959 		/* Last resort - all ipif's have IPIF_NOLOCAL */
20960 		nipif = ipif;
20961 	} else {
20962 		need_rele = B_TRUE;
20963 	}
20964 
20965 	ire = ire_create(
20966 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
20967 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
20968 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
20969 	    NULL,				/* no gateway */
20970 	    &ipif->ipif_mtu,			/* max frag */
20971 	    NULL,				/* no src nce */
20972 	    NULL,				/* no recv from queue */
20973 	    stq,				/* send-to queue */
20974 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20975 	    ipif,
20976 	    0,
20977 	    0,
20978 	    0,
20979 	    0,
20980 	    &ire_uinfo_null,
20981 	    NULL,
20982 	    NULL,
20983 	    ipst);
20984 
20985 	if (ire != NULL) {
20986 		ire_t *ret_ire;
20987 		int error;
20988 
20989 		/*
20990 		 * We don't need ipif_ire anymore. We need to delete
20991 		 * before we add so that ire_add does not detect
20992 		 * duplicates.
20993 		 */
20994 		ire_delete(ipif_ire);
20995 		ret_ire = ire;
20996 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
20997 		ASSERT(error == 0);
20998 		ASSERT(ire == ret_ire);
20999 		/* Held in ire_add */
21000 		ire_refrele(ret_ire);
21001 	}
21002 	/*
21003 	 * Either we are falling through from above or could not
21004 	 * allocate a replacement.
21005 	 */
21006 	ire_refrele(ipif_ire);
21007 	if (need_rele)
21008 		ipif_refrele(nipif);
21009 }
21010 
21011 /*
21012  * This old_ipif is going away.
21013  *
21014  * Determine if any other ipif's is using our address as
21015  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21016  * IPIF_DEPRECATED).
21017  * Find the IRE_INTERFACE for such ipifs and recreate them
21018  * to use an different source address following the rules in
21019  * ipif_up_done.
21020  *
21021  * This function takes an illgrp as an argument so that illgrp_delete
21022  * can call this to update source address even after deleting the
21023  * old_ipif->ipif_ill from the ill group.
21024  */
21025 static void
21026 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21027 {
21028 	ipif_t *ipif;
21029 	ill_t *ill;
21030 	char	buf[INET6_ADDRSTRLEN];
21031 
21032 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21033 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21034 
21035 	ill = old_ipif->ipif_ill;
21036 
21037 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21038 	    ill->ill_name,
21039 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21040 	    buf, sizeof (buf))));
21041 	/*
21042 	 * If this part of a group, look at all ills as ipif_select_source
21043 	 * borrows source address across all the ills in the group.
21044 	 */
21045 	if (illgrp != NULL)
21046 		ill = illgrp->illgrp_ill;
21047 
21048 	for (; ill != NULL; ill = ill->ill_group_next) {
21049 		for (ipif = ill->ill_ipif; ipif != NULL;
21050 		    ipif = ipif->ipif_next) {
21051 
21052 			if (ipif == old_ipif)
21053 				continue;
21054 
21055 			ipif_recreate_interface_routes(old_ipif, ipif);
21056 		}
21057 	}
21058 }
21059 
21060 /* ARGSUSED */
21061 int
21062 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21063 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21064 {
21065 	/*
21066 	 * ill_phyint_reinit merged the v4 and v6 into a single
21067 	 * ipsq. Could also have become part of a ipmp group in the
21068 	 * process, and we might not have been able to complete the
21069 	 * operation in ipif_set_values, if we could not become
21070 	 * exclusive.  If so restart it here.
21071 	 */
21072 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21073 }
21074 
21075 
21076 /*
21077  * Can operate on either a module or a driver queue.
21078  * Returns an error if not a module queue.
21079  */
21080 /* ARGSUSED */
21081 int
21082 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21083     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21084 {
21085 	queue_t		*q1 = q;
21086 	char 		*cp;
21087 	char		interf_name[LIFNAMSIZ];
21088 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21089 
21090 	if (q->q_next == NULL) {
21091 		ip1dbg((
21092 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21093 		return (EINVAL);
21094 	}
21095 
21096 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21097 		return (EALREADY);
21098 
21099 	do {
21100 		q1 = q1->q_next;
21101 	} while (q1->q_next);
21102 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21103 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21104 
21105 	/*
21106 	 * Here we are not going to delay the ioack until after
21107 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21108 	 * original ioctl message before sending the requests.
21109 	 */
21110 	return (ipif_set_values(q, mp, interf_name, &ppa));
21111 }
21112 
21113 /* ARGSUSED */
21114 int
21115 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21116     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21117 {
21118 	return (ENXIO);
21119 }
21120 
21121 /*
21122  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21123  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21124  * ire_check_and_create_bcast()).
21125  */
21126 static ire_t **
21127 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21128 {
21129 	ipaddr_t addr;
21130 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21131 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21132 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21133 
21134 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21135 
21136 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21137 
21138 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21139 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21140 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21141 
21142 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21143 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21144 
21145 	/*
21146 	 * For backward compatibility, we create net broadcast IREs based on
21147 	 * the old "IP address class system", since some old machines only
21148 	 * respond to these class derived net broadcast.  However, we must not
21149 	 * create these net broadcast IREs if the subnetmask is shorter than
21150 	 * the IP address class based derived netmask.  Otherwise, we may
21151 	 * create a net broadcast address which is the same as an IP address
21152 	 * on the subnet -- and then TCP will refuse to talk to that address.
21153 	 */
21154 	if (netmask < subnetmask) {
21155 		addr = netmask & ipif->ipif_subnet;
21156 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21157 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21158 		    flags);
21159 	}
21160 
21161 	/*
21162 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21163 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21164 	 * created.  Creating these broadcast IREs will only create confusion
21165 	 * as `addr' will be the same as the IP address.
21166 	 */
21167 	if (subnetmask != 0xFFFFFFFF) {
21168 		addr = ipif->ipif_subnet;
21169 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21170 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21171 		    irep, flags);
21172 	}
21173 
21174 	return (irep);
21175 }
21176 
21177 /*
21178  * Broadcast IRE info structure used in the functions below.  Since we
21179  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21180  */
21181 typedef struct bcast_ireinfo {
21182 	uchar_t		bi_type;	/* BCAST_* value from below */
21183 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21184 			bi_needrep:1,	/* do we need to replace it? */
21185 			bi_haverep:1,	/* have we replaced it? */
21186 			bi_pad:5;
21187 	ipaddr_t	bi_addr;	/* IRE address */
21188 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21189 } bcast_ireinfo_t;
21190 
21191 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21192 
21193 /*
21194  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21195  * return B_TRUE if it should immediately be used to recreate the IRE.
21196  */
21197 static boolean_t
21198 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21199 {
21200 	ipaddr_t addr;
21201 
21202 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21203 
21204 	switch (bireinfop->bi_type) {
21205 	case BCAST_NET:
21206 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21207 		if (addr != bireinfop->bi_addr)
21208 			return (B_FALSE);
21209 		break;
21210 	case BCAST_SUBNET:
21211 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21212 			return (B_FALSE);
21213 		break;
21214 	}
21215 
21216 	bireinfop->bi_needrep = 1;
21217 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21218 		if (bireinfop->bi_backup == NULL)
21219 			bireinfop->bi_backup = ipif;
21220 		return (B_FALSE);
21221 	}
21222 	return (B_TRUE);
21223 }
21224 
21225 /*
21226  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21227  * them ala ire_check_and_create_bcast().
21228  */
21229 static ire_t **
21230 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21231 {
21232 	ipaddr_t mask, addr;
21233 
21234 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21235 
21236 	addr = bireinfop->bi_addr;
21237 	irep = ire_create_bcast(ipif, addr, irep);
21238 
21239 	switch (bireinfop->bi_type) {
21240 	case BCAST_NET:
21241 		mask = ip_net_mask(ipif->ipif_subnet);
21242 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21243 		break;
21244 	case BCAST_SUBNET:
21245 		mask = ipif->ipif_net_mask;
21246 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21247 		break;
21248 	}
21249 
21250 	bireinfop->bi_haverep = 1;
21251 	return (irep);
21252 }
21253 
21254 /*
21255  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21256  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21257  * that are going away are still needed.  If so, have ipif_create_bcast()
21258  * recreate them (except for the deprecated case, as explained below).
21259  */
21260 static ire_t **
21261 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21262     ire_t **irep)
21263 {
21264 	int i;
21265 	ipif_t *ipif;
21266 
21267 	ASSERT(!ill->ill_isv6);
21268 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21269 		/*
21270 		 * Skip this ipif if it's (a) the one being taken down, (b)
21271 		 * not in the same zone, or (c) has no valid local address.
21272 		 */
21273 		if (ipif == test_ipif ||
21274 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21275 		    ipif->ipif_subnet == 0 ||
21276 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21277 		    (IPIF_UP|IPIF_BROADCAST))
21278 			continue;
21279 
21280 		/*
21281 		 * For each dying IRE that hasn't yet been replaced, see if
21282 		 * `ipif' needs it and whether the IRE should be recreated on
21283 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21284 		 * will return B_FALSE even if `ipif' needs the IRE on the
21285 		 * hopes that we'll later find a needy non-deprecated ipif.
21286 		 * However, the ipif is recorded in bi_backup for possible
21287 		 * subsequent use by ipif_check_bcast_ires().
21288 		 */
21289 		for (i = 0; i < BCAST_COUNT; i++) {
21290 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21291 				continue;
21292 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21293 				continue;
21294 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21295 		}
21296 
21297 		/*
21298 		 * If we've replaced all of the broadcast IREs that are going
21299 		 * to be taken down, we know we're done.
21300 		 */
21301 		for (i = 0; i < BCAST_COUNT; i++) {
21302 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21303 				break;
21304 		}
21305 		if (i == BCAST_COUNT)
21306 			break;
21307 	}
21308 	return (irep);
21309 }
21310 
21311 /*
21312  * Check if `test_ipif' (which is going away) is associated with any existing
21313  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21314  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21315  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21316  *
21317  * This is necessary because broadcast IREs are shared.  In particular, a
21318  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21319  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21320  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21321  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21322  * same zone, they will share the same set of broadcast IREs.
21323  *
21324  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21325  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21326  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21327  */
21328 static void
21329 ipif_check_bcast_ires(ipif_t *test_ipif)
21330 {
21331 	ill_t		*ill = test_ipif->ipif_ill;
21332 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21333 	ire_t		**irep1, **irep = &ire_array[0];
21334 	uint_t 		i, willdie;
21335 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21336 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21337 
21338 	ASSERT(!test_ipif->ipif_isv6);
21339 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21340 
21341 	/*
21342 	 * No broadcast IREs for the LOOPBACK interface
21343 	 * or others such as point to point and IPIF_NOXMIT.
21344 	 */
21345 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21346 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21347 		return;
21348 
21349 	bzero(bireinfo, sizeof (bireinfo));
21350 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21351 	bireinfo[0].bi_addr = 0;
21352 
21353 	bireinfo[1].bi_type = BCAST_ALLONES;
21354 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21355 
21356 	bireinfo[2].bi_type = BCAST_NET;
21357 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21358 
21359 	if (test_ipif->ipif_net_mask != 0)
21360 		mask = test_ipif->ipif_net_mask;
21361 	bireinfo[3].bi_type = BCAST_SUBNET;
21362 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21363 
21364 	/*
21365 	 * Figure out what (if any) broadcast IREs will die as a result of
21366 	 * `test_ipif' going away.  If none will die, we're done.
21367 	 */
21368 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21369 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21370 		    test_ipif, ALL_ZONES, NULL,
21371 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21372 		if (ire != NULL) {
21373 			willdie++;
21374 			bireinfo[i].bi_willdie = 1;
21375 			ire_refrele(ire);
21376 		}
21377 	}
21378 
21379 	if (willdie == 0)
21380 		return;
21381 
21382 	/*
21383 	 * Walk through all the ipifs that will be affected by the dying IREs,
21384 	 * and recreate the IREs as necessary.
21385 	 */
21386 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21387 
21388 	/*
21389 	 * Scan through the set of broadcast IREs and see if there are any
21390 	 * that we need to replace that have not yet been replaced.  If so,
21391 	 * replace them using the appropriate backup ipif.
21392 	 */
21393 	for (i = 0; i < BCAST_COUNT; i++) {
21394 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21395 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21396 			    &bireinfo[i], irep);
21397 	}
21398 
21399 	/*
21400 	 * If we can't create all of them, don't add any of them.  (Code in
21401 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21402 	 * non-loopback copy and loopback copy for a given address.)
21403 	 */
21404 	for (irep1 = irep; irep1 > ire_array; ) {
21405 		irep1--;
21406 		if (*irep1 == NULL) {
21407 			ip0dbg(("ipif_check_bcast_ires: can't create "
21408 			    "IRE_BROADCAST, memory allocation failure\n"));
21409 			while (irep > ire_array) {
21410 				irep--;
21411 				if (*irep != NULL)
21412 					ire_delete(*irep);
21413 			}
21414 			return;
21415 		}
21416 	}
21417 
21418 	for (irep1 = irep; irep1 > ire_array; ) {
21419 		irep1--;
21420 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21421 			ire_refrele(*irep1);		/* Held in ire_add */
21422 	}
21423 }
21424 
21425 /*
21426  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21427  * from lifr_flags and the name from lifr_name.
21428  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21429  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21430  * Returns EINPROGRESS when mp has been consumed by queueing it on
21431  * ill_pending_mp and the ioctl will complete in ip_rput.
21432  *
21433  * Can operate on either a module or a driver queue.
21434  * Returns an error if not a module queue.
21435  */
21436 /* ARGSUSED */
21437 int
21438 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21439     ip_ioctl_cmd_t *ipip, void *if_req)
21440 {
21441 	ill_t	*ill = q->q_ptr;
21442 	phyint_t *phyi;
21443 	ip_stack_t *ipst;
21444 	struct lifreq *lifr = if_req;
21445 
21446 	ASSERT(ipif != NULL);
21447 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21448 
21449 	if (q->q_next == NULL) {
21450 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21451 		return (EINVAL);
21452 	}
21453 
21454 	/*
21455 	 * If we are not writer on 'q' then this interface exists already
21456 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21457 	 * so return EALREADY.
21458 	 */
21459 	if (ill != ipif->ipif_ill)
21460 		return (EALREADY);
21461 
21462 	if (ill->ill_name[0] != '\0')
21463 		return (EALREADY);
21464 
21465 	/*
21466 	 * Set all the flags. Allows all kinds of override. Provide some
21467 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21468 	 * unless there is either multicast/broadcast support in the driver
21469 	 * or it is a pt-pt link.
21470 	 */
21471 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21472 		/* Meaningless to IP thus don't allow them to be set. */
21473 		ip1dbg(("ip_setname: EINVAL 1\n"));
21474 		return (EINVAL);
21475 	}
21476 
21477 	/*
21478 	 * If there's another ill already with the requested name, ensure
21479 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21480 	 * fuse together two unrelated ills, which will cause chaos.
21481 	 */
21482 	ipst = ill->ill_ipst;
21483 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21484 	    lifr->lifr_name, NULL);
21485 	if (phyi != NULL) {
21486 		ill_t *ill_mate = phyi->phyint_illv4;
21487 
21488 		if (ill_mate == NULL)
21489 			ill_mate = phyi->phyint_illv6;
21490 		ASSERT(ill_mate != NULL);
21491 
21492 		if (ill_mate->ill_media->ip_m_mac_type !=
21493 		    ill->ill_media->ip_m_mac_type) {
21494 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21495 			    "use the same ill name on differing media\n"));
21496 			return (EINVAL);
21497 		}
21498 	}
21499 
21500 	/*
21501 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21502 	 * ill_bcast_addr_length info.
21503 	 */
21504 	if (!ill->ill_needs_attach &&
21505 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21506 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21507 	    ill->ill_bcast_addr_length == 0)) {
21508 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21509 		ip1dbg(("ip_setname: EINVAL 2\n"));
21510 		return (EINVAL);
21511 	}
21512 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21513 	    ((lifr->lifr_flags & IFF_IPV6) ||
21514 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21515 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21516 		ip1dbg(("ip_setname: EINVAL 3\n"));
21517 		return (EINVAL);
21518 	}
21519 	if (lifr->lifr_flags & IFF_UP) {
21520 		/* Can only be set with SIOCSLIFFLAGS */
21521 		ip1dbg(("ip_setname: EINVAL 4\n"));
21522 		return (EINVAL);
21523 	}
21524 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21525 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21526 		ip1dbg(("ip_setname: EINVAL 5\n"));
21527 		return (EINVAL);
21528 	}
21529 	/*
21530 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21531 	 */
21532 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21533 	    !(lifr->lifr_flags & IFF_IPV6) &&
21534 	    !(ipif->ipif_isv6)) {
21535 		ip1dbg(("ip_setname: EINVAL 6\n"));
21536 		return (EINVAL);
21537 	}
21538 
21539 	/*
21540 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21541 	 * we have all the flags here. So, we assign rather than we OR.
21542 	 * We can't OR the flags here because we don't want to set
21543 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21544 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21545 	 * on lifr_flags value here.
21546 	 */
21547 	/*
21548 	 * This ill has not been inserted into the global list.
21549 	 * So we are still single threaded and don't need any lock
21550 	 */
21551 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21552 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21553 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21554 
21555 	/* We started off as V4. */
21556 	if (ill->ill_flags & ILLF_IPV6) {
21557 		ill->ill_phyint->phyint_illv6 = ill;
21558 		ill->ill_phyint->phyint_illv4 = NULL;
21559 	}
21560 
21561 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21562 }
21563 
21564 /* ARGSUSED */
21565 int
21566 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21567     ip_ioctl_cmd_t *ipip, void *if_req)
21568 {
21569 	/*
21570 	 * ill_phyint_reinit merged the v4 and v6 into a single
21571 	 * ipsq. Could also have become part of a ipmp group in the
21572 	 * process, and we might not have been able to complete the
21573 	 * slifname in ipif_set_values, if we could not become
21574 	 * exclusive.  If so restart it here
21575 	 */
21576 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21577 }
21578 
21579 /*
21580  * Return a pointer to the ipif which matches the index, IP version type and
21581  * zoneid.
21582  */
21583 ipif_t *
21584 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21585     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21586 {
21587 	ill_t	*ill;
21588 	ipif_t	*ipif = NULL;
21589 
21590 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21591 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21592 
21593 	if (err != NULL)
21594 		*err = 0;
21595 
21596 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21597 	if (ill != NULL) {
21598 		mutex_enter(&ill->ill_lock);
21599 		for (ipif = ill->ill_ipif; ipif != NULL;
21600 		    ipif = ipif->ipif_next) {
21601 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21602 			    zoneid == ipif->ipif_zoneid ||
21603 			    ipif->ipif_zoneid == ALL_ZONES)) {
21604 				ipif_refhold_locked(ipif);
21605 				break;
21606 			}
21607 		}
21608 		mutex_exit(&ill->ill_lock);
21609 		ill_refrele(ill);
21610 		if (ipif == NULL && err != NULL)
21611 			*err = ENXIO;
21612 	}
21613 	return (ipif);
21614 }
21615 
21616 typedef struct conn_change_s {
21617 	uint_t cc_old_ifindex;
21618 	uint_t cc_new_ifindex;
21619 } conn_change_t;
21620 
21621 /*
21622  * ipcl_walk function for changing interface index.
21623  */
21624 static void
21625 conn_change_ifindex(conn_t *connp, caddr_t arg)
21626 {
21627 	conn_change_t *connc;
21628 	uint_t old_ifindex;
21629 	uint_t new_ifindex;
21630 	int i;
21631 	ilg_t *ilg;
21632 
21633 	connc = (conn_change_t *)arg;
21634 	old_ifindex = connc->cc_old_ifindex;
21635 	new_ifindex = connc->cc_new_ifindex;
21636 
21637 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21638 		connp->conn_orig_bound_ifindex = new_ifindex;
21639 
21640 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21641 		connp->conn_orig_multicast_ifindex = new_ifindex;
21642 
21643 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21644 		ilg = &connp->conn_ilg[i];
21645 		if (ilg->ilg_orig_ifindex == old_ifindex)
21646 			ilg->ilg_orig_ifindex = new_ifindex;
21647 	}
21648 }
21649 
21650 /*
21651  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21652  * to new_index if it matches the old_index.
21653  *
21654  * Failovers typically happen within a group of ills. But somebody
21655  * can remove an ill from the group after a failover happened. If
21656  * we are setting the ifindex after this, we potentially need to
21657  * look at all the ills rather than just the ones in the group.
21658  * We cut down the work by looking at matching ill_net_types
21659  * and ill_types as we could not possibly grouped them together.
21660  */
21661 static void
21662 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21663 {
21664 	ill_t *ill;
21665 	ipif_t *ipif;
21666 	uint_t old_ifindex;
21667 	uint_t new_ifindex;
21668 	ilm_t *ilm;
21669 	ill_walk_context_t ctx;
21670 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21671 
21672 	old_ifindex = connc->cc_old_ifindex;
21673 	new_ifindex = connc->cc_new_ifindex;
21674 
21675 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21676 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21677 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21678 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21679 		    (ill_orig->ill_type != ill->ill_type)) {
21680 			continue;
21681 		}
21682 		for (ipif = ill->ill_ipif; ipif != NULL;
21683 		    ipif = ipif->ipif_next) {
21684 			if (ipif->ipif_orig_ifindex == old_ifindex)
21685 				ipif->ipif_orig_ifindex = new_ifindex;
21686 		}
21687 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21688 			if (ilm->ilm_orig_ifindex == old_ifindex)
21689 				ilm->ilm_orig_ifindex = new_ifindex;
21690 		}
21691 	}
21692 	rw_exit(&ipst->ips_ill_g_lock);
21693 }
21694 
21695 /*
21696  * We first need to ensure that the new index is unique, and
21697  * then carry the change across both v4 and v6 ill representation
21698  * of the physical interface.
21699  */
21700 /* ARGSUSED */
21701 int
21702 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21703     ip_ioctl_cmd_t *ipip, void *ifreq)
21704 {
21705 	ill_t		*ill;
21706 	ill_t		*ill_other;
21707 	phyint_t	*phyi;
21708 	int		old_index;
21709 	conn_change_t	connc;
21710 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21711 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21712 	uint_t	index;
21713 	ill_t	*ill_v4;
21714 	ill_t	*ill_v6;
21715 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21716 
21717 	if (ipip->ipi_cmd_type == IF_CMD)
21718 		index = ifr->ifr_index;
21719 	else
21720 		index = lifr->lifr_index;
21721 
21722 	/*
21723 	 * Only allow on physical interface. Also, index zero is illegal.
21724 	 *
21725 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21726 	 *
21727 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21728 	 *    implies a possible failback might have to happen. As failback
21729 	 *    depends on the old index, we should fail setting the index.
21730 	 *
21731 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21732 	 *    any addresses or multicast memberships are failed over to
21733 	 *    a non-STANDBY interface. As failback depends on the old
21734 	 *    index, we should fail setting the index for this case also.
21735 	 *
21736 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21737 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21738 	 */
21739 	ill = ipif->ipif_ill;
21740 	phyi = ill->ill_phyint;
21741 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21742 	    ipif->ipif_id != 0 || index == 0) {
21743 		return (EINVAL);
21744 	}
21745 	old_index = phyi->phyint_ifindex;
21746 
21747 	/* If the index is not changing, no work to do */
21748 	if (old_index == index)
21749 		return (0);
21750 
21751 	/*
21752 	 * Use ill_lookup_on_ifindex to determine if the
21753 	 * new index is unused and if so allow the change.
21754 	 */
21755 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21756 	    ipst);
21757 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21758 	    ipst);
21759 	if (ill_v6 != NULL || ill_v4 != NULL) {
21760 		if (ill_v4 != NULL)
21761 			ill_refrele(ill_v4);
21762 		if (ill_v6 != NULL)
21763 			ill_refrele(ill_v6);
21764 		return (EBUSY);
21765 	}
21766 
21767 	/*
21768 	 * The new index is unused. Set it in the phyint.
21769 	 * Locate the other ill so that we can send a routing
21770 	 * sockets message.
21771 	 */
21772 	if (ill->ill_isv6) {
21773 		ill_other = phyi->phyint_illv4;
21774 	} else {
21775 		ill_other = phyi->phyint_illv6;
21776 	}
21777 
21778 	phyi->phyint_ifindex = index;
21779 
21780 	/* Update SCTP's ILL list */
21781 	sctp_ill_reindex(ill, old_index);
21782 
21783 	connc.cc_old_ifindex = old_index;
21784 	connc.cc_new_ifindex = index;
21785 	ip_change_ifindex(ill, &connc);
21786 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21787 
21788 	/* Send the routing sockets message */
21789 	ip_rts_ifmsg(ipif);
21790 	if (ill_other != NULL)
21791 		ip_rts_ifmsg(ill_other->ill_ipif);
21792 
21793 	return (0);
21794 }
21795 
21796 /* ARGSUSED */
21797 int
21798 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21799     ip_ioctl_cmd_t *ipip, void *ifreq)
21800 {
21801 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21802 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21803 
21804 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21805 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21806 	/* Get the interface index */
21807 	if (ipip->ipi_cmd_type == IF_CMD) {
21808 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21809 	} else {
21810 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21811 	}
21812 	return (0);
21813 }
21814 
21815 /* ARGSUSED */
21816 int
21817 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21818     ip_ioctl_cmd_t *ipip, void *ifreq)
21819 {
21820 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21821 
21822 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21823 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21824 	/* Get the interface zone */
21825 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21826 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21827 	return (0);
21828 }
21829 
21830 /*
21831  * Set the zoneid of an interface.
21832  */
21833 /* ARGSUSED */
21834 int
21835 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21836     ip_ioctl_cmd_t *ipip, void *ifreq)
21837 {
21838 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21839 	int err = 0;
21840 	boolean_t need_up = B_FALSE;
21841 	zone_t *zptr;
21842 	zone_status_t status;
21843 	zoneid_t zoneid;
21844 
21845 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21846 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21847 		if (!is_system_labeled())
21848 			return (ENOTSUP);
21849 		zoneid = GLOBAL_ZONEID;
21850 	}
21851 
21852 	/* cannot assign instance zero to a non-global zone */
21853 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21854 		return (ENOTSUP);
21855 
21856 	/*
21857 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21858 	 * the event of a race with the zone shutdown processing, since IP
21859 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21860 	 * interface will be cleaned up even if the zone is shut down
21861 	 * immediately after the status check. If the interface can't be brought
21862 	 * down right away, and the zone is shut down before the restart
21863 	 * function is called, we resolve the possible races by rechecking the
21864 	 * zone status in the restart function.
21865 	 */
21866 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21867 		return (EINVAL);
21868 	status = zone_status_get(zptr);
21869 	zone_rele(zptr);
21870 
21871 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21872 		return (EINVAL);
21873 
21874 	if (ipif->ipif_flags & IPIF_UP) {
21875 		/*
21876 		 * If the interface is already marked up,
21877 		 * we call ipif_down which will take care
21878 		 * of ditching any IREs that have been set
21879 		 * up based on the old interface address.
21880 		 */
21881 		err = ipif_logical_down(ipif, q, mp);
21882 		if (err == EINPROGRESS)
21883 			return (err);
21884 		ipif_down_tail(ipif);
21885 		need_up = B_TRUE;
21886 	}
21887 
21888 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21889 	return (err);
21890 }
21891 
21892 static int
21893 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21894     queue_t *q, mblk_t *mp, boolean_t need_up)
21895 {
21896 	int	err = 0;
21897 	ip_stack_t	*ipst;
21898 
21899 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21900 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21901 
21902 	if (CONN_Q(q))
21903 		ipst = CONNQ_TO_IPST(q);
21904 	else
21905 		ipst = ILLQ_TO_IPST(q);
21906 
21907 	/*
21908 	 * For exclusive stacks we don't allow a different zoneid than
21909 	 * global.
21910 	 */
21911 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
21912 	    zoneid != GLOBAL_ZONEID)
21913 		return (EINVAL);
21914 
21915 	/* Set the new zone id. */
21916 	ipif->ipif_zoneid = zoneid;
21917 
21918 	/* Update sctp list */
21919 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
21920 
21921 	if (need_up) {
21922 		/*
21923 		 * Now bring the interface back up.  If this
21924 		 * is the only IPIF for the ILL, ipif_up
21925 		 * will have to re-bind to the device, so
21926 		 * we may get back EINPROGRESS, in which
21927 		 * case, this IOCTL will get completed in
21928 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
21929 		 */
21930 		err = ipif_up(ipif, q, mp);
21931 	}
21932 	return (err);
21933 }
21934 
21935 /* ARGSUSED */
21936 int
21937 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21938     ip_ioctl_cmd_t *ipip, void *if_req)
21939 {
21940 	struct lifreq *lifr = (struct lifreq *)if_req;
21941 	zoneid_t zoneid;
21942 	zone_t *zptr;
21943 	zone_status_t status;
21944 
21945 	ASSERT(ipif->ipif_id != 0);
21946 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21947 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
21948 		zoneid = GLOBAL_ZONEID;
21949 
21950 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
21951 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21952 
21953 	/*
21954 	 * We recheck the zone status to resolve the following race condition:
21955 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
21956 	 * 2) hme0:1 is up and can't be brought down right away;
21957 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
21958 	 * 3) zone "myzone" is halted; the zone status switches to
21959 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
21960 	 * the interfaces to remove - hme0:1 is not returned because it's not
21961 	 * yet in "myzone", so it won't be removed;
21962 	 * 4) the restart function for SIOCSLIFZONE is called; without the
21963 	 * status check here, we would have hme0:1 in "myzone" after it's been
21964 	 * destroyed.
21965 	 * Note that if the status check fails, we need to bring the interface
21966 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
21967 	 * ipif_up_done[_v6]().
21968 	 */
21969 	status = ZONE_IS_UNINITIALIZED;
21970 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
21971 		status = zone_status_get(zptr);
21972 		zone_rele(zptr);
21973 	}
21974 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
21975 		if (ipif->ipif_isv6) {
21976 			(void) ipif_up_done_v6(ipif);
21977 		} else {
21978 			(void) ipif_up_done(ipif);
21979 		}
21980 		return (EINVAL);
21981 	}
21982 
21983 	ipif_down_tail(ipif);
21984 
21985 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
21986 	    B_TRUE));
21987 }
21988 
21989 /* ARGSUSED */
21990 int
21991 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21992 	ip_ioctl_cmd_t *ipip, void *ifreq)
21993 {
21994 	struct lifreq	*lifr = ifreq;
21995 
21996 	ASSERT(q->q_next == NULL);
21997 	ASSERT(CONN_Q(q));
21998 
21999 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22000 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22001 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22002 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22003 
22004 	return (0);
22005 }
22006 
22007 
22008 /* Find the previous ILL in this usesrc group */
22009 static ill_t *
22010 ill_prev_usesrc(ill_t *uill)
22011 {
22012 	ill_t *ill;
22013 
22014 	for (ill = uill->ill_usesrc_grp_next;
22015 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22016 	    ill = ill->ill_usesrc_grp_next)
22017 		/* do nothing */;
22018 	return (ill);
22019 }
22020 
22021 /*
22022  * Release all members of the usesrc group. This routine is called
22023  * from ill_delete when the interface being unplumbed is the
22024  * group head.
22025  */
22026 static void
22027 ill_disband_usesrc_group(ill_t *uill)
22028 {
22029 	ill_t *next_ill, *tmp_ill;
22030 	ip_stack_t	*ipst = uill->ill_ipst;
22031 
22032 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22033 	next_ill = uill->ill_usesrc_grp_next;
22034 
22035 	do {
22036 		ASSERT(next_ill != NULL);
22037 		tmp_ill = next_ill->ill_usesrc_grp_next;
22038 		ASSERT(tmp_ill != NULL);
22039 		next_ill->ill_usesrc_grp_next = NULL;
22040 		next_ill->ill_usesrc_ifindex = 0;
22041 		next_ill = tmp_ill;
22042 	} while (next_ill->ill_usesrc_ifindex != 0);
22043 	uill->ill_usesrc_grp_next = NULL;
22044 }
22045 
22046 /*
22047  * Remove the client usesrc ILL from the list and relink to a new list
22048  */
22049 int
22050 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22051 {
22052 	ill_t *ill, *tmp_ill;
22053 	ip_stack_t	*ipst = ucill->ill_ipst;
22054 
22055 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22056 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22057 
22058 	/*
22059 	 * Check if the usesrc client ILL passed in is not already
22060 	 * in use as a usesrc ILL i.e one whose source address is
22061 	 * in use OR a usesrc ILL is not already in use as a usesrc
22062 	 * client ILL
22063 	 */
22064 	if ((ucill->ill_usesrc_ifindex == 0) ||
22065 	    (uill->ill_usesrc_ifindex != 0)) {
22066 		return (-1);
22067 	}
22068 
22069 	ill = ill_prev_usesrc(ucill);
22070 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22071 
22072 	/* Remove from the current list */
22073 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22074 		/* Only two elements in the list */
22075 		ASSERT(ill->ill_usesrc_ifindex == 0);
22076 		ill->ill_usesrc_grp_next = NULL;
22077 	} else {
22078 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22079 	}
22080 
22081 	if (ifindex == 0) {
22082 		ucill->ill_usesrc_ifindex = 0;
22083 		ucill->ill_usesrc_grp_next = NULL;
22084 		return (0);
22085 	}
22086 
22087 	ucill->ill_usesrc_ifindex = ifindex;
22088 	tmp_ill = uill->ill_usesrc_grp_next;
22089 	uill->ill_usesrc_grp_next = ucill;
22090 	ucill->ill_usesrc_grp_next =
22091 	    (tmp_ill != NULL) ? tmp_ill : uill;
22092 	return (0);
22093 }
22094 
22095 /*
22096  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22097  * ip.c for locking details.
22098  */
22099 /* ARGSUSED */
22100 int
22101 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22102     ip_ioctl_cmd_t *ipip, void *ifreq)
22103 {
22104 	struct lifreq *lifr = (struct lifreq *)ifreq;
22105 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22106 	    ill_flag_changed = B_FALSE;
22107 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22108 	int err = 0, ret;
22109 	uint_t ifindex;
22110 	phyint_t *us_phyint, *us_cli_phyint;
22111 	ipsq_t *ipsq = NULL;
22112 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22113 
22114 	ASSERT(IAM_WRITER_IPIF(ipif));
22115 	ASSERT(q->q_next == NULL);
22116 	ASSERT(CONN_Q(q));
22117 
22118 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22119 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22120 
22121 	ASSERT(us_cli_phyint != NULL);
22122 
22123 	/*
22124 	 * If the client ILL is being used for IPMP, abort.
22125 	 * Note, this can be done before ipsq_try_enter since we are already
22126 	 * exclusive on this ILL
22127 	 */
22128 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22129 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22130 		return (EINVAL);
22131 	}
22132 
22133 	ifindex = lifr->lifr_index;
22134 	if (ifindex == 0) {
22135 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22136 			/* non usesrc group interface, nothing to reset */
22137 			return (0);
22138 		}
22139 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22140 		/* valid reset request */
22141 		reset_flg = B_TRUE;
22142 	}
22143 
22144 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22145 	    ip_process_ioctl, &err, ipst);
22146 
22147 	if (usesrc_ill == NULL) {
22148 		return (err);
22149 	}
22150 
22151 	/*
22152 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22153 	 * group nor can either of the interfaces be used for standy. So
22154 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22155 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22156 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22157 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22158 	 * the usesrc_cli_ill
22159 	 */
22160 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22161 	    NEW_OP, B_TRUE);
22162 	if (ipsq == NULL) {
22163 		err = EINPROGRESS;
22164 		/* Operation enqueued on the ipsq of the usesrc ILL */
22165 		goto done;
22166 	}
22167 
22168 	/* Check if the usesrc_ill is used for IPMP */
22169 	us_phyint = usesrc_ill->ill_phyint;
22170 	if ((us_phyint->phyint_groupname != NULL) ||
22171 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22172 		err = EINVAL;
22173 		goto done;
22174 	}
22175 
22176 	/*
22177 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22178 	 * already a client then return EINVAL
22179 	 */
22180 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22181 		err = EINVAL;
22182 		goto done;
22183 	}
22184 
22185 	/*
22186 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22187 	 * be then this is a duplicate operation.
22188 	 */
22189 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22190 		err = 0;
22191 		goto done;
22192 	}
22193 
22194 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22195 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22196 	    usesrc_ill->ill_isv6));
22197 
22198 	/*
22199 	 * The next step ensures that no new ires will be created referencing
22200 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22201 	 * we go through an ire walk deleting all ire caches that reference
22202 	 * the client ill. New ires referencing the client ill that are added
22203 	 * to the ire table before the ILL_CHANGING flag is set, will be
22204 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22205 	 * the client ill while the ILL_CHANGING flag is set will be failed
22206 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22207 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22208 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22209 	 * belong to the same usesrc group.
22210 	 */
22211 	mutex_enter(&usesrc_cli_ill->ill_lock);
22212 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22213 	mutex_exit(&usesrc_cli_ill->ill_lock);
22214 	ill_flag_changed = B_TRUE;
22215 
22216 	if (ipif->ipif_isv6)
22217 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22218 		    ALL_ZONES, ipst);
22219 	else
22220 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22221 		    ALL_ZONES, ipst);
22222 
22223 	/*
22224 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22225 	 * and the ill_usesrc_ifindex fields
22226 	 */
22227 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22228 
22229 	if (reset_flg) {
22230 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22231 		if (ret != 0) {
22232 			err = EINVAL;
22233 		}
22234 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22235 		goto done;
22236 	}
22237 
22238 	/*
22239 	 * Four possibilities to consider:
22240 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22241 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22242 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22243 	 * 4. Both are part of their respective usesrc groups
22244 	 */
22245 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22246 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22247 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22248 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22249 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22250 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22251 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22252 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22253 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22254 		/* Insert at head of list */
22255 		usesrc_cli_ill->ill_usesrc_grp_next =
22256 		    usesrc_ill->ill_usesrc_grp_next;
22257 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22258 	} else {
22259 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22260 		    ifindex);
22261 		if (ret != 0)
22262 			err = EINVAL;
22263 	}
22264 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22265 
22266 done:
22267 	if (ill_flag_changed) {
22268 		mutex_enter(&usesrc_cli_ill->ill_lock);
22269 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22270 		mutex_exit(&usesrc_cli_ill->ill_lock);
22271 	}
22272 	if (ipsq != NULL)
22273 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22274 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22275 	ill_refrele(usesrc_ill);
22276 	return (err);
22277 }
22278 
22279 /*
22280  * comparison function used by avl.
22281  */
22282 static int
22283 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22284 {
22285 
22286 	uint_t index;
22287 
22288 	ASSERT(phyip != NULL && index_ptr != NULL);
22289 
22290 	index = *((uint_t *)index_ptr);
22291 	/*
22292 	 * let the phyint with the lowest index be on top.
22293 	 */
22294 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22295 		return (1);
22296 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22297 		return (-1);
22298 	return (0);
22299 }
22300 
22301 /*
22302  * comparison function used by avl.
22303  */
22304 static int
22305 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22306 {
22307 	ill_t *ill;
22308 	int res = 0;
22309 
22310 	ASSERT(phyip != NULL && name_ptr != NULL);
22311 
22312 	if (((phyint_t *)phyip)->phyint_illv4)
22313 		ill = ((phyint_t *)phyip)->phyint_illv4;
22314 	else
22315 		ill = ((phyint_t *)phyip)->phyint_illv6;
22316 	ASSERT(ill != NULL);
22317 
22318 	res = strcmp(ill->ill_name, (char *)name_ptr);
22319 	if (res > 0)
22320 		return (1);
22321 	else if (res < 0)
22322 		return (-1);
22323 	return (0);
22324 }
22325 /*
22326  * This function is called from ill_delete when the ill is being
22327  * unplumbed. We remove the reference from the phyint and we also
22328  * free the phyint when there are no more references to it.
22329  */
22330 static void
22331 ill_phyint_free(ill_t *ill)
22332 {
22333 	phyint_t *phyi;
22334 	phyint_t *next_phyint;
22335 	ipsq_t *cur_ipsq;
22336 	ip_stack_t	*ipst = ill->ill_ipst;
22337 
22338 	ASSERT(ill->ill_phyint != NULL);
22339 
22340 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22341 	phyi = ill->ill_phyint;
22342 	ill->ill_phyint = NULL;
22343 	/*
22344 	 * ill_init allocates a phyint always to store the copy
22345 	 * of flags relevant to phyint. At that point in time, we could
22346 	 * not assign the name and hence phyint_illv4/v6 could not be
22347 	 * initialized. Later in ipif_set_values, we assign the name to
22348 	 * the ill, at which point in time we assign phyint_illv4/v6.
22349 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22350 	 */
22351 	if (ill->ill_flags & ILLF_IPV6) {
22352 		phyi->phyint_illv6 = NULL;
22353 	} else {
22354 		phyi->phyint_illv4 = NULL;
22355 	}
22356 	/*
22357 	 * ipif_down removes it from the group when the last ipif goes
22358 	 * down.
22359 	 */
22360 	ASSERT(ill->ill_group == NULL);
22361 
22362 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22363 		return;
22364 
22365 	/*
22366 	 * Make sure this phyint was put in the list.
22367 	 */
22368 	if (phyi->phyint_ifindex > 0) {
22369 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22370 		    phyi);
22371 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22372 		    phyi);
22373 	}
22374 	/*
22375 	 * remove phyint from the ipsq list.
22376 	 */
22377 	cur_ipsq = phyi->phyint_ipsq;
22378 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22379 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22380 	} else {
22381 		next_phyint = cur_ipsq->ipsq_phyint_list;
22382 		while (next_phyint != NULL) {
22383 			if (next_phyint->phyint_ipsq_next == phyi) {
22384 				next_phyint->phyint_ipsq_next =
22385 				    phyi->phyint_ipsq_next;
22386 				break;
22387 			}
22388 			next_phyint = next_phyint->phyint_ipsq_next;
22389 		}
22390 		ASSERT(next_phyint != NULL);
22391 	}
22392 	IPSQ_DEC_REF(cur_ipsq, ipst);
22393 
22394 	if (phyi->phyint_groupname_len != 0) {
22395 		ASSERT(phyi->phyint_groupname != NULL);
22396 		mi_free(phyi->phyint_groupname);
22397 	}
22398 	mi_free(phyi);
22399 }
22400 
22401 /*
22402  * Attach the ill to the phyint structure which can be shared by both
22403  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22404  * function is called from ipif_set_values and ill_lookup_on_name (for
22405  * loopback) where we know the name of the ill. We lookup the ill and if
22406  * there is one present already with the name use that phyint. Otherwise
22407  * reuse the one allocated by ill_init.
22408  */
22409 static void
22410 ill_phyint_reinit(ill_t *ill)
22411 {
22412 	boolean_t isv6 = ill->ill_isv6;
22413 	phyint_t *phyi_old;
22414 	phyint_t *phyi;
22415 	avl_index_t where = 0;
22416 	ill_t	*ill_other = NULL;
22417 	ipsq_t	*ipsq;
22418 	ip_stack_t	*ipst = ill->ill_ipst;
22419 
22420 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22421 
22422 	phyi_old = ill->ill_phyint;
22423 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22424 	    phyi_old->phyint_illv6 == NULL));
22425 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22426 	    phyi_old->phyint_illv4 == NULL));
22427 	ASSERT(phyi_old->phyint_ifindex == 0);
22428 
22429 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22430 	    ill->ill_name, &where);
22431 
22432 	/*
22433 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22434 	 *    the global list of ills. So no other thread could have located
22435 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22436 	 * 2. Now locate the other protocol instance of this ill.
22437 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22438 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22439 	 *    of neither ill can change.
22440 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22441 	 *    other ill.
22442 	 * 5. Release all locks.
22443 	 */
22444 
22445 	/*
22446 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22447 	 * we are initializing IPv4.
22448 	 */
22449 	if (phyi != NULL) {
22450 		ill_other = (isv6) ? phyi->phyint_illv4 :
22451 		    phyi->phyint_illv6;
22452 		ASSERT(ill_other->ill_phyint != NULL);
22453 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22454 		    (!isv6 && ill_other->ill_isv6));
22455 		GRAB_ILL_LOCKS(ill, ill_other);
22456 		/*
22457 		 * We are potentially throwing away phyint_flags which
22458 		 * could be different from the one that we obtain from
22459 		 * ill_other->ill_phyint. But it is okay as we are assuming
22460 		 * that the state maintained within IP is correct.
22461 		 */
22462 		mutex_enter(&phyi->phyint_lock);
22463 		if (isv6) {
22464 			ASSERT(phyi->phyint_illv6 == NULL);
22465 			phyi->phyint_illv6 = ill;
22466 		} else {
22467 			ASSERT(phyi->phyint_illv4 == NULL);
22468 			phyi->phyint_illv4 = ill;
22469 		}
22470 		/*
22471 		 * This is a new ill, currently undergoing SLIFNAME
22472 		 * So we could not have joined an IPMP group until now.
22473 		 */
22474 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22475 		    phyi_old->phyint_groupname == NULL);
22476 
22477 		/*
22478 		 * This phyi_old is going away. Decref ipsq_refs and
22479 		 * assert it is zero. The ipsq itself will be freed in
22480 		 * ipsq_exit
22481 		 */
22482 		ipsq = phyi_old->phyint_ipsq;
22483 		IPSQ_DEC_REF(ipsq, ipst);
22484 		ASSERT(ipsq->ipsq_refs == 0);
22485 		/* Get the singleton phyint out of the ipsq list */
22486 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22487 		ipsq->ipsq_phyint_list = NULL;
22488 		phyi_old->phyint_illv4 = NULL;
22489 		phyi_old->phyint_illv6 = NULL;
22490 		mi_free(phyi_old);
22491 	} else {
22492 		mutex_enter(&ill->ill_lock);
22493 		/*
22494 		 * We don't need to acquire any lock, since
22495 		 * the ill is not yet visible globally  and we
22496 		 * have not yet released the ill_g_lock.
22497 		 */
22498 		phyi = phyi_old;
22499 		mutex_enter(&phyi->phyint_lock);
22500 		/* XXX We need a recovery strategy here. */
22501 		if (!phyint_assign_ifindex(phyi, ipst))
22502 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22503 
22504 		/* No IPMP group yet, thus the hook uses the ifindex */
22505 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22506 
22507 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22508 		    (void *)phyi, where);
22509 
22510 		(void) avl_find(&ipst->ips_phyint_g_list->
22511 		    phyint_list_avl_by_index,
22512 		    &phyi->phyint_ifindex, &where);
22513 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22514 		    (void *)phyi, where);
22515 	}
22516 
22517 	/*
22518 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22519 	 * pending mp is not affected because that is per ill basis.
22520 	 */
22521 	ill->ill_phyint = phyi;
22522 
22523 	/*
22524 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22525 	 * We do this here as when the first ipif was allocated,
22526 	 * ipif_allocate does not know the right interface index.
22527 	 */
22528 
22529 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22530 	/*
22531 	 * Now that the phyint's ifindex has been assigned, complete the
22532 	 * remaining
22533 	 */
22534 
22535 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22536 	if (ill->ill_isv6) {
22537 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22538 		    ill->ill_phyint->phyint_ifindex;
22539 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22540 	} else {
22541 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22542 	}
22543 
22544 	/*
22545 	 * Generate an event within the hooks framework to indicate that
22546 	 * a new interface has just been added to IP.  For this event to
22547 	 * be generated, the network interface must, at least, have an
22548 	 * ifindex assigned to it.
22549 	 *
22550 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22551 	 * that the ordering of delivered events to listeners matches the
22552 	 * order of them in the kernel.
22553 	 *
22554 	 * This function could be called from ill_lookup_on_name. In that case
22555 	 * the interface is loopback "lo", which will not generate a NIC event.
22556 	 */
22557 	if (ill->ill_name_length <= 2 ||
22558 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22559 		/*
22560 		 * Generate nic plumb event for ill_name even if
22561 		 * ipmp_hook_emulation is set. That avoids generating events
22562 		 * for the ill_names should ipmp_hook_emulation be turned on
22563 		 * later.
22564 		 */
22565 		ill_nic_info_plumb(ill, B_FALSE);
22566 	}
22567 	RELEASE_ILL_LOCKS(ill, ill_other);
22568 	mutex_exit(&phyi->phyint_lock);
22569 }
22570 
22571 /*
22572  * Allocate a NE_PLUMB nic info event and store in the ill.
22573  * If 'group' is set we do it for the group name, otherwise the ill name.
22574  * It will be sent when we leave the ipsq.
22575  */
22576 void
22577 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22578 {
22579 	phyint_t	*phyi = ill->ill_phyint;
22580 	char		*name;
22581 	int		namelen;
22582 
22583 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22584 
22585 	if (group) {
22586 		ASSERT(phyi->phyint_groupname_len != 0);
22587 		namelen = phyi->phyint_groupname_len;
22588 		name = phyi->phyint_groupname;
22589 	} else {
22590 		namelen = ill->ill_name_length;
22591 		name = ill->ill_name;
22592 	}
22593 
22594 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22595 }
22596 
22597 /*
22598  * Unhook the nic event message from the ill and enqueue it
22599  * into the nic event taskq.
22600  */
22601 void
22602 ill_nic_info_dispatch(ill_t *ill)
22603 {
22604 	hook_nic_event_t *info;
22605 
22606 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22607 
22608 	if ((info = ill->ill_nic_event_info) != NULL) {
22609 		if (ddi_taskq_dispatch(eventq_queue_nic,
22610 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22611 			ip2dbg(("ill_nic_info_dispatch: "
22612 			    "ddi_taskq_dispatch failed\n"));
22613 			if (info->hne_data != NULL)
22614 				kmem_free(info->hne_data, info->hne_datalen);
22615 			kmem_free(info, sizeof (hook_nic_event_t));
22616 		}
22617 		ill->ill_nic_event_info = NULL;
22618 	}
22619 }
22620 
22621 /*
22622  * Notify any downstream modules of the name of this interface.
22623  * An M_IOCTL is used even though we don't expect a successful reply.
22624  * Any reply message from the driver (presumably an M_IOCNAK) will
22625  * eventually get discarded somewhere upstream.  The message format is
22626  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22627  * to IP.
22628  */
22629 static void
22630 ip_ifname_notify(ill_t *ill, queue_t *q)
22631 {
22632 	mblk_t *mp1, *mp2;
22633 	struct iocblk *iocp;
22634 	struct lifreq *lifr;
22635 
22636 	mp1 = mkiocb(SIOCSLIFNAME);
22637 	if (mp1 == NULL)
22638 		return;
22639 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22640 	if (mp2 == NULL) {
22641 		freeb(mp1);
22642 		return;
22643 	}
22644 
22645 	mp1->b_cont = mp2;
22646 	iocp = (struct iocblk *)mp1->b_rptr;
22647 	iocp->ioc_count = sizeof (struct lifreq);
22648 
22649 	lifr = (struct lifreq *)mp2->b_rptr;
22650 	mp2->b_wptr += sizeof (struct lifreq);
22651 	bzero(lifr, sizeof (struct lifreq));
22652 
22653 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22654 	lifr->lifr_ppa = ill->ill_ppa;
22655 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22656 
22657 	putnext(q, mp1);
22658 }
22659 
22660 static int
22661 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22662 {
22663 	int err;
22664 	ip_stack_t	*ipst = ill->ill_ipst;
22665 
22666 	/* Set the obsolete NDD per-interface forwarding name. */
22667 	err = ill_set_ndd_name(ill);
22668 	if (err != 0) {
22669 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22670 		    err);
22671 	}
22672 
22673 	/* Tell downstream modules where they are. */
22674 	ip_ifname_notify(ill, q);
22675 
22676 	/*
22677 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22678 	 * Error cases are ENOMEM ...
22679 	 */
22680 	err = ill_dl_phys(ill, ipif, mp, q);
22681 
22682 	/*
22683 	 * If there is no IRE expiration timer running, get one started.
22684 	 * igmp and mld timers will be triggered by the first multicast
22685 	 */
22686 	if (ipst->ips_ip_ire_expire_id == 0) {
22687 		/*
22688 		 * acquire the lock and check again.
22689 		 */
22690 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22691 		if (ipst->ips_ip_ire_expire_id == 0) {
22692 			ipst->ips_ip_ire_expire_id = timeout(
22693 			    ip_trash_timer_expire, ipst,
22694 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22695 		}
22696 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22697 	}
22698 
22699 	if (ill->ill_isv6) {
22700 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22701 		if (ipst->ips_mld_slowtimeout_id == 0) {
22702 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22703 			    (void *)ipst,
22704 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22705 		}
22706 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22707 	} else {
22708 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22709 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22710 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22711 			    (void *)ipst,
22712 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22713 		}
22714 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22715 	}
22716 
22717 	return (err);
22718 }
22719 
22720 /*
22721  * Common routine for ppa and ifname setting. Should be called exclusive.
22722  *
22723  * Returns EINPROGRESS when mp has been consumed by queueing it on
22724  * ill_pending_mp and the ioctl will complete in ip_rput.
22725  *
22726  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22727  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22728  * For SLIFNAME, we pass these values back to the userland.
22729  */
22730 static int
22731 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22732 {
22733 	ill_t	*ill;
22734 	ipif_t	*ipif;
22735 	ipsq_t	*ipsq;
22736 	char	*ppa_ptr;
22737 	char	*old_ptr;
22738 	char	old_char;
22739 	int	error;
22740 	ip_stack_t	*ipst;
22741 
22742 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22743 	ASSERT(q->q_next != NULL);
22744 	ASSERT(interf_name != NULL);
22745 
22746 	ill = (ill_t *)q->q_ptr;
22747 	ipst = ill->ill_ipst;
22748 
22749 	ASSERT(ill->ill_ipst != NULL);
22750 	ASSERT(ill->ill_name[0] == '\0');
22751 	ASSERT(IAM_WRITER_ILL(ill));
22752 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22753 	ASSERT(ill->ill_ppa == UINT_MAX);
22754 
22755 	/* The ppa is sent down by ifconfig or is chosen */
22756 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22757 		return (EINVAL);
22758 	}
22759 
22760 	/*
22761 	 * make sure ppa passed in is same as ppa in the name.
22762 	 * This check is not made when ppa == UINT_MAX in that case ppa
22763 	 * in the name could be anything. System will choose a ppa and
22764 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22765 	 */
22766 	if (*new_ppa_ptr != UINT_MAX) {
22767 		/* stoi changes the pointer */
22768 		old_ptr = ppa_ptr;
22769 		/*
22770 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22771 		 * (they don't have an externally visible ppa).  We assign one
22772 		 * here so that we can manage the interface.  Note that in
22773 		 * the past this value was always 0 for DLPI 1 drivers.
22774 		 */
22775 		if (*new_ppa_ptr == 0)
22776 			*new_ppa_ptr = stoi(&old_ptr);
22777 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22778 			return (EINVAL);
22779 	}
22780 	/*
22781 	 * terminate string before ppa
22782 	 * save char at that location.
22783 	 */
22784 	old_char = ppa_ptr[0];
22785 	ppa_ptr[0] = '\0';
22786 
22787 	ill->ill_ppa = *new_ppa_ptr;
22788 	/*
22789 	 * Finish as much work now as possible before calling ill_glist_insert
22790 	 * which makes the ill globally visible and also merges it with the
22791 	 * other protocol instance of this phyint. The remaining work is
22792 	 * done after entering the ipsq which may happen sometime later.
22793 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22794 	 */
22795 	ipif = ill->ill_ipif;
22796 
22797 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22798 	ipif_assign_seqid(ipif);
22799 
22800 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22801 		ill->ill_flags |= ILLF_IPV4;
22802 
22803 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22804 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22805 
22806 	if (ill->ill_flags & ILLF_IPV6) {
22807 
22808 		ill->ill_isv6 = B_TRUE;
22809 		if (ill->ill_rq != NULL) {
22810 			ill->ill_rq->q_qinfo = &iprinitv6;
22811 			ill->ill_wq->q_qinfo = &ipwinitv6;
22812 		}
22813 
22814 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22815 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22816 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22817 		ipif->ipif_v6subnet = ipv6_all_zeros;
22818 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22819 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22820 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22821 		/*
22822 		 * point-to-point or Non-mulicast capable
22823 		 * interfaces won't do NUD unless explicitly
22824 		 * configured to do so.
22825 		 */
22826 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22827 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22828 			ill->ill_flags |= ILLF_NONUD;
22829 		}
22830 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22831 		if (ill->ill_flags & ILLF_NOARP) {
22832 			/*
22833 			 * Note: xresolv interfaces will eventually need
22834 			 * NOARP set here as well, but that will require
22835 			 * those external resolvers to have some
22836 			 * knowledge of that flag and act appropriately.
22837 			 * Not to be changed at present.
22838 			 */
22839 			ill->ill_flags &= ~ILLF_NOARP;
22840 		}
22841 		/*
22842 		 * Set the ILLF_ROUTER flag according to the global
22843 		 * IPv6 forwarding policy.
22844 		 */
22845 		if (ipst->ips_ipv6_forward != 0)
22846 			ill->ill_flags |= ILLF_ROUTER;
22847 	} else if (ill->ill_flags & ILLF_IPV4) {
22848 		ill->ill_isv6 = B_FALSE;
22849 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22850 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22851 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22852 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22853 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22854 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22855 		/*
22856 		 * Set the ILLF_ROUTER flag according to the global
22857 		 * IPv4 forwarding policy.
22858 		 */
22859 		if (ipst->ips_ip_g_forward != 0)
22860 			ill->ill_flags |= ILLF_ROUTER;
22861 	}
22862 
22863 	ASSERT(ill->ill_phyint != NULL);
22864 
22865 	/*
22866 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22867 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22868 	 */
22869 	if (!ill_allocate_mibs(ill))
22870 		return (ENOMEM);
22871 
22872 	/*
22873 	 * Pick a default sap until we get the DL_INFO_ACK back from
22874 	 * the driver.
22875 	 */
22876 	if (ill->ill_sap == 0) {
22877 		if (ill->ill_isv6)
22878 			ill->ill_sap  = IP6_DL_SAP;
22879 		else
22880 			ill->ill_sap  = IP_DL_SAP;
22881 	}
22882 
22883 	ill->ill_ifname_pending = 1;
22884 	ill->ill_ifname_pending_err = 0;
22885 
22886 	ill_refhold(ill);
22887 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
22888 	if ((error = ill_glist_insert(ill, interf_name,
22889 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22890 		ill->ill_ppa = UINT_MAX;
22891 		ill->ill_name[0] = '\0';
22892 		/*
22893 		 * undo null termination done above.
22894 		 */
22895 		ppa_ptr[0] = old_char;
22896 		rw_exit(&ipst->ips_ill_g_lock);
22897 		ill_refrele(ill);
22898 		return (error);
22899 	}
22900 
22901 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
22902 
22903 	/*
22904 	 * When we return the buffer pointed to by interf_name should contain
22905 	 * the same name as in ill_name.
22906 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
22907 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
22908 	 * so copy full name and update the ppa ptr.
22909 	 * When ppa passed in != UINT_MAX all values are correct just undo
22910 	 * null termination, this saves a bcopy.
22911 	 */
22912 	if (*new_ppa_ptr == UINT_MAX) {
22913 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
22914 		*new_ppa_ptr = ill->ill_ppa;
22915 	} else {
22916 		/*
22917 		 * undo null termination done above.
22918 		 */
22919 		ppa_ptr[0] = old_char;
22920 	}
22921 
22922 	/* Let SCTP know about this ILL */
22923 	sctp_update_ill(ill, SCTP_ILL_INSERT);
22924 
22925 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
22926 	    B_TRUE);
22927 
22928 	rw_exit(&ipst->ips_ill_g_lock);
22929 	ill_refrele(ill);
22930 	if (ipsq == NULL)
22931 		return (EINPROGRESS);
22932 
22933 	/*
22934 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
22935 	 */
22936 	if (ipsq->ipsq_current_ipif == NULL)
22937 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
22938 	else
22939 		ASSERT(ipsq->ipsq_current_ipif == ipif);
22940 
22941 	error = ipif_set_values_tail(ill, ipif, mp, q);
22942 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
22943 	if (error != 0 && error != EINPROGRESS) {
22944 		/*
22945 		 * restore previous values
22946 		 */
22947 		ill->ill_isv6 = B_FALSE;
22948 	}
22949 	return (error);
22950 }
22951 
22952 
22953 void
22954 ipif_init(ip_stack_t *ipst)
22955 {
22956 	hrtime_t hrt;
22957 	int i;
22958 
22959 	/*
22960 	 * Can't call drv_getparm here as it is too early in the boot.
22961 	 * As we use ipif_src_random just for picking a different
22962 	 * source address everytime, this need not be really random.
22963 	 */
22964 	hrt = gethrtime();
22965 	ipst->ips_ipif_src_random =
22966 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
22967 
22968 	for (i = 0; i < MAX_G_HEADS; i++) {
22969 		ipst->ips_ill_g_heads[i].ill_g_list_head =
22970 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
22971 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
22972 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
22973 	}
22974 
22975 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22976 	    ill_phyint_compare_index,
22977 	    sizeof (phyint_t),
22978 	    offsetof(struct phyint, phyint_avl_by_index));
22979 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22980 	    ill_phyint_compare_name,
22981 	    sizeof (phyint_t),
22982 	    offsetof(struct phyint, phyint_avl_by_name));
22983 }
22984 
22985 /*
22986  * Lookup the ipif corresponding to the onlink destination address. For
22987  * point-to-point interfaces, it matches with remote endpoint destination
22988  * address. For point-to-multipoint interfaces it only tries to match the
22989  * destination with the interface's subnet address. The longest, most specific
22990  * match is found to take care of such rare network configurations like -
22991  * le0: 129.146.1.1/16
22992  * le1: 129.146.2.2/24
22993  * It is used only by SO_DONTROUTE at the moment.
22994  */
22995 ipif_t *
22996 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
22997 {
22998 	ipif_t	*ipif, *best_ipif;
22999 	ill_t	*ill;
23000 	ill_walk_context_t ctx;
23001 
23002 	ASSERT(zoneid != ALL_ZONES);
23003 	best_ipif = NULL;
23004 
23005 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23006 	ill = ILL_START_WALK_V4(&ctx, ipst);
23007 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23008 		mutex_enter(&ill->ill_lock);
23009 		for (ipif = ill->ill_ipif; ipif != NULL;
23010 		    ipif = ipif->ipif_next) {
23011 			if (!IPIF_CAN_LOOKUP(ipif))
23012 				continue;
23013 			if (ipif->ipif_zoneid != zoneid &&
23014 			    ipif->ipif_zoneid != ALL_ZONES)
23015 				continue;
23016 			/*
23017 			 * Point-to-point case. Look for exact match with
23018 			 * destination address.
23019 			 */
23020 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23021 				if (ipif->ipif_pp_dst_addr == addr) {
23022 					ipif_refhold_locked(ipif);
23023 					mutex_exit(&ill->ill_lock);
23024 					rw_exit(&ipst->ips_ill_g_lock);
23025 					if (best_ipif != NULL)
23026 						ipif_refrele(best_ipif);
23027 					return (ipif);
23028 				}
23029 			} else if (ipif->ipif_subnet == (addr &
23030 			    ipif->ipif_net_mask)) {
23031 				/*
23032 				 * Point-to-multipoint case. Looping through to
23033 				 * find the most specific match. If there are
23034 				 * multiple best match ipif's then prefer ipif's
23035 				 * that are UP. If there is only one best match
23036 				 * ipif and it is DOWN we must still return it.
23037 				 */
23038 				if ((best_ipif == NULL) ||
23039 				    (ipif->ipif_net_mask >
23040 				    best_ipif->ipif_net_mask) ||
23041 				    ((ipif->ipif_net_mask ==
23042 				    best_ipif->ipif_net_mask) &&
23043 				    ((ipif->ipif_flags & IPIF_UP) &&
23044 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23045 					ipif_refhold_locked(ipif);
23046 					mutex_exit(&ill->ill_lock);
23047 					rw_exit(&ipst->ips_ill_g_lock);
23048 					if (best_ipif != NULL)
23049 						ipif_refrele(best_ipif);
23050 					best_ipif = ipif;
23051 					rw_enter(&ipst->ips_ill_g_lock,
23052 					    RW_READER);
23053 					mutex_enter(&ill->ill_lock);
23054 				}
23055 			}
23056 		}
23057 		mutex_exit(&ill->ill_lock);
23058 	}
23059 	rw_exit(&ipst->ips_ill_g_lock);
23060 	return (best_ipif);
23061 }
23062 
23063 
23064 /*
23065  * Save enough information so that we can recreate the IRE if
23066  * the interface goes down and then up.
23067  */
23068 static void
23069 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23070 {
23071 	mblk_t	*save_mp;
23072 
23073 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23074 	if (save_mp != NULL) {
23075 		ifrt_t	*ifrt;
23076 
23077 		save_mp->b_wptr += sizeof (ifrt_t);
23078 		ifrt = (ifrt_t *)save_mp->b_rptr;
23079 		bzero(ifrt, sizeof (ifrt_t));
23080 		ifrt->ifrt_type = ire->ire_type;
23081 		ifrt->ifrt_addr = ire->ire_addr;
23082 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23083 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23084 		ifrt->ifrt_mask = ire->ire_mask;
23085 		ifrt->ifrt_flags = ire->ire_flags;
23086 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23087 		mutex_enter(&ipif->ipif_saved_ire_lock);
23088 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23089 		ipif->ipif_saved_ire_mp = save_mp;
23090 		ipif->ipif_saved_ire_cnt++;
23091 		mutex_exit(&ipif->ipif_saved_ire_lock);
23092 	}
23093 }
23094 
23095 
23096 static void
23097 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23098 {
23099 	mblk_t	**mpp;
23100 	mblk_t	*mp;
23101 	ifrt_t	*ifrt;
23102 
23103 	/* Remove from ipif_saved_ire_mp list if it is there */
23104 	mutex_enter(&ipif->ipif_saved_ire_lock);
23105 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23106 	    mpp = &(*mpp)->b_cont) {
23107 		/*
23108 		 * On a given ipif, the triple of address, gateway and
23109 		 * mask is unique for each saved IRE (in the case of
23110 		 * ordinary interface routes, the gateway address is
23111 		 * all-zeroes).
23112 		 */
23113 		mp = *mpp;
23114 		ifrt = (ifrt_t *)mp->b_rptr;
23115 		if (ifrt->ifrt_addr == ire->ire_addr &&
23116 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23117 		    ifrt->ifrt_mask == ire->ire_mask) {
23118 			*mpp = mp->b_cont;
23119 			ipif->ipif_saved_ire_cnt--;
23120 			freeb(mp);
23121 			break;
23122 		}
23123 	}
23124 	mutex_exit(&ipif->ipif_saved_ire_lock);
23125 }
23126 
23127 
23128 /*
23129  * IP multirouting broadcast routes handling
23130  * Append CGTP broadcast IREs to regular ones created
23131  * at ifconfig time.
23132  */
23133 static void
23134 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23135 {
23136 	ire_t *ire_prim;
23137 
23138 	ASSERT(ire != NULL);
23139 	ASSERT(ire_dst != NULL);
23140 
23141 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23142 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23143 	if (ire_prim != NULL) {
23144 		/*
23145 		 * We are in the special case of broadcasts for
23146 		 * CGTP. We add an IRE_BROADCAST that holds
23147 		 * the RTF_MULTIRT flag, the destination
23148 		 * address of ire_dst and the low level
23149 		 * info of ire_prim. In other words, CGTP
23150 		 * broadcast is added to the redundant ipif.
23151 		 */
23152 		ipif_t *ipif_prim;
23153 		ire_t  *bcast_ire;
23154 
23155 		ipif_prim = ire_prim->ire_ipif;
23156 
23157 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23158 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23159 		    (void *)ire_dst, (void *)ire_prim,
23160 		    (void *)ipif_prim));
23161 
23162 		bcast_ire = ire_create(
23163 		    (uchar_t *)&ire->ire_addr,
23164 		    (uchar_t *)&ip_g_all_ones,
23165 		    (uchar_t *)&ire_dst->ire_src_addr,
23166 		    (uchar_t *)&ire->ire_gateway_addr,
23167 		    &ipif_prim->ipif_mtu,
23168 		    NULL,
23169 		    ipif_prim->ipif_rq,
23170 		    ipif_prim->ipif_wq,
23171 		    IRE_BROADCAST,
23172 		    ipif_prim,
23173 		    0,
23174 		    0,
23175 		    0,
23176 		    ire->ire_flags,
23177 		    &ire_uinfo_null,
23178 		    NULL,
23179 		    NULL,
23180 		    ipst);
23181 
23182 		if (bcast_ire != NULL) {
23183 
23184 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23185 			    B_FALSE) == 0) {
23186 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23187 				    "added bcast_ire %p\n",
23188 				    (void *)bcast_ire));
23189 
23190 				ipif_save_ire(bcast_ire->ire_ipif,
23191 				    bcast_ire);
23192 				ire_refrele(bcast_ire);
23193 			}
23194 		}
23195 		ire_refrele(ire_prim);
23196 	}
23197 }
23198 
23199 
23200 /*
23201  * IP multirouting broadcast routes handling
23202  * Remove the broadcast ire
23203  */
23204 static void
23205 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23206 {
23207 	ire_t *ire_dst;
23208 
23209 	ASSERT(ire != NULL);
23210 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23211 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23212 	if (ire_dst != NULL) {
23213 		ire_t *ire_prim;
23214 
23215 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23216 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23217 		if (ire_prim != NULL) {
23218 			ipif_t *ipif_prim;
23219 			ire_t  *bcast_ire;
23220 
23221 			ipif_prim = ire_prim->ire_ipif;
23222 
23223 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23224 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23225 			    (void *)ire_dst, (void *)ire_prim,
23226 			    (void *)ipif_prim));
23227 
23228 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23229 			    ire->ire_gateway_addr,
23230 			    IRE_BROADCAST,
23231 			    ipif_prim, ALL_ZONES,
23232 			    NULL,
23233 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23234 			    MATCH_IRE_MASK, ipst);
23235 
23236 			if (bcast_ire != NULL) {
23237 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23238 				    "looked up bcast_ire %p\n",
23239 				    (void *)bcast_ire));
23240 				ipif_remove_ire(bcast_ire->ire_ipif,
23241 				    bcast_ire);
23242 				ire_delete(bcast_ire);
23243 				ire_refrele(bcast_ire);
23244 			}
23245 			ire_refrele(ire_prim);
23246 		}
23247 		ire_refrele(ire_dst);
23248 	}
23249 }
23250 
23251 /*
23252  * IPsec hardware acceleration capabilities related functions.
23253  */
23254 
23255 /*
23256  * Free a per-ill IPsec capabilities structure.
23257  */
23258 static void
23259 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23260 {
23261 	if (capab->auth_hw_algs != NULL)
23262 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23263 	if (capab->encr_hw_algs != NULL)
23264 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23265 	if (capab->encr_algparm != NULL)
23266 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23267 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23268 }
23269 
23270 /*
23271  * Allocate a new per-ill IPsec capabilities structure. This structure
23272  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23273  * an array which specifies, for each algorithm, whether this algorithm
23274  * is supported by the ill or not.
23275  */
23276 static ill_ipsec_capab_t *
23277 ill_ipsec_capab_alloc(void)
23278 {
23279 	ill_ipsec_capab_t *capab;
23280 	uint_t nelems;
23281 
23282 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23283 	if (capab == NULL)
23284 		return (NULL);
23285 
23286 	/* we need one bit per algorithm */
23287 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23288 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23289 
23290 	/* allocate memory to store algorithm flags */
23291 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23292 	if (capab->encr_hw_algs == NULL)
23293 		goto nomem;
23294 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23295 	if (capab->auth_hw_algs == NULL)
23296 		goto nomem;
23297 	/*
23298 	 * Leave encr_algparm NULL for now since we won't need it half
23299 	 * the time
23300 	 */
23301 	return (capab);
23302 
23303 nomem:
23304 	ill_ipsec_capab_free(capab);
23305 	return (NULL);
23306 }
23307 
23308 /*
23309  * Resize capability array.  Since we're exclusive, this is OK.
23310  */
23311 static boolean_t
23312 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23313 {
23314 	ipsec_capab_algparm_t *nalp, *oalp;
23315 	uint32_t olen, nlen;
23316 
23317 	oalp = capab->encr_algparm;
23318 	olen = capab->encr_algparm_size;
23319 
23320 	if (oalp != NULL) {
23321 		if (algid < capab->encr_algparm_end)
23322 			return (B_TRUE);
23323 	}
23324 
23325 	nlen = (algid + 1) * sizeof (*nalp);
23326 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23327 	if (nalp == NULL)
23328 		return (B_FALSE);
23329 
23330 	if (oalp != NULL) {
23331 		bcopy(oalp, nalp, olen);
23332 		kmem_free(oalp, olen);
23333 	}
23334 	capab->encr_algparm = nalp;
23335 	capab->encr_algparm_size = nlen;
23336 	capab->encr_algparm_end = algid + 1;
23337 
23338 	return (B_TRUE);
23339 }
23340 
23341 /*
23342  * Compare the capabilities of the specified ill with the protocol
23343  * and algorithms specified by the SA passed as argument.
23344  * If they match, returns B_TRUE, B_FALSE if they do not match.
23345  *
23346  * The ill can be passed as a pointer to it, or by specifying its index
23347  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23348  *
23349  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23350  * packet is eligible for hardware acceleration, and by
23351  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23352  * to a particular ill.
23353  */
23354 boolean_t
23355 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23356     ipsa_t *sa, netstack_t *ns)
23357 {
23358 	boolean_t sa_isv6;
23359 	uint_t algid;
23360 	struct ill_ipsec_capab_s *cpp;
23361 	boolean_t need_refrele = B_FALSE;
23362 	ip_stack_t	*ipst = ns->netstack_ip;
23363 
23364 	if (ill == NULL) {
23365 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23366 		    NULL, NULL, NULL, ipst);
23367 		if (ill == NULL) {
23368 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23369 			return (B_FALSE);
23370 		}
23371 		need_refrele = B_TRUE;
23372 	}
23373 
23374 	/*
23375 	 * Use the address length specified by the SA to determine
23376 	 * if it corresponds to a IPv6 address, and fail the matching
23377 	 * if the isv6 flag passed as argument does not match.
23378 	 * Note: this check is used for SADB capability checking before
23379 	 * sending SA information to an ill.
23380 	 */
23381 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23382 	if (sa_isv6 != ill_isv6)
23383 		/* protocol mismatch */
23384 		goto done;
23385 
23386 	/*
23387 	 * Check if the ill supports the protocol, algorithm(s) and
23388 	 * key size(s) specified by the SA, and get the pointers to
23389 	 * the algorithms supported by the ill.
23390 	 */
23391 	switch (sa->ipsa_type) {
23392 
23393 	case SADB_SATYPE_ESP:
23394 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23395 			/* ill does not support ESP acceleration */
23396 			goto done;
23397 		cpp = ill->ill_ipsec_capab_esp;
23398 		algid = sa->ipsa_auth_alg;
23399 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23400 			goto done;
23401 		algid = sa->ipsa_encr_alg;
23402 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23403 			goto done;
23404 		if (algid < cpp->encr_algparm_end) {
23405 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23406 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23407 				goto done;
23408 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23409 				goto done;
23410 		}
23411 		break;
23412 
23413 	case SADB_SATYPE_AH:
23414 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23415 			/* ill does not support AH acceleration */
23416 			goto done;
23417 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23418 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23419 			goto done;
23420 		break;
23421 	}
23422 
23423 	if (need_refrele)
23424 		ill_refrele(ill);
23425 	return (B_TRUE);
23426 done:
23427 	if (need_refrele)
23428 		ill_refrele(ill);
23429 	return (B_FALSE);
23430 }
23431 
23432 
23433 /*
23434  * Add a new ill to the list of IPsec capable ills.
23435  * Called from ill_capability_ipsec_ack() when an ACK was received
23436  * indicating that IPsec hardware processing was enabled for an ill.
23437  *
23438  * ill must point to the ill for which acceleration was enabled.
23439  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23440  */
23441 static void
23442 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23443 {
23444 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23445 	uint_t sa_type;
23446 	uint_t ipproto;
23447 	ip_stack_t	*ipst = ill->ill_ipst;
23448 
23449 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23450 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23451 
23452 	switch (dl_cap) {
23453 	case DL_CAPAB_IPSEC_AH:
23454 		sa_type = SADB_SATYPE_AH;
23455 		ills = &ipst->ips_ipsec_capab_ills_ah;
23456 		ipproto = IPPROTO_AH;
23457 		break;
23458 	case DL_CAPAB_IPSEC_ESP:
23459 		sa_type = SADB_SATYPE_ESP;
23460 		ills = &ipst->ips_ipsec_capab_ills_esp;
23461 		ipproto = IPPROTO_ESP;
23462 		break;
23463 	}
23464 
23465 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23466 
23467 	/*
23468 	 * Add ill index to list of hardware accelerators. If
23469 	 * already in list, do nothing.
23470 	 */
23471 	for (cur_ill = *ills; cur_ill != NULL &&
23472 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23473 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23474 		;
23475 
23476 	if (cur_ill == NULL) {
23477 		/* if this is a new entry for this ill */
23478 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23479 		if (new_ill == NULL) {
23480 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23481 			return;
23482 		}
23483 
23484 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23485 		new_ill->ill_isv6 = ill->ill_isv6;
23486 		new_ill->next = *ills;
23487 		*ills = new_ill;
23488 	} else if (!sadb_resync) {
23489 		/* not resync'ing SADB and an entry exists for this ill */
23490 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23491 		return;
23492 	}
23493 
23494 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23495 
23496 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23497 		/*
23498 		 * IPsec module for protocol loaded, initiate dump
23499 		 * of the SADB to this ill.
23500 		 */
23501 		sadb_ill_download(ill, sa_type);
23502 }
23503 
23504 /*
23505  * Remove an ill from the list of IPsec capable ills.
23506  */
23507 static void
23508 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23509 {
23510 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
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 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23517 	    &ipst->ips_ipsec_capab_ills_esp;
23518 
23519 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23520 
23521 	prev_ill = NULL;
23522 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23523 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23524 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23525 		;
23526 	if (cur_ill == NULL) {
23527 		/* entry not found */
23528 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23529 		return;
23530 	}
23531 	if (prev_ill == NULL) {
23532 		/* entry at front of list */
23533 		*ills = NULL;
23534 	} else {
23535 		prev_ill->next = cur_ill->next;
23536 	}
23537 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23538 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23539 }
23540 
23541 /*
23542  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23543  * supporting the specified IPsec protocol acceleration.
23544  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23545  * We free the mblk and, if sa is non-null, release the held referece.
23546  */
23547 void
23548 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23549     netstack_t *ns)
23550 {
23551 	ipsec_capab_ill_t *ici, *cur_ici;
23552 	ill_t *ill;
23553 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23554 	ip_stack_t	*ipst = ns->netstack_ip;
23555 
23556 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23557 	    ipst->ips_ipsec_capab_ills_esp;
23558 
23559 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23560 
23561 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23562 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23563 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23564 
23565 		/*
23566 		 * Handle the case where the ill goes away while the SADB is
23567 		 * attempting to send messages.  If it's going away, it's
23568 		 * nuking its shadow SADB, so we don't care..
23569 		 */
23570 
23571 		if (ill == NULL)
23572 			continue;
23573 
23574 		if (sa != NULL) {
23575 			/*
23576 			 * Make sure capabilities match before
23577 			 * sending SA to ill.
23578 			 */
23579 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23580 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23581 				ill_refrele(ill);
23582 				continue;
23583 			}
23584 
23585 			mutex_enter(&sa->ipsa_lock);
23586 			sa->ipsa_flags |= IPSA_F_HW;
23587 			mutex_exit(&sa->ipsa_lock);
23588 		}
23589 
23590 		/*
23591 		 * Copy template message, and add it to the front
23592 		 * of the mblk ship list. We want to avoid holding
23593 		 * the ipsec_capab_ills_lock while sending the
23594 		 * message to the ills.
23595 		 *
23596 		 * The b_next and b_prev are temporarily used
23597 		 * to build a list of mblks to be sent down, and to
23598 		 * save the ill to which they must be sent.
23599 		 */
23600 		nmp = copymsg(mp);
23601 		if (nmp == NULL) {
23602 			ill_refrele(ill);
23603 			continue;
23604 		}
23605 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23606 		nmp->b_next = mp_ship_list;
23607 		mp_ship_list = nmp;
23608 		nmp->b_prev = (mblk_t *)ill;
23609 	}
23610 
23611 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23612 
23613 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23614 		/* restore the mblk to a sane state */
23615 		next_mp = nmp->b_next;
23616 		nmp->b_next = NULL;
23617 		ill = (ill_t *)nmp->b_prev;
23618 		nmp->b_prev = NULL;
23619 
23620 		ill_dlpi_send(ill, nmp);
23621 		ill_refrele(ill);
23622 	}
23623 
23624 	if (sa != NULL)
23625 		IPSA_REFRELE(sa);
23626 	freemsg(mp);
23627 }
23628 
23629 /*
23630  * Derive an interface id from the link layer address.
23631  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23632  */
23633 static boolean_t
23634 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23635 {
23636 	char		*addr;
23637 
23638 	if (phys_length != ETHERADDRL)
23639 		return (B_FALSE);
23640 
23641 	/* Form EUI-64 like address */
23642 	addr = (char *)&v6addr->s6_addr32[2];
23643 	bcopy((char *)phys_addr, addr, 3);
23644 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23645 	addr[3] = (char)0xff;
23646 	addr[4] = (char)0xfe;
23647 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23648 	return (B_TRUE);
23649 }
23650 
23651 /* ARGSUSED */
23652 static boolean_t
23653 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23654 {
23655 	return (B_FALSE);
23656 }
23657 
23658 /* ARGSUSED */
23659 static boolean_t
23660 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23661     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23662 {
23663 	/*
23664 	 * Multicast address mappings used over Ethernet/802.X.
23665 	 * This address is used as a base for mappings.
23666 	 */
23667 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23668 	    0x00, 0x00, 0x00};
23669 
23670 	/*
23671 	 * Extract low order 32 bits from IPv6 multicast address.
23672 	 * Or that into the link layer address, starting from the
23673 	 * second byte.
23674 	 */
23675 	*hw_start = 2;
23676 	v6_extract_mask->s6_addr32[0] = 0;
23677 	v6_extract_mask->s6_addr32[1] = 0;
23678 	v6_extract_mask->s6_addr32[2] = 0;
23679 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23680 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23681 	return (B_TRUE);
23682 }
23683 
23684 /*
23685  * Indicate by return value whether multicast is supported. If not,
23686  * this code should not touch/change any parameters.
23687  */
23688 /* ARGSUSED */
23689 static boolean_t
23690 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23691     uint32_t *hw_start, ipaddr_t *extract_mask)
23692 {
23693 	/*
23694 	 * Multicast address mappings used over Ethernet/802.X.
23695 	 * This address is used as a base for mappings.
23696 	 */
23697 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23698 	    0x00, 0x00, 0x00 };
23699 
23700 	if (phys_length != ETHERADDRL)
23701 		return (B_FALSE);
23702 
23703 	*extract_mask = htonl(0x007fffff);
23704 	*hw_start = 2;
23705 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23706 	return (B_TRUE);
23707 }
23708 
23709 /*
23710  * Derive IPoIB interface id from the link layer address.
23711  */
23712 static boolean_t
23713 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23714 {
23715 	char		*addr;
23716 
23717 	if (phys_length != 20)
23718 		return (B_FALSE);
23719 	addr = (char *)&v6addr->s6_addr32[2];
23720 	bcopy(phys_addr + 12, addr, 8);
23721 	/*
23722 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23723 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23724 	 * rules. In these cases, the IBA considers these GUIDs to be in
23725 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23726 	 * required; vendors are required not to assign global EUI-64's
23727 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23728 	 * of the interface identifier. Whether the GUID is in modified
23729 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23730 	 * bit set to 1.
23731 	 */
23732 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23733 	return (B_TRUE);
23734 }
23735 
23736 /*
23737  * Note on mapping from multicast IP addresses to IPoIB multicast link
23738  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23739  * The format of an IPoIB multicast address is:
23740  *
23741  *  4 byte QPN      Scope Sign.  Pkey
23742  * +--------------------------------------------+
23743  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23744  * +--------------------------------------------+
23745  *
23746  * The Scope and Pkey components are properties of the IBA port and
23747  * network interface. They can be ascertained from the broadcast address.
23748  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23749  */
23750 
23751 static boolean_t
23752 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23753     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23754 {
23755 	/*
23756 	 * Base IPoIB IPv6 multicast address used for mappings.
23757 	 * Does not contain the IBA scope/Pkey values.
23758 	 */
23759 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23760 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23761 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23762 
23763 	/*
23764 	 * Extract low order 80 bits from IPv6 multicast address.
23765 	 * Or that into the link layer address, starting from the
23766 	 * sixth byte.
23767 	 */
23768 	*hw_start = 6;
23769 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23770 
23771 	/*
23772 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23773 	 */
23774 	*(maddr + 5) = *(bphys_addr + 5);
23775 	*(maddr + 8) = *(bphys_addr + 8);
23776 	*(maddr + 9) = *(bphys_addr + 9);
23777 
23778 	v6_extract_mask->s6_addr32[0] = 0;
23779 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23780 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23781 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23782 	return (B_TRUE);
23783 }
23784 
23785 static boolean_t
23786 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23787     uint32_t *hw_start, ipaddr_t *extract_mask)
23788 {
23789 	/*
23790 	 * Base IPoIB IPv4 multicast address used for mappings.
23791 	 * Does not contain the IBA scope/Pkey values.
23792 	 */
23793 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23794 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23795 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23796 
23797 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23798 		return (B_FALSE);
23799 
23800 	/*
23801 	 * Extract low order 28 bits from IPv4 multicast address.
23802 	 * Or that into the link layer address, starting from the
23803 	 * sixteenth byte.
23804 	 */
23805 	*extract_mask = htonl(0x0fffffff);
23806 	*hw_start = 16;
23807 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23808 
23809 	/*
23810 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23811 	 */
23812 	*(maddr + 5) = *(bphys_addr + 5);
23813 	*(maddr + 8) = *(bphys_addr + 8);
23814 	*(maddr + 9) = *(bphys_addr + 9);
23815 	return (B_TRUE);
23816 }
23817 
23818 /*
23819  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23820  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23821  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23822  * the link-local address is preferred.
23823  */
23824 boolean_t
23825 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23826 {
23827 	ipif_t	*ipif;
23828 	ipif_t	*maybe_ipif = NULL;
23829 
23830 	mutex_enter(&ill->ill_lock);
23831 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23832 		mutex_exit(&ill->ill_lock);
23833 		if (ipifp != NULL)
23834 			*ipifp = NULL;
23835 		return (B_FALSE);
23836 	}
23837 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23838 		if (!IPIF_CAN_LOOKUP(ipif))
23839 			continue;
23840 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23841 		    ipif->ipif_zoneid != ALL_ZONES)
23842 			continue;
23843 		if ((ipif->ipif_flags & flags) != flags)
23844 			continue;
23845 
23846 		if (ipifp == NULL) {
23847 			mutex_exit(&ill->ill_lock);
23848 			ASSERT(maybe_ipif == NULL);
23849 			return (B_TRUE);
23850 		}
23851 		if (!ill->ill_isv6 ||
23852 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23853 			ipif_refhold_locked(ipif);
23854 			mutex_exit(&ill->ill_lock);
23855 			*ipifp = ipif;
23856 			return (B_TRUE);
23857 		}
23858 		if (maybe_ipif == NULL)
23859 			maybe_ipif = ipif;
23860 	}
23861 	if (ipifp != NULL) {
23862 		if (maybe_ipif != NULL)
23863 			ipif_refhold_locked(maybe_ipif);
23864 		*ipifp = maybe_ipif;
23865 	}
23866 	mutex_exit(&ill->ill_lock);
23867 	return (maybe_ipif != NULL);
23868 }
23869 
23870 /*
23871  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23872  */
23873 boolean_t
23874 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23875 {
23876 	ill_t *illg;
23877 	ip_stack_t	*ipst = ill->ill_ipst;
23878 
23879 	/*
23880 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23881 	 */
23882 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23883 		return (B_TRUE);
23884 	}
23885 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23886 	if (ill->ill_group == NULL) {
23887 		/* ill not in a group */
23888 		rw_exit(&ipst->ips_ill_g_lock);
23889 		return (B_FALSE);
23890 	}
23891 
23892 	/*
23893 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
23894 	 * group. We need to look for an ipif in the zone on all the ills in the
23895 	 * group.
23896 	 */
23897 	illg = ill->ill_group->illgrp_ill;
23898 	do {
23899 		/*
23900 		 * We don't call ipif_lookup_zoneid() on ill as we already know
23901 		 * that it's not there.
23902 		 */
23903 		if (illg != ill &&
23904 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
23905 			break;
23906 		}
23907 	} while ((illg = illg->ill_group_next) != NULL);
23908 	rw_exit(&ipst->ips_ill_g_lock);
23909 	return (illg != NULL);
23910 }
23911 
23912 /*
23913  * Check if this ill is only being used to send ICMP probes for IPMP
23914  */
23915 boolean_t
23916 ill_is_probeonly(ill_t *ill)
23917 {
23918 	/*
23919 	 * Check if the interface is FAILED, or INACTIVE
23920 	 */
23921 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
23922 		return (B_TRUE);
23923 
23924 	return (B_FALSE);
23925 }
23926 
23927 /*
23928  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
23929  * If a pointer to an ipif_t is returned then the caller will need to do
23930  * an ill_refrele().
23931  *
23932  * If there is no real interface which matches the ifindex, then it looks
23933  * for a group that has a matching index. In the case of a group match the
23934  * lifidx must be zero. We don't need emulate the logical interfaces
23935  * since IP Filter's use of netinfo doesn't use that.
23936  */
23937 ipif_t *
23938 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
23939     ip_stack_t *ipst)
23940 {
23941 	ipif_t *ipif;
23942 	ill_t *ill;
23943 
23944 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
23945 	    ipst);
23946 
23947 	if (ill == NULL) {
23948 		/* Fallback to group names only if hook_emulation set */
23949 		if (!ipst->ips_ipmp_hook_emulation)
23950 			return (NULL);
23951 
23952 		if (lifidx != 0)
23953 			return (NULL);
23954 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
23955 		if (ill == NULL)
23956 			return (NULL);
23957 	}
23958 
23959 	mutex_enter(&ill->ill_lock);
23960 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23961 		mutex_exit(&ill->ill_lock);
23962 		ill_refrele(ill);
23963 		return (NULL);
23964 	}
23965 
23966 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23967 		if (!IPIF_CAN_LOOKUP(ipif))
23968 			continue;
23969 		if (lifidx == ipif->ipif_id) {
23970 			ipif_refhold_locked(ipif);
23971 			break;
23972 		}
23973 	}
23974 
23975 	mutex_exit(&ill->ill_lock);
23976 	ill_refrele(ill);
23977 	return (ipif);
23978 }
23979 
23980 /*
23981  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
23982  * There is one exceptions IRE_BROADCAST are difficult to recreate,
23983  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
23984  * for details.
23985  */
23986 void
23987 ill_fastpath_flush(ill_t *ill)
23988 {
23989 	ip_stack_t *ipst = ill->ill_ipst;
23990 
23991 	nce_fastpath_list_dispatch(ill, NULL, NULL);
23992 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
23993 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
23994 }
23995 
23996 /*
23997  * Set the physical address information for `ill' to the contents of the
23998  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
23999  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24000  * EINPROGRESS will be returned.
24001  */
24002 int
24003 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24004 {
24005 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24006 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24007 
24008 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24009 
24010 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24011 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24012 		/* Changing DL_IPV6_TOKEN is not yet supported */
24013 		return (0);
24014 	}
24015 
24016 	/*
24017 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24018 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24019 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24020 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24021 	 */
24022 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24023 		freemsg(mp);
24024 		return (ENOMEM);
24025 	}
24026 
24027 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24028 
24029 	/*
24030 	 * If we can quiesce the ill, then set the address.  If not, then
24031 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24032 	 */
24033 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24034 	mutex_enter(&ill->ill_lock);
24035 	if (!ill_is_quiescent(ill)) {
24036 		/* call cannot fail since `conn_t *' argument is NULL */
24037 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24038 		    mp, ILL_DOWN);
24039 		mutex_exit(&ill->ill_lock);
24040 		return (EINPROGRESS);
24041 	}
24042 	mutex_exit(&ill->ill_lock);
24043 
24044 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24045 	return (0);
24046 }
24047 
24048 /*
24049  * Once the ill associated with `q' has quiesced, set its physical address
24050  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24051  * are passed (linked by b_cont), since we sometimes need to save two distinct
24052  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24053  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24054  * is quiesced, we know any stale IREs with the old address information have
24055  * already been removed, so we don't need to call ill_fastpath_flush().
24056  */
24057 /* ARGSUSED */
24058 static void
24059 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24060 {
24061 	ill_t		*ill = q->q_ptr;
24062 	mblk_t		*addrmp2 = unlinkb(addrmp);
24063 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24064 	uint_t		addrlen, addroff;
24065 
24066 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24067 
24068 	addroff	= dlindp->dl_addr_offset;
24069 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24070 
24071 	switch (dlindp->dl_data) {
24072 	case DL_IPV6_LINK_LAYER_ADDR:
24073 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24074 		freemsg(addrmp2);
24075 		break;
24076 
24077 	case DL_CURR_PHYS_ADDR:
24078 		freemsg(ill->ill_phys_addr_mp);
24079 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24080 		ill->ill_phys_addr_mp = addrmp;
24081 		ill->ill_phys_addr_length = addrlen;
24082 
24083 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24084 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24085 		else
24086 			freemsg(addrmp2);
24087 		break;
24088 	default:
24089 		ASSERT(0);
24090 	}
24091 
24092 	/*
24093 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24094 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24095 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24096 	 * brought up.
24097 	 */
24098 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24099 		ipsq_current_finish(ipsq);
24100 }
24101 
24102 /*
24103  * Helper routine for setting the ill_nd_lla fields.
24104  */
24105 void
24106 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24107 {
24108 	freemsg(ill->ill_nd_lla_mp);
24109 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24110 	ill->ill_nd_lla_mp = ndmp;
24111 	ill->ill_nd_lla_len = addrlen;
24112 }
24113 
24114 major_t IP_MAJ;
24115 #define	IP	"ip"
24116 
24117 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24118 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24119 
24120 /*
24121  * Issue REMOVEIF ioctls to have the loopback interfaces
24122  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24123  * the former going away when the user-level processes in the zone
24124  * are killed  * and the latter are cleaned up by the stream head
24125  * str_stack_shutdown callback that undoes all I_PLINKs.
24126  */
24127 void
24128 ip_loopback_cleanup(ip_stack_t *ipst)
24129 {
24130 	int error;
24131 	ldi_handle_t	lh = NULL;
24132 	ldi_ident_t	li = NULL;
24133 	int		rval;
24134 	cred_t		*cr;
24135 	struct strioctl iocb;
24136 	struct lifreq	lifreq;
24137 
24138 	IP_MAJ = ddi_name_to_major(IP);
24139 
24140 #ifdef NS_DEBUG
24141 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24142 	    ipst->ips_netstack->netstack_stackid);
24143 #endif
24144 
24145 	bzero(&lifreq, sizeof (lifreq));
24146 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24147 
24148 	error = ldi_ident_from_major(IP_MAJ, &li);
24149 	if (error) {
24150 #ifdef DEBUG
24151 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24152 		    error);
24153 #endif
24154 		return;
24155 	}
24156 
24157 	cr = zone_get_kcred(netstackid_to_zoneid(
24158 	    ipst->ips_netstack->netstack_stackid));
24159 	ASSERT(cr != NULL);
24160 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24161 	if (error) {
24162 #ifdef DEBUG
24163 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24164 		    error);
24165 #endif
24166 		goto out;
24167 	}
24168 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24169 	iocb.ic_timout = 15;
24170 	iocb.ic_len = sizeof (lifreq);
24171 	iocb.ic_dp = (char *)&lifreq;
24172 
24173 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24174 	/* LINTED - statement has no consequent */
24175 	if (error) {
24176 #ifdef NS_DEBUG
24177 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24178 		    "UDP6 error %d\n", error);
24179 #endif
24180 	}
24181 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24182 	lh = NULL;
24183 
24184 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24185 	if (error) {
24186 #ifdef NS_DEBUG
24187 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24188 		    error);
24189 #endif
24190 		goto out;
24191 	}
24192 
24193 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24194 	iocb.ic_timout = 15;
24195 	iocb.ic_len = sizeof (lifreq);
24196 	iocb.ic_dp = (char *)&lifreq;
24197 
24198 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24199 	/* LINTED - statement has no consequent */
24200 	if (error) {
24201 #ifdef NS_DEBUG
24202 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24203 		    "UDP error %d\n", error);
24204 #endif
24205 	}
24206 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24207 	lh = NULL;
24208 
24209 out:
24210 	/* Close layered handles */
24211 	if (lh)
24212 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24213 	if (li)
24214 		ldi_ident_release(li);
24215 
24216 	crfree(cr);
24217 }
24218 
24219 /*
24220  * This needs to be in-sync with nic_event_t definition
24221  */
24222 static const char *
24223 ill_hook_event2str(nic_event_t event)
24224 {
24225 	switch (event) {
24226 	case NE_PLUMB:
24227 		return ("PLUMB");
24228 	case NE_UNPLUMB:
24229 		return ("UNPLUMB");
24230 	case NE_UP:
24231 		return ("UP");
24232 	case NE_DOWN:
24233 		return ("DOWN");
24234 	case NE_ADDRESS_CHANGE:
24235 		return ("ADDRESS_CHANGE");
24236 	default:
24237 		return ("UNKNOWN");
24238 	}
24239 }
24240 
24241 static void
24242 ill_hook_event_destroy(ill_t *ill)
24243 {
24244 	hook_nic_event_t	*info;
24245 
24246 	if ((info = ill->ill_nic_event_info) != NULL) {
24247 		if (info->hne_data != NULL)
24248 			kmem_free(info->hne_data, info->hne_datalen);
24249 		kmem_free(info, sizeof (hook_nic_event_t));
24250 
24251 		ill->ill_nic_event_info = NULL;
24252 	}
24253 
24254 }
24255 
24256 boolean_t
24257 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24258     nic_event_data_t data, size_t datalen)
24259 {
24260 	ip_stack_t		*ipst = ill->ill_ipst;
24261 	hook_nic_event_t	*info;
24262 	const char		*str = NULL;
24263 
24264 	/* destroy nic event info if it exists */
24265 	if ((info = ill->ill_nic_event_info) != NULL) {
24266 		str = ill_hook_event2str(info->hne_event);
24267 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24268 		    "attached for %s\n", str, ill->ill_name));
24269 		ill_hook_event_destroy(ill);
24270 	}
24271 
24272 	/* create a new nic event info */
24273 	if ((info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP)) == NULL)
24274 		goto fail;
24275 
24276 	ill->ill_nic_event_info = info;
24277 
24278 	if (event == NE_UNPLUMB)
24279 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
24280 	else
24281 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24282 	info->hne_lif = lif;
24283 	info->hne_event = event;
24284 	info->hne_family = ill->ill_isv6 ?
24285 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24286 	info->hne_data = NULL;
24287 	info->hne_datalen = 0;
24288 
24289 	if (data != NULL && datalen != 0) {
24290 		info->hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24291 		if (info->hne_data != NULL) {
24292 			bcopy(data, info->hne_data, datalen);
24293 			info->hne_datalen = datalen;
24294 		} else {
24295 			ill_hook_event_destroy(ill);
24296 			goto fail;
24297 		}
24298 	}
24299 
24300 	return (B_TRUE);
24301 fail:
24302 	str = ill_hook_event2str(event);
24303 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24304 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24305 	return (B_FALSE);
24306 }
24307