xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 
48 #include <sys/kmem.h>
49 #include <sys/systm.h>
50 #include <sys/param.h>
51 #include <sys/socket.h>
52 #include <sys/isa_defs.h>
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/if_types.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
58 #include <sys/sockio.h>
59 #include <netinet/in.h>
60 #include <netinet/ip6.h>
61 #include <netinet/icmp6.h>
62 #include <netinet/igmp_var.h>
63 #include <sys/strsun.h>
64 #include <sys/policy.h>
65 #include <sys/ethernet.h>
66 
67 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
68 #include <inet/mi.h>
69 #include <inet/nd.h>
70 #include <inet/arp.h>
71 #include <inet/mib2.h>
72 #include <inet/ip.h>
73 #include <inet/ip6.h>
74 #include <inet/ip6_asp.h>
75 #include <inet/tcp.h>
76 #include <inet/ip_multi.h>
77 #include <inet/ip_ire.h>
78 #include <inet/ip_ftable.h>
79 #include <inet/ip_rts.h>
80 #include <inet/ip_ndp.h>
81 #include <inet/ip_if.h>
82 #include <inet/ip_impl.h>
83 #include <inet/tun.h>
84 #include <inet/sctp_ip.h>
85 #include <inet/ip_netinfo.h>
86 #include <inet/mib2.h>
87 
88 #include <net/pfkeyv2.h>
89 #include <inet/ipsec_info.h>
90 #include <inet/sadb.h>
91 #include <inet/ipsec_impl.h>
92 #include <sys/iphada.h>
93 
94 
95 #include <netinet/igmp.h>
96 #include <inet/ip_listutils.h>
97 #include <inet/ipclassifier.h>
98 #include <sys/mac.h>
99 
100 #include <sys/systeminfo.h>
101 #include <sys/bootconf.h>
102 
103 #include <sys/tsol/tndb.h>
104 #include <sys/tsol/tnet.h>
105 
106 /* The character which tells where the ill_name ends */
107 #define	IPIF_SEPARATOR_CHAR	':'
108 
109 /* IP ioctl function table entry */
110 typedef struct ipft_s {
111 	int	ipft_cmd;
112 	pfi_t	ipft_pfi;
113 	int	ipft_min_size;
114 	int	ipft_flags;
115 } ipft_t;
116 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
117 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
118 
119 typedef struct ip_sock_ar_s {
120 	union {
121 		area_t	ip_sock_area;
122 		ared_t	ip_sock_ared;
123 		areq_t	ip_sock_areq;
124 	} ip_sock_ar_u;
125 	queue_t	*ip_sock_ar_q;
126 } ip_sock_ar_t;
127 
128 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
129 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
130 		    char *value, caddr_t cp, cred_t *ioc_cr);
131 
132 static boolean_t ill_is_quiescent(ill_t *);
133 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
134 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
135 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
140     queue_t *q, mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
142     mblk_t *mp);
143 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
148     int ioccmd, struct linkblk *li, boolean_t doconsist);
149 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
150 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
151 static void	ipsq_flush(ill_t *ill);
152 
153 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
154     queue_t *q, mblk_t *mp, boolean_t need_up);
155 static void	ipsq_delete(ipsq_t *);
156 
157 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
158 		    boolean_t initialize);
159 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
160 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
161 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
162 		    boolean_t isv6);
163 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
164 static void	ipif_delete_cache_ire(ire_t *, char *);
165 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
166 static void	ipif_free(ipif_t *ipif);
167 static void	ipif_free_tail(ipif_t *ipif);
168 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
169 static void	ipif_multicast_down(ipif_t *ipif);
170 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
171 static void	ipif_set_default(ipif_t *ipif);
172 static int	ipif_set_values(queue_t *q, mblk_t *mp,
173     char *interf_name, uint_t *ppa);
174 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
175     queue_t *q);
176 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
177     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
178     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
179 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
180 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
181 
182 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
183 static int	ill_arp_off(ill_t *ill);
184 static int	ill_arp_on(ill_t *ill);
185 static void	ill_delete_interface_type(ill_if_t *);
186 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
187 static void	ill_dl_down(ill_t *ill);
188 static void	ill_down(ill_t *ill);
189 static void	ill_downi(ire_t *ire, char *ill_arg);
190 static void	ill_free_mib(ill_t *ill);
191 static void	ill_glist_delete(ill_t *);
192 static boolean_t ill_has_usable_ipif(ill_t *);
193 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
194 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
195 static void	ill_phyint_free(ill_t *ill);
196 static void	ill_phyint_reinit(ill_t *ill);
197 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
198 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
199 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
200 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
201 static void	ill_stq_cache_delete(ire_t *, char *);
202 
203 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
204 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
206     in6_addr_t *);
207 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     ipaddr_t *);
209 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
210 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
211     in6_addr_t *);
212 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     ipaddr_t *);
214 
215 static void	ipif_save_ire(ipif_t *, ire_t *);
216 static void	ipif_remove_ire(ipif_t *, ire_t *);
217 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
218 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
219 
220 /*
221  * Per-ill IPsec capabilities management.
222  */
223 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
224 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
225 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
226 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
227 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
228 static void ill_capability_proto(ill_t *, int, mblk_t *);
229 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
230     boolean_t);
231 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
234 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
236 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
238 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
239     dl_capability_sub_t *);
240 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
241 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
242 static void ill_capability_lso_reset(ill_t *, mblk_t **);
243 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
245 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
246 static void	ill_capability_dls_disable(ill_t *);
247 
248 static void	illgrp_cache_delete(ire_t *, char *);
249 static void	illgrp_delete(ill_t *ill);
250 static void	illgrp_reset_schednext(ill_t *ill);
251 
252 static ill_t	*ill_prev_usesrc(ill_t *);
253 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
254 static void	ill_disband_usesrc_group(ill_t *);
255 
256 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
257 
258 #ifdef DEBUG
259 static	void	ill_trace_cleanup(const ill_t *);
260 static	void	ipif_trace_cleanup(const ipif_t *);
261 #endif
262 
263 /*
264  * if we go over the memory footprint limit more than once in this msec
265  * interval, we'll start pruning aggressively.
266  */
267 int ip_min_frag_prune_time = 0;
268 
269 /*
270  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
271  * and the IPsec DOI
272  */
273 #define	MAX_IPSEC_ALGS	256
274 
275 #define	BITSPERBYTE	8
276 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
277 
278 #define	IPSEC_ALG_ENABLE(algs, algid) \
279 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
280 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
281 
282 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
283 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
284 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
285 
286 typedef uint8_t ipsec_capab_elem_t;
287 
288 /*
289  * Per-algorithm parameters.  Note that at present, only encryption
290  * algorithms have variable keysize (IKE does not provide a way to negotiate
291  * auth algorithm keysize).
292  *
293  * All sizes here are in bits.
294  */
295 typedef struct
296 {
297 	uint16_t	minkeylen;
298 	uint16_t	maxkeylen;
299 } ipsec_capab_algparm_t;
300 
301 /*
302  * Per-ill capabilities.
303  */
304 struct ill_ipsec_capab_s {
305 	ipsec_capab_elem_t *encr_hw_algs;
306 	ipsec_capab_elem_t *auth_hw_algs;
307 	uint32_t algs_size;	/* size of _hw_algs in bytes */
308 	/* algorithm key lengths */
309 	ipsec_capab_algparm_t *encr_algparm;
310 	uint32_t encr_algparm_size;
311 	uint32_t encr_algparm_end;
312 };
313 
314 /*
315  * The field values are larger than strictly necessary for simple
316  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
317  */
318 static area_t	ip_area_template = {
319 	AR_ENTRY_ADD,			/* area_cmd */
320 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
321 					/* area_name_offset */
322 	/* area_name_length temporarily holds this structure length */
323 	sizeof (area_t),			/* area_name_length */
324 	IP_ARP_PROTO_TYPE,		/* area_proto */
325 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
326 	IP_ADDR_LEN,			/* area_proto_addr_length */
327 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
328 					/* area_proto_mask_offset */
329 	0,				/* area_flags */
330 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
331 					/* area_hw_addr_offset */
332 	/* Zero length hw_addr_length means 'use your idea of the address' */
333 	0				/* area_hw_addr_length */
334 };
335 
336 /*
337  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
338  * support
339  */
340 static area_t	ip6_area_template = {
341 	AR_ENTRY_ADD,			/* area_cmd */
342 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
343 					/* area_name_offset */
344 	/* area_name_length temporarily holds this structure length */
345 	sizeof (area_t),			/* area_name_length */
346 	IP_ARP_PROTO_TYPE,		/* area_proto */
347 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
348 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
349 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
350 					/* area_proto_mask_offset */
351 	0,				/* area_flags */
352 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
353 					/* area_hw_addr_offset */
354 	/* Zero length hw_addr_length means 'use your idea of the address' */
355 	0				/* area_hw_addr_length */
356 };
357 
358 static ared_t	ip_ared_template = {
359 	AR_ENTRY_DELETE,
360 	sizeof (ared_t) + IP_ADDR_LEN,
361 	sizeof (ared_t),
362 	IP_ARP_PROTO_TYPE,
363 	sizeof (ared_t),
364 	IP_ADDR_LEN,
365 	0
366 };
367 
368 static ared_t	ip6_ared_template = {
369 	AR_ENTRY_DELETE,
370 	sizeof (ared_t) + IPV6_ADDR_LEN,
371 	sizeof (ared_t),
372 	IP_ARP_PROTO_TYPE,
373 	sizeof (ared_t),
374 	IPV6_ADDR_LEN,
375 	0
376 };
377 
378 /*
379  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
380  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
381  * areq is used).
382  */
383 static areq_t	ip_areq_template = {
384 	AR_ENTRY_QUERY,			/* cmd */
385 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
386 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
387 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
388 	sizeof (areq_t),			/* target addr offset */
389 	IP_ADDR_LEN,			/* target addr_length */
390 	0,				/* flags */
391 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
392 	IP_ADDR_LEN,			/* sender addr length */
393 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
394 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
395 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
396 	/* anything else filled in by the code */
397 };
398 
399 static arc_t	ip_aru_template = {
400 	AR_INTERFACE_UP,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_ard_template = {
406 	AR_INTERFACE_DOWN,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aron_template = {
412 	AR_INTERFACE_ON,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 static arc_t	ip_aroff_template = {
418 	AR_INTERFACE_OFF,
419 	sizeof (arc_t),		/* Name offset */
420 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
421 };
422 
423 static arma_t	ip_arma_multi_template = {
424 	AR_MAPPING_ADD,
425 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
426 				/* Name offset */
427 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
428 	IP_ARP_PROTO_TYPE,
429 	sizeof (arma_t),			/* proto_addr_offset */
430 	IP_ADDR_LEN,				/* proto_addr_length */
431 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
432 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
433 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
434 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
435 	IP_MAX_HW_LEN,				/* hw_addr_length */
436 	0,					/* hw_mapping_start */
437 };
438 
439 static ipft_t	ip_ioctl_ftbl[] = {
440 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
441 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
444 		IPFT_F_NO_REPLY },
445 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
446 	{ 0 }
447 };
448 
449 /* Simple ICMP IP Header Template */
450 static ipha_t icmp_ipha = {
451 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
452 };
453 
454 /* Flag descriptors for ip_ipif_report */
455 static nv_t	ipif_nv_tbl[] = {
456 	{ IPIF_UP,		"UP" },
457 	{ IPIF_BROADCAST,	"BROADCAST" },
458 	{ ILLF_DEBUG,		"DEBUG" },
459 	{ PHYI_LOOPBACK,	"LOOPBACK" },
460 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
461 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
462 	{ PHYI_RUNNING,		"RUNNING" },
463 	{ ILLF_NOARP,		"NOARP" },
464 	{ PHYI_PROMISC,		"PROMISC" },
465 	{ PHYI_ALLMULTI,	"ALLMULTI" },
466 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
467 	{ ILLF_MULTICAST,	"MULTICAST" },
468 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
469 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
470 	{ IPIF_DHCPRUNNING,	"DHCP" },
471 	{ IPIF_PRIVATE,		"PRIVATE" },
472 	{ IPIF_NOXMIT,		"NOXMIT" },
473 	{ IPIF_NOLOCAL,		"NOLOCAL" },
474 	{ IPIF_DEPRECATED,	"DEPRECATED" },
475 	{ IPIF_PREFERRED,	"PREFERRED" },
476 	{ IPIF_TEMPORARY,	"TEMPORARY" },
477 	{ IPIF_ADDRCONF,	"ADDRCONF" },
478 	{ PHYI_VIRTUAL,		"VIRTUAL" },
479 	{ ILLF_ROUTER,		"ROUTER" },
480 	{ ILLF_NONUD,		"NONUD" },
481 	{ IPIF_ANYCAST,		"ANYCAST" },
482 	{ ILLF_NORTEXCH,	"NORTEXCH" },
483 	{ ILLF_IPV4,		"IPV4" },
484 	{ ILLF_IPV6,		"IPV6" },
485 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
486 	{ PHYI_FAILED,		"FAILED" },
487 	{ PHYI_STANDBY,		"STANDBY" },
488 	{ PHYI_INACTIVE,	"INACTIVE" },
489 	{ PHYI_OFFLINE,		"OFFLINE" },
490 };
491 
492 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
493 
494 static ip_m_t	ip_m_tbl[] = {
495 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_ether_v6intfid },
497 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_ether_v6intfid },
505 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
506 	    ip_ib_v6intfid },
507 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
508 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
509 	    ip_nodef_v6intfid }
510 };
511 
512 static ill_t	ill_null;		/* Empty ILL for init. */
513 char	ipif_loopback_name[] = "lo0";
514 static char *ipv4_forward_suffix = ":ip_forwarding";
515 static char *ipv6_forward_suffix = ":ip6_forwarding";
516 static	sin6_t	sin6_null;	/* Zero address for quick clears */
517 static	sin_t	sin_null;	/* Zero address for quick clears */
518 
519 /* When set search for unused ipif_seqid */
520 static ipif_t	ipif_zero;
521 
522 /*
523  * ppa arena is created after these many
524  * interfaces have been plumbed.
525  */
526 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
527 
528 /*
529  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
530  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
531  * set through platform specific code (Niagara/Ontario).
532  */
533 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
534 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
535 
536 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
537 
538 static uint_t
539 ipif_rand(ip_stack_t *ipst)
540 {
541 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
542 	    12345;
543 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
544 }
545 
546 /*
547  * Allocate per-interface mibs.
548  * Returns true if ok. False otherwise.
549  *  ipsq  may not yet be allocated (loopback case ).
550  */
551 static boolean_t
552 ill_allocate_mibs(ill_t *ill)
553 {
554 	/* Already allocated? */
555 	if (ill->ill_ip_mib != NULL) {
556 		if (ill->ill_isv6)
557 			ASSERT(ill->ill_icmp6_mib != NULL);
558 		return (B_TRUE);
559 	}
560 
561 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
562 	    KM_NOSLEEP);
563 	if (ill->ill_ip_mib == NULL) {
564 		return (B_FALSE);
565 	}
566 
567 	/* Setup static information */
568 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
569 	    sizeof (mib2_ipIfStatsEntry_t));
570 	if (ill->ill_isv6) {
571 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
573 		    sizeof (mib2_ipv6AddrEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
575 		    sizeof (mib2_ipv6RouteEntry_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
577 		    sizeof (mib2_ipv6NetToMediaEntry_t));
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
579 		    sizeof (ipv6_member_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
581 		    sizeof (ipv6_grpsrc_t));
582 	} else {
583 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
585 		    sizeof (mib2_ipAddrEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
587 		    sizeof (mib2_ipRouteEntry_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
589 		    sizeof (mib2_ipNetToMediaEntry_t));
590 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
591 		    sizeof (ip_member_t));
592 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
593 		    sizeof (ip_grpsrc_t));
594 
595 		/*
596 		 * For a v4 ill, we are done at this point, because per ill
597 		 * icmp mibs are only used for v6.
598 		 */
599 		return (B_TRUE);
600 	}
601 
602 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
603 	    KM_NOSLEEP);
604 	if (ill->ill_icmp6_mib == NULL) {
605 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
606 		ill->ill_ip_mib = NULL;
607 		return (B_FALSE);
608 	}
609 	/* static icmp info */
610 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
611 	    sizeof (mib2_ipv6IfIcmpEntry_t);
612 	/*
613 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
614 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
615 	 * -> ill_phyint_reinit
616 	 */
617 	return (B_TRUE);
618 }
619 
620 /*
621  * Common code for preparation of ARP commands.  Two points to remember:
622  * 	1) The ill_name is tacked on at the end of the allocated space so
623  *	   the templates name_offset field must contain the total space
624  *	   to allocate less the name length.
625  *
626  *	2) The templates name_length field should contain the *template*
627  *	   length.  We use it as a parameter to bcopy() and then write
628  *	   the real ill_name_length into the name_length field of the copy.
629  * (Always called as writer.)
630  */
631 mblk_t *
632 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
633 {
634 	arc_t	*arc = (arc_t *)template;
635 	char	*cp;
636 	int	len;
637 	mblk_t	*mp;
638 	uint_t	name_length = ill->ill_name_length;
639 	uint_t	template_len = arc->arc_name_length;
640 
641 	len = arc->arc_name_offset + name_length;
642 	mp = allocb(len, BPRI_HI);
643 	if (mp == NULL)
644 		return (NULL);
645 	cp = (char *)mp->b_rptr;
646 	mp->b_wptr = (uchar_t *)&cp[len];
647 	if (template_len)
648 		bcopy(template, cp, template_len);
649 	if (len > template_len)
650 		bzero(&cp[template_len], len - template_len);
651 	mp->b_datap->db_type = M_PROTO;
652 
653 	arc = (arc_t *)cp;
654 	arc->arc_name_length = name_length;
655 	cp = (char *)arc + arc->arc_name_offset;
656 	bcopy(ill->ill_name, cp, name_length);
657 
658 	if (addr) {
659 		area_t	*area = (area_t *)mp->b_rptr;
660 
661 		cp = (char *)area + area->area_proto_addr_offset;
662 		bcopy(addr, cp, area->area_proto_addr_length);
663 		if (area->area_cmd == AR_ENTRY_ADD) {
664 			cp = (char *)area;
665 			len = area->area_proto_addr_length;
666 			if (area->area_proto_mask_offset)
667 				cp += area->area_proto_mask_offset;
668 			else
669 				cp += area->area_proto_addr_offset + len;
670 			while (len-- > 0)
671 				*cp++ = (char)~0;
672 		}
673 	}
674 	return (mp);
675 }
676 
677 mblk_t *
678 ipif_area_alloc(ipif_t *ipif)
679 {
680 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
681 	    (char *)&ipif->ipif_lcl_addr));
682 }
683 
684 mblk_t *
685 ipif_ared_alloc(ipif_t *ipif)
686 {
687 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
688 	    (char *)&ipif->ipif_lcl_addr));
689 }
690 
691 mblk_t *
692 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
693 {
694 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
695 	    (char *)&addr));
696 }
697 
698 /*
699  * Completely vaporize a lower level tap and all associated interfaces.
700  * ill_delete is called only out of ip_close when the device control
701  * stream is being closed.
702  */
703 void
704 ill_delete(ill_t *ill)
705 {
706 	ipif_t	*ipif;
707 	ill_t	*prev_ill;
708 	ip_stack_t	*ipst = ill->ill_ipst;
709 
710 	/*
711 	 * ill_delete may be forcibly entering the ipsq. The previous
712 	 * ioctl may not have completed and may need to be aborted.
713 	 * ipsq_flush takes care of it. If we don't need to enter the
714 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
715 	 * ill_delete_tail is sufficient.
716 	 */
717 	ipsq_flush(ill);
718 
719 	/*
720 	 * Nuke all interfaces.  ipif_free will take down the interface,
721 	 * remove it from the list, and free the data structure.
722 	 * Walk down the ipif list and remove the logical interfaces
723 	 * first before removing the main ipif. We can't unplumb
724 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
725 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
726 	 * POINTOPOINT.
727 	 *
728 	 * If ill_ipif was not properly initialized (i.e low on memory),
729 	 * then no interfaces to clean up. In this case just clean up the
730 	 * ill.
731 	 */
732 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
733 		ipif_free(ipif);
734 
735 	/*
736 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
737 	 * So nobody can be using this mp now. Free the mp allocated for
738 	 * honoring ILLF_NOARP
739 	 */
740 	freemsg(ill->ill_arp_on_mp);
741 	ill->ill_arp_on_mp = NULL;
742 
743 	/* Clean up msgs on pending upcalls for mrouted */
744 	reset_mrt_ill(ill);
745 
746 	/*
747 	 * ipif_free -> reset_conn_ipif will remove all multicast
748 	 * references for IPv4. For IPv6, we need to do it here as
749 	 * it points only at ills.
750 	 */
751 	reset_conn_ill(ill);
752 
753 	/*
754 	 * ill_down will arrange to blow off any IRE's dependent on this
755 	 * ILL, and shut down fragmentation reassembly.
756 	 */
757 	ill_down(ill);
758 
759 	/* Let SCTP know, so that it can remove this from its list. */
760 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
761 
762 	/*
763 	 * If an address on this ILL is being used as a source address then
764 	 * clear out the pointers in other ILLs that point to this ILL.
765 	 */
766 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
767 	if (ill->ill_usesrc_grp_next != NULL) {
768 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
769 			ill_disband_usesrc_group(ill);
770 		} else {	/* consumer of the usesrc ILL */
771 			prev_ill = ill_prev_usesrc(ill);
772 			prev_ill->ill_usesrc_grp_next =
773 			    ill->ill_usesrc_grp_next;
774 		}
775 	}
776 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
777 }
778 
779 static void
780 ipif_non_duplicate(ipif_t *ipif)
781 {
782 	ill_t *ill = ipif->ipif_ill;
783 	mutex_enter(&ill->ill_lock);
784 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
785 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
786 		ASSERT(ill->ill_ipif_dup_count > 0);
787 		ill->ill_ipif_dup_count--;
788 	}
789 	mutex_exit(&ill->ill_lock);
790 }
791 
792 /*
793  * ill_delete_tail is called from ip_modclose after all references
794  * to the closing ill are gone. The wait is done in ip_modclose
795  */
796 void
797 ill_delete_tail(ill_t *ill)
798 {
799 	mblk_t	**mpp;
800 	ipif_t	*ipif;
801 	ip_stack_t	*ipst = ill->ill_ipst;
802 
803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
804 		ipif_non_duplicate(ipif);
805 		ipif_down_tail(ipif);
806 	}
807 
808 	ASSERT(ill->ill_ipif_dup_count == 0 &&
809 	    ill->ill_arp_down_mp == NULL &&
810 	    ill->ill_arp_del_mapping_mp == NULL);
811 
812 	/*
813 	 * If polling capability is enabled (which signifies direct
814 	 * upcall into IP and driver has ill saved as a handle),
815 	 * we need to make sure that unbind has completed before we
816 	 * let the ill disappear and driver no longer has any reference
817 	 * to this ill.
818 	 */
819 	mutex_enter(&ill->ill_lock);
820 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
821 		cv_wait(&ill->ill_cv, &ill->ill_lock);
822 	mutex_exit(&ill->ill_lock);
823 
824 	/*
825 	 * Clean up polling and soft ring capabilities
826 	 */
827 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
828 		ill_capability_dls_disable(ill);
829 
830 	if (ill->ill_net_type != IRE_LOOPBACK)
831 		qprocsoff(ill->ill_rq);
832 
833 	/*
834 	 * We do an ipsq_flush once again now. New messages could have
835 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
836 	 * could also have landed up if an ioctl thread had looked up
837 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
838 	 * enqueued the ioctl when we did the ipsq_flush last time.
839 	 */
840 	ipsq_flush(ill);
841 
842 	/*
843 	 * Free capabilities.
844 	 */
845 	if (ill->ill_ipsec_capab_ah != NULL) {
846 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
847 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
848 		ill->ill_ipsec_capab_ah = NULL;
849 	}
850 
851 	if (ill->ill_ipsec_capab_esp != NULL) {
852 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
853 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
854 		ill->ill_ipsec_capab_esp = NULL;
855 	}
856 
857 	if (ill->ill_mdt_capab != NULL) {
858 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
859 		ill->ill_mdt_capab = NULL;
860 	}
861 
862 	if (ill->ill_hcksum_capab != NULL) {
863 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
864 		ill->ill_hcksum_capab = NULL;
865 	}
866 
867 	if (ill->ill_zerocopy_capab != NULL) {
868 		kmem_free(ill->ill_zerocopy_capab,
869 		    sizeof (ill_zerocopy_capab_t));
870 		ill->ill_zerocopy_capab = NULL;
871 	}
872 
873 	if (ill->ill_lso_capab != NULL) {
874 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
875 		ill->ill_lso_capab = NULL;
876 	}
877 
878 	if (ill->ill_dls_capab != NULL) {
879 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
880 		ill->ill_dls_capab->ill_unbind_conn = NULL;
881 		kmem_free(ill->ill_dls_capab,
882 		    sizeof (ill_dls_capab_t) +
883 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
884 		ill->ill_dls_capab = NULL;
885 	}
886 
887 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
888 
889 	while (ill->ill_ipif != NULL)
890 		ipif_free_tail(ill->ill_ipif);
891 
892 	/*
893 	 * We have removed all references to ilm from conn and the ones joined
894 	 * within the kernel.
895 	 *
896 	 * We don't walk conns, mrts and ires because
897 	 *
898 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
899 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
900 	 *    ill references.
901 	 */
902 	ASSERT(ilm_walk_ill(ill) == 0);
903 	/*
904 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
905 	 * could free the phyint. No more reference to the phyint after this
906 	 * point.
907 	 */
908 	(void) ill_glist_delete(ill);
909 
910 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
911 	if (ill->ill_ndd_name != NULL)
912 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
913 	rw_exit(&ipst->ips_ip_g_nd_lock);
914 
915 	if (ill->ill_frag_ptr != NULL) {
916 		uint_t count;
917 
918 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
919 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
920 		}
921 		mi_free(ill->ill_frag_ptr);
922 		ill->ill_frag_ptr = NULL;
923 		ill->ill_frag_hash_tbl = NULL;
924 	}
925 
926 	freemsg(ill->ill_nd_lla_mp);
927 	/* Free all retained control messages. */
928 	mpp = &ill->ill_first_mp_to_free;
929 	do {
930 		while (mpp[0]) {
931 			mblk_t  *mp;
932 			mblk_t  *mp1;
933 
934 			mp = mpp[0];
935 			mpp[0] = mp->b_next;
936 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
937 				mp1->b_next = NULL;
938 				mp1->b_prev = NULL;
939 			}
940 			freemsg(mp);
941 		}
942 	} while (mpp++ != &ill->ill_last_mp_to_free);
943 
944 	ill_free_mib(ill);
945 
946 #ifdef DEBUG
947 	ill_trace_cleanup(ill);
948 #endif
949 
950 	/* Drop refcnt here */
951 	netstack_rele(ill->ill_ipst->ips_netstack);
952 	ill->ill_ipst = NULL;
953 }
954 
955 static void
956 ill_free_mib(ill_t *ill)
957 {
958 	ip_stack_t *ipst = ill->ill_ipst;
959 
960 	/*
961 	 * MIB statistics must not be lost, so when an interface
962 	 * goes away the counter values will be added to the global
963 	 * MIBs.
964 	 */
965 	if (ill->ill_ip_mib != NULL) {
966 		if (ill->ill_isv6) {
967 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
968 			    ill->ill_ip_mib);
969 		} else {
970 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
971 			    ill->ill_ip_mib);
972 		}
973 
974 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
975 		ill->ill_ip_mib = NULL;
976 	}
977 	if (ill->ill_icmp6_mib != NULL) {
978 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
979 		    ill->ill_icmp6_mib);
980 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
981 		ill->ill_icmp6_mib = NULL;
982 	}
983 }
984 
985 /*
986  * Concatenate together a physical address and a sap.
987  *
988  * Sap_lengths are interpreted as follows:
989  *   sap_length == 0	==>	no sap
990  *   sap_length > 0	==>	sap is at the head of the dlpi address
991  *   sap_length < 0	==>	sap is at the tail of the dlpi address
992  */
993 static void
994 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
995     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
996 {
997 	uint16_t sap_addr = (uint16_t)sap_src;
998 
999 	if (sap_length == 0) {
1000 		if (phys_src == NULL)
1001 			bzero(dst, phys_length);
1002 		else
1003 			bcopy(phys_src, dst, phys_length);
1004 	} else if (sap_length < 0) {
1005 		if (phys_src == NULL)
1006 			bzero(dst, phys_length);
1007 		else
1008 			bcopy(phys_src, dst, phys_length);
1009 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1010 	} else {
1011 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1012 		if (phys_src == NULL)
1013 			bzero((char *)dst + sap_length, phys_length);
1014 		else
1015 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1016 	}
1017 }
1018 
1019 /*
1020  * Generate a dl_unitdata_req mblk for the device and address given.
1021  * addr_length is the length of the physical portion of the address.
1022  * If addr is NULL include an all zero address of the specified length.
1023  * TRUE? In any case, addr_length is taken to be the entire length of the
1024  * dlpi address, including the absolute value of sap_length.
1025  */
1026 mblk_t *
1027 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1028 		t_scalar_t sap_length)
1029 {
1030 	dl_unitdata_req_t *dlur;
1031 	mblk_t	*mp;
1032 	t_scalar_t	abs_sap_length;		/* absolute value */
1033 
1034 	abs_sap_length = ABS(sap_length);
1035 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1036 	    DL_UNITDATA_REQ);
1037 	if (mp == NULL)
1038 		return (NULL);
1039 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1040 	/* HACK: accomodate incompatible DLPI drivers */
1041 	if (addr_length == 8)
1042 		addr_length = 6;
1043 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1044 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1045 	dlur->dl_priority.dl_min = 0;
1046 	dlur->dl_priority.dl_max = 0;
1047 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1048 	    (uchar_t *)&dlur[1]);
1049 	return (mp);
1050 }
1051 
1052 /*
1053  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1054  * Return an error if we already have 1 or more ioctls in progress.
1055  * This is used only for non-exclusive ioctls. Currently this is used
1056  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1057  * and thus need to use ipsq_pending_mp_add.
1058  */
1059 boolean_t
1060 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1061 {
1062 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1063 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1064 	/*
1065 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1066 	 */
1067 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1068 	    (add_mp->b_datap->db_type == M_IOCTL));
1069 
1070 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1071 	/*
1072 	 * Return error if the conn has started closing. The conn
1073 	 * could have finished cleaning up the pending mp list,
1074 	 * If so we should not add another mp to the list negating
1075 	 * the cleanup.
1076 	 */
1077 	if (connp->conn_state_flags & CONN_CLOSING)
1078 		return (B_FALSE);
1079 	/*
1080 	 * Add the pending mp to the head of the list, chained by b_next.
1081 	 * Note down the conn on which the ioctl request came, in b_prev.
1082 	 * This will be used to later get the conn, when we get a response
1083 	 * on the ill queue, from some other module (typically arp)
1084 	 */
1085 	add_mp->b_next = (void *)ill->ill_pending_mp;
1086 	add_mp->b_queue = CONNP_TO_WQ(connp);
1087 	ill->ill_pending_mp = add_mp;
1088 	if (connp != NULL)
1089 		connp->conn_oper_pending_ill = ill;
1090 	return (B_TRUE);
1091 }
1092 
1093 /*
1094  * Retrieve the ill_pending_mp and return it. We have to walk the list
1095  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1096  */
1097 mblk_t *
1098 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1099 {
1100 	mblk_t	*prev = NULL;
1101 	mblk_t	*curr = NULL;
1102 	uint_t	id;
1103 	conn_t	*connp;
1104 
1105 	/*
1106 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1107 	 * up the pending mp, but it does not know the ioc_id and
1108 	 * passes in a zero for it.
1109 	 */
1110 	mutex_enter(&ill->ill_lock);
1111 	if (ioc_id != 0)
1112 		*connpp = NULL;
1113 
1114 	/* Search the list for the appropriate ioctl based on ioc_id */
1115 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1116 	    prev = curr, curr = curr->b_next) {
1117 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1118 		connp = Q_TO_CONN(curr->b_queue);
1119 		/* Match based on the ioc_id or based on the conn */
1120 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1121 			break;
1122 	}
1123 
1124 	if (curr != NULL) {
1125 		/* Unlink the mblk from the pending mp list */
1126 		if (prev != NULL) {
1127 			prev->b_next = curr->b_next;
1128 		} else {
1129 			ASSERT(ill->ill_pending_mp == curr);
1130 			ill->ill_pending_mp = curr->b_next;
1131 		}
1132 
1133 		/*
1134 		 * conn refcnt must have been bumped up at the start of
1135 		 * the ioctl. So we can safely access the conn.
1136 		 */
1137 		ASSERT(CONN_Q(curr->b_queue));
1138 		*connpp = Q_TO_CONN(curr->b_queue);
1139 		curr->b_next = NULL;
1140 		curr->b_queue = NULL;
1141 	}
1142 
1143 	mutex_exit(&ill->ill_lock);
1144 
1145 	return (curr);
1146 }
1147 
1148 /*
1149  * Add the pending mp to the list. There can be only 1 pending mp
1150  * in the list. Any exclusive ioctl that needs to wait for a response
1151  * from another module or driver needs to use this function to set
1152  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1153  * the other module/driver. This is also used while waiting for the
1154  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1155  */
1156 boolean_t
1157 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1158     int waitfor)
1159 {
1160 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1161 
1162 	ASSERT(IAM_WRITER_IPIF(ipif));
1163 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1164 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1165 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1166 	/*
1167 	 * The caller may be using a different ipif than the one passed into
1168 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1169 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1170 	 * that `ipsq_current_ipif == ipif'.
1171 	 */
1172 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1173 
1174 	/*
1175 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1176 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1177 	 */
1178 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1179 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1180 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1181 
1182 	if (connp != NULL) {
1183 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1184 		/*
1185 		 * Return error if the conn has started closing. The conn
1186 		 * could have finished cleaning up the pending mp list,
1187 		 * If so we should not add another mp to the list negating
1188 		 * the cleanup.
1189 		 */
1190 		if (connp->conn_state_flags & CONN_CLOSING)
1191 			return (B_FALSE);
1192 	}
1193 	mutex_enter(&ipsq->ipsq_lock);
1194 	ipsq->ipsq_pending_ipif = ipif;
1195 	/*
1196 	 * Note down the queue in b_queue. This will be returned by
1197 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1198 	 * the processing
1199 	 */
1200 	add_mp->b_next = NULL;
1201 	add_mp->b_queue = q;
1202 	ipsq->ipsq_pending_mp = add_mp;
1203 	ipsq->ipsq_waitfor = waitfor;
1204 
1205 	if (connp != NULL)
1206 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1207 	mutex_exit(&ipsq->ipsq_lock);
1208 	return (B_TRUE);
1209 }
1210 
1211 /*
1212  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1213  * queued in the list.
1214  */
1215 mblk_t *
1216 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1217 {
1218 	mblk_t	*curr = NULL;
1219 
1220 	mutex_enter(&ipsq->ipsq_lock);
1221 	*connpp = NULL;
1222 	if (ipsq->ipsq_pending_mp == NULL) {
1223 		mutex_exit(&ipsq->ipsq_lock);
1224 		return (NULL);
1225 	}
1226 
1227 	/* There can be only 1 such excl message */
1228 	curr = ipsq->ipsq_pending_mp;
1229 	ASSERT(curr != NULL && curr->b_next == NULL);
1230 	ipsq->ipsq_pending_ipif = NULL;
1231 	ipsq->ipsq_pending_mp = NULL;
1232 	ipsq->ipsq_waitfor = 0;
1233 	mutex_exit(&ipsq->ipsq_lock);
1234 
1235 	if (CONN_Q(curr->b_queue)) {
1236 		/*
1237 		 * This mp did a refhold on the conn, at the start of the ioctl.
1238 		 * So we can safely return a pointer to the conn to the caller.
1239 		 */
1240 		*connpp = Q_TO_CONN(curr->b_queue);
1241 	} else {
1242 		*connpp = NULL;
1243 	}
1244 	curr->b_next = NULL;
1245 	curr->b_prev = NULL;
1246 	return (curr);
1247 }
1248 
1249 /*
1250  * Cleanup the ioctl mp queued in ipsq_pending_mp
1251  * - Called in the ill_delete path
1252  * - Called in the M_ERROR or M_HANGUP path on the ill.
1253  * - Called in the conn close path.
1254  */
1255 boolean_t
1256 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1257 {
1258 	mblk_t	*mp;
1259 	ipsq_t	*ipsq;
1260 	queue_t	*q;
1261 	ipif_t	*ipif;
1262 
1263 	ASSERT(IAM_WRITER_ILL(ill));
1264 	ipsq = ill->ill_phyint->phyint_ipsq;
1265 	mutex_enter(&ipsq->ipsq_lock);
1266 	/*
1267 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1268 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1269 	 * even if it is meant for another ill, since we have to enqueue
1270 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1271 	 * If connp is non-null we are called from the conn close path.
1272 	 */
1273 	mp = ipsq->ipsq_pending_mp;
1274 	if (mp == NULL || (connp != NULL &&
1275 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1276 		mutex_exit(&ipsq->ipsq_lock);
1277 		return (B_FALSE);
1278 	}
1279 	/* Now remove from the ipsq_pending_mp */
1280 	ipsq->ipsq_pending_mp = NULL;
1281 	q = mp->b_queue;
1282 	mp->b_next = NULL;
1283 	mp->b_prev = NULL;
1284 	mp->b_queue = NULL;
1285 
1286 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1287 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1288 	if (ill->ill_move_in_progress) {
1289 		ILL_CLEAR_MOVE(ill);
1290 	} else if (ill->ill_up_ipifs) {
1291 		ill_group_cleanup(ill);
1292 	}
1293 
1294 	ipif = ipsq->ipsq_pending_ipif;
1295 	ipsq->ipsq_pending_ipif = NULL;
1296 	ipsq->ipsq_waitfor = 0;
1297 	ipsq->ipsq_current_ipif = NULL;
1298 	ipsq->ipsq_current_ioctl = 0;
1299 	ipsq->ipsq_current_done = B_TRUE;
1300 	mutex_exit(&ipsq->ipsq_lock);
1301 
1302 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1303 		if (connp == NULL) {
1304 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1305 		} else {
1306 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1307 			mutex_enter(&ipif->ipif_ill->ill_lock);
1308 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1309 			mutex_exit(&ipif->ipif_ill->ill_lock);
1310 		}
1311 	} else {
1312 		/*
1313 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1314 		 * be just inet_freemsg. we have to restart it
1315 		 * otherwise the thread will be stuck.
1316 		 */
1317 		inet_freemsg(mp);
1318 	}
1319 	return (B_TRUE);
1320 }
1321 
1322 /*
1323  * The ill is closing. Cleanup all the pending mps. Called exclusively
1324  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1325  * knows this ill, and hence nobody can add an mp to this list
1326  */
1327 static void
1328 ill_pending_mp_cleanup(ill_t *ill)
1329 {
1330 	mblk_t	*mp;
1331 	queue_t	*q;
1332 
1333 	ASSERT(IAM_WRITER_ILL(ill));
1334 
1335 	mutex_enter(&ill->ill_lock);
1336 	/*
1337 	 * Every mp on the pending mp list originating from an ioctl
1338 	 * added 1 to the conn refcnt, at the start of the ioctl.
1339 	 * So bump it down now.  See comments in ip_wput_nondata()
1340 	 */
1341 	while (ill->ill_pending_mp != NULL) {
1342 		mp = ill->ill_pending_mp;
1343 		ill->ill_pending_mp = mp->b_next;
1344 		mutex_exit(&ill->ill_lock);
1345 
1346 		q = mp->b_queue;
1347 		ASSERT(CONN_Q(q));
1348 		mp->b_next = NULL;
1349 		mp->b_prev = NULL;
1350 		mp->b_queue = NULL;
1351 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1352 		mutex_enter(&ill->ill_lock);
1353 	}
1354 	ill->ill_pending_ipif = NULL;
1355 
1356 	mutex_exit(&ill->ill_lock);
1357 }
1358 
1359 /*
1360  * Called in the conn close path and ill delete path
1361  */
1362 static void
1363 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1364 {
1365 	ipsq_t	*ipsq;
1366 	mblk_t	*prev;
1367 	mblk_t	*curr;
1368 	mblk_t	*next;
1369 	queue_t	*q;
1370 	mblk_t	*tmp_list = NULL;
1371 
1372 	ASSERT(IAM_WRITER_ILL(ill));
1373 	if (connp != NULL)
1374 		q = CONNP_TO_WQ(connp);
1375 	else
1376 		q = ill->ill_wq;
1377 
1378 	ipsq = ill->ill_phyint->phyint_ipsq;
1379 	/*
1380 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1381 	 * In the case of ioctl from a conn, there can be only 1 mp
1382 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1383 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1384 	 * ioctls meant for this ill form conn's are not flushed. They will
1385 	 * be processed during ipsq_exit and will not find the ill and will
1386 	 * return error.
1387 	 */
1388 	mutex_enter(&ipsq->ipsq_lock);
1389 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1390 	    curr = next) {
1391 		next = curr->b_next;
1392 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1393 			/* Unlink the mblk from the pending mp list */
1394 			if (prev != NULL) {
1395 				prev->b_next = curr->b_next;
1396 			} else {
1397 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1398 				ipsq->ipsq_xopq_mphead = curr->b_next;
1399 			}
1400 			if (ipsq->ipsq_xopq_mptail == curr)
1401 				ipsq->ipsq_xopq_mptail = prev;
1402 			/*
1403 			 * Create a temporary list and release the ipsq lock
1404 			 * New elements are added to the head of the tmp_list
1405 			 */
1406 			curr->b_next = tmp_list;
1407 			tmp_list = curr;
1408 		} else {
1409 			prev = curr;
1410 		}
1411 	}
1412 	mutex_exit(&ipsq->ipsq_lock);
1413 
1414 	while (tmp_list != NULL) {
1415 		curr = tmp_list;
1416 		tmp_list = curr->b_next;
1417 		curr->b_next = NULL;
1418 		curr->b_prev = NULL;
1419 		curr->b_queue = NULL;
1420 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1421 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1422 			    CONN_CLOSE : NO_COPYOUT, NULL);
1423 		} else {
1424 			/*
1425 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1426 			 * this can't be just inet_freemsg. we have to
1427 			 * restart it otherwise the thread will be stuck.
1428 			 */
1429 			inet_freemsg(curr);
1430 		}
1431 	}
1432 }
1433 
1434 /*
1435  * This conn has started closing. Cleanup any pending ioctl from this conn.
1436  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1437  */
1438 void
1439 conn_ioctl_cleanup(conn_t *connp)
1440 {
1441 	mblk_t *curr;
1442 	ipsq_t	*ipsq;
1443 	ill_t	*ill;
1444 	boolean_t refheld;
1445 
1446 	/*
1447 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1448 	 * ioctl has not yet started, the mp is pending in the list headed by
1449 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1450 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1451 	 * is currently executing now the mp is not queued anywhere but
1452 	 * conn_oper_pending_ill is null. The conn close will wait
1453 	 * till the conn_ref drops to zero.
1454 	 */
1455 	mutex_enter(&connp->conn_lock);
1456 	ill = connp->conn_oper_pending_ill;
1457 	if (ill == NULL) {
1458 		mutex_exit(&connp->conn_lock);
1459 		return;
1460 	}
1461 
1462 	curr = ill_pending_mp_get(ill, &connp, 0);
1463 	if (curr != NULL) {
1464 		mutex_exit(&connp->conn_lock);
1465 		CONN_DEC_REF(connp);
1466 		inet_freemsg(curr);
1467 		return;
1468 	}
1469 	/*
1470 	 * We may not be able to refhold the ill if the ill/ipif
1471 	 * is changing. But we need to make sure that the ill will
1472 	 * not vanish. So we just bump up the ill_waiter count.
1473 	 */
1474 	refheld = ill_waiter_inc(ill);
1475 	mutex_exit(&connp->conn_lock);
1476 	if (refheld) {
1477 		if (ipsq_enter(ill, B_TRUE)) {
1478 			ill_waiter_dcr(ill);
1479 			/*
1480 			 * Check whether this ioctl has started and is
1481 			 * pending now in ipsq_pending_mp. If it is not
1482 			 * found there then check whether this ioctl has
1483 			 * not even started and is in the ipsq_xopq list.
1484 			 */
1485 			if (!ipsq_pending_mp_cleanup(ill, connp))
1486 				ipsq_xopq_mp_cleanup(ill, connp);
1487 			ipsq = ill->ill_phyint->phyint_ipsq;
1488 			ipsq_exit(ipsq);
1489 			return;
1490 		}
1491 	}
1492 
1493 	/*
1494 	 * The ill is also closing and we could not bump up the
1495 	 * ill_waiter_count or we could not enter the ipsq. Leave
1496 	 * the cleanup to ill_delete
1497 	 */
1498 	mutex_enter(&connp->conn_lock);
1499 	while (connp->conn_oper_pending_ill != NULL)
1500 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1501 	mutex_exit(&connp->conn_lock);
1502 	if (refheld)
1503 		ill_waiter_dcr(ill);
1504 }
1505 
1506 /*
1507  * ipcl_walk function for cleaning up conn_*_ill fields.
1508  */
1509 static void
1510 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1511 {
1512 	ill_t	*ill = (ill_t *)arg;
1513 	ire_t	*ire;
1514 
1515 	mutex_enter(&connp->conn_lock);
1516 	if (connp->conn_multicast_ill == ill) {
1517 		/* Revert to late binding */
1518 		connp->conn_multicast_ill = NULL;
1519 		connp->conn_orig_multicast_ifindex = 0;
1520 	}
1521 	if (connp->conn_incoming_ill == ill)
1522 		connp->conn_incoming_ill = NULL;
1523 	if (connp->conn_outgoing_ill == ill)
1524 		connp->conn_outgoing_ill = NULL;
1525 	if (connp->conn_outgoing_pill == ill)
1526 		connp->conn_outgoing_pill = NULL;
1527 	if (connp->conn_nofailover_ill == ill)
1528 		connp->conn_nofailover_ill = NULL;
1529 	if (connp->conn_dhcpinit_ill == ill) {
1530 		connp->conn_dhcpinit_ill = NULL;
1531 		ASSERT(ill->ill_dhcpinit != 0);
1532 		atomic_dec_32(&ill->ill_dhcpinit);
1533 	}
1534 	if (connp->conn_ire_cache != NULL) {
1535 		ire = connp->conn_ire_cache;
1536 		/*
1537 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1538 		 * interface X and ipif coming from interface Y, if interface
1539 		 * X and Y are part of the same IPMPgroup. Thus whenever
1540 		 * interface X goes down, remove all references to it by
1541 		 * checking both on ire_ipif and ire_stq.
1542 		 */
1543 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1544 		    (ire->ire_type == IRE_CACHE &&
1545 		    ire->ire_stq == ill->ill_wq)) {
1546 			connp->conn_ire_cache = NULL;
1547 			mutex_exit(&connp->conn_lock);
1548 			ire_refrele_notr(ire);
1549 			return;
1550 		}
1551 	}
1552 	mutex_exit(&connp->conn_lock);
1553 }
1554 
1555 /* ARGSUSED */
1556 void
1557 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1558 {
1559 	ill_t	*ill = q->q_ptr;
1560 	ipif_t	*ipif;
1561 
1562 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1563 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1564 		ipif_non_duplicate(ipif);
1565 		ipif_down_tail(ipif);
1566 	}
1567 	freemsg(mp);
1568 	ipsq_current_finish(ipsq);
1569 }
1570 
1571 /*
1572  * ill_down_start is called when we want to down this ill and bring it up again
1573  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1574  * all interfaces, but don't tear down any plumbing.
1575  */
1576 boolean_t
1577 ill_down_start(queue_t *q, mblk_t *mp)
1578 {
1579 	ill_t	*ill = q->q_ptr;
1580 	ipif_t	*ipif;
1581 
1582 	ASSERT(IAM_WRITER_ILL(ill));
1583 
1584 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1585 		(void) ipif_down(ipif, NULL, NULL);
1586 
1587 	ill_down(ill);
1588 
1589 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1590 
1591 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1592 
1593 	/*
1594 	 * Atomically test and add the pending mp if references are active.
1595 	 */
1596 	mutex_enter(&ill->ill_lock);
1597 	if (!ill_is_quiescent(ill)) {
1598 		/* call cannot fail since `conn_t *' argument is NULL */
1599 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1600 		    mp, ILL_DOWN);
1601 		mutex_exit(&ill->ill_lock);
1602 		return (B_FALSE);
1603 	}
1604 	mutex_exit(&ill->ill_lock);
1605 	return (B_TRUE);
1606 }
1607 
1608 static void
1609 ill_down(ill_t *ill)
1610 {
1611 	ip_stack_t	*ipst = ill->ill_ipst;
1612 
1613 	/* Blow off any IREs dependent on this ILL. */
1614 	ire_walk(ill_downi, (char *)ill, ipst);
1615 
1616 	/* Remove any conn_*_ill depending on this ill */
1617 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1618 
1619 	if (ill->ill_group != NULL) {
1620 		illgrp_delete(ill);
1621 	}
1622 }
1623 
1624 /*
1625  * ire_walk routine used to delete every IRE that depends on queues
1626  * associated with 'ill'.  (Always called as writer.)
1627  */
1628 static void
1629 ill_downi(ire_t *ire, char *ill_arg)
1630 {
1631 	ill_t	*ill = (ill_t *)ill_arg;
1632 
1633 	/*
1634 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1635 	 * interface X and ipif coming from interface Y, if interface
1636 	 * X and Y are part of the same IPMP group. Thus whenever interface
1637 	 * X goes down, remove all references to it by checking both
1638 	 * on ire_ipif and ire_stq.
1639 	 */
1640 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1641 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1642 		ire_delete(ire);
1643 	}
1644 }
1645 
1646 /*
1647  * Remove ire/nce from the fastpath list.
1648  */
1649 void
1650 ill_fastpath_nack(ill_t *ill)
1651 {
1652 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1653 }
1654 
1655 /* Consume an M_IOCACK of the fastpath probe. */
1656 void
1657 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1658 {
1659 	mblk_t	*mp1 = mp;
1660 
1661 	/*
1662 	 * If this was the first attempt turn on the fastpath probing.
1663 	 */
1664 	mutex_enter(&ill->ill_lock);
1665 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1666 		ill->ill_dlpi_fastpath_state = IDS_OK;
1667 	mutex_exit(&ill->ill_lock);
1668 
1669 	/* Free the M_IOCACK mblk, hold on to the data */
1670 	mp = mp->b_cont;
1671 	freeb(mp1);
1672 	if (mp == NULL)
1673 		return;
1674 	if (mp->b_cont != NULL) {
1675 		/*
1676 		 * Update all IRE's or NCE's that are waiting for
1677 		 * fastpath update.
1678 		 */
1679 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1680 		mp1 = mp->b_cont;
1681 		freeb(mp);
1682 		mp = mp1;
1683 	} else {
1684 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1685 	}
1686 
1687 	freeb(mp);
1688 }
1689 
1690 /*
1691  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1692  * The data portion of the request is a dl_unitdata_req_t template for
1693  * what we would send downstream in the absence of a fastpath confirmation.
1694  */
1695 int
1696 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1697 {
1698 	struct iocblk	*ioc;
1699 	mblk_t	*mp;
1700 
1701 	if (dlur_mp == NULL)
1702 		return (EINVAL);
1703 
1704 	mutex_enter(&ill->ill_lock);
1705 	switch (ill->ill_dlpi_fastpath_state) {
1706 	case IDS_FAILED:
1707 		/*
1708 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1709 		 * support it.
1710 		 */
1711 		mutex_exit(&ill->ill_lock);
1712 		return (ENOTSUP);
1713 	case IDS_UNKNOWN:
1714 		/* This is the first probe */
1715 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1716 		break;
1717 	default:
1718 		break;
1719 	}
1720 	mutex_exit(&ill->ill_lock);
1721 
1722 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1723 		return (EAGAIN);
1724 
1725 	mp->b_cont = copyb(dlur_mp);
1726 	if (mp->b_cont == NULL) {
1727 		freeb(mp);
1728 		return (EAGAIN);
1729 	}
1730 
1731 	ioc = (struct iocblk *)mp->b_rptr;
1732 	ioc->ioc_count = msgdsize(mp->b_cont);
1733 
1734 	putnext(ill->ill_wq, mp);
1735 	return (0);
1736 }
1737 
1738 void
1739 ill_capability_probe(ill_t *ill)
1740 {
1741 	/*
1742 	 * Do so only if capabilities are still unknown.
1743 	 */
1744 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1745 		return;
1746 
1747 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1748 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1749 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1750 }
1751 
1752 void
1753 ill_capability_reset(ill_t *ill)
1754 {
1755 	mblk_t *sc_mp = NULL;
1756 	mblk_t *tmp;
1757 
1758 	/*
1759 	 * Note here that we reset the state to UNKNOWN, and later send
1760 	 * down the DL_CAPABILITY_REQ without first setting the state to
1761 	 * INPROGRESS.  We do this in order to distinguish the
1762 	 * DL_CAPABILITY_ACK response which may come back in response to
1763 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1764 	 * also handle the case where the driver doesn't send us back
1765 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1766 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1767 	 * features are turned off until the state reaches IDS_OK.
1768 	 */
1769 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1770 	ill->ill_capab_reneg = B_FALSE;
1771 
1772 	/*
1773 	 * Disable sub-capabilities and request a list of sub-capability
1774 	 * messages which will be sent down to the driver.  Each handler
1775 	 * allocates the corresponding dl_capability_sub_t inside an
1776 	 * mblk, and links it to the existing sc_mp mblk, or return it
1777 	 * as sc_mp if it's the first sub-capability (the passed in
1778 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1779 	 * sc_mp will be pulled-up, before passing it downstream.
1780 	 */
1781 	ill_capability_mdt_reset(ill, &sc_mp);
1782 	ill_capability_hcksum_reset(ill, &sc_mp);
1783 	ill_capability_zerocopy_reset(ill, &sc_mp);
1784 	ill_capability_ipsec_reset(ill, &sc_mp);
1785 	ill_capability_dls_reset(ill, &sc_mp);
1786 	ill_capability_lso_reset(ill, &sc_mp);
1787 
1788 	/* Nothing to send down in order to disable the capabilities? */
1789 	if (sc_mp == NULL)
1790 		return;
1791 
1792 	tmp = msgpullup(sc_mp, -1);
1793 	freemsg(sc_mp);
1794 	if ((sc_mp = tmp) == NULL) {
1795 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1796 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1797 		return;
1798 	}
1799 
1800 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1801 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1802 }
1803 
1804 /*
1805  * Request or set new-style hardware capabilities supported by DLS provider.
1806  */
1807 static void
1808 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1809 {
1810 	mblk_t *mp;
1811 	dl_capability_req_t *capb;
1812 	size_t size = 0;
1813 	uint8_t *ptr;
1814 
1815 	if (reqp != NULL)
1816 		size = MBLKL(reqp);
1817 
1818 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1819 	if (mp == NULL) {
1820 		freemsg(reqp);
1821 		return;
1822 	}
1823 	ptr = mp->b_rptr;
1824 
1825 	capb = (dl_capability_req_t *)ptr;
1826 	ptr += sizeof (dl_capability_req_t);
1827 
1828 	if (reqp != NULL) {
1829 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1830 		capb->dl_sub_length = size;
1831 		bcopy(reqp->b_rptr, ptr, size);
1832 		ptr += size;
1833 		mp->b_cont = reqp->b_cont;
1834 		freeb(reqp);
1835 	}
1836 	ASSERT(ptr == mp->b_wptr);
1837 
1838 	ill_dlpi_send(ill, mp);
1839 }
1840 
1841 static void
1842 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1843 {
1844 	dl_capab_id_t *id_ic;
1845 	uint_t sub_dl_cap = outers->dl_cap;
1846 	dl_capability_sub_t *inners;
1847 	uint8_t *capend;
1848 
1849 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1850 
1851 	/*
1852 	 * Note: range checks here are not absolutely sufficient to
1853 	 * make us robust against malformed messages sent by drivers;
1854 	 * this is in keeping with the rest of IP's dlpi handling.
1855 	 * (Remember, it's coming from something else in the kernel
1856 	 * address space)
1857 	 */
1858 
1859 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1860 	if (capend > mp->b_wptr) {
1861 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1862 		    "malformed sub-capability too long for mblk");
1863 		return;
1864 	}
1865 
1866 	id_ic = (dl_capab_id_t *)(outers + 1);
1867 
1868 	if (outers->dl_length < sizeof (*id_ic) ||
1869 	    (inners = &id_ic->id_subcap,
1870 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1871 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1872 		    "encapsulated capab type %d too long for mblk",
1873 		    inners->dl_cap);
1874 		return;
1875 	}
1876 
1877 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1878 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1879 		    "isn't as expected; pass-thru module(s) detected, "
1880 		    "discarding capability\n", inners->dl_cap));
1881 		return;
1882 	}
1883 
1884 	/* Process the encapsulated sub-capability */
1885 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1886 }
1887 
1888 /*
1889  * Process Multidata Transmit capability negotiation ack received from a
1890  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1891  * DL_CAPABILITY_ACK message.
1892  */
1893 static void
1894 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1895 {
1896 	mblk_t *nmp = NULL;
1897 	dl_capability_req_t *oc;
1898 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1899 	ill_mdt_capab_t **ill_mdt_capab;
1900 	uint_t sub_dl_cap = isub->dl_cap;
1901 	uint8_t *capend;
1902 
1903 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1904 
1905 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1906 
1907 	/*
1908 	 * Note: range checks here are not absolutely sufficient to
1909 	 * make us robust against malformed messages sent by drivers;
1910 	 * this is in keeping with the rest of IP's dlpi handling.
1911 	 * (Remember, it's coming from something else in the kernel
1912 	 * address space)
1913 	 */
1914 
1915 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1916 	if (capend > mp->b_wptr) {
1917 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1918 		    "malformed sub-capability too long for mblk");
1919 		return;
1920 	}
1921 
1922 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1923 
1924 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1925 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1926 		    "unsupported MDT sub-capability (version %d, expected %d)",
1927 		    mdt_ic->mdt_version, MDT_VERSION_2);
1928 		return;
1929 	}
1930 
1931 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1932 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1933 		    "capability isn't as expected; pass-thru module(s) "
1934 		    "detected, discarding capability\n"));
1935 		return;
1936 	}
1937 
1938 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1939 
1940 		if (*ill_mdt_capab == NULL) {
1941 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1942 			    KM_NOSLEEP);
1943 
1944 			if (*ill_mdt_capab == NULL) {
1945 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1946 				    "could not enable MDT version %d "
1947 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1948 				    ill->ill_name);
1949 				return;
1950 			}
1951 		}
1952 
1953 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1954 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1955 		    "header spaces, %d max pld bufs, %d span limit)\n",
1956 		    ill->ill_name, MDT_VERSION_2,
1957 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1958 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1959 
1960 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1961 		(*ill_mdt_capab)->ill_mdt_on = 1;
1962 		/*
1963 		 * Round the following values to the nearest 32-bit; ULP
1964 		 * may further adjust them to accomodate for additional
1965 		 * protocol headers.  We pass these values to ULP during
1966 		 * bind time.
1967 		 */
1968 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1969 		    roundup(mdt_ic->mdt_hdr_head, 4);
1970 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1971 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1972 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1973 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1974 
1975 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1976 	} else {
1977 		uint_t size;
1978 		uchar_t *rptr;
1979 
1980 		size = sizeof (dl_capability_req_t) +
1981 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1982 
1983 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1984 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1985 			    "could not enable MDT for %s (ENOMEM)\n",
1986 			    ill->ill_name);
1987 			return;
1988 		}
1989 
1990 		rptr = nmp->b_rptr;
1991 		/* initialize dl_capability_req_t */
1992 		oc = (dl_capability_req_t *)nmp->b_rptr;
1993 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1994 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1995 		    sizeof (dl_capab_mdt_t);
1996 		nmp->b_rptr += sizeof (dl_capability_req_t);
1997 
1998 		/* initialize dl_capability_sub_t */
1999 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2000 		nmp->b_rptr += sizeof (*isub);
2001 
2002 		/* initialize dl_capab_mdt_t */
2003 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2004 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2005 
2006 		nmp->b_rptr = rptr;
2007 
2008 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2009 		    "to enable MDT version %d\n", ill->ill_name,
2010 		    MDT_VERSION_2));
2011 
2012 		/* set ENABLE flag */
2013 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2014 
2015 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2016 		ill_dlpi_send(ill, nmp);
2017 	}
2018 }
2019 
2020 static void
2021 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2022 {
2023 	mblk_t *mp;
2024 	dl_capab_mdt_t *mdt_subcap;
2025 	dl_capability_sub_t *dl_subcap;
2026 	int size;
2027 
2028 	if (!ILL_MDT_CAPABLE(ill))
2029 		return;
2030 
2031 	ASSERT(ill->ill_mdt_capab != NULL);
2032 	/*
2033 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2034 	 * structure since it's possible that another thread is still
2035 	 * referring to it.  The structure only gets deallocated when
2036 	 * we destroy the ill.
2037 	 */
2038 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2039 
2040 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2041 
2042 	mp = allocb(size, BPRI_HI);
2043 	if (mp == NULL) {
2044 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2045 		    "request to disable MDT\n"));
2046 		return;
2047 	}
2048 
2049 	mp->b_wptr = mp->b_rptr + size;
2050 
2051 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2052 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2053 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2054 
2055 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2056 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2057 	mdt_subcap->mdt_flags = 0;
2058 	mdt_subcap->mdt_hdr_head = 0;
2059 	mdt_subcap->mdt_hdr_tail = 0;
2060 
2061 	if (*sc_mp != NULL)
2062 		linkb(*sc_mp, mp);
2063 	else
2064 		*sc_mp = mp;
2065 }
2066 
2067 /*
2068  * Send a DL_NOTIFY_REQ to the specified ill to enable
2069  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2070  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2071  * acceleration.
2072  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2073  */
2074 static boolean_t
2075 ill_enable_promisc_notify(ill_t *ill)
2076 {
2077 	mblk_t *mp;
2078 	dl_notify_req_t *req;
2079 
2080 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2081 
2082 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2083 	if (mp == NULL)
2084 		return (B_FALSE);
2085 
2086 	req = (dl_notify_req_t *)mp->b_rptr;
2087 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2088 	    DL_NOTE_PROMISC_OFF_PHYS;
2089 
2090 	ill_dlpi_send(ill, mp);
2091 
2092 	return (B_TRUE);
2093 }
2094 
2095 /*
2096  * Allocate an IPsec capability request which will be filled by our
2097  * caller to turn on support for one or more algorithms.
2098  */
2099 static mblk_t *
2100 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2101 {
2102 	mblk_t *nmp;
2103 	dl_capability_req_t	*ocap;
2104 	dl_capab_ipsec_t	*ocip;
2105 	dl_capab_ipsec_t	*icip;
2106 	uint8_t			*ptr;
2107 	icip = (dl_capab_ipsec_t *)(isub + 1);
2108 
2109 	/*
2110 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2111 	 * PROMISC_ON/OFF notification from the provider. We need to
2112 	 * do this before enabling the algorithms to avoid leakage of
2113 	 * cleartext packets.
2114 	 */
2115 
2116 	if (!ill_enable_promisc_notify(ill))
2117 		return (NULL);
2118 
2119 	/*
2120 	 * Allocate new mblk which will contain a new capability
2121 	 * request to enable the capabilities.
2122 	 */
2123 
2124 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2125 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2126 	if (nmp == NULL)
2127 		return (NULL);
2128 
2129 	ptr = nmp->b_rptr;
2130 
2131 	/* initialize dl_capability_req_t */
2132 	ocap = (dl_capability_req_t *)ptr;
2133 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2134 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2135 	ptr += sizeof (dl_capability_req_t);
2136 
2137 	/* initialize dl_capability_sub_t */
2138 	bcopy(isub, ptr, sizeof (*isub));
2139 	ptr += sizeof (*isub);
2140 
2141 	/* initialize dl_capab_ipsec_t */
2142 	ocip = (dl_capab_ipsec_t *)ptr;
2143 	bcopy(icip, ocip, sizeof (*icip));
2144 
2145 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2146 	return (nmp);
2147 }
2148 
2149 /*
2150  * Process an IPsec capability negotiation ack received from a DLS Provider.
2151  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2152  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2153  */
2154 static void
2155 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2156 {
2157 	dl_capab_ipsec_t	*icip;
2158 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2159 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2160 	uint_t cipher, nciphers;
2161 	mblk_t *nmp;
2162 	uint_t alg_len;
2163 	boolean_t need_sadb_dump;
2164 	uint_t sub_dl_cap = isub->dl_cap;
2165 	ill_ipsec_capab_t **ill_capab;
2166 	uint64_t ill_capab_flag;
2167 	uint8_t *capend, *ciphend;
2168 	boolean_t sadb_resync;
2169 
2170 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2171 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2172 
2173 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2174 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2175 		ill_capab_flag = ILL_CAPAB_AH;
2176 	} else {
2177 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2178 		ill_capab_flag = ILL_CAPAB_ESP;
2179 	}
2180 
2181 	/*
2182 	 * If the ill capability structure exists, then this incoming
2183 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2184 	 * If this is so, then we'd need to resynchronize the SADB
2185 	 * after re-enabling the offloaded ciphers.
2186 	 */
2187 	sadb_resync = (*ill_capab != NULL);
2188 
2189 	/*
2190 	 * Note: range checks here are not absolutely sufficient to
2191 	 * make us robust against malformed messages sent by drivers;
2192 	 * this is in keeping with the rest of IP's dlpi handling.
2193 	 * (Remember, it's coming from something else in the kernel
2194 	 * address space)
2195 	 */
2196 
2197 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2198 	if (capend > mp->b_wptr) {
2199 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2200 		    "malformed sub-capability too long for mblk");
2201 		return;
2202 	}
2203 
2204 	/*
2205 	 * There are two types of acks we process here:
2206 	 * 1. acks in reply to a (first form) generic capability req
2207 	 *    (no ENABLE flag set)
2208 	 * 2. acks in reply to a ENABLE capability req.
2209 	 *    (ENABLE flag set)
2210 	 *
2211 	 * We process the subcapability passed as argument as follows:
2212 	 * 1 do initializations
2213 	 *   1.1 initialize nmp = NULL
2214 	 *   1.2 set need_sadb_dump to B_FALSE
2215 	 * 2 for each cipher in subcapability:
2216 	 *   2.1 if ENABLE flag is set:
2217 	 *	2.1.1 update per-ill ipsec capabilities info
2218 	 *	2.1.2 set need_sadb_dump to B_TRUE
2219 	 *   2.2 if ENABLE flag is not set:
2220 	 *	2.2.1 if nmp is NULL:
2221 	 *		2.2.1.1 allocate and initialize nmp
2222 	 *		2.2.1.2 init current pos in nmp
2223 	 *	2.2.2 copy current cipher to current pos in nmp
2224 	 *	2.2.3 set ENABLE flag in nmp
2225 	 *	2.2.4 update current pos
2226 	 * 3 if nmp is not equal to NULL, send enable request
2227 	 *   3.1 send capability request
2228 	 * 4 if need_sadb_dump is B_TRUE
2229 	 *   4.1 enable promiscuous on/off notifications
2230 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2231 	 *	AH or ESP SA's to interface.
2232 	 */
2233 
2234 	nmp = NULL;
2235 	oalg = NULL;
2236 	need_sadb_dump = B_FALSE;
2237 	icip = (dl_capab_ipsec_t *)(isub + 1);
2238 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2239 
2240 	nciphers = icip->cip_nciphers;
2241 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2242 
2243 	if (ciphend > capend) {
2244 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2245 		    "too many ciphers for sub-capability len");
2246 		return;
2247 	}
2248 
2249 	for (cipher = 0; cipher < nciphers; cipher++) {
2250 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2251 
2252 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2253 			/*
2254 			 * TBD: when we provide a way to disable capabilities
2255 			 * from above, need to manage the request-pending state
2256 			 * and fail if we were not expecting this ACK.
2257 			 */
2258 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2259 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2260 
2261 			/*
2262 			 * Update IPsec capabilities for this ill
2263 			 */
2264 
2265 			if (*ill_capab == NULL) {
2266 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2267 				    ("ill_capability_ipsec_ack: "
2268 				    "allocating ipsec_capab for ill\n"));
2269 				*ill_capab = ill_ipsec_capab_alloc();
2270 
2271 				if (*ill_capab == NULL) {
2272 					cmn_err(CE_WARN,
2273 					    "ill_capability_ipsec_ack: "
2274 					    "could not enable IPsec Hardware "
2275 					    "acceleration for %s (ENOMEM)\n",
2276 					    ill->ill_name);
2277 					return;
2278 				}
2279 			}
2280 
2281 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2282 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2283 
2284 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2285 				cmn_err(CE_WARN,
2286 				    "ill_capability_ipsec_ack: "
2287 				    "malformed IPsec algorithm id %d",
2288 				    ialg->alg_prim);
2289 				continue;
2290 			}
2291 
2292 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2293 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2294 				    ialg->alg_prim);
2295 			} else {
2296 				ipsec_capab_algparm_t *alp;
2297 
2298 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2299 				    ialg->alg_prim);
2300 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2301 				    ialg->alg_prim)) {
2302 					cmn_err(CE_WARN,
2303 					    "ill_capability_ipsec_ack: "
2304 					    "no space for IPsec alg id %d",
2305 					    ialg->alg_prim);
2306 					continue;
2307 				}
2308 				alp = &((*ill_capab)->encr_algparm[
2309 				    ialg->alg_prim]);
2310 				alp->minkeylen = ialg->alg_minbits;
2311 				alp->maxkeylen = ialg->alg_maxbits;
2312 			}
2313 			ill->ill_capabilities |= ill_capab_flag;
2314 			/*
2315 			 * indicate that a capability was enabled, which
2316 			 * will be used below to kick off a SADB dump
2317 			 * to the ill.
2318 			 */
2319 			need_sadb_dump = B_TRUE;
2320 		} else {
2321 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2322 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2323 			    ialg->alg_prim));
2324 
2325 			if (nmp == NULL) {
2326 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2327 				if (nmp == NULL) {
2328 					/*
2329 					 * Sending the PROMISC_ON/OFF
2330 					 * notification request failed.
2331 					 * We cannot enable the algorithms
2332 					 * since the Provider will not
2333 					 * notify IP of promiscous mode
2334 					 * changes, which could lead
2335 					 * to leakage of packets.
2336 					 */
2337 					cmn_err(CE_WARN,
2338 					    "ill_capability_ipsec_ack: "
2339 					    "could not enable IPsec Hardware "
2340 					    "acceleration for %s (ENOMEM)\n",
2341 					    ill->ill_name);
2342 					return;
2343 				}
2344 				/* ptr to current output alg specifier */
2345 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2346 			}
2347 
2348 			/*
2349 			 * Copy current alg specifier, set ENABLE
2350 			 * flag, and advance to next output alg.
2351 			 * For now we enable all IPsec capabilities.
2352 			 */
2353 			ASSERT(oalg != NULL);
2354 			bcopy(ialg, oalg, alg_len);
2355 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2356 			nmp->b_wptr += alg_len;
2357 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2358 		}
2359 
2360 		/* move to next input algorithm specifier */
2361 		ialg = (dl_capab_ipsec_alg_t *)
2362 		    ((char *)ialg + alg_len);
2363 	}
2364 
2365 	if (nmp != NULL)
2366 		/*
2367 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2368 		 * IPsec hardware acceleration.
2369 		 */
2370 		ill_dlpi_send(ill, nmp);
2371 
2372 	if (need_sadb_dump)
2373 		/*
2374 		 * An acknowledgement corresponding to a request to
2375 		 * enable acceleration was received, notify SADB.
2376 		 */
2377 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2378 }
2379 
2380 /*
2381  * Given an mblk with enough space in it, create sub-capability entries for
2382  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2383  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2384  * in preparation for the reset the DL_CAPABILITY_REQ message.
2385  */
2386 static void
2387 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2388     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2389 {
2390 	dl_capab_ipsec_t *oipsec;
2391 	dl_capab_ipsec_alg_t *oalg;
2392 	dl_capability_sub_t *dl_subcap;
2393 	int i, k;
2394 
2395 	ASSERT(nciphers > 0);
2396 	ASSERT(ill_cap != NULL);
2397 	ASSERT(mp != NULL);
2398 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2399 
2400 	/* dl_capability_sub_t for "stype" */
2401 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2402 	dl_subcap->dl_cap = stype;
2403 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2404 	mp->b_wptr += sizeof (dl_capability_sub_t);
2405 
2406 	/* dl_capab_ipsec_t for "stype" */
2407 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2408 	oipsec->cip_version = 1;
2409 	oipsec->cip_nciphers = nciphers;
2410 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2411 
2412 	/* create entries for "stype" AUTH ciphers */
2413 	for (i = 0; i < ill_cap->algs_size; i++) {
2414 		for (k = 0; k < BITSPERBYTE; k++) {
2415 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2416 				continue;
2417 
2418 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2419 			bzero((void *)oalg, sizeof (*oalg));
2420 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2421 			oalg->alg_prim = k + (BITSPERBYTE * i);
2422 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2423 		}
2424 	}
2425 	/* create entries for "stype" ENCR ciphers */
2426 	for (i = 0; i < ill_cap->algs_size; i++) {
2427 		for (k = 0; k < BITSPERBYTE; k++) {
2428 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2429 				continue;
2430 
2431 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2432 			bzero((void *)oalg, sizeof (*oalg));
2433 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2434 			oalg->alg_prim = k + (BITSPERBYTE * i);
2435 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2436 		}
2437 	}
2438 }
2439 
2440 /*
2441  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2442  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2443  * POPC instruction, but our macro is more flexible for an arbitrary length
2444  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2445  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2446  * stays that way, we can reduce the number of iterations required.
2447  */
2448 #define	COUNT_1S(val, sum) {					\
2449 	uint8_t x = val & 0xff;					\
2450 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2451 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2452 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2453 }
2454 
2455 /* ARGSUSED */
2456 static void
2457 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2458 {
2459 	mblk_t *mp;
2460 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2461 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2462 	uint64_t ill_capabilities = ill->ill_capabilities;
2463 	int ah_cnt = 0, esp_cnt = 0;
2464 	int ah_len = 0, esp_len = 0;
2465 	int i, size = 0;
2466 
2467 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2468 		return;
2469 
2470 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2471 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2472 
2473 	/* Find out the number of ciphers for AH */
2474 	if (cap_ah != NULL) {
2475 		for (i = 0; i < cap_ah->algs_size; i++) {
2476 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2477 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2478 		}
2479 		if (ah_cnt > 0) {
2480 			size += sizeof (dl_capability_sub_t) +
2481 			    sizeof (dl_capab_ipsec_t);
2482 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2483 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2484 			size += ah_len;
2485 		}
2486 	}
2487 
2488 	/* Find out the number of ciphers for ESP */
2489 	if (cap_esp != NULL) {
2490 		for (i = 0; i < cap_esp->algs_size; i++) {
2491 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2492 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2493 		}
2494 		if (esp_cnt > 0) {
2495 			size += sizeof (dl_capability_sub_t) +
2496 			    sizeof (dl_capab_ipsec_t);
2497 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2498 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2499 			size += esp_len;
2500 		}
2501 	}
2502 
2503 	if (size == 0) {
2504 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2505 		    "there's nothing to reset\n"));
2506 		return;
2507 	}
2508 
2509 	mp = allocb(size, BPRI_HI);
2510 	if (mp == NULL) {
2511 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2512 		    "request to disable IPSEC Hardware Acceleration\n"));
2513 		return;
2514 	}
2515 
2516 	/*
2517 	 * Clear the capability flags for IPsec HA but retain the ill
2518 	 * capability structures since it's possible that another thread
2519 	 * is still referring to them.  The structures only get deallocated
2520 	 * when we destroy the ill.
2521 	 *
2522 	 * Various places check the flags to see if the ill is capable of
2523 	 * hardware acceleration, and by clearing them we ensure that new
2524 	 * outbound IPsec packets are sent down encrypted.
2525 	 */
2526 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2527 
2528 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2529 	if (ah_cnt > 0) {
2530 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2531 		    cap_ah, mp);
2532 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2533 	}
2534 
2535 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2536 	if (esp_cnt > 0) {
2537 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2538 		    cap_esp, mp);
2539 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2540 	}
2541 
2542 	/*
2543 	 * At this point we've composed a bunch of sub-capabilities to be
2544 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2545 	 * by the caller.  Upon receiving this reset message, the driver
2546 	 * must stop inbound decryption (by destroying all inbound SAs)
2547 	 * and let the corresponding packets come in encrypted.
2548 	 */
2549 
2550 	if (*sc_mp != NULL)
2551 		linkb(*sc_mp, mp);
2552 	else
2553 		*sc_mp = mp;
2554 }
2555 
2556 static void
2557 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2558     boolean_t encapsulated)
2559 {
2560 	boolean_t legacy = B_FALSE;
2561 
2562 	/*
2563 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2564 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2565 	 * instructed the driver to disable its advertised capabilities,
2566 	 * so there's no point in accepting any response at this moment.
2567 	 */
2568 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2569 		return;
2570 
2571 	/*
2572 	 * Note that only the following two sub-capabilities may be
2573 	 * considered as "legacy", since their original definitions
2574 	 * do not incorporate the dl_mid_t module ID token, and hence
2575 	 * may require the use of the wrapper sub-capability.
2576 	 */
2577 	switch (subp->dl_cap) {
2578 	case DL_CAPAB_IPSEC_AH:
2579 	case DL_CAPAB_IPSEC_ESP:
2580 		legacy = B_TRUE;
2581 		break;
2582 	}
2583 
2584 	/*
2585 	 * For legacy sub-capabilities which don't incorporate a queue_t
2586 	 * pointer in their structures, discard them if we detect that
2587 	 * there are intermediate modules in between IP and the driver.
2588 	 */
2589 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2590 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2591 		    "%d discarded; %d module(s) present below IP\n",
2592 		    subp->dl_cap, ill->ill_lmod_cnt));
2593 		return;
2594 	}
2595 
2596 	switch (subp->dl_cap) {
2597 	case DL_CAPAB_IPSEC_AH:
2598 	case DL_CAPAB_IPSEC_ESP:
2599 		ill_capability_ipsec_ack(ill, mp, subp);
2600 		break;
2601 	case DL_CAPAB_MDT:
2602 		ill_capability_mdt_ack(ill, mp, subp);
2603 		break;
2604 	case DL_CAPAB_HCKSUM:
2605 		ill_capability_hcksum_ack(ill, mp, subp);
2606 		break;
2607 	case DL_CAPAB_ZEROCOPY:
2608 		ill_capability_zerocopy_ack(ill, mp, subp);
2609 		break;
2610 	case DL_CAPAB_POLL:
2611 		if (!SOFT_RINGS_ENABLED())
2612 			ill_capability_dls_ack(ill, mp, subp);
2613 		break;
2614 	case DL_CAPAB_SOFT_RING:
2615 		if (SOFT_RINGS_ENABLED())
2616 			ill_capability_dls_ack(ill, mp, subp);
2617 		break;
2618 	case DL_CAPAB_LSO:
2619 		ill_capability_lso_ack(ill, mp, subp);
2620 		break;
2621 	default:
2622 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2623 		    subp->dl_cap));
2624 	}
2625 }
2626 
2627 /*
2628  * As part of negotiating polling capability, the driver tells us
2629  * the default (or normal) blanking interval and packet threshold
2630  * (the receive timer fires if blanking interval is reached or
2631  * the packet threshold is reached).
2632  *
2633  * As part of manipulating the polling interval, we always use our
2634  * estimated interval (avg service time * number of packets queued
2635  * on the squeue) but we try to blank for a minimum of
2636  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2637  * packet threshold during this time. When we are not in polling mode
2638  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2639  * rr_min_blank_ratio but up the packet cnt by a ratio of
2640  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2641  * possible although for a shorter interval.
2642  */
2643 #define	RR_MAX_BLANK_RATIO	20
2644 #define	RR_MIN_BLANK_RATIO	10
2645 #define	RR_MAX_PKT_CNT_RATIO	3
2646 #define	RR_MIN_PKT_CNT_RATIO	3
2647 
2648 /*
2649  * These can be tuned via /etc/system.
2650  */
2651 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2652 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2653 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2654 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2655 
2656 static mac_resource_handle_t
2657 ill_ring_add(void *arg, mac_resource_t *mrp)
2658 {
2659 	ill_t			*ill = (ill_t *)arg;
2660 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2661 	ill_rx_ring_t		*rx_ring;
2662 	int			ip_rx_index;
2663 
2664 	ASSERT(mrp != NULL);
2665 	if (mrp->mr_type != MAC_RX_FIFO) {
2666 		return (NULL);
2667 	}
2668 	ASSERT(ill != NULL);
2669 	ASSERT(ill->ill_dls_capab != NULL);
2670 
2671 	mutex_enter(&ill->ill_lock);
2672 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2673 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2674 		ASSERT(rx_ring != NULL);
2675 
2676 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2677 			time_t normal_blank_time =
2678 			    mrfp->mrf_normal_blank_time;
2679 			uint_t normal_pkt_cnt =
2680 			    mrfp->mrf_normal_pkt_count;
2681 
2682 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2683 
2684 	rx_ring->rr_blank = mrfp->mrf_blank;
2685 	rx_ring->rr_handle = mrfp->mrf_arg;
2686 	rx_ring->rr_ill = ill;
2687 	rx_ring->rr_normal_blank_time = normal_blank_time;
2688 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2689 
2690 			rx_ring->rr_max_blank_time =
2691 			    normal_blank_time * rr_max_blank_ratio;
2692 			rx_ring->rr_min_blank_time =
2693 			    normal_blank_time * rr_min_blank_ratio;
2694 			rx_ring->rr_max_pkt_cnt =
2695 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2696 			rx_ring->rr_min_pkt_cnt =
2697 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2698 
2699 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2700 			mutex_exit(&ill->ill_lock);
2701 
2702 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2703 			    (int), ip_rx_index);
2704 			return ((mac_resource_handle_t)rx_ring);
2705 		}
2706 	}
2707 
2708 	/*
2709 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2710 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2711 	 * should be made configurable. Meanwhile it cause no panic because
2712 	 * driver will pass ip_input a NULL handle which will make
2713 	 * IP allocate the default squeue and Polling mode will not
2714 	 * be used for this ring.
2715 	 */
2716 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2717 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2718 
2719 	mutex_exit(&ill->ill_lock);
2720 	return (NULL);
2721 }
2722 
2723 static boolean_t
2724 ill_capability_dls_init(ill_t *ill)
2725 {
2726 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2727 	conn_t 			*connp;
2728 	size_t			sz;
2729 	ip_stack_t *ipst = ill->ill_ipst;
2730 
2731 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2732 		if (ill_dls == NULL) {
2733 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2734 			    "soft_ring enabled for ill=%s (%p) but data "
2735 			    "structs uninitialized\n", ill->ill_name,
2736 			    (void *)ill);
2737 		}
2738 		return (B_TRUE);
2739 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2740 		if (ill_dls == NULL) {
2741 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2742 			    "polling enabled for ill=%s (%p) but data "
2743 			    "structs uninitialized\n", ill->ill_name,
2744 			    (void *)ill);
2745 		}
2746 		return (B_TRUE);
2747 	}
2748 
2749 	if (ill_dls != NULL) {
2750 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2751 		/* Soft_Ring or polling is being re-enabled */
2752 
2753 		connp = ill_dls->ill_unbind_conn;
2754 		ASSERT(rx_ring != NULL);
2755 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2756 		bzero((void *)rx_ring,
2757 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2758 		ill_dls->ill_ring_tbl = rx_ring;
2759 		ill_dls->ill_unbind_conn = connp;
2760 		return (B_TRUE);
2761 	}
2762 
2763 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2764 	    ipst->ips_netstack)) == NULL)
2765 		return (B_FALSE);
2766 
2767 	sz = sizeof (ill_dls_capab_t);
2768 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2769 
2770 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2771 	if (ill_dls == NULL) {
2772 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2773 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2774 		    (void *)ill);
2775 		CONN_DEC_REF(connp);
2776 		return (B_FALSE);
2777 	}
2778 
2779 	/* Allocate space to hold ring table */
2780 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2781 	ill->ill_dls_capab = ill_dls;
2782 	ill_dls->ill_unbind_conn = connp;
2783 	return (B_TRUE);
2784 }
2785 
2786 /*
2787  * ill_capability_dls_disable: disable soft_ring and/or polling
2788  * capability. Since any of the rings might already be in use, need
2789  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2790  * direct calls if necessary.
2791  */
2792 static void
2793 ill_capability_dls_disable(ill_t *ill)
2794 {
2795 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2796 
2797 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2798 		ip_squeue_clean_all(ill);
2799 		ill_dls->ill_tx = NULL;
2800 		ill_dls->ill_tx_handle = NULL;
2801 		ill_dls->ill_dls_change_status = NULL;
2802 		ill_dls->ill_dls_bind = NULL;
2803 		ill_dls->ill_dls_unbind = NULL;
2804 	}
2805 
2806 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2807 }
2808 
2809 static void
2810 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2811     dl_capability_sub_t *isub)
2812 {
2813 	uint_t			size;
2814 	uchar_t			*rptr;
2815 	dl_capab_dls_t	dls, *odls;
2816 	ill_dls_capab_t	*ill_dls;
2817 	mblk_t			*nmp = NULL;
2818 	dl_capability_req_t	*ocap;
2819 	uint_t			sub_dl_cap = isub->dl_cap;
2820 
2821 	if (!ill_capability_dls_init(ill))
2822 		return;
2823 	ill_dls = ill->ill_dls_capab;
2824 
2825 	/* Copy locally to get the members aligned */
2826 	bcopy((void *)idls, (void *)&dls,
2827 	    sizeof (dl_capab_dls_t));
2828 
2829 	/* Get the tx function and handle from dld */
2830 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2831 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2832 
2833 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2834 		ill_dls->ill_dls_change_status =
2835 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2836 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2837 		ill_dls->ill_dls_unbind =
2838 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2839 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2840 	}
2841 
2842 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2843 	    isub->dl_length;
2844 
2845 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2846 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2847 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2848 		    ill->ill_name, (void *)ill);
2849 		return;
2850 	}
2851 
2852 	/* initialize dl_capability_req_t */
2853 	rptr = nmp->b_rptr;
2854 	ocap = (dl_capability_req_t *)rptr;
2855 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2856 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2857 	rptr += sizeof (dl_capability_req_t);
2858 
2859 	/* initialize dl_capability_sub_t */
2860 	bcopy(isub, rptr, sizeof (*isub));
2861 	rptr += sizeof (*isub);
2862 
2863 	odls = (dl_capab_dls_t *)rptr;
2864 	rptr += sizeof (dl_capab_dls_t);
2865 
2866 	/* initialize dl_capab_dls_t to be sent down */
2867 	dls.dls_rx_handle = (uintptr_t)ill;
2868 	dls.dls_rx = (uintptr_t)ip_input;
2869 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2870 
2871 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2872 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2873 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2874 		dls.dls_flags = SOFT_RING_ENABLE;
2875 	} else {
2876 		dls.dls_flags = POLL_ENABLE;
2877 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2878 		    "to enable polling\n", ill->ill_name));
2879 	}
2880 	bcopy((void *)&dls, (void *)odls,
2881 	    sizeof (dl_capab_dls_t));
2882 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2883 	/*
2884 	 * nmp points to a DL_CAPABILITY_REQ message to
2885 	 * enable either soft_ring or polling
2886 	 */
2887 	ill_dlpi_send(ill, nmp);
2888 }
2889 
2890 static void
2891 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2892 {
2893 	mblk_t *mp;
2894 	dl_capab_dls_t *idls;
2895 	dl_capability_sub_t *dl_subcap;
2896 	int size;
2897 
2898 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2899 		return;
2900 
2901 	ASSERT(ill->ill_dls_capab != NULL);
2902 
2903 	size = sizeof (*dl_subcap) + sizeof (*idls);
2904 
2905 	mp = allocb(size, BPRI_HI);
2906 	if (mp == NULL) {
2907 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2908 		    "request to disable soft_ring\n"));
2909 		return;
2910 	}
2911 
2912 	mp->b_wptr = mp->b_rptr + size;
2913 
2914 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2915 	dl_subcap->dl_length = sizeof (*idls);
2916 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2917 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2918 	else
2919 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2920 
2921 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2922 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2923 		idls->dls_flags = SOFT_RING_DISABLE;
2924 	else
2925 		idls->dls_flags = POLL_DISABLE;
2926 
2927 	if (*sc_mp != NULL)
2928 		linkb(*sc_mp, mp);
2929 	else
2930 		*sc_mp = mp;
2931 }
2932 
2933 /*
2934  * Process a soft_ring/poll capability negotiation ack received
2935  * from a DLS Provider.isub must point to the sub-capability
2936  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2937  */
2938 static void
2939 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2940 {
2941 	dl_capab_dls_t		*idls;
2942 	uint_t			sub_dl_cap = isub->dl_cap;
2943 	uint8_t			*capend;
2944 
2945 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2946 	    sub_dl_cap == DL_CAPAB_POLL);
2947 
2948 	if (ill->ill_isv6)
2949 		return;
2950 
2951 	/*
2952 	 * Note: range checks here are not absolutely sufficient to
2953 	 * make us robust against malformed messages sent by drivers;
2954 	 * this is in keeping with the rest of IP's dlpi handling.
2955 	 * (Remember, it's coming from something else in the kernel
2956 	 * address space)
2957 	 */
2958 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2959 	if (capend > mp->b_wptr) {
2960 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2961 		    "malformed sub-capability too long for mblk");
2962 		return;
2963 	}
2964 
2965 	/*
2966 	 * There are two types of acks we process here:
2967 	 * 1. acks in reply to a (first form) generic capability req
2968 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2969 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2970 	 *    capability req.
2971 	 */
2972 	idls = (dl_capab_dls_t *)(isub + 1);
2973 
2974 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2975 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2976 		    "capability isn't as expected; pass-thru "
2977 		    "module(s) detected, discarding capability\n"));
2978 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2979 			/*
2980 			 * This is a capability renegotitation case.
2981 			 * The interface better be unusable at this
2982 			 * point other wise bad things will happen
2983 			 * if we disable direct calls on a running
2984 			 * and up interface.
2985 			 */
2986 			ill_capability_dls_disable(ill);
2987 		}
2988 		return;
2989 	}
2990 
2991 	switch (idls->dls_flags) {
2992 	default:
2993 		/* Disable if unknown flag */
2994 	case SOFT_RING_DISABLE:
2995 	case POLL_DISABLE:
2996 		ill_capability_dls_disable(ill);
2997 		break;
2998 	case SOFT_RING_CAPABLE:
2999 	case POLL_CAPABLE:
3000 		/*
3001 		 * If the capability was already enabled, its safe
3002 		 * to disable it first to get rid of stale information
3003 		 * and then start enabling it again.
3004 		 */
3005 		ill_capability_dls_disable(ill);
3006 		ill_capability_dls_capable(ill, idls, isub);
3007 		break;
3008 	case SOFT_RING_ENABLE:
3009 	case POLL_ENABLE:
3010 		mutex_enter(&ill->ill_lock);
3011 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3012 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3013 			ASSERT(ill->ill_dls_capab != NULL);
3014 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3015 		}
3016 		if (sub_dl_cap == DL_CAPAB_POLL &&
3017 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3018 			ASSERT(ill->ill_dls_capab != NULL);
3019 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3020 			ip1dbg(("ill_capability_dls_ack: interface %s "
3021 			    "has enabled polling\n", ill->ill_name));
3022 		}
3023 		mutex_exit(&ill->ill_lock);
3024 		break;
3025 	}
3026 }
3027 
3028 /*
3029  * Process a hardware checksum offload capability negotiation ack received
3030  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3031  * of a DL_CAPABILITY_ACK message.
3032  */
3033 static void
3034 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3035 {
3036 	dl_capability_req_t	*ocap;
3037 	dl_capab_hcksum_t	*ihck, *ohck;
3038 	ill_hcksum_capab_t	**ill_hcksum;
3039 	mblk_t			*nmp = NULL;
3040 	uint_t			sub_dl_cap = isub->dl_cap;
3041 	uint8_t			*capend;
3042 
3043 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3044 
3045 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3046 
3047 	/*
3048 	 * Note: range checks here are not absolutely sufficient to
3049 	 * make us robust against malformed messages sent by drivers;
3050 	 * this is in keeping with the rest of IP's dlpi handling.
3051 	 * (Remember, it's coming from something else in the kernel
3052 	 * address space)
3053 	 */
3054 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3055 	if (capend > mp->b_wptr) {
3056 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3057 		    "malformed sub-capability too long for mblk");
3058 		return;
3059 	}
3060 
3061 	/*
3062 	 * There are two types of acks we process here:
3063 	 * 1. acks in reply to a (first form) generic capability req
3064 	 *    (no ENABLE flag set)
3065 	 * 2. acks in reply to a ENABLE capability req.
3066 	 *    (ENABLE flag set)
3067 	 */
3068 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3069 
3070 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3071 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3072 		    "unsupported hardware checksum "
3073 		    "sub-capability (version %d, expected %d)",
3074 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3075 		return;
3076 	}
3077 
3078 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3079 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3080 		    "checksum capability isn't as expected; pass-thru "
3081 		    "module(s) detected, discarding capability\n"));
3082 		return;
3083 	}
3084 
3085 #define	CURR_HCKSUM_CAPAB				\
3086 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3087 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3088 
3089 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3090 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3091 		/* do ENABLE processing */
3092 		if (*ill_hcksum == NULL) {
3093 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3094 			    KM_NOSLEEP);
3095 
3096 			if (*ill_hcksum == NULL) {
3097 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3098 				    "could not enable hcksum version %d "
3099 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3100 				    ill->ill_name);
3101 				return;
3102 			}
3103 		}
3104 
3105 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3106 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3107 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3108 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3109 		    "has enabled hardware checksumming\n ",
3110 		    ill->ill_name));
3111 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3112 		/*
3113 		 * Enabling hardware checksum offload
3114 		 * Currently IP supports {TCP,UDP}/IPv4
3115 		 * partial and full cksum offload and
3116 		 * IPv4 header checksum offload.
3117 		 * Allocate new mblk which will
3118 		 * contain a new capability request
3119 		 * to enable hardware checksum offload.
3120 		 */
3121 		uint_t	size;
3122 		uchar_t	*rptr;
3123 
3124 		size = sizeof (dl_capability_req_t) +
3125 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3126 
3127 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3128 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3129 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3130 			    ill->ill_name);
3131 			return;
3132 		}
3133 
3134 		rptr = nmp->b_rptr;
3135 		/* initialize dl_capability_req_t */
3136 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3137 		ocap->dl_sub_offset =
3138 		    sizeof (dl_capability_req_t);
3139 		ocap->dl_sub_length =
3140 		    sizeof (dl_capability_sub_t) +
3141 		    isub->dl_length;
3142 		nmp->b_rptr += sizeof (dl_capability_req_t);
3143 
3144 		/* initialize dl_capability_sub_t */
3145 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3146 		nmp->b_rptr += sizeof (*isub);
3147 
3148 		/* initialize dl_capab_hcksum_t */
3149 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3150 		bcopy(ihck, ohck, sizeof (*ihck));
3151 
3152 		nmp->b_rptr = rptr;
3153 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3154 
3155 		/* Set ENABLE flag */
3156 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3157 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3158 
3159 		/*
3160 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3161 		 * hardware checksum acceleration.
3162 		 */
3163 		ill_dlpi_send(ill, nmp);
3164 	} else {
3165 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3166 		    "advertised %x hardware checksum capability flags\n",
3167 		    ill->ill_name, ihck->hcksum_txflags));
3168 	}
3169 }
3170 
3171 static void
3172 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3173 {
3174 	mblk_t *mp;
3175 	dl_capab_hcksum_t *hck_subcap;
3176 	dl_capability_sub_t *dl_subcap;
3177 	int size;
3178 
3179 	if (!ILL_HCKSUM_CAPABLE(ill))
3180 		return;
3181 
3182 	ASSERT(ill->ill_hcksum_capab != NULL);
3183 	/*
3184 	 * Clear the capability flag for hardware checksum offload but
3185 	 * retain the ill_hcksum_capab structure since it's possible that
3186 	 * another thread is still referring to it.  The structure only
3187 	 * gets deallocated when we destroy the ill.
3188 	 */
3189 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3190 
3191 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3192 
3193 	mp = allocb(size, BPRI_HI);
3194 	if (mp == NULL) {
3195 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3196 		    "request to disable hardware checksum offload\n"));
3197 		return;
3198 	}
3199 
3200 	mp->b_wptr = mp->b_rptr + size;
3201 
3202 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3203 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3204 	dl_subcap->dl_length = sizeof (*hck_subcap);
3205 
3206 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3207 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3208 	hck_subcap->hcksum_txflags = 0;
3209 
3210 	if (*sc_mp != NULL)
3211 		linkb(*sc_mp, mp);
3212 	else
3213 		*sc_mp = mp;
3214 }
3215 
3216 static void
3217 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3218 {
3219 	mblk_t *nmp = NULL;
3220 	dl_capability_req_t *oc;
3221 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3222 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3223 	uint_t sub_dl_cap = isub->dl_cap;
3224 	uint8_t *capend;
3225 
3226 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3227 
3228 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3229 
3230 	/*
3231 	 * Note: range checks here are not absolutely sufficient to
3232 	 * make us robust against malformed messages sent by drivers;
3233 	 * this is in keeping with the rest of IP's dlpi handling.
3234 	 * (Remember, it's coming from something else in the kernel
3235 	 * address space)
3236 	 */
3237 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3238 	if (capend > mp->b_wptr) {
3239 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3240 		    "malformed sub-capability too long for mblk");
3241 		return;
3242 	}
3243 
3244 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3245 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3246 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3247 		    "unsupported ZEROCOPY sub-capability (version %d, "
3248 		    "expected %d)", zc_ic->zerocopy_version,
3249 		    ZEROCOPY_VERSION_1);
3250 		return;
3251 	}
3252 
3253 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3254 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3255 		    "capability isn't as expected; pass-thru module(s) "
3256 		    "detected, discarding capability\n"));
3257 		return;
3258 	}
3259 
3260 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3261 		if (*ill_zerocopy_capab == NULL) {
3262 			*ill_zerocopy_capab =
3263 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3264 			    KM_NOSLEEP);
3265 
3266 			if (*ill_zerocopy_capab == NULL) {
3267 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3268 				    "could not enable Zero-copy version %d "
3269 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3270 				    ill->ill_name);
3271 				return;
3272 			}
3273 		}
3274 
3275 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3276 		    "supports Zero-copy version %d\n", ill->ill_name,
3277 		    ZEROCOPY_VERSION_1));
3278 
3279 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3280 		    zc_ic->zerocopy_version;
3281 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3282 		    zc_ic->zerocopy_flags;
3283 
3284 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3285 	} else {
3286 		uint_t size;
3287 		uchar_t *rptr;
3288 
3289 		size = sizeof (dl_capability_req_t) +
3290 		    sizeof (dl_capability_sub_t) +
3291 		    sizeof (dl_capab_zerocopy_t);
3292 
3293 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3294 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3295 			    "could not enable zerocopy for %s (ENOMEM)\n",
3296 			    ill->ill_name);
3297 			return;
3298 		}
3299 
3300 		rptr = nmp->b_rptr;
3301 		/* initialize dl_capability_req_t */
3302 		oc = (dl_capability_req_t *)rptr;
3303 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3304 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3305 		    sizeof (dl_capab_zerocopy_t);
3306 		rptr += sizeof (dl_capability_req_t);
3307 
3308 		/* initialize dl_capability_sub_t */
3309 		bcopy(isub, rptr, sizeof (*isub));
3310 		rptr += sizeof (*isub);
3311 
3312 		/* initialize dl_capab_zerocopy_t */
3313 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3314 		*zc_oc = *zc_ic;
3315 
3316 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3317 		    "to enable zero-copy version %d\n", ill->ill_name,
3318 		    ZEROCOPY_VERSION_1));
3319 
3320 		/* set VMSAFE_MEM flag */
3321 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3322 
3323 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3324 		ill_dlpi_send(ill, nmp);
3325 	}
3326 }
3327 
3328 static void
3329 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3330 {
3331 	mblk_t *mp;
3332 	dl_capab_zerocopy_t *zerocopy_subcap;
3333 	dl_capability_sub_t *dl_subcap;
3334 	int size;
3335 
3336 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3337 		return;
3338 
3339 	ASSERT(ill->ill_zerocopy_capab != NULL);
3340 	/*
3341 	 * Clear the capability flag for Zero-copy but retain the
3342 	 * ill_zerocopy_capab structure since it's possible that another
3343 	 * thread is still referring to it.  The structure only gets
3344 	 * deallocated when we destroy the ill.
3345 	 */
3346 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3347 
3348 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3349 
3350 	mp = allocb(size, BPRI_HI);
3351 	if (mp == NULL) {
3352 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3353 		    "request to disable Zero-copy\n"));
3354 		return;
3355 	}
3356 
3357 	mp->b_wptr = mp->b_rptr + size;
3358 
3359 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3360 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3361 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3362 
3363 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3364 	zerocopy_subcap->zerocopy_version =
3365 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3366 	zerocopy_subcap->zerocopy_flags = 0;
3367 
3368 	if (*sc_mp != NULL)
3369 		linkb(*sc_mp, mp);
3370 	else
3371 		*sc_mp = mp;
3372 }
3373 
3374 /*
3375  * Process Large Segment Offload capability negotiation ack received from a
3376  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3377  * DL_CAPABILITY_ACK message.
3378  */
3379 static void
3380 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3381 {
3382 	mblk_t *nmp = NULL;
3383 	dl_capability_req_t *oc;
3384 	dl_capab_lso_t *lso_ic, *lso_oc;
3385 	ill_lso_capab_t **ill_lso_capab;
3386 	uint_t sub_dl_cap = isub->dl_cap;
3387 	uint8_t *capend;
3388 
3389 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3390 
3391 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3392 
3393 	/*
3394 	 * Note: range checks here are not absolutely sufficient to
3395 	 * make us robust against malformed messages sent by drivers;
3396 	 * this is in keeping with the rest of IP's dlpi handling.
3397 	 * (Remember, it's coming from something else in the kernel
3398 	 * address space)
3399 	 */
3400 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3401 	if (capend > mp->b_wptr) {
3402 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3403 		    "malformed sub-capability too long for mblk");
3404 		return;
3405 	}
3406 
3407 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3408 
3409 	if (lso_ic->lso_version != LSO_VERSION_1) {
3410 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3411 		    "unsupported LSO sub-capability (version %d, expected %d)",
3412 		    lso_ic->lso_version, LSO_VERSION_1);
3413 		return;
3414 	}
3415 
3416 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3417 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3418 		    "capability isn't as expected; pass-thru module(s) "
3419 		    "detected, discarding capability\n"));
3420 		return;
3421 	}
3422 
3423 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3424 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3425 		if (*ill_lso_capab == NULL) {
3426 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3427 			    KM_NOSLEEP);
3428 
3429 			if (*ill_lso_capab == NULL) {
3430 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3431 				    "could not enable LSO version %d "
3432 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3433 				    ill->ill_name);
3434 				return;
3435 			}
3436 		}
3437 
3438 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3439 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3440 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3441 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3442 
3443 		ip1dbg(("ill_capability_lso_ack: interface %s "
3444 		    "has enabled LSO\n ", ill->ill_name));
3445 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3446 		uint_t size;
3447 		uchar_t *rptr;
3448 
3449 		size = sizeof (dl_capability_req_t) +
3450 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3451 
3452 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3453 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3454 			    "could not enable LSO for %s (ENOMEM)\n",
3455 			    ill->ill_name);
3456 			return;
3457 		}
3458 
3459 		rptr = nmp->b_rptr;
3460 		/* initialize dl_capability_req_t */
3461 		oc = (dl_capability_req_t *)nmp->b_rptr;
3462 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3463 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3464 		    sizeof (dl_capab_lso_t);
3465 		nmp->b_rptr += sizeof (dl_capability_req_t);
3466 
3467 		/* initialize dl_capability_sub_t */
3468 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3469 		nmp->b_rptr += sizeof (*isub);
3470 
3471 		/* initialize dl_capab_lso_t */
3472 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3473 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3474 
3475 		nmp->b_rptr = rptr;
3476 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3477 
3478 		/* set ENABLE flag */
3479 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3480 
3481 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3482 		ill_dlpi_send(ill, nmp);
3483 	} else {
3484 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3485 		    "advertised %x LSO capability flags\n",
3486 		    ill->ill_name, lso_ic->lso_flags));
3487 	}
3488 }
3489 
3490 static void
3491 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3492 {
3493 	mblk_t *mp;
3494 	dl_capab_lso_t *lso_subcap;
3495 	dl_capability_sub_t *dl_subcap;
3496 	int size;
3497 
3498 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3499 		return;
3500 
3501 	ASSERT(ill->ill_lso_capab != NULL);
3502 	/*
3503 	 * Clear the capability flag for LSO but retain the
3504 	 * ill_lso_capab structure since it's possible that another
3505 	 * thread is still referring to it.  The structure only gets
3506 	 * deallocated when we destroy the ill.
3507 	 */
3508 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3509 
3510 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3511 
3512 	mp = allocb(size, BPRI_HI);
3513 	if (mp == NULL) {
3514 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3515 		    "request to disable LSO\n"));
3516 		return;
3517 	}
3518 
3519 	mp->b_wptr = mp->b_rptr + size;
3520 
3521 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3522 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3523 	dl_subcap->dl_length = sizeof (*lso_subcap);
3524 
3525 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3526 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3527 	lso_subcap->lso_flags = 0;
3528 
3529 	if (*sc_mp != NULL)
3530 		linkb(*sc_mp, mp);
3531 	else
3532 		*sc_mp = mp;
3533 }
3534 
3535 /*
3536  * Consume a new-style hardware capabilities negotiation ack.
3537  * Called from ip_rput_dlpi_writer().
3538  */
3539 void
3540 ill_capability_ack(ill_t *ill, mblk_t *mp)
3541 {
3542 	dl_capability_ack_t *capp;
3543 	dl_capability_sub_t *subp, *endp;
3544 
3545 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3546 		ill->ill_dlpi_capab_state = IDS_OK;
3547 
3548 	capp = (dl_capability_ack_t *)mp->b_rptr;
3549 
3550 	if (capp->dl_sub_length == 0)
3551 		/* no new-style capabilities */
3552 		return;
3553 
3554 	/* make sure the driver supplied correct dl_sub_length */
3555 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3556 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3557 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3558 		return;
3559 	}
3560 
3561 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3562 	/*
3563 	 * There are sub-capabilities. Process the ones we know about.
3564 	 * Loop until we don't have room for another sub-cap header..
3565 	 */
3566 	for (subp = SC(capp, capp->dl_sub_offset),
3567 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3568 	    subp <= endp;
3569 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3570 
3571 		switch (subp->dl_cap) {
3572 		case DL_CAPAB_ID_WRAPPER:
3573 			ill_capability_id_ack(ill, mp, subp);
3574 			break;
3575 		default:
3576 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3577 			break;
3578 		}
3579 	}
3580 #undef SC
3581 }
3582 
3583 /*
3584  * This routine is called to scan the fragmentation reassembly table for
3585  * the specified ILL for any packets that are starting to smell.
3586  * dead_interval is the maximum time in seconds that will be tolerated.  It
3587  * will either be the value specified in ip_g_frag_timeout, or zero if the
3588  * ILL is shutting down and it is time to blow everything off.
3589  *
3590  * It returns the number of seconds (as a time_t) that the next frag timer
3591  * should be scheduled for, 0 meaning that the timer doesn't need to be
3592  * re-started.  Note that the method of calculating next_timeout isn't
3593  * entirely accurate since time will flow between the time we grab
3594  * current_time and the time we schedule the next timeout.  This isn't a
3595  * big problem since this is the timer for sending an ICMP reassembly time
3596  * exceeded messages, and it doesn't have to be exactly accurate.
3597  *
3598  * This function is
3599  * sometimes called as writer, although this is not required.
3600  */
3601 time_t
3602 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3603 {
3604 	ipfb_t	*ipfb;
3605 	ipfb_t	*endp;
3606 	ipf_t	*ipf;
3607 	ipf_t	*ipfnext;
3608 	mblk_t	*mp;
3609 	time_t	current_time = gethrestime_sec();
3610 	time_t	next_timeout = 0;
3611 	uint32_t	hdr_length;
3612 	mblk_t	*send_icmp_head;
3613 	mblk_t	*send_icmp_head_v6;
3614 	zoneid_t zoneid;
3615 	ip_stack_t *ipst = ill->ill_ipst;
3616 
3617 	ipfb = ill->ill_frag_hash_tbl;
3618 	if (ipfb == NULL)
3619 		return (B_FALSE);
3620 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3621 	/* Walk the frag hash table. */
3622 	for (; ipfb < endp; ipfb++) {
3623 		send_icmp_head = NULL;
3624 		send_icmp_head_v6 = NULL;
3625 		mutex_enter(&ipfb->ipfb_lock);
3626 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3627 			time_t frag_time = current_time - ipf->ipf_timestamp;
3628 			time_t frag_timeout;
3629 
3630 			if (frag_time < dead_interval) {
3631 				/*
3632 				 * There are some outstanding fragments
3633 				 * that will timeout later.  Make note of
3634 				 * the time so that we can reschedule the
3635 				 * next timeout appropriately.
3636 				 */
3637 				frag_timeout = dead_interval - frag_time;
3638 				if (next_timeout == 0 ||
3639 				    frag_timeout < next_timeout) {
3640 					next_timeout = frag_timeout;
3641 				}
3642 				break;
3643 			}
3644 			/* Time's up.  Get it out of here. */
3645 			hdr_length = ipf->ipf_nf_hdr_len;
3646 			ipfnext = ipf->ipf_hash_next;
3647 			if (ipfnext)
3648 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3649 			*ipf->ipf_ptphn = ipfnext;
3650 			mp = ipf->ipf_mp->b_cont;
3651 			for (; mp; mp = mp->b_cont) {
3652 				/* Extra points for neatness. */
3653 				IP_REASS_SET_START(mp, 0);
3654 				IP_REASS_SET_END(mp, 0);
3655 			}
3656 			mp = ipf->ipf_mp->b_cont;
3657 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3658 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3659 			ipfb->ipfb_count -= ipf->ipf_count;
3660 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3661 			ipfb->ipfb_frag_pkts--;
3662 			/*
3663 			 * We do not send any icmp message from here because
3664 			 * we currently are holding the ipfb_lock for this
3665 			 * hash chain. If we try and send any icmp messages
3666 			 * from here we may end up via a put back into ip
3667 			 * trying to get the same lock, causing a recursive
3668 			 * mutex panic. Instead we build a list and send all
3669 			 * the icmp messages after we have dropped the lock.
3670 			 */
3671 			if (ill->ill_isv6) {
3672 				if (hdr_length != 0) {
3673 					mp->b_next = send_icmp_head_v6;
3674 					send_icmp_head_v6 = mp;
3675 				} else {
3676 					freemsg(mp);
3677 				}
3678 			} else {
3679 				if (hdr_length != 0) {
3680 					mp->b_next = send_icmp_head;
3681 					send_icmp_head = mp;
3682 				} else {
3683 					freemsg(mp);
3684 				}
3685 			}
3686 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3687 			freeb(ipf->ipf_mp);
3688 		}
3689 		mutex_exit(&ipfb->ipfb_lock);
3690 		/*
3691 		 * Now need to send any icmp messages that we delayed from
3692 		 * above.
3693 		 */
3694 		while (send_icmp_head_v6 != NULL) {
3695 			ip6_t *ip6h;
3696 
3697 			mp = send_icmp_head_v6;
3698 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3699 			mp->b_next = NULL;
3700 			if (mp->b_datap->db_type == M_CTL)
3701 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3702 			else
3703 				ip6h = (ip6_t *)mp->b_rptr;
3704 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3705 			    ill, ipst);
3706 			if (zoneid == ALL_ZONES) {
3707 				freemsg(mp);
3708 			} else {
3709 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3710 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3711 				    B_FALSE, zoneid, ipst);
3712 			}
3713 		}
3714 		while (send_icmp_head != NULL) {
3715 			ipaddr_t dst;
3716 
3717 			mp = send_icmp_head;
3718 			send_icmp_head = send_icmp_head->b_next;
3719 			mp->b_next = NULL;
3720 
3721 			if (mp->b_datap->db_type == M_CTL)
3722 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3723 			else
3724 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3725 
3726 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3727 			if (zoneid == ALL_ZONES) {
3728 				freemsg(mp);
3729 			} else {
3730 				icmp_time_exceeded(ill->ill_wq, mp,
3731 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3732 				    ipst);
3733 			}
3734 		}
3735 	}
3736 	/*
3737 	 * A non-dying ILL will use the return value to decide whether to
3738 	 * restart the frag timer, and for how long.
3739 	 */
3740 	return (next_timeout);
3741 }
3742 
3743 /*
3744  * This routine is called when the approximate count of mblk memory used
3745  * for the specified ILL has exceeded max_count.
3746  */
3747 void
3748 ill_frag_prune(ill_t *ill, uint_t max_count)
3749 {
3750 	ipfb_t	*ipfb;
3751 	ipf_t	*ipf;
3752 	size_t	count;
3753 
3754 	/*
3755 	 * If we are here within ip_min_frag_prune_time msecs remove
3756 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3757 	 * ill_frag_free_num_pkts.
3758 	 */
3759 	mutex_enter(&ill->ill_lock);
3760 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3761 	    (ip_min_frag_prune_time != 0 ?
3762 	    ip_min_frag_prune_time : msec_per_tick)) {
3763 
3764 		ill->ill_frag_free_num_pkts++;
3765 
3766 	} else {
3767 		ill->ill_frag_free_num_pkts = 0;
3768 	}
3769 	ill->ill_last_frag_clean_time = lbolt;
3770 	mutex_exit(&ill->ill_lock);
3771 
3772 	/*
3773 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3774 	 */
3775 	if (ill->ill_frag_free_num_pkts != 0) {
3776 		int ix;
3777 
3778 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3779 			ipfb = &ill->ill_frag_hash_tbl[ix];
3780 			mutex_enter(&ipfb->ipfb_lock);
3781 			if (ipfb->ipfb_ipf != NULL) {
3782 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3783 				    ill->ill_frag_free_num_pkts);
3784 			}
3785 			mutex_exit(&ipfb->ipfb_lock);
3786 		}
3787 	}
3788 	/*
3789 	 * While the reassembly list for this ILL is too big, prune a fragment
3790 	 * queue by age, oldest first.
3791 	 */
3792 	while (ill->ill_frag_count > max_count) {
3793 		int	ix;
3794 		ipfb_t	*oipfb = NULL;
3795 		uint_t	oldest = UINT_MAX;
3796 
3797 		count = 0;
3798 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3799 			ipfb = &ill->ill_frag_hash_tbl[ix];
3800 			mutex_enter(&ipfb->ipfb_lock);
3801 			ipf = ipfb->ipfb_ipf;
3802 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3803 				oldest = ipf->ipf_gen;
3804 				oipfb = ipfb;
3805 			}
3806 			count += ipfb->ipfb_count;
3807 			mutex_exit(&ipfb->ipfb_lock);
3808 		}
3809 		if (oipfb == NULL)
3810 			break;
3811 
3812 		if (count <= max_count)
3813 			return;	/* Somebody beat us to it, nothing to do */
3814 		mutex_enter(&oipfb->ipfb_lock);
3815 		ipf = oipfb->ipfb_ipf;
3816 		if (ipf != NULL) {
3817 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3818 		}
3819 		mutex_exit(&oipfb->ipfb_lock);
3820 	}
3821 }
3822 
3823 /*
3824  * free 'free_cnt' fragmented packets starting at ipf.
3825  */
3826 void
3827 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3828 {
3829 	size_t	count;
3830 	mblk_t	*mp;
3831 	mblk_t	*tmp;
3832 	ipf_t **ipfp = ipf->ipf_ptphn;
3833 
3834 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3835 	ASSERT(ipfp != NULL);
3836 	ASSERT(ipf != NULL);
3837 
3838 	while (ipf != NULL && free_cnt-- > 0) {
3839 		count = ipf->ipf_count;
3840 		mp = ipf->ipf_mp;
3841 		ipf = ipf->ipf_hash_next;
3842 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3843 			IP_REASS_SET_START(tmp, 0);
3844 			IP_REASS_SET_END(tmp, 0);
3845 		}
3846 		atomic_add_32(&ill->ill_frag_count, -count);
3847 		ASSERT(ipfb->ipfb_count >= count);
3848 		ipfb->ipfb_count -= count;
3849 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3850 		ipfb->ipfb_frag_pkts--;
3851 		freemsg(mp);
3852 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3853 	}
3854 
3855 	if (ipf)
3856 		ipf->ipf_ptphn = ipfp;
3857 	ipfp[0] = ipf;
3858 }
3859 
3860 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3861 	"obsolete and may be removed in a future release of Solaris.  Use " \
3862 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3863 
3864 /*
3865  * For obsolete per-interface forwarding configuration;
3866  * called in response to ND_GET.
3867  */
3868 /* ARGSUSED */
3869 static int
3870 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3871 {
3872 	ill_t *ill = (ill_t *)cp;
3873 
3874 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3875 
3876 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3877 	return (0);
3878 }
3879 
3880 /*
3881  * For obsolete per-interface forwarding configuration;
3882  * called in response to ND_SET.
3883  */
3884 /* ARGSUSED */
3885 static int
3886 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3887     cred_t *ioc_cr)
3888 {
3889 	long value;
3890 	int retval;
3891 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3892 
3893 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3894 
3895 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3896 	    value < 0 || value > 1) {
3897 		return (EINVAL);
3898 	}
3899 
3900 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3901 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3902 	rw_exit(&ipst->ips_ill_g_lock);
3903 	return (retval);
3904 }
3905 
3906 /*
3907  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3908  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3909  * up RTS_IFINFO routing socket messages for each interface whose flags we
3910  * change.
3911  */
3912 int
3913 ill_forward_set(ill_t *ill, boolean_t enable)
3914 {
3915 	ill_group_t *illgrp;
3916 	ip_stack_t	*ipst = ill->ill_ipst;
3917 
3918 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3919 
3920 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3921 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3922 		return (0);
3923 
3924 	if (IS_LOOPBACK(ill))
3925 		return (EINVAL);
3926 
3927 	/*
3928 	 * If the ill is in an IPMP group, set the forwarding policy on all
3929 	 * members of the group to the same value.
3930 	 */
3931 	illgrp = ill->ill_group;
3932 	if (illgrp != NULL) {
3933 		ill_t *tmp_ill;
3934 
3935 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3936 		    tmp_ill = tmp_ill->ill_group_next) {
3937 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3938 			    (enable ? "Enabling" : "Disabling"),
3939 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3940 			    tmp_ill->ill_name));
3941 			mutex_enter(&tmp_ill->ill_lock);
3942 			if (enable)
3943 				tmp_ill->ill_flags |= ILLF_ROUTER;
3944 			else
3945 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3946 			mutex_exit(&tmp_ill->ill_lock);
3947 			if (tmp_ill->ill_isv6)
3948 				ill_set_nce_router_flags(tmp_ill, enable);
3949 			/* Notify routing socket listeners of this change. */
3950 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3951 		}
3952 	} else {
3953 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3954 		    (enable ? "Enabling" : "Disabling"),
3955 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3956 		mutex_enter(&ill->ill_lock);
3957 		if (enable)
3958 			ill->ill_flags |= ILLF_ROUTER;
3959 		else
3960 			ill->ill_flags &= ~ILLF_ROUTER;
3961 		mutex_exit(&ill->ill_lock);
3962 		if (ill->ill_isv6)
3963 			ill_set_nce_router_flags(ill, enable);
3964 		/* Notify routing socket listeners of this change. */
3965 		ip_rts_ifmsg(ill->ill_ipif);
3966 	}
3967 
3968 	return (0);
3969 }
3970 
3971 /*
3972  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3973  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3974  * set or clear.
3975  */
3976 static void
3977 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3978 {
3979 	ipif_t *ipif;
3980 	nce_t *nce;
3981 
3982 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3983 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3984 		if (nce != NULL) {
3985 			mutex_enter(&nce->nce_lock);
3986 			if (enable)
3987 				nce->nce_flags |= NCE_F_ISROUTER;
3988 			else
3989 				nce->nce_flags &= ~NCE_F_ISROUTER;
3990 			mutex_exit(&nce->nce_lock);
3991 			NCE_REFRELE(nce);
3992 		}
3993 	}
3994 }
3995 
3996 /*
3997  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3998  * for this ill.  Make sure the v6/v4 question has been answered about this
3999  * ill.  The creation of this ndd variable is only for backwards compatibility.
4000  * The preferred way to control per-interface IP forwarding is through the
4001  * ILLF_ROUTER interface flag.
4002  */
4003 static int
4004 ill_set_ndd_name(ill_t *ill)
4005 {
4006 	char *suffix;
4007 	ip_stack_t	*ipst = ill->ill_ipst;
4008 
4009 	ASSERT(IAM_WRITER_ILL(ill));
4010 
4011 	if (ill->ill_isv6)
4012 		suffix = ipv6_forward_suffix;
4013 	else
4014 		suffix = ipv4_forward_suffix;
4015 
4016 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4017 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4018 	/*
4019 	 * Copies over the '\0'.
4020 	 * Note that strlen(suffix) is always bounded.
4021 	 */
4022 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4023 	    strlen(suffix) + 1);
4024 
4025 	/*
4026 	 * Use of the nd table requires holding the reader lock.
4027 	 * Modifying the nd table thru nd_load/nd_unload requires
4028 	 * the writer lock.
4029 	 */
4030 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4031 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4032 	    nd_ill_forward_set, (caddr_t)ill)) {
4033 		/*
4034 		 * If the nd_load failed, it only meant that it could not
4035 		 * allocate a new bunch of room for further NDD expansion.
4036 		 * Because of that, the ill_ndd_name will be set to 0, and
4037 		 * this interface is at the mercy of the global ip_forwarding
4038 		 * variable.
4039 		 */
4040 		rw_exit(&ipst->ips_ip_g_nd_lock);
4041 		ill->ill_ndd_name = NULL;
4042 		return (ENOMEM);
4043 	}
4044 	rw_exit(&ipst->ips_ip_g_nd_lock);
4045 	return (0);
4046 }
4047 
4048 /*
4049  * Intializes the context structure and returns the first ill in the list
4050  * cuurently start_list and end_list can have values:
4051  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4052  * IP_V4_G_HEAD		Traverse IPV4 list only.
4053  * IP_V6_G_HEAD		Traverse IPV6 list only.
4054  */
4055 
4056 /*
4057  * We don't check for CONDEMNED ills here. Caller must do that if
4058  * necessary under the ill lock.
4059  */
4060 ill_t *
4061 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4062     ip_stack_t *ipst)
4063 {
4064 	ill_if_t *ifp;
4065 	ill_t *ill;
4066 	avl_tree_t *avl_tree;
4067 
4068 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4069 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4070 
4071 	/*
4072 	 * setup the lists to search
4073 	 */
4074 	if (end_list != MAX_G_HEADS) {
4075 		ctx->ctx_current_list = start_list;
4076 		ctx->ctx_last_list = end_list;
4077 	} else {
4078 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4079 		ctx->ctx_current_list = 0;
4080 	}
4081 
4082 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4083 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4084 		if (ifp != (ill_if_t *)
4085 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4086 			avl_tree = &ifp->illif_avl_by_ppa;
4087 			ill = avl_first(avl_tree);
4088 			/*
4089 			 * ill is guaranteed to be non NULL or ifp should have
4090 			 * not existed.
4091 			 */
4092 			ASSERT(ill != NULL);
4093 			return (ill);
4094 		}
4095 		ctx->ctx_current_list++;
4096 	}
4097 
4098 	return (NULL);
4099 }
4100 
4101 /*
4102  * returns the next ill in the list. ill_first() must have been called
4103  * before calling ill_next() or bad things will happen.
4104  */
4105 
4106 /*
4107  * We don't check for CONDEMNED ills here. Caller must do that if
4108  * necessary under the ill lock.
4109  */
4110 ill_t *
4111 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4112 {
4113 	ill_if_t *ifp;
4114 	ill_t *ill;
4115 	ip_stack_t	*ipst = lastill->ill_ipst;
4116 
4117 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4118 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4119 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4120 	    AVL_AFTER)) != NULL) {
4121 		return (ill);
4122 	}
4123 
4124 	/* goto next ill_ifp in the list. */
4125 	ifp = lastill->ill_ifptr->illif_next;
4126 
4127 	/* make sure not at end of circular list */
4128 	while (ifp ==
4129 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4130 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4131 			return (NULL);
4132 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4133 	}
4134 
4135 	return (avl_first(&ifp->illif_avl_by_ppa));
4136 }
4137 
4138 /*
4139  * Check interface name for correct format which is name+ppa.
4140  * name can contain characters and digits, the right most digits
4141  * make up the ppa number. use of octal is not allowed, name must contain
4142  * a ppa, return pointer to the start of ppa.
4143  * In case of error return NULL.
4144  */
4145 static char *
4146 ill_get_ppa_ptr(char *name)
4147 {
4148 	int namelen = mi_strlen(name);
4149 
4150 	int len = namelen;
4151 
4152 	name += len;
4153 	while (len > 0) {
4154 		name--;
4155 		if (*name < '0' || *name > '9')
4156 			break;
4157 		len--;
4158 	}
4159 
4160 	/* empty string, all digits, or no trailing digits */
4161 	if (len == 0 || len == (int)namelen)
4162 		return (NULL);
4163 
4164 	name++;
4165 	/* check for attempted use of octal */
4166 	if (*name == '0' && len != (int)namelen - 1)
4167 		return (NULL);
4168 	return (name);
4169 }
4170 
4171 /*
4172  * use avl tree to locate the ill.
4173  */
4174 static ill_t *
4175 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4176     ipsq_func_t func, int *error, ip_stack_t *ipst)
4177 {
4178 	char *ppa_ptr = NULL;
4179 	int len;
4180 	uint_t ppa;
4181 	ill_t *ill = NULL;
4182 	ill_if_t *ifp;
4183 	int list;
4184 	ipsq_t *ipsq;
4185 
4186 	if (error != NULL)
4187 		*error = 0;
4188 
4189 	/*
4190 	 * get ppa ptr
4191 	 */
4192 	if (isv6)
4193 		list = IP_V6_G_HEAD;
4194 	else
4195 		list = IP_V4_G_HEAD;
4196 
4197 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4198 		if (error != NULL)
4199 			*error = ENXIO;
4200 		return (NULL);
4201 	}
4202 
4203 	len = ppa_ptr - name + 1;
4204 
4205 	ppa = stoi(&ppa_ptr);
4206 
4207 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4208 
4209 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4210 		/*
4211 		 * match is done on len - 1 as the name is not null
4212 		 * terminated it contains ppa in addition to the interface
4213 		 * name.
4214 		 */
4215 		if ((ifp->illif_name_len == len) &&
4216 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4217 			break;
4218 		} else {
4219 			ifp = ifp->illif_next;
4220 		}
4221 	}
4222 
4223 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4224 		/*
4225 		 * Even the interface type does not exist.
4226 		 */
4227 		if (error != NULL)
4228 			*error = ENXIO;
4229 		return (NULL);
4230 	}
4231 
4232 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4233 	if (ill != NULL) {
4234 		/*
4235 		 * The block comment at the start of ipif_down
4236 		 * explains the use of the macros used below
4237 		 */
4238 		GRAB_CONN_LOCK(q);
4239 		mutex_enter(&ill->ill_lock);
4240 		if (ILL_CAN_LOOKUP(ill)) {
4241 			ill_refhold_locked(ill);
4242 			mutex_exit(&ill->ill_lock);
4243 			RELEASE_CONN_LOCK(q);
4244 			return (ill);
4245 		} else if (ILL_CAN_WAIT(ill, q)) {
4246 			ipsq = ill->ill_phyint->phyint_ipsq;
4247 			mutex_enter(&ipsq->ipsq_lock);
4248 			mutex_exit(&ill->ill_lock);
4249 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4250 			mutex_exit(&ipsq->ipsq_lock);
4251 			RELEASE_CONN_LOCK(q);
4252 			if (error != NULL)
4253 				*error = EINPROGRESS;
4254 			return (NULL);
4255 		}
4256 		mutex_exit(&ill->ill_lock);
4257 		RELEASE_CONN_LOCK(q);
4258 	}
4259 	if (error != NULL)
4260 		*error = ENXIO;
4261 	return (NULL);
4262 }
4263 
4264 /*
4265  * comparison function for use with avl.
4266  */
4267 static int
4268 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4269 {
4270 	uint_t ppa;
4271 	uint_t ill_ppa;
4272 
4273 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4274 
4275 	ppa = *((uint_t *)ppa_ptr);
4276 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4277 	/*
4278 	 * We want the ill with the lowest ppa to be on the
4279 	 * top.
4280 	 */
4281 	if (ill_ppa < ppa)
4282 		return (1);
4283 	if (ill_ppa > ppa)
4284 		return (-1);
4285 	return (0);
4286 }
4287 
4288 /*
4289  * remove an interface type from the global list.
4290  */
4291 static void
4292 ill_delete_interface_type(ill_if_t *interface)
4293 {
4294 	ASSERT(interface != NULL);
4295 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4296 
4297 	avl_destroy(&interface->illif_avl_by_ppa);
4298 	if (interface->illif_ppa_arena != NULL)
4299 		vmem_destroy(interface->illif_ppa_arena);
4300 
4301 	remque(interface);
4302 
4303 	mi_free(interface);
4304 }
4305 
4306 /*
4307  * remove ill from the global list.
4308  */
4309 static void
4310 ill_glist_delete(ill_t *ill)
4311 {
4312 	ip_stack_t	*ipst;
4313 
4314 	if (ill == NULL)
4315 		return;
4316 	ipst = ill->ill_ipst;
4317 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4318 
4319 	/*
4320 	 * If the ill was never inserted into the AVL tree
4321 	 * we skip the if branch.
4322 	 */
4323 	if (ill->ill_ifptr != NULL) {
4324 		/*
4325 		 * remove from AVL tree and free ppa number
4326 		 */
4327 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4328 
4329 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4330 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4331 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4332 		}
4333 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4334 			ill_delete_interface_type(ill->ill_ifptr);
4335 		}
4336 
4337 		/*
4338 		 * Indicate ill is no longer in the list.
4339 		 */
4340 		ill->ill_ifptr = NULL;
4341 		ill->ill_name_length = 0;
4342 		ill->ill_name[0] = '\0';
4343 		ill->ill_ppa = UINT_MAX;
4344 	}
4345 
4346 	/*
4347 	 * Run the unplumb hook after the NIC has disappeared from being
4348 	 * visible so that attempts to revalidate its existance will fail.
4349 	 *
4350 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4351 	 * that the ordering of delivered events to listeners matches the
4352 	 * order of them in the kernel.
4353 	 */
4354 	mutex_enter(&ill->ill_lock);
4355 	ill_nic_info_dispatch(ill);
4356 	mutex_exit(&ill->ill_lock);
4357 
4358 	/* Generate NE_UNPLUMB event for ill_name. */
4359 	(void) ill_hook_event_create(ill, 0, NE_UNPLUMB, ill->ill_name,
4360 	    ill->ill_name_length);
4361 
4362 	ill_phyint_free(ill);
4363 
4364 	mutex_enter(&ill->ill_lock);
4365 	ill_nic_info_dispatch(ill);
4366 	mutex_exit(&ill->ill_lock);
4367 
4368 	rw_exit(&ipst->ips_ill_g_lock);
4369 }
4370 
4371 /*
4372  * allocate a ppa, if the number of plumbed interfaces of this type are
4373  * less than ill_no_arena do a linear search to find a unused ppa.
4374  * When the number goes beyond ill_no_arena switch to using an arena.
4375  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4376  * is the return value for an error condition, so allocation starts at one
4377  * and is decremented by one.
4378  */
4379 static int
4380 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4381 {
4382 	ill_t *tmp_ill;
4383 	uint_t start, end;
4384 	int ppa;
4385 
4386 	if (ifp->illif_ppa_arena == NULL &&
4387 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4388 		/*
4389 		 * Create an arena.
4390 		 */
4391 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4392 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4393 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4394 			/* allocate what has already been assigned */
4395 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4396 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4397 		    tmp_ill, AVL_AFTER)) {
4398 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4399 			    1,		/* size */
4400 			    1,		/* align/quantum */
4401 			    0,		/* phase */
4402 			    0,		/* nocross */
4403 			    /* minaddr */
4404 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4405 			    /* maxaddr */
4406 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4407 			    VM_NOSLEEP|VM_FIRSTFIT);
4408 			if (ppa == 0) {
4409 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4410 				    " failed while switching"));
4411 				vmem_destroy(ifp->illif_ppa_arena);
4412 				ifp->illif_ppa_arena = NULL;
4413 				break;
4414 			}
4415 		}
4416 	}
4417 
4418 	if (ifp->illif_ppa_arena != NULL) {
4419 		if (ill->ill_ppa == UINT_MAX) {
4420 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4421 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4422 			if (ppa == 0)
4423 				return (EAGAIN);
4424 			ill->ill_ppa = --ppa;
4425 		} else {
4426 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4427 			    1, 		/* size */
4428 			    1, 		/* align/quantum */
4429 			    0, 		/* phase */
4430 			    0, 		/* nocross */
4431 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4432 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4433 			    VM_NOSLEEP|VM_FIRSTFIT);
4434 			/*
4435 			 * Most likely the allocation failed because
4436 			 * the requested ppa was in use.
4437 			 */
4438 			if (ppa == 0)
4439 				return (EEXIST);
4440 		}
4441 		return (0);
4442 	}
4443 
4444 	/*
4445 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4446 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4447 	 */
4448 	if (ill->ill_ppa == UINT_MAX) {
4449 		end = UINT_MAX - 1;
4450 		start = 0;
4451 	} else {
4452 		end = start = ill->ill_ppa;
4453 	}
4454 
4455 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4456 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4457 		if (start++ >= end) {
4458 			if (ill->ill_ppa == UINT_MAX)
4459 				return (EAGAIN);
4460 			else
4461 				return (EEXIST);
4462 		}
4463 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4464 	}
4465 	ill->ill_ppa = start;
4466 	return (0);
4467 }
4468 
4469 /*
4470  * Insert ill into the list of configured ill's. Once this function completes,
4471  * the ill is globally visible and is available through lookups. More precisely
4472  * this happens after the caller drops the ill_g_lock.
4473  */
4474 static int
4475 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4476 {
4477 	ill_if_t *ill_interface;
4478 	avl_index_t where = 0;
4479 	int error;
4480 	int name_length;
4481 	int index;
4482 	boolean_t check_length = B_FALSE;
4483 	ip_stack_t	*ipst = ill->ill_ipst;
4484 
4485 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4486 
4487 	name_length = mi_strlen(name) + 1;
4488 
4489 	if (isv6)
4490 		index = IP_V6_G_HEAD;
4491 	else
4492 		index = IP_V4_G_HEAD;
4493 
4494 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4495 	/*
4496 	 * Search for interface type based on name
4497 	 */
4498 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4499 		if ((ill_interface->illif_name_len == name_length) &&
4500 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4501 			break;
4502 		}
4503 		ill_interface = ill_interface->illif_next;
4504 	}
4505 
4506 	/*
4507 	 * Interface type not found, create one.
4508 	 */
4509 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4510 
4511 		ill_g_head_t ghead;
4512 
4513 		/*
4514 		 * allocate ill_if_t structure
4515 		 */
4516 
4517 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4518 		if (ill_interface == NULL) {
4519 			return (ENOMEM);
4520 		}
4521 
4522 
4523 
4524 		(void) strcpy(ill_interface->illif_name, name);
4525 		ill_interface->illif_name_len = name_length;
4526 
4527 		avl_create(&ill_interface->illif_avl_by_ppa,
4528 		    ill_compare_ppa, sizeof (ill_t),
4529 		    offsetof(struct ill_s, ill_avl_byppa));
4530 
4531 		/*
4532 		 * link the structure in the back to maintain order
4533 		 * of configuration for ifconfig output.
4534 		 */
4535 		ghead = ipst->ips_ill_g_heads[index];
4536 		insque(ill_interface, ghead.ill_g_list_tail);
4537 
4538 	}
4539 
4540 	if (ill->ill_ppa == UINT_MAX)
4541 		check_length = B_TRUE;
4542 
4543 	error = ill_alloc_ppa(ill_interface, ill);
4544 	if (error != 0) {
4545 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4546 			ill_delete_interface_type(ill->ill_ifptr);
4547 		return (error);
4548 	}
4549 
4550 	/*
4551 	 * When the ppa is choosen by the system, check that there is
4552 	 * enough space to insert ppa. if a specific ppa was passed in this
4553 	 * check is not required as the interface name passed in will have
4554 	 * the right ppa in it.
4555 	 */
4556 	if (check_length) {
4557 		/*
4558 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4559 		 */
4560 		char buf[sizeof (uint_t) * 3];
4561 
4562 		/*
4563 		 * convert ppa to string to calculate the amount of space
4564 		 * required for it in the name.
4565 		 */
4566 		numtos(ill->ill_ppa, buf);
4567 
4568 		/* Do we have enough space to insert ppa ? */
4569 
4570 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4571 			/* Free ppa and interface type struct */
4572 			if (ill_interface->illif_ppa_arena != NULL) {
4573 				vmem_free(ill_interface->illif_ppa_arena,
4574 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4575 			}
4576 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4577 				ill_delete_interface_type(ill->ill_ifptr);
4578 
4579 			return (EINVAL);
4580 		}
4581 	}
4582 
4583 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4584 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4585 
4586 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4587 	    &where);
4588 	ill->ill_ifptr = ill_interface;
4589 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4590 
4591 	ill_phyint_reinit(ill);
4592 	return (0);
4593 }
4594 
4595 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4596 static boolean_t
4597 ipsq_init(ill_t *ill)
4598 {
4599 	ipsq_t  *ipsq;
4600 
4601 	/* Init the ipsq and impicitly enter as writer */
4602 	ill->ill_phyint->phyint_ipsq =
4603 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4604 	if (ill->ill_phyint->phyint_ipsq == NULL)
4605 		return (B_FALSE);
4606 	ipsq = ill->ill_phyint->phyint_ipsq;
4607 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4608 	ill->ill_phyint->phyint_ipsq_next = NULL;
4609 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4610 	ipsq->ipsq_refs = 1;
4611 	ipsq->ipsq_writer = curthread;
4612 	ipsq->ipsq_reentry_cnt = 1;
4613 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4614 #ifdef DEBUG
4615 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4616 	    IPSQ_STACK_DEPTH);
4617 #endif
4618 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4619 	return (B_TRUE);
4620 }
4621 
4622 /*
4623  * ill_init is called by ip_open when a device control stream is opened.
4624  * It does a few initializations, and shoots a DL_INFO_REQ message down
4625  * to the driver.  The response is later picked up in ip_rput_dlpi and
4626  * used to set up default mechanisms for talking to the driver.  (Always
4627  * called as writer.)
4628  *
4629  * If this function returns error, ip_open will call ip_close which in
4630  * turn will call ill_delete to clean up any memory allocated here that
4631  * is not yet freed.
4632  */
4633 int
4634 ill_init(queue_t *q, ill_t *ill)
4635 {
4636 	int	count;
4637 	dl_info_req_t	*dlir;
4638 	mblk_t	*info_mp;
4639 	uchar_t *frag_ptr;
4640 
4641 	/*
4642 	 * The ill is initialized to zero by mi_alloc*(). In addition
4643 	 * some fields already contain valid values, initialized in
4644 	 * ip_open(), before we reach here.
4645 	 */
4646 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4647 
4648 	ill->ill_rq = q;
4649 	ill->ill_wq = WR(q);
4650 
4651 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4652 	    BPRI_HI);
4653 	if (info_mp == NULL)
4654 		return (ENOMEM);
4655 
4656 	/*
4657 	 * Allocate sufficient space to contain our fragment hash table and
4658 	 * the device name.
4659 	 */
4660 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4661 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4662 	if (frag_ptr == NULL) {
4663 		freemsg(info_mp);
4664 		return (ENOMEM);
4665 	}
4666 	ill->ill_frag_ptr = frag_ptr;
4667 	ill->ill_frag_free_num_pkts = 0;
4668 	ill->ill_last_frag_clean_time = 0;
4669 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4670 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4671 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4672 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4673 		    NULL, MUTEX_DEFAULT, NULL);
4674 	}
4675 
4676 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4677 	if (ill->ill_phyint == NULL) {
4678 		freemsg(info_mp);
4679 		mi_free(frag_ptr);
4680 		return (ENOMEM);
4681 	}
4682 
4683 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4684 	/*
4685 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4686 	 * at this point because of the following reason. If we can't
4687 	 * enter the ipsq at some point and cv_wait, the writer that
4688 	 * wakes us up tries to locate us using the list of all phyints
4689 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4690 	 * If we don't set it now, we risk a missed wakeup.
4691 	 */
4692 	ill->ill_phyint->phyint_illv4 = ill;
4693 	ill->ill_ppa = UINT_MAX;
4694 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4695 
4696 	if (!ipsq_init(ill)) {
4697 		freemsg(info_mp);
4698 		mi_free(frag_ptr);
4699 		mi_free(ill->ill_phyint);
4700 		return (ENOMEM);
4701 	}
4702 
4703 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4704 
4705 	/* Frag queue limit stuff */
4706 	ill->ill_frag_count = 0;
4707 	ill->ill_ipf_gen = 0;
4708 
4709 	ill->ill_global_timer = INFINITY;
4710 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4711 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4712 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4713 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4714 
4715 	/*
4716 	 * Initialize IPv6 configuration variables.  The IP module is always
4717 	 * opened as an IPv4 module.  Instead tracking down the cases where
4718 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4719 	 * here for convenience, this has no effect until the ill is set to do
4720 	 * IPv6.
4721 	 */
4722 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4723 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4724 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4725 	ill->ill_max_buf = ND_MAX_Q;
4726 	ill->ill_refcnt = 0;
4727 
4728 	/* Send down the Info Request to the driver. */
4729 	info_mp->b_datap->db_type = M_PCPROTO;
4730 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4731 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4732 	dlir->dl_primitive = DL_INFO_REQ;
4733 
4734 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4735 
4736 	qprocson(q);
4737 	ill_dlpi_send(ill, info_mp);
4738 
4739 	return (0);
4740 }
4741 
4742 /*
4743  * ill_dls_info
4744  * creates datalink socket info from the device.
4745  */
4746 int
4747 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4748 {
4749 	size_t	len;
4750 	ill_t	*ill = ipif->ipif_ill;
4751 
4752 	sdl->sdl_family = AF_LINK;
4753 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4754 	sdl->sdl_type = ill->ill_type;
4755 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4756 	len = strlen(sdl->sdl_data);
4757 	ASSERT(len < 256);
4758 	sdl->sdl_nlen = (uchar_t)len;
4759 	sdl->sdl_alen = ill->ill_phys_addr_length;
4760 	sdl->sdl_slen = 0;
4761 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4762 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4763 
4764 	return (sizeof (struct sockaddr_dl));
4765 }
4766 
4767 /*
4768  * ill_xarp_info
4769  * creates xarp info from the device.
4770  */
4771 static int
4772 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4773 {
4774 	sdl->sdl_family = AF_LINK;
4775 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4776 	sdl->sdl_type = ill->ill_type;
4777 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4778 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4779 	sdl->sdl_alen = ill->ill_phys_addr_length;
4780 	sdl->sdl_slen = 0;
4781 	return (sdl->sdl_nlen);
4782 }
4783 
4784 static int
4785 loopback_kstat_update(kstat_t *ksp, int rw)
4786 {
4787 	kstat_named_t *kn;
4788 	netstackid_t	stackid;
4789 	netstack_t	*ns;
4790 	ip_stack_t	*ipst;
4791 
4792 	if (ksp == NULL || ksp->ks_data == NULL)
4793 		return (EIO);
4794 
4795 	if (rw == KSTAT_WRITE)
4796 		return (EACCES);
4797 
4798 	kn = KSTAT_NAMED_PTR(ksp);
4799 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4800 
4801 	ns = netstack_find_by_stackid(stackid);
4802 	if (ns == NULL)
4803 		return (-1);
4804 
4805 	ipst = ns->netstack_ip;
4806 	if (ipst == NULL) {
4807 		netstack_rele(ns);
4808 		return (-1);
4809 	}
4810 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4811 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4812 	netstack_rele(ns);
4813 	return (0);
4814 }
4815 
4816 /*
4817  * Has ifindex been plumbed already.
4818  * Compares both phyint_ifindex and phyint_group_ifindex.
4819  */
4820 static boolean_t
4821 phyint_exists(uint_t index, ip_stack_t *ipst)
4822 {
4823 	phyint_t *phyi;
4824 
4825 	ASSERT(index != 0);
4826 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4827 	/*
4828 	 * Indexes are stored in the phyint - a common structure
4829 	 * to both IPv4 and IPv6.
4830 	 */
4831 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4832 	for (; phyi != NULL;
4833 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4834 	    phyi, AVL_AFTER)) {
4835 		if (phyi->phyint_ifindex == index ||
4836 		    phyi->phyint_group_ifindex == index)
4837 			return (B_TRUE);
4838 	}
4839 	return (B_FALSE);
4840 }
4841 
4842 /* Pick a unique ifindex */
4843 boolean_t
4844 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4845 {
4846 	uint_t starting_index;
4847 
4848 	if (!ipst->ips_ill_index_wrap) {
4849 		*indexp = ipst->ips_ill_index++;
4850 		if (ipst->ips_ill_index == 0) {
4851 			/* Reached the uint_t limit Next time wrap  */
4852 			ipst->ips_ill_index_wrap = B_TRUE;
4853 		}
4854 		return (B_TRUE);
4855 	}
4856 
4857 	/*
4858 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4859 	 * at this point and don't want to call any function that attempts
4860 	 * to get the lock again.
4861 	 */
4862 	starting_index = ipst->ips_ill_index++;
4863 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4864 		if (ipst->ips_ill_index != 0 &&
4865 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4866 			/* found unused index - use it */
4867 			*indexp = ipst->ips_ill_index;
4868 			return (B_TRUE);
4869 		}
4870 	}
4871 
4872 	/*
4873 	 * all interface indicies are inuse.
4874 	 */
4875 	return (B_FALSE);
4876 }
4877 
4878 /*
4879  * Assign a unique interface index for the phyint.
4880  */
4881 static boolean_t
4882 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4883 {
4884 	ASSERT(phyi->phyint_ifindex == 0);
4885 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4886 }
4887 
4888 /*
4889  * Return a pointer to the ill which matches the supplied name.  Note that
4890  * the ill name length includes the null termination character.  (May be
4891  * called as writer.)
4892  * If do_alloc and the interface is "lo0" it will be automatically created.
4893  * Cannot bump up reference on condemned ills. So dup detect can't be done
4894  * using this func.
4895  */
4896 ill_t *
4897 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4898     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4899     ip_stack_t *ipst)
4900 {
4901 	ill_t	*ill;
4902 	ipif_t	*ipif;
4903 	kstat_named_t	*kn;
4904 	boolean_t isloopback;
4905 	ipsq_t *old_ipsq;
4906 	in6_addr_t ov6addr;
4907 
4908 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4909 
4910 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4911 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4912 	rw_exit(&ipst->ips_ill_g_lock);
4913 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4914 		return (ill);
4915 
4916 	/*
4917 	 * Couldn't find it.  Does this happen to be a lookup for the
4918 	 * loopback device and are we allowed to allocate it?
4919 	 */
4920 	if (!isloopback || !do_alloc)
4921 		return (NULL);
4922 
4923 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4924 
4925 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4926 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4927 		rw_exit(&ipst->ips_ill_g_lock);
4928 		return (ill);
4929 	}
4930 
4931 	/* Create the loopback device on demand */
4932 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4933 	    sizeof (ipif_loopback_name), BPRI_MED));
4934 	if (ill == NULL)
4935 		goto done;
4936 
4937 	*ill = ill_null;
4938 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4939 	ill->ill_ipst = ipst;
4940 	netstack_hold(ipst->ips_netstack);
4941 	/*
4942 	 * For exclusive stacks we set the zoneid to zero
4943 	 * to make IP operate as if in the global zone.
4944 	 */
4945 	ill->ill_zoneid = GLOBAL_ZONEID;
4946 
4947 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4948 	if (ill->ill_phyint == NULL)
4949 		goto done;
4950 
4951 	if (isv6)
4952 		ill->ill_phyint->phyint_illv6 = ill;
4953 	else
4954 		ill->ill_phyint->phyint_illv4 = ill;
4955 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4956 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4957 	/* Add room for tcp+ip headers */
4958 	if (isv6) {
4959 		ill->ill_isv6 = B_TRUE;
4960 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4961 	} else {
4962 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4963 	}
4964 	if (!ill_allocate_mibs(ill))
4965 		goto done;
4966 	ill->ill_max_mtu = ill->ill_max_frag;
4967 	/*
4968 	 * ipif_loopback_name can't be pointed at directly because its used
4969 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4970 	 * from the glist, ill_glist_delete() sets the first character of
4971 	 * ill_name to '\0'.
4972 	 */
4973 	ill->ill_name = (char *)ill + sizeof (*ill);
4974 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4975 	ill->ill_name_length = sizeof (ipif_loopback_name);
4976 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4977 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4978 
4979 	ill->ill_global_timer = INFINITY;
4980 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4981 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4982 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4983 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4984 
4985 	/* No resolver here. */
4986 	ill->ill_net_type = IRE_LOOPBACK;
4987 
4988 	/* Initialize the ipsq */
4989 	if (!ipsq_init(ill))
4990 		goto done;
4991 
4992 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4993 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4994 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4995 #ifdef DEBUG
4996 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4997 #endif
4998 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4999 	if (ipif == NULL)
5000 		goto done;
5001 
5002 	ill->ill_flags = ILLF_MULTICAST;
5003 
5004 	ov6addr = ipif->ipif_v6lcl_addr;
5005 	/* Set up default loopback address and mask. */
5006 	if (!isv6) {
5007 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5008 
5009 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5010 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5011 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5012 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5013 		    ipif->ipif_v6subnet);
5014 		ill->ill_flags |= ILLF_IPV4;
5015 	} else {
5016 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5017 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5018 		ipif->ipif_v6net_mask = ipv6_all_ones;
5019 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5020 		    ipif->ipif_v6subnet);
5021 		ill->ill_flags |= ILLF_IPV6;
5022 	}
5023 
5024 	/*
5025 	 * Chain us in at the end of the ill list. hold the ill
5026 	 * before we make it globally visible. 1 for the lookup.
5027 	 */
5028 	ill->ill_refcnt = 0;
5029 	ill_refhold(ill);
5030 
5031 	ill->ill_frag_count = 0;
5032 	ill->ill_frag_free_num_pkts = 0;
5033 	ill->ill_last_frag_clean_time = 0;
5034 
5035 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5036 
5037 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5038 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5039 
5040 	/* Let SCTP know so that it can add this to its list */
5041 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5042 
5043 	/*
5044 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5045 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5046 	 * requires to be after ill_glist_insert() since we need the
5047 	 * ill_index set. Pass on ipv6_loopback as the old address.
5048 	 */
5049 	sctp_update_ipif_addr(ipif, ov6addr);
5050 
5051 	/*
5052 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5053 	 */
5054 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5055 		/* Loopback ills aren't in any IPMP group */
5056 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5057 		ipsq_delete(old_ipsq);
5058 	}
5059 
5060 	/*
5061 	 * Delay this till the ipif is allocated as ipif_allocate
5062 	 * de-references ill_phyint for getting the ifindex. We
5063 	 * can't do this before ipif_allocate because ill_phyint_reinit
5064 	 * -> phyint_assign_ifindex expects ipif to be present.
5065 	 */
5066 	mutex_enter(&ill->ill_phyint->phyint_lock);
5067 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5068 	mutex_exit(&ill->ill_phyint->phyint_lock);
5069 
5070 	if (ipst->ips_loopback_ksp == NULL) {
5071 		/* Export loopback interface statistics */
5072 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5073 		    ipif_loopback_name, "net",
5074 		    KSTAT_TYPE_NAMED, 2, 0,
5075 		    ipst->ips_netstack->netstack_stackid);
5076 		if (ipst->ips_loopback_ksp != NULL) {
5077 			ipst->ips_loopback_ksp->ks_update =
5078 			    loopback_kstat_update;
5079 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5080 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5081 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5082 			ipst->ips_loopback_ksp->ks_private =
5083 			    (void *)(uintptr_t)ipst->ips_netstack->
5084 			    netstack_stackid;
5085 			kstat_install(ipst->ips_loopback_ksp);
5086 		}
5087 	}
5088 
5089 	if (error != NULL)
5090 		*error = 0;
5091 	*did_alloc = B_TRUE;
5092 	rw_exit(&ipst->ips_ill_g_lock);
5093 	return (ill);
5094 done:
5095 	if (ill != NULL) {
5096 		if (ill->ill_phyint != NULL) {
5097 			ipsq_t	*ipsq;
5098 
5099 			ipsq = ill->ill_phyint->phyint_ipsq;
5100 			if (ipsq != NULL) {
5101 				ipsq->ipsq_ipst = NULL;
5102 				kmem_free(ipsq, sizeof (ipsq_t));
5103 			}
5104 			mi_free(ill->ill_phyint);
5105 		}
5106 		ill_free_mib(ill);
5107 		if (ill->ill_ipst != NULL)
5108 			netstack_rele(ill->ill_ipst->ips_netstack);
5109 		mi_free(ill);
5110 	}
5111 	rw_exit(&ipst->ips_ill_g_lock);
5112 	if (error != NULL)
5113 		*error = ENOMEM;
5114 	return (NULL);
5115 }
5116 
5117 /*
5118  * For IPP calls - use the ip_stack_t for global stack.
5119  */
5120 ill_t *
5121 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5122     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5123 {
5124 	ip_stack_t	*ipst;
5125 	ill_t		*ill;
5126 
5127 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5128 	if (ipst == NULL) {
5129 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5130 		return (NULL);
5131 	}
5132 
5133 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5134 	netstack_rele(ipst->ips_netstack);
5135 	return (ill);
5136 }
5137 
5138 /*
5139  * Return a pointer to the ill which matches the index and IP version type.
5140  */
5141 ill_t *
5142 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5143     ipsq_func_t func, int *err, ip_stack_t *ipst)
5144 {
5145 	ill_t	*ill;
5146 	ipsq_t  *ipsq;
5147 	phyint_t *phyi;
5148 
5149 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5150 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5151 
5152 	if (err != NULL)
5153 		*err = 0;
5154 
5155 	/*
5156 	 * Indexes are stored in the phyint - a common structure
5157 	 * to both IPv4 and IPv6.
5158 	 */
5159 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5160 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5161 	    (void *) &index, NULL);
5162 	if (phyi != NULL) {
5163 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5164 		if (ill != NULL) {
5165 			/*
5166 			 * The block comment at the start of ipif_down
5167 			 * explains the use of the macros used below
5168 			 */
5169 			GRAB_CONN_LOCK(q);
5170 			mutex_enter(&ill->ill_lock);
5171 			if (ILL_CAN_LOOKUP(ill)) {
5172 				ill_refhold_locked(ill);
5173 				mutex_exit(&ill->ill_lock);
5174 				RELEASE_CONN_LOCK(q);
5175 				rw_exit(&ipst->ips_ill_g_lock);
5176 				return (ill);
5177 			} else if (ILL_CAN_WAIT(ill, q)) {
5178 				ipsq = ill->ill_phyint->phyint_ipsq;
5179 				mutex_enter(&ipsq->ipsq_lock);
5180 				rw_exit(&ipst->ips_ill_g_lock);
5181 				mutex_exit(&ill->ill_lock);
5182 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5183 				mutex_exit(&ipsq->ipsq_lock);
5184 				RELEASE_CONN_LOCK(q);
5185 				if (err != NULL)
5186 					*err = EINPROGRESS;
5187 				return (NULL);
5188 			}
5189 			RELEASE_CONN_LOCK(q);
5190 			mutex_exit(&ill->ill_lock);
5191 		}
5192 	}
5193 	rw_exit(&ipst->ips_ill_g_lock);
5194 	if (err != NULL)
5195 		*err = ENXIO;
5196 	return (NULL);
5197 }
5198 
5199 /*
5200  * Return the ifindex next in sequence after the passed in ifindex.
5201  * If there is no next ifindex for the given protocol, return 0.
5202  */
5203 uint_t
5204 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5205 {
5206 	phyint_t *phyi;
5207 	phyint_t *phyi_initial;
5208 	uint_t   ifindex;
5209 
5210 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5211 
5212 	if (index == 0) {
5213 		phyi = avl_first(
5214 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5215 	} else {
5216 		phyi = phyi_initial = avl_find(
5217 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5218 		    (void *) &index, NULL);
5219 	}
5220 
5221 	for (; phyi != NULL;
5222 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5223 	    phyi, AVL_AFTER)) {
5224 		/*
5225 		 * If we're not returning the first interface in the tree
5226 		 * and we still haven't moved past the phyint_t that
5227 		 * corresponds to index, avl_walk needs to be called again
5228 		 */
5229 		if (!((index != 0) && (phyi == phyi_initial))) {
5230 			if (isv6) {
5231 				if ((phyi->phyint_illv6) &&
5232 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5233 				    (phyi->phyint_illv6->ill_isv6 == 1))
5234 					break;
5235 			} else {
5236 				if ((phyi->phyint_illv4) &&
5237 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5238 				    (phyi->phyint_illv4->ill_isv6 == 0))
5239 					break;
5240 			}
5241 		}
5242 	}
5243 
5244 	rw_exit(&ipst->ips_ill_g_lock);
5245 
5246 	if (phyi != NULL)
5247 		ifindex = phyi->phyint_ifindex;
5248 	else
5249 		ifindex = 0;
5250 
5251 	return (ifindex);
5252 }
5253 
5254 /*
5255  * Return the ifindex for the named interface.
5256  * If there is no next ifindex for the interface, return 0.
5257  */
5258 uint_t
5259 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5260 {
5261 	phyint_t	*phyi;
5262 	avl_index_t	where = 0;
5263 	uint_t		ifindex;
5264 
5265 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5266 
5267 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5268 	    name, &where)) == NULL) {
5269 		rw_exit(&ipst->ips_ill_g_lock);
5270 		return (0);
5271 	}
5272 
5273 	ifindex = phyi->phyint_ifindex;
5274 
5275 	rw_exit(&ipst->ips_ill_g_lock);
5276 
5277 	return (ifindex);
5278 }
5279 
5280 /*
5281  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5282  * that gives a running thread a reference to the ill. This reference must be
5283  * released by the thread when it is done accessing the ill and related
5284  * objects. ill_refcnt can not be used to account for static references
5285  * such as other structures pointing to an ill. Callers must generally
5286  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5287  * or be sure that the ill is not being deleted or changing state before
5288  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5289  * ill won't change any of its critical state such as address, netmask etc.
5290  */
5291 void
5292 ill_refhold(ill_t *ill)
5293 {
5294 	mutex_enter(&ill->ill_lock);
5295 	ill->ill_refcnt++;
5296 	ILL_TRACE_REF(ill);
5297 	mutex_exit(&ill->ill_lock);
5298 }
5299 
5300 void
5301 ill_refhold_locked(ill_t *ill)
5302 {
5303 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5304 	ill->ill_refcnt++;
5305 	ILL_TRACE_REF(ill);
5306 }
5307 
5308 int
5309 ill_check_and_refhold(ill_t *ill)
5310 {
5311 	mutex_enter(&ill->ill_lock);
5312 	if (ILL_CAN_LOOKUP(ill)) {
5313 		ill_refhold_locked(ill);
5314 		mutex_exit(&ill->ill_lock);
5315 		return (0);
5316 	}
5317 	mutex_exit(&ill->ill_lock);
5318 	return (ILL_LOOKUP_FAILED);
5319 }
5320 
5321 /*
5322  * Must not be called while holding any locks. Otherwise if this is
5323  * the last reference to be released, there is a chance of recursive mutex
5324  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5325  * to restart an ioctl.
5326  */
5327 void
5328 ill_refrele(ill_t *ill)
5329 {
5330 	mutex_enter(&ill->ill_lock);
5331 	ASSERT(ill->ill_refcnt != 0);
5332 	ill->ill_refcnt--;
5333 	ILL_UNTRACE_REF(ill);
5334 	if (ill->ill_refcnt != 0) {
5335 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5336 		mutex_exit(&ill->ill_lock);
5337 		return;
5338 	}
5339 
5340 	/* Drops the ill_lock */
5341 	ipif_ill_refrele_tail(ill);
5342 }
5343 
5344 /*
5345  * Obtain a weak reference count on the ill. This reference ensures the
5346  * ill won't be freed, but the ill may change any of its critical state
5347  * such as netmask, address etc. Returns an error if the ill has started
5348  * closing.
5349  */
5350 boolean_t
5351 ill_waiter_inc(ill_t *ill)
5352 {
5353 	mutex_enter(&ill->ill_lock);
5354 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5355 		mutex_exit(&ill->ill_lock);
5356 		return (B_FALSE);
5357 	}
5358 	ill->ill_waiters++;
5359 	mutex_exit(&ill->ill_lock);
5360 	return (B_TRUE);
5361 }
5362 
5363 void
5364 ill_waiter_dcr(ill_t *ill)
5365 {
5366 	mutex_enter(&ill->ill_lock);
5367 	ill->ill_waiters--;
5368 	if (ill->ill_waiters == 0)
5369 		cv_broadcast(&ill->ill_cv);
5370 	mutex_exit(&ill->ill_lock);
5371 }
5372 
5373 /*
5374  * Named Dispatch routine to produce a formatted report on all ILLs.
5375  * This report is accessed by using the ndd utility to "get" ND variable
5376  * "ip_ill_status".
5377  */
5378 /* ARGSUSED */
5379 int
5380 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5381 {
5382 	ill_t		*ill;
5383 	ill_walk_context_t ctx;
5384 	ip_stack_t	*ipst;
5385 
5386 	ipst = CONNQ_TO_IPST(q);
5387 
5388 	(void) mi_mpprintf(mp,
5389 	    "ILL      " MI_COL_HDRPAD_STR
5390 	/*   01234567[89ABCDEF] */
5391 	    "rq       " MI_COL_HDRPAD_STR
5392 	/*   01234567[89ABCDEF] */
5393 	    "wq       " MI_COL_HDRPAD_STR
5394 	/*   01234567[89ABCDEF] */
5395 	    "upcnt mxfrg err name");
5396 	/*   12345 12345 123 xxxxxxxx  */
5397 
5398 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5399 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5400 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5401 		(void) mi_mpprintf(mp,
5402 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5403 		    "%05u %05u %03d %s",
5404 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5405 		    ill->ill_ipif_up_count,
5406 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5407 	}
5408 	rw_exit(&ipst->ips_ill_g_lock);
5409 
5410 	return (0);
5411 }
5412 
5413 /*
5414  * Named Dispatch routine to produce a formatted report on all IPIFs.
5415  * This report is accessed by using the ndd utility to "get" ND variable
5416  * "ip_ipif_status".
5417  */
5418 /* ARGSUSED */
5419 int
5420 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5421 {
5422 	char	buf1[INET6_ADDRSTRLEN];
5423 	char	buf2[INET6_ADDRSTRLEN];
5424 	char	buf3[INET6_ADDRSTRLEN];
5425 	char	buf4[INET6_ADDRSTRLEN];
5426 	char	buf5[INET6_ADDRSTRLEN];
5427 	char	buf6[INET6_ADDRSTRLEN];
5428 	char	buf[LIFNAMSIZ];
5429 	ill_t	*ill;
5430 	ipif_t	*ipif;
5431 	nv_t	*nvp;
5432 	uint64_t flags;
5433 	zoneid_t zoneid;
5434 	ill_walk_context_t ctx;
5435 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5436 
5437 	(void) mi_mpprintf(mp,
5438 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5439 	    "\tlocal address\n"
5440 	    "\tsrc address\n"
5441 	    "\tsubnet\n"
5442 	    "\tmask\n"
5443 	    "\tbroadcast\n"
5444 	    "\tp-p-dst");
5445 
5446 	ASSERT(q->q_next == NULL);
5447 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5448 
5449 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5450 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5451 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5452 		for (ipif = ill->ill_ipif; ipif != NULL;
5453 		    ipif = ipif->ipif_next) {
5454 			if (zoneid != GLOBAL_ZONEID &&
5455 			    zoneid != ipif->ipif_zoneid &&
5456 			    ipif->ipif_zoneid != ALL_ZONES)
5457 				continue;
5458 
5459 			ipif_get_name(ipif, buf, sizeof (buf));
5460 			(void) mi_mpprintf(mp,
5461 			    MI_COL_PTRFMT_STR
5462 			    "%04u %05u %u/%u/%u %s %d",
5463 			    (void *)ipif,
5464 			    ipif->ipif_metric, ipif->ipif_mtu,
5465 			    ipif->ipif_ib_pkt_count,
5466 			    ipif->ipif_ob_pkt_count,
5467 			    ipif->ipif_fo_pkt_count,
5468 			    buf,
5469 			    ipif->ipif_zoneid);
5470 
5471 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5472 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5473 
5474 		/* Tack on text strings for any flags. */
5475 		nvp = ipif_nv_tbl;
5476 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5477 			if (nvp->nv_value & flags)
5478 				(void) mi_mpprintf_nr(mp, " %s",
5479 				    nvp->nv_name);
5480 		}
5481 		(void) mi_mpprintf(mp,
5482 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5483 		    inet_ntop(AF_INET6,
5484 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5485 		    inet_ntop(AF_INET6,
5486 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5487 		    inet_ntop(AF_INET6,
5488 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5489 		    inet_ntop(AF_INET6,
5490 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5491 		    inet_ntop(AF_INET6,
5492 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5493 		    inet_ntop(AF_INET6,
5494 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5495 		}
5496 	}
5497 	rw_exit(&ipst->ips_ill_g_lock);
5498 	return (0);
5499 }
5500 
5501 /*
5502  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5503  * driver.  We construct best guess defaults for lower level information that
5504  * we need.  If an interface is brought up without injection of any overriding
5505  * information from outside, we have to be ready to go with these defaults.
5506  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5507  * we primarely want the dl_provider_style.
5508  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5509  * at which point we assume the other part of the information is valid.
5510  */
5511 void
5512 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5513 {
5514 	uchar_t		*brdcst_addr;
5515 	uint_t		brdcst_addr_length, phys_addr_length;
5516 	t_scalar_t	sap_length;
5517 	dl_info_ack_t	*dlia;
5518 	ip_m_t		*ipm;
5519 	dl_qos_cl_sel1_t *sel1;
5520 
5521 	ASSERT(IAM_WRITER_ILL(ill));
5522 
5523 	/*
5524 	 * Till the ill is fully up ILL_CHANGING will be set and
5525 	 * the ill is not globally visible. So no need for a lock.
5526 	 */
5527 	dlia = (dl_info_ack_t *)mp->b_rptr;
5528 	ill->ill_mactype = dlia->dl_mac_type;
5529 
5530 	ipm = ip_m_lookup(dlia->dl_mac_type);
5531 	if (ipm == NULL) {
5532 		ipm = ip_m_lookup(DL_OTHER);
5533 		ASSERT(ipm != NULL);
5534 	}
5535 	ill->ill_media = ipm;
5536 
5537 	/*
5538 	 * When the new DLPI stuff is ready we'll pull lengths
5539 	 * from dlia.
5540 	 */
5541 	if (dlia->dl_version == DL_VERSION_2) {
5542 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5543 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5544 		    brdcst_addr_length);
5545 		if (brdcst_addr == NULL) {
5546 			brdcst_addr_length = 0;
5547 		}
5548 		sap_length = dlia->dl_sap_length;
5549 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5550 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5551 		    brdcst_addr_length, sap_length, phys_addr_length));
5552 	} else {
5553 		brdcst_addr_length = 6;
5554 		brdcst_addr = ip_six_byte_all_ones;
5555 		sap_length = -2;
5556 		phys_addr_length = brdcst_addr_length;
5557 	}
5558 
5559 	ill->ill_bcast_addr_length = brdcst_addr_length;
5560 	ill->ill_phys_addr_length = phys_addr_length;
5561 	ill->ill_sap_length = sap_length;
5562 	ill->ill_max_frag = dlia->dl_max_sdu;
5563 	ill->ill_max_mtu = ill->ill_max_frag;
5564 
5565 	ill->ill_type = ipm->ip_m_type;
5566 
5567 	if (!ill->ill_dlpi_style_set) {
5568 		if (dlia->dl_provider_style == DL_STYLE2)
5569 			ill->ill_needs_attach = 1;
5570 
5571 		/*
5572 		 * Allocate the first ipif on this ill. We don't delay it
5573 		 * further as ioctl handling assumes atleast one ipif to
5574 		 * be present.
5575 		 *
5576 		 * At this point we don't know whether the ill is v4 or v6.
5577 		 * We will know this whan the SIOCSLIFNAME happens and
5578 		 * the correct value for ill_isv6 will be assigned in
5579 		 * ipif_set_values(). We need to hold the ill lock and
5580 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5581 		 * the wakeup.
5582 		 */
5583 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5584 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5585 		mutex_enter(&ill->ill_lock);
5586 		ASSERT(ill->ill_dlpi_style_set == 0);
5587 		ill->ill_dlpi_style_set = 1;
5588 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5589 		cv_broadcast(&ill->ill_cv);
5590 		mutex_exit(&ill->ill_lock);
5591 		freemsg(mp);
5592 		return;
5593 	}
5594 	ASSERT(ill->ill_ipif != NULL);
5595 	/*
5596 	 * We know whether it is IPv4 or IPv6 now, as this is the
5597 	 * second DL_INFO_ACK we are recieving in response to the
5598 	 * DL_INFO_REQ sent in ipif_set_values.
5599 	 */
5600 	if (ill->ill_isv6)
5601 		ill->ill_sap = IP6_DL_SAP;
5602 	else
5603 		ill->ill_sap = IP_DL_SAP;
5604 	/*
5605 	 * Set ipif_mtu which is used to set the IRE's
5606 	 * ire_max_frag value. The driver could have sent
5607 	 * a different mtu from what it sent last time. No
5608 	 * need to call ipif_mtu_change because IREs have
5609 	 * not yet been created.
5610 	 */
5611 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5612 	/*
5613 	 * Clear all the flags that were set based on ill_bcast_addr_length
5614 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5615 	 * changed now and we need to re-evaluate.
5616 	 */
5617 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5618 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5619 
5620 	/*
5621 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5622 	 * changed now.
5623 	 */
5624 	if (ill->ill_bcast_addr_length == 0) {
5625 		if (ill->ill_resolver_mp != NULL)
5626 			freemsg(ill->ill_resolver_mp);
5627 		if (ill->ill_bcast_mp != NULL)
5628 			freemsg(ill->ill_bcast_mp);
5629 		if (ill->ill_flags & ILLF_XRESOLV)
5630 			ill->ill_net_type = IRE_IF_RESOLVER;
5631 		else
5632 			ill->ill_net_type = IRE_IF_NORESOLVER;
5633 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5634 		    ill->ill_phys_addr_length,
5635 		    ill->ill_sap,
5636 		    ill->ill_sap_length);
5637 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5638 
5639 		if (ill->ill_isv6)
5640 			/*
5641 			 * Note: xresolv interfaces will eventually need NOARP
5642 			 * set here as well, but that will require those
5643 			 * external resolvers to have some knowledge of
5644 			 * that flag and act appropriately. Not to be changed
5645 			 * at present.
5646 			 */
5647 			ill->ill_flags |= ILLF_NONUD;
5648 		else
5649 			ill->ill_flags |= ILLF_NOARP;
5650 
5651 		if (ill->ill_phys_addr_length == 0) {
5652 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5653 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5654 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5655 			} else {
5656 				/* pt-pt supports multicast. */
5657 				ill->ill_flags |= ILLF_MULTICAST;
5658 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5659 			}
5660 		}
5661 	} else {
5662 		ill->ill_net_type = IRE_IF_RESOLVER;
5663 		if (ill->ill_bcast_mp != NULL)
5664 			freemsg(ill->ill_bcast_mp);
5665 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5666 		    ill->ill_bcast_addr_length, ill->ill_sap,
5667 		    ill->ill_sap_length);
5668 		/*
5669 		 * Later detect lack of DLPI driver multicast
5670 		 * capability by catching DL_ENABMULTI errors in
5671 		 * ip_rput_dlpi.
5672 		 */
5673 		ill->ill_flags |= ILLF_MULTICAST;
5674 		if (!ill->ill_isv6)
5675 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5676 	}
5677 	/* By default an interface does not support any CoS marking */
5678 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5679 
5680 	/*
5681 	 * If we get QoS information in DL_INFO_ACK, the device supports
5682 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5683 	 */
5684 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5685 	    dlia->dl_qos_length);
5686 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5687 		ill->ill_flags |= ILLF_COS_ENABLED;
5688 	}
5689 
5690 	/* Clear any previous error indication. */
5691 	ill->ill_error = 0;
5692 	freemsg(mp);
5693 }
5694 
5695 /*
5696  * Perform various checks to verify that an address would make sense as a
5697  * local, remote, or subnet interface address.
5698  */
5699 static boolean_t
5700 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5701 {
5702 	ipaddr_t	net_mask;
5703 
5704 	/*
5705 	 * Don't allow all zeroes, or all ones, but allow
5706 	 * all ones netmask.
5707 	 */
5708 	if ((net_mask = ip_net_mask(addr)) == 0)
5709 		return (B_FALSE);
5710 	/* A given netmask overrides the "guess" netmask */
5711 	if (subnet_mask != 0)
5712 		net_mask = subnet_mask;
5713 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5714 	    (addr == (addr | ~net_mask)))) {
5715 		return (B_FALSE);
5716 	}
5717 
5718 	/*
5719 	 * Even if the netmask is all ones, we do not allow address to be
5720 	 * 255.255.255.255
5721 	 */
5722 	if (addr == INADDR_BROADCAST)
5723 		return (B_FALSE);
5724 
5725 	if (CLASSD(addr))
5726 		return (B_FALSE);
5727 
5728 	return (B_TRUE);
5729 }
5730 
5731 #define	V6_IPIF_LINKLOCAL(p)	\
5732 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5733 
5734 /*
5735  * Compare two given ipifs and check if the second one is better than
5736  * the first one using the order of preference (not taking deprecated
5737  * into acount) specified in ipif_lookup_multicast().
5738  */
5739 static boolean_t
5740 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5741 {
5742 	/* Check the least preferred first. */
5743 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5744 		/* If both ipifs are the same, use the first one. */
5745 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5746 			return (B_FALSE);
5747 		else
5748 			return (B_TRUE);
5749 	}
5750 
5751 	/* For IPv6, check for link local address. */
5752 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5753 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5754 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5755 			/* The second one is equal or less preferred. */
5756 			return (B_FALSE);
5757 		} else {
5758 			return (B_TRUE);
5759 		}
5760 	}
5761 
5762 	/* Then check for point to point interface. */
5763 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5764 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5765 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5766 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5767 			return (B_FALSE);
5768 		} else {
5769 			return (B_TRUE);
5770 		}
5771 	}
5772 
5773 	/* old_ipif is a normal interface, so no need to use the new one. */
5774 	return (B_FALSE);
5775 }
5776 
5777 /*
5778  * Find any non-virtual, not condemned, and up multicast capable interface
5779  * given an IP instance and zoneid.  Order of preference is:
5780  *
5781  * 1. normal
5782  * 1.1 normal, but deprecated
5783  * 2. point to point
5784  * 2.1 point to point, but deprecated
5785  * 3. link local
5786  * 3.1 link local, but deprecated
5787  * 4. loopback.
5788  */
5789 ipif_t *
5790 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5791 {
5792 	ill_t			*ill;
5793 	ill_walk_context_t	ctx;
5794 	ipif_t			*ipif;
5795 	ipif_t			*saved_ipif = NULL;
5796 	ipif_t			*dep_ipif = NULL;
5797 
5798 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5799 	if (isv6)
5800 		ill = ILL_START_WALK_V6(&ctx, ipst);
5801 	else
5802 		ill = ILL_START_WALK_V4(&ctx, ipst);
5803 
5804 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5805 		mutex_enter(&ill->ill_lock);
5806 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5807 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5808 			mutex_exit(&ill->ill_lock);
5809 			continue;
5810 		}
5811 		for (ipif = ill->ill_ipif; ipif != NULL;
5812 		    ipif = ipif->ipif_next) {
5813 			if (zoneid != ipif->ipif_zoneid &&
5814 			    zoneid != ALL_ZONES &&
5815 			    ipif->ipif_zoneid != ALL_ZONES) {
5816 				continue;
5817 			}
5818 			if (!(ipif->ipif_flags & IPIF_UP) ||
5819 			    !IPIF_CAN_LOOKUP(ipif)) {
5820 				continue;
5821 			}
5822 
5823 			/*
5824 			 * Found one candidate.  If it is deprecated,
5825 			 * remember it in dep_ipif.  If it is not deprecated,
5826 			 * remember it in saved_ipif.
5827 			 */
5828 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5829 				if (dep_ipif == NULL) {
5830 					dep_ipif = ipif;
5831 				} else if (ipif_comp_multi(dep_ipif, ipif,
5832 				    isv6)) {
5833 					/*
5834 					 * If the previous dep_ipif does not
5835 					 * belong to the same ill, we've done
5836 					 * a ipif_refhold() on it.  So we need
5837 					 * to release it.
5838 					 */
5839 					if (dep_ipif->ipif_ill != ill)
5840 						ipif_refrele(dep_ipif);
5841 					dep_ipif = ipif;
5842 				}
5843 				continue;
5844 			}
5845 			if (saved_ipif == NULL) {
5846 				saved_ipif = ipif;
5847 			} else {
5848 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5849 					if (saved_ipif->ipif_ill != ill)
5850 						ipif_refrele(saved_ipif);
5851 					saved_ipif = ipif;
5852 				}
5853 			}
5854 		}
5855 		/*
5856 		 * Before going to the next ill, do a ipif_refhold() on the
5857 		 * saved ones.
5858 		 */
5859 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5860 			ipif_refhold_locked(saved_ipif);
5861 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5862 			ipif_refhold_locked(dep_ipif);
5863 		mutex_exit(&ill->ill_lock);
5864 	}
5865 	rw_exit(&ipst->ips_ill_g_lock);
5866 
5867 	/*
5868 	 * If we have only the saved_ipif, return it.  But if we have both
5869 	 * saved_ipif and dep_ipif, check to see which one is better.
5870 	 */
5871 	if (saved_ipif != NULL) {
5872 		if (dep_ipif != NULL) {
5873 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5874 				ipif_refrele(saved_ipif);
5875 				return (dep_ipif);
5876 			} else {
5877 				ipif_refrele(dep_ipif);
5878 				return (saved_ipif);
5879 			}
5880 		}
5881 		return (saved_ipif);
5882 	} else {
5883 		return (dep_ipif);
5884 	}
5885 }
5886 
5887 /*
5888  * This function is called when an application does not specify an interface
5889  * to be used for multicast traffic (joining a group/sending data).  It
5890  * calls ire_lookup_multi() to look for an interface route for the
5891  * specified multicast group.  Doing this allows the administrator to add
5892  * prefix routes for multicast to indicate which interface to be used for
5893  * multicast traffic in the above scenario.  The route could be for all
5894  * multicast (224.0/4), for a single multicast group (a /32 route) or
5895  * anything in between.  If there is no such multicast route, we just find
5896  * any multicast capable interface and return it.  The returned ipif
5897  * is refhold'ed.
5898  */
5899 ipif_t *
5900 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5901 {
5902 	ire_t			*ire;
5903 	ipif_t			*ipif;
5904 
5905 	ire = ire_lookup_multi(group, zoneid, ipst);
5906 	if (ire != NULL) {
5907 		ipif = ire->ire_ipif;
5908 		ipif_refhold(ipif);
5909 		ire_refrele(ire);
5910 		return (ipif);
5911 	}
5912 
5913 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5914 }
5915 
5916 /*
5917  * Look for an ipif with the specified interface address and destination.
5918  * The destination address is used only for matching point-to-point interfaces.
5919  */
5920 ipif_t *
5921 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5922     ipsq_func_t func, int *error, ip_stack_t *ipst)
5923 {
5924 	ipif_t	*ipif;
5925 	ill_t	*ill;
5926 	ill_walk_context_t ctx;
5927 	ipsq_t	*ipsq;
5928 
5929 	if (error != NULL)
5930 		*error = 0;
5931 
5932 	/*
5933 	 * First match all the point-to-point interfaces
5934 	 * before looking at non-point-to-point interfaces.
5935 	 * This is done to avoid returning non-point-to-point
5936 	 * ipif instead of unnumbered point-to-point ipif.
5937 	 */
5938 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5939 	ill = ILL_START_WALK_V4(&ctx, ipst);
5940 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5941 		GRAB_CONN_LOCK(q);
5942 		mutex_enter(&ill->ill_lock);
5943 		for (ipif = ill->ill_ipif; ipif != NULL;
5944 		    ipif = ipif->ipif_next) {
5945 			/* Allow the ipif to be down */
5946 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5947 			    (ipif->ipif_lcl_addr == if_addr) &&
5948 			    (ipif->ipif_pp_dst_addr == dst)) {
5949 				/*
5950 				 * The block comment at the start of ipif_down
5951 				 * explains the use of the macros used below
5952 				 */
5953 				if (IPIF_CAN_LOOKUP(ipif)) {
5954 					ipif_refhold_locked(ipif);
5955 					mutex_exit(&ill->ill_lock);
5956 					RELEASE_CONN_LOCK(q);
5957 					rw_exit(&ipst->ips_ill_g_lock);
5958 					return (ipif);
5959 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5960 					ipsq = ill->ill_phyint->phyint_ipsq;
5961 					mutex_enter(&ipsq->ipsq_lock);
5962 					mutex_exit(&ill->ill_lock);
5963 					rw_exit(&ipst->ips_ill_g_lock);
5964 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5965 					    ill);
5966 					mutex_exit(&ipsq->ipsq_lock);
5967 					RELEASE_CONN_LOCK(q);
5968 					if (error != NULL)
5969 						*error = EINPROGRESS;
5970 					return (NULL);
5971 				}
5972 			}
5973 		}
5974 		mutex_exit(&ill->ill_lock);
5975 		RELEASE_CONN_LOCK(q);
5976 	}
5977 	rw_exit(&ipst->ips_ill_g_lock);
5978 
5979 	/* lookup the ipif based on interface address */
5980 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5981 	    ipst);
5982 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5983 	return (ipif);
5984 }
5985 
5986 /*
5987  * Look for an ipif with the specified address. For point-point links
5988  * we look for matches on either the destination address and the local
5989  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5990  * is set.
5991  * Matches on a specific ill if match_ill is set.
5992  */
5993 ipif_t *
5994 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5995     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5996 {
5997 	ipif_t  *ipif;
5998 	ill_t   *ill;
5999 	boolean_t ptp = B_FALSE;
6000 	ipsq_t	*ipsq;
6001 	ill_walk_context_t	ctx;
6002 
6003 	if (error != NULL)
6004 		*error = 0;
6005 
6006 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6007 	/*
6008 	 * Repeat twice, first based on local addresses and
6009 	 * next time for pointopoint.
6010 	 */
6011 repeat:
6012 	ill = ILL_START_WALK_V4(&ctx, ipst);
6013 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6014 		if (match_ill != NULL && ill != match_ill) {
6015 			continue;
6016 		}
6017 		GRAB_CONN_LOCK(q);
6018 		mutex_enter(&ill->ill_lock);
6019 		for (ipif = ill->ill_ipif; ipif != NULL;
6020 		    ipif = ipif->ipif_next) {
6021 			if (zoneid != ALL_ZONES &&
6022 			    zoneid != ipif->ipif_zoneid &&
6023 			    ipif->ipif_zoneid != ALL_ZONES)
6024 				continue;
6025 			/* Allow the ipif to be down */
6026 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6027 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6028 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6029 			    (ipif->ipif_pp_dst_addr == addr))) {
6030 				/*
6031 				 * The block comment at the start of ipif_down
6032 				 * explains the use of the macros used below
6033 				 */
6034 				if (IPIF_CAN_LOOKUP(ipif)) {
6035 					ipif_refhold_locked(ipif);
6036 					mutex_exit(&ill->ill_lock);
6037 					RELEASE_CONN_LOCK(q);
6038 					rw_exit(&ipst->ips_ill_g_lock);
6039 					return (ipif);
6040 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6041 					ipsq = ill->ill_phyint->phyint_ipsq;
6042 					mutex_enter(&ipsq->ipsq_lock);
6043 					mutex_exit(&ill->ill_lock);
6044 					rw_exit(&ipst->ips_ill_g_lock);
6045 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6046 					    ill);
6047 					mutex_exit(&ipsq->ipsq_lock);
6048 					RELEASE_CONN_LOCK(q);
6049 					if (error != NULL)
6050 						*error = EINPROGRESS;
6051 					return (NULL);
6052 				}
6053 			}
6054 		}
6055 		mutex_exit(&ill->ill_lock);
6056 		RELEASE_CONN_LOCK(q);
6057 	}
6058 
6059 	/* If we already did the ptp case, then we are done */
6060 	if (ptp) {
6061 		rw_exit(&ipst->ips_ill_g_lock);
6062 		if (error != NULL)
6063 			*error = ENXIO;
6064 		return (NULL);
6065 	}
6066 	ptp = B_TRUE;
6067 	goto repeat;
6068 }
6069 
6070 /*
6071  * Look for an ipif with the specified address. For point-point links
6072  * we look for matches on either the destination address and the local
6073  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6074  * is set.
6075  * Matches on a specific ill if match_ill is set.
6076  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6077  */
6078 zoneid_t
6079 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6080 {
6081 	zoneid_t zoneid;
6082 	ipif_t  *ipif;
6083 	ill_t   *ill;
6084 	boolean_t ptp = B_FALSE;
6085 	ill_walk_context_t	ctx;
6086 
6087 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6088 	/*
6089 	 * Repeat twice, first based on local addresses and
6090 	 * next time for pointopoint.
6091 	 */
6092 repeat:
6093 	ill = ILL_START_WALK_V4(&ctx, ipst);
6094 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6095 		if (match_ill != NULL && ill != match_ill) {
6096 			continue;
6097 		}
6098 		mutex_enter(&ill->ill_lock);
6099 		for (ipif = ill->ill_ipif; ipif != NULL;
6100 		    ipif = ipif->ipif_next) {
6101 			/* Allow the ipif to be down */
6102 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6103 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6104 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6105 			    (ipif->ipif_pp_dst_addr == addr)) &&
6106 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6107 				zoneid = ipif->ipif_zoneid;
6108 				mutex_exit(&ill->ill_lock);
6109 				rw_exit(&ipst->ips_ill_g_lock);
6110 				/*
6111 				 * If ipif_zoneid was ALL_ZONES then we have
6112 				 * a trusted extensions shared IP address.
6113 				 * In that case GLOBAL_ZONEID works to send.
6114 				 */
6115 				if (zoneid == ALL_ZONES)
6116 					zoneid = GLOBAL_ZONEID;
6117 				return (zoneid);
6118 			}
6119 		}
6120 		mutex_exit(&ill->ill_lock);
6121 	}
6122 
6123 	/* If we already did the ptp case, then we are done */
6124 	if (ptp) {
6125 		rw_exit(&ipst->ips_ill_g_lock);
6126 		return (ALL_ZONES);
6127 	}
6128 	ptp = B_TRUE;
6129 	goto repeat;
6130 }
6131 
6132 /*
6133  * Look for an ipif that matches the specified remote address i.e. the
6134  * ipif that would receive the specified packet.
6135  * First look for directly connected interfaces and then do a recursive
6136  * IRE lookup and pick the first ipif corresponding to the source address in the
6137  * ire.
6138  * Returns: held ipif
6139  */
6140 ipif_t *
6141 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6142 {
6143 	ipif_t	*ipif;
6144 	ire_t	*ire;
6145 	ip_stack_t	*ipst = ill->ill_ipst;
6146 
6147 	ASSERT(!ill->ill_isv6);
6148 
6149 	/*
6150 	 * Someone could be changing this ipif currently or change it
6151 	 * after we return this. Thus  a few packets could use the old
6152 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6153 	 * will atomically be updated or cleaned up with the new value
6154 	 * Thus we don't need a lock to check the flags or other attrs below.
6155 	 */
6156 	mutex_enter(&ill->ill_lock);
6157 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6158 		if (!IPIF_CAN_LOOKUP(ipif))
6159 			continue;
6160 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6161 		    ipif->ipif_zoneid != ALL_ZONES)
6162 			continue;
6163 		/* Allow the ipif to be down */
6164 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6165 			if ((ipif->ipif_pp_dst_addr == addr) ||
6166 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6167 			    ipif->ipif_lcl_addr == addr)) {
6168 				ipif_refhold_locked(ipif);
6169 				mutex_exit(&ill->ill_lock);
6170 				return (ipif);
6171 			}
6172 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6173 			ipif_refhold_locked(ipif);
6174 			mutex_exit(&ill->ill_lock);
6175 			return (ipif);
6176 		}
6177 	}
6178 	mutex_exit(&ill->ill_lock);
6179 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6180 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6181 	if (ire != NULL) {
6182 		/*
6183 		 * The callers of this function wants to know the
6184 		 * interface on which they have to send the replies
6185 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6186 		 * derived from different ills, we really don't care
6187 		 * what we return here.
6188 		 */
6189 		ipif = ire->ire_ipif;
6190 		if (ipif != NULL) {
6191 			ipif_refhold(ipif);
6192 			ire_refrele(ire);
6193 			return (ipif);
6194 		}
6195 		ire_refrele(ire);
6196 	}
6197 	/* Pick the first interface */
6198 	ipif = ipif_get_next_ipif(NULL, ill);
6199 	return (ipif);
6200 }
6201 
6202 /*
6203  * This func does not prevent refcnt from increasing. But if
6204  * the caller has taken steps to that effect, then this func
6205  * can be used to determine whether the ill has become quiescent
6206  */
6207 static boolean_t
6208 ill_is_quiescent(ill_t *ill)
6209 {
6210 	ipif_t	*ipif;
6211 
6212 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6213 
6214 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6215 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6216 			return (B_FALSE);
6217 		}
6218 	}
6219 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6220 		return (B_FALSE);
6221 	}
6222 	return (B_TRUE);
6223 }
6224 
6225 boolean_t
6226 ill_is_freeable(ill_t *ill)
6227 {
6228 	ipif_t	*ipif;
6229 
6230 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6231 
6232 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6233 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6234 			return (B_FALSE);
6235 		}
6236 	}
6237 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6238 		return (B_FALSE);
6239 	}
6240 	return (B_TRUE);
6241 }
6242 
6243 /*
6244  * This func does not prevent refcnt from increasing. But if
6245  * the caller has taken steps to that effect, then this func
6246  * can be used to determine whether the ipif has become quiescent
6247  */
6248 static boolean_t
6249 ipif_is_quiescent(ipif_t *ipif)
6250 {
6251 	ill_t *ill;
6252 
6253 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6254 
6255 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6256 		return (B_FALSE);
6257 	}
6258 
6259 	ill = ipif->ipif_ill;
6260 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6261 	    ill->ill_logical_down) {
6262 		return (B_TRUE);
6263 	}
6264 
6265 	/* This is the last ipif going down or being deleted on this ill */
6266 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6267 		return (B_FALSE);
6268 	}
6269 
6270 	return (B_TRUE);
6271 }
6272 
6273 /*
6274  * return true if the ipif can be destroyed: the ipif has to be quiescent
6275  * with zero references from ire/nce/ilm to it.
6276  */
6277 static boolean_t
6278 ipif_is_freeable(ipif_t *ipif)
6279 {
6280 
6281 	ill_t *ill;
6282 
6283 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6284 
6285 	if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6286 		return (B_FALSE);
6287 	}
6288 
6289 	ill = ipif->ipif_ill;
6290 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6291 	    ill->ill_logical_down) {
6292 		return (B_TRUE);
6293 	}
6294 
6295 	/* This is the last ipif going down or being deleted on this ill */
6296 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6297 		return (B_FALSE);
6298 	}
6299 
6300 	return (B_TRUE);
6301 }
6302 
6303 /*
6304  * This func does not prevent refcnt from increasing. But if
6305  * the caller has taken steps to that effect, then this func
6306  * can be used to determine whether the ipifs marked with IPIF_MOVING
6307  * have become quiescent and can be moved in a failover/failback.
6308  */
6309 static ipif_t *
6310 ill_quiescent_to_move(ill_t *ill)
6311 {
6312 	ipif_t  *ipif;
6313 
6314 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6315 
6316 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6317 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6318 			if (ipif->ipif_refcnt != 0 ||
6319 			    !IPIF_DOWN_OK(ipif)) {
6320 				return (ipif);
6321 			}
6322 		}
6323 	}
6324 	return (NULL);
6325 }
6326 
6327 /*
6328  * The ipif/ill/ire has been refreled. Do the tail processing.
6329  * Determine if the ipif or ill in question has become quiescent and if so
6330  * wakeup close and/or restart any queued pending ioctl that is waiting
6331  * for the ipif_down (or ill_down)
6332  */
6333 void
6334 ipif_ill_refrele_tail(ill_t *ill)
6335 {
6336 	mblk_t	*mp;
6337 	conn_t	*connp;
6338 	ipsq_t	*ipsq;
6339 	ipif_t	*ipif;
6340 	dl_notify_ind_t *dlindp;
6341 
6342 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6343 
6344 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6345 	    ill_is_freeable(ill)) {
6346 		/* ill_close may be waiting */
6347 		cv_broadcast(&ill->ill_cv);
6348 	}
6349 
6350 	/* ipsq can't change because ill_lock  is held */
6351 	ipsq = ill->ill_phyint->phyint_ipsq;
6352 	if (ipsq->ipsq_waitfor == 0) {
6353 		/* Not waiting for anything, just return. */
6354 		mutex_exit(&ill->ill_lock);
6355 		return;
6356 	}
6357 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6358 	    ipsq->ipsq_pending_ipif != NULL);
6359 	/*
6360 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6361 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6362 	 * be zero for restarting an ioctl that ends up downing the ill.
6363 	 */
6364 	ipif = ipsq->ipsq_pending_ipif;
6365 	if (ipif->ipif_ill != ill) {
6366 		/* The ioctl is pending on some other ill. */
6367 		mutex_exit(&ill->ill_lock);
6368 		return;
6369 	}
6370 
6371 	switch (ipsq->ipsq_waitfor) {
6372 	case IPIF_DOWN:
6373 		if (!ipif_is_quiescent(ipif)) {
6374 			mutex_exit(&ill->ill_lock);
6375 			return;
6376 		}
6377 		break;
6378 	case IPIF_FREE:
6379 		if (!ipif_is_freeable(ipif)) {
6380 			mutex_exit(&ill->ill_lock);
6381 			return;
6382 		}
6383 		break;
6384 
6385 	case ILL_DOWN:
6386 		if (!ill_is_quiescent(ill)) {
6387 			mutex_exit(&ill->ill_lock);
6388 			return;
6389 		}
6390 		break;
6391 	case ILL_FREE:
6392 		/*
6393 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6394 		 * waits synchronously in ip_close, and no message is queued in
6395 		 * ipsq_pending_mp at all in this case
6396 		 */
6397 		if (!ill_is_freeable(ill)) {
6398 			mutex_exit(&ill->ill_lock);
6399 			return;
6400 		}
6401 		break;
6402 
6403 	case ILL_MOVE_OK:
6404 		if (ill_quiescent_to_move(ill) != NULL) {
6405 			mutex_exit(&ill->ill_lock);
6406 			return;
6407 		}
6408 		break;
6409 	default:
6410 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6411 		    (void *)ipsq, ipsq->ipsq_waitfor);
6412 	}
6413 
6414 	/*
6415 	 * Incr refcnt for the qwriter_ip call below which
6416 	 * does a refrele
6417 	 */
6418 	ill_refhold_locked(ill);
6419 	mp = ipsq_pending_mp_get(ipsq, &connp);
6420 	mutex_exit(&ill->ill_lock);
6421 
6422 	ASSERT(mp != NULL);
6423 	/*
6424 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6425 	 * we can only get here when the current operation decides it
6426 	 * it needs to quiesce via ipsq_pending_mp_add().
6427 	 */
6428 	switch (mp->b_datap->db_type) {
6429 	case M_PCPROTO:
6430 	case M_PROTO:
6431 		/*
6432 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6433 		 */
6434 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6435 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6436 
6437 		switch (dlindp->dl_notification) {
6438 		case DL_NOTE_PHYS_ADDR:
6439 			qwriter_ip(ill, ill->ill_rq, mp,
6440 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6441 			return;
6442 		default:
6443 			ASSERT(0);
6444 		}
6445 		break;
6446 
6447 	case M_ERROR:
6448 	case M_HANGUP:
6449 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6450 		    B_TRUE);
6451 		return;
6452 
6453 	case M_IOCTL:
6454 	case M_IOCDATA:
6455 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6456 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6457 		return;
6458 
6459 	default:
6460 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6461 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6462 	}
6463 }
6464 
6465 #ifdef DEBUG
6466 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6467 static void
6468 th_trace_rrecord(th_trace_t *th_trace)
6469 {
6470 	tr_buf_t *tr_buf;
6471 	uint_t lastref;
6472 
6473 	lastref = th_trace->th_trace_lastref;
6474 	lastref++;
6475 	if (lastref == TR_BUF_MAX)
6476 		lastref = 0;
6477 	th_trace->th_trace_lastref = lastref;
6478 	tr_buf = &th_trace->th_trbuf[lastref];
6479 	tr_buf->tr_time = lbolt;
6480 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6481 }
6482 
6483 static void
6484 th_trace_free(void *value)
6485 {
6486 	th_trace_t *th_trace = value;
6487 
6488 	ASSERT(th_trace->th_refcnt == 0);
6489 	kmem_free(th_trace, sizeof (*th_trace));
6490 }
6491 
6492 /*
6493  * Find or create the per-thread hash table used to track object references.
6494  * The ipst argument is NULL if we shouldn't allocate.
6495  *
6496  * Accesses per-thread data, so there's no need to lock here.
6497  */
6498 static mod_hash_t *
6499 th_trace_gethash(ip_stack_t *ipst)
6500 {
6501 	th_hash_t *thh;
6502 
6503 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6504 		mod_hash_t *mh;
6505 		char name[256];
6506 		size_t objsize, rshift;
6507 		int retv;
6508 
6509 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6510 			return (NULL);
6511 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6512 		    (void *)curthread);
6513 
6514 		/*
6515 		 * We use mod_hash_create_extended here rather than the more
6516 		 * obvious mod_hash_create_ptrhash because the latter has a
6517 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6518 		 * block.
6519 		 */
6520 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6521 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6522 		rshift = highbit(objsize);
6523 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6524 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6525 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6526 		if (mh == NULL) {
6527 			kmem_free(thh, sizeof (*thh));
6528 			return (NULL);
6529 		}
6530 		thh->thh_hash = mh;
6531 		thh->thh_ipst = ipst;
6532 		/*
6533 		 * We trace ills, ipifs, ires, and nces.  All of these are
6534 		 * per-IP-stack, so the lock on the thread list is as well.
6535 		 */
6536 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6537 		list_insert_tail(&ip_thread_list, thh);
6538 		rw_exit(&ip_thread_rwlock);
6539 		retv = tsd_set(ip_thread_data, thh);
6540 		ASSERT(retv == 0);
6541 	}
6542 	return (thh != NULL ? thh->thh_hash : NULL);
6543 }
6544 
6545 boolean_t
6546 th_trace_ref(const void *obj, ip_stack_t *ipst)
6547 {
6548 	th_trace_t *th_trace;
6549 	mod_hash_t *mh;
6550 	mod_hash_val_t val;
6551 
6552 	if ((mh = th_trace_gethash(ipst)) == NULL)
6553 		return (B_FALSE);
6554 
6555 	/*
6556 	 * Attempt to locate the trace buffer for this obj and thread.
6557 	 * If it does not exist, then allocate a new trace buffer and
6558 	 * insert into the hash.
6559 	 */
6560 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6561 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6562 		if (th_trace == NULL)
6563 			return (B_FALSE);
6564 
6565 		th_trace->th_id = curthread;
6566 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6567 		    (mod_hash_val_t)th_trace) != 0) {
6568 			kmem_free(th_trace, sizeof (th_trace_t));
6569 			return (B_FALSE);
6570 		}
6571 	} else {
6572 		th_trace = (th_trace_t *)val;
6573 	}
6574 
6575 	ASSERT(th_trace->th_refcnt >= 0 &&
6576 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6577 
6578 	th_trace->th_refcnt++;
6579 	th_trace_rrecord(th_trace);
6580 	return (B_TRUE);
6581 }
6582 
6583 /*
6584  * For the purpose of tracing a reference release, we assume that global
6585  * tracing is always on and that the same thread initiated the reference hold
6586  * is releasing.
6587  */
6588 void
6589 th_trace_unref(const void *obj)
6590 {
6591 	int retv;
6592 	mod_hash_t *mh;
6593 	th_trace_t *th_trace;
6594 	mod_hash_val_t val;
6595 
6596 	mh = th_trace_gethash(NULL);
6597 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6598 	ASSERT(retv == 0);
6599 	th_trace = (th_trace_t *)val;
6600 
6601 	ASSERT(th_trace->th_refcnt > 0);
6602 	th_trace->th_refcnt--;
6603 	th_trace_rrecord(th_trace);
6604 }
6605 
6606 /*
6607  * If tracing has been disabled, then we assume that the reference counts are
6608  * now useless, and we clear them out before destroying the entries.
6609  */
6610 void
6611 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6612 {
6613 	th_hash_t	*thh;
6614 	mod_hash_t	*mh;
6615 	mod_hash_val_t	val;
6616 	th_trace_t	*th_trace;
6617 	int		retv;
6618 
6619 	rw_enter(&ip_thread_rwlock, RW_READER);
6620 	for (thh = list_head(&ip_thread_list); thh != NULL;
6621 	    thh = list_next(&ip_thread_list, thh)) {
6622 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6623 		    &val) == 0) {
6624 			th_trace = (th_trace_t *)val;
6625 			if (trace_disable)
6626 				th_trace->th_refcnt = 0;
6627 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6628 			ASSERT(retv == 0);
6629 		}
6630 	}
6631 	rw_exit(&ip_thread_rwlock);
6632 }
6633 
6634 void
6635 ipif_trace_ref(ipif_t *ipif)
6636 {
6637 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6638 
6639 	if (ipif->ipif_trace_disable)
6640 		return;
6641 
6642 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6643 		ipif->ipif_trace_disable = B_TRUE;
6644 		ipif_trace_cleanup(ipif);
6645 	}
6646 }
6647 
6648 void
6649 ipif_untrace_ref(ipif_t *ipif)
6650 {
6651 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6652 
6653 	if (!ipif->ipif_trace_disable)
6654 		th_trace_unref(ipif);
6655 }
6656 
6657 void
6658 ill_trace_ref(ill_t *ill)
6659 {
6660 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6661 
6662 	if (ill->ill_trace_disable)
6663 		return;
6664 
6665 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6666 		ill->ill_trace_disable = B_TRUE;
6667 		ill_trace_cleanup(ill);
6668 	}
6669 }
6670 
6671 void
6672 ill_untrace_ref(ill_t *ill)
6673 {
6674 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6675 
6676 	if (!ill->ill_trace_disable)
6677 		th_trace_unref(ill);
6678 }
6679 
6680 /*
6681  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6682  * failure, ipif_trace_disable is set.
6683  */
6684 static void
6685 ipif_trace_cleanup(const ipif_t *ipif)
6686 {
6687 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6688 }
6689 
6690 /*
6691  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6692  * failure, ill_trace_disable is set.
6693  */
6694 static void
6695 ill_trace_cleanup(const ill_t *ill)
6696 {
6697 	th_trace_cleanup(ill, ill->ill_trace_disable);
6698 }
6699 #endif /* DEBUG */
6700 
6701 void
6702 ipif_refhold_locked(ipif_t *ipif)
6703 {
6704 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6705 	ipif->ipif_refcnt++;
6706 	IPIF_TRACE_REF(ipif);
6707 }
6708 
6709 void
6710 ipif_refhold(ipif_t *ipif)
6711 {
6712 	ill_t	*ill;
6713 
6714 	ill = ipif->ipif_ill;
6715 	mutex_enter(&ill->ill_lock);
6716 	ipif->ipif_refcnt++;
6717 	IPIF_TRACE_REF(ipif);
6718 	mutex_exit(&ill->ill_lock);
6719 }
6720 
6721 /*
6722  * Must not be called while holding any locks. Otherwise if this is
6723  * the last reference to be released there is a chance of recursive mutex
6724  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6725  * to restart an ioctl.
6726  */
6727 void
6728 ipif_refrele(ipif_t *ipif)
6729 {
6730 	ill_t	*ill;
6731 
6732 	ill = ipif->ipif_ill;
6733 
6734 	mutex_enter(&ill->ill_lock);
6735 	ASSERT(ipif->ipif_refcnt != 0);
6736 	ipif->ipif_refcnt--;
6737 	IPIF_UNTRACE_REF(ipif);
6738 	if (ipif->ipif_refcnt != 0) {
6739 		mutex_exit(&ill->ill_lock);
6740 		return;
6741 	}
6742 
6743 	/* Drops the ill_lock */
6744 	ipif_ill_refrele_tail(ill);
6745 }
6746 
6747 ipif_t *
6748 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6749 {
6750 	ipif_t	*ipif;
6751 
6752 	mutex_enter(&ill->ill_lock);
6753 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6754 	    ipif != NULL; ipif = ipif->ipif_next) {
6755 		if (!IPIF_CAN_LOOKUP(ipif))
6756 			continue;
6757 		ipif_refhold_locked(ipif);
6758 		mutex_exit(&ill->ill_lock);
6759 		return (ipif);
6760 	}
6761 	mutex_exit(&ill->ill_lock);
6762 	return (NULL);
6763 }
6764 
6765 /*
6766  * TODO: make this table extendible at run time
6767  * Return a pointer to the mac type info for 'mac_type'
6768  */
6769 static ip_m_t *
6770 ip_m_lookup(t_uscalar_t mac_type)
6771 {
6772 	ip_m_t	*ipm;
6773 
6774 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6775 		if (ipm->ip_m_mac_type == mac_type)
6776 			return (ipm);
6777 	return (NULL);
6778 }
6779 
6780 /*
6781  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6782  * ipif_arg is passed in to associate it with the correct interface.
6783  * We may need to restart this operation if the ipif cannot be looked up
6784  * due to an exclusive operation that is currently in progress. The restart
6785  * entry point is specified by 'func'
6786  */
6787 int
6788 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6789     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6790     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6791     struct rtsa_s *sp, ip_stack_t *ipst)
6792 {
6793 	ire_t	*ire;
6794 	ire_t	*gw_ire = NULL;
6795 	ipif_t	*ipif = NULL;
6796 	boolean_t ipif_refheld = B_FALSE;
6797 	uint_t	type;
6798 	int	match_flags = MATCH_IRE_TYPE;
6799 	int	error;
6800 	tsol_gc_t *gc = NULL;
6801 	tsol_gcgrp_t *gcgrp = NULL;
6802 	boolean_t gcgrp_xtraref = B_FALSE;
6803 
6804 	ip1dbg(("ip_rt_add:"));
6805 
6806 	if (ire_arg != NULL)
6807 		*ire_arg = NULL;
6808 
6809 	/*
6810 	 * If this is the case of RTF_HOST being set, then we set the netmask
6811 	 * to all ones (regardless if one was supplied).
6812 	 */
6813 	if (flags & RTF_HOST)
6814 		mask = IP_HOST_MASK;
6815 
6816 	/*
6817 	 * Prevent routes with a zero gateway from being created (since
6818 	 * interfaces can currently be plumbed and brought up no assigned
6819 	 * address).
6820 	 */
6821 	if (gw_addr == 0)
6822 		return (ENETUNREACH);
6823 	/*
6824 	 * Get the ipif, if any, corresponding to the gw_addr
6825 	 */
6826 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6827 	    ipst);
6828 	if (ipif != NULL) {
6829 		if (IS_VNI(ipif->ipif_ill)) {
6830 			ipif_refrele(ipif);
6831 			return (EINVAL);
6832 		}
6833 		ipif_refheld = B_TRUE;
6834 	} else if (error == EINPROGRESS) {
6835 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6836 		return (EINPROGRESS);
6837 	} else {
6838 		error = 0;
6839 	}
6840 
6841 	if (ipif != NULL) {
6842 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6843 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6844 	} else {
6845 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6846 	}
6847 
6848 	/*
6849 	 * GateD will attempt to create routes with a loopback interface
6850 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6851 	 * these routes to be added, but create them as interface routes
6852 	 * since the gateway is an interface address.
6853 	 */
6854 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6855 		flags &= ~RTF_GATEWAY;
6856 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6857 		    mask == IP_HOST_MASK) {
6858 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6859 			    ALL_ZONES, NULL, match_flags, ipst);
6860 			if (ire != NULL) {
6861 				ire_refrele(ire);
6862 				if (ipif_refheld)
6863 					ipif_refrele(ipif);
6864 				return (EEXIST);
6865 			}
6866 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6867 			    "for 0x%x\n", (void *)ipif,
6868 			    ipif->ipif_ire_type,
6869 			    ntohl(ipif->ipif_lcl_addr)));
6870 			ire = ire_create(
6871 			    (uchar_t *)&dst_addr,	/* dest address */
6872 			    (uchar_t *)&mask,		/* mask */
6873 			    (uchar_t *)&ipif->ipif_src_addr,
6874 			    NULL,			/* no gateway */
6875 			    &ipif->ipif_mtu,
6876 			    NULL,
6877 			    ipif->ipif_rq,		/* recv-from queue */
6878 			    NULL,			/* no send-to queue */
6879 			    ipif->ipif_ire_type,	/* LOOPBACK */
6880 			    ipif,
6881 			    0,
6882 			    0,
6883 			    0,
6884 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6885 			    RTF_PRIVATE : 0,
6886 			    &ire_uinfo_null,
6887 			    NULL,
6888 			    NULL,
6889 			    ipst);
6890 
6891 			if (ire == NULL) {
6892 				if (ipif_refheld)
6893 					ipif_refrele(ipif);
6894 				return (ENOMEM);
6895 			}
6896 			error = ire_add(&ire, q, mp, func, B_FALSE);
6897 			if (error == 0)
6898 				goto save_ire;
6899 			if (ipif_refheld)
6900 				ipif_refrele(ipif);
6901 			return (error);
6902 
6903 		}
6904 	}
6905 
6906 	/*
6907 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6908 	 * and the gateway address provided is one of the system's interface
6909 	 * addresses.  By using the routing socket interface and supplying an
6910 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6911 	 * specifying an interface route to be created is available which uses
6912 	 * the interface index that specifies the outgoing interface rather than
6913 	 * the address of an outgoing interface (which may not be able to
6914 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6915 	 * flag, routes can be specified which not only specify the next-hop to
6916 	 * be used when routing to a certain prefix, but also which outgoing
6917 	 * interface should be used.
6918 	 *
6919 	 * Previously, interfaces would have unique addresses assigned to them
6920 	 * and so the address assigned to a particular interface could be used
6921 	 * to identify a particular interface.  One exception to this was the
6922 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6923 	 *
6924 	 * With the advent of IPv6 and its link-local addresses, this
6925 	 * restriction was relaxed and interfaces could share addresses between
6926 	 * themselves.  In fact, typically all of the link-local interfaces on
6927 	 * an IPv6 node or router will have the same link-local address.  In
6928 	 * order to differentiate between these interfaces, the use of an
6929 	 * interface index is necessary and this index can be carried inside a
6930 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6931 	 * of using the interface index, however, is that all of the ipif's that
6932 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6933 	 * cannot be used to differentiate between ipif's (or logical
6934 	 * interfaces) that belong to the same ill (physical interface).
6935 	 *
6936 	 * For example, in the following case involving IPv4 interfaces and
6937 	 * logical interfaces
6938 	 *
6939 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6940 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6941 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6942 	 *
6943 	 * the ipif's corresponding to each of these interface routes can be
6944 	 * uniquely identified by the "gateway" (actually interface address).
6945 	 *
6946 	 * In this case involving multiple IPv6 default routes to a particular
6947 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6948 	 * default route is of interest:
6949 	 *
6950 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6951 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6952 	 */
6953 
6954 	/* RTF_GATEWAY not set */
6955 	if (!(flags & RTF_GATEWAY)) {
6956 		queue_t	*stq;
6957 
6958 		if (sp != NULL) {
6959 			ip2dbg(("ip_rt_add: gateway security attributes "
6960 			    "cannot be set with interface route\n"));
6961 			if (ipif_refheld)
6962 				ipif_refrele(ipif);
6963 			return (EINVAL);
6964 		}
6965 
6966 		/*
6967 		 * As the interface index specified with the RTA_IFP sockaddr is
6968 		 * the same for all ipif's off of an ill, the matching logic
6969 		 * below uses MATCH_IRE_ILL if such an index was specified.
6970 		 * This means that routes sharing the same prefix when added
6971 		 * using a RTA_IFP sockaddr must have distinct interface
6972 		 * indices (namely, they must be on distinct ill's).
6973 		 *
6974 		 * On the other hand, since the gateway address will usually be
6975 		 * different for each ipif on the system, the matching logic
6976 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6977 		 * route.  This means that interface routes for the same prefix
6978 		 * can be created if they belong to distinct ipif's and if a
6979 		 * RTA_IFP sockaddr is not present.
6980 		 */
6981 		if (ipif_arg != NULL) {
6982 			if (ipif_refheld)  {
6983 				ipif_refrele(ipif);
6984 				ipif_refheld = B_FALSE;
6985 			}
6986 			ipif = ipif_arg;
6987 			match_flags |= MATCH_IRE_ILL;
6988 		} else {
6989 			/*
6990 			 * Check the ipif corresponding to the gw_addr
6991 			 */
6992 			if (ipif == NULL)
6993 				return (ENETUNREACH);
6994 			match_flags |= MATCH_IRE_IPIF;
6995 		}
6996 		ASSERT(ipif != NULL);
6997 
6998 		/*
6999 		 * We check for an existing entry at this point.
7000 		 *
7001 		 * Since a netmask isn't passed in via the ioctl interface
7002 		 * (SIOCADDRT), we don't check for a matching netmask in that
7003 		 * case.
7004 		 */
7005 		if (!ioctl_msg)
7006 			match_flags |= MATCH_IRE_MASK;
7007 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7008 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7009 		if (ire != NULL) {
7010 			ire_refrele(ire);
7011 			if (ipif_refheld)
7012 				ipif_refrele(ipif);
7013 			return (EEXIST);
7014 		}
7015 
7016 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7017 		    ? ipif->ipif_rq : ipif->ipif_wq;
7018 
7019 		/*
7020 		 * Create a copy of the IRE_LOOPBACK,
7021 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7022 		 * the modified address and netmask.
7023 		 */
7024 		ire = ire_create(
7025 		    (uchar_t *)&dst_addr,
7026 		    (uint8_t *)&mask,
7027 		    (uint8_t *)&ipif->ipif_src_addr,
7028 		    NULL,
7029 		    &ipif->ipif_mtu,
7030 		    NULL,
7031 		    NULL,
7032 		    stq,
7033 		    ipif->ipif_net_type,
7034 		    ipif,
7035 		    0,
7036 		    0,
7037 		    0,
7038 		    flags,
7039 		    &ire_uinfo_null,
7040 		    NULL,
7041 		    NULL,
7042 		    ipst);
7043 		if (ire == NULL) {
7044 			if (ipif_refheld)
7045 				ipif_refrele(ipif);
7046 			return (ENOMEM);
7047 		}
7048 
7049 		/*
7050 		 * Some software (for example, GateD and Sun Cluster) attempts
7051 		 * to create (what amount to) IRE_PREFIX routes with the
7052 		 * loopback address as the gateway.  This is primarily done to
7053 		 * set up prefixes with the RTF_REJECT flag set (for example,
7054 		 * when generating aggregate routes.)
7055 		 *
7056 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7057 		 * IRE_LOOPBACK, then we map the request into a
7058 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7059 		 * these interface routes, by definition, can only be that.
7060 		 *
7061 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7062 		 * routine, but rather using ire_create() directly.
7063 		 *
7064 		 */
7065 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7066 			ire->ire_type = IRE_IF_NORESOLVER;
7067 			ire->ire_flags |= RTF_BLACKHOLE;
7068 		}
7069 
7070 		error = ire_add(&ire, q, mp, func, B_FALSE);
7071 		if (error == 0)
7072 			goto save_ire;
7073 
7074 		/*
7075 		 * In the result of failure, ire_add() will have already
7076 		 * deleted the ire in question, so there is no need to
7077 		 * do that here.
7078 		 */
7079 		if (ipif_refheld)
7080 			ipif_refrele(ipif);
7081 		return (error);
7082 	}
7083 	if (ipif_refheld) {
7084 		ipif_refrele(ipif);
7085 		ipif_refheld = B_FALSE;
7086 	}
7087 
7088 	/*
7089 	 * Get an interface IRE for the specified gateway.
7090 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7091 	 * gateway, it is currently unreachable and we fail the request
7092 	 * accordingly.
7093 	 */
7094 	ipif = ipif_arg;
7095 	if (ipif_arg != NULL)
7096 		match_flags |= MATCH_IRE_ILL;
7097 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7098 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7099 	if (gw_ire == NULL)
7100 		return (ENETUNREACH);
7101 
7102 	/*
7103 	 * We create one of three types of IREs as a result of this request
7104 	 * based on the netmask.  A netmask of all ones (which is automatically
7105 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7106 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7107 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7108 	 * destination prefix.
7109 	 */
7110 	if (mask == IP_HOST_MASK)
7111 		type = IRE_HOST;
7112 	else if (mask == 0)
7113 		type = IRE_DEFAULT;
7114 	else
7115 		type = IRE_PREFIX;
7116 
7117 	/* check for a duplicate entry */
7118 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7119 	    NULL, ALL_ZONES, 0, NULL,
7120 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7121 	if (ire != NULL) {
7122 		ire_refrele(gw_ire);
7123 		ire_refrele(ire);
7124 		return (EEXIST);
7125 	}
7126 
7127 	/* Security attribute exists */
7128 	if (sp != NULL) {
7129 		tsol_gcgrp_addr_t ga;
7130 
7131 		/* find or create the gateway credentials group */
7132 		ga.ga_af = AF_INET;
7133 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7134 
7135 		/* we hold reference to it upon success */
7136 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7137 		if (gcgrp == NULL) {
7138 			ire_refrele(gw_ire);
7139 			return (ENOMEM);
7140 		}
7141 
7142 		/*
7143 		 * Create and add the security attribute to the group; a
7144 		 * reference to the group is made upon allocating a new
7145 		 * entry successfully.  If it finds an already-existing
7146 		 * entry for the security attribute in the group, it simply
7147 		 * returns it and no new reference is made to the group.
7148 		 */
7149 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7150 		if (gc == NULL) {
7151 			/* release reference held by gcgrp_lookup */
7152 			GCGRP_REFRELE(gcgrp);
7153 			ire_refrele(gw_ire);
7154 			return (ENOMEM);
7155 		}
7156 	}
7157 
7158 	/* Create the IRE. */
7159 	ire = ire_create(
7160 	    (uchar_t *)&dst_addr,		/* dest address */
7161 	    (uchar_t *)&mask,			/* mask */
7162 	    /* src address assigned by the caller? */
7163 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7164 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7165 	    (uchar_t *)&gw_addr,		/* gateway address */
7166 	    &gw_ire->ire_max_frag,
7167 	    NULL,				/* no src nce */
7168 	    NULL,				/* no recv-from queue */
7169 	    NULL,				/* no send-to queue */
7170 	    (ushort_t)type,			/* IRE type */
7171 	    ipif_arg,
7172 	    0,
7173 	    0,
7174 	    0,
7175 	    flags,
7176 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7177 	    gc,					/* security attribute */
7178 	    NULL,
7179 	    ipst);
7180 
7181 	/*
7182 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7183 	 * reference to the 'gcgrp'. We can now release the extra reference
7184 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7185 	 */
7186 	if (gcgrp_xtraref)
7187 		GCGRP_REFRELE(gcgrp);
7188 	if (ire == NULL) {
7189 		if (gc != NULL)
7190 			GC_REFRELE(gc);
7191 		ire_refrele(gw_ire);
7192 		return (ENOMEM);
7193 	}
7194 
7195 	/*
7196 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7197 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7198 	 */
7199 
7200 	/* Add the new IRE. */
7201 	error = ire_add(&ire, q, mp, func, B_FALSE);
7202 	if (error != 0) {
7203 		/*
7204 		 * In the result of failure, ire_add() will have already
7205 		 * deleted the ire in question, so there is no need to
7206 		 * do that here.
7207 		 */
7208 		ire_refrele(gw_ire);
7209 		return (error);
7210 	}
7211 
7212 	if (flags & RTF_MULTIRT) {
7213 		/*
7214 		 * Invoke the CGTP (multirouting) filtering module
7215 		 * to add the dst address in the filtering database.
7216 		 * Replicated inbound packets coming from that address
7217 		 * will be filtered to discard the duplicates.
7218 		 * It is not necessary to call the CGTP filter hook
7219 		 * when the dst address is a broadcast or multicast,
7220 		 * because an IP source address cannot be a broadcast
7221 		 * or a multicast.
7222 		 */
7223 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7224 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7225 		if (ire_dst != NULL) {
7226 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7227 			ire_refrele(ire_dst);
7228 			goto save_ire;
7229 		}
7230 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7231 		    !CLASSD(ire->ire_addr)) {
7232 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7233 			    ipst->ips_netstack->netstack_stackid,
7234 			    ire->ire_addr,
7235 			    ire->ire_gateway_addr,
7236 			    ire->ire_src_addr,
7237 			    gw_ire->ire_src_addr);
7238 			if (res != 0) {
7239 				ire_refrele(gw_ire);
7240 				ire_delete(ire);
7241 				return (res);
7242 			}
7243 		}
7244 	}
7245 
7246 	/*
7247 	 * Now that the prefix IRE entry has been created, delete any
7248 	 * existing gateway IRE cache entries as well as any IRE caches
7249 	 * using the gateway, and force them to be created through
7250 	 * ip_newroute.
7251 	 */
7252 	if (gc != NULL) {
7253 		ASSERT(gcgrp != NULL);
7254 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7255 	}
7256 
7257 save_ire:
7258 	if (gw_ire != NULL) {
7259 		ire_refrele(gw_ire);
7260 	}
7261 	if (ipif != NULL) {
7262 		/*
7263 		 * Save enough information so that we can recreate the IRE if
7264 		 * the interface goes down and then up.  The metrics associated
7265 		 * with the route will be saved as well when rts_setmetrics() is
7266 		 * called after the IRE has been created.  In the case where
7267 		 * memory cannot be allocated, none of this information will be
7268 		 * saved.
7269 		 */
7270 		ipif_save_ire(ipif, ire);
7271 	}
7272 	if (ioctl_msg)
7273 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7274 	if (ire_arg != NULL) {
7275 		/*
7276 		 * Store the ire that was successfully added into where ire_arg
7277 		 * points to so that callers don't have to look it up
7278 		 * themselves (but they are responsible for ire_refrele()ing
7279 		 * the ire when they are finished with it).
7280 		 */
7281 		*ire_arg = ire;
7282 	} else {
7283 		ire_refrele(ire);		/* Held in ire_add */
7284 	}
7285 	if (ipif_refheld)
7286 		ipif_refrele(ipif);
7287 	return (0);
7288 }
7289 
7290 /*
7291  * ip_rt_delete is called to delete an IPv4 route.
7292  * ipif_arg is passed in to associate it with the correct interface.
7293  * We may need to restart this operation if the ipif cannot be looked up
7294  * due to an exclusive operation that is currently in progress. The restart
7295  * entry point is specified by 'func'
7296  */
7297 /* ARGSUSED4 */
7298 int
7299 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7300     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7301     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7302 {
7303 	ire_t	*ire = NULL;
7304 	ipif_t	*ipif;
7305 	boolean_t ipif_refheld = B_FALSE;
7306 	uint_t	type;
7307 	uint_t	match_flags = MATCH_IRE_TYPE;
7308 	int	err = 0;
7309 
7310 	ip1dbg(("ip_rt_delete:"));
7311 	/*
7312 	 * If this is the case of RTF_HOST being set, then we set the netmask
7313 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7314 	 */
7315 	if (flags & RTF_HOST) {
7316 		mask = IP_HOST_MASK;
7317 		match_flags |= MATCH_IRE_MASK;
7318 	} else if (rtm_addrs & RTA_NETMASK) {
7319 		match_flags |= MATCH_IRE_MASK;
7320 	}
7321 
7322 	/*
7323 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7324 	 * we check if the gateway address is one of our interfaces first,
7325 	 * and fall back on RTF_GATEWAY routes.
7326 	 *
7327 	 * This makes it possible to delete an original
7328 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7329 	 *
7330 	 * As the interface index specified with the RTA_IFP sockaddr is the
7331 	 * same for all ipif's off of an ill, the matching logic below uses
7332 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7333 	 * sharing the same prefix and interface index as the the route
7334 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7335 	 * is specified in the request.
7336 	 *
7337 	 * On the other hand, since the gateway address will usually be
7338 	 * different for each ipif on the system, the matching logic
7339 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7340 	 * route.  This means that interface routes for the same prefix can be
7341 	 * uniquely identified if they belong to distinct ipif's and if a
7342 	 * RTA_IFP sockaddr is not present.
7343 	 *
7344 	 * For more detail on specifying routes by gateway address and by
7345 	 * interface index, see the comments in ip_rt_add().
7346 	 */
7347 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7348 	    ipst);
7349 	if (ipif != NULL)
7350 		ipif_refheld = B_TRUE;
7351 	else if (err == EINPROGRESS)
7352 		return (err);
7353 	else
7354 		err = 0;
7355 	if (ipif != NULL) {
7356 		if (ipif_arg != NULL) {
7357 			if (ipif_refheld) {
7358 				ipif_refrele(ipif);
7359 				ipif_refheld = B_FALSE;
7360 			}
7361 			ipif = ipif_arg;
7362 			match_flags |= MATCH_IRE_ILL;
7363 		} else {
7364 			match_flags |= MATCH_IRE_IPIF;
7365 		}
7366 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7367 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7368 			    ALL_ZONES, NULL, match_flags, ipst);
7369 		}
7370 		if (ire == NULL) {
7371 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7372 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7373 			    match_flags, ipst);
7374 		}
7375 	}
7376 
7377 	if (ire == NULL) {
7378 		/*
7379 		 * At this point, the gateway address is not one of our own
7380 		 * addresses or a matching interface route was not found.  We
7381 		 * set the IRE type to lookup based on whether
7382 		 * this is a host route, a default route or just a prefix.
7383 		 *
7384 		 * If an ipif_arg was passed in, then the lookup is based on an
7385 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7386 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7387 		 * set as the route being looked up is not a traditional
7388 		 * interface route.
7389 		 */
7390 		match_flags &= ~MATCH_IRE_IPIF;
7391 		match_flags |= MATCH_IRE_GW;
7392 		if (ipif_arg != NULL)
7393 			match_flags |= MATCH_IRE_ILL;
7394 		if (mask == IP_HOST_MASK)
7395 			type = IRE_HOST;
7396 		else if (mask == 0)
7397 			type = IRE_DEFAULT;
7398 		else
7399 			type = IRE_PREFIX;
7400 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7401 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7402 	}
7403 
7404 	if (ipif_refheld)
7405 		ipif_refrele(ipif);
7406 
7407 	/* ipif is not refheld anymore */
7408 	if (ire == NULL)
7409 		return (ESRCH);
7410 
7411 	if (ire->ire_flags & RTF_MULTIRT) {
7412 		/*
7413 		 * Invoke the CGTP (multirouting) filtering module
7414 		 * to remove the dst address from the filtering database.
7415 		 * Packets coming from that address will no longer be
7416 		 * filtered to remove duplicates.
7417 		 */
7418 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7419 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7420 			    ipst->ips_netstack->netstack_stackid,
7421 			    ire->ire_addr, ire->ire_gateway_addr);
7422 		}
7423 		ip_cgtp_bcast_delete(ire, ipst);
7424 	}
7425 
7426 	ipif = ire->ire_ipif;
7427 	if (ipif != NULL)
7428 		ipif_remove_ire(ipif, ire);
7429 	if (ioctl_msg)
7430 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7431 	ire_delete(ire);
7432 	ire_refrele(ire);
7433 	return (err);
7434 }
7435 
7436 /*
7437  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7438  */
7439 /* ARGSUSED */
7440 int
7441 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7442     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7443 {
7444 	ipaddr_t dst_addr;
7445 	ipaddr_t gw_addr;
7446 	ipaddr_t mask;
7447 	int error = 0;
7448 	mblk_t *mp1;
7449 	struct rtentry *rt;
7450 	ipif_t *ipif = NULL;
7451 	ip_stack_t	*ipst;
7452 
7453 	ASSERT(q->q_next == NULL);
7454 	ipst = CONNQ_TO_IPST(q);
7455 
7456 	ip1dbg(("ip_siocaddrt:"));
7457 	/* Existence of mp1 verified in ip_wput_nondata */
7458 	mp1 = mp->b_cont->b_cont;
7459 	rt = (struct rtentry *)mp1->b_rptr;
7460 
7461 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7462 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7463 
7464 	/*
7465 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7466 	 * to a particular host address.  In this case, we set the netmask to
7467 	 * all ones for the particular destination address.  Otherwise,
7468 	 * determine the netmask to be used based on dst_addr and the interfaces
7469 	 * in use.
7470 	 */
7471 	if (rt->rt_flags & RTF_HOST) {
7472 		mask = IP_HOST_MASK;
7473 	} else {
7474 		/*
7475 		 * Note that ip_subnet_mask returns a zero mask in the case of
7476 		 * default (an all-zeroes address).
7477 		 */
7478 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7479 	}
7480 
7481 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7482 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7483 	if (ipif != NULL)
7484 		ipif_refrele(ipif);
7485 	return (error);
7486 }
7487 
7488 /*
7489  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7490  */
7491 /* ARGSUSED */
7492 int
7493 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7494     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7495 {
7496 	ipaddr_t dst_addr;
7497 	ipaddr_t gw_addr;
7498 	ipaddr_t mask;
7499 	int error;
7500 	mblk_t *mp1;
7501 	struct rtentry *rt;
7502 	ipif_t *ipif = NULL;
7503 	ip_stack_t	*ipst;
7504 
7505 	ASSERT(q->q_next == NULL);
7506 	ipst = CONNQ_TO_IPST(q);
7507 
7508 	ip1dbg(("ip_siocdelrt:"));
7509 	/* Existence of mp1 verified in ip_wput_nondata */
7510 	mp1 = mp->b_cont->b_cont;
7511 	rt = (struct rtentry *)mp1->b_rptr;
7512 
7513 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7514 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7515 
7516 	/*
7517 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7518 	 * to a particular host address.  In this case, we set the netmask to
7519 	 * all ones for the particular destination address.  Otherwise,
7520 	 * determine the netmask to be used based on dst_addr and the interfaces
7521 	 * in use.
7522 	 */
7523 	if (rt->rt_flags & RTF_HOST) {
7524 		mask = IP_HOST_MASK;
7525 	} else {
7526 		/*
7527 		 * Note that ip_subnet_mask returns a zero mask in the case of
7528 		 * default (an all-zeroes address).
7529 		 */
7530 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7531 	}
7532 
7533 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7534 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7535 	    mp, ip_process_ioctl, ipst);
7536 	if (ipif != NULL)
7537 		ipif_refrele(ipif);
7538 	return (error);
7539 }
7540 
7541 /*
7542  * Enqueue the mp onto the ipsq, chained by b_next.
7543  * b_prev stores the function to be executed later, and b_queue the queue
7544  * where this mp originated.
7545  */
7546 void
7547 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7548     ill_t *pending_ill)
7549 {
7550 	conn_t	*connp = NULL;
7551 
7552 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7553 	ASSERT(func != NULL);
7554 
7555 	mp->b_queue = q;
7556 	mp->b_prev = (void *)func;
7557 	mp->b_next = NULL;
7558 
7559 	switch (type) {
7560 	case CUR_OP:
7561 		if (ipsq->ipsq_mptail != NULL) {
7562 			ASSERT(ipsq->ipsq_mphead != NULL);
7563 			ipsq->ipsq_mptail->b_next = mp;
7564 		} else {
7565 			ASSERT(ipsq->ipsq_mphead == NULL);
7566 			ipsq->ipsq_mphead = mp;
7567 		}
7568 		ipsq->ipsq_mptail = mp;
7569 		break;
7570 
7571 	case NEW_OP:
7572 		if (ipsq->ipsq_xopq_mptail != NULL) {
7573 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7574 			ipsq->ipsq_xopq_mptail->b_next = mp;
7575 		} else {
7576 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7577 			ipsq->ipsq_xopq_mphead = mp;
7578 		}
7579 		ipsq->ipsq_xopq_mptail = mp;
7580 		break;
7581 	default:
7582 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7583 	}
7584 
7585 	if (CONN_Q(q) && pending_ill != NULL) {
7586 		connp = Q_TO_CONN(q);
7587 
7588 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7589 		connp->conn_oper_pending_ill = pending_ill;
7590 	}
7591 }
7592 
7593 /*
7594  * Return the mp at the head of the ipsq. After emptying the ipsq
7595  * look at the next ioctl, if this ioctl is complete. Otherwise
7596  * return, we will resume when we complete the current ioctl.
7597  * The current ioctl will wait till it gets a response from the
7598  * driver below.
7599  */
7600 static mblk_t *
7601 ipsq_dq(ipsq_t *ipsq)
7602 {
7603 	mblk_t	*mp;
7604 
7605 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7606 
7607 	mp = ipsq->ipsq_mphead;
7608 	if (mp != NULL) {
7609 		ipsq->ipsq_mphead = mp->b_next;
7610 		if (ipsq->ipsq_mphead == NULL)
7611 			ipsq->ipsq_mptail = NULL;
7612 		mp->b_next = NULL;
7613 		return (mp);
7614 	}
7615 	if (ipsq->ipsq_current_ipif != NULL)
7616 		return (NULL);
7617 	mp = ipsq->ipsq_xopq_mphead;
7618 	if (mp != NULL) {
7619 		ipsq->ipsq_xopq_mphead = mp->b_next;
7620 		if (ipsq->ipsq_xopq_mphead == NULL)
7621 			ipsq->ipsq_xopq_mptail = NULL;
7622 		mp->b_next = NULL;
7623 		return (mp);
7624 	}
7625 	return (NULL);
7626 }
7627 
7628 /*
7629  * Enter the ipsq corresponding to ill, by waiting synchronously till
7630  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7631  * will have to drain completely before ipsq_enter returns success.
7632  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7633  * and the ipsq_exit logic will start the next enqueued ioctl after
7634  * completion of the current ioctl. If 'force' is used, we don't wait
7635  * for the enqueued ioctls. This is needed when a conn_close wants to
7636  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7637  * of an ill can also use this option. But we dont' use it currently.
7638  */
7639 #define	ENTER_SQ_WAIT_TICKS 100
7640 boolean_t
7641 ipsq_enter(ill_t *ill, boolean_t force)
7642 {
7643 	ipsq_t	*ipsq;
7644 	boolean_t waited_enough = B_FALSE;
7645 
7646 	/*
7647 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7648 	 * Since the <ill-ipsq> assocs could change while we wait for the
7649 	 * writer, it is easier to wait on a fixed global rather than try to
7650 	 * cv_wait on a changing ipsq.
7651 	 */
7652 	mutex_enter(&ill->ill_lock);
7653 	for (;;) {
7654 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7655 			mutex_exit(&ill->ill_lock);
7656 			return (B_FALSE);
7657 		}
7658 
7659 		ipsq = ill->ill_phyint->phyint_ipsq;
7660 		mutex_enter(&ipsq->ipsq_lock);
7661 		if (ipsq->ipsq_writer == NULL &&
7662 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7663 			break;
7664 		} else if (ipsq->ipsq_writer != NULL) {
7665 			mutex_exit(&ipsq->ipsq_lock);
7666 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7667 		} else {
7668 			mutex_exit(&ipsq->ipsq_lock);
7669 			if (force) {
7670 				(void) cv_timedwait(&ill->ill_cv,
7671 				    &ill->ill_lock,
7672 				    lbolt + ENTER_SQ_WAIT_TICKS);
7673 				waited_enough = B_TRUE;
7674 				continue;
7675 			} else {
7676 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7677 			}
7678 		}
7679 	}
7680 
7681 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7682 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7683 	ipsq->ipsq_writer = curthread;
7684 	ipsq->ipsq_reentry_cnt++;
7685 #ifdef DEBUG
7686 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7687 #endif
7688 	mutex_exit(&ipsq->ipsq_lock);
7689 	mutex_exit(&ill->ill_lock);
7690 	return (B_TRUE);
7691 }
7692 
7693 /*
7694  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7695  * certain critical operations like plumbing (i.e. most set ioctls),
7696  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7697  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7698  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7699  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7700  * threads executing in the ipsq. Responses from the driver pertain to the
7701  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7702  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7703  *
7704  * If a thread does not want to reenter the ipsq when it is already writer,
7705  * it must make sure that the specified reentry point to be called later
7706  * when the ipsq is empty, nor any code path starting from the specified reentry
7707  * point must never ever try to enter the ipsq again. Otherwise it can lead
7708  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7709  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7710  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7711  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7712  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7713  * ioctl if the current ioctl has completed. If the current ioctl is still
7714  * in progress it simply returns. The current ioctl could be waiting for
7715  * a response from another module (arp_ or the driver or could be waiting for
7716  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7717  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7718  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7719  * ipsq_current_ipif is clear which happens only on ioctl completion.
7720  */
7721 
7722 /*
7723  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7724  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7725  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7726  * completion.
7727  */
7728 ipsq_t *
7729 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7730     ipsq_func_t func, int type, boolean_t reentry_ok)
7731 {
7732 	ipsq_t	*ipsq;
7733 
7734 	/* Only 1 of ipif or ill can be specified */
7735 	ASSERT((ipif != NULL) ^ (ill != NULL));
7736 	if (ipif != NULL)
7737 		ill = ipif->ipif_ill;
7738 
7739 	/*
7740 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7741 	 * ipsq of an ill can't change when ill_lock is held.
7742 	 */
7743 	GRAB_CONN_LOCK(q);
7744 	mutex_enter(&ill->ill_lock);
7745 	ipsq = ill->ill_phyint->phyint_ipsq;
7746 	mutex_enter(&ipsq->ipsq_lock);
7747 
7748 	/*
7749 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7750 	 *    (Note: If the caller does not specify reentry_ok then neither
7751 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7752 	 *    again. Otherwise it can lead to an infinite loop
7753 	 * 2. Enter the ipsq if there is no current writer and this attempted
7754 	 *    entry is part of the current ioctl or operation
7755 	 * 3. Enter the ipsq if there is no current writer and this is a new
7756 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7757 	 *    empty and there is no ioctl (or operation) currently in progress
7758 	 */
7759 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7760 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7761 	    ipsq->ipsq_current_ipif == NULL))) ||
7762 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7763 		/* Success. */
7764 		ipsq->ipsq_reentry_cnt++;
7765 		ipsq->ipsq_writer = curthread;
7766 		mutex_exit(&ipsq->ipsq_lock);
7767 		mutex_exit(&ill->ill_lock);
7768 		RELEASE_CONN_LOCK(q);
7769 #ifdef DEBUG
7770 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7771 		    IPSQ_STACK_DEPTH);
7772 #endif
7773 		return (ipsq);
7774 	}
7775 
7776 	ipsq_enq(ipsq, q, mp, func, type, ill);
7777 
7778 	mutex_exit(&ipsq->ipsq_lock);
7779 	mutex_exit(&ill->ill_lock);
7780 	RELEASE_CONN_LOCK(q);
7781 	return (NULL);
7782 }
7783 
7784 /*
7785  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7786  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7787  * cannot be entered, the mp is queued for completion.
7788  */
7789 void
7790 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7791     boolean_t reentry_ok)
7792 {
7793 	ipsq_t	*ipsq;
7794 
7795 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7796 
7797 	/*
7798 	 * Drop the caller's refhold on the ill.  This is safe since we either
7799 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7800 	 * IPSQ, in which case we return without accessing ill anymore.  This
7801 	 * is needed because func needs to see the correct refcount.
7802 	 * e.g. removeif can work only then.
7803 	 */
7804 	ill_refrele(ill);
7805 	if (ipsq != NULL) {
7806 		(*func)(ipsq, q, mp, NULL);
7807 		ipsq_exit(ipsq);
7808 	}
7809 }
7810 
7811 /*
7812  * If there are more than ILL_GRP_CNT ills in a group,
7813  * we use kmem alloc'd buffers, else use the stack
7814  */
7815 #define	ILL_GRP_CNT	14
7816 /*
7817  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7818  * Called by a thread that is currently exclusive on this ipsq.
7819  */
7820 void
7821 ipsq_exit(ipsq_t *ipsq)
7822 {
7823 	queue_t	*q;
7824 	mblk_t	*mp;
7825 	ipsq_func_t	func;
7826 	int	next;
7827 	ill_t	**ill_list = NULL;
7828 	size_t	ill_list_size = 0;
7829 	int	cnt = 0;
7830 	boolean_t need_ipsq_free = B_FALSE;
7831 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7832 
7833 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7834 	mutex_enter(&ipsq->ipsq_lock);
7835 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7836 	if (ipsq->ipsq_reentry_cnt != 1) {
7837 		ipsq->ipsq_reentry_cnt--;
7838 		mutex_exit(&ipsq->ipsq_lock);
7839 		return;
7840 	}
7841 
7842 	mp = ipsq_dq(ipsq);
7843 	while (mp != NULL) {
7844 again:
7845 		mutex_exit(&ipsq->ipsq_lock);
7846 		func = (ipsq_func_t)mp->b_prev;
7847 		q = (queue_t *)mp->b_queue;
7848 		mp->b_prev = NULL;
7849 		mp->b_queue = NULL;
7850 
7851 		/*
7852 		 * If 'q' is an conn queue, it is valid, since we did a
7853 		 * a refhold on the connp, at the start of the ioctl.
7854 		 * If 'q' is an ill queue, it is valid, since close of an
7855 		 * ill will clean up the 'ipsq'.
7856 		 */
7857 		(*func)(ipsq, q, mp, NULL);
7858 
7859 		mutex_enter(&ipsq->ipsq_lock);
7860 		mp = ipsq_dq(ipsq);
7861 	}
7862 
7863 	mutex_exit(&ipsq->ipsq_lock);
7864 
7865 	/*
7866 	 * Need to grab the locks in the right order. Need to
7867 	 * atomically check (under ipsq_lock) that there are no
7868 	 * messages before relinquishing the ipsq. Also need to
7869 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7870 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7871 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7872 	 * to grab ill_g_lock as writer.
7873 	 */
7874 	rw_enter(&ipst->ips_ill_g_lock,
7875 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7876 
7877 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7878 	if (ipsq->ipsq_refs != 0) {
7879 		/* At most 2 ills v4/v6 per phyint */
7880 		cnt = ipsq->ipsq_refs << 1;
7881 		ill_list_size = cnt * sizeof (ill_t *);
7882 		/*
7883 		 * If memory allocation fails, we will do the split
7884 		 * the next time ipsq_exit is called for whatever reason.
7885 		 * As long as the ipsq_split flag is set the need to
7886 		 * split is remembered.
7887 		 */
7888 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7889 		if (ill_list != NULL)
7890 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7891 	}
7892 	mutex_enter(&ipsq->ipsq_lock);
7893 	mp = ipsq_dq(ipsq);
7894 	if (mp != NULL) {
7895 		/* oops, some message has landed up, we can't get out */
7896 		if (ill_list != NULL)
7897 			ill_unlock_ills(ill_list, cnt);
7898 		rw_exit(&ipst->ips_ill_g_lock);
7899 		if (ill_list != NULL)
7900 			kmem_free(ill_list, ill_list_size);
7901 		ill_list = NULL;
7902 		ill_list_size = 0;
7903 		cnt = 0;
7904 		goto again;
7905 	}
7906 
7907 	/*
7908 	 * Split only if no ioctl is pending and if memory alloc succeeded
7909 	 * above.
7910 	 */
7911 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7912 	    ill_list != NULL) {
7913 		/*
7914 		 * No new ill can join this ipsq since we are holding the
7915 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7916 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7917 		 * If so we will retry on the next ipsq_exit.
7918 		 */
7919 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7920 	}
7921 
7922 	/*
7923 	 * We are holding the ipsq lock, hence no new messages can
7924 	 * land up on the ipsq, and there are no messages currently.
7925 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7926 	 * atomically while holding ill locks.
7927 	 */
7928 	ipsq->ipsq_writer = NULL;
7929 	ipsq->ipsq_reentry_cnt--;
7930 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7931 #ifdef DEBUG
7932 	ipsq->ipsq_depth = 0;
7933 #endif
7934 	mutex_exit(&ipsq->ipsq_lock);
7935 	/*
7936 	 * For IPMP this should wake up all ills in this ipsq.
7937 	 * We need to hold the ill_lock while waking up waiters to
7938 	 * avoid missed wakeups. But there is no need to acquire all
7939 	 * the ill locks and then wakeup. If we have not acquired all
7940 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7941 	 * wakes up ills one at a time after getting the right ill_lock
7942 	 */
7943 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7944 	if (ill_list != NULL)
7945 		ill_unlock_ills(ill_list, cnt);
7946 	if (ipsq->ipsq_refs == 0)
7947 		need_ipsq_free = B_TRUE;
7948 	rw_exit(&ipst->ips_ill_g_lock);
7949 	if (ill_list != 0)
7950 		kmem_free(ill_list, ill_list_size);
7951 
7952 	if (need_ipsq_free) {
7953 		/*
7954 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7955 		 * looked up. ipsq can be looked up only thru ill or phyint
7956 		 * and there are no ills/phyint on this ipsq.
7957 		 */
7958 		ipsq_delete(ipsq);
7959 	}
7960 
7961 	/*
7962 	 * Now that we're outside the IPSQ, start any IGMP/MLD timers.  We
7963 	 * can't start these inside the IPSQ since e.g. igmp_start_timers() ->
7964 	 * untimeout() (inside the IPSQ, waiting for an executing timeout to
7965 	 * finish) could deadlock with igmp_timeout_handler() -> ipsq_enter()
7966 	 * (executing the timeout, waiting to get inside the IPSQ).
7967 	 *
7968 	 * However, there is one exception to the above: if this thread *is*
7969 	 * the IGMP/MLD timeout handler thread, then we must not start its
7970 	 * timer until the current handler is done.
7971 	 */
7972 	mutex_enter(&ipst->ips_igmp_timer_lock);
7973 	if (curthread != ipst->ips_igmp_timer_thread) {
7974 		next = ipst->ips_igmp_deferred_next;
7975 		ipst->ips_igmp_deferred_next = INFINITY;
7976 		mutex_exit(&ipst->ips_igmp_timer_lock);
7977 
7978 		if (next != INFINITY)
7979 			igmp_start_timers(next, ipst);
7980 	} else {
7981 		mutex_exit(&ipst->ips_igmp_timer_lock);
7982 	}
7983 
7984 	mutex_enter(&ipst->ips_mld_timer_lock);
7985 	if (curthread != ipst->ips_mld_timer_thread) {
7986 		next = ipst->ips_mld_deferred_next;
7987 		ipst->ips_mld_deferred_next = INFINITY;
7988 		mutex_exit(&ipst->ips_mld_timer_lock);
7989 
7990 		if (next != INFINITY)
7991 			mld_start_timers(next, ipst);
7992 	} else {
7993 		mutex_exit(&ipst->ips_mld_timer_lock);
7994 	}
7995 }
7996 
7997 /*
7998  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7999  * and `ioccmd'.
8000  */
8001 void
8002 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8003 {
8004 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8005 
8006 	mutex_enter(&ipsq->ipsq_lock);
8007 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8008 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8009 	ipsq->ipsq_current_done = B_FALSE;
8010 	ipsq->ipsq_current_ipif = ipif;
8011 	ipsq->ipsq_current_ioctl = ioccmd;
8012 	mutex_exit(&ipsq->ipsq_lock);
8013 }
8014 
8015 /*
8016  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8017  * the next exclusive operation to begin once we ipsq_exit().  However, if
8018  * pending DLPI operations remain, then we will wait for the queue to drain
8019  * before allowing the next exclusive operation to begin.  This ensures that
8020  * DLPI operations from one exclusive operation are never improperly processed
8021  * as part of a subsequent exclusive operation.
8022  */
8023 void
8024 ipsq_current_finish(ipsq_t *ipsq)
8025 {
8026 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8027 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8028 
8029 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8030 
8031 	/*
8032 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8033 	 * (but in that case, IPIF_CHANGING will already be clear and no
8034 	 * pending DLPI messages can remain).
8035 	 */
8036 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8037 		ill_t *ill = ipif->ipif_ill;
8038 
8039 		mutex_enter(&ill->ill_lock);
8040 		dlpi_pending = ill->ill_dlpi_pending;
8041 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8042 		/* Send any queued event */
8043 		ill_nic_info_dispatch(ill);
8044 		mutex_exit(&ill->ill_lock);
8045 	}
8046 
8047 	mutex_enter(&ipsq->ipsq_lock);
8048 	ipsq->ipsq_current_ioctl = 0;
8049 	ipsq->ipsq_current_done = B_TRUE;
8050 	if (dlpi_pending == DL_PRIM_INVAL)
8051 		ipsq->ipsq_current_ipif = NULL;
8052 	mutex_exit(&ipsq->ipsq_lock);
8053 }
8054 
8055 /*
8056  * The ill is closing. Flush all messages on the ipsq that originated
8057  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8058  * for this ill since ipsq_enter could not have entered until then.
8059  * New messages can't be queued since the CONDEMNED flag is set.
8060  */
8061 static void
8062 ipsq_flush(ill_t *ill)
8063 {
8064 	queue_t	*q;
8065 	mblk_t	*prev;
8066 	mblk_t	*mp;
8067 	mblk_t	*mp_next;
8068 	ipsq_t	*ipsq;
8069 
8070 	ASSERT(IAM_WRITER_ILL(ill));
8071 	ipsq = ill->ill_phyint->phyint_ipsq;
8072 	/*
8073 	 * Flush any messages sent up by the driver.
8074 	 */
8075 	mutex_enter(&ipsq->ipsq_lock);
8076 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8077 		mp_next = mp->b_next;
8078 		q = mp->b_queue;
8079 		if (q == ill->ill_rq || q == ill->ill_wq) {
8080 			/* Remove the mp from the ipsq */
8081 			if (prev == NULL)
8082 				ipsq->ipsq_mphead = mp->b_next;
8083 			else
8084 				prev->b_next = mp->b_next;
8085 			if (ipsq->ipsq_mptail == mp) {
8086 				ASSERT(mp_next == NULL);
8087 				ipsq->ipsq_mptail = prev;
8088 			}
8089 			inet_freemsg(mp);
8090 		} else {
8091 			prev = mp;
8092 		}
8093 	}
8094 	mutex_exit(&ipsq->ipsq_lock);
8095 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8096 	ipsq_xopq_mp_cleanup(ill, NULL);
8097 	ill_pending_mp_cleanup(ill);
8098 }
8099 
8100 /* ARGSUSED */
8101 int
8102 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8103     ip_ioctl_cmd_t *ipip, void *ifreq)
8104 {
8105 	ill_t	*ill;
8106 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8107 	boolean_t isv6;
8108 	conn_t	*connp;
8109 	ip_stack_t	*ipst;
8110 
8111 	connp = Q_TO_CONN(q);
8112 	ipst = connp->conn_netstack->netstack_ip;
8113 	isv6 = connp->conn_af_isv6;
8114 	/*
8115 	 * Set original index.
8116 	 * Failover and failback move logical interfaces
8117 	 * from one physical interface to another.  The
8118 	 * original index indicates the parent of a logical
8119 	 * interface, in other words, the physical interface
8120 	 * the logical interface will be moved back to on
8121 	 * failback.
8122 	 */
8123 
8124 	/*
8125 	 * Don't allow the original index to be changed
8126 	 * for non-failover addresses, autoconfigured
8127 	 * addresses, or IPv6 link local addresses.
8128 	 */
8129 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8130 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8131 		return (EINVAL);
8132 	}
8133 	/*
8134 	 * The new original index must be in use by some
8135 	 * physical interface.
8136 	 */
8137 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8138 	    NULL, NULL, ipst);
8139 	if (ill == NULL)
8140 		return (ENXIO);
8141 	ill_refrele(ill);
8142 
8143 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8144 	/*
8145 	 * When this ipif gets failed back, don't
8146 	 * preserve the original id, as it is no
8147 	 * longer applicable.
8148 	 */
8149 	ipif->ipif_orig_ipifid = 0;
8150 	/*
8151 	 * For IPv4, change the original index of any
8152 	 * multicast addresses associated with the
8153 	 * ipif to the new value.
8154 	 */
8155 	if (!isv6) {
8156 		ilm_t *ilm;
8157 
8158 		mutex_enter(&ipif->ipif_ill->ill_lock);
8159 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8160 		    ilm = ilm->ilm_next) {
8161 			if (ilm->ilm_ipif == ipif) {
8162 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8163 			}
8164 		}
8165 		mutex_exit(&ipif->ipif_ill->ill_lock);
8166 	}
8167 	return (0);
8168 }
8169 
8170 /* ARGSUSED */
8171 int
8172 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8173     ip_ioctl_cmd_t *ipip, void *ifreq)
8174 {
8175 	struct lifreq *lifr = (struct lifreq *)ifreq;
8176 
8177 	/*
8178 	 * Get the original interface index i.e the one
8179 	 * before FAILOVER if it ever happened.
8180 	 */
8181 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8182 	return (0);
8183 }
8184 
8185 /*
8186  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8187  * refhold and return the associated ipif
8188  */
8189 /* ARGSUSED */
8190 int
8191 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8192     cmd_info_t *ci, ipsq_func_t func)
8193 {
8194 	boolean_t exists;
8195 	struct iftun_req *ta;
8196 	ipif_t	*ipif;
8197 	ill_t	*ill;
8198 	boolean_t isv6;
8199 	mblk_t	*mp1;
8200 	int	error;
8201 	conn_t	*connp;
8202 	ip_stack_t	*ipst;
8203 
8204 	/* Existence verified in ip_wput_nondata */
8205 	mp1 = mp->b_cont->b_cont;
8206 	ta = (struct iftun_req *)mp1->b_rptr;
8207 	/*
8208 	 * Null terminate the string to protect against buffer
8209 	 * overrun. String was generated by user code and may not
8210 	 * be trusted.
8211 	 */
8212 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8213 
8214 	connp = Q_TO_CONN(q);
8215 	isv6 = connp->conn_af_isv6;
8216 	ipst = connp->conn_netstack->netstack_ip;
8217 
8218 	/* Disallows implicit create */
8219 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8220 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8221 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8222 	if (ipif == NULL)
8223 		return (error);
8224 
8225 	if (ipif->ipif_id != 0) {
8226 		/*
8227 		 * We really don't want to set/get tunnel parameters
8228 		 * on virtual tunnel interfaces.  Only allow the
8229 		 * base tunnel to do these.
8230 		 */
8231 		ipif_refrele(ipif);
8232 		return (EINVAL);
8233 	}
8234 
8235 	/*
8236 	 * Send down to tunnel mod for ioctl processing.
8237 	 * Will finish ioctl in ip_rput_other().
8238 	 */
8239 	ill = ipif->ipif_ill;
8240 	if (ill->ill_net_type == IRE_LOOPBACK) {
8241 		ipif_refrele(ipif);
8242 		return (EOPNOTSUPP);
8243 	}
8244 
8245 	if (ill->ill_wq == NULL) {
8246 		ipif_refrele(ipif);
8247 		return (ENXIO);
8248 	}
8249 	/*
8250 	 * Mark the ioctl as coming from an IPv6 interface for
8251 	 * tun's convenience.
8252 	 */
8253 	if (ill->ill_isv6)
8254 		ta->ifta_flags |= 0x80000000;
8255 	ci->ci_ipif = ipif;
8256 	return (0);
8257 }
8258 
8259 /*
8260  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8261  * and return the associated ipif.
8262  * Return value:
8263  *	Non zero: An error has occurred. ci may not be filled out.
8264  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8265  *	a held ipif in ci.ci_ipif.
8266  */
8267 int
8268 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8269     cmd_info_t *ci, ipsq_func_t func)
8270 {
8271 	sin_t		*sin;
8272 	sin6_t		*sin6;
8273 	char		*name;
8274 	struct ifreq    *ifr;
8275 	struct lifreq    *lifr;
8276 	ipif_t		*ipif = NULL;
8277 	ill_t		*ill;
8278 	conn_t		*connp;
8279 	boolean_t	isv6;
8280 	boolean_t	exists;
8281 	int		err;
8282 	mblk_t		*mp1;
8283 	zoneid_t	zoneid;
8284 	ip_stack_t	*ipst;
8285 
8286 	if (q->q_next != NULL) {
8287 		ill = (ill_t *)q->q_ptr;
8288 		isv6 = ill->ill_isv6;
8289 		connp = NULL;
8290 		zoneid = ALL_ZONES;
8291 		ipst = ill->ill_ipst;
8292 	} else {
8293 		ill = NULL;
8294 		connp = Q_TO_CONN(q);
8295 		isv6 = connp->conn_af_isv6;
8296 		zoneid = connp->conn_zoneid;
8297 		if (zoneid == GLOBAL_ZONEID) {
8298 			/* global zone can access ipifs in all zones */
8299 			zoneid = ALL_ZONES;
8300 		}
8301 		ipst = connp->conn_netstack->netstack_ip;
8302 	}
8303 
8304 	/* Has been checked in ip_wput_nondata */
8305 	mp1 = mp->b_cont->b_cont;
8306 
8307 	if (ipip->ipi_cmd_type == IF_CMD) {
8308 		/* This a old style SIOC[GS]IF* command */
8309 		ifr = (struct ifreq *)mp1->b_rptr;
8310 		/*
8311 		 * Null terminate the string to protect against buffer
8312 		 * overrun. String was generated by user code and may not
8313 		 * be trusted.
8314 		 */
8315 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8316 		sin = (sin_t *)&ifr->ifr_addr;
8317 		name = ifr->ifr_name;
8318 		ci->ci_sin = sin;
8319 		ci->ci_sin6 = NULL;
8320 		ci->ci_lifr = (struct lifreq *)ifr;
8321 	} else {
8322 		/* This a new style SIOC[GS]LIF* command */
8323 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8324 		lifr = (struct lifreq *)mp1->b_rptr;
8325 		/*
8326 		 * Null terminate the string to protect against buffer
8327 		 * overrun. String was generated by user code and may not
8328 		 * be trusted.
8329 		 */
8330 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8331 		name = lifr->lifr_name;
8332 		sin = (sin_t *)&lifr->lifr_addr;
8333 		sin6 = (sin6_t *)&lifr->lifr_addr;
8334 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8335 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8336 			    LIFNAMSIZ);
8337 		}
8338 		ci->ci_sin = sin;
8339 		ci->ci_sin6 = sin6;
8340 		ci->ci_lifr = lifr;
8341 	}
8342 
8343 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8344 		/*
8345 		 * The ioctl will be failed if the ioctl comes down
8346 		 * an conn stream
8347 		 */
8348 		if (ill == NULL) {
8349 			/*
8350 			 * Not an ill queue, return EINVAL same as the
8351 			 * old error code.
8352 			 */
8353 			return (ENXIO);
8354 		}
8355 		ipif = ill->ill_ipif;
8356 		ipif_refhold(ipif);
8357 	} else {
8358 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8359 		    &exists, isv6, zoneid,
8360 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8361 		    ipst);
8362 		if (ipif == NULL) {
8363 			if (err == EINPROGRESS)
8364 				return (err);
8365 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8366 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8367 				/*
8368 				 * Need to try both v4 and v6 since this
8369 				 * ioctl can come down either v4 or v6
8370 				 * socket. The lifreq.lifr_family passed
8371 				 * down by this ioctl is AF_UNSPEC.
8372 				 */
8373 				ipif = ipif_lookup_on_name(name,
8374 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8375 				    zoneid, (connp == NULL) ? q :
8376 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8377 				if (err == EINPROGRESS)
8378 					return (err);
8379 			}
8380 			err = 0;	/* Ensure we don't use it below */
8381 		}
8382 	}
8383 
8384 	/*
8385 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8386 	 */
8387 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8388 		ipif_refrele(ipif);
8389 		return (ENXIO);
8390 	}
8391 
8392 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8393 	    name[0] == '\0') {
8394 		/*
8395 		 * Handle a or a SIOC?IF* with a null name
8396 		 * during plumb (on the ill queue before the I_PLINK).
8397 		 */
8398 		ipif = ill->ill_ipif;
8399 		ipif_refhold(ipif);
8400 	}
8401 
8402 	if (ipif == NULL)
8403 		return (ENXIO);
8404 
8405 	/*
8406 	 * Allow only GET operations if this ipif has been created
8407 	 * temporarily due to a MOVE operation.
8408 	 */
8409 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8410 		ipif_refrele(ipif);
8411 		return (EINVAL);
8412 	}
8413 
8414 	ci->ci_ipif = ipif;
8415 	return (0);
8416 }
8417 
8418 /*
8419  * Return the total number of ipifs.
8420  */
8421 static uint_t
8422 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8423 {
8424 	uint_t numifs = 0;
8425 	ill_t	*ill;
8426 	ill_walk_context_t	ctx;
8427 	ipif_t	*ipif;
8428 
8429 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8430 	ill = ILL_START_WALK_V4(&ctx, ipst);
8431 
8432 	while (ill != NULL) {
8433 		for (ipif = ill->ill_ipif; ipif != NULL;
8434 		    ipif = ipif->ipif_next) {
8435 			if (ipif->ipif_zoneid == zoneid ||
8436 			    ipif->ipif_zoneid == ALL_ZONES)
8437 				numifs++;
8438 		}
8439 		ill = ill_next(&ctx, ill);
8440 	}
8441 	rw_exit(&ipst->ips_ill_g_lock);
8442 	return (numifs);
8443 }
8444 
8445 /*
8446  * Return the total number of ipifs.
8447  */
8448 static uint_t
8449 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8450 {
8451 	uint_t numifs = 0;
8452 	ill_t	*ill;
8453 	ipif_t	*ipif;
8454 	ill_walk_context_t	ctx;
8455 
8456 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8457 
8458 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8459 	if (family == AF_INET)
8460 		ill = ILL_START_WALK_V4(&ctx, ipst);
8461 	else if (family == AF_INET6)
8462 		ill = ILL_START_WALK_V6(&ctx, ipst);
8463 	else
8464 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8465 
8466 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8467 		for (ipif = ill->ill_ipif; ipif != NULL;
8468 		    ipif = ipif->ipif_next) {
8469 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8470 			    !(lifn_flags & LIFC_NOXMIT))
8471 				continue;
8472 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8473 			    !(lifn_flags & LIFC_TEMPORARY))
8474 				continue;
8475 			if (((ipif->ipif_flags &
8476 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8477 			    IPIF_DEPRECATED)) ||
8478 			    IS_LOOPBACK(ill) ||
8479 			    !(ipif->ipif_flags & IPIF_UP)) &&
8480 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8481 				continue;
8482 
8483 			if (zoneid != ipif->ipif_zoneid &&
8484 			    ipif->ipif_zoneid != ALL_ZONES &&
8485 			    (zoneid != GLOBAL_ZONEID ||
8486 			    !(lifn_flags & LIFC_ALLZONES)))
8487 				continue;
8488 
8489 			numifs++;
8490 		}
8491 	}
8492 	rw_exit(&ipst->ips_ill_g_lock);
8493 	return (numifs);
8494 }
8495 
8496 uint_t
8497 ip_get_lifsrcofnum(ill_t *ill)
8498 {
8499 	uint_t numifs = 0;
8500 	ill_t	*ill_head = ill;
8501 	ip_stack_t	*ipst = ill->ill_ipst;
8502 
8503 	/*
8504 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8505 	 * other thread may be trying to relink the ILLs in this usesrc group
8506 	 * and adjusting the ill_usesrc_grp_next pointers
8507 	 */
8508 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8509 	if ((ill->ill_usesrc_ifindex == 0) &&
8510 	    (ill->ill_usesrc_grp_next != NULL)) {
8511 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8512 		    ill = ill->ill_usesrc_grp_next)
8513 			numifs++;
8514 	}
8515 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8516 
8517 	return (numifs);
8518 }
8519 
8520 /* Null values are passed in for ipif, sin, and ifreq */
8521 /* ARGSUSED */
8522 int
8523 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8524     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8525 {
8526 	int *nump;
8527 	conn_t *connp = Q_TO_CONN(q);
8528 
8529 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8530 
8531 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8532 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8533 
8534 	*nump = ip_get_numifs(connp->conn_zoneid,
8535 	    connp->conn_netstack->netstack_ip);
8536 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8537 	return (0);
8538 }
8539 
8540 /* Null values are passed in for ipif, sin, and ifreq */
8541 /* ARGSUSED */
8542 int
8543 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8544     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8545 {
8546 	struct lifnum *lifn;
8547 	mblk_t	*mp1;
8548 	conn_t *connp = Q_TO_CONN(q);
8549 
8550 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8551 
8552 	/* Existence checked in ip_wput_nondata */
8553 	mp1 = mp->b_cont->b_cont;
8554 
8555 	lifn = (struct lifnum *)mp1->b_rptr;
8556 	switch (lifn->lifn_family) {
8557 	case AF_UNSPEC:
8558 	case AF_INET:
8559 	case AF_INET6:
8560 		break;
8561 	default:
8562 		return (EAFNOSUPPORT);
8563 	}
8564 
8565 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8566 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8567 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8568 	return (0);
8569 }
8570 
8571 /* ARGSUSED */
8572 int
8573 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8574     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8575 {
8576 	STRUCT_HANDLE(ifconf, ifc);
8577 	mblk_t *mp1;
8578 	struct iocblk *iocp;
8579 	struct ifreq *ifr;
8580 	ill_walk_context_t	ctx;
8581 	ill_t	*ill;
8582 	ipif_t	*ipif;
8583 	struct sockaddr_in *sin;
8584 	int32_t	ifclen;
8585 	zoneid_t zoneid;
8586 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8587 
8588 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8589 
8590 	ip1dbg(("ip_sioctl_get_ifconf"));
8591 	/* Existence verified in ip_wput_nondata */
8592 	mp1 = mp->b_cont->b_cont;
8593 	iocp = (struct iocblk *)mp->b_rptr;
8594 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8595 
8596 	/*
8597 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8598 	 * the user buffer address and length into which the list of struct
8599 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8600 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8601 	 * the SIOCGIFCONF operation was redefined to simply provide
8602 	 * a large output buffer into which we are supposed to jam the ifreq
8603 	 * array.  The same ioctl command code was used, despite the fact that
8604 	 * both the applications and the kernel code had to change, thus making
8605 	 * it impossible to support both interfaces.
8606 	 *
8607 	 * For reasons not good enough to try to explain, the following
8608 	 * algorithm is used for deciding what to do with one of these:
8609 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8610 	 * form with the output buffer coming down as the continuation message.
8611 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8612 	 * and we have to copy in the ifconf structure to find out how big the
8613 	 * output buffer is and where to copy out to.  Sure no problem...
8614 	 *
8615 	 */
8616 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8617 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8618 		int numifs = 0;
8619 		size_t ifc_bufsize;
8620 
8621 		/*
8622 		 * Must be (better be!) continuation of a TRANSPARENT
8623 		 * IOCTL.  We just copied in the ifconf structure.
8624 		 */
8625 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8626 		    (struct ifconf *)mp1->b_rptr);
8627 
8628 		/*
8629 		 * Allocate a buffer to hold requested information.
8630 		 *
8631 		 * If ifc_len is larger than what is needed, we only
8632 		 * allocate what we will use.
8633 		 *
8634 		 * If ifc_len is smaller than what is needed, return
8635 		 * EINVAL.
8636 		 *
8637 		 * XXX: the ill_t structure can hava 2 counters, for
8638 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8639 		 * number of interfaces for a device, so we don't need
8640 		 * to count them here...
8641 		 */
8642 		numifs = ip_get_numifs(zoneid, ipst);
8643 
8644 		ifclen = STRUCT_FGET(ifc, ifc_len);
8645 		ifc_bufsize = numifs * sizeof (struct ifreq);
8646 		if (ifc_bufsize > ifclen) {
8647 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8648 				/* old behaviour */
8649 				return (EINVAL);
8650 			} else {
8651 				ifc_bufsize = ifclen;
8652 			}
8653 		}
8654 
8655 		mp1 = mi_copyout_alloc(q, mp,
8656 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8657 		if (mp1 == NULL)
8658 			return (ENOMEM);
8659 
8660 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8661 	}
8662 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8663 	/*
8664 	 * the SIOCGIFCONF ioctl only knows about
8665 	 * IPv4 addresses, so don't try to tell
8666 	 * it about interfaces with IPv6-only
8667 	 * addresses. (Last parm 'isv6' is B_FALSE)
8668 	 */
8669 
8670 	ifr = (struct ifreq *)mp1->b_rptr;
8671 
8672 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8673 	ill = ILL_START_WALK_V4(&ctx, ipst);
8674 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8675 		for (ipif = ill->ill_ipif; ipif != NULL;
8676 		    ipif = ipif->ipif_next) {
8677 			if (zoneid != ipif->ipif_zoneid &&
8678 			    ipif->ipif_zoneid != ALL_ZONES)
8679 				continue;
8680 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8681 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8682 					/* old behaviour */
8683 					rw_exit(&ipst->ips_ill_g_lock);
8684 					return (EINVAL);
8685 				} else {
8686 					goto if_copydone;
8687 				}
8688 			}
8689 			ipif_get_name(ipif, ifr->ifr_name,
8690 			    sizeof (ifr->ifr_name));
8691 			sin = (sin_t *)&ifr->ifr_addr;
8692 			*sin = sin_null;
8693 			sin->sin_family = AF_INET;
8694 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8695 			ifr++;
8696 		}
8697 	}
8698 if_copydone:
8699 	rw_exit(&ipst->ips_ill_g_lock);
8700 	mp1->b_wptr = (uchar_t *)ifr;
8701 
8702 	if (STRUCT_BUF(ifc) != NULL) {
8703 		STRUCT_FSET(ifc, ifc_len,
8704 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8705 	}
8706 	return (0);
8707 }
8708 
8709 /*
8710  * Get the interfaces using the address hosted on the interface passed in,
8711  * as a source adddress
8712  */
8713 /* ARGSUSED */
8714 int
8715 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8716     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8717 {
8718 	mblk_t *mp1;
8719 	ill_t	*ill, *ill_head;
8720 	ipif_t	*ipif, *orig_ipif;
8721 	int	numlifs = 0;
8722 	size_t	lifs_bufsize, lifsmaxlen;
8723 	struct	lifreq *lifr;
8724 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8725 	uint_t	ifindex;
8726 	zoneid_t zoneid;
8727 	int err = 0;
8728 	boolean_t isv6 = B_FALSE;
8729 	struct	sockaddr_in	*sin;
8730 	struct	sockaddr_in6	*sin6;
8731 	STRUCT_HANDLE(lifsrcof, lifs);
8732 	ip_stack_t		*ipst;
8733 
8734 	ipst = CONNQ_TO_IPST(q);
8735 
8736 	ASSERT(q->q_next == NULL);
8737 
8738 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8739 
8740 	/* Existence verified in ip_wput_nondata */
8741 	mp1 = mp->b_cont->b_cont;
8742 
8743 	/*
8744 	 * Must be (better be!) continuation of a TRANSPARENT
8745 	 * IOCTL.  We just copied in the lifsrcof structure.
8746 	 */
8747 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8748 	    (struct lifsrcof *)mp1->b_rptr);
8749 
8750 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8751 		return (EINVAL);
8752 
8753 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8754 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8755 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8756 	    ip_process_ioctl, &err, ipst);
8757 	if (ipif == NULL) {
8758 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8759 		    ifindex));
8760 		return (err);
8761 	}
8762 
8763 	/* Allocate a buffer to hold requested information */
8764 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8765 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8766 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8767 	/* The actual size needed is always returned in lifs_len */
8768 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8769 
8770 	/* If the amount we need is more than what is passed in, abort */
8771 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8772 		ipif_refrele(ipif);
8773 		return (0);
8774 	}
8775 
8776 	mp1 = mi_copyout_alloc(q, mp,
8777 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8778 	if (mp1 == NULL) {
8779 		ipif_refrele(ipif);
8780 		return (ENOMEM);
8781 	}
8782 
8783 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8784 	bzero(mp1->b_rptr, lifs_bufsize);
8785 
8786 	lifr = (struct lifreq *)mp1->b_rptr;
8787 
8788 	ill = ill_head = ipif->ipif_ill;
8789 	orig_ipif = ipif;
8790 
8791 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8792 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8793 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8794 
8795 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8796 	for (; (ill != NULL) && (ill != ill_head);
8797 	    ill = ill->ill_usesrc_grp_next) {
8798 
8799 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8800 			break;
8801 
8802 		ipif = ill->ill_ipif;
8803 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8804 		if (ipif->ipif_isv6) {
8805 			sin6 = (sin6_t *)&lifr->lifr_addr;
8806 			*sin6 = sin6_null;
8807 			sin6->sin6_family = AF_INET6;
8808 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8809 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8810 			    &ipif->ipif_v6net_mask);
8811 		} else {
8812 			sin = (sin_t *)&lifr->lifr_addr;
8813 			*sin = sin_null;
8814 			sin->sin_family = AF_INET;
8815 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8816 			lifr->lifr_addrlen = ip_mask_to_plen(
8817 			    ipif->ipif_net_mask);
8818 		}
8819 		lifr++;
8820 	}
8821 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8822 	rw_exit(&ipst->ips_ill_g_lock);
8823 	ipif_refrele(orig_ipif);
8824 	mp1->b_wptr = (uchar_t *)lifr;
8825 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8826 
8827 	return (0);
8828 }
8829 
8830 /* ARGSUSED */
8831 int
8832 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8833     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8834 {
8835 	mblk_t *mp1;
8836 	int	list;
8837 	ill_t	*ill;
8838 	ipif_t	*ipif;
8839 	int	flags;
8840 	int	numlifs = 0;
8841 	size_t	lifc_bufsize;
8842 	struct	lifreq *lifr;
8843 	sa_family_t	family;
8844 	struct	sockaddr_in	*sin;
8845 	struct	sockaddr_in6	*sin6;
8846 	ill_walk_context_t	ctx;
8847 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8848 	int32_t	lifclen;
8849 	zoneid_t zoneid;
8850 	STRUCT_HANDLE(lifconf, lifc);
8851 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8852 
8853 	ip1dbg(("ip_sioctl_get_lifconf"));
8854 
8855 	ASSERT(q->q_next == NULL);
8856 
8857 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8858 
8859 	/* Existence verified in ip_wput_nondata */
8860 	mp1 = mp->b_cont->b_cont;
8861 
8862 	/*
8863 	 * An extended version of SIOCGIFCONF that takes an
8864 	 * additional address family and flags field.
8865 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8866 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8867 	 * interfaces are omitted.
8868 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8869 	 * unless LIFC_TEMPORARY is specified.
8870 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8871 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8872 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8873 	 * has priority over LIFC_NOXMIT.
8874 	 */
8875 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8876 
8877 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8878 		return (EINVAL);
8879 
8880 	/*
8881 	 * Must be (better be!) continuation of a TRANSPARENT
8882 	 * IOCTL.  We just copied in the lifconf structure.
8883 	 */
8884 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8885 
8886 	family = STRUCT_FGET(lifc, lifc_family);
8887 	flags = STRUCT_FGET(lifc, lifc_flags);
8888 
8889 	switch (family) {
8890 	case AF_UNSPEC:
8891 		/*
8892 		 * walk all ILL's.
8893 		 */
8894 		list = MAX_G_HEADS;
8895 		break;
8896 	case AF_INET:
8897 		/*
8898 		 * walk only IPV4 ILL's.
8899 		 */
8900 		list = IP_V4_G_HEAD;
8901 		break;
8902 	case AF_INET6:
8903 		/*
8904 		 * walk only IPV6 ILL's.
8905 		 */
8906 		list = IP_V6_G_HEAD;
8907 		break;
8908 	default:
8909 		return (EAFNOSUPPORT);
8910 	}
8911 
8912 	/*
8913 	 * Allocate a buffer to hold requested information.
8914 	 *
8915 	 * If lifc_len is larger than what is needed, we only
8916 	 * allocate what we will use.
8917 	 *
8918 	 * If lifc_len is smaller than what is needed, return
8919 	 * EINVAL.
8920 	 */
8921 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8922 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8923 	lifclen = STRUCT_FGET(lifc, lifc_len);
8924 	if (lifc_bufsize > lifclen) {
8925 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8926 			return (EINVAL);
8927 		else
8928 			lifc_bufsize = lifclen;
8929 	}
8930 
8931 	mp1 = mi_copyout_alloc(q, mp,
8932 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8933 	if (mp1 == NULL)
8934 		return (ENOMEM);
8935 
8936 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8937 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8938 
8939 	lifr = (struct lifreq *)mp1->b_rptr;
8940 
8941 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8942 	ill = ill_first(list, list, &ctx, ipst);
8943 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8944 		for (ipif = ill->ill_ipif; ipif != NULL;
8945 		    ipif = ipif->ipif_next) {
8946 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8947 			    !(flags & LIFC_NOXMIT))
8948 				continue;
8949 
8950 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8951 			    !(flags & LIFC_TEMPORARY))
8952 				continue;
8953 
8954 			if (((ipif->ipif_flags &
8955 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8956 			    IPIF_DEPRECATED)) ||
8957 			    IS_LOOPBACK(ill) ||
8958 			    !(ipif->ipif_flags & IPIF_UP)) &&
8959 			    (flags & LIFC_EXTERNAL_SOURCE))
8960 				continue;
8961 
8962 			if (zoneid != ipif->ipif_zoneid &&
8963 			    ipif->ipif_zoneid != ALL_ZONES &&
8964 			    (zoneid != GLOBAL_ZONEID ||
8965 			    !(flags & LIFC_ALLZONES)))
8966 				continue;
8967 
8968 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8969 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8970 					rw_exit(&ipst->ips_ill_g_lock);
8971 					return (EINVAL);
8972 				} else {
8973 					goto lif_copydone;
8974 				}
8975 			}
8976 
8977 			ipif_get_name(ipif, lifr->lifr_name,
8978 			    sizeof (lifr->lifr_name));
8979 			if (ipif->ipif_isv6) {
8980 				sin6 = (sin6_t *)&lifr->lifr_addr;
8981 				*sin6 = sin6_null;
8982 				sin6->sin6_family = AF_INET6;
8983 				sin6->sin6_addr =
8984 				    ipif->ipif_v6lcl_addr;
8985 				lifr->lifr_addrlen =
8986 				    ip_mask_to_plen_v6(
8987 				    &ipif->ipif_v6net_mask);
8988 			} else {
8989 				sin = (sin_t *)&lifr->lifr_addr;
8990 				*sin = sin_null;
8991 				sin->sin_family = AF_INET;
8992 				sin->sin_addr.s_addr =
8993 				    ipif->ipif_lcl_addr;
8994 				lifr->lifr_addrlen =
8995 				    ip_mask_to_plen(
8996 				    ipif->ipif_net_mask);
8997 			}
8998 			lifr++;
8999 		}
9000 	}
9001 lif_copydone:
9002 	rw_exit(&ipst->ips_ill_g_lock);
9003 
9004 	mp1->b_wptr = (uchar_t *)lifr;
9005 	if (STRUCT_BUF(lifc) != NULL) {
9006 		STRUCT_FSET(lifc, lifc_len,
9007 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9008 	}
9009 	return (0);
9010 }
9011 
9012 /* ARGSUSED */
9013 int
9014 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9015     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9016 {
9017 	ip_stack_t	*ipst;
9018 
9019 	if (q->q_next == NULL)
9020 		ipst = CONNQ_TO_IPST(q);
9021 	else
9022 		ipst = ILLQ_TO_IPST(q);
9023 
9024 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9025 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9026 	return (0);
9027 }
9028 
9029 static void
9030 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9031 {
9032 	ip6_asp_t *table;
9033 	size_t table_size;
9034 	mblk_t *data_mp;
9035 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9036 	ip_stack_t	*ipst;
9037 
9038 	if (q->q_next == NULL)
9039 		ipst = CONNQ_TO_IPST(q);
9040 	else
9041 		ipst = ILLQ_TO_IPST(q);
9042 
9043 	/* These two ioctls are I_STR only */
9044 	if (iocp->ioc_count == TRANSPARENT) {
9045 		miocnak(q, mp, 0, EINVAL);
9046 		return;
9047 	}
9048 
9049 	data_mp = mp->b_cont;
9050 	if (data_mp == NULL) {
9051 		/* The user passed us a NULL argument */
9052 		table = NULL;
9053 		table_size = iocp->ioc_count;
9054 	} else {
9055 		/*
9056 		 * The user provided a table.  The stream head
9057 		 * may have copied in the user data in chunks,
9058 		 * so make sure everything is pulled up
9059 		 * properly.
9060 		 */
9061 		if (MBLKL(data_mp) < iocp->ioc_count) {
9062 			mblk_t *new_data_mp;
9063 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9064 			    NULL) {
9065 				miocnak(q, mp, 0, ENOMEM);
9066 				return;
9067 			}
9068 			freemsg(data_mp);
9069 			data_mp = new_data_mp;
9070 			mp->b_cont = data_mp;
9071 		}
9072 		table = (ip6_asp_t *)data_mp->b_rptr;
9073 		table_size = iocp->ioc_count;
9074 	}
9075 
9076 	switch (iocp->ioc_cmd) {
9077 	case SIOCGIP6ADDRPOLICY:
9078 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9079 		if (iocp->ioc_rval == -1)
9080 			iocp->ioc_error = EINVAL;
9081 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9082 		else if (table != NULL &&
9083 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9084 			ip6_asp_t *src = table;
9085 			ip6_asp32_t *dst = (void *)table;
9086 			int count = table_size / sizeof (ip6_asp_t);
9087 			int i;
9088 
9089 			/*
9090 			 * We need to do an in-place shrink of the array
9091 			 * to match the alignment attributes of the
9092 			 * 32-bit ABI looking at it.
9093 			 */
9094 			/* LINTED: logical expression always true: op "||" */
9095 			ASSERT(sizeof (*src) > sizeof (*dst));
9096 			for (i = 1; i < count; i++)
9097 				bcopy(src + i, dst + i, sizeof (*dst));
9098 		}
9099 #endif
9100 		break;
9101 
9102 	case SIOCSIP6ADDRPOLICY:
9103 		ASSERT(mp->b_prev == NULL);
9104 		mp->b_prev = (void *)q;
9105 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9106 		/*
9107 		 * We pass in the datamodel here so that the ip6_asp_replace()
9108 		 * routine can handle converting from 32-bit to native formats
9109 		 * where necessary.
9110 		 *
9111 		 * A better way to handle this might be to convert the inbound
9112 		 * data structure here, and hang it off a new 'mp'; thus the
9113 		 * ip6_asp_replace() logic would always be dealing with native
9114 		 * format data structures..
9115 		 *
9116 		 * (An even simpler way to handle these ioctls is to just
9117 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9118 		 * and just recompile everything that depends on it.)
9119 		 */
9120 #endif
9121 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9122 		    iocp->ioc_flag & IOC_MODELS);
9123 		return;
9124 	}
9125 
9126 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9127 	qreply(q, mp);
9128 }
9129 
9130 static void
9131 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9132 {
9133 	mblk_t 		*data_mp;
9134 	struct dstinforeq	*dir;
9135 	uint8_t		*end, *cur;
9136 	in6_addr_t	*daddr, *saddr;
9137 	ipaddr_t	v4daddr;
9138 	ire_t		*ire;
9139 	char		*slabel, *dlabel;
9140 	boolean_t	isipv4;
9141 	int		match_ire;
9142 	ill_t		*dst_ill;
9143 	ipif_t		*src_ipif, *ire_ipif;
9144 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9145 	zoneid_t	zoneid;
9146 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9147 
9148 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9149 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9150 
9151 	/*
9152 	 * This ioctl is I_STR only, and must have a
9153 	 * data mblk following the M_IOCTL mblk.
9154 	 */
9155 	data_mp = mp->b_cont;
9156 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9157 		miocnak(q, mp, 0, EINVAL);
9158 		return;
9159 	}
9160 
9161 	if (MBLKL(data_mp) < iocp->ioc_count) {
9162 		mblk_t *new_data_mp;
9163 
9164 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9165 			miocnak(q, mp, 0, ENOMEM);
9166 			return;
9167 		}
9168 		freemsg(data_mp);
9169 		data_mp = new_data_mp;
9170 		mp->b_cont = data_mp;
9171 	}
9172 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9173 
9174 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9175 	    end - cur >= sizeof (struct dstinforeq);
9176 	    cur += sizeof (struct dstinforeq)) {
9177 		dir = (struct dstinforeq *)cur;
9178 		daddr = &dir->dir_daddr;
9179 		saddr = &dir->dir_saddr;
9180 
9181 		/*
9182 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9183 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9184 		 * and ipif_select_source[_v6]() do not.
9185 		 */
9186 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9187 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9188 
9189 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9190 		if (isipv4) {
9191 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9192 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9193 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9194 		} else {
9195 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9196 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9197 		}
9198 		if (ire == NULL) {
9199 			dir->dir_dreachable = 0;
9200 
9201 			/* move on to next dst addr */
9202 			continue;
9203 		}
9204 		dir->dir_dreachable = 1;
9205 
9206 		ire_ipif = ire->ire_ipif;
9207 		if (ire_ipif == NULL)
9208 			goto next_dst;
9209 
9210 		/*
9211 		 * We expect to get back an interface ire or a
9212 		 * gateway ire cache entry.  For both types, the
9213 		 * output interface is ire_ipif->ipif_ill.
9214 		 */
9215 		dst_ill = ire_ipif->ipif_ill;
9216 		dir->dir_dmactype = dst_ill->ill_mactype;
9217 
9218 		if (isipv4) {
9219 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9220 		} else {
9221 			src_ipif = ipif_select_source_v6(dst_ill,
9222 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9223 			    zoneid);
9224 		}
9225 		if (src_ipif == NULL)
9226 			goto next_dst;
9227 
9228 		*saddr = src_ipif->ipif_v6lcl_addr;
9229 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9230 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9231 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9232 		dir->dir_sdeprecated =
9233 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9234 		ipif_refrele(src_ipif);
9235 next_dst:
9236 		ire_refrele(ire);
9237 	}
9238 	miocack(q, mp, iocp->ioc_count, 0);
9239 }
9240 
9241 /*
9242  * Check if this is an address assigned to this machine.
9243  * Skips interfaces that are down by using ire checks.
9244  * Translates mapped addresses to v4 addresses and then
9245  * treats them as such, returning true if the v4 address
9246  * associated with this mapped address is configured.
9247  * Note: Applications will have to be careful what they do
9248  * with the response; use of mapped addresses limits
9249  * what can be done with the socket, especially with
9250  * respect to socket options and ioctls - neither IPv4
9251  * options nor IPv6 sticky options/ancillary data options
9252  * may be used.
9253  */
9254 /* ARGSUSED */
9255 int
9256 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9257     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9258 {
9259 	struct sioc_addrreq *sia;
9260 	sin_t *sin;
9261 	ire_t *ire;
9262 	mblk_t *mp1;
9263 	zoneid_t zoneid;
9264 	ip_stack_t	*ipst;
9265 
9266 	ip1dbg(("ip_sioctl_tmyaddr"));
9267 
9268 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9269 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9270 	ipst = CONNQ_TO_IPST(q);
9271 
9272 	/* Existence verified in ip_wput_nondata */
9273 	mp1 = mp->b_cont->b_cont;
9274 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9275 	sin = (sin_t *)&sia->sa_addr;
9276 	switch (sin->sin_family) {
9277 	case AF_INET6: {
9278 		sin6_t *sin6 = (sin6_t *)sin;
9279 
9280 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9281 			ipaddr_t v4_addr;
9282 
9283 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9284 			    v4_addr);
9285 			ire = ire_ctable_lookup(v4_addr, 0,
9286 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9287 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9288 		} else {
9289 			in6_addr_t v6addr;
9290 
9291 			v6addr = sin6->sin6_addr;
9292 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9293 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9294 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9295 		}
9296 		break;
9297 	}
9298 	case AF_INET: {
9299 		ipaddr_t v4addr;
9300 
9301 		v4addr = sin->sin_addr.s_addr;
9302 		ire = ire_ctable_lookup(v4addr, 0,
9303 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9304 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9305 		break;
9306 	}
9307 	default:
9308 		return (EAFNOSUPPORT);
9309 	}
9310 	if (ire != NULL) {
9311 		sia->sa_res = 1;
9312 		ire_refrele(ire);
9313 	} else {
9314 		sia->sa_res = 0;
9315 	}
9316 	return (0);
9317 }
9318 
9319 /*
9320  * Check if this is an address assigned on-link i.e. neighbor,
9321  * and makes sure it's reachable from the current zone.
9322  * Returns true for my addresses as well.
9323  * Translates mapped addresses to v4 addresses and then
9324  * treats them as such, returning true if the v4 address
9325  * associated with this mapped address is configured.
9326  * Note: Applications will have to be careful what they do
9327  * with the response; use of mapped addresses limits
9328  * what can be done with the socket, especially with
9329  * respect to socket options and ioctls - neither IPv4
9330  * options nor IPv6 sticky options/ancillary data options
9331  * may be used.
9332  */
9333 /* ARGSUSED */
9334 int
9335 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9336     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9337 {
9338 	struct sioc_addrreq *sia;
9339 	sin_t *sin;
9340 	mblk_t	*mp1;
9341 	ire_t *ire = NULL;
9342 	zoneid_t zoneid;
9343 	ip_stack_t	*ipst;
9344 
9345 	ip1dbg(("ip_sioctl_tonlink"));
9346 
9347 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9348 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9349 	ipst = CONNQ_TO_IPST(q);
9350 
9351 	/* Existence verified in ip_wput_nondata */
9352 	mp1 = mp->b_cont->b_cont;
9353 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9354 	sin = (sin_t *)&sia->sa_addr;
9355 
9356 	/*
9357 	 * Match addresses with a zero gateway field to avoid
9358 	 * routes going through a router.
9359 	 * Exclude broadcast and multicast addresses.
9360 	 */
9361 	switch (sin->sin_family) {
9362 	case AF_INET6: {
9363 		sin6_t *sin6 = (sin6_t *)sin;
9364 
9365 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9366 			ipaddr_t v4_addr;
9367 
9368 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9369 			    v4_addr);
9370 			if (!CLASSD(v4_addr)) {
9371 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9372 				    NULL, NULL, zoneid, NULL,
9373 				    MATCH_IRE_GW, ipst);
9374 			}
9375 		} else {
9376 			in6_addr_t v6addr;
9377 			in6_addr_t v6gw;
9378 
9379 			v6addr = sin6->sin6_addr;
9380 			v6gw = ipv6_all_zeros;
9381 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9382 				ire = ire_route_lookup_v6(&v6addr, 0,
9383 				    &v6gw, 0, NULL, NULL, zoneid,
9384 				    NULL, MATCH_IRE_GW, ipst);
9385 			}
9386 		}
9387 		break;
9388 	}
9389 	case AF_INET: {
9390 		ipaddr_t v4addr;
9391 
9392 		v4addr = sin->sin_addr.s_addr;
9393 		if (!CLASSD(v4addr)) {
9394 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9395 			    NULL, NULL, zoneid, NULL,
9396 			    MATCH_IRE_GW, ipst);
9397 		}
9398 		break;
9399 	}
9400 	default:
9401 		return (EAFNOSUPPORT);
9402 	}
9403 	sia->sa_res = 0;
9404 	if (ire != NULL) {
9405 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9406 		    IRE_LOCAL|IRE_LOOPBACK)) {
9407 			sia->sa_res = 1;
9408 		}
9409 		ire_refrele(ire);
9410 	}
9411 	return (0);
9412 }
9413 
9414 /*
9415  * TBD: implement when kernel maintaines a list of site prefixes.
9416  */
9417 /* ARGSUSED */
9418 int
9419 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9420     ip_ioctl_cmd_t *ipip, void *ifreq)
9421 {
9422 	return (ENXIO);
9423 }
9424 
9425 /* ARGSUSED */
9426 int
9427 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9428     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9429 {
9430 	ill_t  		*ill;
9431 	mblk_t		*mp1;
9432 	conn_t		*connp;
9433 	boolean_t	success;
9434 
9435 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9436 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9437 	/* ioctl comes down on an conn */
9438 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9439 	connp = Q_TO_CONN(q);
9440 
9441 	mp->b_datap->db_type = M_IOCTL;
9442 
9443 	/*
9444 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9445 	 * The original mp contains contaminated b_next values due to 'mi',
9446 	 * which is needed to do the mi_copy_done. Unfortunately if we
9447 	 * send down the original mblk itself and if we are popped due to an
9448 	 * an unplumb before the response comes back from tunnel,
9449 	 * the streamhead (which does a freemsg) will see this contaminated
9450 	 * message and the assertion in freemsg about non-null b_next/b_prev
9451 	 * will panic a DEBUG kernel.
9452 	 */
9453 	mp1 = copymsg(mp);
9454 	if (mp1 == NULL)
9455 		return (ENOMEM);
9456 
9457 	ill = ipif->ipif_ill;
9458 	mutex_enter(&connp->conn_lock);
9459 	mutex_enter(&ill->ill_lock);
9460 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9461 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9462 		    mp, 0);
9463 	} else {
9464 		success = ill_pending_mp_add(ill, connp, mp);
9465 	}
9466 	mutex_exit(&ill->ill_lock);
9467 	mutex_exit(&connp->conn_lock);
9468 
9469 	if (success) {
9470 		ip1dbg(("sending down tunparam request "));
9471 		putnext(ill->ill_wq, mp1);
9472 		return (EINPROGRESS);
9473 	} else {
9474 		/* The conn has started closing */
9475 		freemsg(mp1);
9476 		return (EINTR);
9477 	}
9478 }
9479 
9480 /*
9481  * ARP IOCTLs.
9482  * How does IP get in the business of fronting ARP configuration/queries?
9483  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9484  * are by tradition passed in through a datagram socket.  That lands in IP.
9485  * As it happens, this is just as well since the interface is quite crude in
9486  * that it passes in no information about protocol or hardware types, or
9487  * interface association.  After making the protocol assumption, IP is in
9488  * the position to look up the name of the ILL, which ARP will need, and
9489  * format a request that can be handled by ARP.  The request is passed up
9490  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9491  * back a response.  ARP supports its own set of more general IOCTLs, in
9492  * case anyone is interested.
9493  */
9494 /* ARGSUSED */
9495 int
9496 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9497     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9498 {
9499 	mblk_t *mp1;
9500 	mblk_t *mp2;
9501 	mblk_t *pending_mp;
9502 	ipaddr_t ipaddr;
9503 	area_t *area;
9504 	struct iocblk *iocp;
9505 	conn_t *connp;
9506 	struct arpreq *ar;
9507 	struct xarpreq *xar;
9508 	int flags, alength;
9509 	char *lladdr;
9510 	ip_stack_t	*ipst;
9511 	ill_t *ill = ipif->ipif_ill;
9512 	boolean_t if_arp_ioctl = B_FALSE;
9513 
9514 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9515 	connp = Q_TO_CONN(q);
9516 	ipst = connp->conn_netstack->netstack_ip;
9517 
9518 	if (ipip->ipi_cmd_type == XARP_CMD) {
9519 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9520 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9521 		ar = NULL;
9522 
9523 		flags = xar->xarp_flags;
9524 		lladdr = LLADDR(&xar->xarp_ha);
9525 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9526 		/*
9527 		 * Validate against user's link layer address length
9528 		 * input and name and addr length limits.
9529 		 */
9530 		alength = ill->ill_phys_addr_length;
9531 		if (ipip->ipi_cmd == SIOCSXARP) {
9532 			if (alength != xar->xarp_ha.sdl_alen ||
9533 			    (alength + xar->xarp_ha.sdl_nlen >
9534 			    sizeof (xar->xarp_ha.sdl_data)))
9535 				return (EINVAL);
9536 		}
9537 	} else {
9538 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9539 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9540 		xar = NULL;
9541 
9542 		flags = ar->arp_flags;
9543 		lladdr = ar->arp_ha.sa_data;
9544 		/*
9545 		 * Theoretically, the sa_family could tell us what link
9546 		 * layer type this operation is trying to deal with. By
9547 		 * common usage AF_UNSPEC means ethernet. We'll assume
9548 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9549 		 * for now. Our new SIOC*XARP ioctls can be used more
9550 		 * generally.
9551 		 *
9552 		 * If the underlying media happens to have a non 6 byte
9553 		 * address, arp module will fail set/get, but the del
9554 		 * operation will succeed.
9555 		 */
9556 		alength = 6;
9557 		if ((ipip->ipi_cmd != SIOCDARP) &&
9558 		    (alength != ill->ill_phys_addr_length)) {
9559 			return (EINVAL);
9560 		}
9561 	}
9562 
9563 	/*
9564 	 * We are going to pass up to ARP a packet chain that looks
9565 	 * like:
9566 	 *
9567 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9568 	 *
9569 	 * Get a copy of the original IOCTL mblk to head the chain,
9570 	 * to be sent up (in mp1). Also get another copy to store
9571 	 * in the ill_pending_mp list, for matching the response
9572 	 * when it comes back from ARP.
9573 	 */
9574 	mp1 = copyb(mp);
9575 	pending_mp = copymsg(mp);
9576 	if (mp1 == NULL || pending_mp == NULL) {
9577 		if (mp1 != NULL)
9578 			freeb(mp1);
9579 		if (pending_mp != NULL)
9580 			inet_freemsg(pending_mp);
9581 		return (ENOMEM);
9582 	}
9583 
9584 	ipaddr = sin->sin_addr.s_addr;
9585 
9586 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9587 	    (caddr_t)&ipaddr);
9588 	if (mp2 == NULL) {
9589 		freeb(mp1);
9590 		inet_freemsg(pending_mp);
9591 		return (ENOMEM);
9592 	}
9593 	/* Put together the chain. */
9594 	mp1->b_cont = mp2;
9595 	mp1->b_datap->db_type = M_IOCTL;
9596 	mp2->b_cont = mp;
9597 	mp2->b_datap->db_type = M_DATA;
9598 
9599 	iocp = (struct iocblk *)mp1->b_rptr;
9600 
9601 	/*
9602 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9603 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9604 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9605 	 * ioc_count field; set ioc_count to be correct.
9606 	 */
9607 	iocp->ioc_count = MBLKL(mp1->b_cont);
9608 
9609 	/*
9610 	 * Set the proper command in the ARP message.
9611 	 * Convert the SIOC{G|S|D}ARP calls into our
9612 	 * AR_ENTRY_xxx calls.
9613 	 */
9614 	area = (area_t *)mp2->b_rptr;
9615 	switch (iocp->ioc_cmd) {
9616 	case SIOCDARP:
9617 	case SIOCDXARP:
9618 		/*
9619 		 * We defer deleting the corresponding IRE until
9620 		 * we return from arp.
9621 		 */
9622 		area->area_cmd = AR_ENTRY_DELETE;
9623 		area->area_proto_mask_offset = 0;
9624 		break;
9625 	case SIOCGARP:
9626 	case SIOCGXARP:
9627 		area->area_cmd = AR_ENTRY_SQUERY;
9628 		area->area_proto_mask_offset = 0;
9629 		break;
9630 	case SIOCSARP:
9631 	case SIOCSXARP:
9632 		/*
9633 		 * Delete the corresponding ire to make sure IP will
9634 		 * pick up any change from arp.
9635 		 */
9636 		if (!if_arp_ioctl) {
9637 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9638 		} else {
9639 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9640 			if (ipif != NULL) {
9641 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9642 				    ipst);
9643 				ipif_refrele(ipif);
9644 			}
9645 		}
9646 		break;
9647 	}
9648 	iocp->ioc_cmd = area->area_cmd;
9649 
9650 	/*
9651 	 * Fill in the rest of the ARP operation fields.
9652 	 */
9653 	area->area_hw_addr_length = alength;
9654 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9655 
9656 	/* Translate the flags. */
9657 	if (flags & ATF_PERM)
9658 		area->area_flags |= ACE_F_PERMANENT;
9659 	if (flags & ATF_PUBL)
9660 		area->area_flags |= ACE_F_PUBLISH;
9661 	if (flags & ATF_AUTHORITY)
9662 		area->area_flags |= ACE_F_AUTHORITY;
9663 
9664 	/*
9665 	 * Before sending 'mp' to ARP, we have to clear the b_next
9666 	 * and b_prev. Otherwise if STREAMS encounters such a message
9667 	 * in freemsg(), (because ARP can close any time) it can cause
9668 	 * a panic. But mi code needs the b_next and b_prev values of
9669 	 * mp->b_cont, to complete the ioctl. So we store it here
9670 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9671 	 * when the response comes down from ARP.
9672 	 */
9673 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9674 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9675 	mp->b_cont->b_next = NULL;
9676 	mp->b_cont->b_prev = NULL;
9677 
9678 	mutex_enter(&connp->conn_lock);
9679 	mutex_enter(&ill->ill_lock);
9680 	/* conn has not yet started closing, hence this can't fail */
9681 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9682 	mutex_exit(&ill->ill_lock);
9683 	mutex_exit(&connp->conn_lock);
9684 
9685 	/*
9686 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9687 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9688 	 */
9689 	putnext(ill->ill_rq, mp1);
9690 	return (EINPROGRESS);
9691 }
9692 
9693 /*
9694  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9695  * the associated sin and refhold and return the associated ipif via `ci'.
9696  */
9697 int
9698 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9699     cmd_info_t *ci, ipsq_func_t func)
9700 {
9701 	mblk_t	*mp1;
9702 	int	err;
9703 	sin_t	*sin;
9704 	conn_t	*connp;
9705 	ipif_t	*ipif;
9706 	ire_t	*ire = NULL;
9707 	ill_t	*ill = NULL;
9708 	boolean_t exists;
9709 	ip_stack_t *ipst;
9710 	struct arpreq *ar;
9711 	struct xarpreq *xar;
9712 	struct sockaddr_dl *sdl;
9713 
9714 	/* ioctl comes down on a conn */
9715 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9716 	connp = Q_TO_CONN(q);
9717 	if (connp->conn_af_isv6)
9718 		return (ENXIO);
9719 
9720 	ipst = connp->conn_netstack->netstack_ip;
9721 
9722 	/* Verified in ip_wput_nondata */
9723 	mp1 = mp->b_cont->b_cont;
9724 
9725 	if (ipip->ipi_cmd_type == XARP_CMD) {
9726 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9727 		xar = (struct xarpreq *)mp1->b_rptr;
9728 		sin = (sin_t *)&xar->xarp_pa;
9729 		sdl = &xar->xarp_ha;
9730 
9731 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9732 			return (ENXIO);
9733 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9734 			return (EINVAL);
9735 	} else {
9736 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9737 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9738 		ar = (struct arpreq *)mp1->b_rptr;
9739 		sin = (sin_t *)&ar->arp_pa;
9740 	}
9741 
9742 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9743 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9744 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9745 		    mp, func, &err, ipst);
9746 		if (ipif == NULL)
9747 			return (err);
9748 		if (ipif->ipif_id != 0 ||
9749 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9750 			ipif_refrele(ipif);
9751 			return (ENXIO);
9752 		}
9753 	} else {
9754 		/*
9755 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9756 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9757 		 * case, a simple forwarding table lookup will return the
9758 		 * IRE_IF_RESOLVER for the first interface in the group, which
9759 		 * might not be the interface on which the requested IP
9760 		 * address was resolved due to the ill selection algorithm
9761 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9762 		 * lookup first: if the IRE cache entry for the IP address is
9763 		 * still there, it will contain the ill pointer for the right
9764 		 * interface, so we use that. If the cache entry has been
9765 		 * flushed, we fall back to the forwarding table lookup. This
9766 		 * should be rare enough since IRE cache entries have a longer
9767 		 * life expectancy than ARP cache entries.
9768 		 */
9769 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9770 		    ipst);
9771 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9772 		    ((ill = ire_to_ill(ire)) == NULL) ||
9773 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9774 			if (ire != NULL)
9775 				ire_refrele(ire);
9776 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9777 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9778 			    NULL, MATCH_IRE_TYPE, ipst);
9779 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9780 
9781 				if (ire != NULL)
9782 					ire_refrele(ire);
9783 				return (ENXIO);
9784 			}
9785 		}
9786 		ASSERT(ire != NULL && ill != NULL);
9787 		ipif = ill->ill_ipif;
9788 		ipif_refhold(ipif);
9789 		ire_refrele(ire);
9790 	}
9791 	ci->ci_sin = sin;
9792 	ci->ci_ipif = ipif;
9793 	return (0);
9794 }
9795 
9796 /*
9797  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9798  * atomically set/clear the muxids. Also complete the ioctl by acking or
9799  * naking it.  Note that the code is structured such that the link type,
9800  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9801  * its clones use the persistent link, while pppd(1M) and perhaps many
9802  * other daemons may use non-persistent link.  When combined with some
9803  * ill_t states, linking and unlinking lower streams may be used as
9804  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9805  */
9806 /* ARGSUSED */
9807 void
9808 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9809 {
9810 	mblk_t		*mp1, *mp2;
9811 	struct linkblk	*li;
9812 	struct ipmx_s	*ipmxp;
9813 	ill_t		*ill;
9814 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9815 	int		err = 0;
9816 	boolean_t	entered_ipsq = B_FALSE;
9817 	boolean_t	islink;
9818 	ip_stack_t	*ipst;
9819 
9820 	if (CONN_Q(q))
9821 		ipst = CONNQ_TO_IPST(q);
9822 	else
9823 		ipst = ILLQ_TO_IPST(q);
9824 
9825 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9826 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9827 
9828 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9829 
9830 	mp1 = mp->b_cont;	/* This is the linkblk info */
9831 	li = (struct linkblk *)mp1->b_rptr;
9832 
9833 	/*
9834 	 * ARP has added this special mblk, and the utility is asking us
9835 	 * to perform consistency checks, and also atomically set the
9836 	 * muxid. Ifconfig is an example.  It achieves this by using
9837 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9838 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9839 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9840 	 * and other comments in this routine for more details.
9841 	 */
9842 	mp2 = mp1->b_cont;	/* This is added by ARP */
9843 
9844 	/*
9845 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9846 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9847 	 * get the special mblk above.  For backward compatibility, we
9848 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9849 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9850 	 * not atomic, and can leave the streams unplumbable if the utility
9851 	 * is interrupted before it does the SIOCSLIFMUXID.
9852 	 */
9853 	if (mp2 == NULL) {
9854 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9855 		if (err == EINPROGRESS)
9856 			return;
9857 		goto done;
9858 	}
9859 
9860 	/*
9861 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9862 	 * ARP has appended this last mblk to tell us whether the lower stream
9863 	 * is an arp-dev stream or an IP module stream.
9864 	 */
9865 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9866 	if (ipmxp->ipmx_arpdev_stream) {
9867 		/*
9868 		 * The lower stream is the arp-dev stream.
9869 		 */
9870 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9871 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9872 		if (ill == NULL) {
9873 			if (err == EINPROGRESS)
9874 				return;
9875 			err = EINVAL;
9876 			goto done;
9877 		}
9878 
9879 		if (ipsq == NULL) {
9880 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9881 			    NEW_OP, B_TRUE);
9882 			if (ipsq == NULL) {
9883 				ill_refrele(ill);
9884 				return;
9885 			}
9886 			entered_ipsq = B_TRUE;
9887 		}
9888 		ASSERT(IAM_WRITER_ILL(ill));
9889 		ill_refrele(ill);
9890 
9891 		/*
9892 		 * To ensure consistency between IP and ARP, the following
9893 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9894 		 * This is because the muxid's are stored in the IP stream on
9895 		 * the ill.
9896 		 *
9897 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9898 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9899 		 * not yet plinked, and it also checks that the corresponding
9900 		 * IP stream is already plinked.
9901 		 *
9902 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9903 		 * punlinking the IP stream. IP does not allow punlink of the
9904 		 * IP stream unless the arp stream has been punlinked.
9905 		 */
9906 		if ((islink &&
9907 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9908 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9909 			err = EINVAL;
9910 			goto done;
9911 		}
9912 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9913 	} else {
9914 		/*
9915 		 * The lower stream is probably an IP module stream.  Do
9916 		 * consistency checking.
9917 		 */
9918 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9919 		if (err == EINPROGRESS)
9920 			return;
9921 	}
9922 done:
9923 	if (err == 0)
9924 		miocack(q, mp, 0, 0);
9925 	else
9926 		miocnak(q, mp, 0, err);
9927 
9928 	/* Conn was refheld in ip_sioctl_copyin_setup */
9929 	if (CONN_Q(q))
9930 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9931 	if (entered_ipsq)
9932 		ipsq_exit(ipsq);
9933 }
9934 
9935 /*
9936  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9937  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9938  * module stream).  If `doconsist' is set, then do the extended consistency
9939  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9940  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9941  * an error code on failure.
9942  */
9943 static int
9944 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9945     struct linkblk *li, boolean_t doconsist)
9946 {
9947 	ill_t  		*ill;
9948 	queue_t		*ipwq, *dwq;
9949 	const char	*name;
9950 	struct qinit	*qinfo;
9951 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9952 	boolean_t	entered_ipsq = B_FALSE;
9953 
9954 	/*
9955 	 * Walk the lower stream to verify it's the IP module stream.
9956 	 * The IP module is identified by its name, wput function,
9957 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9958 	 * (li->l_qbot) will not vanish until this ioctl completes.
9959 	 */
9960 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9961 		qinfo = ipwq->q_qinfo;
9962 		name = qinfo->qi_minfo->mi_idname;
9963 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9964 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9965 			break;
9966 		}
9967 	}
9968 
9969 	/*
9970 	 * If this isn't an IP module stream, bail.
9971 	 */
9972 	if (ipwq == NULL)
9973 		return (0);
9974 
9975 	ill = ipwq->q_ptr;
9976 	ASSERT(ill != NULL);
9977 
9978 	if (ipsq == NULL) {
9979 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9980 		    NEW_OP, B_TRUE);
9981 		if (ipsq == NULL)
9982 			return (EINPROGRESS);
9983 		entered_ipsq = B_TRUE;
9984 	}
9985 	ASSERT(IAM_WRITER_ILL(ill));
9986 
9987 	if (doconsist) {
9988 		/*
9989 		 * Consistency checking requires that I_{P}LINK occurs
9990 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9991 		 * occurs prior to clearing ill_arp_muxid.
9992 		 */
9993 		if ((islink && ill->ill_ip_muxid != 0) ||
9994 		    (!islink && ill->ill_arp_muxid != 0)) {
9995 			if (entered_ipsq)
9996 				ipsq_exit(ipsq);
9997 			return (EINVAL);
9998 		}
9999 	}
10000 
10001 	/*
10002 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10003 	 * the read queue of the module immediately below IP in the ill.
10004 	 * These are used during the capability negotiation below.
10005 	 */
10006 	ill->ill_lmod_rq = NULL;
10007 	ill->ill_lmod_cnt = 0;
10008 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10009 		ill->ill_lmod_rq = RD(dwq);
10010 		for (; dwq != NULL; dwq = dwq->q_next)
10011 			ill->ill_lmod_cnt++;
10012 	}
10013 
10014 	if (doconsist)
10015 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10016 
10017 	/*
10018 	 * If there's at least one up ipif on this ill, then we're bound to
10019 	 * the underlying driver via DLPI.  In that case, renegotiate
10020 	 * capabilities to account for any possible change in modules
10021 	 * interposed between IP and the driver.
10022 	 */
10023 	if (ill->ill_ipif_up_count > 0) {
10024 		if (islink)
10025 			ill_capability_probe(ill);
10026 		else
10027 			ill_capability_reset(ill);
10028 	}
10029 
10030 	if (entered_ipsq)
10031 		ipsq_exit(ipsq);
10032 
10033 	return (0);
10034 }
10035 
10036 /*
10037  * Search the ioctl command in the ioctl tables and return a pointer
10038  * to the ioctl command information. The ioctl command tables are
10039  * static and fully populated at compile time.
10040  */
10041 ip_ioctl_cmd_t *
10042 ip_sioctl_lookup(int ioc_cmd)
10043 {
10044 	int index;
10045 	ip_ioctl_cmd_t *ipip;
10046 	ip_ioctl_cmd_t *ipip_end;
10047 
10048 	if (ioc_cmd == IPI_DONTCARE)
10049 		return (NULL);
10050 
10051 	/*
10052 	 * Do a 2 step search. First search the indexed table
10053 	 * based on the least significant byte of the ioctl cmd.
10054 	 * If we don't find a match, then search the misc table
10055 	 * serially.
10056 	 */
10057 	index = ioc_cmd & 0xFF;
10058 	if (index < ip_ndx_ioctl_count) {
10059 		ipip = &ip_ndx_ioctl_table[index];
10060 		if (ipip->ipi_cmd == ioc_cmd) {
10061 			/* Found a match in the ndx table */
10062 			return (ipip);
10063 		}
10064 	}
10065 
10066 	/* Search the misc table */
10067 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10068 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10069 		if (ipip->ipi_cmd == ioc_cmd)
10070 			/* Found a match in the misc table */
10071 			return (ipip);
10072 	}
10073 
10074 	return (NULL);
10075 }
10076 
10077 /*
10078  * Wrapper function for resuming deferred ioctl processing
10079  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10080  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10081  */
10082 /* ARGSUSED */
10083 void
10084 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10085     void *dummy_arg)
10086 {
10087 	ip_sioctl_copyin_setup(q, mp);
10088 }
10089 
10090 /*
10091  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10092  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10093  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10094  * We establish here the size of the block to be copied in.  mi_copyin
10095  * arranges for this to happen, an processing continues in ip_wput with
10096  * an M_IOCDATA message.
10097  */
10098 void
10099 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10100 {
10101 	int	copyin_size;
10102 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10103 	ip_ioctl_cmd_t *ipip;
10104 	cred_t *cr;
10105 	ip_stack_t	*ipst;
10106 
10107 	if (CONN_Q(q))
10108 		ipst = CONNQ_TO_IPST(q);
10109 	else
10110 		ipst = ILLQ_TO_IPST(q);
10111 
10112 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10113 	if (ipip == NULL) {
10114 		/*
10115 		 * The ioctl is not one we understand or own.
10116 		 * Pass it along to be processed down stream,
10117 		 * if this is a module instance of IP, else nak
10118 		 * the ioctl.
10119 		 */
10120 		if (q->q_next == NULL) {
10121 			goto nak;
10122 		} else {
10123 			putnext(q, mp);
10124 			return;
10125 		}
10126 	}
10127 
10128 	/*
10129 	 * If this is deferred, then we will do all the checks when we
10130 	 * come back.
10131 	 */
10132 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10133 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10134 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10135 		return;
10136 	}
10137 
10138 	/*
10139 	 * Only allow a very small subset of IP ioctls on this stream if
10140 	 * IP is a module and not a driver. Allowing ioctls to be processed
10141 	 * in this case may cause assert failures or data corruption.
10142 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10143 	 * ioctls allowed on an IP module stream, after which this stream
10144 	 * normally becomes a multiplexor (at which time the stream head
10145 	 * will fail all ioctls).
10146 	 */
10147 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10148 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10149 			/*
10150 			 * Pass common Streams ioctls which the IP
10151 			 * module does not own or consume along to
10152 			 * be processed down stream.
10153 			 */
10154 			putnext(q, mp);
10155 			return;
10156 		} else {
10157 			goto nak;
10158 		}
10159 	}
10160 
10161 	/* Make sure we have ioctl data to process. */
10162 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10163 		goto nak;
10164 
10165 	/*
10166 	 * Prefer dblk credential over ioctl credential; some synthesized
10167 	 * ioctls have kcred set because there's no way to crhold()
10168 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10169 	 * the framework; the caller of ioctl needs to hold the reference
10170 	 * for the duration of the call).
10171 	 */
10172 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10173 
10174 	/* Make sure normal users don't send down privileged ioctls */
10175 	if ((ipip->ipi_flags & IPI_PRIV) &&
10176 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10177 		/* We checked the privilege earlier but log it here */
10178 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10179 		return;
10180 	}
10181 
10182 	/*
10183 	 * The ioctl command tables can only encode fixed length
10184 	 * ioctl data. If the length is variable, the table will
10185 	 * encode the length as zero. Such special cases are handled
10186 	 * below in the switch.
10187 	 */
10188 	if (ipip->ipi_copyin_size != 0) {
10189 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10190 		return;
10191 	}
10192 
10193 	switch (iocp->ioc_cmd) {
10194 	case O_SIOCGIFCONF:
10195 	case SIOCGIFCONF:
10196 		/*
10197 		 * This IOCTL is hilarious.  See comments in
10198 		 * ip_sioctl_get_ifconf for the story.
10199 		 */
10200 		if (iocp->ioc_count == TRANSPARENT)
10201 			copyin_size = SIZEOF_STRUCT(ifconf,
10202 			    iocp->ioc_flag);
10203 		else
10204 			copyin_size = iocp->ioc_count;
10205 		mi_copyin(q, mp, NULL, copyin_size);
10206 		return;
10207 
10208 	case O_SIOCGLIFCONF:
10209 	case SIOCGLIFCONF:
10210 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10211 		mi_copyin(q, mp, NULL, copyin_size);
10212 		return;
10213 
10214 	case SIOCGLIFSRCOF:
10215 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10216 		mi_copyin(q, mp, NULL, copyin_size);
10217 		return;
10218 	case SIOCGIP6ADDRPOLICY:
10219 		ip_sioctl_ip6addrpolicy(q, mp);
10220 		ip6_asp_table_refrele(ipst);
10221 		return;
10222 
10223 	case SIOCSIP6ADDRPOLICY:
10224 		ip_sioctl_ip6addrpolicy(q, mp);
10225 		return;
10226 
10227 	case SIOCGDSTINFO:
10228 		ip_sioctl_dstinfo(q, mp);
10229 		ip6_asp_table_refrele(ipst);
10230 		return;
10231 
10232 	case I_PLINK:
10233 	case I_PUNLINK:
10234 	case I_LINK:
10235 	case I_UNLINK:
10236 		/*
10237 		 * We treat non-persistent link similarly as the persistent
10238 		 * link case, in terms of plumbing/unplumbing, as well as
10239 		 * dynamic re-plumbing events indicator.  See comments
10240 		 * in ip_sioctl_plink() for more.
10241 		 *
10242 		 * Request can be enqueued in the 'ipsq' while waiting
10243 		 * to become exclusive. So bump up the conn ref.
10244 		 */
10245 		if (CONN_Q(q))
10246 			CONN_INC_REF(Q_TO_CONN(q));
10247 		ip_sioctl_plink(NULL, q, mp, NULL);
10248 		return;
10249 
10250 	case ND_GET:
10251 	case ND_SET:
10252 		/*
10253 		 * Use of the nd table requires holding the reader lock.
10254 		 * Modifying the nd table thru nd_load/nd_unload requires
10255 		 * the writer lock.
10256 		 */
10257 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10258 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10259 			rw_exit(&ipst->ips_ip_g_nd_lock);
10260 
10261 			if (iocp->ioc_error)
10262 				iocp->ioc_count = 0;
10263 			mp->b_datap->db_type = M_IOCACK;
10264 			qreply(q, mp);
10265 			return;
10266 		}
10267 		rw_exit(&ipst->ips_ip_g_nd_lock);
10268 		/*
10269 		 * We don't understand this subioctl of ND_GET / ND_SET.
10270 		 * Maybe intended for some driver / module below us
10271 		 */
10272 		if (q->q_next) {
10273 			putnext(q, mp);
10274 		} else {
10275 			iocp->ioc_error = ENOENT;
10276 			mp->b_datap->db_type = M_IOCNAK;
10277 			iocp->ioc_count = 0;
10278 			qreply(q, mp);
10279 		}
10280 		return;
10281 
10282 	case IP_IOCTL:
10283 		ip_wput_ioctl(q, mp);
10284 		return;
10285 	default:
10286 		cmn_err(CE_PANIC, "should not happen ");
10287 	}
10288 nak:
10289 	if (mp->b_cont != NULL) {
10290 		freemsg(mp->b_cont);
10291 		mp->b_cont = NULL;
10292 	}
10293 	iocp->ioc_error = EINVAL;
10294 	mp->b_datap->db_type = M_IOCNAK;
10295 	iocp->ioc_count = 0;
10296 	qreply(q, mp);
10297 }
10298 
10299 /* ip_wput hands off ARP IOCTL responses to us */
10300 void
10301 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10302 {
10303 	struct arpreq *ar;
10304 	struct xarpreq *xar;
10305 	area_t	*area;
10306 	mblk_t	*area_mp;
10307 	struct iocblk *iocp;
10308 	mblk_t	*orig_ioc_mp, *tmp;
10309 	struct iocblk	*orig_iocp;
10310 	ill_t *ill;
10311 	conn_t *connp = NULL;
10312 	uint_t ioc_id;
10313 	mblk_t *pending_mp;
10314 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10315 	int *flagsp;
10316 	char *storage = NULL;
10317 	sin_t *sin;
10318 	ipaddr_t addr;
10319 	int err;
10320 	ip_stack_t *ipst;
10321 
10322 	ill = q->q_ptr;
10323 	ASSERT(ill != NULL);
10324 	ipst = ill->ill_ipst;
10325 
10326 	/*
10327 	 * We should get back from ARP a packet chain that looks like:
10328 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10329 	 */
10330 	if (!(area_mp = mp->b_cont) ||
10331 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10332 	    !(orig_ioc_mp = area_mp->b_cont) ||
10333 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10334 		freemsg(mp);
10335 		return;
10336 	}
10337 
10338 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10339 
10340 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10341 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10342 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10343 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10344 		x_arp_ioctl = B_TRUE;
10345 		xar = (struct xarpreq *)tmp->b_rptr;
10346 		sin = (sin_t *)&xar->xarp_pa;
10347 		flagsp = &xar->xarp_flags;
10348 		storage = xar->xarp_ha.sdl_data;
10349 		if (xar->xarp_ha.sdl_nlen != 0)
10350 			ifx_arp_ioctl = B_TRUE;
10351 	} else {
10352 		ar = (struct arpreq *)tmp->b_rptr;
10353 		sin = (sin_t *)&ar->arp_pa;
10354 		flagsp = &ar->arp_flags;
10355 		storage = ar->arp_ha.sa_data;
10356 	}
10357 
10358 	iocp = (struct iocblk *)mp->b_rptr;
10359 
10360 	/*
10361 	 * Pick out the originating queue based on the ioc_id.
10362 	 */
10363 	ioc_id = iocp->ioc_id;
10364 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10365 	if (pending_mp == NULL) {
10366 		ASSERT(connp == NULL);
10367 		inet_freemsg(mp);
10368 		return;
10369 	}
10370 	ASSERT(connp != NULL);
10371 	q = CONNP_TO_WQ(connp);
10372 
10373 	/* Uncouple the internally generated IOCTL from the original one */
10374 	area = (area_t *)area_mp->b_rptr;
10375 	area_mp->b_cont = NULL;
10376 
10377 	/*
10378 	 * Restore the b_next and b_prev used by mi code. This is needed
10379 	 * to complete the ioctl using mi* functions. We stored them in
10380 	 * the pending mp prior to sending the request to ARP.
10381 	 */
10382 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10383 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10384 	inet_freemsg(pending_mp);
10385 
10386 	/*
10387 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10388 	 * Catch the case where there is an IRE_CACHE by no entry in the
10389 	 * arp table.
10390 	 */
10391 	addr = sin->sin_addr.s_addr;
10392 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10393 		ire_t			*ire;
10394 		dl_unitdata_req_t	*dlup;
10395 		mblk_t			*llmp;
10396 		int			addr_len;
10397 		ill_t			*ipsqill = NULL;
10398 
10399 		if (ifx_arp_ioctl) {
10400 			/*
10401 			 * There's no need to lookup the ill, since
10402 			 * we've already done that when we started
10403 			 * processing the ioctl and sent the message
10404 			 * to ARP on that ill.  So use the ill that
10405 			 * is stored in q->q_ptr.
10406 			 */
10407 			ipsqill = ill;
10408 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10409 			    ipsqill->ill_ipif, ALL_ZONES,
10410 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10411 		} else {
10412 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10413 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10414 			if (ire != NULL)
10415 				ipsqill = ire_to_ill(ire);
10416 		}
10417 
10418 		if ((x_arp_ioctl) && (ipsqill != NULL))
10419 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10420 
10421 		if (ire != NULL) {
10422 			/*
10423 			 * Since the ire obtained from cachetable is used for
10424 			 * mac addr copying below, treat an incomplete ire as if
10425 			 * as if we never found it.
10426 			 */
10427 			if (ire->ire_nce != NULL &&
10428 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10429 				ire_refrele(ire);
10430 				ire = NULL;
10431 				ipsqill = NULL;
10432 				goto errack;
10433 			}
10434 			*flagsp = ATF_INUSE;
10435 			llmp = (ire->ire_nce != NULL ?
10436 			    ire->ire_nce->nce_res_mp : NULL);
10437 			if (llmp != NULL && ipsqill != NULL) {
10438 				uchar_t *macaddr;
10439 
10440 				addr_len = ipsqill->ill_phys_addr_length;
10441 				if (x_arp_ioctl && ((addr_len +
10442 				    ipsqill->ill_name_length) >
10443 				    sizeof (xar->xarp_ha.sdl_data))) {
10444 					ire_refrele(ire);
10445 					freemsg(mp);
10446 					ip_ioctl_finish(q, orig_ioc_mp,
10447 					    EINVAL, NO_COPYOUT, NULL);
10448 					return;
10449 				}
10450 				*flagsp |= ATF_COM;
10451 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10452 				if (ipsqill->ill_sap_length < 0)
10453 					macaddr = llmp->b_rptr +
10454 					    dlup->dl_dest_addr_offset;
10455 				else
10456 					macaddr = llmp->b_rptr +
10457 					    dlup->dl_dest_addr_offset +
10458 					    ipsqill->ill_sap_length;
10459 				/*
10460 				 * For SIOCGARP, MAC address length
10461 				 * validation has already been done
10462 				 * before the ioctl was issued to ARP to
10463 				 * allow it to progress only on 6 byte
10464 				 * addressable (ethernet like) media. Thus
10465 				 * the mac address copying can not overwrite
10466 				 * the sa_data area below.
10467 				 */
10468 				bcopy(macaddr, storage, addr_len);
10469 			}
10470 			/* Ditch the internal IOCTL. */
10471 			freemsg(mp);
10472 			ire_refrele(ire);
10473 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10474 			return;
10475 		}
10476 	}
10477 
10478 	/*
10479 	 * Delete the coresponding IRE_CACHE if any.
10480 	 * Reset the error if there was one (in case there was no entry
10481 	 * in arp.)
10482 	 */
10483 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10484 		ipif_t *ipintf = NULL;
10485 
10486 		if (ifx_arp_ioctl) {
10487 			/*
10488 			 * There's no need to lookup the ill, since
10489 			 * we've already done that when we started
10490 			 * processing the ioctl and sent the message
10491 			 * to ARP on that ill.  So use the ill that
10492 			 * is stored in q->q_ptr.
10493 			 */
10494 			ipintf = ill->ill_ipif;
10495 		}
10496 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10497 			/*
10498 			 * The address in "addr" may be an entry for a
10499 			 * router. If that's true, then any off-net
10500 			 * IRE_CACHE entries that go through the router
10501 			 * with address "addr" must be clobbered. Use
10502 			 * ire_walk to achieve this goal.
10503 			 */
10504 			if (ifx_arp_ioctl)
10505 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10506 				    ire_delete_cache_gw, (char *)&addr, ill);
10507 			else
10508 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10509 				    ALL_ZONES, ipst);
10510 			iocp->ioc_error = 0;
10511 		}
10512 	}
10513 errack:
10514 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10515 		err = iocp->ioc_error;
10516 		freemsg(mp);
10517 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10518 		return;
10519 	}
10520 
10521 	/*
10522 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10523 	 * the area_t into the struct {x}arpreq.
10524 	 */
10525 	if (x_arp_ioctl) {
10526 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10527 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10528 		    sizeof (xar->xarp_ha.sdl_data)) {
10529 			freemsg(mp);
10530 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10531 			    NULL);
10532 			return;
10533 		}
10534 	}
10535 	*flagsp = ATF_INUSE;
10536 	if (area->area_flags & ACE_F_PERMANENT)
10537 		*flagsp |= ATF_PERM;
10538 	if (area->area_flags & ACE_F_PUBLISH)
10539 		*flagsp |= ATF_PUBL;
10540 	if (area->area_flags & ACE_F_AUTHORITY)
10541 		*flagsp |= ATF_AUTHORITY;
10542 	if (area->area_hw_addr_length != 0) {
10543 		*flagsp |= ATF_COM;
10544 		/*
10545 		 * For SIOCGARP, MAC address length validation has
10546 		 * already been done before the ioctl was issued to ARP
10547 		 * to allow it to progress only on 6 byte addressable
10548 		 * (ethernet like) media. Thus the mac address copying
10549 		 * can not overwrite the sa_data area below.
10550 		 */
10551 		bcopy((char *)area + area->area_hw_addr_offset,
10552 		    storage, area->area_hw_addr_length);
10553 	}
10554 
10555 	/* Ditch the internal IOCTL. */
10556 	freemsg(mp);
10557 	/* Complete the original. */
10558 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10559 }
10560 
10561 /*
10562  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10563  * interface) create the next available logical interface for this
10564  * physical interface.
10565  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10566  * ipif with the specified name.
10567  *
10568  * If the address family is not AF_UNSPEC then set the address as well.
10569  *
10570  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10571  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10572  *
10573  * Executed as a writer on the ill or ill group.
10574  * So no lock is needed to traverse the ipif chain, or examine the
10575  * phyint flags.
10576  */
10577 /* ARGSUSED */
10578 int
10579 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10580     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10581 {
10582 	mblk_t	*mp1;
10583 	struct lifreq *lifr;
10584 	boolean_t	isv6;
10585 	boolean_t	exists;
10586 	char 	*name;
10587 	char	*endp;
10588 	char	*cp;
10589 	int	namelen;
10590 	ipif_t	*ipif;
10591 	long	id;
10592 	ipsq_t	*ipsq;
10593 	ill_t	*ill;
10594 	sin_t	*sin;
10595 	int	err = 0;
10596 	boolean_t found_sep = B_FALSE;
10597 	conn_t	*connp;
10598 	zoneid_t zoneid;
10599 	int	orig_ifindex = 0;
10600 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10601 
10602 	ASSERT(q->q_next == NULL);
10603 	ip1dbg(("ip_sioctl_addif\n"));
10604 	/* Existence of mp1 has been checked in ip_wput_nondata */
10605 	mp1 = mp->b_cont->b_cont;
10606 	/*
10607 	 * Null terminate the string to protect against buffer
10608 	 * overrun. String was generated by user code and may not
10609 	 * be trusted.
10610 	 */
10611 	lifr = (struct lifreq *)mp1->b_rptr;
10612 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10613 	name = lifr->lifr_name;
10614 	ASSERT(CONN_Q(q));
10615 	connp = Q_TO_CONN(q);
10616 	isv6 = connp->conn_af_isv6;
10617 	zoneid = connp->conn_zoneid;
10618 	namelen = mi_strlen(name);
10619 	if (namelen == 0)
10620 		return (EINVAL);
10621 
10622 	exists = B_FALSE;
10623 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10624 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10625 		/*
10626 		 * Allow creating lo0 using SIOCLIFADDIF.
10627 		 * can't be any other writer thread. So can pass null below
10628 		 * for the last 4 args to ipif_lookup_name.
10629 		 */
10630 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10631 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10632 		/* Prevent any further action */
10633 		if (ipif == NULL) {
10634 			return (ENOBUFS);
10635 		} else if (!exists) {
10636 			/* We created the ipif now and as writer */
10637 			ipif_refrele(ipif);
10638 			return (0);
10639 		} else {
10640 			ill = ipif->ipif_ill;
10641 			ill_refhold(ill);
10642 			ipif_refrele(ipif);
10643 		}
10644 	} else {
10645 		/* Look for a colon in the name. */
10646 		endp = &name[namelen];
10647 		for (cp = endp; --cp > name; ) {
10648 			if (*cp == IPIF_SEPARATOR_CHAR) {
10649 				found_sep = B_TRUE;
10650 				/*
10651 				 * Reject any non-decimal aliases for plumbing
10652 				 * of logical interfaces. Aliases with leading
10653 				 * zeroes are also rejected as they introduce
10654 				 * ambiguity in the naming of the interfaces.
10655 				 * Comparing with "0" takes care of all such
10656 				 * cases.
10657 				 */
10658 				if ((strncmp("0", cp+1, 1)) == 0)
10659 					return (EINVAL);
10660 
10661 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10662 				    id <= 0 || *endp != '\0') {
10663 					return (EINVAL);
10664 				}
10665 				*cp = '\0';
10666 				break;
10667 			}
10668 		}
10669 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10670 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10671 		if (found_sep)
10672 			*cp = IPIF_SEPARATOR_CHAR;
10673 		if (ill == NULL)
10674 			return (err);
10675 	}
10676 
10677 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10678 	    B_TRUE);
10679 
10680 	/*
10681 	 * Release the refhold due to the lookup, now that we are excl
10682 	 * or we are just returning
10683 	 */
10684 	ill_refrele(ill);
10685 
10686 	if (ipsq == NULL)
10687 		return (EINPROGRESS);
10688 
10689 	/*
10690 	 * If the interface is failed, inactive or offlined, look for a working
10691 	 * interface in the ill group and create the ipif there. If we can't
10692 	 * find a good interface, create the ipif anyway so that in.mpathd can
10693 	 * move it to the first repaired interface.
10694 	 */
10695 	if ((ill->ill_phyint->phyint_flags &
10696 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10697 	    ill->ill_phyint->phyint_groupname_len != 0) {
10698 		phyint_t *phyi;
10699 		char *groupname = ill->ill_phyint->phyint_groupname;
10700 
10701 		/*
10702 		 * We're looking for a working interface, but it doesn't matter
10703 		 * if it's up or down; so instead of following the group lists,
10704 		 * we look at each physical interface and compare the groupname.
10705 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10706 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10707 		 * Otherwise we create the ipif on the failed interface.
10708 		 */
10709 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10710 		phyi = avl_first(&ipst->ips_phyint_g_list->
10711 		    phyint_list_avl_by_index);
10712 		for (; phyi != NULL;
10713 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10714 		    phyint_list_avl_by_index,
10715 		    phyi, AVL_AFTER)) {
10716 			if (phyi->phyint_groupname_len == 0)
10717 				continue;
10718 			ASSERT(phyi->phyint_groupname != NULL);
10719 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10720 			    !(phyi->phyint_flags &
10721 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10722 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10723 			    (phyi->phyint_illv4 != NULL))) {
10724 				break;
10725 			}
10726 		}
10727 		rw_exit(&ipst->ips_ill_g_lock);
10728 
10729 		if (phyi != NULL) {
10730 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10731 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10732 			    phyi->phyint_illv4);
10733 		}
10734 	}
10735 
10736 	/*
10737 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10738 	 * before or after us.
10739 	 */
10740 	ASSERT(IAM_WRITER_ILL(ill));
10741 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10742 
10743 	if (found_sep && orig_ifindex == 0) {
10744 		/* Now see if there is an IPIF with this unit number. */
10745 		for (ipif = ill->ill_ipif; ipif != NULL;
10746 		    ipif = ipif->ipif_next) {
10747 			if (ipif->ipif_id == id) {
10748 				err = EEXIST;
10749 				goto done;
10750 			}
10751 		}
10752 	}
10753 
10754 	/*
10755 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10756 	 * of lo0. We never come here when we plumb lo0:0. It
10757 	 * happens in ipif_lookup_on_name.
10758 	 * The specified unit number is ignored when we create the ipif on a
10759 	 * different interface. However, we save it in ipif_orig_ipifid below so
10760 	 * that the ipif fails back to the right position.
10761 	 */
10762 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10763 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10764 		err = ENOBUFS;
10765 		goto done;
10766 	}
10767 
10768 	/* Return created name with ioctl */
10769 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10770 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10771 	ip1dbg(("created %s\n", lifr->lifr_name));
10772 
10773 	/* Set address */
10774 	sin = (sin_t *)&lifr->lifr_addr;
10775 	if (sin->sin_family != AF_UNSPEC) {
10776 		err = ip_sioctl_addr(ipif, sin, q, mp,
10777 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10778 	}
10779 
10780 	/* Set ifindex and unit number for failback */
10781 	if (err == 0 && orig_ifindex != 0) {
10782 		ipif->ipif_orig_ifindex = orig_ifindex;
10783 		if (found_sep) {
10784 			ipif->ipif_orig_ipifid = id;
10785 		}
10786 	}
10787 
10788 done:
10789 	ipsq_exit(ipsq);
10790 	return (err);
10791 }
10792 
10793 /*
10794  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10795  * interface) delete it based on the IP address (on this physical interface).
10796  * Otherwise delete it based on the ipif_id.
10797  * Also, special handling to allow a removeif of lo0.
10798  */
10799 /* ARGSUSED */
10800 int
10801 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10802     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10803 {
10804 	conn_t		*connp;
10805 	ill_t		*ill = ipif->ipif_ill;
10806 	boolean_t	 success;
10807 	ip_stack_t	*ipst;
10808 
10809 	ipst = CONNQ_TO_IPST(q);
10810 
10811 	ASSERT(q->q_next == NULL);
10812 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10813 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10814 	ASSERT(IAM_WRITER_IPIF(ipif));
10815 
10816 	connp = Q_TO_CONN(q);
10817 	/*
10818 	 * Special case for unplumbing lo0 (the loopback physical interface).
10819 	 * If unplumbing lo0, the incoming address structure has been
10820 	 * initialized to all zeros. When unplumbing lo0, all its logical
10821 	 * interfaces must be removed too.
10822 	 *
10823 	 * Note that this interface may be called to remove a specific
10824 	 * loopback logical interface (eg, lo0:1). But in that case
10825 	 * ipif->ipif_id != 0 so that the code path for that case is the
10826 	 * same as any other interface (meaning it skips the code directly
10827 	 * below).
10828 	 */
10829 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10830 		if (sin->sin_family == AF_UNSPEC &&
10831 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10832 			/*
10833 			 * Mark it condemned. No new ref. will be made to ill.
10834 			 */
10835 			mutex_enter(&ill->ill_lock);
10836 			ill->ill_state_flags |= ILL_CONDEMNED;
10837 			for (ipif = ill->ill_ipif; ipif != NULL;
10838 			    ipif = ipif->ipif_next) {
10839 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10840 			}
10841 			mutex_exit(&ill->ill_lock);
10842 
10843 			ipif = ill->ill_ipif;
10844 			/* unplumb the loopback interface */
10845 			ill_delete(ill);
10846 			mutex_enter(&connp->conn_lock);
10847 			mutex_enter(&ill->ill_lock);
10848 			ASSERT(ill->ill_group == NULL);
10849 
10850 			/* Are any references to this ill active */
10851 			if (ill_is_freeable(ill)) {
10852 				mutex_exit(&ill->ill_lock);
10853 				mutex_exit(&connp->conn_lock);
10854 				ill_delete_tail(ill);
10855 				mi_free(ill);
10856 				return (0);
10857 			}
10858 			success = ipsq_pending_mp_add(connp, ipif,
10859 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10860 			mutex_exit(&connp->conn_lock);
10861 			mutex_exit(&ill->ill_lock);
10862 			if (success)
10863 				return (EINPROGRESS);
10864 			else
10865 				return (EINTR);
10866 		}
10867 	}
10868 
10869 	/*
10870 	 * We are exclusive on the ipsq, so an ill move will be serialized
10871 	 * before or after us.
10872 	 */
10873 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10874 
10875 	if (ipif->ipif_id == 0) {
10876 
10877 		ipsq_t *ipsq;
10878 
10879 		/* Find based on address */
10880 		if (ipif->ipif_isv6) {
10881 			sin6_t *sin6;
10882 
10883 			if (sin->sin_family != AF_INET6)
10884 				return (EAFNOSUPPORT);
10885 
10886 			sin6 = (sin6_t *)sin;
10887 			/* We are a writer, so we should be able to lookup */
10888 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10889 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10890 			if (ipif == NULL) {
10891 				/*
10892 				 * Maybe the address in on another interface in
10893 				 * the same IPMP group? We check this below.
10894 				 */
10895 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10896 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10897 				    ipst);
10898 			}
10899 		} else {
10900 			ipaddr_t addr;
10901 
10902 			if (sin->sin_family != AF_INET)
10903 				return (EAFNOSUPPORT);
10904 
10905 			addr = sin->sin_addr.s_addr;
10906 			/* We are a writer, so we should be able to lookup */
10907 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10908 			    NULL, NULL, NULL, ipst);
10909 			if (ipif == NULL) {
10910 				/*
10911 				 * Maybe the address in on another interface in
10912 				 * the same IPMP group? We check this below.
10913 				 */
10914 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10915 				    NULL, NULL, NULL, NULL, ipst);
10916 			}
10917 		}
10918 		if (ipif == NULL) {
10919 			return (EADDRNOTAVAIL);
10920 		}
10921 
10922 		/*
10923 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10924 		 * lifr_name of the physical interface but with an ip address
10925 		 * lifr_addr of a logical interface plumbed over it.
10926 		 * So update ipsq_current_ipif once ipif points to the
10927 		 * correct interface after doing ipif_lookup_addr().
10928 		 */
10929 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10930 		ASSERT(ipsq != NULL);
10931 
10932 		mutex_enter(&ipsq->ipsq_lock);
10933 		ipsq->ipsq_current_ipif = ipif;
10934 		mutex_exit(&ipsq->ipsq_lock);
10935 
10936 		/*
10937 		 * When the address to be removed is hosted on a different
10938 		 * interface, we check if the interface is in the same IPMP
10939 		 * group as the specified one; if so we proceed with the
10940 		 * removal.
10941 		 * ill->ill_group is NULL when the ill is down, so we have to
10942 		 * compare the group names instead.
10943 		 */
10944 		if (ipif->ipif_ill != ill &&
10945 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10946 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10947 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10948 		    ill->ill_phyint->phyint_groupname) != 0)) {
10949 			ipif_refrele(ipif);
10950 			return (EADDRNOTAVAIL);
10951 		}
10952 
10953 		/* This is a writer */
10954 		ipif_refrele(ipif);
10955 	}
10956 
10957 	/*
10958 	 * Can not delete instance zero since it is tied to the ill.
10959 	 */
10960 	if (ipif->ipif_id == 0)
10961 		return (EBUSY);
10962 
10963 	mutex_enter(&ill->ill_lock);
10964 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10965 	mutex_exit(&ill->ill_lock);
10966 
10967 	ipif_free(ipif);
10968 
10969 	mutex_enter(&connp->conn_lock);
10970 	mutex_enter(&ill->ill_lock);
10971 
10972 
10973 	/* Are any references to this ipif active */
10974 	if (ipif_is_freeable(ipif)) {
10975 		mutex_exit(&ill->ill_lock);
10976 		mutex_exit(&connp->conn_lock);
10977 		ipif_non_duplicate(ipif);
10978 		ipif_down_tail(ipif);
10979 		ipif_free_tail(ipif); /* frees ipif */
10980 		return (0);
10981 	}
10982 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10983 	    IPIF_FREE);
10984 	mutex_exit(&ill->ill_lock);
10985 	mutex_exit(&connp->conn_lock);
10986 	if (success)
10987 		return (EINPROGRESS);
10988 	else
10989 		return (EINTR);
10990 }
10991 
10992 /*
10993  * Restart the removeif ioctl. The refcnt has gone down to 0.
10994  * The ipif is already condemned. So can't find it thru lookups.
10995  */
10996 /* ARGSUSED */
10997 int
10998 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10999     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11000 {
11001 	ill_t *ill = ipif->ipif_ill;
11002 
11003 	ASSERT(IAM_WRITER_IPIF(ipif));
11004 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11005 
11006 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11007 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11008 
11009 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11010 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
11011 		ill_delete_tail(ill);
11012 		mi_free(ill);
11013 		return (0);
11014 	}
11015 
11016 	ipif_non_duplicate(ipif);
11017 	ipif_down_tail(ipif);
11018 	ipif_free_tail(ipif);
11019 
11020 	ILL_UNMARK_CHANGING(ill);
11021 	return (0);
11022 }
11023 
11024 /*
11025  * Set the local interface address.
11026  * Allow an address of all zero when the interface is down.
11027  */
11028 /* ARGSUSED */
11029 int
11030 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11031     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11032 {
11033 	int err = 0;
11034 	in6_addr_t v6addr;
11035 	boolean_t need_up = B_FALSE;
11036 
11037 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11038 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11039 
11040 	ASSERT(IAM_WRITER_IPIF(ipif));
11041 
11042 	if (ipif->ipif_isv6) {
11043 		sin6_t *sin6;
11044 		ill_t *ill;
11045 		phyint_t *phyi;
11046 
11047 		if (sin->sin_family != AF_INET6)
11048 			return (EAFNOSUPPORT);
11049 
11050 		sin6 = (sin6_t *)sin;
11051 		v6addr = sin6->sin6_addr;
11052 		ill = ipif->ipif_ill;
11053 		phyi = ill->ill_phyint;
11054 
11055 		/*
11056 		 * Enforce that true multicast interfaces have a link-local
11057 		 * address for logical unit 0.
11058 		 */
11059 		if (ipif->ipif_id == 0 &&
11060 		    (ill->ill_flags & ILLF_MULTICAST) &&
11061 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11062 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11063 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11064 			return (EADDRNOTAVAIL);
11065 		}
11066 
11067 		/*
11068 		 * up interfaces shouldn't have the unspecified address
11069 		 * unless they also have the IPIF_NOLOCAL flags set and
11070 		 * have a subnet assigned.
11071 		 */
11072 		if ((ipif->ipif_flags & IPIF_UP) &&
11073 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11074 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11075 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11076 			return (EADDRNOTAVAIL);
11077 		}
11078 
11079 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11080 			return (EADDRNOTAVAIL);
11081 	} else {
11082 		ipaddr_t addr;
11083 
11084 		if (sin->sin_family != AF_INET)
11085 			return (EAFNOSUPPORT);
11086 
11087 		addr = sin->sin_addr.s_addr;
11088 
11089 		/* Allow 0 as the local address. */
11090 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11091 			return (EADDRNOTAVAIL);
11092 
11093 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11094 	}
11095 
11096 	/*
11097 	 * Even if there is no change we redo things just to rerun
11098 	 * ipif_set_default.
11099 	 */
11100 	if (ipif->ipif_flags & IPIF_UP) {
11101 		/*
11102 		 * Setting a new local address, make sure
11103 		 * we have net and subnet bcast ire's for
11104 		 * the old address if we need them.
11105 		 */
11106 		if (!ipif->ipif_isv6)
11107 			ipif_check_bcast_ires(ipif);
11108 		/*
11109 		 * If the interface is already marked up,
11110 		 * we call ipif_down which will take care
11111 		 * of ditching any IREs that have been set
11112 		 * up based on the old interface address.
11113 		 */
11114 		err = ipif_logical_down(ipif, q, mp);
11115 		if (err == EINPROGRESS)
11116 			return (err);
11117 		ipif_down_tail(ipif);
11118 		need_up = 1;
11119 	}
11120 
11121 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11122 	return (err);
11123 }
11124 
11125 int
11126 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11127     boolean_t need_up)
11128 {
11129 	in6_addr_t v6addr;
11130 	in6_addr_t ov6addr;
11131 	ipaddr_t addr;
11132 	sin6_t	*sin6;
11133 	int	sinlen;
11134 	int	err = 0;
11135 	ill_t	*ill = ipif->ipif_ill;
11136 	boolean_t need_dl_down;
11137 	boolean_t need_arp_down;
11138 	struct iocblk *iocp;
11139 
11140 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11141 
11142 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11143 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11144 	ASSERT(IAM_WRITER_IPIF(ipif));
11145 
11146 	/* Must cancel any pending timer before taking the ill_lock */
11147 	if (ipif->ipif_recovery_id != 0)
11148 		(void) untimeout(ipif->ipif_recovery_id);
11149 	ipif->ipif_recovery_id = 0;
11150 
11151 	if (ipif->ipif_isv6) {
11152 		sin6 = (sin6_t *)sin;
11153 		v6addr = sin6->sin6_addr;
11154 		sinlen = sizeof (struct sockaddr_in6);
11155 	} else {
11156 		addr = sin->sin_addr.s_addr;
11157 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11158 		sinlen = sizeof (struct sockaddr_in);
11159 	}
11160 	mutex_enter(&ill->ill_lock);
11161 	ov6addr = ipif->ipif_v6lcl_addr;
11162 	ipif->ipif_v6lcl_addr = v6addr;
11163 	sctp_update_ipif_addr(ipif, ov6addr);
11164 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11165 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11166 	} else {
11167 		ipif->ipif_v6src_addr = v6addr;
11168 	}
11169 	ipif->ipif_addr_ready = 0;
11170 
11171 	/*
11172 	 * If the interface was previously marked as a duplicate, then since
11173 	 * we've now got a "new" address, it should no longer be considered a
11174 	 * duplicate -- even if the "new" address is the same as the old one.
11175 	 * Note that if all ipifs are down, we may have a pending ARP down
11176 	 * event to handle.  This is because we want to recover from duplicates
11177 	 * and thus delay tearing down ARP until the duplicates have been
11178 	 * removed or disabled.
11179 	 */
11180 	need_dl_down = need_arp_down = B_FALSE;
11181 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11182 		need_arp_down = !need_up;
11183 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11184 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11185 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11186 			need_dl_down = B_TRUE;
11187 		}
11188 	}
11189 
11190 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11191 	    !ill->ill_is_6to4tun) {
11192 		queue_t *wqp = ill->ill_wq;
11193 
11194 		/*
11195 		 * The local address of this interface is a 6to4 address,
11196 		 * check if this interface is in fact a 6to4 tunnel or just
11197 		 * an interface configured with a 6to4 address.  We are only
11198 		 * interested in the former.
11199 		 */
11200 		if (wqp != NULL) {
11201 			while ((wqp->q_next != NULL) &&
11202 			    (wqp->q_next->q_qinfo != NULL) &&
11203 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11204 
11205 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11206 				    == TUN6TO4_MODID) {
11207 					/* set for use in IP */
11208 					ill->ill_is_6to4tun = 1;
11209 					break;
11210 				}
11211 				wqp = wqp->q_next;
11212 			}
11213 		}
11214 	}
11215 
11216 	ipif_set_default(ipif);
11217 
11218 	/*
11219 	 * When publishing an interface address change event, we only notify
11220 	 * the event listeners of the new address.  It is assumed that if they
11221 	 * actively care about the addresses assigned that they will have
11222 	 * already discovered the previous address assigned (if there was one.)
11223 	 *
11224 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11225 	 */
11226 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11227 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11228 		    NE_ADDRESS_CHANGE, sin, sinlen);
11229 	}
11230 
11231 	mutex_exit(&ill->ill_lock);
11232 
11233 	if (need_up) {
11234 		/*
11235 		 * Now bring the interface back up.  If this
11236 		 * is the only IPIF for the ILL, ipif_up
11237 		 * will have to re-bind to the device, so
11238 		 * we may get back EINPROGRESS, in which
11239 		 * case, this IOCTL will get completed in
11240 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11241 		 */
11242 		err = ipif_up(ipif, q, mp);
11243 	}
11244 
11245 	if (need_dl_down)
11246 		ill_dl_down(ill);
11247 	if (need_arp_down)
11248 		ipif_arp_down(ipif);
11249 
11250 	return (err);
11251 }
11252 
11253 
11254 /*
11255  * Restart entry point to restart the address set operation after the
11256  * refcounts have dropped to zero.
11257  */
11258 /* ARGSUSED */
11259 int
11260 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11261     ip_ioctl_cmd_t *ipip, void *ifreq)
11262 {
11263 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11264 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11265 	ASSERT(IAM_WRITER_IPIF(ipif));
11266 	ipif_down_tail(ipif);
11267 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11268 }
11269 
11270 /* ARGSUSED */
11271 int
11272 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11273     ip_ioctl_cmd_t *ipip, void *if_req)
11274 {
11275 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11276 	struct lifreq *lifr = (struct lifreq *)if_req;
11277 
11278 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11279 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11280 	/*
11281 	 * The net mask and address can't change since we have a
11282 	 * reference to the ipif. So no lock is necessary.
11283 	 */
11284 	if (ipif->ipif_isv6) {
11285 		*sin6 = sin6_null;
11286 		sin6->sin6_family = AF_INET6;
11287 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11288 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11289 		lifr->lifr_addrlen =
11290 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11291 	} else {
11292 		*sin = sin_null;
11293 		sin->sin_family = AF_INET;
11294 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11295 		if (ipip->ipi_cmd_type == LIF_CMD) {
11296 			lifr->lifr_addrlen =
11297 			    ip_mask_to_plen(ipif->ipif_net_mask);
11298 		}
11299 	}
11300 	return (0);
11301 }
11302 
11303 /*
11304  * Set the destination address for a pt-pt interface.
11305  */
11306 /* ARGSUSED */
11307 int
11308 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11309     ip_ioctl_cmd_t *ipip, void *if_req)
11310 {
11311 	int err = 0;
11312 	in6_addr_t v6addr;
11313 	boolean_t need_up = B_FALSE;
11314 
11315 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11316 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11317 	ASSERT(IAM_WRITER_IPIF(ipif));
11318 
11319 	if (ipif->ipif_isv6) {
11320 		sin6_t *sin6;
11321 
11322 		if (sin->sin_family != AF_INET6)
11323 			return (EAFNOSUPPORT);
11324 
11325 		sin6 = (sin6_t *)sin;
11326 		v6addr = sin6->sin6_addr;
11327 
11328 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11329 			return (EADDRNOTAVAIL);
11330 	} else {
11331 		ipaddr_t addr;
11332 
11333 		if (sin->sin_family != AF_INET)
11334 			return (EAFNOSUPPORT);
11335 
11336 		addr = sin->sin_addr.s_addr;
11337 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11338 			return (EADDRNOTAVAIL);
11339 
11340 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11341 	}
11342 
11343 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11344 		return (0);	/* No change */
11345 
11346 	if (ipif->ipif_flags & IPIF_UP) {
11347 		/*
11348 		 * If the interface is already marked up,
11349 		 * we call ipif_down which will take care
11350 		 * of ditching any IREs that have been set
11351 		 * up based on the old pp dst address.
11352 		 */
11353 		err = ipif_logical_down(ipif, q, mp);
11354 		if (err == EINPROGRESS)
11355 			return (err);
11356 		ipif_down_tail(ipif);
11357 		need_up = B_TRUE;
11358 	}
11359 	/*
11360 	 * could return EINPROGRESS. If so ioctl will complete in
11361 	 * ip_rput_dlpi_writer
11362 	 */
11363 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11364 	return (err);
11365 }
11366 
11367 static int
11368 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11369     boolean_t need_up)
11370 {
11371 	in6_addr_t v6addr;
11372 	ill_t	*ill = ipif->ipif_ill;
11373 	int	err = 0;
11374 	boolean_t need_dl_down;
11375 	boolean_t need_arp_down;
11376 
11377 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11378 	    ipif->ipif_id, (void *)ipif));
11379 
11380 	/* Must cancel any pending timer before taking the ill_lock */
11381 	if (ipif->ipif_recovery_id != 0)
11382 		(void) untimeout(ipif->ipif_recovery_id);
11383 	ipif->ipif_recovery_id = 0;
11384 
11385 	if (ipif->ipif_isv6) {
11386 		sin6_t *sin6;
11387 
11388 		sin6 = (sin6_t *)sin;
11389 		v6addr = sin6->sin6_addr;
11390 	} else {
11391 		ipaddr_t addr;
11392 
11393 		addr = sin->sin_addr.s_addr;
11394 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11395 	}
11396 	mutex_enter(&ill->ill_lock);
11397 	/* Set point to point destination address. */
11398 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11399 		/*
11400 		 * Allow this as a means of creating logical
11401 		 * pt-pt interfaces on top of e.g. an Ethernet.
11402 		 * XXX Undocumented HACK for testing.
11403 		 * pt-pt interfaces are created with NUD disabled.
11404 		 */
11405 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11406 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11407 		if (ipif->ipif_isv6)
11408 			ill->ill_flags |= ILLF_NONUD;
11409 	}
11410 
11411 	/*
11412 	 * If the interface was previously marked as a duplicate, then since
11413 	 * we've now got a "new" address, it should no longer be considered a
11414 	 * duplicate -- even if the "new" address is the same as the old one.
11415 	 * Note that if all ipifs are down, we may have a pending ARP down
11416 	 * event to handle.
11417 	 */
11418 	need_dl_down = need_arp_down = B_FALSE;
11419 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11420 		need_arp_down = !need_up;
11421 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11422 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11423 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11424 			need_dl_down = B_TRUE;
11425 		}
11426 	}
11427 
11428 	/* Set the new address. */
11429 	ipif->ipif_v6pp_dst_addr = v6addr;
11430 	/* Make sure subnet tracks pp_dst */
11431 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11432 	mutex_exit(&ill->ill_lock);
11433 
11434 	if (need_up) {
11435 		/*
11436 		 * Now bring the interface back up.  If this
11437 		 * is the only IPIF for the ILL, ipif_up
11438 		 * will have to re-bind to the device, so
11439 		 * we may get back EINPROGRESS, in which
11440 		 * case, this IOCTL will get completed in
11441 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11442 		 */
11443 		err = ipif_up(ipif, q, mp);
11444 	}
11445 
11446 	if (need_dl_down)
11447 		ill_dl_down(ill);
11448 
11449 	if (need_arp_down)
11450 		ipif_arp_down(ipif);
11451 	return (err);
11452 }
11453 
11454 /*
11455  * Restart entry point to restart the dstaddress set operation after the
11456  * refcounts have dropped to zero.
11457  */
11458 /* ARGSUSED */
11459 int
11460 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11461     ip_ioctl_cmd_t *ipip, void *ifreq)
11462 {
11463 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11464 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11465 	ipif_down_tail(ipif);
11466 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11467 }
11468 
11469 /* ARGSUSED */
11470 int
11471 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11472     ip_ioctl_cmd_t *ipip, void *if_req)
11473 {
11474 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11475 
11476 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11477 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11478 	/*
11479 	 * Get point to point destination address. The addresses can't
11480 	 * change since we hold a reference to the ipif.
11481 	 */
11482 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11483 		return (EADDRNOTAVAIL);
11484 
11485 	if (ipif->ipif_isv6) {
11486 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11487 		*sin6 = sin6_null;
11488 		sin6->sin6_family = AF_INET6;
11489 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11490 	} else {
11491 		*sin = sin_null;
11492 		sin->sin_family = AF_INET;
11493 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11494 	}
11495 	return (0);
11496 }
11497 
11498 /*
11499  * part of ipmp, make this func return the active/inactive state and
11500  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11501  */
11502 /*
11503  * This function either sets or clears the IFF_INACTIVE flag.
11504  *
11505  * As long as there are some addresses or multicast memberships on the
11506  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11507  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11508  * will be used for outbound packets.
11509  *
11510  * Caller needs to verify the validity of setting IFF_INACTIVE.
11511  */
11512 static void
11513 phyint_inactive(phyint_t *phyi)
11514 {
11515 	ill_t *ill_v4;
11516 	ill_t *ill_v6;
11517 	ipif_t *ipif;
11518 	ilm_t *ilm;
11519 
11520 	ill_v4 = phyi->phyint_illv4;
11521 	ill_v6 = phyi->phyint_illv6;
11522 
11523 	/*
11524 	 * No need for a lock while traversing the list since iam
11525 	 * a writer
11526 	 */
11527 	if (ill_v4 != NULL) {
11528 		ASSERT(IAM_WRITER_ILL(ill_v4));
11529 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11530 		    ipif = ipif->ipif_next) {
11531 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11532 				mutex_enter(&phyi->phyint_lock);
11533 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11534 				mutex_exit(&phyi->phyint_lock);
11535 				return;
11536 			}
11537 		}
11538 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11539 		    ilm = ilm->ilm_next) {
11540 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11541 				mutex_enter(&phyi->phyint_lock);
11542 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11543 				mutex_exit(&phyi->phyint_lock);
11544 				return;
11545 			}
11546 		}
11547 	}
11548 	if (ill_v6 != NULL) {
11549 		ill_v6 = phyi->phyint_illv6;
11550 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11551 		    ipif = ipif->ipif_next) {
11552 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11553 				mutex_enter(&phyi->phyint_lock);
11554 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11555 				mutex_exit(&phyi->phyint_lock);
11556 				return;
11557 			}
11558 		}
11559 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11560 		    ilm = ilm->ilm_next) {
11561 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11562 				mutex_enter(&phyi->phyint_lock);
11563 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11564 				mutex_exit(&phyi->phyint_lock);
11565 				return;
11566 			}
11567 		}
11568 	}
11569 	mutex_enter(&phyi->phyint_lock);
11570 	phyi->phyint_flags |= PHYI_INACTIVE;
11571 	mutex_exit(&phyi->phyint_lock);
11572 }
11573 
11574 /*
11575  * This function is called only when the phyint flags change. Currently
11576  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11577  * that we can select a good ill.
11578  */
11579 static void
11580 ip_redo_nomination(phyint_t *phyi)
11581 {
11582 	ill_t *ill_v4;
11583 
11584 	ill_v4 = phyi->phyint_illv4;
11585 
11586 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11587 		ASSERT(IAM_WRITER_ILL(ill_v4));
11588 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11589 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11590 	}
11591 }
11592 
11593 /*
11594  * Heuristic to check if ill is INACTIVE.
11595  * Checks if ill has an ipif with an usable ip address.
11596  *
11597  * Return values:
11598  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11599  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11600  */
11601 static boolean_t
11602 ill_is_inactive(ill_t *ill)
11603 {
11604 	ipif_t *ipif;
11605 
11606 	/* Check whether it is in an IPMP group */
11607 	if (ill->ill_phyint->phyint_groupname == NULL)
11608 		return (B_FALSE);
11609 
11610 	if (ill->ill_ipif_up_count == 0)
11611 		return (B_TRUE);
11612 
11613 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11614 		uint64_t flags = ipif->ipif_flags;
11615 
11616 		/*
11617 		 * This ipif is usable if it is IPIF_UP and not a
11618 		 * dedicated test address.  A dedicated test address
11619 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11620 		 * (note in particular that V6 test addresses are
11621 		 * link-local data addresses and thus are marked
11622 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11623 		 */
11624 		if ((flags & IPIF_UP) &&
11625 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11626 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11627 			return (B_FALSE);
11628 	}
11629 	return (B_TRUE);
11630 }
11631 
11632 /*
11633  * Set interface flags.
11634  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11635  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11636  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11637  *
11638  * NOTE : We really don't enforce that ipif_id zero should be used
11639  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11640  *	  is because applications generally does SICGLIFFLAGS and
11641  *	  ORs in the new flags (that affects the logical) and does a
11642  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11643  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11644  *	  flags that will be turned on is correct with respect to
11645  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11646  */
11647 /* ARGSUSED */
11648 int
11649 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11650     ip_ioctl_cmd_t *ipip, void *if_req)
11651 {
11652 	uint64_t turn_on;
11653 	uint64_t turn_off;
11654 	int	err;
11655 	phyint_t *phyi;
11656 	ill_t *ill;
11657 	uint64_t intf_flags;
11658 	boolean_t phyint_flags_modified = B_FALSE;
11659 	uint64_t flags;
11660 	struct ifreq *ifr;
11661 	struct lifreq *lifr;
11662 	boolean_t set_linklocal = B_FALSE;
11663 	boolean_t zero_source = B_FALSE;
11664 	ip_stack_t *ipst;
11665 
11666 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11667 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11668 
11669 	ASSERT(IAM_WRITER_IPIF(ipif));
11670 
11671 	ill = ipif->ipif_ill;
11672 	phyi = ill->ill_phyint;
11673 	ipst = ill->ill_ipst;
11674 
11675 	if (ipip->ipi_cmd_type == IF_CMD) {
11676 		ifr = (struct ifreq *)if_req;
11677 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11678 	} else {
11679 		lifr = (struct lifreq *)if_req;
11680 		flags = lifr->lifr_flags;
11681 	}
11682 
11683 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11684 
11685 	/*
11686 	 * Have the flags been set correctly until now?
11687 	 */
11688 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11689 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11690 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11691 	/*
11692 	 * Compare the new flags to the old, and partition
11693 	 * into those coming on and those going off.
11694 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11695 	 */
11696 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11697 		flags |= intf_flags & ~0xFFFF;
11698 
11699 	/*
11700 	 * First check which bits will change and then which will
11701 	 * go on and off
11702 	 */
11703 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11704 	if (!turn_on)
11705 		return (0);	/* No change */
11706 
11707 	turn_off = intf_flags & turn_on;
11708 	turn_on ^= turn_off;
11709 	err = 0;
11710 
11711 	/*
11712 	 * Don't allow any bits belonging to the logical interface
11713 	 * to be set or cleared on the replacement ipif that was
11714 	 * created temporarily during a MOVE.
11715 	 */
11716 	if (ipif->ipif_replace_zero &&
11717 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11718 		return (EINVAL);
11719 	}
11720 
11721 	/*
11722 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11723 	 * IPv6 interfaces.
11724 	 */
11725 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11726 		return (EINVAL);
11727 
11728 	/*
11729 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11730 	 */
11731 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11732 		return (EINVAL);
11733 
11734 	/*
11735 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11736 	 * interfaces.  It makes no sense in that context.
11737 	 */
11738 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11739 		return (EINVAL);
11740 
11741 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11742 		zero_source = B_TRUE;
11743 
11744 	/*
11745 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11746 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11747 	 * If the link local address isn't set, and can be set, it will get
11748 	 * set later on in this function.
11749 	 */
11750 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11751 	    (flags & IFF_UP) && !zero_source &&
11752 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11753 		if (ipif_cant_setlinklocal(ipif))
11754 			return (EINVAL);
11755 		set_linklocal = B_TRUE;
11756 	}
11757 
11758 	/*
11759 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11760 	 * same time. No need to grab ill_g_usesrc_lock here, see
11761 	 * synchronization notes in ip.c
11762 	 */
11763 	if (turn_on & PHYI_STANDBY &&
11764 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11765 		return (EINVAL);
11766 	}
11767 
11768 	/*
11769 	 * If we modify physical interface flags, we'll potentially need to
11770 	 * send up two routing socket messages for the changes (one for the
11771 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11772 	 */
11773 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11774 		phyint_flags_modified = B_TRUE;
11775 
11776 	/*
11777 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11778 	 * we need to flush the IRE_CACHES belonging to this ill.
11779 	 * We handle this case here without doing the DOWN/UP dance
11780 	 * like it is done for other flags. If some other flags are
11781 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11782 	 * below will handle it by bringing it down and then
11783 	 * bringing it UP.
11784 	 */
11785 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11786 		ill_t *ill_v4, *ill_v6;
11787 
11788 		ill_v4 = phyi->phyint_illv4;
11789 		ill_v6 = phyi->phyint_illv6;
11790 
11791 		/*
11792 		 * First set the INACTIVE flag if needed. Then delete the ires.
11793 		 * ire_add will atomically prevent creating new IRE_CACHEs
11794 		 * unless hidden flag is set.
11795 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11796 		 */
11797 		if ((turn_on & PHYI_FAILED) &&
11798 		    ((intf_flags & PHYI_STANDBY) ||
11799 		    !ipst->ips_ipmp_enable_failback)) {
11800 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11801 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11802 		}
11803 		if ((turn_off & PHYI_FAILED) &&
11804 		    ((intf_flags & PHYI_STANDBY) ||
11805 		    (!ipst->ips_ipmp_enable_failback &&
11806 		    ill_is_inactive(ill)))) {
11807 			phyint_inactive(phyi);
11808 		}
11809 
11810 		if (turn_on & PHYI_STANDBY) {
11811 			/*
11812 			 * We implicitly set INACTIVE only when STANDBY is set.
11813 			 * INACTIVE is also set on non-STANDBY phyint when user
11814 			 * disables FAILBACK using configuration file.
11815 			 * Do not allow STANDBY to be set on such INACTIVE
11816 			 * phyint
11817 			 */
11818 			if (phyi->phyint_flags & PHYI_INACTIVE)
11819 				return (EINVAL);
11820 			if (!(phyi->phyint_flags & PHYI_FAILED))
11821 				phyint_inactive(phyi);
11822 		}
11823 		if (turn_off & PHYI_STANDBY) {
11824 			if (ipst->ips_ipmp_enable_failback) {
11825 				/*
11826 				 * Reset PHYI_INACTIVE.
11827 				 */
11828 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11829 			} else if (ill_is_inactive(ill) &&
11830 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11831 				/*
11832 				 * Need to set INACTIVE, when user sets
11833 				 * STANDBY on a non-STANDBY phyint and
11834 				 * later resets STANDBY
11835 				 */
11836 				phyint_inactive(phyi);
11837 			}
11838 		}
11839 		/*
11840 		 * We should always send up a message so that the
11841 		 * daemons come to know of it. Note that the zeroth
11842 		 * interface can be down and the check below for IPIF_UP
11843 		 * will not make sense as we are actually setting
11844 		 * a phyint flag here. We assume that the ipif used
11845 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11846 		 * send up any message for non-zero ipifs).
11847 		 */
11848 		phyint_flags_modified = B_TRUE;
11849 
11850 		if (ill_v4 != NULL) {
11851 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11852 			    IRE_CACHE, ill_stq_cache_delete,
11853 			    (char *)ill_v4, ill_v4);
11854 			illgrp_reset_schednext(ill_v4);
11855 		}
11856 		if (ill_v6 != NULL) {
11857 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11858 			    IRE_CACHE, ill_stq_cache_delete,
11859 			    (char *)ill_v6, ill_v6);
11860 			illgrp_reset_schednext(ill_v6);
11861 		}
11862 	}
11863 
11864 	/*
11865 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11866 	 * status of the interface and, if the interface is part of an IPMP
11867 	 * group, all other interfaces that are part of the same IPMP
11868 	 * group.
11869 	 */
11870 	if ((turn_on | turn_off) & ILLF_ROUTER)
11871 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11872 
11873 	/*
11874 	 * If the interface is not UP and we are not going to
11875 	 * bring it UP, record the flags and return. When the
11876 	 * interface comes UP later, the right actions will be
11877 	 * taken.
11878 	 */
11879 	if (!(ipif->ipif_flags & IPIF_UP) &&
11880 	    !(turn_on & IPIF_UP)) {
11881 		/* Record new flags in their respective places. */
11882 		mutex_enter(&ill->ill_lock);
11883 		mutex_enter(&ill->ill_phyint->phyint_lock);
11884 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11885 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11886 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11887 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11888 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11889 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11890 		mutex_exit(&ill->ill_lock);
11891 		mutex_exit(&ill->ill_phyint->phyint_lock);
11892 
11893 		/*
11894 		 * We do the broadcast and nomination here rather
11895 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11896 		 * the case of FAILBACK from INACTIVE standby to the
11897 		 * interface that has been repaired, PHYI_FAILED has not
11898 		 * been cleared yet. If there are only two interfaces in
11899 		 * that group, all we have is a FAILED and INACTIVE
11900 		 * interface. If we do the nomination soon after a failback,
11901 		 * the broadcast nomination code would select the
11902 		 * INACTIVE interface for receiving broadcasts as FAILED is
11903 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11904 		 * receive broadcast packets, we need to redo nomination
11905 		 * when the FAILED is cleared here. Thus, in general we
11906 		 * always do the nomination here for FAILED, STANDBY
11907 		 * and OFFLINE.
11908 		 */
11909 		if (((turn_on | turn_off) &
11910 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11911 			ip_redo_nomination(phyi);
11912 		}
11913 		if (phyint_flags_modified) {
11914 			if (phyi->phyint_illv4 != NULL) {
11915 				ip_rts_ifmsg(phyi->phyint_illv4->
11916 				    ill_ipif);
11917 			}
11918 			if (phyi->phyint_illv6 != NULL) {
11919 				ip_rts_ifmsg(phyi->phyint_illv6->
11920 				    ill_ipif);
11921 			}
11922 		}
11923 		return (0);
11924 	} else if (set_linklocal || zero_source) {
11925 		mutex_enter(&ill->ill_lock);
11926 		if (set_linklocal)
11927 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11928 		if (zero_source)
11929 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11930 		mutex_exit(&ill->ill_lock);
11931 	}
11932 
11933 	/*
11934 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11935 	 * or point-to-point interfaces with an unspecified destination. We do
11936 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11937 	 * have a subnet assigned, which is how in.ndpd currently manages its
11938 	 * onlink prefix list when no addresses are configured with those
11939 	 * prefixes.
11940 	 */
11941 	if (ipif->ipif_isv6 &&
11942 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11943 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11944 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11945 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11946 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11947 		return (EINVAL);
11948 	}
11949 
11950 	/*
11951 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11952 	 * from being brought up.
11953 	 */
11954 	if (!ipif->ipif_isv6 &&
11955 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11956 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11957 		return (EINVAL);
11958 	}
11959 
11960 	/*
11961 	 * The only flag changes that we currently take specific action on
11962 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11963 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11964 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11965 	 * the flags and bringing it back up again.
11966 	 */
11967 	if ((turn_on|turn_off) &
11968 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11969 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11970 		/*
11971 		 * Taking this ipif down, make sure we have
11972 		 * valid net and subnet bcast ire's for other
11973 		 * logical interfaces, if we need them.
11974 		 */
11975 		if (!ipif->ipif_isv6)
11976 			ipif_check_bcast_ires(ipif);
11977 
11978 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11979 		    !(turn_off & IPIF_UP)) {
11980 			if (ipif->ipif_flags & IPIF_UP)
11981 				ill->ill_logical_down = 1;
11982 			turn_on &= ~IPIF_UP;
11983 		}
11984 		err = ipif_down(ipif, q, mp);
11985 		ip1dbg(("ipif_down returns %d err ", err));
11986 		if (err == EINPROGRESS)
11987 			return (err);
11988 		ipif_down_tail(ipif);
11989 	}
11990 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11991 }
11992 
11993 static int
11994 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11995 {
11996 	ill_t	*ill;
11997 	phyint_t *phyi;
11998 	uint64_t turn_on;
11999 	uint64_t turn_off;
12000 	uint64_t intf_flags;
12001 	boolean_t phyint_flags_modified = B_FALSE;
12002 	int	err = 0;
12003 	boolean_t set_linklocal = B_FALSE;
12004 	boolean_t zero_source = B_FALSE;
12005 
12006 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12007 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12008 
12009 	ASSERT(IAM_WRITER_IPIF(ipif));
12010 
12011 	ill = ipif->ipif_ill;
12012 	phyi = ill->ill_phyint;
12013 
12014 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12015 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12016 
12017 	turn_off = intf_flags & turn_on;
12018 	turn_on ^= turn_off;
12019 
12020 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12021 		phyint_flags_modified = B_TRUE;
12022 
12023 	/*
12024 	 * Now we change the flags. Track current value of
12025 	 * other flags in their respective places.
12026 	 */
12027 	mutex_enter(&ill->ill_lock);
12028 	mutex_enter(&phyi->phyint_lock);
12029 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12030 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12031 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12032 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12033 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12034 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12035 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12036 		set_linklocal = B_TRUE;
12037 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12038 	}
12039 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12040 		zero_source = B_TRUE;
12041 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12042 	}
12043 	mutex_exit(&ill->ill_lock);
12044 	mutex_exit(&phyi->phyint_lock);
12045 
12046 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12047 		ip_redo_nomination(phyi);
12048 
12049 	if (set_linklocal)
12050 		(void) ipif_setlinklocal(ipif);
12051 
12052 	if (zero_source)
12053 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12054 	else
12055 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12056 
12057 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
12058 		/*
12059 		 * XXX ipif_up really does not know whether a phyint flags
12060 		 * was modified or not. So, it sends up information on
12061 		 * only one routing sockets message. As we don't bring up
12062 		 * the interface and also set STANDBY/FAILED simultaneously
12063 		 * it should be okay.
12064 		 */
12065 		err = ipif_up(ipif, q, mp);
12066 	} else {
12067 		/*
12068 		 * Make sure routing socket sees all changes to the flags.
12069 		 * ipif_up_done* handles this when we use ipif_up.
12070 		 */
12071 		if (phyint_flags_modified) {
12072 			if (phyi->phyint_illv4 != NULL) {
12073 				ip_rts_ifmsg(phyi->phyint_illv4->
12074 				    ill_ipif);
12075 			}
12076 			if (phyi->phyint_illv6 != NULL) {
12077 				ip_rts_ifmsg(phyi->phyint_illv6->
12078 				    ill_ipif);
12079 			}
12080 		} else {
12081 			ip_rts_ifmsg(ipif);
12082 		}
12083 		/*
12084 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12085 		 * this in need_up case.
12086 		 */
12087 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12088 	}
12089 	return (err);
12090 }
12091 
12092 /*
12093  * Restart the flags operation now that the refcounts have dropped to zero.
12094  */
12095 /* ARGSUSED */
12096 int
12097 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12098     ip_ioctl_cmd_t *ipip, void *if_req)
12099 {
12100 	uint64_t flags;
12101 	struct ifreq *ifr = if_req;
12102 	struct lifreq *lifr = if_req;
12103 
12104 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12105 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12106 
12107 	ipif_down_tail(ipif);
12108 	if (ipip->ipi_cmd_type == IF_CMD) {
12109 		/* cast to uint16_t prevents unwanted sign extension */
12110 		flags = (uint16_t)ifr->ifr_flags;
12111 	} else {
12112 		flags = lifr->lifr_flags;
12113 	}
12114 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
12115 }
12116 
12117 /*
12118  * Can operate on either a module or a driver queue.
12119  */
12120 /* ARGSUSED */
12121 int
12122 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12123     ip_ioctl_cmd_t *ipip, void *if_req)
12124 {
12125 	/*
12126 	 * Has the flags been set correctly till now ?
12127 	 */
12128 	ill_t *ill = ipif->ipif_ill;
12129 	phyint_t *phyi = ill->ill_phyint;
12130 
12131 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12132 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12133 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12134 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12135 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12136 
12137 	/*
12138 	 * Need a lock since some flags can be set even when there are
12139 	 * references to the ipif.
12140 	 */
12141 	mutex_enter(&ill->ill_lock);
12142 	if (ipip->ipi_cmd_type == IF_CMD) {
12143 		struct ifreq *ifr = (struct ifreq *)if_req;
12144 
12145 		/* Get interface flags (low 16 only). */
12146 		ifr->ifr_flags = ((ipif->ipif_flags |
12147 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12148 	} else {
12149 		struct lifreq *lifr = (struct lifreq *)if_req;
12150 
12151 		/* Get interface flags. */
12152 		lifr->lifr_flags = ipif->ipif_flags |
12153 		    ill->ill_flags | phyi->phyint_flags;
12154 	}
12155 	mutex_exit(&ill->ill_lock);
12156 	return (0);
12157 }
12158 
12159 /* ARGSUSED */
12160 int
12161 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12162     ip_ioctl_cmd_t *ipip, void *if_req)
12163 {
12164 	int mtu;
12165 	int ip_min_mtu;
12166 	struct ifreq	*ifr;
12167 	struct lifreq *lifr;
12168 	ire_t	*ire;
12169 	ip_stack_t *ipst;
12170 
12171 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12172 	    ipif->ipif_id, (void *)ipif));
12173 	if (ipip->ipi_cmd_type == IF_CMD) {
12174 		ifr = (struct ifreq *)if_req;
12175 		mtu = ifr->ifr_metric;
12176 	} else {
12177 		lifr = (struct lifreq *)if_req;
12178 		mtu = lifr->lifr_mtu;
12179 	}
12180 
12181 	if (ipif->ipif_isv6)
12182 		ip_min_mtu = IPV6_MIN_MTU;
12183 	else
12184 		ip_min_mtu = IP_MIN_MTU;
12185 
12186 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12187 		return (EINVAL);
12188 
12189 	/*
12190 	 * Change the MTU size in all relevant ire's.
12191 	 * Mtu change Vs. new ire creation - protocol below.
12192 	 * First change ipif_mtu and the ire_max_frag of the
12193 	 * interface ire. Then do an ire walk and change the
12194 	 * ire_max_frag of all affected ires. During ire_add
12195 	 * under the bucket lock, set the ire_max_frag of the
12196 	 * new ire being created from the ipif/ire from which
12197 	 * it is being derived. If an mtu change happens after
12198 	 * the ire is added, the new ire will be cleaned up.
12199 	 * Conversely if the mtu change happens before the ire
12200 	 * is added, ire_add will see the new value of the mtu.
12201 	 */
12202 	ipif->ipif_mtu = mtu;
12203 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12204 
12205 	if (ipif->ipif_isv6)
12206 		ire = ipif_to_ire_v6(ipif);
12207 	else
12208 		ire = ipif_to_ire(ipif);
12209 	if (ire != NULL) {
12210 		ire->ire_max_frag = ipif->ipif_mtu;
12211 		ire_refrele(ire);
12212 	}
12213 	ipst = ipif->ipif_ill->ill_ipst;
12214 	if (ipif->ipif_flags & IPIF_UP) {
12215 		if (ipif->ipif_isv6)
12216 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12217 			    ipst);
12218 		else
12219 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12220 			    ipst);
12221 	}
12222 	/* Update the MTU in SCTP's list */
12223 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12224 	return (0);
12225 }
12226 
12227 /* Get interface MTU. */
12228 /* ARGSUSED */
12229 int
12230 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12231 	ip_ioctl_cmd_t *ipip, void *if_req)
12232 {
12233 	struct ifreq	*ifr;
12234 	struct lifreq	*lifr;
12235 
12236 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12237 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12238 	if (ipip->ipi_cmd_type == IF_CMD) {
12239 		ifr = (struct ifreq *)if_req;
12240 		ifr->ifr_metric = ipif->ipif_mtu;
12241 	} else {
12242 		lifr = (struct lifreq *)if_req;
12243 		lifr->lifr_mtu = ipif->ipif_mtu;
12244 	}
12245 	return (0);
12246 }
12247 
12248 /* Set interface broadcast address. */
12249 /* ARGSUSED2 */
12250 int
12251 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12252 	ip_ioctl_cmd_t *ipip, void *if_req)
12253 {
12254 	ipaddr_t addr;
12255 	ire_t	*ire;
12256 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12257 
12258 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12259 	    ipif->ipif_id));
12260 
12261 	ASSERT(IAM_WRITER_IPIF(ipif));
12262 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12263 		return (EADDRNOTAVAIL);
12264 
12265 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12266 
12267 	if (sin->sin_family != AF_INET)
12268 		return (EAFNOSUPPORT);
12269 
12270 	addr = sin->sin_addr.s_addr;
12271 	if (ipif->ipif_flags & IPIF_UP) {
12272 		/*
12273 		 * If we are already up, make sure the new
12274 		 * broadcast address makes sense.  If it does,
12275 		 * there should be an IRE for it already.
12276 		 * Don't match on ipif, only on the ill
12277 		 * since we are sharing these now. Don't use
12278 		 * MATCH_IRE_ILL_GROUP as we are looking for
12279 		 * the broadcast ire on this ill and each ill
12280 		 * in the group has its own broadcast ire.
12281 		 */
12282 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12283 		    ipif, ALL_ZONES, NULL,
12284 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12285 		if (ire == NULL) {
12286 			return (EINVAL);
12287 		} else {
12288 			ire_refrele(ire);
12289 		}
12290 	}
12291 	/*
12292 	 * Changing the broadcast addr for this ipif.
12293 	 * Make sure we have valid net and subnet bcast
12294 	 * ire's for other logical interfaces, if needed.
12295 	 */
12296 	if (addr != ipif->ipif_brd_addr)
12297 		ipif_check_bcast_ires(ipif);
12298 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12299 	return (0);
12300 }
12301 
12302 /* Get interface broadcast address. */
12303 /* ARGSUSED */
12304 int
12305 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12306     ip_ioctl_cmd_t *ipip, void *if_req)
12307 {
12308 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12309 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12310 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12311 		return (EADDRNOTAVAIL);
12312 
12313 	/* IPIF_BROADCAST not possible with IPv6 */
12314 	ASSERT(!ipif->ipif_isv6);
12315 	*sin = sin_null;
12316 	sin->sin_family = AF_INET;
12317 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12318 	return (0);
12319 }
12320 
12321 /*
12322  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12323  */
12324 /* ARGSUSED */
12325 int
12326 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12327     ip_ioctl_cmd_t *ipip, void *if_req)
12328 {
12329 	int err = 0;
12330 	in6_addr_t v6mask;
12331 
12332 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12333 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12334 
12335 	ASSERT(IAM_WRITER_IPIF(ipif));
12336 
12337 	if (ipif->ipif_isv6) {
12338 		sin6_t *sin6;
12339 
12340 		if (sin->sin_family != AF_INET6)
12341 			return (EAFNOSUPPORT);
12342 
12343 		sin6 = (sin6_t *)sin;
12344 		v6mask = sin6->sin6_addr;
12345 	} else {
12346 		ipaddr_t mask;
12347 
12348 		if (sin->sin_family != AF_INET)
12349 			return (EAFNOSUPPORT);
12350 
12351 		mask = sin->sin_addr.s_addr;
12352 		V4MASK_TO_V6(mask, v6mask);
12353 	}
12354 
12355 	/*
12356 	 * No big deal if the interface isn't already up, or the mask
12357 	 * isn't really changing, or this is pt-pt.
12358 	 */
12359 	if (!(ipif->ipif_flags & IPIF_UP) ||
12360 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12361 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12362 		ipif->ipif_v6net_mask = v6mask;
12363 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12364 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12365 			    ipif->ipif_v6net_mask,
12366 			    ipif->ipif_v6subnet);
12367 		}
12368 		return (0);
12369 	}
12370 	/*
12371 	 * Make sure we have valid net and subnet broadcast ire's
12372 	 * for the old netmask, if needed by other logical interfaces.
12373 	 */
12374 	if (!ipif->ipif_isv6)
12375 		ipif_check_bcast_ires(ipif);
12376 
12377 	err = ipif_logical_down(ipif, q, mp);
12378 	if (err == EINPROGRESS)
12379 		return (err);
12380 	ipif_down_tail(ipif);
12381 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12382 	return (err);
12383 }
12384 
12385 static int
12386 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12387 {
12388 	in6_addr_t v6mask;
12389 	int err = 0;
12390 
12391 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12392 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12393 
12394 	if (ipif->ipif_isv6) {
12395 		sin6_t *sin6;
12396 
12397 		sin6 = (sin6_t *)sin;
12398 		v6mask = sin6->sin6_addr;
12399 	} else {
12400 		ipaddr_t mask;
12401 
12402 		mask = sin->sin_addr.s_addr;
12403 		V4MASK_TO_V6(mask, v6mask);
12404 	}
12405 
12406 	ipif->ipif_v6net_mask = v6mask;
12407 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12408 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12409 		    ipif->ipif_v6subnet);
12410 	}
12411 	err = ipif_up(ipif, q, mp);
12412 
12413 	if (err == 0 || err == EINPROGRESS) {
12414 		/*
12415 		 * The interface must be DL_BOUND if this packet has to
12416 		 * go out on the wire. Since we only go through a logical
12417 		 * down and are bound with the driver during an internal
12418 		 * down/up that is satisfied.
12419 		 */
12420 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12421 			/* Potentially broadcast an address mask reply. */
12422 			ipif_mask_reply(ipif);
12423 		}
12424 	}
12425 	return (err);
12426 }
12427 
12428 /* ARGSUSED */
12429 int
12430 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12431     ip_ioctl_cmd_t *ipip, void *if_req)
12432 {
12433 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12434 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12435 	ipif_down_tail(ipif);
12436 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12437 }
12438 
12439 /* Get interface net mask. */
12440 /* ARGSUSED */
12441 int
12442 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12443     ip_ioctl_cmd_t *ipip, void *if_req)
12444 {
12445 	struct lifreq *lifr = (struct lifreq *)if_req;
12446 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12447 
12448 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12449 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12450 
12451 	/*
12452 	 * net mask can't change since we have a reference to the ipif.
12453 	 */
12454 	if (ipif->ipif_isv6) {
12455 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12456 		*sin6 = sin6_null;
12457 		sin6->sin6_family = AF_INET6;
12458 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12459 		lifr->lifr_addrlen =
12460 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12461 	} else {
12462 		*sin = sin_null;
12463 		sin->sin_family = AF_INET;
12464 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12465 		if (ipip->ipi_cmd_type == LIF_CMD) {
12466 			lifr->lifr_addrlen =
12467 			    ip_mask_to_plen(ipif->ipif_net_mask);
12468 		}
12469 	}
12470 	return (0);
12471 }
12472 
12473 /* ARGSUSED */
12474 int
12475 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12476     ip_ioctl_cmd_t *ipip, void *if_req)
12477 {
12478 
12479 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12480 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12481 	/*
12482 	 * Set interface metric.  We don't use this for
12483 	 * anything but we keep track of it in case it is
12484 	 * important to routing applications or such.
12485 	 */
12486 	if (ipip->ipi_cmd_type == IF_CMD) {
12487 		struct ifreq    *ifr;
12488 
12489 		ifr = (struct ifreq *)if_req;
12490 		ipif->ipif_metric = ifr->ifr_metric;
12491 	} else {
12492 		struct lifreq   *lifr;
12493 
12494 		lifr = (struct lifreq *)if_req;
12495 		ipif->ipif_metric = lifr->lifr_metric;
12496 	}
12497 	return (0);
12498 }
12499 
12500 /* ARGSUSED */
12501 int
12502 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12503     ip_ioctl_cmd_t *ipip, void *if_req)
12504 {
12505 	/* Get interface metric. */
12506 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12507 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12508 	if (ipip->ipi_cmd_type == IF_CMD) {
12509 		struct ifreq    *ifr;
12510 
12511 		ifr = (struct ifreq *)if_req;
12512 		ifr->ifr_metric = ipif->ipif_metric;
12513 	} else {
12514 		struct lifreq   *lifr;
12515 
12516 		lifr = (struct lifreq *)if_req;
12517 		lifr->lifr_metric = ipif->ipif_metric;
12518 	}
12519 
12520 	return (0);
12521 }
12522 
12523 /* ARGSUSED */
12524 int
12525 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12526     ip_ioctl_cmd_t *ipip, void *if_req)
12527 {
12528 
12529 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12530 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12531 	/*
12532 	 * Set the muxid returned from I_PLINK.
12533 	 */
12534 	if (ipip->ipi_cmd_type == IF_CMD) {
12535 		struct ifreq *ifr = (struct ifreq *)if_req;
12536 
12537 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12538 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12539 	} else {
12540 		struct lifreq *lifr = (struct lifreq *)if_req;
12541 
12542 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12543 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12544 	}
12545 	return (0);
12546 }
12547 
12548 /* ARGSUSED */
12549 int
12550 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12551     ip_ioctl_cmd_t *ipip, void *if_req)
12552 {
12553 
12554 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12555 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12556 	/*
12557 	 * Get the muxid saved in ill for I_PUNLINK.
12558 	 */
12559 	if (ipip->ipi_cmd_type == IF_CMD) {
12560 		struct ifreq *ifr = (struct ifreq *)if_req;
12561 
12562 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12563 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12564 	} else {
12565 		struct lifreq *lifr = (struct lifreq *)if_req;
12566 
12567 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12568 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12569 	}
12570 	return (0);
12571 }
12572 
12573 /*
12574  * Set the subnet prefix. Does not modify the broadcast address.
12575  */
12576 /* ARGSUSED */
12577 int
12578 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12579     ip_ioctl_cmd_t *ipip, void *if_req)
12580 {
12581 	int err = 0;
12582 	in6_addr_t v6addr;
12583 	in6_addr_t v6mask;
12584 	boolean_t need_up = B_FALSE;
12585 	int addrlen;
12586 
12587 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12588 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12589 
12590 	ASSERT(IAM_WRITER_IPIF(ipif));
12591 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12592 
12593 	if (ipif->ipif_isv6) {
12594 		sin6_t *sin6;
12595 
12596 		if (sin->sin_family != AF_INET6)
12597 			return (EAFNOSUPPORT);
12598 
12599 		sin6 = (sin6_t *)sin;
12600 		v6addr = sin6->sin6_addr;
12601 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12602 			return (EADDRNOTAVAIL);
12603 	} else {
12604 		ipaddr_t addr;
12605 
12606 		if (sin->sin_family != AF_INET)
12607 			return (EAFNOSUPPORT);
12608 
12609 		addr = sin->sin_addr.s_addr;
12610 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12611 			return (EADDRNOTAVAIL);
12612 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12613 		/* Add 96 bits */
12614 		addrlen += IPV6_ABITS - IP_ABITS;
12615 	}
12616 
12617 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12618 		return (EINVAL);
12619 
12620 	/* Check if bits in the address is set past the mask */
12621 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12622 		return (EINVAL);
12623 
12624 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12625 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12626 		return (0);	/* No change */
12627 
12628 	if (ipif->ipif_flags & IPIF_UP) {
12629 		/*
12630 		 * If the interface is already marked up,
12631 		 * we call ipif_down which will take care
12632 		 * of ditching any IREs that have been set
12633 		 * up based on the old interface address.
12634 		 */
12635 		err = ipif_logical_down(ipif, q, mp);
12636 		if (err == EINPROGRESS)
12637 			return (err);
12638 		ipif_down_tail(ipif);
12639 		need_up = B_TRUE;
12640 	}
12641 
12642 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12643 	return (err);
12644 }
12645 
12646 static int
12647 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12648     queue_t *q, mblk_t *mp, boolean_t need_up)
12649 {
12650 	ill_t	*ill = ipif->ipif_ill;
12651 	int	err = 0;
12652 
12653 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12654 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12655 
12656 	/* Set the new address. */
12657 	mutex_enter(&ill->ill_lock);
12658 	ipif->ipif_v6net_mask = v6mask;
12659 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12660 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12661 		    ipif->ipif_v6subnet);
12662 	}
12663 	mutex_exit(&ill->ill_lock);
12664 
12665 	if (need_up) {
12666 		/*
12667 		 * Now bring the interface back up.  If this
12668 		 * is the only IPIF for the ILL, ipif_up
12669 		 * will have to re-bind to the device, so
12670 		 * we may get back EINPROGRESS, in which
12671 		 * case, this IOCTL will get completed in
12672 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12673 		 */
12674 		err = ipif_up(ipif, q, mp);
12675 		if (err == EINPROGRESS)
12676 			return (err);
12677 	}
12678 	return (err);
12679 }
12680 
12681 /* ARGSUSED */
12682 int
12683 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12684     ip_ioctl_cmd_t *ipip, void *if_req)
12685 {
12686 	int	addrlen;
12687 	in6_addr_t v6addr;
12688 	in6_addr_t v6mask;
12689 	struct lifreq *lifr = (struct lifreq *)if_req;
12690 
12691 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12692 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12693 	ipif_down_tail(ipif);
12694 
12695 	addrlen = lifr->lifr_addrlen;
12696 	if (ipif->ipif_isv6) {
12697 		sin6_t *sin6;
12698 
12699 		sin6 = (sin6_t *)sin;
12700 		v6addr = sin6->sin6_addr;
12701 	} else {
12702 		ipaddr_t addr;
12703 
12704 		addr = sin->sin_addr.s_addr;
12705 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12706 		addrlen += IPV6_ABITS - IP_ABITS;
12707 	}
12708 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12709 
12710 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12711 }
12712 
12713 /* ARGSUSED */
12714 int
12715 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12716     ip_ioctl_cmd_t *ipip, void *if_req)
12717 {
12718 	struct lifreq *lifr = (struct lifreq *)if_req;
12719 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12720 
12721 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12722 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12723 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12724 
12725 	if (ipif->ipif_isv6) {
12726 		*sin6 = sin6_null;
12727 		sin6->sin6_family = AF_INET6;
12728 		sin6->sin6_addr = ipif->ipif_v6subnet;
12729 		lifr->lifr_addrlen =
12730 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12731 	} else {
12732 		*sin = sin_null;
12733 		sin->sin_family = AF_INET;
12734 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12735 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12736 	}
12737 	return (0);
12738 }
12739 
12740 /*
12741  * Set the IPv6 address token.
12742  */
12743 /* ARGSUSED */
12744 int
12745 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12746     ip_ioctl_cmd_t *ipi, void *if_req)
12747 {
12748 	ill_t *ill = ipif->ipif_ill;
12749 	int err;
12750 	in6_addr_t v6addr;
12751 	in6_addr_t v6mask;
12752 	boolean_t need_up = B_FALSE;
12753 	int i;
12754 	sin6_t *sin6 = (sin6_t *)sin;
12755 	struct lifreq *lifr = (struct lifreq *)if_req;
12756 	int addrlen;
12757 
12758 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12759 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12760 	ASSERT(IAM_WRITER_IPIF(ipif));
12761 
12762 	addrlen = lifr->lifr_addrlen;
12763 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12764 	if (ipif->ipif_id != 0)
12765 		return (EINVAL);
12766 
12767 	if (!ipif->ipif_isv6)
12768 		return (EINVAL);
12769 
12770 	if (addrlen > IPV6_ABITS)
12771 		return (EINVAL);
12772 
12773 	v6addr = sin6->sin6_addr;
12774 
12775 	/*
12776 	 * The length of the token is the length from the end.  To get
12777 	 * the proper mask for this, compute the mask of the bits not
12778 	 * in the token; ie. the prefix, and then xor to get the mask.
12779 	 */
12780 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12781 		return (EINVAL);
12782 	for (i = 0; i < 4; i++) {
12783 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12784 	}
12785 
12786 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12787 	    ill->ill_token_length == addrlen)
12788 		return (0);	/* No change */
12789 
12790 	if (ipif->ipif_flags & IPIF_UP) {
12791 		err = ipif_logical_down(ipif, q, mp);
12792 		if (err == EINPROGRESS)
12793 			return (err);
12794 		ipif_down_tail(ipif);
12795 		need_up = B_TRUE;
12796 	}
12797 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12798 	return (err);
12799 }
12800 
12801 static int
12802 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12803     mblk_t *mp, boolean_t need_up)
12804 {
12805 	in6_addr_t v6addr;
12806 	in6_addr_t v6mask;
12807 	ill_t	*ill = ipif->ipif_ill;
12808 	int	i;
12809 	int	err = 0;
12810 
12811 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12812 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12813 	v6addr = sin6->sin6_addr;
12814 	/*
12815 	 * The length of the token is the length from the end.  To get
12816 	 * the proper mask for this, compute the mask of the bits not
12817 	 * in the token; ie. the prefix, and then xor to get the mask.
12818 	 */
12819 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12820 	for (i = 0; i < 4; i++)
12821 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12822 
12823 	mutex_enter(&ill->ill_lock);
12824 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12825 	ill->ill_token_length = addrlen;
12826 	mutex_exit(&ill->ill_lock);
12827 
12828 	if (need_up) {
12829 		/*
12830 		 * Now bring the interface back up.  If this
12831 		 * is the only IPIF for the ILL, ipif_up
12832 		 * will have to re-bind to the device, so
12833 		 * we may get back EINPROGRESS, in which
12834 		 * case, this IOCTL will get completed in
12835 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12836 		 */
12837 		err = ipif_up(ipif, q, mp);
12838 		if (err == EINPROGRESS)
12839 			return (err);
12840 	}
12841 	return (err);
12842 }
12843 
12844 /* ARGSUSED */
12845 int
12846 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12847     ip_ioctl_cmd_t *ipi, void *if_req)
12848 {
12849 	ill_t *ill;
12850 	sin6_t *sin6 = (sin6_t *)sin;
12851 	struct lifreq *lifr = (struct lifreq *)if_req;
12852 
12853 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12854 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12855 	if (ipif->ipif_id != 0)
12856 		return (EINVAL);
12857 
12858 	ill = ipif->ipif_ill;
12859 	if (!ill->ill_isv6)
12860 		return (ENXIO);
12861 
12862 	*sin6 = sin6_null;
12863 	sin6->sin6_family = AF_INET6;
12864 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12865 	sin6->sin6_addr = ill->ill_token;
12866 	lifr->lifr_addrlen = ill->ill_token_length;
12867 	return (0);
12868 }
12869 
12870 /*
12871  * Set (hardware) link specific information that might override
12872  * what was acquired through the DL_INFO_ACK.
12873  * The logic is as follows.
12874  *
12875  * become exclusive
12876  * set CHANGING flag
12877  * change mtu on affected IREs
12878  * clear CHANGING flag
12879  *
12880  * An ire add that occurs before the CHANGING flag is set will have its mtu
12881  * changed by the ip_sioctl_lnkinfo.
12882  *
12883  * During the time the CHANGING flag is set, no new ires will be added to the
12884  * bucket, and ire add will fail (due the CHANGING flag).
12885  *
12886  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12887  * before it is added to the bucket.
12888  *
12889  * Obviously only 1 thread can set the CHANGING flag and we need to become
12890  * exclusive to set the flag.
12891  */
12892 /* ARGSUSED */
12893 int
12894 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12895     ip_ioctl_cmd_t *ipi, void *if_req)
12896 {
12897 	ill_t		*ill = ipif->ipif_ill;
12898 	ipif_t		*nipif;
12899 	int		ip_min_mtu;
12900 	boolean_t	mtu_walk = B_FALSE;
12901 	struct lifreq	*lifr = (struct lifreq *)if_req;
12902 	lif_ifinfo_req_t *lir;
12903 	ire_t		*ire;
12904 
12905 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12906 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12907 	lir = &lifr->lifr_ifinfo;
12908 	ASSERT(IAM_WRITER_IPIF(ipif));
12909 
12910 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12911 	if (ipif->ipif_id != 0)
12912 		return (EINVAL);
12913 
12914 	/* Set interface MTU. */
12915 	if (ipif->ipif_isv6)
12916 		ip_min_mtu = IPV6_MIN_MTU;
12917 	else
12918 		ip_min_mtu = IP_MIN_MTU;
12919 
12920 	/*
12921 	 * Verify values before we set anything. Allow zero to
12922 	 * mean unspecified.
12923 	 */
12924 	if (lir->lir_maxmtu != 0 &&
12925 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12926 	    lir->lir_maxmtu < ip_min_mtu))
12927 		return (EINVAL);
12928 	if (lir->lir_reachtime != 0 &&
12929 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12930 		return (EINVAL);
12931 	if (lir->lir_reachretrans != 0 &&
12932 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12933 		return (EINVAL);
12934 
12935 	mutex_enter(&ill->ill_lock);
12936 	ill->ill_state_flags |= ILL_CHANGING;
12937 	for (nipif = ill->ill_ipif; nipif != NULL;
12938 	    nipif = nipif->ipif_next) {
12939 		nipif->ipif_state_flags |= IPIF_CHANGING;
12940 	}
12941 
12942 	mutex_exit(&ill->ill_lock);
12943 
12944 	if (lir->lir_maxmtu != 0) {
12945 		ill->ill_max_mtu = lir->lir_maxmtu;
12946 		ill->ill_mtu_userspecified = 1;
12947 		mtu_walk = B_TRUE;
12948 	}
12949 
12950 	if (lir->lir_reachtime != 0)
12951 		ill->ill_reachable_time = lir->lir_reachtime;
12952 
12953 	if (lir->lir_reachretrans != 0)
12954 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12955 
12956 	ill->ill_max_hops = lir->lir_maxhops;
12957 
12958 	ill->ill_max_buf = ND_MAX_Q;
12959 
12960 	if (mtu_walk) {
12961 		/*
12962 		 * Set the MTU on all ipifs associated with this ill except
12963 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12964 		 */
12965 		for (nipif = ill->ill_ipif; nipif != NULL;
12966 		    nipif = nipif->ipif_next) {
12967 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12968 				continue;
12969 
12970 			nipif->ipif_mtu = ill->ill_max_mtu;
12971 
12972 			if (!(nipif->ipif_flags & IPIF_UP))
12973 				continue;
12974 
12975 			if (nipif->ipif_isv6)
12976 				ire = ipif_to_ire_v6(nipif);
12977 			else
12978 				ire = ipif_to_ire(nipif);
12979 			if (ire != NULL) {
12980 				ire->ire_max_frag = ipif->ipif_mtu;
12981 				ire_refrele(ire);
12982 			}
12983 
12984 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12985 			    nipif, ill);
12986 		}
12987 	}
12988 
12989 	mutex_enter(&ill->ill_lock);
12990 	for (nipif = ill->ill_ipif; nipif != NULL;
12991 	    nipif = nipif->ipif_next) {
12992 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12993 	}
12994 	ILL_UNMARK_CHANGING(ill);
12995 	mutex_exit(&ill->ill_lock);
12996 
12997 	return (0);
12998 }
12999 
13000 /* ARGSUSED */
13001 int
13002 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13003     ip_ioctl_cmd_t *ipi, void *if_req)
13004 {
13005 	struct lif_ifinfo_req *lir;
13006 	ill_t *ill = ipif->ipif_ill;
13007 
13008 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13009 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13010 	if (ipif->ipif_id != 0)
13011 		return (EINVAL);
13012 
13013 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13014 	lir->lir_maxhops = ill->ill_max_hops;
13015 	lir->lir_reachtime = ill->ill_reachable_time;
13016 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13017 	lir->lir_maxmtu = ill->ill_max_mtu;
13018 
13019 	return (0);
13020 }
13021 
13022 /*
13023  * Return best guess as to the subnet mask for the specified address.
13024  * Based on the subnet masks for all the configured interfaces.
13025  *
13026  * We end up returning a zero mask in the case of default, multicast or
13027  * experimental.
13028  */
13029 static ipaddr_t
13030 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13031 {
13032 	ipaddr_t net_mask;
13033 	ill_t	*ill;
13034 	ipif_t	*ipif;
13035 	ill_walk_context_t ctx;
13036 	ipif_t	*fallback_ipif = NULL;
13037 
13038 	net_mask = ip_net_mask(addr);
13039 	if (net_mask == 0) {
13040 		*ipifp = NULL;
13041 		return (0);
13042 	}
13043 
13044 	/* Let's check to see if this is maybe a local subnet route. */
13045 	/* this function only applies to IPv4 interfaces */
13046 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13047 	ill = ILL_START_WALK_V4(&ctx, ipst);
13048 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13049 		mutex_enter(&ill->ill_lock);
13050 		for (ipif = ill->ill_ipif; ipif != NULL;
13051 		    ipif = ipif->ipif_next) {
13052 			if (!IPIF_CAN_LOOKUP(ipif))
13053 				continue;
13054 			if (!(ipif->ipif_flags & IPIF_UP))
13055 				continue;
13056 			if ((ipif->ipif_subnet & net_mask) ==
13057 			    (addr & net_mask)) {
13058 				/*
13059 				 * Don't trust pt-pt interfaces if there are
13060 				 * other interfaces.
13061 				 */
13062 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13063 					if (fallback_ipif == NULL) {
13064 						ipif_refhold_locked(ipif);
13065 						fallback_ipif = ipif;
13066 					}
13067 					continue;
13068 				}
13069 
13070 				/*
13071 				 * Fine. Just assume the same net mask as the
13072 				 * directly attached subnet interface is using.
13073 				 */
13074 				ipif_refhold_locked(ipif);
13075 				mutex_exit(&ill->ill_lock);
13076 				rw_exit(&ipst->ips_ill_g_lock);
13077 				if (fallback_ipif != NULL)
13078 					ipif_refrele(fallback_ipif);
13079 				*ipifp = ipif;
13080 				return (ipif->ipif_net_mask);
13081 			}
13082 		}
13083 		mutex_exit(&ill->ill_lock);
13084 	}
13085 	rw_exit(&ipst->ips_ill_g_lock);
13086 
13087 	*ipifp = fallback_ipif;
13088 	return ((fallback_ipif != NULL) ?
13089 	    fallback_ipif->ipif_net_mask : net_mask);
13090 }
13091 
13092 /*
13093  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13094  */
13095 static void
13096 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13097 {
13098 	IOCP	iocp;
13099 	ipft_t	*ipft;
13100 	ipllc_t	*ipllc;
13101 	mblk_t	*mp1;
13102 	cred_t	*cr;
13103 	int	error = 0;
13104 	conn_t	*connp;
13105 
13106 	ip1dbg(("ip_wput_ioctl"));
13107 	iocp = (IOCP)mp->b_rptr;
13108 	mp1 = mp->b_cont;
13109 	if (mp1 == NULL) {
13110 		iocp->ioc_error = EINVAL;
13111 		mp->b_datap->db_type = M_IOCNAK;
13112 		iocp->ioc_count = 0;
13113 		qreply(q, mp);
13114 		return;
13115 	}
13116 
13117 	/*
13118 	 * These IOCTLs provide various control capabilities to
13119 	 * upstream agents such as ULPs and processes.	There
13120 	 * are currently two such IOCTLs implemented.  They
13121 	 * are used by TCP to provide update information for
13122 	 * existing IREs and to forcibly delete an IRE for a
13123 	 * host that is not responding, thereby forcing an
13124 	 * attempt at a new route.
13125 	 */
13126 	iocp->ioc_error = EINVAL;
13127 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13128 		goto done;
13129 
13130 	ipllc = (ipllc_t *)mp1->b_rptr;
13131 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13132 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13133 			break;
13134 	}
13135 	/*
13136 	 * prefer credential from mblk over ioctl;
13137 	 * see ip_sioctl_copyin_setup
13138 	 */
13139 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13140 
13141 	/*
13142 	 * Refhold the conn in case the request gets queued up in some lookup
13143 	 */
13144 	ASSERT(CONN_Q(q));
13145 	connp = Q_TO_CONN(q);
13146 	CONN_INC_REF(connp);
13147 	if (ipft->ipft_pfi &&
13148 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13149 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13150 		error = (*ipft->ipft_pfi)(q,
13151 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13152 	}
13153 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13154 		/*
13155 		 * CONN_OPER_PENDING_DONE happens in the function called
13156 		 * through ipft_pfi above.
13157 		 */
13158 		return;
13159 	}
13160 
13161 	CONN_OPER_PENDING_DONE(connp);
13162 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13163 		freemsg(mp);
13164 		return;
13165 	}
13166 	iocp->ioc_error = error;
13167 
13168 done:
13169 	mp->b_datap->db_type = M_IOCACK;
13170 	if (iocp->ioc_error)
13171 		iocp->ioc_count = 0;
13172 	qreply(q, mp);
13173 }
13174 
13175 /*
13176  * Lookup an ipif using the sequence id (ipif_seqid)
13177  */
13178 ipif_t *
13179 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13180 {
13181 	ipif_t *ipif;
13182 
13183 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13184 
13185 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13186 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13187 			return (ipif);
13188 	}
13189 	return (NULL);
13190 }
13191 
13192 /*
13193  * Assign a unique id for the ipif. This is used later when we send
13194  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13195  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13196  * IRE is added, we verify that ipif has not disappeared.
13197  */
13198 
13199 static void
13200 ipif_assign_seqid(ipif_t *ipif)
13201 {
13202 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13203 
13204 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13205 }
13206 
13207 /*
13208  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13209  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13210  * be inserted into the first space available in the list. The value of
13211  * ipif_id will then be set to the appropriate value for its position.
13212  */
13213 static int
13214 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13215 {
13216 	ill_t *ill;
13217 	ipif_t *tipif;
13218 	ipif_t **tipifp;
13219 	int id;
13220 	ip_stack_t	*ipst;
13221 
13222 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13223 	    IAM_WRITER_IPIF(ipif));
13224 
13225 	ill = ipif->ipif_ill;
13226 	ASSERT(ill != NULL);
13227 	ipst = ill->ill_ipst;
13228 
13229 	/*
13230 	 * In the case of lo0:0 we already hold the ill_g_lock.
13231 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13232 	 * ipif_insert. Another such caller is ipif_move.
13233 	 */
13234 	if (acquire_g_lock)
13235 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13236 	if (acquire_ill_lock)
13237 		mutex_enter(&ill->ill_lock);
13238 	id = ipif->ipif_id;
13239 	tipifp = &(ill->ill_ipif);
13240 	if (id == -1) {	/* need to find a real id */
13241 		id = 0;
13242 		while ((tipif = *tipifp) != NULL) {
13243 			ASSERT(tipif->ipif_id >= id);
13244 			if (tipif->ipif_id != id)
13245 				break; /* non-consecutive id */
13246 			id++;
13247 			tipifp = &(tipif->ipif_next);
13248 		}
13249 		/* limit number of logical interfaces */
13250 		if (id >= ipst->ips_ip_addrs_per_if) {
13251 			if (acquire_ill_lock)
13252 				mutex_exit(&ill->ill_lock);
13253 			if (acquire_g_lock)
13254 				rw_exit(&ipst->ips_ill_g_lock);
13255 			return (-1);
13256 		}
13257 		ipif->ipif_id = id; /* assign new id */
13258 	} else if (id < ipst->ips_ip_addrs_per_if) {
13259 		/* we have a real id; insert ipif in the right place */
13260 		while ((tipif = *tipifp) != NULL) {
13261 			ASSERT(tipif->ipif_id != id);
13262 			if (tipif->ipif_id > id)
13263 				break; /* found correct location */
13264 			tipifp = &(tipif->ipif_next);
13265 		}
13266 	} else {
13267 		if (acquire_ill_lock)
13268 			mutex_exit(&ill->ill_lock);
13269 		if (acquire_g_lock)
13270 			rw_exit(&ipst->ips_ill_g_lock);
13271 		return (-1);
13272 	}
13273 
13274 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13275 
13276 	ipif->ipif_next = tipif;
13277 	*tipifp = ipif;
13278 	if (acquire_ill_lock)
13279 		mutex_exit(&ill->ill_lock);
13280 	if (acquire_g_lock)
13281 		rw_exit(&ipst->ips_ill_g_lock);
13282 	return (0);
13283 }
13284 
13285 static void
13286 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13287 {
13288 	ipif_t	**ipifp;
13289 	ill_t	*ill = ipif->ipif_ill;
13290 
13291 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13292 	if (acquire_ill_lock)
13293 		mutex_enter(&ill->ill_lock);
13294 	else
13295 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13296 
13297 	ipifp = &ill->ill_ipif;
13298 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13299 		if (*ipifp == ipif) {
13300 			*ipifp = ipif->ipif_next;
13301 			break;
13302 		}
13303 	}
13304 
13305 	if (acquire_ill_lock)
13306 		mutex_exit(&ill->ill_lock);
13307 }
13308 
13309 /*
13310  * Allocate and initialize a new interface control structure.  (Always
13311  * called as writer.)
13312  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13313  * is not part of the global linked list of ills. ipif_seqid is unique
13314  * in the system and to preserve the uniqueness, it is assigned only
13315  * when ill becomes part of the global list. At that point ill will
13316  * have a name. If it doesn't get assigned here, it will get assigned
13317  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13318  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13319  * the interface flags or any other information from the DL_INFO_ACK for
13320  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13321  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13322  * second DL_INFO_ACK comes in from the driver.
13323  */
13324 static ipif_t *
13325 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13326 {
13327 	ipif_t	*ipif;
13328 	phyint_t *phyi;
13329 
13330 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13331 	    ill->ill_name, id, (void *)ill));
13332 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13333 
13334 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13335 		return (NULL);
13336 	*ipif = ipif_zero;	/* start clean */
13337 
13338 	ipif->ipif_ill = ill;
13339 	ipif->ipif_id = id;	/* could be -1 */
13340 	/*
13341 	 * Inherit the zoneid from the ill; for the shared stack instance
13342 	 * this is always the global zone
13343 	 */
13344 	ipif->ipif_zoneid = ill->ill_zoneid;
13345 
13346 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13347 
13348 	ipif->ipif_refcnt = 0;
13349 	ipif->ipif_saved_ire_cnt = 0;
13350 
13351 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13352 		mi_free(ipif);
13353 		return (NULL);
13354 	}
13355 	/* -1 id should have been replaced by real id */
13356 	id = ipif->ipif_id;
13357 	ASSERT(id >= 0);
13358 
13359 	if (ill->ill_name[0] != '\0')
13360 		ipif_assign_seqid(ipif);
13361 
13362 	/*
13363 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13364 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13365 	 * ioctl sets ipif_orig_ipifid to zero.
13366 	 */
13367 	ipif->ipif_orig_ipifid = id;
13368 
13369 	/*
13370 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13371 	 * The ipif is still not up and can't be looked up until the
13372 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13373 	 */
13374 	mutex_enter(&ill->ill_lock);
13375 	mutex_enter(&ill->ill_phyint->phyint_lock);
13376 	/*
13377 	 * Set the running flag when logical interface zero is created.
13378 	 * For subsequent logical interfaces, a DLPI link down
13379 	 * notification message may have cleared the running flag to
13380 	 * indicate the link is down, so we shouldn't just blindly set it.
13381 	 */
13382 	if (id == 0)
13383 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13384 	ipif->ipif_ire_type = ire_type;
13385 	phyi = ill->ill_phyint;
13386 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13387 
13388 	if (ipif->ipif_isv6) {
13389 		ill->ill_flags |= ILLF_IPV6;
13390 	} else {
13391 		ipaddr_t inaddr_any = INADDR_ANY;
13392 
13393 		ill->ill_flags |= ILLF_IPV4;
13394 
13395 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13396 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13397 		    &ipif->ipif_v6lcl_addr);
13398 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13399 		    &ipif->ipif_v6src_addr);
13400 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13401 		    &ipif->ipif_v6subnet);
13402 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13403 		    &ipif->ipif_v6net_mask);
13404 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13405 		    &ipif->ipif_v6brd_addr);
13406 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13407 		    &ipif->ipif_v6pp_dst_addr);
13408 	}
13409 
13410 	/*
13411 	 * Don't set the interface flags etc. now, will do it in
13412 	 * ip_ll_subnet_defaults.
13413 	 */
13414 	if (!initialize) {
13415 		mutex_exit(&ill->ill_lock);
13416 		mutex_exit(&ill->ill_phyint->phyint_lock);
13417 		return (ipif);
13418 	}
13419 	ipif->ipif_mtu = ill->ill_max_mtu;
13420 
13421 	if (ill->ill_bcast_addr_length != 0) {
13422 		/*
13423 		 * Later detect lack of DLPI driver multicast
13424 		 * capability by catching DL_ENABMULTI errors in
13425 		 * ip_rput_dlpi.
13426 		 */
13427 		ill->ill_flags |= ILLF_MULTICAST;
13428 		if (!ipif->ipif_isv6)
13429 			ipif->ipif_flags |= IPIF_BROADCAST;
13430 	} else {
13431 		if (ill->ill_net_type != IRE_LOOPBACK) {
13432 			if (ipif->ipif_isv6)
13433 				/*
13434 				 * Note: xresolv interfaces will eventually need
13435 				 * NOARP set here as well, but that will require
13436 				 * those external resolvers to have some
13437 				 * knowledge of that flag and act appropriately.
13438 				 * Not to be changed at present.
13439 				 */
13440 				ill->ill_flags |= ILLF_NONUD;
13441 			else
13442 				ill->ill_flags |= ILLF_NOARP;
13443 		}
13444 		if (ill->ill_phys_addr_length == 0) {
13445 			if (ill->ill_media &&
13446 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13447 				ipif->ipif_flags |= IPIF_NOXMIT;
13448 				phyi->phyint_flags |= PHYI_VIRTUAL;
13449 			} else {
13450 				/* pt-pt supports multicast. */
13451 				ill->ill_flags |= ILLF_MULTICAST;
13452 				if (ill->ill_net_type == IRE_LOOPBACK) {
13453 					phyi->phyint_flags |=
13454 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13455 				} else {
13456 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13457 				}
13458 			}
13459 		}
13460 	}
13461 	mutex_exit(&ill->ill_lock);
13462 	mutex_exit(&ill->ill_phyint->phyint_lock);
13463 	return (ipif);
13464 }
13465 
13466 /*
13467  * If appropriate, send a message up to the resolver delete the entry
13468  * for the address of this interface which is going out of business.
13469  * (Always called as writer).
13470  *
13471  * NOTE : We need to check for NULL mps as some of the fields are
13472  *	  initialized only for some interface types. See ipif_resolver_up()
13473  *	  for details.
13474  */
13475 void
13476 ipif_arp_down(ipif_t *ipif)
13477 {
13478 	mblk_t	*mp;
13479 	ill_t	*ill = ipif->ipif_ill;
13480 
13481 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13482 	ASSERT(IAM_WRITER_IPIF(ipif));
13483 
13484 	/* Delete the mapping for the local address */
13485 	mp = ipif->ipif_arp_del_mp;
13486 	if (mp != NULL) {
13487 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13488 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13489 		putnext(ill->ill_rq, mp);
13490 		ipif->ipif_arp_del_mp = NULL;
13491 	}
13492 
13493 	/*
13494 	 * If this is the last ipif that is going down and there are no
13495 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13496 	 * clean up ARP completely.
13497 	 */
13498 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13499 
13500 		/* Send up AR_INTERFACE_DOWN message */
13501 		mp = ill->ill_arp_down_mp;
13502 		if (mp != NULL) {
13503 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13504 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13505 			    ipif->ipif_id));
13506 			putnext(ill->ill_rq, mp);
13507 			ill->ill_arp_down_mp = NULL;
13508 		}
13509 
13510 		/* Tell ARP to delete the multicast mappings */
13511 		mp = ill->ill_arp_del_mapping_mp;
13512 		if (mp != NULL) {
13513 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13514 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13515 			    ipif->ipif_id));
13516 			putnext(ill->ill_rq, mp);
13517 			ill->ill_arp_del_mapping_mp = NULL;
13518 		}
13519 	}
13520 }
13521 
13522 /*
13523  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13524  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13525  * that it wants the add_mp allocated in this function to be returned
13526  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13527  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13528  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13529  * as it does a ipif_arp_down after calling this function - which will
13530  * remove what we add here.
13531  *
13532  * Returns -1 on failures and 0 on success.
13533  */
13534 int
13535 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13536 {
13537 	mblk_t	*del_mp = NULL;
13538 	mblk_t *add_mp = NULL;
13539 	mblk_t *mp;
13540 	ill_t	*ill = ipif->ipif_ill;
13541 	phyint_t *phyi = ill->ill_phyint;
13542 	ipaddr_t addr, mask, extract_mask = 0;
13543 	arma_t	*arma;
13544 	uint8_t *maddr, *bphys_addr;
13545 	uint32_t hw_start;
13546 	dl_unitdata_req_t *dlur;
13547 
13548 	ASSERT(IAM_WRITER_IPIF(ipif));
13549 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13550 		return (0);
13551 
13552 	/*
13553 	 * Delete the existing mapping from ARP. Normally ipif_down
13554 	 * -> ipif_arp_down should send this up to ARP. The only
13555 	 * reason we would find this when we are switching from
13556 	 * Multicast to Broadcast where we did not do a down.
13557 	 */
13558 	mp = ill->ill_arp_del_mapping_mp;
13559 	if (mp != NULL) {
13560 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13561 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13562 		putnext(ill->ill_rq, mp);
13563 		ill->ill_arp_del_mapping_mp = NULL;
13564 	}
13565 
13566 	if (arp_add_mapping_mp != NULL)
13567 		*arp_add_mapping_mp = NULL;
13568 
13569 	/*
13570 	 * Check that the address is not to long for the constant
13571 	 * length reserved in the template arma_t.
13572 	 */
13573 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13574 		return (-1);
13575 
13576 	/* Add mapping mblk */
13577 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13578 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13579 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13580 	    (caddr_t)&addr);
13581 	if (add_mp == NULL)
13582 		return (-1);
13583 	arma = (arma_t *)add_mp->b_rptr;
13584 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13585 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13586 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13587 
13588 	/*
13589 	 * Determine the broadcast address.
13590 	 */
13591 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13592 	if (ill->ill_sap_length < 0)
13593 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13594 	else
13595 		bphys_addr = (uchar_t *)dlur +
13596 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13597 	/*
13598 	 * Check PHYI_MULTI_BCAST and length of physical
13599 	 * address to determine if we use the mapping or the
13600 	 * broadcast address.
13601 	 */
13602 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13603 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13604 		    bphys_addr, maddr, &hw_start, &extract_mask))
13605 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13606 
13607 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13608 	    (ill->ill_flags & ILLF_MULTICAST)) {
13609 		/* Make sure this will not match the "exact" entry. */
13610 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13611 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13612 		    (caddr_t)&addr);
13613 		if (del_mp == NULL) {
13614 			freemsg(add_mp);
13615 			return (-1);
13616 		}
13617 		bcopy(&extract_mask, (char *)arma +
13618 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13619 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13620 			/* Use link-layer broadcast address for MULTI_BCAST */
13621 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13622 			ip2dbg(("ipif_arp_setup_multicast: adding"
13623 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13624 		} else {
13625 			arma->arma_hw_mapping_start = hw_start;
13626 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13627 			    " ARP setup for %s\n", ill->ill_name));
13628 		}
13629 	} else {
13630 		freemsg(add_mp);
13631 		ASSERT(del_mp == NULL);
13632 		/* It is neither MULTICAST nor MULTI_BCAST */
13633 		return (0);
13634 	}
13635 	ASSERT(add_mp != NULL && del_mp != NULL);
13636 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13637 	ill->ill_arp_del_mapping_mp = del_mp;
13638 	if (arp_add_mapping_mp != NULL) {
13639 		/* The caller just wants the mblks allocated */
13640 		*arp_add_mapping_mp = add_mp;
13641 	} else {
13642 		/* The caller wants us to send it to arp */
13643 		putnext(ill->ill_rq, add_mp);
13644 	}
13645 	return (0);
13646 }
13647 
13648 /*
13649  * Get the resolver set up for a new interface address.
13650  * (Always called as writer.)
13651  * Called both for IPv4 and IPv6 interfaces,
13652  * though it only sets up the resolver for v6
13653  * if it's an xresolv interface (one using an external resolver).
13654  * Honors ILLF_NOARP.
13655  * The enumerated value res_act is used to tune the behavior.
13656  * If set to Res_act_initial, then we set up all the resolver
13657  * structures for a new interface.  If set to Res_act_move, then
13658  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13659  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13660  * asynchronous hardware address change notification.  If set to
13661  * Res_act_defend, then we tell ARP that it needs to send a single
13662  * gratuitous message in defense of the address.
13663  * Returns error on failure.
13664  */
13665 int
13666 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13667 {
13668 	caddr_t	addr;
13669 	mblk_t	*arp_up_mp = NULL;
13670 	mblk_t	*arp_down_mp = NULL;
13671 	mblk_t	*arp_add_mp = NULL;
13672 	mblk_t	*arp_del_mp = NULL;
13673 	mblk_t	*arp_add_mapping_mp = NULL;
13674 	mblk_t	*arp_del_mapping_mp = NULL;
13675 	ill_t	*ill = ipif->ipif_ill;
13676 	uchar_t	*area_p = NULL;
13677 	uchar_t	*ared_p = NULL;
13678 	int	err = ENOMEM;
13679 	boolean_t was_dup;
13680 
13681 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13682 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13683 	ASSERT(IAM_WRITER_IPIF(ipif));
13684 
13685 	was_dup = B_FALSE;
13686 	if (res_act == Res_act_initial) {
13687 		ipif->ipif_addr_ready = 0;
13688 		/*
13689 		 * We're bringing an interface up here.  There's no way that we
13690 		 * should need to shut down ARP now.
13691 		 */
13692 		mutex_enter(&ill->ill_lock);
13693 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13694 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13695 			ill->ill_ipif_dup_count--;
13696 			was_dup = B_TRUE;
13697 		}
13698 		mutex_exit(&ill->ill_lock);
13699 	}
13700 	if (ipif->ipif_recovery_id != 0)
13701 		(void) untimeout(ipif->ipif_recovery_id);
13702 	ipif->ipif_recovery_id = 0;
13703 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13704 		ipif->ipif_addr_ready = 1;
13705 		return (0);
13706 	}
13707 	/* NDP will set the ipif_addr_ready flag when it's ready */
13708 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13709 		return (0);
13710 
13711 	if (ill->ill_isv6) {
13712 		/*
13713 		 * External resolver for IPv6
13714 		 */
13715 		ASSERT(res_act == Res_act_initial);
13716 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13717 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13718 			area_p = (uchar_t *)&ip6_area_template;
13719 			ared_p = (uchar_t *)&ip6_ared_template;
13720 		}
13721 	} else {
13722 		/*
13723 		 * IPv4 arp case. If the ARP stream has already started
13724 		 * closing, fail this request for ARP bringup. Else
13725 		 * record the fact that an ARP bringup is pending.
13726 		 */
13727 		mutex_enter(&ill->ill_lock);
13728 		if (ill->ill_arp_closing) {
13729 			mutex_exit(&ill->ill_lock);
13730 			err = EINVAL;
13731 			goto failed;
13732 		} else {
13733 			if (ill->ill_ipif_up_count == 0 &&
13734 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13735 				ill->ill_arp_bringup_pending = 1;
13736 			mutex_exit(&ill->ill_lock);
13737 		}
13738 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13739 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13740 			area_p = (uchar_t *)&ip_area_template;
13741 			ared_p = (uchar_t *)&ip_ared_template;
13742 		}
13743 	}
13744 
13745 	/*
13746 	 * Add an entry for the local address in ARP only if it
13747 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13748 	 */
13749 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13750 		area_t *area;
13751 
13752 		/* Now ask ARP to publish our address. */
13753 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13754 		if (arp_add_mp == NULL)
13755 			goto failed;
13756 		area = (area_t *)arp_add_mp->b_rptr;
13757 		if (res_act != Res_act_initial) {
13758 			/*
13759 			 * Copy the new hardware address and length into
13760 			 * arp_add_mp to be sent to ARP.
13761 			 */
13762 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13763 			bcopy(ill->ill_phys_addr,
13764 			    ((char *)area + area->area_hw_addr_offset),
13765 			    area->area_hw_addr_length);
13766 		}
13767 
13768 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13769 		    ACE_F_MYADDR;
13770 
13771 		if (res_act == Res_act_defend) {
13772 			area->area_flags |= ACE_F_DEFEND;
13773 			/*
13774 			 * If we're just defending our address now, then
13775 			 * there's no need to set up ARP multicast mappings.
13776 			 * The publish command is enough.
13777 			 */
13778 			goto done;
13779 		}
13780 
13781 		if (res_act != Res_act_initial)
13782 			goto arp_setup_multicast;
13783 
13784 		/*
13785 		 * Allocate an ARP deletion message so we know we can tell ARP
13786 		 * when the interface goes down.
13787 		 */
13788 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13789 		if (arp_del_mp == NULL)
13790 			goto failed;
13791 
13792 	} else {
13793 		if (res_act != Res_act_initial)
13794 			goto done;
13795 	}
13796 	/*
13797 	 * Need to bring up ARP or setup multicast mapping only
13798 	 * when the first interface is coming UP.
13799 	 */
13800 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13801 	    was_dup) {
13802 		goto done;
13803 	}
13804 
13805 	/*
13806 	 * Allocate an ARP down message (to be saved) and an ARP up
13807 	 * message.
13808 	 */
13809 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13810 	if (arp_down_mp == NULL)
13811 		goto failed;
13812 
13813 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13814 	if (arp_up_mp == NULL)
13815 		goto failed;
13816 
13817 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13818 		goto done;
13819 
13820 arp_setup_multicast:
13821 	/*
13822 	 * Setup the multicast mappings. This function initializes
13823 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13824 	 * IPv6.
13825 	 */
13826 	if (!ill->ill_isv6) {
13827 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13828 		if (err != 0)
13829 			goto failed;
13830 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13831 		ASSERT(arp_add_mapping_mp != NULL);
13832 	}
13833 
13834 done:
13835 	if (arp_del_mp != NULL) {
13836 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13837 		ipif->ipif_arp_del_mp = arp_del_mp;
13838 	}
13839 	if (arp_down_mp != NULL) {
13840 		ASSERT(ill->ill_arp_down_mp == NULL);
13841 		ill->ill_arp_down_mp = arp_down_mp;
13842 	}
13843 	if (arp_del_mapping_mp != NULL) {
13844 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13845 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13846 	}
13847 	if (arp_up_mp != NULL) {
13848 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13849 		    ill->ill_name, ipif->ipif_id));
13850 		putnext(ill->ill_rq, arp_up_mp);
13851 	}
13852 	if (arp_add_mp != NULL) {
13853 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13854 		    ill->ill_name, ipif->ipif_id));
13855 		/*
13856 		 * If it's an extended ARP implementation, then we'll wait to
13857 		 * hear that DAD has finished before using the interface.
13858 		 */
13859 		if (!ill->ill_arp_extend)
13860 			ipif->ipif_addr_ready = 1;
13861 		putnext(ill->ill_rq, arp_add_mp);
13862 	} else {
13863 		ipif->ipif_addr_ready = 1;
13864 	}
13865 	if (arp_add_mapping_mp != NULL) {
13866 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13867 		    ill->ill_name, ipif->ipif_id));
13868 		putnext(ill->ill_rq, arp_add_mapping_mp);
13869 	}
13870 	if (res_act != Res_act_initial)
13871 		return (0);
13872 
13873 	if (ill->ill_flags & ILLF_NOARP)
13874 		err = ill_arp_off(ill);
13875 	else
13876 		err = ill_arp_on(ill);
13877 	if (err != 0) {
13878 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13879 		freemsg(ipif->ipif_arp_del_mp);
13880 		freemsg(ill->ill_arp_down_mp);
13881 		freemsg(ill->ill_arp_del_mapping_mp);
13882 		ipif->ipif_arp_del_mp = NULL;
13883 		ill->ill_arp_down_mp = NULL;
13884 		ill->ill_arp_del_mapping_mp = NULL;
13885 		return (err);
13886 	}
13887 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13888 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13889 
13890 failed:
13891 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13892 	freemsg(arp_add_mp);
13893 	freemsg(arp_del_mp);
13894 	freemsg(arp_add_mapping_mp);
13895 	freemsg(arp_up_mp);
13896 	freemsg(arp_down_mp);
13897 	ill->ill_arp_bringup_pending = 0;
13898 	return (err);
13899 }
13900 
13901 /*
13902  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13903  * just gone back up.
13904  */
13905 static void
13906 ipif_arp_start_dad(ipif_t *ipif)
13907 {
13908 	ill_t *ill = ipif->ipif_ill;
13909 	mblk_t *arp_add_mp;
13910 	area_t *area;
13911 
13912 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13913 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13914 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13915 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13916 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13917 		/*
13918 		 * If we can't contact ARP for some reason, that's not really a
13919 		 * problem.  Just send out the routing socket notification that
13920 		 * DAD completion would have done, and continue.
13921 		 */
13922 		ipif_mask_reply(ipif);
13923 		ip_rts_ifmsg(ipif);
13924 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13925 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13926 		ipif->ipif_addr_ready = 1;
13927 		return;
13928 	}
13929 
13930 	/* Setting the 'unverified' flag restarts DAD */
13931 	area = (area_t *)arp_add_mp->b_rptr;
13932 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13933 	    ACE_F_UNVERIFIED;
13934 	putnext(ill->ill_rq, arp_add_mp);
13935 }
13936 
13937 static void
13938 ipif_ndp_start_dad(ipif_t *ipif)
13939 {
13940 	nce_t *nce;
13941 
13942 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13943 	if (nce == NULL)
13944 		return;
13945 
13946 	if (!ndp_restart_dad(nce)) {
13947 		/*
13948 		 * If we can't restart DAD for some reason, that's not really a
13949 		 * problem.  Just send out the routing socket notification that
13950 		 * DAD completion would have done, and continue.
13951 		 */
13952 		ip_rts_ifmsg(ipif);
13953 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13954 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13955 		ipif->ipif_addr_ready = 1;
13956 	}
13957 	NCE_REFRELE(nce);
13958 }
13959 
13960 /*
13961  * Restart duplicate address detection on all interfaces on the given ill.
13962  *
13963  * This is called when an interface transitions from down to up
13964  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13965  *
13966  * Note that since the underlying physical link has transitioned, we must cause
13967  * at least one routing socket message to be sent here, either via DAD
13968  * completion or just by default on the first ipif.  (If we don't do this, then
13969  * in.mpathd will see long delays when doing link-based failure recovery.)
13970  */
13971 void
13972 ill_restart_dad(ill_t *ill, boolean_t went_up)
13973 {
13974 	ipif_t *ipif;
13975 
13976 	if (ill == NULL)
13977 		return;
13978 
13979 	/*
13980 	 * If layer two doesn't support duplicate address detection, then just
13981 	 * send the routing socket message now and be done with it.
13982 	 */
13983 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13984 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13985 		ip_rts_ifmsg(ill->ill_ipif);
13986 		return;
13987 	}
13988 
13989 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13990 		if (went_up) {
13991 			if (ipif->ipif_flags & IPIF_UP) {
13992 				if (ill->ill_isv6)
13993 					ipif_ndp_start_dad(ipif);
13994 				else
13995 					ipif_arp_start_dad(ipif);
13996 			} else if (ill->ill_isv6 &&
13997 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13998 				/*
13999 				 * For IPv4, the ARP module itself will
14000 				 * automatically start the DAD process when it
14001 				 * sees DL_NOTE_LINK_UP.  We respond to the
14002 				 * AR_CN_READY at the completion of that task.
14003 				 * For IPv6, we must kick off the bring-up
14004 				 * process now.
14005 				 */
14006 				ndp_do_recovery(ipif);
14007 			} else {
14008 				/*
14009 				 * Unfortunately, the first ipif is "special"
14010 				 * and represents the underlying ill in the
14011 				 * routing socket messages.  Thus, when this
14012 				 * one ipif is down, we must still notify so
14013 				 * that the user knows the IFF_RUNNING status
14014 				 * change.  (If the first ipif is up, then
14015 				 * we'll handle eventual routing socket
14016 				 * notification via DAD completion.)
14017 				 */
14018 				if (ipif == ill->ill_ipif)
14019 					ip_rts_ifmsg(ill->ill_ipif);
14020 			}
14021 		} else {
14022 			/*
14023 			 * After link down, we'll need to send a new routing
14024 			 * message when the link comes back, so clear
14025 			 * ipif_addr_ready.
14026 			 */
14027 			ipif->ipif_addr_ready = 0;
14028 		}
14029 	}
14030 
14031 	/*
14032 	 * If we've torn down links, then notify the user right away.
14033 	 */
14034 	if (!went_up)
14035 		ip_rts_ifmsg(ill->ill_ipif);
14036 }
14037 
14038 /*
14039  * Wakeup all threads waiting to enter the ipsq, and sleeping
14040  * on any of the ills in this ipsq. The ill_lock of the ill
14041  * must be held so that waiters don't miss wakeups
14042  */
14043 static void
14044 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14045 {
14046 	phyint_t *phyint;
14047 
14048 	phyint = ipsq->ipsq_phyint_list;
14049 	while (phyint != NULL) {
14050 		if (phyint->phyint_illv4) {
14051 			if (!caller_holds_lock)
14052 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14053 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14054 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14055 			if (!caller_holds_lock)
14056 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14057 		}
14058 		if (phyint->phyint_illv6) {
14059 			if (!caller_holds_lock)
14060 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14061 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14062 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14063 			if (!caller_holds_lock)
14064 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14065 		}
14066 		phyint = phyint->phyint_ipsq_next;
14067 	}
14068 }
14069 
14070 static ipsq_t *
14071 ipsq_create(char *groupname, ip_stack_t *ipst)
14072 {
14073 	ipsq_t	*ipsq;
14074 
14075 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14076 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14077 	if (ipsq == NULL) {
14078 		return (NULL);
14079 	}
14080 
14081 	if (groupname != NULL)
14082 		(void) strcpy(ipsq->ipsq_name, groupname);
14083 	else
14084 		ipsq->ipsq_name[0] = '\0';
14085 
14086 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14087 	ipsq->ipsq_flags |= IPSQ_GROUP;
14088 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14089 	ipst->ips_ipsq_g_head = ipsq;
14090 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14091 	return (ipsq);
14092 }
14093 
14094 /*
14095  * Return an ipsq correspoding to the groupname. If 'create' is true
14096  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14097  * uniquely with an IPMP group. However during IPMP groupname operations,
14098  * multiple IPMP groups may be associated with a single ipsq. But no
14099  * IPMP group can be associated with more than 1 ipsq at any time.
14100  * For example
14101  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14102  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14103  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14104  *
14105  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14106  * status shown below during the execution of the above command.
14107  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14108  *
14109  * After the completion of the above groupname command we return to the stable
14110  * state shown below.
14111  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14112  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14113  *
14114  * Because of the above, we don't search based on the ipsq_name since that
14115  * would miss the correct ipsq during certain windows as shown above.
14116  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14117  * natural state.
14118  */
14119 static ipsq_t *
14120 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14121     ip_stack_t *ipst)
14122 {
14123 	ipsq_t	*ipsq;
14124 	int	group_len;
14125 	phyint_t *phyint;
14126 
14127 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14128 
14129 	group_len = strlen(groupname);
14130 	ASSERT(group_len != 0);
14131 	group_len++;
14132 
14133 	for (ipsq = ipst->ips_ipsq_g_head;
14134 	    ipsq != NULL;
14135 	    ipsq = ipsq->ipsq_next) {
14136 		/*
14137 		 * When an ipsq is being split, and ill_split_ipsq
14138 		 * calls this function, we exclude it from being considered.
14139 		 */
14140 		if (ipsq == exclude_ipsq)
14141 			continue;
14142 
14143 		/*
14144 		 * Compare against the ipsq_name. The groupname change happens
14145 		 * in 2 phases. The 1st phase merges the from group into
14146 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14147 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14148 		 * ipsq_name. At this point the phyint_groupname has not been
14149 		 * updated.
14150 		 */
14151 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14152 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14153 			/*
14154 			 * Verify that an ipmp groupname is exactly
14155 			 * part of 1 ipsq and is not found in any other
14156 			 * ipsq.
14157 			 */
14158 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14159 			    NULL);
14160 			return (ipsq);
14161 		}
14162 
14163 		/*
14164 		 * Comparison against ipsq_name alone is not sufficient.
14165 		 * In the case when groups are currently being
14166 		 * merged, the ipsq could hold other IPMP groups temporarily.
14167 		 * so we walk the phyint list and compare against the
14168 		 * phyint_groupname as well.
14169 		 */
14170 		phyint = ipsq->ipsq_phyint_list;
14171 		while (phyint != NULL) {
14172 			if ((group_len == phyint->phyint_groupname_len) &&
14173 			    (bcmp(phyint->phyint_groupname, groupname,
14174 			    group_len) == 0)) {
14175 				/*
14176 				 * Verify that an ipmp groupname is exactly
14177 				 * part of 1 ipsq and is not found in any other
14178 				 * ipsq.
14179 				 */
14180 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14181 				    ipst) == NULL);
14182 				return (ipsq);
14183 			}
14184 			phyint = phyint->phyint_ipsq_next;
14185 		}
14186 	}
14187 	if (create)
14188 		ipsq = ipsq_create(groupname, ipst);
14189 	return (ipsq);
14190 }
14191 
14192 static void
14193 ipsq_delete(ipsq_t *ipsq)
14194 {
14195 	ipsq_t *nipsq;
14196 	ipsq_t *pipsq = NULL;
14197 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14198 
14199 	/*
14200 	 * We don't hold the ipsq lock, but we are sure no new
14201 	 * messages can land up, since the ipsq_refs is zero.
14202 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14203 	 * is associated with this ipsq. (Lookups are based on ill_name
14204 	 * or phyint_groupname)
14205 	 */
14206 	ASSERT(ipsq->ipsq_refs == 0);
14207 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14208 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14209 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14210 		/*
14211 		 * This is not the ipsq of an IPMP group.
14212 		 */
14213 		ipsq->ipsq_ipst = NULL;
14214 		kmem_free(ipsq, sizeof (ipsq_t));
14215 		return;
14216 	}
14217 
14218 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14219 
14220 	/*
14221 	 * Locate the ipsq  before we can remove it from
14222 	 * the singly linked list of ipsq's.
14223 	 */
14224 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14225 	    nipsq = nipsq->ipsq_next) {
14226 		if (nipsq == ipsq) {
14227 			break;
14228 		}
14229 		pipsq = nipsq;
14230 	}
14231 
14232 	ASSERT(nipsq == ipsq);
14233 
14234 	/* unlink ipsq from the list */
14235 	if (pipsq != NULL)
14236 		pipsq->ipsq_next = ipsq->ipsq_next;
14237 	else
14238 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14239 	ipsq->ipsq_ipst = NULL;
14240 	kmem_free(ipsq, sizeof (ipsq_t));
14241 	rw_exit(&ipst->ips_ill_g_lock);
14242 }
14243 
14244 static void
14245 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14246     queue_t *q)
14247 {
14248 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14249 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14250 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14251 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14252 	ASSERT(current_mp != NULL);
14253 
14254 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14255 	    NEW_OP, NULL);
14256 
14257 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14258 	    new_ipsq->ipsq_xopq_mphead != NULL);
14259 
14260 	/*
14261 	 * move from old ipsq to the new ipsq.
14262 	 */
14263 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14264 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14265 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14266 
14267 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14268 }
14269 
14270 void
14271 ill_group_cleanup(ill_t *ill)
14272 {
14273 	ill_t *ill_v4;
14274 	ill_t *ill_v6;
14275 	ipif_t *ipif;
14276 
14277 	ill_v4 = ill->ill_phyint->phyint_illv4;
14278 	ill_v6 = ill->ill_phyint->phyint_illv6;
14279 
14280 	if (ill_v4 != NULL) {
14281 		mutex_enter(&ill_v4->ill_lock);
14282 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14283 		    ipif = ipif->ipif_next) {
14284 			IPIF_UNMARK_MOVING(ipif);
14285 		}
14286 		ill_v4->ill_up_ipifs = B_FALSE;
14287 		mutex_exit(&ill_v4->ill_lock);
14288 	}
14289 
14290 	if (ill_v6 != NULL) {
14291 		mutex_enter(&ill_v6->ill_lock);
14292 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14293 		    ipif = ipif->ipif_next) {
14294 			IPIF_UNMARK_MOVING(ipif);
14295 		}
14296 		ill_v6->ill_up_ipifs = B_FALSE;
14297 		mutex_exit(&ill_v6->ill_lock);
14298 	}
14299 }
14300 /*
14301  * This function is called when an ill has had a change in its group status
14302  * to bring up all the ipifs that were up before the change.
14303  */
14304 int
14305 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14306 {
14307 	ipif_t *ipif;
14308 	ill_t *ill_v4;
14309 	ill_t *ill_v6;
14310 	ill_t *from_ill;
14311 	int err = 0;
14312 
14313 	ASSERT(IAM_WRITER_ILL(ill));
14314 
14315 	/*
14316 	 * Except for ipif_state_flags and ill_state_flags the other
14317 	 * fields of the ipif/ill that are modified below are protected
14318 	 * implicitly since we are a writer. We would have tried to down
14319 	 * even an ipif that was already down, in ill_down_ipifs. So we
14320 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14321 	 */
14322 	ill_v4 = ill->ill_phyint->phyint_illv4;
14323 	ill_v6 = ill->ill_phyint->phyint_illv6;
14324 	if (ill_v4 != NULL) {
14325 		ill_v4->ill_up_ipifs = B_TRUE;
14326 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14327 		    ipif = ipif->ipif_next) {
14328 			mutex_enter(&ill_v4->ill_lock);
14329 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14330 			IPIF_UNMARK_MOVING(ipif);
14331 			mutex_exit(&ill_v4->ill_lock);
14332 			if (ipif->ipif_was_up) {
14333 				if (!(ipif->ipif_flags & IPIF_UP))
14334 					err = ipif_up(ipif, q, mp);
14335 				ipif->ipif_was_up = B_FALSE;
14336 				if (err != 0) {
14337 					/*
14338 					 * Can there be any other error ?
14339 					 */
14340 					ASSERT(err == EINPROGRESS);
14341 					return (err);
14342 				}
14343 			}
14344 		}
14345 		mutex_enter(&ill_v4->ill_lock);
14346 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14347 		mutex_exit(&ill_v4->ill_lock);
14348 		ill_v4->ill_up_ipifs = B_FALSE;
14349 		if (ill_v4->ill_move_in_progress) {
14350 			ASSERT(ill_v4->ill_move_peer != NULL);
14351 			ill_v4->ill_move_in_progress = B_FALSE;
14352 			from_ill = ill_v4->ill_move_peer;
14353 			from_ill->ill_move_in_progress = B_FALSE;
14354 			from_ill->ill_move_peer = NULL;
14355 			mutex_enter(&from_ill->ill_lock);
14356 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14357 			mutex_exit(&from_ill->ill_lock);
14358 			if (ill_v6 == NULL) {
14359 				if (from_ill->ill_phyint->phyint_flags &
14360 				    PHYI_STANDBY) {
14361 					phyint_inactive(from_ill->ill_phyint);
14362 				}
14363 				if (ill_v4->ill_phyint->phyint_flags &
14364 				    PHYI_STANDBY) {
14365 					phyint_inactive(ill_v4->ill_phyint);
14366 				}
14367 			}
14368 			ill_v4->ill_move_peer = NULL;
14369 		}
14370 	}
14371 
14372 	if (ill_v6 != NULL) {
14373 		ill_v6->ill_up_ipifs = B_TRUE;
14374 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14375 		    ipif = ipif->ipif_next) {
14376 			mutex_enter(&ill_v6->ill_lock);
14377 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14378 			IPIF_UNMARK_MOVING(ipif);
14379 			mutex_exit(&ill_v6->ill_lock);
14380 			if (ipif->ipif_was_up) {
14381 				if (!(ipif->ipif_flags & IPIF_UP))
14382 					err = ipif_up(ipif, q, mp);
14383 				ipif->ipif_was_up = B_FALSE;
14384 				if (err != 0) {
14385 					/*
14386 					 * Can there be any other error ?
14387 					 */
14388 					ASSERT(err == EINPROGRESS);
14389 					return (err);
14390 				}
14391 			}
14392 		}
14393 		mutex_enter(&ill_v6->ill_lock);
14394 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14395 		mutex_exit(&ill_v6->ill_lock);
14396 		ill_v6->ill_up_ipifs = B_FALSE;
14397 		if (ill_v6->ill_move_in_progress) {
14398 			ASSERT(ill_v6->ill_move_peer != NULL);
14399 			ill_v6->ill_move_in_progress = B_FALSE;
14400 			from_ill = ill_v6->ill_move_peer;
14401 			from_ill->ill_move_in_progress = B_FALSE;
14402 			from_ill->ill_move_peer = NULL;
14403 			mutex_enter(&from_ill->ill_lock);
14404 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14405 			mutex_exit(&from_ill->ill_lock);
14406 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14407 				phyint_inactive(from_ill->ill_phyint);
14408 			}
14409 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14410 				phyint_inactive(ill_v6->ill_phyint);
14411 			}
14412 			ill_v6->ill_move_peer = NULL;
14413 		}
14414 	}
14415 	return (0);
14416 }
14417 
14418 /*
14419  * bring down all the approriate ipifs.
14420  */
14421 /* ARGSUSED */
14422 static void
14423 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14424 {
14425 	ipif_t *ipif;
14426 
14427 	ASSERT(IAM_WRITER_ILL(ill));
14428 
14429 	/*
14430 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14431 	 * are modified below are protected implicitly since we are a writer
14432 	 */
14433 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14434 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14435 			continue;
14436 		/*
14437 		 * Don't bring down the LINK LOCAL addresses as they are tied
14438 		 * to physical interface and they don't move. Treat them as
14439 		 * IPIF_NOFAILOVER.
14440 		 */
14441 		if (chk_nofailover && ill->ill_isv6 &&
14442 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))
14443 			continue;
14444 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14445 			/*
14446 			 * We go through the ipif_down logic even if the ipif
14447 			 * is already down, since routes can be added based
14448 			 * on down ipifs. Going through ipif_down once again
14449 			 * will delete any IREs created based on these routes.
14450 			 */
14451 			if (ipif->ipif_flags & IPIF_UP)
14452 				ipif->ipif_was_up = B_TRUE;
14453 			/*
14454 			 * If called with chk_nofailover true ipif is moving.
14455 			 */
14456 			mutex_enter(&ill->ill_lock);
14457 			if (chk_nofailover) {
14458 				ipif->ipif_state_flags |=
14459 				    IPIF_MOVING | IPIF_CHANGING;
14460 			} else {
14461 				ipif->ipif_state_flags |= IPIF_CHANGING;
14462 			}
14463 			mutex_exit(&ill->ill_lock);
14464 			/*
14465 			 * Need to re-create net/subnet bcast ires if
14466 			 * they are dependent on ipif.
14467 			 */
14468 			if (!ipif->ipif_isv6)
14469 				ipif_check_bcast_ires(ipif);
14470 			(void) ipif_logical_down(ipif, NULL, NULL);
14471 			ipif_non_duplicate(ipif);
14472 			ipif_down_tail(ipif);
14473 		}
14474 	}
14475 }
14476 
14477 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14478 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14479 	(ipsq)->ipsq_refs++;				\
14480 }
14481 
14482 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14483 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14484 	(ipsq)->ipsq_refs--;				\
14485 	if ((ipsq)->ipsq_refs == 0)				\
14486 		(ipsq)->ipsq_name[0] = '\0'; 		\
14487 }
14488 
14489 /*
14490  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14491  * new_ipsq.
14492  */
14493 static void
14494 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14495 {
14496 	phyint_t *phyint;
14497 	phyint_t *next_phyint;
14498 
14499 	/*
14500 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14501 	 * writer and the ill_lock of the ill in question. Also the dest
14502 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14503 	 */
14504 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14505 
14506 	phyint = cur_ipsq->ipsq_phyint_list;
14507 	cur_ipsq->ipsq_phyint_list = NULL;
14508 	while (phyint != NULL) {
14509 		next_phyint = phyint->phyint_ipsq_next;
14510 		IPSQ_DEC_REF(cur_ipsq, ipst);
14511 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14512 		new_ipsq->ipsq_phyint_list = phyint;
14513 		IPSQ_INC_REF(new_ipsq, ipst);
14514 		phyint->phyint_ipsq = new_ipsq;
14515 		phyint = next_phyint;
14516 	}
14517 }
14518 
14519 #define	SPLIT_SUCCESS		0
14520 #define	SPLIT_NOT_NEEDED	1
14521 #define	SPLIT_FAILED		2
14522 
14523 int
14524 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14525     ip_stack_t *ipst)
14526 {
14527 	ipsq_t *newipsq = NULL;
14528 
14529 	/*
14530 	 * Assertions denote pre-requisites for changing the ipsq of
14531 	 * a phyint
14532 	 */
14533 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14534 	/*
14535 	 * <ill-phyint> assocs can't change while ill_g_lock
14536 	 * is held as writer. See ill_phyint_reinit()
14537 	 */
14538 	ASSERT(phyint->phyint_illv4 == NULL ||
14539 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14540 	ASSERT(phyint->phyint_illv6 == NULL ||
14541 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14542 
14543 	if ((phyint->phyint_groupname_len !=
14544 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14545 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14546 	    phyint->phyint_groupname_len) != 0)) {
14547 		/*
14548 		 * Once we fail in creating a new ipsq due to memory shortage,
14549 		 * don't attempt to create new ipsq again, based on another
14550 		 * phyint, since we want all phyints belonging to an IPMP group
14551 		 * to be in the same ipsq even in the event of mem alloc fails.
14552 		 */
14553 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14554 		    cur_ipsq, ipst);
14555 		if (newipsq == NULL) {
14556 			/* Memory allocation failure */
14557 			return (SPLIT_FAILED);
14558 		} else {
14559 			/* ipsq_refs protected by ill_g_lock (writer) */
14560 			IPSQ_DEC_REF(cur_ipsq, ipst);
14561 			phyint->phyint_ipsq = newipsq;
14562 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14563 			newipsq->ipsq_phyint_list = phyint;
14564 			IPSQ_INC_REF(newipsq, ipst);
14565 			return (SPLIT_SUCCESS);
14566 		}
14567 	}
14568 	return (SPLIT_NOT_NEEDED);
14569 }
14570 
14571 /*
14572  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14573  * to do this split
14574  */
14575 static int
14576 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14577 {
14578 	ipsq_t *newipsq;
14579 
14580 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14581 	/*
14582 	 * <ill-phyint> assocs can't change while ill_g_lock
14583 	 * is held as writer. See ill_phyint_reinit()
14584 	 */
14585 
14586 	ASSERT(phyint->phyint_illv4 == NULL ||
14587 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14588 	ASSERT(phyint->phyint_illv6 == NULL ||
14589 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14590 
14591 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14592 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14593 		/*
14594 		 * ipsq_init failed due to no memory
14595 		 * caller will use the same ipsq
14596 		 */
14597 		return (SPLIT_FAILED);
14598 	}
14599 
14600 	/* ipsq_ref is protected by ill_g_lock (writer) */
14601 	IPSQ_DEC_REF(cur_ipsq, ipst);
14602 
14603 	/*
14604 	 * This is a new ipsq that is unknown to the world.
14605 	 * So we don't need to hold ipsq_lock,
14606 	 */
14607 	newipsq = phyint->phyint_ipsq;
14608 	newipsq->ipsq_writer = NULL;
14609 	newipsq->ipsq_reentry_cnt--;
14610 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14611 #ifdef DEBUG
14612 	newipsq->ipsq_depth = 0;
14613 #endif
14614 
14615 	return (SPLIT_SUCCESS);
14616 }
14617 
14618 /*
14619  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14620  * ipsq's representing their individual groups or themselves. Return
14621  * whether split needs to be retried again later.
14622  */
14623 static boolean_t
14624 ill_split_ipsq(ipsq_t *cur_ipsq)
14625 {
14626 	phyint_t *phyint;
14627 	phyint_t *next_phyint;
14628 	int	error;
14629 	boolean_t need_retry = B_FALSE;
14630 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14631 
14632 	phyint = cur_ipsq->ipsq_phyint_list;
14633 	cur_ipsq->ipsq_phyint_list = NULL;
14634 	while (phyint != NULL) {
14635 		next_phyint = phyint->phyint_ipsq_next;
14636 		/*
14637 		 * 'created' will tell us whether the callee actually
14638 		 * created an ipsq. Lack of memory may force the callee
14639 		 * to return without creating an ipsq.
14640 		 */
14641 		if (phyint->phyint_groupname == NULL) {
14642 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14643 		} else {
14644 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14645 			    need_retry, ipst);
14646 		}
14647 
14648 		switch (error) {
14649 		case SPLIT_FAILED:
14650 			need_retry = B_TRUE;
14651 			/* FALLTHRU */
14652 		case SPLIT_NOT_NEEDED:
14653 			/*
14654 			 * Keep it on the list.
14655 			 */
14656 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14657 			cur_ipsq->ipsq_phyint_list = phyint;
14658 			break;
14659 		case SPLIT_SUCCESS:
14660 			break;
14661 		default:
14662 			ASSERT(0);
14663 		}
14664 
14665 		phyint = next_phyint;
14666 	}
14667 	return (need_retry);
14668 }
14669 
14670 /*
14671  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14672  * and return the ills in the list. This list will be
14673  * needed to unlock all the ills later on by the caller.
14674  * The <ill-ipsq> associations could change between the
14675  * lock and unlock. Hence the unlock can't traverse the
14676  * ipsq to get the list of ills.
14677  */
14678 static int
14679 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14680 {
14681 	int	cnt = 0;
14682 	phyint_t	*phyint;
14683 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14684 
14685 	/*
14686 	 * The caller holds ill_g_lock to ensure that the ill memberships
14687 	 * of the ipsq don't change
14688 	 */
14689 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14690 
14691 	phyint = ipsq->ipsq_phyint_list;
14692 	while (phyint != NULL) {
14693 		if (phyint->phyint_illv4 != NULL) {
14694 			ASSERT(cnt < list_max);
14695 			list[cnt++] = phyint->phyint_illv4;
14696 		}
14697 		if (phyint->phyint_illv6 != NULL) {
14698 			ASSERT(cnt < list_max);
14699 			list[cnt++] = phyint->phyint_illv6;
14700 		}
14701 		phyint = phyint->phyint_ipsq_next;
14702 	}
14703 	ill_lock_ills(list, cnt);
14704 	return (cnt);
14705 }
14706 
14707 void
14708 ill_lock_ills(ill_t **list, int cnt)
14709 {
14710 	int	i;
14711 
14712 	if (cnt > 1) {
14713 		boolean_t try_again;
14714 		do {
14715 			try_again = B_FALSE;
14716 			for (i = 0; i < cnt - 1; i++) {
14717 				if (list[i] < list[i + 1]) {
14718 					ill_t	*tmp;
14719 
14720 					/* swap the elements */
14721 					tmp = list[i];
14722 					list[i] = list[i + 1];
14723 					list[i + 1] = tmp;
14724 					try_again = B_TRUE;
14725 				}
14726 			}
14727 		} while (try_again);
14728 	}
14729 
14730 	for (i = 0; i < cnt; i++) {
14731 		if (i == 0) {
14732 			if (list[i] != NULL)
14733 				mutex_enter(&list[i]->ill_lock);
14734 			else
14735 				return;
14736 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14737 			mutex_enter(&list[i]->ill_lock);
14738 		}
14739 	}
14740 }
14741 
14742 void
14743 ill_unlock_ills(ill_t **list, int cnt)
14744 {
14745 	int	i;
14746 
14747 	for (i = 0; i < cnt; i++) {
14748 		if ((i == 0) && (list[i] != NULL)) {
14749 			mutex_exit(&list[i]->ill_lock);
14750 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14751 			mutex_exit(&list[i]->ill_lock);
14752 		}
14753 	}
14754 }
14755 
14756 /*
14757  * Merge all the ills from 1 ipsq group into another ipsq group.
14758  * The source ipsq group is specified by the ipsq associated with
14759  * 'from_ill'. The destination ipsq group is specified by the ipsq
14760  * associated with 'to_ill' or 'groupname' respectively.
14761  * Note that ipsq itself does not have a reference count mechanism
14762  * and functions don't look up an ipsq and pass it around. Instead
14763  * functions pass around an ill or groupname, and the ipsq is looked
14764  * up from the ill or groupname and the required operation performed
14765  * atomically with the lookup on the ipsq.
14766  */
14767 static int
14768 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14769     queue_t *q)
14770 {
14771 	ipsq_t *old_ipsq;
14772 	ipsq_t *new_ipsq;
14773 	ill_t	**ill_list;
14774 	int	cnt;
14775 	size_t	ill_list_size;
14776 	boolean_t became_writer_on_new_sq = B_FALSE;
14777 	ip_stack_t	*ipst = from_ill->ill_ipst;
14778 
14779 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14780 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14781 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14782 
14783 	/*
14784 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14785 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14786 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14787 	 */
14788 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14789 
14790 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14791 	if (groupname != NULL)
14792 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14793 	else {
14794 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14795 	}
14796 
14797 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14798 
14799 	/*
14800 	 * both groups are on the same ipsq.
14801 	 */
14802 	if (old_ipsq == new_ipsq) {
14803 		rw_exit(&ipst->ips_ill_g_lock);
14804 		return (0);
14805 	}
14806 
14807 	cnt = old_ipsq->ipsq_refs << 1;
14808 	ill_list_size = cnt * sizeof (ill_t *);
14809 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14810 	if (ill_list == NULL) {
14811 		rw_exit(&ipst->ips_ill_g_lock);
14812 		return (ENOMEM);
14813 	}
14814 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14815 
14816 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14817 	mutex_enter(&new_ipsq->ipsq_lock);
14818 	if ((new_ipsq->ipsq_writer == NULL &&
14819 	    new_ipsq->ipsq_current_ipif == NULL) ||
14820 	    (new_ipsq->ipsq_writer == curthread)) {
14821 		new_ipsq->ipsq_writer = curthread;
14822 		new_ipsq->ipsq_reentry_cnt++;
14823 		became_writer_on_new_sq = B_TRUE;
14824 	}
14825 
14826 	/*
14827 	 * We are holding ill_g_lock as writer and all the ill locks of
14828 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14829 	 * message can land up on the old ipsq even though we don't hold the
14830 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14831 	 */
14832 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14833 
14834 	/*
14835 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14836 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14837 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14838 	 */
14839 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14840 
14841 	/*
14842 	 * Mark the new ipsq as needing a split since it is currently
14843 	 * being shared by more than 1 IPMP group. The split will
14844 	 * occur at the end of ipsq_exit
14845 	 */
14846 	new_ipsq->ipsq_split = B_TRUE;
14847 
14848 	/* Now release all the locks */
14849 	mutex_exit(&new_ipsq->ipsq_lock);
14850 	ill_unlock_ills(ill_list, cnt);
14851 	rw_exit(&ipst->ips_ill_g_lock);
14852 
14853 	kmem_free(ill_list, ill_list_size);
14854 
14855 	/*
14856 	 * If we succeeded in becoming writer on the new ipsq, then
14857 	 * drain the new ipsq and start processing  all enqueued messages
14858 	 * including the current ioctl we are processing which is either
14859 	 * a set groupname or failover/failback.
14860 	 */
14861 	if (became_writer_on_new_sq)
14862 		ipsq_exit(new_ipsq);
14863 
14864 	/*
14865 	 * syncq has been changed and all the messages have been moved.
14866 	 */
14867 	mutex_enter(&old_ipsq->ipsq_lock);
14868 	old_ipsq->ipsq_current_ipif = NULL;
14869 	old_ipsq->ipsq_current_ioctl = 0;
14870 	old_ipsq->ipsq_current_done = B_TRUE;
14871 	mutex_exit(&old_ipsq->ipsq_lock);
14872 	return (EINPROGRESS);
14873 }
14874 
14875 /*
14876  * Delete and add the loopback copy and non-loopback copy of
14877  * the BROADCAST ire corresponding to ill and addr. Used to
14878  * group broadcast ires together when ill becomes part of
14879  * a group.
14880  *
14881  * This function is also called when ill is leaving the group
14882  * so that the ires belonging to the group gets re-grouped.
14883  */
14884 static void
14885 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14886 {
14887 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14888 	ire_t **ire_ptpn = &ire_head;
14889 	ip_stack_t	*ipst = ill->ill_ipst;
14890 
14891 	/*
14892 	 * The loopback and non-loopback IREs are inserted in the order in which
14893 	 * they're found, on the basis that they are correctly ordered (loopback
14894 	 * first).
14895 	 */
14896 	for (;;) {
14897 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14898 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14899 		if (ire == NULL)
14900 			break;
14901 
14902 		/*
14903 		 * we are passing in KM_SLEEP because it is not easy to
14904 		 * go back to a sane state in case of memory failure.
14905 		 */
14906 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14907 		ASSERT(nire != NULL);
14908 		bzero(nire, sizeof (ire_t));
14909 		/*
14910 		 * Don't use ire_max_frag directly since we don't
14911 		 * hold on to 'ire' until we add the new ire 'nire' and
14912 		 * we don't want the new ire to have a dangling reference
14913 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14914 		 * be in sync with the ipif_mtu of the associate ipif.
14915 		 * For eg. this happens as a result of SIOCSLIFNAME,
14916 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14917 		 * the driver. A change in ire_max_frag triggered as
14918 		 * as a result of path mtu discovery, or due to an
14919 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14920 		 * route change -mtu command does not apply to broadcast ires.
14921 		 *
14922 		 * XXX We need a recovery strategy here if ire_init fails
14923 		 */
14924 		if (ire_init(nire,
14925 		    (uchar_t *)&ire->ire_addr,
14926 		    (uchar_t *)&ire->ire_mask,
14927 		    (uchar_t *)&ire->ire_src_addr,
14928 		    (uchar_t *)&ire->ire_gateway_addr,
14929 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14930 		    &ire->ire_ipif->ipif_mtu,
14931 		    ire->ire_nce,
14932 		    ire->ire_rfq,
14933 		    ire->ire_stq,
14934 		    ire->ire_type,
14935 		    ire->ire_ipif,
14936 		    ire->ire_cmask,
14937 		    ire->ire_phandle,
14938 		    ire->ire_ihandle,
14939 		    ire->ire_flags,
14940 		    &ire->ire_uinfo,
14941 		    NULL,
14942 		    NULL,
14943 		    ipst) == NULL) {
14944 			cmn_err(CE_PANIC, "ire_init() failed");
14945 		}
14946 		ire_delete(ire);
14947 		ire_refrele(ire);
14948 
14949 		/*
14950 		 * The newly created IREs are inserted at the tail of the list
14951 		 * starting with ire_head. As we've just allocated them no one
14952 		 * knows about them so it's safe.
14953 		 */
14954 		*ire_ptpn = nire;
14955 		ire_ptpn = &nire->ire_next;
14956 	}
14957 
14958 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14959 		int error;
14960 		ire_t *oire;
14961 		/* unlink the IRE from our list before calling ire_add() */
14962 		nire_next = nire->ire_next;
14963 		nire->ire_next = NULL;
14964 
14965 		/* ire_add adds the ire at the right place in the list */
14966 		oire = nire;
14967 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14968 		ASSERT(error == 0);
14969 		ASSERT(oire == nire);
14970 		ire_refrele(nire);	/* Held in ire_add */
14971 	}
14972 }
14973 
14974 /*
14975  * This function is usually called when an ill is inserted in
14976  * a group and all the ipifs are already UP. As all the ipifs
14977  * are already UP, the broadcast ires have already been created
14978  * and been inserted. But, ire_add_v4 would not have grouped properly.
14979  * We need to re-group for the benefit of ip_wput_ire which
14980  * expects BROADCAST ires to be grouped properly to avoid sending
14981  * more than one copy of the broadcast packet per group.
14982  *
14983  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14984  *	  because when ipif_up_done ends up calling this, ires have
14985  *        already been added before illgrp_insert i.e before ill_group
14986  *	  has been initialized.
14987  */
14988 static void
14989 ill_group_bcast_for_xmit(ill_t *ill)
14990 {
14991 	ill_group_t *illgrp;
14992 	ipif_t *ipif;
14993 	ipaddr_t addr;
14994 	ipaddr_t net_mask;
14995 	ipaddr_t subnet_netmask;
14996 
14997 	illgrp = ill->ill_group;
14998 
14999 	/*
15000 	 * This function is called even when an ill is deleted from
15001 	 * the group. Hence, illgrp could be null.
15002 	 */
15003 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15004 		return;
15005 
15006 	/*
15007 	 * Delete all the BROADCAST ires matching this ill and add
15008 	 * them back. This time, ire_add_v4 should take care of
15009 	 * grouping them with others because ill is part of the
15010 	 * group.
15011 	 */
15012 	ill_bcast_delete_and_add(ill, 0);
15013 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15014 
15015 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15016 
15017 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15018 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15019 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15020 		} else {
15021 			net_mask = htonl(IN_CLASSA_NET);
15022 		}
15023 		addr = net_mask & ipif->ipif_subnet;
15024 		ill_bcast_delete_and_add(ill, addr);
15025 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15026 
15027 		subnet_netmask = ipif->ipif_net_mask;
15028 		addr = ipif->ipif_subnet;
15029 		ill_bcast_delete_and_add(ill, addr);
15030 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15031 	}
15032 }
15033 
15034 /*
15035  * This function is called from illgrp_delete when ill is being deleted
15036  * from the group.
15037  *
15038  * As ill is not there in the group anymore, any address belonging
15039  * to this ill should be cleared of IRE_MARK_NORECV.
15040  */
15041 static void
15042 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15043 {
15044 	ire_t *ire;
15045 	irb_t *irb;
15046 	ip_stack_t	*ipst = ill->ill_ipst;
15047 
15048 	ASSERT(ill->ill_group == NULL);
15049 
15050 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15051 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15052 
15053 	if (ire != NULL) {
15054 		/*
15055 		 * IPMP and plumbing operations are serialized on the ipsq, so
15056 		 * no one will insert or delete a broadcast ire under our feet.
15057 		 */
15058 		irb = ire->ire_bucket;
15059 		rw_enter(&irb->irb_lock, RW_READER);
15060 		ire_refrele(ire);
15061 
15062 		for (; ire != NULL; ire = ire->ire_next) {
15063 			if (ire->ire_addr != addr)
15064 				break;
15065 			if (ire_to_ill(ire) != ill)
15066 				continue;
15067 
15068 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15069 			ire->ire_marks &= ~IRE_MARK_NORECV;
15070 		}
15071 		rw_exit(&irb->irb_lock);
15072 	}
15073 }
15074 
15075 ire_t *
15076 irep_insert(ill_group_t *illgrp, ipaddr_t addr, ire_t *ire, ire_t ***pirep)
15077 {
15078 	boolean_t first = B_TRUE;
15079 	ire_t *clear_ire = NULL;
15080 	ire_t *start_ire = NULL;
15081 	uint64_t match_flags;
15082 	uint64_t phyi_flags;
15083 	boolean_t fallback = B_FALSE;
15084 
15085 	/*
15086 	 * irb_lock must be held by the caller.
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 && (*pirep == NULL))
15112 			*pirep = 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 		*pirep = NULL;
15158 		goto redo;
15159 	}
15160 	return (clear_ire);
15161 }
15162 
15163 /*
15164  * This function must be called only after the broadcast ires
15165  * have been grouped together. For a given address addr, nominate
15166  * only one of the ires whose interface is not FAILED or OFFLINE.
15167  *
15168  * This is also called when an ipif goes down, so that we can nominate
15169  * a different ire with the same address for receiving.
15170  */
15171 static void
15172 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15173 {
15174 	irb_t *irb;
15175 	ire_t *ire;
15176 	ire_t *ire1;
15177 	ire_t *save_ire;
15178 	ire_t **irep = NULL;
15179 	ire_t *clear_ire = NULL;
15180 	ire_t	*new_lb_ire;
15181 	ire_t	*new_nlb_ire;
15182 	boolean_t new_lb_ire_used = B_FALSE;
15183 	boolean_t new_nlb_ire_used = B_FALSE;
15184 	boolean_t refrele_lb_ire = B_FALSE;
15185 	boolean_t refrele_nlb_ire = B_FALSE;
15186 	uint_t	max_frag;
15187 
15188 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15189 	    NULL, MATCH_IRE_TYPE, ipst);
15190 	/*
15191 	 * We may not be able to find some ires if a previous
15192 	 * ire_create failed. This happens when an ipif goes
15193 	 * down and we are unable to create BROADCAST ires due
15194 	 * to memory failure. Thus, we have to check for NULL
15195 	 * below. This should handle the case for LOOPBACK,
15196 	 * POINTOPOINT and interfaces with some POINTOPOINT
15197 	 * logicals for which there are no BROADCAST ires.
15198 	 */
15199 	if (ire == NULL)
15200 		return;
15201 	/*
15202 	 * Currently IRE_BROADCASTS are deleted when an ipif
15203 	 * goes down which runs exclusively. Thus, setting
15204 	 * IRE_MARK_RCVD should not race with ire_delete marking
15205 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15206 	 * be consistent with other parts of the code that walks
15207 	 * a given bucket.
15208 	 */
15209 	save_ire = ire;
15210 	irb = ire->ire_bucket;
15211 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15212 	if (new_lb_ire == NULL) {
15213 		ire_refrele(ire);
15214 		return;
15215 	}
15216 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15217 	if (new_nlb_ire == NULL) {
15218 		ire_refrele(ire);
15219 		kmem_cache_free(ire_cache, new_lb_ire);
15220 		return;
15221 	}
15222 	IRB_REFHOLD(irb);
15223 	rw_enter(&irb->irb_lock, RW_WRITER);
15224 	clear_ire = irep_insert(illgrp, addr, ire, &irep);
15225 
15226 	/*
15227 	 * irep non-NULL indicates that we entered the while loop
15228 	 * above. If clear_ire is at the insertion point, we don't
15229 	 * have to do anything. clear_ire will be NULL if all the
15230 	 * interfaces are failed.
15231 	 *
15232 	 * We cannot unlink and reinsert the ire at the right place
15233 	 * in the list since there can be other walkers of this bucket.
15234 	 * Instead we delete and recreate the ire
15235 	 */
15236 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15237 		ire_t *clear_ire_stq = NULL;
15238 		ire_t *clr_ire = NULL;
15239 		ire_t *ire_next = NULL;
15240 
15241 		if (clear_ire->ire_stq == NULL)
15242 			ire_next = clear_ire->ire_next;
15243 
15244 		rw_exit(&irb->irb_lock);
15245 
15246 		bzero(new_lb_ire, sizeof (ire_t));
15247 		/* XXX We need a recovery strategy here. */
15248 		if (ire_init(new_lb_ire,
15249 		    (uchar_t *)&clear_ire->ire_addr,
15250 		    (uchar_t *)&clear_ire->ire_mask,
15251 		    (uchar_t *)&clear_ire->ire_src_addr,
15252 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15253 		    &clear_ire->ire_max_frag,
15254 		    NULL, /* let ire_nce_init derive the resolver info */
15255 		    clear_ire->ire_rfq,
15256 		    clear_ire->ire_stq,
15257 		    clear_ire->ire_type,
15258 		    clear_ire->ire_ipif,
15259 		    clear_ire->ire_cmask,
15260 		    clear_ire->ire_phandle,
15261 		    clear_ire->ire_ihandle,
15262 		    clear_ire->ire_flags,
15263 		    &clear_ire->ire_uinfo,
15264 		    NULL,
15265 		    NULL,
15266 		    ipst) == NULL)
15267 			cmn_err(CE_PANIC, "ire_init() failed");
15268 
15269 		refrele_lb_ire = B_TRUE;
15270 
15271 		if (ire_next != NULL &&
15272 		    ire_next->ire_stq != NULL &&
15273 		    ire_next->ire_addr == clear_ire->ire_addr &&
15274 		    ire_next->ire_ipif->ipif_ill ==
15275 		    clear_ire->ire_ipif->ipif_ill) {
15276 			clear_ire_stq = ire_next;
15277 
15278 			bzero(new_nlb_ire, sizeof (ire_t));
15279 			/* XXX We need a recovery strategy here. */
15280 			if (ire_init(new_nlb_ire,
15281 			    (uchar_t *)&clear_ire_stq->ire_addr,
15282 			    (uchar_t *)&clear_ire_stq->ire_mask,
15283 			    (uchar_t *)&clear_ire_stq->ire_src_addr,
15284 			    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15285 			    &clear_ire_stq->ire_max_frag,
15286 			    NULL,
15287 			    clear_ire_stq->ire_rfq,
15288 			    clear_ire_stq->ire_stq,
15289 			    clear_ire_stq->ire_type,
15290 			    clear_ire_stq->ire_ipif,
15291 			    clear_ire_stq->ire_cmask,
15292 			    clear_ire_stq->ire_phandle,
15293 			    clear_ire_stq->ire_ihandle,
15294 			    clear_ire_stq->ire_flags,
15295 			    &clear_ire_stq->ire_uinfo,
15296 			    NULL,
15297 			    NULL,
15298 			    ipst) == NULL)
15299 				cmn_err(CE_PANIC, "ire_init() failed");
15300 
15301 				refrele_nlb_ire = B_TRUE;
15302 			}
15303 
15304 		rw_enter(&irb->irb_lock, RW_WRITER);
15305 		/*
15306 		 * irb_lock was dropped across call to ire_init() due to
15307 		 * lock ordering issue with ipst->ips_ndp{4,6}->ndp_g_lock
15308 		 * mutex lock. Therefore irep could have changed. call
15309 		 * irep_insert() to get the new insertion point (irep) and
15310 		 * recheck all known conditions.
15311 		 */
15312 		irep = NULL;
15313 		clr_ire = irep_insert(illgrp, addr, save_ire, &irep);
15314 		if ((irep != NULL) && (*irep != clear_ire) &&
15315 		    (clr_ire == clear_ire)) {
15316 			if ((clear_ire_stq != NULL) &&
15317 			    (clr_ire->ire_next != clear_ire_stq))
15318 				clear_ire_stq = NULL;
15319 			/*
15320 			 * Delete the ire. We can't call ire_delete() since
15321 			 * we are holding the bucket lock. We can't release the
15322 			 * bucket lock since we can't allow irep to change.
15323 			 * So just mark it CONDEMNED.
15324 			 * The IRB_REFRELE will delete the ire from the list
15325 			 * and do the refrele.
15326 			 */
15327 			clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15328 			irb->irb_marks |= IRB_MARK_CONDEMNED;
15329 
15330 			if (clear_ire_stq != NULL &&
15331 			    clear_ire_stq->ire_nce != NULL) {
15332 				nce_fastpath_list_delete(
15333 				    clear_ire_stq->ire_nce);
15334 				clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15335 			}
15336 
15337 			/*
15338 			 * Also take care of otherfields like ib/ob pkt count
15339 			 * etc. Need to dup them.
15340 			 * ditto in ill_bcast_delete_and_add
15341 			 */
15342 
15343 			/* Set the max_frag before adding the ire */
15344 			max_frag = *new_lb_ire->ire_max_fragp;
15345 			new_lb_ire->ire_max_fragp = NULL;
15346 			new_lb_ire->ire_max_frag = max_frag;
15347 
15348 			/* Add the new ire's. Insert at *irep */
15349 			new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15350 			ire1 = *irep;
15351 			if (ire1 != NULL)
15352 				ire1->ire_ptpn = &new_lb_ire->ire_next;
15353 			new_lb_ire->ire_next = ire1;
15354 			/* Link the new one in. */
15355 			new_lb_ire->ire_ptpn = irep;
15356 			membar_producer();
15357 			*irep = new_lb_ire;
15358 			new_lb_ire_used = B_TRUE;
15359 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15360 			    ire_stats_inserted);
15361 			new_lb_ire->ire_bucket->irb_ire_cnt++;
15362 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *),
15363 			    new_lb_ire->ire_ipif,
15364 			    (char *), "ire", (void *), new_lb_ire);
15365 			new_lb_ire->ire_ipif->ipif_ire_cnt++;
15366 
15367 			if (clear_ire_stq != NULL) {
15368 				ill_t	*ire_ill;
15369 				/* Set the max_frag before adding the ire */
15370 				max_frag = *new_nlb_ire->ire_max_fragp;
15371 				new_nlb_ire->ire_max_fragp = NULL;
15372 				new_nlb_ire->ire_max_frag = max_frag;
15373 
15374 				new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15375 				irep = &new_lb_ire->ire_next;
15376 				/* Add the new ire. Insert at *irep */
15377 				ire1 = *irep;
15378 				if (ire1 != NULL)
15379 					ire1->ire_ptpn = &new_nlb_ire->ire_next;
15380 				new_nlb_ire->ire_next = ire1;
15381 				/* Link the new one in. */
15382 				new_nlb_ire->ire_ptpn = irep;
15383 				membar_producer();
15384 				*irep = new_nlb_ire;
15385 				new_nlb_ire_used = B_TRUE;
15386 				BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15387 				    ire_stats_inserted);
15388 				new_nlb_ire->ire_bucket->irb_ire_cnt++;
15389 				DTRACE_PROBE3(ipif__incr__cnt,
15390 				    (ipif_t *), new_nlb_ire->ire_ipif,
15391 				    (char *), "ire", (void *), new_nlb_ire);
15392 				new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15393 				DTRACE_PROBE3(ill__incr__cnt,
15394 				    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15395 				    (char *), "ire", (void *), new_nlb_ire);
15396 				ire_ill = (ill_t *)new_nlb_ire->ire_stq->q_ptr;
15397 				ire_ill->ill_ire_cnt++;
15398 			}
15399 		}
15400 	}
15401 	ire_refrele(save_ire);
15402 	rw_exit(&irb->irb_lock);
15403 	/*
15404 	 * Since we dropped the irb_lock across call to ire_init()
15405 	 * and rechecking known conditions, it is possible that
15406 	 * the checks might fail, therefore undo the work done by
15407 	 * ire_init() by calling ire_refrele() on the newly created ire.
15408 	 */
15409 	if (!new_lb_ire_used) {
15410 		if (refrele_lb_ire) {
15411 			ire_refrele(new_lb_ire);
15412 		} else {
15413 			kmem_cache_free(ire_cache, new_lb_ire);
15414 		}
15415 	}
15416 	if (!new_nlb_ire_used) {
15417 		if (refrele_nlb_ire) {
15418 			ire_refrele(new_nlb_ire);
15419 		} else {
15420 			kmem_cache_free(ire_cache, new_nlb_ire);
15421 		}
15422 	}
15423 	IRB_REFRELE(irb);
15424 }
15425 
15426 /*
15427  * Whenever an ipif goes down we have to renominate a different
15428  * broadcast ire to receive. Whenever an ipif comes up, we need
15429  * to make sure that we have only one nominated to receive.
15430  */
15431 static void
15432 ipif_renominate_bcast(ipif_t *ipif)
15433 {
15434 	ill_t *ill = ipif->ipif_ill;
15435 	ipaddr_t subnet_addr;
15436 	ipaddr_t net_addr;
15437 	ipaddr_t net_mask = 0;
15438 	ipaddr_t subnet_netmask;
15439 	ipaddr_t addr;
15440 	ill_group_t *illgrp;
15441 	ip_stack_t	*ipst = ill->ill_ipst;
15442 
15443 	illgrp = ill->ill_group;
15444 	/*
15445 	 * If this is the last ipif going down, it might take
15446 	 * the ill out of the group. In that case ipif_down ->
15447 	 * illgrp_delete takes care of doing the nomination.
15448 	 * ipif_down does not call for this case.
15449 	 */
15450 	ASSERT(illgrp != NULL);
15451 
15452 	/* There could not have been any ires associated with this */
15453 	if (ipif->ipif_subnet == 0)
15454 		return;
15455 
15456 	ill_mark_bcast(illgrp, 0, ipst);
15457 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15458 
15459 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15460 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15461 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15462 	} else {
15463 		net_mask = htonl(IN_CLASSA_NET);
15464 	}
15465 	addr = net_mask & ipif->ipif_subnet;
15466 	ill_mark_bcast(illgrp, addr, ipst);
15467 
15468 	net_addr = ~net_mask | addr;
15469 	ill_mark_bcast(illgrp, net_addr, ipst);
15470 
15471 	subnet_netmask = ipif->ipif_net_mask;
15472 	addr = ipif->ipif_subnet;
15473 	ill_mark_bcast(illgrp, addr, ipst);
15474 
15475 	subnet_addr = ~subnet_netmask | addr;
15476 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15477 }
15478 
15479 /*
15480  * Whenever we form or delete ill groups, we need to nominate one set of
15481  * BROADCAST ires for receiving in the group.
15482  *
15483  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15484  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15485  *    for ill_ipif_up_count to be non-zero. This is the only case where
15486  *    ill_ipif_up_count is zero and we would still find the ires.
15487  *
15488  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15489  *    ipif is UP and we just have to do the nomination.
15490  *
15491  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15492  *    from the group. So, we have to do the nomination.
15493  *
15494  * Because of (3), there could be just one ill in the group. But we have
15495  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15496  * Thus, this function does not optimize when there is only one ill as
15497  * it is not correct for (3).
15498  */
15499 static void
15500 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15501 {
15502 	ill_t *ill;
15503 	ipif_t *ipif;
15504 	ipaddr_t subnet_addr;
15505 	ipaddr_t prev_subnet_addr = 0;
15506 	ipaddr_t net_addr;
15507 	ipaddr_t prev_net_addr = 0;
15508 	ipaddr_t net_mask = 0;
15509 	ipaddr_t subnet_netmask;
15510 	ipaddr_t addr;
15511 	ip_stack_t	*ipst;
15512 
15513 	/*
15514 	 * When the last memeber is leaving, there is nothing to
15515 	 * nominate.
15516 	 */
15517 	if (illgrp->illgrp_ill_count == 0) {
15518 		ASSERT(illgrp->illgrp_ill == NULL);
15519 		return;
15520 	}
15521 
15522 	ill = illgrp->illgrp_ill;
15523 	ASSERT(!ill->ill_isv6);
15524 	ipst = ill->ill_ipst;
15525 	/*
15526 	 * We assume that ires with same address and belonging to the
15527 	 * same group, has been grouped together. Nominating a *single*
15528 	 * ill in the group for sending and receiving broadcast is done
15529 	 * by making sure that the first BROADCAST ire (which will be
15530 	 * the one returned by ire_ctable_lookup for ip_rput and the
15531 	 * one that will be used in ip_wput_ire) will be the one that
15532 	 * will not have IRE_MARK_NORECV set.
15533 	 *
15534 	 * 1) ip_rput checks and discards packets received on ires marked
15535 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15536 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15537 	 *    first ire in the group for every broadcast address in the group.
15538 	 *    ip_rput will accept packets only on the first ire i.e only
15539 	 *    one copy of the ill.
15540 	 *
15541 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15542 	 *    packet for the whole group. It needs to send out on the ill
15543 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15544 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15545 	 *    the copy echoed back on other port where the ire is not marked
15546 	 *    with IRE_MARK_NORECV.
15547 	 *
15548 	 * Note that we just need to have the first IRE either loopback or
15549 	 * non-loopback (either of them may not exist if ire_create failed
15550 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15551 	 * always hit the first one and hence will always accept one copy.
15552 	 *
15553 	 * We have a broadcast ire per ill for all the unique prefixes
15554 	 * hosted on that ill. As we don't have a way of knowing the
15555 	 * unique prefixes on a given ill and hence in the whole group,
15556 	 * we just call ill_mark_bcast on all the prefixes that exist
15557 	 * in the group. For the common case of one prefix, the code
15558 	 * below optimizes by remebering the last address used for
15559 	 * markng. In the case of multiple prefixes, this will still
15560 	 * optimize depending the order of prefixes.
15561 	 *
15562 	 * The only unique address across the whole group is 0.0.0.0 and
15563 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15564 	 * the first ire in the bucket for receiving and disables the
15565 	 * others.
15566 	 */
15567 	ill_mark_bcast(illgrp, 0, ipst);
15568 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15569 	for (; ill != NULL; ill = ill->ill_group_next) {
15570 
15571 		for (ipif = ill->ill_ipif; ipif != NULL;
15572 		    ipif = ipif->ipif_next) {
15573 
15574 			if (!(ipif->ipif_flags & IPIF_UP) ||
15575 			    ipif->ipif_subnet == 0) {
15576 				continue;
15577 			}
15578 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15579 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15580 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15581 			} else {
15582 				net_mask = htonl(IN_CLASSA_NET);
15583 			}
15584 			addr = net_mask & ipif->ipif_subnet;
15585 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15586 				ill_mark_bcast(illgrp, addr, ipst);
15587 				net_addr = ~net_mask | addr;
15588 				ill_mark_bcast(illgrp, net_addr, ipst);
15589 			}
15590 			prev_net_addr = addr;
15591 
15592 			subnet_netmask = ipif->ipif_net_mask;
15593 			addr = ipif->ipif_subnet;
15594 			if (prev_subnet_addr == 0 ||
15595 			    prev_subnet_addr != addr) {
15596 				ill_mark_bcast(illgrp, addr, ipst);
15597 				subnet_addr = ~subnet_netmask | addr;
15598 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15599 			}
15600 			prev_subnet_addr = addr;
15601 		}
15602 	}
15603 }
15604 
15605 /*
15606  * This function is called while forming ill groups.
15607  *
15608  * Currently, we handle only allmulti groups. We want to join
15609  * allmulti on only one of the ills in the groups. In future,
15610  * when we have link aggregation, we may have to join normal
15611  * multicast groups on multiple ills as switch does inbound load
15612  * balancing. Following are the functions that calls this
15613  * function :
15614  *
15615  * 1) ill_recover_multicast : Interface is coming back UP.
15616  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15617  *    will call ill_recover_multicast to recover all the multicast
15618  *    groups. We need to make sure that only one member is joined
15619  *    in the ill group.
15620  *
15621  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15622  *    Somebody is joining allmulti. We need to make sure that only one
15623  *    member is joined in the group.
15624  *
15625  * 3) illgrp_insert : If allmulti has already joined, we need to make
15626  *    sure that only one member is joined in the group.
15627  *
15628  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15629  *    allmulti who we have nominated. We need to pick someother ill.
15630  *
15631  * 5) illgrp_delete : The ill we nominated is leaving the group,
15632  *    we need to pick a new ill to join the group.
15633  *
15634  * For (1), (2), (5) - we just have to check whether there is
15635  * a good ill joined in the group. If we could not find any ills
15636  * joined the group, we should join.
15637  *
15638  * For (4), the one that was nominated to receive, left the group.
15639  * There could be nobody joined in the group when this function is
15640  * called.
15641  *
15642  * For (3) - we need to explicitly check whether there are multiple
15643  * ills joined in the group.
15644  *
15645  * For simplicity, we don't differentiate any of the above cases. We
15646  * just leave the group if it is joined on any of them and join on
15647  * the first good ill.
15648  */
15649 int
15650 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15651 {
15652 	ilm_t *ilm;
15653 	ill_t *ill;
15654 	ill_t *fallback_inactive_ill = NULL;
15655 	ill_t *fallback_failed_ill = NULL;
15656 	int ret = 0;
15657 
15658 	/*
15659 	 * Leave the allmulti on all the ills and start fresh.
15660 	 */
15661 	for (ill = illgrp->illgrp_ill; ill != NULL;
15662 	    ill = ill->ill_group_next) {
15663 		if (ill->ill_join_allmulti)
15664 			(void) ip_leave_allmulti(ill->ill_ipif);
15665 	}
15666 
15667 	/*
15668 	 * Choose a good ill. Fallback to inactive or failed if
15669 	 * none available. We need to fallback to FAILED in the
15670 	 * case where we have 2 interfaces in a group - where
15671 	 * one of them is failed and another is a good one and
15672 	 * the good one (not marked inactive) is leaving the group.
15673 	 */
15674 	ret = 0;
15675 	for (ill = illgrp->illgrp_ill; ill != NULL;
15676 	    ill = ill->ill_group_next) {
15677 		/* Never pick an offline interface */
15678 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15679 			continue;
15680 
15681 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15682 			fallback_failed_ill = ill;
15683 			continue;
15684 		}
15685 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15686 			fallback_inactive_ill = ill;
15687 			continue;
15688 		}
15689 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15690 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15691 				ret = ip_join_allmulti(ill->ill_ipif);
15692 				/*
15693 				 * ip_join_allmulti can fail because of memory
15694 				 * failures. So, make sure we join at least
15695 				 * on one ill.
15696 				 */
15697 				if (ill->ill_join_allmulti)
15698 					return (0);
15699 			}
15700 		}
15701 	}
15702 	if (ret != 0) {
15703 		/*
15704 		 * If we tried nominating above and failed to do so,
15705 		 * return error. We might have tried multiple times.
15706 		 * But, return the latest error.
15707 		 */
15708 		return (ret);
15709 	}
15710 	if ((ill = fallback_inactive_ill) != NULL) {
15711 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15712 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15713 				ret = ip_join_allmulti(ill->ill_ipif);
15714 				return (ret);
15715 			}
15716 		}
15717 	} else if ((ill = fallback_failed_ill) != NULL) {
15718 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15719 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15720 				ret = ip_join_allmulti(ill->ill_ipif);
15721 				return (ret);
15722 			}
15723 		}
15724 	}
15725 	return (0);
15726 }
15727 
15728 /*
15729  * This function is called from illgrp_delete after it is
15730  * deleted from the group to reschedule responsibilities
15731  * to a different ill.
15732  */
15733 static void
15734 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15735 {
15736 	ilm_t	*ilm;
15737 	ipif_t	*ipif;
15738 	ipaddr_t subnet_addr;
15739 	ipaddr_t net_addr;
15740 	ipaddr_t net_mask = 0;
15741 	ipaddr_t subnet_netmask;
15742 	ipaddr_t addr;
15743 	ip_stack_t *ipst = ill->ill_ipst;
15744 
15745 	ASSERT(ill->ill_group == NULL);
15746 	/*
15747 	 * Broadcast Responsibility:
15748 	 *
15749 	 * 1. If this ill has been nominated for receiving broadcast
15750 	 * packets, we need to find a new one. Before we find a new
15751 	 * one, we need to re-group the ires that are part of this new
15752 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15753 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15754 	 * thing for us.
15755 	 *
15756 	 * 2. If this ill was not nominated for receiving broadcast
15757 	 * packets, we need to clear the IRE_MARK_NORECV flag
15758 	 * so that we continue to send up broadcast packets.
15759 	 */
15760 	if (!ill->ill_isv6) {
15761 		/*
15762 		 * Case 1 above : No optimization here. Just redo the
15763 		 * nomination.
15764 		 */
15765 		ill_group_bcast_for_xmit(ill);
15766 		ill_nominate_bcast_rcv(illgrp);
15767 
15768 		/*
15769 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15770 		 */
15771 		ill_clear_bcast_mark(ill, 0);
15772 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15773 
15774 		for (ipif = ill->ill_ipif; ipif != NULL;
15775 		    ipif = ipif->ipif_next) {
15776 
15777 			if (!(ipif->ipif_flags & IPIF_UP) ||
15778 			    ipif->ipif_subnet == 0) {
15779 				continue;
15780 			}
15781 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15782 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15783 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15784 			} else {
15785 				net_mask = htonl(IN_CLASSA_NET);
15786 			}
15787 			addr = net_mask & ipif->ipif_subnet;
15788 			ill_clear_bcast_mark(ill, addr);
15789 
15790 			net_addr = ~net_mask | addr;
15791 			ill_clear_bcast_mark(ill, net_addr);
15792 
15793 			subnet_netmask = ipif->ipif_net_mask;
15794 			addr = ipif->ipif_subnet;
15795 			ill_clear_bcast_mark(ill, addr);
15796 
15797 			subnet_addr = ~subnet_netmask | addr;
15798 			ill_clear_bcast_mark(ill, subnet_addr);
15799 		}
15800 	}
15801 
15802 	/*
15803 	 * Multicast Responsibility.
15804 	 *
15805 	 * If we have joined allmulti on this one, find a new member
15806 	 * in the group to join allmulti. As this ill is already part
15807 	 * of allmulti, we don't have to join on this one.
15808 	 *
15809 	 * If we have not joined allmulti on this one, there is no
15810 	 * responsibility to handoff. But we need to take new
15811 	 * responsibility i.e, join allmulti on this one if we need
15812 	 * to.
15813 	 */
15814 	if (ill->ill_join_allmulti) {
15815 		(void) ill_nominate_mcast_rcv(illgrp);
15816 	} else {
15817 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15818 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15819 				(void) ip_join_allmulti(ill->ill_ipif);
15820 				break;
15821 			}
15822 		}
15823 	}
15824 
15825 	/*
15826 	 * We intentionally do the flushing of IRE_CACHES only matching
15827 	 * on the ill and not on groups. Note that we are already deleted
15828 	 * from the group.
15829 	 *
15830 	 * This will make sure that all IRE_CACHES whose stq is pointing
15831 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15832 	 * deleted and IRE_CACHES that are not pointing at this ill will
15833 	 * be left alone.
15834 	 */
15835 	ire_walk_ill(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_CACHE,
15836 	    illgrp_cache_delete, ill, ill);
15837 
15838 	/*
15839 	 * Some conn may have cached one of the IREs deleted above. By removing
15840 	 * the ire reference, we clean up the extra reference to the ill held in
15841 	 * ire->ire_stq.
15842 	 */
15843 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15844 
15845 	/*
15846 	 * Re-do source address selection for all the members in the
15847 	 * group, if they borrowed source address from one of the ipifs
15848 	 * in this ill.
15849 	 */
15850 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15851 		if (ill->ill_isv6) {
15852 			ipif_update_other_ipifs_v6(ipif, illgrp);
15853 		} else {
15854 			ipif_update_other_ipifs(ipif, illgrp);
15855 		}
15856 	}
15857 }
15858 
15859 /*
15860  * Delete the ill from the group. The caller makes sure that it is
15861  * in a group and it okay to delete from the group. So, we always
15862  * delete here.
15863  */
15864 static void
15865 illgrp_delete(ill_t *ill)
15866 {
15867 	ill_group_t *illgrp;
15868 	ill_group_t *tmpg;
15869 	ill_t *tmp_ill;
15870 	ip_stack_t	*ipst = ill->ill_ipst;
15871 
15872 	/*
15873 	 * Reset illgrp_ill_schednext if it was pointing at us.
15874 	 * We need to do this before we set ill_group to NULL.
15875 	 */
15876 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15877 	mutex_enter(&ill->ill_lock);
15878 
15879 	illgrp_reset_schednext(ill);
15880 
15881 	illgrp = ill->ill_group;
15882 
15883 	/* Delete the ill from illgrp. */
15884 	if (illgrp->illgrp_ill == ill) {
15885 		illgrp->illgrp_ill = ill->ill_group_next;
15886 	} else {
15887 		tmp_ill = illgrp->illgrp_ill;
15888 		while (tmp_ill->ill_group_next != ill) {
15889 			tmp_ill = tmp_ill->ill_group_next;
15890 			ASSERT(tmp_ill != NULL);
15891 		}
15892 		tmp_ill->ill_group_next = ill->ill_group_next;
15893 	}
15894 	ill->ill_group = NULL;
15895 	ill->ill_group_next = NULL;
15896 
15897 	illgrp->illgrp_ill_count--;
15898 	mutex_exit(&ill->ill_lock);
15899 	rw_exit(&ipst->ips_ill_g_lock);
15900 
15901 	/*
15902 	 * As this ill is leaving the group, we need to hand off
15903 	 * the responsibilities to the other ills in the group, if
15904 	 * this ill had some responsibilities.
15905 	 */
15906 
15907 	ill_handoff_responsibility(ill, illgrp);
15908 
15909 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15910 
15911 	if (illgrp->illgrp_ill_count == 0) {
15912 
15913 		ASSERT(illgrp->illgrp_ill == NULL);
15914 		if (ill->ill_isv6) {
15915 			if (illgrp == ipst->ips_illgrp_head_v6) {
15916 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15917 			} else {
15918 				tmpg = ipst->ips_illgrp_head_v6;
15919 				while (tmpg->illgrp_next != illgrp) {
15920 					tmpg = tmpg->illgrp_next;
15921 					ASSERT(tmpg != NULL);
15922 				}
15923 				tmpg->illgrp_next = illgrp->illgrp_next;
15924 			}
15925 		} else {
15926 			if (illgrp == ipst->ips_illgrp_head_v4) {
15927 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15928 			} else {
15929 				tmpg = ipst->ips_illgrp_head_v4;
15930 				while (tmpg->illgrp_next != illgrp) {
15931 					tmpg = tmpg->illgrp_next;
15932 					ASSERT(tmpg != NULL);
15933 				}
15934 				tmpg->illgrp_next = illgrp->illgrp_next;
15935 			}
15936 		}
15937 		mutex_destroy(&illgrp->illgrp_lock);
15938 		mi_free(illgrp);
15939 	}
15940 	rw_exit(&ipst->ips_ill_g_lock);
15941 
15942 	/*
15943 	 * Even though the ill is out of the group its not necessary
15944 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15945 	 * We will split the ipsq when phyint_groupname is set to NULL.
15946 	 */
15947 
15948 	/*
15949 	 * Send a routing sockets message if we are deleting from
15950 	 * groups with names.
15951 	 */
15952 	if (ill->ill_phyint->phyint_groupname_len != 0)
15953 		ip_rts_ifmsg(ill->ill_ipif);
15954 }
15955 
15956 /*
15957  * Re-do source address selection. This is normally called when
15958  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15959  * ipif comes up.
15960  */
15961 void
15962 ill_update_source_selection(ill_t *ill)
15963 {
15964 	ipif_t *ipif;
15965 
15966 	ASSERT(IAM_WRITER_ILL(ill));
15967 
15968 	if (ill->ill_group != NULL)
15969 		ill = ill->ill_group->illgrp_ill;
15970 
15971 	for (; ill != NULL; ill = ill->ill_group_next) {
15972 		for (ipif = ill->ill_ipif; ipif != NULL;
15973 		    ipif = ipif->ipif_next) {
15974 			if (ill->ill_isv6)
15975 				ipif_recreate_interface_routes_v6(NULL, ipif);
15976 			else
15977 				ipif_recreate_interface_routes(NULL, ipif);
15978 		}
15979 	}
15980 }
15981 
15982 /*
15983  * Insert ill in a group headed by illgrp_head. The caller can either
15984  * pass a groupname in which case we search for a group with the
15985  * same name to insert in or pass a group to insert in. This function
15986  * would only search groups with names.
15987  *
15988  * NOTE : The caller should make sure that there is at least one ipif
15989  *	  UP on this ill so that illgrp_scheduler can pick this ill
15990  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15991  *	  already sent a DL_UNBIND to the driver and we don't want to
15992  *	  send anymore packets. We don't assert for ipif_up_count
15993  *	  to be greater than zero, because ipif_up_done wants to call
15994  *	  this function before bumping up the ipif_up_count. See
15995  *	  ipif_up_done() for details.
15996  */
15997 int
15998 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15999     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16000 {
16001 	ill_group_t *illgrp;
16002 	ill_t *prev_ill;
16003 	phyint_t *phyi;
16004 	ip_stack_t	*ipst = ill->ill_ipst;
16005 
16006 	ASSERT(ill->ill_group == NULL);
16007 
16008 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16009 	mutex_enter(&ill->ill_lock);
16010 
16011 	if (groupname != NULL) {
16012 		/*
16013 		 * Look for a group with a matching groupname to insert.
16014 		 */
16015 		for (illgrp = *illgrp_head; illgrp != NULL;
16016 		    illgrp = illgrp->illgrp_next) {
16017 
16018 			ill_t *tmp_ill;
16019 
16020 			/*
16021 			 * If we have an ill_group_t in the list which has
16022 			 * no ill_t assigned then we must be in the process of
16023 			 * removing this group. We skip this as illgrp_delete()
16024 			 * will remove it from the list.
16025 			 */
16026 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16027 				ASSERT(illgrp->illgrp_ill_count == 0);
16028 				continue;
16029 			}
16030 
16031 			ASSERT(tmp_ill->ill_phyint != NULL);
16032 			phyi = tmp_ill->ill_phyint;
16033 			/*
16034 			 * Look at groups which has names only.
16035 			 */
16036 			if (phyi->phyint_groupname_len == 0)
16037 				continue;
16038 			/*
16039 			 * Names are stored in the phyint common to both
16040 			 * IPv4 and IPv6.
16041 			 */
16042 			if (mi_strcmp(phyi->phyint_groupname,
16043 			    groupname) == 0) {
16044 				break;
16045 			}
16046 		}
16047 	} else {
16048 		/*
16049 		 * If the caller passes in a NULL "grp_to_insert", we
16050 		 * allocate one below and insert this singleton.
16051 		 */
16052 		illgrp = grp_to_insert;
16053 	}
16054 
16055 	ill->ill_group_next = NULL;
16056 
16057 	if (illgrp == NULL) {
16058 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16059 		if (illgrp == NULL) {
16060 			return (ENOMEM);
16061 		}
16062 		illgrp->illgrp_next = *illgrp_head;
16063 		*illgrp_head = illgrp;
16064 		illgrp->illgrp_ill = ill;
16065 		illgrp->illgrp_ill_count = 1;
16066 		ill->ill_group = illgrp;
16067 		/*
16068 		 * Used in illgrp_scheduler to protect multiple threads
16069 		 * from traversing the list.
16070 		 */
16071 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16072 	} else {
16073 		ASSERT(ill->ill_net_type ==
16074 		    illgrp->illgrp_ill->ill_net_type);
16075 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16076 
16077 		/* Insert ill at tail of this group */
16078 		prev_ill = illgrp->illgrp_ill;
16079 		while (prev_ill->ill_group_next != NULL)
16080 			prev_ill = prev_ill->ill_group_next;
16081 		prev_ill->ill_group_next = ill;
16082 		ill->ill_group = illgrp;
16083 		illgrp->illgrp_ill_count++;
16084 		/*
16085 		 * Inherit group properties. Currently only forwarding
16086 		 * is the property we try to keep the same with all the
16087 		 * ills. When there are more, we will abstract this into
16088 		 * a function.
16089 		 */
16090 		ill->ill_flags &= ~ILLF_ROUTER;
16091 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16092 	}
16093 	mutex_exit(&ill->ill_lock);
16094 	rw_exit(&ipst->ips_ill_g_lock);
16095 
16096 	/*
16097 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16098 	 *    may be zero as it has not yet been bumped. But the ires
16099 	 *    have already been added. So, we do the nomination here
16100 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16101 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16102 	 *    ill_ipif_up_count here while nominating broadcast ires for
16103 	 *    receive.
16104 	 *
16105 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16106 	 *    to group them properly as ire_add() has already happened
16107 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16108 	 *    case, we need to do it here anyway.
16109 	 */
16110 	if (!ill->ill_isv6) {
16111 		ill_group_bcast_for_xmit(ill);
16112 		ill_nominate_bcast_rcv(illgrp);
16113 	}
16114 
16115 	if (!ipif_is_coming_up) {
16116 		/*
16117 		 * When ipif_up_done() calls this function, the multicast
16118 		 * groups have not been joined yet. So, there is no point in
16119 		 * nomination. ip_join_allmulti will handle groups when
16120 		 * ill_recover_multicast is called from ipif_up_done() later.
16121 		 */
16122 		(void) ill_nominate_mcast_rcv(illgrp);
16123 		/*
16124 		 * ipif_up_done calls ill_update_source_selection
16125 		 * anyway. Moreover, we don't want to re-create
16126 		 * interface routes while ipif_up_done() still has reference
16127 		 * to them. Refer to ipif_up_done() for more details.
16128 		 */
16129 		ill_update_source_selection(ill);
16130 	}
16131 
16132 	/*
16133 	 * Send a routing sockets message if we are inserting into
16134 	 * groups with names.
16135 	 */
16136 	if (groupname != NULL)
16137 		ip_rts_ifmsg(ill->ill_ipif);
16138 	return (0);
16139 }
16140 
16141 /*
16142  * Return the first phyint matching the groupname. There could
16143  * be more than one when there are ill groups.
16144  *
16145  * If 'usable' is set, then we exclude ones that are marked with any of
16146  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16147  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16148  * emulation of ipmp.
16149  */
16150 phyint_t *
16151 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16152 {
16153 	phyint_t *phyi;
16154 
16155 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16156 	/*
16157 	 * Group names are stored in the phyint - a common structure
16158 	 * to both IPv4 and IPv6.
16159 	 */
16160 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16161 	for (; phyi != NULL;
16162 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16163 	    phyi, AVL_AFTER)) {
16164 		if (phyi->phyint_groupname_len == 0)
16165 			continue;
16166 		/*
16167 		 * Skip the ones that should not be used since the callers
16168 		 * sometime use this for sending packets.
16169 		 */
16170 		if (usable && (phyi->phyint_flags &
16171 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16172 			continue;
16173 
16174 		ASSERT(phyi->phyint_groupname != NULL);
16175 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16176 			return (phyi);
16177 	}
16178 	return (NULL);
16179 }
16180 
16181 
16182 /*
16183  * Return the first usable phyint matching the group index. By 'usable'
16184  * we exclude ones that are marked ununsable with any of
16185  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16186  *
16187  * Used only for the ipmp/netinfo emulation of ipmp.
16188  */
16189 phyint_t *
16190 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16191 {
16192 	phyint_t *phyi;
16193 
16194 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16195 
16196 	if (!ipst->ips_ipmp_hook_emulation)
16197 		return (NULL);
16198 
16199 	/*
16200 	 * Group indicies are stored in the phyint - a common structure
16201 	 * to both IPv4 and IPv6.
16202 	 */
16203 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16204 	for (; phyi != NULL;
16205 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16206 	    phyi, AVL_AFTER)) {
16207 		/* Ignore the ones that do not have a group */
16208 		if (phyi->phyint_groupname_len == 0)
16209 			continue;
16210 
16211 		ASSERT(phyi->phyint_group_ifindex != 0);
16212 		/*
16213 		 * Skip the ones that should not be used since the callers
16214 		 * sometime use this for sending packets.
16215 		 */
16216 		if (phyi->phyint_flags &
16217 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16218 			continue;
16219 		if (phyi->phyint_group_ifindex == group_ifindex)
16220 			return (phyi);
16221 	}
16222 	return (NULL);
16223 }
16224 
16225 /*
16226  * MT notes on creation and deletion of IPMP groups
16227  *
16228  * Creation and deletion of IPMP groups introduce the need to merge or
16229  * split the associated serialization objects i.e the ipsq's. Normally all
16230  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16231  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16232  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16233  * is a need to change the <ill-ipsq> association and we have to operate on both
16234  * the source and destination IPMP groups. For eg. attempting to set the
16235  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16236  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16237  * source or destination IPMP group are mapped to a single ipsq for executing
16238  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16239  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16240  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16241  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16242  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16243  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16244  *
16245  * In the above example the ioctl handling code locates the current ipsq of hme0
16246  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16247  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16248  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16249  * the destination ipsq. If the destination ipsq is not busy, it also enters
16250  * the destination ipsq exclusively. Now the actual groupname setting operation
16251  * can proceed. If the destination ipsq is busy, the operation is enqueued
16252  * on the destination (merged) ipsq and will be handled in the unwind from
16253  * ipsq_exit.
16254  *
16255  * To prevent other threads accessing the ill while the group name change is
16256  * in progres, we bring down the ipifs which also removes the ill from the
16257  * group. The group is changed in phyint and when the first ipif on the ill
16258  * is brought up, the ill is inserted into the right IPMP group by
16259  * illgrp_insert.
16260  */
16261 /* ARGSUSED */
16262 int
16263 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16264     ip_ioctl_cmd_t *ipip, void *ifreq)
16265 {
16266 	int i;
16267 	char *tmp;
16268 	int namelen;
16269 	ill_t *ill = ipif->ipif_ill;
16270 	ill_t *ill_v4, *ill_v6;
16271 	int err = 0;
16272 	phyint_t *phyi;
16273 	phyint_t *phyi_tmp;
16274 	struct lifreq *lifr;
16275 	mblk_t	*mp1;
16276 	char *groupname;
16277 	ipsq_t *ipsq;
16278 	ip_stack_t	*ipst = ill->ill_ipst;
16279 
16280 	ASSERT(IAM_WRITER_IPIF(ipif));
16281 
16282 	/* Existance verified in ip_wput_nondata */
16283 	mp1 = mp->b_cont->b_cont;
16284 	lifr = (struct lifreq *)mp1->b_rptr;
16285 	groupname = lifr->lifr_groupname;
16286 
16287 	if (ipif->ipif_id != 0)
16288 		return (EINVAL);
16289 
16290 	phyi = ill->ill_phyint;
16291 	ASSERT(phyi != NULL);
16292 
16293 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16294 		return (EINVAL);
16295 
16296 	tmp = groupname;
16297 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16298 		;
16299 
16300 	if (i == LIFNAMSIZ) {
16301 		/* no null termination */
16302 		return (EINVAL);
16303 	}
16304 
16305 	/*
16306 	 * Calculate the namelen exclusive of the null
16307 	 * termination character.
16308 	 */
16309 	namelen = tmp - groupname;
16310 
16311 	ill_v4 = phyi->phyint_illv4;
16312 	ill_v6 = phyi->phyint_illv6;
16313 
16314 	/*
16315 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16316 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16317 	 * synchronization notes in ip.c
16318 	 */
16319 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16320 		return (EINVAL);
16321 	}
16322 
16323 	/*
16324 	 * mark the ill as changing.
16325 	 * this should queue all new requests on the syncq.
16326 	 */
16327 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16328 
16329 	if (ill_v4 != NULL)
16330 		ill_v4->ill_state_flags |= ILL_CHANGING;
16331 	if (ill_v6 != NULL)
16332 		ill_v6->ill_state_flags |= ILL_CHANGING;
16333 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16334 
16335 	if (namelen == 0) {
16336 		/*
16337 		 * Null string means remove this interface from the
16338 		 * existing group.
16339 		 */
16340 		if (phyi->phyint_groupname_len == 0) {
16341 			/*
16342 			 * Never was in a group.
16343 			 */
16344 			err = 0;
16345 			goto done;
16346 		}
16347 
16348 		/*
16349 		 * IPv4 or IPv6 may be temporarily out of the group when all
16350 		 * the ipifs are down. Thus, we need to check for ill_group to
16351 		 * be non-NULL.
16352 		 */
16353 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16354 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16355 			mutex_enter(&ill_v4->ill_lock);
16356 			if (!ill_is_quiescent(ill_v4)) {
16357 				/*
16358 				 * ipsq_pending_mp_add will not fail since
16359 				 * connp is NULL
16360 				 */
16361 				(void) ipsq_pending_mp_add(NULL,
16362 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16363 				mutex_exit(&ill_v4->ill_lock);
16364 				err = EINPROGRESS;
16365 				goto done;
16366 			}
16367 			mutex_exit(&ill_v4->ill_lock);
16368 		}
16369 
16370 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16371 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16372 			mutex_enter(&ill_v6->ill_lock);
16373 			if (!ill_is_quiescent(ill_v6)) {
16374 				(void) ipsq_pending_mp_add(NULL,
16375 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16376 				mutex_exit(&ill_v6->ill_lock);
16377 				err = EINPROGRESS;
16378 				goto done;
16379 			}
16380 			mutex_exit(&ill_v6->ill_lock);
16381 		}
16382 
16383 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16384 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16385 		mutex_enter(&phyi->phyint_lock);
16386 		ASSERT(phyi->phyint_groupname != NULL);
16387 		mi_free(phyi->phyint_groupname);
16388 		phyi->phyint_groupname = NULL;
16389 		phyi->phyint_groupname_len = 0;
16390 
16391 		/* Restore the ifindex used to be the per interface one */
16392 		phyi->phyint_group_ifindex = 0;
16393 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16394 		mutex_exit(&phyi->phyint_lock);
16395 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16396 		rw_exit(&ipst->ips_ill_g_lock);
16397 		err = ill_up_ipifs(ill, q, mp);
16398 
16399 		/*
16400 		 * set the split flag so that the ipsq can be split
16401 		 */
16402 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16403 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16404 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16405 
16406 	} else {
16407 		if (phyi->phyint_groupname_len != 0) {
16408 			ASSERT(phyi->phyint_groupname != NULL);
16409 			/* Are we inserting in the same group ? */
16410 			if (mi_strcmp(groupname,
16411 			    phyi->phyint_groupname) == 0) {
16412 				err = 0;
16413 				goto done;
16414 			}
16415 		}
16416 
16417 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16418 		/*
16419 		 * Merge ipsq for the group's.
16420 		 * This check is here as multiple groups/ills might be
16421 		 * sharing the same ipsq.
16422 		 * If we have to merege than the operation is restarted
16423 		 * on the new ipsq.
16424 		 */
16425 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16426 		if (phyi->phyint_ipsq != ipsq) {
16427 			rw_exit(&ipst->ips_ill_g_lock);
16428 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16429 			goto done;
16430 		}
16431 		/*
16432 		 * Running exclusive on new ipsq.
16433 		 */
16434 
16435 		ASSERT(ipsq != NULL);
16436 		ASSERT(ipsq->ipsq_writer == curthread);
16437 
16438 		/*
16439 		 * Check whether the ill_type and ill_net_type matches before
16440 		 * we allocate any memory so that the cleanup is easier.
16441 		 *
16442 		 * We can't group dissimilar ones as we can't load spread
16443 		 * packets across the group because of potential link-level
16444 		 * header differences.
16445 		 */
16446 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16447 		if (phyi_tmp != NULL) {
16448 			if ((ill_v4 != NULL &&
16449 			    phyi_tmp->phyint_illv4 != NULL) &&
16450 			    ((ill_v4->ill_net_type !=
16451 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16452 			    (ill_v4->ill_type !=
16453 			    phyi_tmp->phyint_illv4->ill_type))) {
16454 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16455 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16456 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16457 				rw_exit(&ipst->ips_ill_g_lock);
16458 				return (EINVAL);
16459 			}
16460 			if ((ill_v6 != NULL &&
16461 			    phyi_tmp->phyint_illv6 != NULL) &&
16462 			    ((ill_v6->ill_net_type !=
16463 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16464 			    (ill_v6->ill_type !=
16465 			    phyi_tmp->phyint_illv6->ill_type))) {
16466 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16467 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16468 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16469 				rw_exit(&ipst->ips_ill_g_lock);
16470 				return (EINVAL);
16471 			}
16472 		}
16473 
16474 		rw_exit(&ipst->ips_ill_g_lock);
16475 
16476 		/*
16477 		 * bring down all v4 ipifs.
16478 		 */
16479 		if (ill_v4 != NULL) {
16480 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16481 		}
16482 
16483 		/*
16484 		 * bring down all v6 ipifs.
16485 		 */
16486 		if (ill_v6 != NULL) {
16487 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16488 		}
16489 
16490 		/*
16491 		 * make sure all ipifs are down and there are no active
16492 		 * references. Call to ipsq_pending_mp_add will not fail
16493 		 * since connp is NULL.
16494 		 */
16495 		if (ill_v4 != NULL) {
16496 			mutex_enter(&ill_v4->ill_lock);
16497 			if (!ill_is_quiescent(ill_v4)) {
16498 				(void) ipsq_pending_mp_add(NULL,
16499 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16500 				mutex_exit(&ill_v4->ill_lock);
16501 				err = EINPROGRESS;
16502 				goto done;
16503 			}
16504 			mutex_exit(&ill_v4->ill_lock);
16505 		}
16506 
16507 		if (ill_v6 != NULL) {
16508 			mutex_enter(&ill_v6->ill_lock);
16509 			if (!ill_is_quiescent(ill_v6)) {
16510 				(void) ipsq_pending_mp_add(NULL,
16511 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16512 				mutex_exit(&ill_v6->ill_lock);
16513 				err = EINPROGRESS;
16514 				goto done;
16515 			}
16516 			mutex_exit(&ill_v6->ill_lock);
16517 		}
16518 
16519 		/*
16520 		 * allocate including space for null terminator
16521 		 * before we insert.
16522 		 */
16523 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16524 		if (tmp == NULL)
16525 			return (ENOMEM);
16526 
16527 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16528 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16529 		mutex_enter(&phyi->phyint_lock);
16530 		if (phyi->phyint_groupname_len != 0) {
16531 			ASSERT(phyi->phyint_groupname != NULL);
16532 			mi_free(phyi->phyint_groupname);
16533 		}
16534 
16535 		/*
16536 		 * setup the new group name.
16537 		 */
16538 		phyi->phyint_groupname = tmp;
16539 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16540 		phyi->phyint_groupname_len = namelen + 1;
16541 
16542 		if (ipst->ips_ipmp_hook_emulation) {
16543 			/*
16544 			 * If the group already exists we use the existing
16545 			 * group_ifindex, otherwise we pick a new index here.
16546 			 */
16547 			if (phyi_tmp != NULL) {
16548 				phyi->phyint_group_ifindex =
16549 				    phyi_tmp->phyint_group_ifindex;
16550 			} else {
16551 				/* XXX We need a recovery strategy here. */
16552 				if (!ip_assign_ifindex(
16553 				    &phyi->phyint_group_ifindex, ipst))
16554 					cmn_err(CE_PANIC,
16555 					    "ip_assign_ifindex() failed");
16556 			}
16557 		}
16558 		/*
16559 		 * Select whether the netinfo and hook use the per-interface
16560 		 * or per-group ifindex.
16561 		 */
16562 		if (ipst->ips_ipmp_hook_emulation)
16563 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16564 		else
16565 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16566 
16567 		if (ipst->ips_ipmp_hook_emulation &&
16568 		    phyi_tmp != NULL) {
16569 			/* First phyint in group - group PLUMB event */
16570 			ill_nic_info_plumb(ill, B_TRUE);
16571 		}
16572 		mutex_exit(&phyi->phyint_lock);
16573 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16574 		rw_exit(&ipst->ips_ill_g_lock);
16575 
16576 		err = ill_up_ipifs(ill, q, mp);
16577 	}
16578 
16579 done:
16580 	/*
16581 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16582 	 */
16583 	if (err != EINPROGRESS) {
16584 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16585 		if (ill_v4 != NULL)
16586 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16587 		if (ill_v6 != NULL)
16588 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16589 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16590 	}
16591 	return (err);
16592 }
16593 
16594 /* ARGSUSED */
16595 int
16596 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16597     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16598 {
16599 	ill_t *ill;
16600 	phyint_t *phyi;
16601 	struct lifreq *lifr;
16602 	mblk_t	*mp1;
16603 
16604 	/* Existence verified in ip_wput_nondata */
16605 	mp1 = mp->b_cont->b_cont;
16606 	lifr = (struct lifreq *)mp1->b_rptr;
16607 	ill = ipif->ipif_ill;
16608 	phyi = ill->ill_phyint;
16609 
16610 	lifr->lifr_groupname[0] = '\0';
16611 	/*
16612 	 * ill_group may be null if all the interfaces
16613 	 * are down. But still, the phyint should always
16614 	 * hold the name.
16615 	 */
16616 	if (phyi->phyint_groupname_len != 0) {
16617 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16618 		    phyi->phyint_groupname_len);
16619 	}
16620 
16621 	return (0);
16622 }
16623 
16624 
16625 typedef struct conn_move_s {
16626 	ill_t	*cm_from_ill;
16627 	ill_t	*cm_to_ill;
16628 	int	cm_ifindex;
16629 } conn_move_t;
16630 
16631 /*
16632  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16633  */
16634 static void
16635 conn_move(conn_t *connp, caddr_t arg)
16636 {
16637 	conn_move_t *connm;
16638 	int ifindex;
16639 	int i;
16640 	ill_t *from_ill;
16641 	ill_t *to_ill;
16642 	ilg_t *ilg;
16643 	ilm_t *ret_ilm;
16644 
16645 	connm = (conn_move_t *)arg;
16646 	ifindex = connm->cm_ifindex;
16647 	from_ill = connm->cm_from_ill;
16648 	to_ill = connm->cm_to_ill;
16649 
16650 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16651 
16652 	/* All multicast fields protected by conn_lock */
16653 	mutex_enter(&connp->conn_lock);
16654 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16655 	if ((connp->conn_outgoing_ill == from_ill) &&
16656 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16657 		connp->conn_outgoing_ill = to_ill;
16658 		connp->conn_incoming_ill = to_ill;
16659 	}
16660 
16661 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16662 
16663 	if ((connp->conn_multicast_ill == from_ill) &&
16664 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16665 		connp->conn_multicast_ill = connm->cm_to_ill;
16666 	}
16667 
16668 	/*
16669 	 * Change the ilg_ill to point to the new one. This assumes
16670 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16671 	 * has been told to receive packets on this interface.
16672 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16673 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16674 	 * some ilms may not have moved. We check to see whether
16675 	 * the ilms have moved to to_ill. We can't check on from_ill
16676 	 * as in the process of moving, we could have split an ilm
16677 	 * in to two - which has the same orig_ifindex and v6group.
16678 	 *
16679 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16680 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16681 	 */
16682 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16683 		ilg = &connp->conn_ilg[i];
16684 		if ((ilg->ilg_ill == from_ill) &&
16685 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16686 			/* ifindex != 0 indicates failback */
16687 			if (ifindex != 0) {
16688 				connp->conn_ilg[i].ilg_ill = to_ill;
16689 				continue;
16690 			}
16691 
16692 			mutex_enter(&to_ill->ill_lock);
16693 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16694 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16695 			    connp->conn_zoneid);
16696 			mutex_exit(&to_ill->ill_lock);
16697 
16698 			if (ret_ilm != NULL)
16699 				connp->conn_ilg[i].ilg_ill = to_ill;
16700 		}
16701 	}
16702 	mutex_exit(&connp->conn_lock);
16703 }
16704 
16705 static void
16706 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16707 {
16708 	conn_move_t connm;
16709 	ip_stack_t	*ipst = from_ill->ill_ipst;
16710 
16711 	connm.cm_from_ill = from_ill;
16712 	connm.cm_to_ill = to_ill;
16713 	connm.cm_ifindex = ifindex;
16714 
16715 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16716 }
16717 
16718 /*
16719  * ilm has been moved from from_ill to to_ill.
16720  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16721  * appropriately.
16722  *
16723  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16724  *	  the code there de-references ipif_ill to get the ill to
16725  *	  send multicast requests. It does not work as ipif is on its
16726  *	  move and already moved when this function is called.
16727  *	  Thus, we need to use from_ill and to_ill send down multicast
16728  *	  requests.
16729  */
16730 static void
16731 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16732 {
16733 	ipif_t *ipif;
16734 	ilm_t *ilm;
16735 
16736 	/*
16737 	 * See whether we need to send down DL_ENABMULTI_REQ on
16738 	 * to_ill as ilm has just been added.
16739 	 */
16740 	ASSERT(IAM_WRITER_ILL(to_ill));
16741 	ASSERT(IAM_WRITER_ILL(from_ill));
16742 
16743 	ILM_WALKER_HOLD(to_ill);
16744 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16745 
16746 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16747 			continue;
16748 		/*
16749 		 * no locks held, ill/ipif cannot dissappear as long
16750 		 * as we are writer.
16751 		 */
16752 		ipif = to_ill->ill_ipif;
16753 		/*
16754 		 * No need to hold any lock as we are the writer and this
16755 		 * can only be changed by a writer.
16756 		 */
16757 		ilm->ilm_is_new = B_FALSE;
16758 
16759 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16760 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16761 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16762 			    "resolver\n"));
16763 			continue;		/* Must be IRE_IF_NORESOLVER */
16764 		}
16765 
16766 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16767 			ip1dbg(("ilm_send_multicast_reqs: "
16768 			    "to_ill MULTI_BCAST\n"));
16769 			goto from;
16770 		}
16771 
16772 		if (to_ill->ill_isv6)
16773 			mld_joingroup(ilm);
16774 		else
16775 			igmp_joingroup(ilm);
16776 
16777 		if (to_ill->ill_ipif_up_count == 0) {
16778 			/*
16779 			 * Nobody there. All multicast addresses will be
16780 			 * re-joined when we get the DL_BIND_ACK bringing the
16781 			 * interface up.
16782 			 */
16783 			ilm->ilm_notify_driver = B_FALSE;
16784 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16785 			goto from;
16786 		}
16787 
16788 		/*
16789 		 * For allmulti address, we want to join on only one interface.
16790 		 * Checking for ilm_numentries_v6 is not correct as you may
16791 		 * find an ilm with zero address on to_ill, but we may not
16792 		 * have nominated to_ill for receiving. Thus, if we have
16793 		 * nominated from_ill (ill_join_allmulti is set), nominate
16794 		 * only if to_ill is not already nominated (to_ill normally
16795 		 * should not have been nominated if "from_ill" has already
16796 		 * been nominated. As we don't prevent failovers from happening
16797 		 * across groups, we don't assert).
16798 		 */
16799 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16800 			/*
16801 			 * There is no need to hold ill locks as we are
16802 			 * writer on both ills and when ill_join_allmulti
16803 			 * is changed the thread is always a writer.
16804 			 */
16805 			if (from_ill->ill_join_allmulti &&
16806 			    !to_ill->ill_join_allmulti) {
16807 				(void) ip_join_allmulti(to_ill->ill_ipif);
16808 			}
16809 		} else if (ilm->ilm_notify_driver) {
16810 
16811 			/*
16812 			 * This is a newly moved ilm so we need to tell the
16813 			 * driver about the new group. There can be more than
16814 			 * one ilm's for the same group in the list each with a
16815 			 * different orig_ifindex. We have to inform the driver
16816 			 * once. In ilm_move_v[4,6] we only set the flag
16817 			 * ilm_notify_driver for the first ilm.
16818 			 */
16819 
16820 			(void) ip_ll_send_enabmulti_req(to_ill,
16821 			    &ilm->ilm_v6addr);
16822 		}
16823 
16824 		ilm->ilm_notify_driver = B_FALSE;
16825 
16826 		/*
16827 		 * See whether we need to send down DL_DISABMULTI_REQ on
16828 		 * from_ill as ilm has just been removed.
16829 		 */
16830 from:
16831 		ipif = from_ill->ill_ipif;
16832 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16833 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16834 			ip1dbg(("ilm_send_multicast_reqs: "
16835 			    "from_ill not resolver\n"));
16836 			continue;		/* Must be IRE_IF_NORESOLVER */
16837 		}
16838 
16839 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16840 			ip1dbg(("ilm_send_multicast_reqs: "
16841 			    "from_ill MULTI_BCAST\n"));
16842 			continue;
16843 		}
16844 
16845 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16846 			if (from_ill->ill_join_allmulti)
16847 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16848 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16849 			(void) ip_ll_send_disabmulti_req(from_ill,
16850 			    &ilm->ilm_v6addr);
16851 		}
16852 	}
16853 	ILM_WALKER_RELE(to_ill);
16854 }
16855 
16856 /*
16857  * This function is called when all multicast memberships needs
16858  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16859  * called only once unlike the IPv4 counterpart where it is called after
16860  * every logical interface is moved. The reason is due to multicast
16861  * memberships are joined using an interface address in IPv4 while in
16862  * IPv6, interface index is used.
16863  */
16864 static void
16865 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16866 {
16867 	ilm_t	*ilm;
16868 	ilm_t	*ilm_next;
16869 	ilm_t	*new_ilm;
16870 	ilm_t	**ilmp;
16871 	int	count;
16872 	char buf[INET6_ADDRSTRLEN];
16873 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16874 	ip_stack_t	*ipst = from_ill->ill_ipst;
16875 
16876 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16877 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16878 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16879 
16880 	if (ifindex == 0) {
16881 		/*
16882 		 * Form the solicited node mcast address which is used later.
16883 		 */
16884 		ipif_t *ipif;
16885 
16886 		ipif = from_ill->ill_ipif;
16887 		ASSERT(ipif->ipif_id == 0);
16888 
16889 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16890 	}
16891 
16892 	ilmp = &from_ill->ill_ilm;
16893 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16894 		ilm_next = ilm->ilm_next;
16895 
16896 		if (ilm->ilm_flags & ILM_DELETED) {
16897 			ilmp = &ilm->ilm_next;
16898 			continue;
16899 		}
16900 
16901 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16902 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16903 		ASSERT(ilm->ilm_orig_ifindex != 0);
16904 		if (ilm->ilm_orig_ifindex == ifindex) {
16905 			/*
16906 			 * We are failing back multicast memberships.
16907 			 * If the same ilm exists in to_ill, it means somebody
16908 			 * has joined the same group there e.g. ff02::1
16909 			 * is joined within the kernel when the interfaces
16910 			 * came UP.
16911 			 */
16912 			ASSERT(ilm->ilm_ipif == NULL);
16913 			if (new_ilm != NULL) {
16914 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16915 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16916 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16917 					new_ilm->ilm_is_new = B_TRUE;
16918 				}
16919 			} else {
16920 				/*
16921 				 * check if we can just move the ilm
16922 				 */
16923 				if (from_ill->ill_ilm_walker_cnt != 0) {
16924 					/*
16925 					 * We have walkers we cannot move
16926 					 * the ilm, so allocate a new ilm,
16927 					 * this (old) ilm will be marked
16928 					 * ILM_DELETED at the end of the loop
16929 					 * and will be freed when the
16930 					 * last walker exits.
16931 					 */
16932 					new_ilm = (ilm_t *)mi_zalloc
16933 					    (sizeof (ilm_t));
16934 					if (new_ilm == NULL) {
16935 						ip0dbg(("ilm_move_v6: "
16936 						    "FAILBACK of IPv6"
16937 						    " multicast address %s : "
16938 						    "from %s to"
16939 						    " %s failed : ENOMEM \n",
16940 						    inet_ntop(AF_INET6,
16941 						    &ilm->ilm_v6addr, buf,
16942 						    sizeof (buf)),
16943 						    from_ill->ill_name,
16944 						    to_ill->ill_name));
16945 
16946 							ilmp = &ilm->ilm_next;
16947 							continue;
16948 					}
16949 					*new_ilm = *ilm;
16950 					/*
16951 					 * we don't want new_ilm linked to
16952 					 * ilm's filter list.
16953 					 */
16954 					new_ilm->ilm_filter = NULL;
16955 				} else {
16956 					/*
16957 					 * No walkers we can move the ilm.
16958 					 * lets take it out of the list.
16959 					 */
16960 					*ilmp = ilm->ilm_next;
16961 					ilm->ilm_next = NULL;
16962 					DTRACE_PROBE3(ill__decr__cnt,
16963 					    (ill_t *), from_ill,
16964 					    (char *), "ilm", (void *), ilm);
16965 					ASSERT(from_ill->ill_ilm_cnt > 0);
16966 					from_ill->ill_ilm_cnt--;
16967 
16968 					new_ilm = ilm;
16969 				}
16970 
16971 				/*
16972 				 * if this is the first ilm for the group
16973 				 * set ilm_notify_driver so that we notify the
16974 				 * driver in ilm_send_multicast_reqs.
16975 				 */
16976 				if (ilm_lookup_ill_v6(to_ill,
16977 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16978 					new_ilm->ilm_notify_driver = B_TRUE;
16979 
16980 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16981 				    (char *), "ilm", (void *), new_ilm);
16982 				new_ilm->ilm_ill = to_ill;
16983 				to_ill->ill_ilm_cnt++;
16984 
16985 				/* Add to the to_ill's list */
16986 				new_ilm->ilm_next = to_ill->ill_ilm;
16987 				to_ill->ill_ilm = new_ilm;
16988 				/*
16989 				 * set the flag so that mld_joingroup is
16990 				 * called in ilm_send_multicast_reqs().
16991 				 */
16992 				new_ilm->ilm_is_new = B_TRUE;
16993 			}
16994 			goto bottom;
16995 		} else if (ifindex != 0) {
16996 			/*
16997 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16998 			 * has not matched above, look at the next ilm.
16999 			 */
17000 			ilmp = &ilm->ilm_next;
17001 			continue;
17002 		}
17003 		/*
17004 		 * If we are here, it means ifindex is 0. Failover
17005 		 * everything.
17006 		 *
17007 		 * We need to handle solicited node mcast address
17008 		 * and all_nodes mcast address differently as they
17009 		 * are joined witin the kenrel (ipif_multicast_up)
17010 		 * and potentially from the userland. We are called
17011 		 * after the ipifs of from_ill has been moved.
17012 		 * If we still find ilms on ill with solicited node
17013 		 * mcast address or all_nodes mcast address, it must
17014 		 * belong to the UP interface that has not moved e.g.
17015 		 * ipif_id 0 with the link local prefix does not move.
17016 		 * We join this on the new ill accounting for all the
17017 		 * userland memberships so that applications don't
17018 		 * see any failure.
17019 		 *
17020 		 * We need to make sure that we account only for the
17021 		 * solicited node and all node multicast addresses
17022 		 * that was brought UP on these. In the case of
17023 		 * a failover from A to B, we might have ilms belonging
17024 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17025 		 * for the membership from the userland. If we are failing
17026 		 * over from B to C now, we will find the ones belonging
17027 		 * to A on B. These don't account for the ill_ipif_up_count.
17028 		 * They just move from B to C. The check below on
17029 		 * ilm_orig_ifindex ensures that.
17030 		 */
17031 		if ((ilm->ilm_orig_ifindex ==
17032 		    from_ill->ill_phyint->phyint_ifindex) &&
17033 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17034 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17035 		    &ilm->ilm_v6addr))) {
17036 			ASSERT(ilm->ilm_refcnt > 0);
17037 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17038 			/*
17039 			 * For indentation reasons, we are not using a
17040 			 * "else" here.
17041 			 */
17042 			if (count == 0) {
17043 				ilmp = &ilm->ilm_next;
17044 				continue;
17045 			}
17046 			ilm->ilm_refcnt -= count;
17047 			if (new_ilm != NULL) {
17048 				/*
17049 				 * Can find one with the same
17050 				 * ilm_orig_ifindex, if we are failing
17051 				 * over to a STANDBY. This happens
17052 				 * when somebody wants to join a group
17053 				 * on a STANDBY interface and we
17054 				 * internally join on a different one.
17055 				 * If we had joined on from_ill then, a
17056 				 * failover now will find a new ilm
17057 				 * with this index.
17058 				 */
17059 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17060 				    " new ilm on %s, group address %s\n",
17061 				    to_ill->ill_name,
17062 				    inet_ntop(AF_INET6,
17063 				    &ilm->ilm_v6addr, buf,
17064 				    sizeof (buf))));
17065 				new_ilm->ilm_refcnt += count;
17066 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17067 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17068 					new_ilm->ilm_is_new = B_TRUE;
17069 				}
17070 			} else {
17071 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17072 				if (new_ilm == NULL) {
17073 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17074 					    " multicast address %s : from %s to"
17075 					    " %s failed : ENOMEM \n",
17076 					    inet_ntop(AF_INET6,
17077 					    &ilm->ilm_v6addr, buf,
17078 					    sizeof (buf)), from_ill->ill_name,
17079 					    to_ill->ill_name));
17080 					ilmp = &ilm->ilm_next;
17081 					continue;
17082 				}
17083 				*new_ilm = *ilm;
17084 				new_ilm->ilm_filter = NULL;
17085 				new_ilm->ilm_refcnt = count;
17086 				new_ilm->ilm_timer = INFINITY;
17087 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17088 				new_ilm->ilm_is_new = B_TRUE;
17089 				/*
17090 				 * If the to_ill has not joined this
17091 				 * group we need to tell the driver in
17092 				 * ill_send_multicast_reqs.
17093 				 */
17094 				if (ilm_lookup_ill_v6(to_ill,
17095 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17096 					new_ilm->ilm_notify_driver = B_TRUE;
17097 
17098 				new_ilm->ilm_ill = to_ill;
17099 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17100 				    (char *), "ilm", (void *), new_ilm);
17101 				to_ill->ill_ilm_cnt++;
17102 
17103 				/* Add to the to_ill's list */
17104 				new_ilm->ilm_next = to_ill->ill_ilm;
17105 				to_ill->ill_ilm = new_ilm;
17106 				ASSERT(new_ilm->ilm_ipif == NULL);
17107 			}
17108 			if (ilm->ilm_refcnt == 0) {
17109 				goto bottom;
17110 			} else {
17111 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17112 				CLEAR_SLIST(new_ilm->ilm_filter);
17113 				ilmp = &ilm->ilm_next;
17114 			}
17115 			continue;
17116 		} else {
17117 			/*
17118 			 * ifindex = 0 means, move everything pointing at
17119 			 * from_ill. We are doing this becuase ill has
17120 			 * either FAILED or became INACTIVE.
17121 			 *
17122 			 * As we would like to move things later back to
17123 			 * from_ill, we want to retain the identity of this
17124 			 * ilm. Thus, we don't blindly increment the reference
17125 			 * count on the ilms matching the address alone. We
17126 			 * need to match on the ilm_orig_index also. new_ilm
17127 			 * was obtained by matching ilm_orig_index also.
17128 			 */
17129 			if (new_ilm != NULL) {
17130 				/*
17131 				 * This is possible only if a previous restore
17132 				 * was incomplete i.e restore to
17133 				 * ilm_orig_ifindex left some ilms because
17134 				 * of some failures. Thus when we are failing
17135 				 * again, we might find our old friends there.
17136 				 */
17137 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17138 				    " on %s, group address %s\n",
17139 				    to_ill->ill_name,
17140 				    inet_ntop(AF_INET6,
17141 				    &ilm->ilm_v6addr, buf,
17142 				    sizeof (buf))));
17143 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17144 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17145 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17146 					new_ilm->ilm_is_new = B_TRUE;
17147 				}
17148 			} else {
17149 				if (from_ill->ill_ilm_walker_cnt != 0) {
17150 					new_ilm = (ilm_t *)
17151 					    mi_zalloc(sizeof (ilm_t));
17152 					if (new_ilm == NULL) {
17153 						ip0dbg(("ilm_move_v6: "
17154 						    "FAILOVER of IPv6"
17155 						    " multicast address %s : "
17156 						    "from %s to"
17157 						    " %s failed : ENOMEM \n",
17158 						    inet_ntop(AF_INET6,
17159 						    &ilm->ilm_v6addr, buf,
17160 						    sizeof (buf)),
17161 						    from_ill->ill_name,
17162 						    to_ill->ill_name));
17163 
17164 							ilmp = &ilm->ilm_next;
17165 							continue;
17166 					}
17167 					*new_ilm = *ilm;
17168 					new_ilm->ilm_filter = NULL;
17169 				} else {
17170 					*ilmp = ilm->ilm_next;
17171 					DTRACE_PROBE3(ill__decr__cnt,
17172 					    (ill_t *), from_ill,
17173 					    (char *), "ilm", (void *), ilm);
17174 					ASSERT(from_ill->ill_ilm_cnt > 0);
17175 					from_ill->ill_ilm_cnt--;
17176 
17177 					new_ilm = ilm;
17178 				}
17179 				/*
17180 				 * If the to_ill has not joined this
17181 				 * group we need to tell the driver in
17182 				 * ill_send_multicast_reqs.
17183 				 */
17184 				if (ilm_lookup_ill_v6(to_ill,
17185 				    &new_ilm->ilm_v6addr, 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 				ASSERT(ilm->ilm_ipif == NULL);
17192 				new_ilm->ilm_ill = to_ill;
17193 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17194 				    (char *), "ilm", (void *), new_ilm);
17195 				to_ill->ill_ilm_cnt++;
17196 				new_ilm->ilm_is_new = B_TRUE;
17197 			}
17198 
17199 		}
17200 
17201 bottom:
17202 		/*
17203 		 * Revert multicast filter state to (EXCLUDE, NULL).
17204 		 * new_ilm->ilm_is_new should already be set if needed.
17205 		 */
17206 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17207 		CLEAR_SLIST(new_ilm->ilm_filter);
17208 		/*
17209 		 * We allocated/got a new ilm, free the old one.
17210 		 */
17211 		if (new_ilm != ilm) {
17212 			if (from_ill->ill_ilm_walker_cnt == 0) {
17213 				*ilmp = ilm->ilm_next;
17214 
17215 				ASSERT(ilm->ilm_ipif == NULL); /* ipv6 */
17216 				DTRACE_PROBE3(ill__decr__cnt, (ill_t *),
17217 				    from_ill, (char *), "ilm", (void *), ilm);
17218 				ASSERT(from_ill->ill_ilm_cnt > 0);
17219 				from_ill->ill_ilm_cnt--;
17220 
17221 				ilm_inactive(ilm); /* frees this ilm */
17222 
17223 			} else {
17224 				ilm->ilm_flags |= ILM_DELETED;
17225 				from_ill->ill_ilm_cleanup_reqd = 1;
17226 				ilmp = &ilm->ilm_next;
17227 			}
17228 		}
17229 	}
17230 }
17231 
17232 /*
17233  * Move all the multicast memberships to to_ill. Called when
17234  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17235  * different from IPv6 counterpart as multicast memberships are associated
17236  * with ills in IPv6. This function is called after every ipif is moved
17237  * unlike IPv6, where it is moved only once.
17238  */
17239 static void
17240 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17241 {
17242 	ilm_t	*ilm;
17243 	ilm_t	*ilm_next;
17244 	ilm_t	*new_ilm;
17245 	ilm_t	**ilmp;
17246 	ip_stack_t	*ipst = from_ill->ill_ipst;
17247 
17248 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17249 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17250 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17251 
17252 	ilmp = &from_ill->ill_ilm;
17253 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17254 		ilm_next = ilm->ilm_next;
17255 
17256 		if (ilm->ilm_flags & ILM_DELETED) {
17257 			ilmp = &ilm->ilm_next;
17258 			continue;
17259 		}
17260 
17261 		ASSERT(ilm->ilm_ipif != NULL);
17262 
17263 		if (ilm->ilm_ipif != ipif) {
17264 			ilmp = &ilm->ilm_next;
17265 			continue;
17266 		}
17267 
17268 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17269 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17270 			new_ilm = ilm_lookup_ipif(ipif,
17271 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17272 			if (new_ilm != NULL) {
17273 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17274 				/*
17275 				 * We still need to deal with the from_ill.
17276 				 */
17277 				new_ilm->ilm_is_new = B_TRUE;
17278 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17279 				CLEAR_SLIST(new_ilm->ilm_filter);
17280 				ASSERT(ilm->ilm_ipif == ipif);
17281 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17282 				if (from_ill->ill_ilm_walker_cnt == 0) {
17283 					DTRACE_PROBE3(ill__decr__cnt,
17284 					    (ill_t *), from_ill,
17285 					    (char *), "ilm", (void *), ilm);
17286 					ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17287 				}
17288 				goto delete_ilm;
17289 			}
17290 			/*
17291 			 * If we could not find one e.g. ipif is
17292 			 * still down on to_ill, we add this ilm
17293 			 * on ill_new to preserve the reference
17294 			 * count.
17295 			 */
17296 		}
17297 		/*
17298 		 * When ipifs move, ilms always move with it
17299 		 * to the NEW ill. Thus we should never be
17300 		 * able to find ilm till we really move it here.
17301 		 */
17302 		ASSERT(ilm_lookup_ipif(ipif,
17303 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17304 
17305 		if (from_ill->ill_ilm_walker_cnt != 0) {
17306 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17307 			if (new_ilm == NULL) {
17308 				char buf[INET6_ADDRSTRLEN];
17309 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17310 				    " multicast address %s : "
17311 				    "from %s to"
17312 				    " %s failed : ENOMEM \n",
17313 				    inet_ntop(AF_INET,
17314 				    &ilm->ilm_v6addr, buf,
17315 				    sizeof (buf)),
17316 				    from_ill->ill_name,
17317 				    to_ill->ill_name));
17318 
17319 				ilmp = &ilm->ilm_next;
17320 				continue;
17321 			}
17322 			*new_ilm = *ilm;
17323 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
17324 			    (char *), "ilm", (void *), ilm);
17325 			new_ilm->ilm_ipif->ipif_ilm_cnt++;
17326 			/* We don't want new_ilm linked to ilm's filter list */
17327 			new_ilm->ilm_filter = NULL;
17328 		} else {
17329 			/* Remove from the list */
17330 			*ilmp = ilm->ilm_next;
17331 			new_ilm = ilm;
17332 		}
17333 
17334 		/*
17335 		 * If we have never joined this group on the to_ill
17336 		 * make sure we tell the driver.
17337 		 */
17338 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17339 		    ALL_ZONES) == NULL)
17340 			new_ilm->ilm_notify_driver = B_TRUE;
17341 
17342 		/* Add to the to_ill's list */
17343 		new_ilm->ilm_next = to_ill->ill_ilm;
17344 		to_ill->ill_ilm = new_ilm;
17345 		new_ilm->ilm_is_new = B_TRUE;
17346 
17347 		/*
17348 		 * Revert multicast filter state to (EXCLUDE, NULL)
17349 		 */
17350 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17351 		CLEAR_SLIST(new_ilm->ilm_filter);
17352 
17353 		/*
17354 		 * Delete only if we have allocated a new ilm.
17355 		 */
17356 		if (new_ilm != ilm) {
17357 delete_ilm:
17358 			if (from_ill->ill_ilm_walker_cnt == 0) {
17359 				/* Remove from the list */
17360 				*ilmp = ilm->ilm_next;
17361 				ilm->ilm_next = NULL;
17362 				DTRACE_PROBE3(ipif__decr__cnt,
17363 				    (ipif_t *), ilm->ilm_ipif,
17364 				    (char *), "ilm", (void *), ilm);
17365 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17366 				ilm->ilm_ipif->ipif_ilm_cnt--;
17367 				ilm_inactive(ilm);
17368 			} else {
17369 				ilm->ilm_flags |= ILM_DELETED;
17370 				from_ill->ill_ilm_cleanup_reqd = 1;
17371 				ilmp = &ilm->ilm_next;
17372 			}
17373 		}
17374 	}
17375 }
17376 
17377 static uint_t
17378 ipif_get_id(ill_t *ill, uint_t id)
17379 {
17380 	uint_t	unit;
17381 	ipif_t	*tipif;
17382 	boolean_t found = B_FALSE;
17383 	ip_stack_t	*ipst = ill->ill_ipst;
17384 
17385 	/*
17386 	 * During failback, we want to go back to the same id
17387 	 * instead of the smallest id so that the original
17388 	 * configuration is maintained. id is non-zero in that
17389 	 * case.
17390 	 */
17391 	if (id != 0) {
17392 		/*
17393 		 * While failing back, if we still have an ipif with
17394 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17395 		 * as soon as we return from this function. It was
17396 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17397 		 * we can choose the smallest id. Thus we return zero
17398 		 * in that case ignoring the hint.
17399 		 */
17400 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17401 			return (0);
17402 		for (tipif = ill->ill_ipif; tipif != NULL;
17403 		    tipif = tipif->ipif_next) {
17404 			if (tipif->ipif_id == id) {
17405 				found = B_TRUE;
17406 				break;
17407 			}
17408 		}
17409 		/*
17410 		 * If somebody already plumbed another logical
17411 		 * with the same id, we won't be able to find it.
17412 		 */
17413 		if (!found)
17414 			return (id);
17415 	}
17416 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17417 		found = B_FALSE;
17418 		for (tipif = ill->ill_ipif; tipif != NULL;
17419 		    tipif = tipif->ipif_next) {
17420 			if (tipif->ipif_id == unit) {
17421 				found = B_TRUE;
17422 				break;
17423 			}
17424 		}
17425 		if (!found)
17426 			break;
17427 	}
17428 	return (unit);
17429 }
17430 
17431 /* ARGSUSED */
17432 static int
17433 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17434     ipif_t **rep_ipif_ptr)
17435 {
17436 	ill_t	*from_ill;
17437 	ipif_t	*rep_ipif;
17438 	uint_t	unit;
17439 	int err = 0;
17440 	ipif_t	*to_ipif;
17441 	struct iocblk	*iocp;
17442 	boolean_t failback_cmd;
17443 	boolean_t remove_ipif;
17444 	int	rc;
17445 	ip_stack_t	*ipst;
17446 
17447 	ASSERT(IAM_WRITER_ILL(to_ill));
17448 	ASSERT(IAM_WRITER_IPIF(ipif));
17449 
17450 	iocp = (struct iocblk *)mp->b_rptr;
17451 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17452 	remove_ipif = B_FALSE;
17453 
17454 	from_ill = ipif->ipif_ill;
17455 	ipst = from_ill->ill_ipst;
17456 
17457 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17458 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17459 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17460 
17461 	/*
17462 	 * Don't move LINK LOCAL addresses as they are tied to
17463 	 * physical interface.
17464 	 */
17465 	if (from_ill->ill_isv6 &&
17466 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17467 		ipif->ipif_was_up = B_FALSE;
17468 		IPIF_UNMARK_MOVING(ipif);
17469 		return (0);
17470 	}
17471 
17472 	/*
17473 	 * We set the ipif_id to maximum so that the search for
17474 	 * ipif_id will pick the lowest number i.e 0 in the
17475 	 * following 2 cases :
17476 	 *
17477 	 * 1) We have a replacement ipif at the head of to_ill.
17478 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17479 	 *    on to_ill and hence the MOVE might fail. We want to
17480 	 *    remove it only if we could move the ipif. Thus, by
17481 	 *    setting it to the MAX value, we make the search in
17482 	 *    ipif_get_id return the zeroth id.
17483 	 *
17484 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17485 	 *    we might just have a zero address plumbed on the ipif
17486 	 *    with zero id in the case of IPv4. We remove that while
17487 	 *    doing the failback. We want to remove it only if we
17488 	 *    could move the ipif. Thus, by setting it to the MAX
17489 	 *    value, we make the search in ipif_get_id return the
17490 	 *    zeroth id.
17491 	 *
17492 	 * Both (1) and (2) are done only when when we are moving
17493 	 * an ipif (either due to failover/failback) which originally
17494 	 * belonged to this interface i.e the ipif_orig_ifindex is
17495 	 * the same as to_ill's ifindex. This is needed so that
17496 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17497 	 * from B -> A (B is being removed from the group) and
17498 	 * FAILBACK from A -> B restores the original configuration.
17499 	 * Without the check for orig_ifindex, the second FAILOVER
17500 	 * could make the ipif belonging to B replace the A's zeroth
17501 	 * ipif and the subsequent failback re-creating the replacement
17502 	 * ipif again.
17503 	 *
17504 	 * NOTE : We created the replacement ipif when we did a
17505 	 * FAILOVER (See below). We could check for FAILBACK and
17506 	 * then look for replacement ipif to be removed. But we don't
17507 	 * want to do that because we wan't to allow the possibility
17508 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17509 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17510 	 * from B -> A.
17511 	 */
17512 	to_ipif = to_ill->ill_ipif;
17513 	if ((to_ill->ill_phyint->phyint_ifindex ==
17514 	    ipif->ipif_orig_ifindex) &&
17515 	    to_ipif->ipif_replace_zero) {
17516 		ASSERT(to_ipif->ipif_id == 0);
17517 		remove_ipif = B_TRUE;
17518 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17519 	}
17520 	/*
17521 	 * Find the lowest logical unit number on the to_ill.
17522 	 * If we are failing back, try to get the original id
17523 	 * rather than the lowest one so that the original
17524 	 * configuration is maintained.
17525 	 *
17526 	 * XXX need a better scheme for this.
17527 	 */
17528 	if (failback_cmd) {
17529 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17530 	} else {
17531 		unit = ipif_get_id(to_ill, 0);
17532 	}
17533 
17534 	/* Reset back to zero in case we fail below */
17535 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17536 		to_ipif->ipif_id = 0;
17537 
17538 	if (unit == ipst->ips_ip_addrs_per_if) {
17539 		ipif->ipif_was_up = B_FALSE;
17540 		IPIF_UNMARK_MOVING(ipif);
17541 		return (EINVAL);
17542 	}
17543 
17544 	/*
17545 	 * ipif is ready to move from "from_ill" to "to_ill".
17546 	 *
17547 	 * 1) If we are moving ipif with id zero, create a
17548 	 *    replacement ipif for this ipif on from_ill. If this fails
17549 	 *    fail the MOVE operation.
17550 	 *
17551 	 * 2) Remove the replacement ipif on to_ill if any.
17552 	 *    We could remove the replacement ipif when we are moving
17553 	 *    the ipif with id zero. But what if somebody already
17554 	 *    unplumbed it ? Thus we always remove it if it is present.
17555 	 *    We want to do it only if we are sure we are going to
17556 	 *    move the ipif to to_ill which is why there are no
17557 	 *    returns due to error till ipif is linked to to_ill.
17558 	 *    Note that the first ipif that we failback will always
17559 	 *    be zero if it is present.
17560 	 */
17561 	if (ipif->ipif_id == 0) {
17562 		ipaddr_t inaddr_any = INADDR_ANY;
17563 
17564 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17565 		if (rep_ipif == NULL) {
17566 			ipif->ipif_was_up = B_FALSE;
17567 			IPIF_UNMARK_MOVING(ipif);
17568 			return (ENOMEM);
17569 		}
17570 		*rep_ipif = ipif_zero;
17571 		/*
17572 		 * Before we put the ipif on the list, store the addresses
17573 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17574 		 * assumes so. This logic is not any different from what
17575 		 * ipif_allocate does.
17576 		 */
17577 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17578 		    &rep_ipif->ipif_v6lcl_addr);
17579 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17580 		    &rep_ipif->ipif_v6src_addr);
17581 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17582 		    &rep_ipif->ipif_v6subnet);
17583 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17584 		    &rep_ipif->ipif_v6net_mask);
17585 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17586 		    &rep_ipif->ipif_v6brd_addr);
17587 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17588 		    &rep_ipif->ipif_v6pp_dst_addr);
17589 		/*
17590 		 * We mark IPIF_NOFAILOVER so that this can never
17591 		 * move.
17592 		 */
17593 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17594 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17595 		rep_ipif->ipif_replace_zero = B_TRUE;
17596 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17597 		    MUTEX_DEFAULT, NULL);
17598 		rep_ipif->ipif_id = 0;
17599 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17600 		rep_ipif->ipif_ill = from_ill;
17601 		rep_ipif->ipif_orig_ifindex =
17602 		    from_ill->ill_phyint->phyint_ifindex;
17603 		/* Insert at head */
17604 		rep_ipif->ipif_next = from_ill->ill_ipif;
17605 		from_ill->ill_ipif = rep_ipif;
17606 		/*
17607 		 * We don't really care to let apps know about
17608 		 * this interface.
17609 		 */
17610 	}
17611 
17612 	if (remove_ipif) {
17613 		/*
17614 		 * We set to a max value above for this case to get
17615 		 * id zero. ASSERT that we did get one.
17616 		 */
17617 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17618 		rep_ipif = to_ipif;
17619 		to_ill->ill_ipif = rep_ipif->ipif_next;
17620 		rep_ipif->ipif_next = NULL;
17621 		/*
17622 		 * If some apps scanned and find this interface,
17623 		 * it is time to let them know, so that they can
17624 		 * delete it.
17625 		 */
17626 
17627 		*rep_ipif_ptr = rep_ipif;
17628 	}
17629 
17630 	/* Get it out of the ILL interface list. */
17631 	ipif_remove(ipif, B_FALSE);
17632 
17633 	/* Assign the new ill */
17634 	ipif->ipif_ill = to_ill;
17635 	ipif->ipif_id = unit;
17636 	/* id has already been checked */
17637 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17638 	ASSERT(rc == 0);
17639 	/* Let SCTP update its list */
17640 	sctp_move_ipif(ipif, from_ill, to_ill);
17641 	/*
17642 	 * Handle the failover and failback of ipif_t between
17643 	 * ill_t that have differing maximum mtu values.
17644 	 */
17645 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17646 		if (ipif->ipif_saved_mtu == 0) {
17647 			/*
17648 			 * As this ipif_t is moving to an ill_t
17649 			 * that has a lower ill_max_mtu, its
17650 			 * ipif_mtu needs to be saved so it can
17651 			 * be restored during failback or during
17652 			 * failover to an ill_t which has a
17653 			 * higher ill_max_mtu.
17654 			 */
17655 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17656 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17657 		} else {
17658 			/*
17659 			 * The ipif_t is, once again, moving to
17660 			 * an ill_t that has a lower maximum mtu
17661 			 * value.
17662 			 */
17663 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17664 		}
17665 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17666 	    ipif->ipif_saved_mtu != 0) {
17667 		/*
17668 		 * The mtu of this ipif_t had to be reduced
17669 		 * during an earlier failover; this is an
17670 		 * opportunity for it to be increased (either as
17671 		 * part of another failover or a failback).
17672 		 */
17673 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17674 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17675 			ipif->ipif_saved_mtu = 0;
17676 		} else {
17677 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17678 		}
17679 	}
17680 
17681 	/*
17682 	 * We preserve all the other fields of the ipif including
17683 	 * ipif_saved_ire_mp. The routes that are saved here will
17684 	 * be recreated on the new interface and back on the old
17685 	 * interface when we move back.
17686 	 */
17687 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17688 
17689 	return (err);
17690 }
17691 
17692 static int
17693 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17694     int ifindex, ipif_t **rep_ipif_ptr)
17695 {
17696 	ipif_t *mipif;
17697 	ipif_t *ipif_next;
17698 	int err;
17699 
17700 	/*
17701 	 * We don't really try to MOVE back things if some of the
17702 	 * operations fail. The daemon will take care of moving again
17703 	 * later on.
17704 	 */
17705 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17706 		ipif_next = mipif->ipif_next;
17707 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17708 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17709 
17710 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17711 
17712 			/*
17713 			 * When the MOVE fails, it is the job of the
17714 			 * application to take care of this properly
17715 			 * i.e try again if it is ENOMEM.
17716 			 */
17717 			if (mipif->ipif_ill != from_ill) {
17718 				/*
17719 				 * ipif has moved.
17720 				 *
17721 				 * Move the multicast memberships associated
17722 				 * with this ipif to the new ill. For IPv6, we
17723 				 * do it once after all the ipifs are moved
17724 				 * (in ill_move) as they are not associated
17725 				 * with ipifs.
17726 				 *
17727 				 * We need to move the ilms as the ipif has
17728 				 * already been moved to a new ill even
17729 				 * in the case of errors. Neither
17730 				 * ilm_free(ipif) will find the ilm
17731 				 * when somebody unplumbs this ipif nor
17732 				 * ilm_delete(ilm) will be able to find the
17733 				 * ilm, if we don't move now.
17734 				 */
17735 				if (!from_ill->ill_isv6)
17736 					ilm_move_v4(from_ill, to_ill, mipif);
17737 			}
17738 
17739 			if (err != 0)
17740 				return (err);
17741 		}
17742 	}
17743 	return (0);
17744 }
17745 
17746 static int
17747 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17748 {
17749 	int ifindex;
17750 	int err;
17751 	struct iocblk	*iocp;
17752 	ipif_t	*ipif;
17753 	ipif_t *rep_ipif_ptr = NULL;
17754 	ipif_t	*from_ipif = NULL;
17755 	boolean_t check_rep_if = B_FALSE;
17756 	ip_stack_t	*ipst = from_ill->ill_ipst;
17757 
17758 	iocp = (struct iocblk *)mp->b_rptr;
17759 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17760 		/*
17761 		 * Move everything pointing at from_ill to to_ill.
17762 		 * We acheive this by passing in 0 as ifindex.
17763 		 */
17764 		ifindex = 0;
17765 	} else {
17766 		/*
17767 		 * Move everything pointing at from_ill whose original
17768 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17769 		 * We acheive this by passing in ifindex rather than 0.
17770 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17771 		 */
17772 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17773 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17774 	}
17775 
17776 	/*
17777 	 * Determine if there is at least one ipif that would move from
17778 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17779 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17780 	 * the move, in which case we need to quiesce the replacement ipif also.
17781 	 */
17782 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17783 	    from_ipif = from_ipif->ipif_next) {
17784 		if (((ifindex == 0) ||
17785 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17786 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17787 			check_rep_if = B_TRUE;
17788 			break;
17789 		}
17790 	}
17791 
17792 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17793 
17794 	GRAB_ILL_LOCKS(from_ill, to_ill);
17795 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17796 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17797 		    mp, ILL_MOVE_OK);
17798 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17799 		return (EINPROGRESS);
17800 	}
17801 
17802 	/* Check if the replacement ipif is quiescent to delete */
17803 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17804 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17805 		to_ill->ill_ipif->ipif_state_flags |=
17806 		    IPIF_MOVING | IPIF_CHANGING;
17807 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17808 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17809 			    mp, ILL_MOVE_OK);
17810 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17811 			return (EINPROGRESS);
17812 		}
17813 	}
17814 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17815 
17816 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17817 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17818 	GRAB_ILL_LOCKS(from_ill, to_ill);
17819 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17820 
17821 	/* ilm_move is done inside ipif_move for IPv4 */
17822 	if (err == 0 && from_ill->ill_isv6)
17823 		ilm_move_v6(from_ill, to_ill, ifindex);
17824 
17825 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17826 	rw_exit(&ipst->ips_ill_g_lock);
17827 
17828 	/*
17829 	 * send rts messages and multicast messages.
17830 	 */
17831 	if (rep_ipif_ptr != NULL) {
17832 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17833 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17834 			rep_ipif_ptr->ipif_recovery_id = 0;
17835 		}
17836 		ip_rts_ifmsg(rep_ipif_ptr);
17837 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17838 #ifdef DEBUG
17839 		ipif_trace_cleanup(rep_ipif_ptr);
17840 #endif
17841 		mi_free(rep_ipif_ptr);
17842 	}
17843 
17844 	conn_move_ill(from_ill, to_ill, ifindex);
17845 
17846 	return (err);
17847 }
17848 
17849 /*
17850  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17851  * Also checks for the validity of the arguments.
17852  * Note: We are already exclusive inside the from group.
17853  * It is upto the caller to release refcnt on the to_ill's.
17854  */
17855 static int
17856 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17857     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17858 {
17859 	int dst_index;
17860 	ipif_t *ipif_v4, *ipif_v6;
17861 	struct lifreq *lifr;
17862 	mblk_t *mp1;
17863 	boolean_t exists;
17864 	sin_t	*sin;
17865 	int	err = 0;
17866 	ip_stack_t	*ipst;
17867 
17868 	if (CONN_Q(q))
17869 		ipst = CONNQ_TO_IPST(q);
17870 	else
17871 		ipst = ILLQ_TO_IPST(q);
17872 
17873 	if ((mp1 = mp->b_cont) == NULL)
17874 		return (EPROTO);
17875 
17876 	if ((mp1 = mp1->b_cont) == NULL)
17877 		return (EPROTO);
17878 
17879 	lifr = (struct lifreq *)mp1->b_rptr;
17880 	sin = (sin_t *)&lifr->lifr_addr;
17881 
17882 	/*
17883 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17884 	 * specific operations.
17885 	 */
17886 	if (sin->sin_family != AF_UNSPEC)
17887 		return (EINVAL);
17888 
17889 	/*
17890 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17891 	 * NULLs for the last 4 args and we know the lookup won't fail
17892 	 * with EINPROGRESS.
17893 	 */
17894 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17895 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17896 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17897 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17898 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17899 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17900 
17901 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17902 		return (ENXIO);
17903 
17904 	if (ipif_v4 != NULL) {
17905 		ASSERT(ipif_v4->ipif_refcnt != 0);
17906 		if (ipif_v4->ipif_id != 0) {
17907 			err = EINVAL;
17908 			goto done;
17909 		}
17910 
17911 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17912 		*ill_from_v4 = ipif_v4->ipif_ill;
17913 	}
17914 
17915 	if (ipif_v6 != NULL) {
17916 		ASSERT(ipif_v6->ipif_refcnt != 0);
17917 		if (ipif_v6->ipif_id != 0) {
17918 			err = EINVAL;
17919 			goto done;
17920 		}
17921 
17922 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17923 		*ill_from_v6 = ipif_v6->ipif_ill;
17924 	}
17925 
17926 	err = 0;
17927 	dst_index = lifr->lifr_movetoindex;
17928 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17929 	    q, mp, ip_process_ioctl, &err, ipst);
17930 	if (err != 0) {
17931 		/*
17932 		 * A move may be in progress, EINPROGRESS looking up the "to"
17933 		 * ill means changes already done to the "from" ipsq need to
17934 		 * be undone to avoid potential deadlocks.
17935 		 *
17936 		 * ENXIO will usually be because there is only v6 on the ill,
17937 		 * that's not treated as an error unless an ENXIO is also
17938 		 * seen when looking up the v6 "to" ill.
17939 		 *
17940 		 * If EINPROGRESS, the mp has been enqueued and can not be
17941 		 * used to look up the v6 "to" ill, but a preemptive clean
17942 		 * up of changes to the v6 "from" ipsq is done.
17943 		 */
17944 		if (err == EINPROGRESS) {
17945 			if (*ill_from_v4 != NULL) {
17946 				ill_t   *from_ill;
17947 				ipsq_t  *from_ipsq;
17948 
17949 				from_ill = ipif_v4->ipif_ill;
17950 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17951 
17952 				mutex_enter(&from_ipsq->ipsq_lock);
17953 				from_ipsq->ipsq_current_ipif = NULL;
17954 				mutex_exit(&from_ipsq->ipsq_lock);
17955 			}
17956 			if (*ill_from_v6 != NULL) {
17957 				ill_t   *from_ill;
17958 				ipsq_t  *from_ipsq;
17959 
17960 				from_ill = ipif_v6->ipif_ill;
17961 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17962 
17963 				mutex_enter(&from_ipsq->ipsq_lock);
17964 				from_ipsq->ipsq_current_ipif = NULL;
17965 				mutex_exit(&from_ipsq->ipsq_lock);
17966 			}
17967 			goto done;
17968 		}
17969 		ASSERT(err == ENXIO);
17970 		err = 0;
17971 	}
17972 
17973 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17974 	    q, mp, ip_process_ioctl, &err, ipst);
17975 	if (err != 0) {
17976 		/*
17977 		 * A move may be in progress, EINPROGRESS looking up the "to"
17978 		 * ill means changes already done to the "from" ipsq need to
17979 		 * be undone to avoid potential deadlocks.
17980 		 */
17981 		if (err == EINPROGRESS) {
17982 			if (*ill_from_v6 != NULL) {
17983 				ill_t   *from_ill;
17984 				ipsq_t  *from_ipsq;
17985 
17986 				from_ill = ipif_v6->ipif_ill;
17987 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17988 
17989 				mutex_enter(&from_ipsq->ipsq_lock);
17990 				from_ipsq->ipsq_current_ipif = NULL;
17991 				mutex_exit(&from_ipsq->ipsq_lock);
17992 			}
17993 			goto done;
17994 		}
17995 		ASSERT(err == ENXIO);
17996 
17997 		/* Both v4 and v6 lookup failed */
17998 		if (*ill_to_v4 == NULL) {
17999 			err = ENXIO;
18000 			goto done;
18001 		}
18002 		err = 0;
18003 	}
18004 
18005 	/*
18006 	 * If we have something to MOVE i.e "from" not NULL,
18007 	 * "to" should be non-NULL.
18008 	 */
18009 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
18010 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
18011 		err = EINVAL;
18012 	}
18013 
18014 done:
18015 	if (ipif_v4 != NULL)
18016 		ipif_refrele(ipif_v4);
18017 	if (ipif_v6 != NULL)
18018 		ipif_refrele(ipif_v6);
18019 	return (err);
18020 }
18021 
18022 /*
18023  * FAILOVER and FAILBACK are modelled as MOVE operations.
18024  *
18025  * We don't check whether the MOVE is within the same group or
18026  * not, because this ioctl can be used as a generic mechanism
18027  * to failover from interface A to B, though things will function
18028  * only if they are really part of the same group. Moreover,
18029  * all ipifs may be down and hence temporarily out of the group.
18030  *
18031  * ipif's that need to be moved are first brought down; V4 ipifs are brought
18032  * down first and then V6.  For each we wait for the ipif's to become quiescent.
18033  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
18034  * have been deleted and there are no active references. Once quiescent the
18035  * ipif's are moved and brought up on the new ill.
18036  *
18037  * Normally the source ill and destination ill belong to the same IPMP group
18038  * and hence the same ipsq_t. In the event they don't belong to the same
18039  * same group the two ipsq's are first merged into one ipsq - that of the
18040  * to_ill. The multicast memberships on the source and destination ill cannot
18041  * change during the move operation since multicast joins/leaves also have to
18042  * execute on the same ipsq and are hence serialized.
18043  */
18044 /* ARGSUSED */
18045 int
18046 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18047     ip_ioctl_cmd_t *ipip, void *ifreq)
18048 {
18049 	ill_t *ill_to_v4 = NULL;
18050 	ill_t *ill_to_v6 = NULL;
18051 	ill_t *ill_from_v4 = NULL;
18052 	ill_t *ill_from_v6 = NULL;
18053 	int err = 0;
18054 
18055 	/*
18056 	 * setup from and to ill's, we can get EINPROGRESS only for
18057 	 * to_ill's.
18058 	 */
18059 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18060 	    &ill_to_v4, &ill_to_v6);
18061 
18062 	if (err != 0) {
18063 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18064 		goto done;
18065 	}
18066 
18067 	/*
18068 	 * nothing to do.
18069 	 */
18070 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18071 		goto done;
18072 	}
18073 
18074 	/*
18075 	 * nothing to do.
18076 	 */
18077 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18078 		goto done;
18079 	}
18080 
18081 	/*
18082 	 * Mark the ill as changing.
18083 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18084 	 * in ill_up_ipifs in case of error they are cleared below.
18085 	 */
18086 
18087 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18088 	if (ill_from_v4 != NULL)
18089 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18090 	if (ill_from_v6 != NULL)
18091 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18092 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18093 
18094 	/*
18095 	 * Make sure that both src and dst are
18096 	 * in the same syncq group. If not make it happen.
18097 	 * We are not holding any locks because we are the writer
18098 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18099 	 * to protect to_ipsq against changing.
18100 	 */
18101 	if (ill_from_v4 != NULL) {
18102 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18103 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18104 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18105 			    NULL, mp, q);
18106 			goto err_ret;
18107 
18108 		}
18109 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18110 	} else {
18111 
18112 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18113 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18114 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18115 			    NULL, mp, q);
18116 			goto err_ret;
18117 
18118 		}
18119 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18120 	}
18121 
18122 	/*
18123 	 * Now that the ipsq's have been merged and we are the writer
18124 	 * lets mark to_ill as changing as well.
18125 	 */
18126 
18127 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18128 	if (ill_to_v4 != NULL)
18129 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18130 	if (ill_to_v6 != NULL)
18131 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18132 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18133 
18134 	/*
18135 	 * Its ok for us to proceed with the move even if
18136 	 * ill_pending_mp is non null on one of the from ill's as the reply
18137 	 * should not be looking at the ipif, it should only care about the
18138 	 * ill itself.
18139 	 */
18140 
18141 	/*
18142 	 * lets move ipv4 first.
18143 	 */
18144 	if (ill_from_v4 != NULL) {
18145 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18146 		ill_from_v4->ill_move_in_progress = B_TRUE;
18147 		ill_to_v4->ill_move_in_progress = B_TRUE;
18148 		ill_to_v4->ill_move_peer = ill_from_v4;
18149 		ill_from_v4->ill_move_peer = ill_to_v4;
18150 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18151 	}
18152 
18153 	/*
18154 	 * Now lets move ipv6.
18155 	 */
18156 	if (err == 0 && ill_from_v6 != NULL) {
18157 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18158 		ill_from_v6->ill_move_in_progress = B_TRUE;
18159 		ill_to_v6->ill_move_in_progress = B_TRUE;
18160 		ill_to_v6->ill_move_peer = ill_from_v6;
18161 		ill_from_v6->ill_move_peer = ill_to_v6;
18162 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18163 	}
18164 
18165 err_ret:
18166 	/*
18167 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18168 	 * moved to become quiescent.
18169 	 */
18170 	if (err == EINPROGRESS) {
18171 		goto done;
18172 	}
18173 
18174 	/*
18175 	 * if err is set ill_up_ipifs will not be called
18176 	 * lets clear the flags.
18177 	 */
18178 
18179 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18180 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18181 	/*
18182 	 * Some of the clearing may be redundant. But it is simple
18183 	 * not making any extra checks.
18184 	 */
18185 	if (ill_from_v6 != NULL) {
18186 		ill_from_v6->ill_move_in_progress = B_FALSE;
18187 		ill_from_v6->ill_move_peer = NULL;
18188 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18189 	}
18190 	if (ill_from_v4 != NULL) {
18191 		ill_from_v4->ill_move_in_progress = B_FALSE;
18192 		ill_from_v4->ill_move_peer = NULL;
18193 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18194 	}
18195 	if (ill_to_v6 != NULL) {
18196 		ill_to_v6->ill_move_in_progress = B_FALSE;
18197 		ill_to_v6->ill_move_peer = NULL;
18198 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18199 	}
18200 	if (ill_to_v4 != NULL) {
18201 		ill_to_v4->ill_move_in_progress = B_FALSE;
18202 		ill_to_v4->ill_move_peer = NULL;
18203 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18204 	}
18205 
18206 	/*
18207 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18208 	 * Do this always to maintain proper state i.e even in case of errors.
18209 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18210 	 * we need not call on both v4 and v6 interfaces.
18211 	 */
18212 	if (ill_from_v4 != NULL) {
18213 		if ((ill_from_v4->ill_phyint->phyint_flags &
18214 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18215 			phyint_inactive(ill_from_v4->ill_phyint);
18216 		}
18217 	} else if (ill_from_v6 != NULL) {
18218 		if ((ill_from_v6->ill_phyint->phyint_flags &
18219 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18220 			phyint_inactive(ill_from_v6->ill_phyint);
18221 		}
18222 	}
18223 
18224 	if (ill_to_v4 != NULL) {
18225 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18226 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18227 		}
18228 	} else if (ill_to_v6 != NULL) {
18229 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18230 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18231 		}
18232 	}
18233 
18234 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18235 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18236 
18237 no_err:
18238 	/*
18239 	 * lets bring the interfaces up on the to_ill.
18240 	 */
18241 	if (err == 0) {
18242 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18243 		    q, mp);
18244 	}
18245 
18246 	if (err == 0) {
18247 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18248 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18249 
18250 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18251 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18252 	}
18253 done:
18254 
18255 	if (ill_to_v4 != NULL) {
18256 		ill_refrele(ill_to_v4);
18257 	}
18258 	if (ill_to_v6 != NULL) {
18259 		ill_refrele(ill_to_v6);
18260 	}
18261 
18262 	return (err);
18263 }
18264 
18265 static void
18266 ill_dl_down(ill_t *ill)
18267 {
18268 	/*
18269 	 * The ill is down; unbind but stay attached since we're still
18270 	 * associated with a PPA. If we have negotiated DLPI capabilites
18271 	 * with the data link service provider (IDS_OK) then reset them.
18272 	 * The interval between unbinding and rebinding is potentially
18273 	 * unbounded hence we cannot assume things will be the same.
18274 	 * The DLPI capabilities will be probed again when the data link
18275 	 * is brought up.
18276 	 */
18277 	mblk_t	*mp = ill->ill_unbind_mp;
18278 
18279 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18280 
18281 	ill->ill_unbind_mp = NULL;
18282 	if (mp != NULL) {
18283 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18284 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18285 		    ill->ill_name));
18286 		mutex_enter(&ill->ill_lock);
18287 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18288 		mutex_exit(&ill->ill_lock);
18289 		/*
18290 		 * Reset the capabilities if the negotiation is done or is
18291 		 * still in progress. Note that ill_capability_reset() will
18292 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18293 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18294 		 *
18295 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18296 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18297 		 * the capabilities renegotiation from happening.
18298 		 */
18299 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18300 			ill_capability_reset(ill);
18301 		ill->ill_capab_reneg = B_FALSE;
18302 
18303 		ill_dlpi_send(ill, mp);
18304 	}
18305 
18306 	/*
18307 	 * Toss all of our multicast memberships.  We could keep them, but
18308 	 * then we'd have to do bookkeeping of any joins and leaves performed
18309 	 * by the application while the the interface is down (we can't just
18310 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18311 	 * on a downed interface).
18312 	 */
18313 	ill_leave_multicast(ill);
18314 
18315 	mutex_enter(&ill->ill_lock);
18316 	ill->ill_dl_up = 0;
18317 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18318 	mutex_exit(&ill->ill_lock);
18319 }
18320 
18321 static void
18322 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18323 {
18324 	union DL_primitives *dlp;
18325 	t_uscalar_t prim;
18326 
18327 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18328 
18329 	dlp = (union DL_primitives *)mp->b_rptr;
18330 	prim = dlp->dl_primitive;
18331 
18332 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18333 	    dl_primstr(prim), prim, ill->ill_name));
18334 
18335 	switch (prim) {
18336 	case DL_PHYS_ADDR_REQ:
18337 	{
18338 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18339 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18340 		break;
18341 	}
18342 	case DL_BIND_REQ:
18343 		mutex_enter(&ill->ill_lock);
18344 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18345 		mutex_exit(&ill->ill_lock);
18346 		break;
18347 	}
18348 
18349 	/*
18350 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18351 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18352 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18353 	 */
18354 	mutex_enter(&ill->ill_lock);
18355 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18356 	    (prim == DL_UNBIND_REQ)) {
18357 		ill->ill_dlpi_pending = prim;
18358 	}
18359 	mutex_exit(&ill->ill_lock);
18360 
18361 	putnext(ill->ill_wq, mp);
18362 }
18363 
18364 /*
18365  * Helper function for ill_dlpi_send().
18366  */
18367 /* ARGSUSED */
18368 static void
18369 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18370 {
18371 	ill_dlpi_send(q->q_ptr, mp);
18372 }
18373 
18374 /*
18375  * Send a DLPI control message to the driver but make sure there
18376  * is only one outstanding message. Uses ill_dlpi_pending to tell
18377  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18378  * when an ACK or a NAK is received to process the next queued message.
18379  */
18380 void
18381 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18382 {
18383 	mblk_t **mpp;
18384 
18385 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18386 
18387 	/*
18388 	 * To ensure that any DLPI requests for current exclusive operation
18389 	 * are always completely sent before any DLPI messages for other
18390 	 * operations, require writer access before enqueuing.
18391 	 */
18392 	if (!IAM_WRITER_ILL(ill)) {
18393 		ill_refhold(ill);
18394 		/* qwriter_ip() does the ill_refrele() */
18395 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18396 		    NEW_OP, B_TRUE);
18397 		return;
18398 	}
18399 
18400 	mutex_enter(&ill->ill_lock);
18401 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18402 		/* Must queue message. Tail insertion */
18403 		mpp = &ill->ill_dlpi_deferred;
18404 		while (*mpp != NULL)
18405 			mpp = &((*mpp)->b_next);
18406 
18407 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18408 		    ill->ill_name));
18409 
18410 		*mpp = mp;
18411 		mutex_exit(&ill->ill_lock);
18412 		return;
18413 	}
18414 	mutex_exit(&ill->ill_lock);
18415 	ill_dlpi_dispatch(ill, mp);
18416 }
18417 
18418 /*
18419  * Send all deferred DLPI messages without waiting for their ACKs.
18420  */
18421 void
18422 ill_dlpi_send_deferred(ill_t *ill)
18423 {
18424 	mblk_t *mp, *nextmp;
18425 
18426 	/*
18427 	 * Clear ill_dlpi_pending so that the message is not queued in
18428 	 * ill_dlpi_send().
18429 	 */
18430 	mutex_enter(&ill->ill_lock);
18431 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18432 	mp = ill->ill_dlpi_deferred;
18433 	ill->ill_dlpi_deferred = NULL;
18434 	mutex_exit(&ill->ill_lock);
18435 
18436 	for (; mp != NULL; mp = nextmp) {
18437 		nextmp = mp->b_next;
18438 		mp->b_next = NULL;
18439 		ill_dlpi_send(ill, mp);
18440 	}
18441 }
18442 
18443 /*
18444  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18445  */
18446 boolean_t
18447 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18448 {
18449 	t_uscalar_t pending;
18450 
18451 	mutex_enter(&ill->ill_lock);
18452 	if (ill->ill_dlpi_pending == prim) {
18453 		mutex_exit(&ill->ill_lock);
18454 		return (B_TRUE);
18455 	}
18456 
18457 	/*
18458 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18459 	 * without waiting, so don't print any warnings in that case.
18460 	 */
18461 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18462 		mutex_exit(&ill->ill_lock);
18463 		return (B_FALSE);
18464 	}
18465 	pending = ill->ill_dlpi_pending;
18466 	mutex_exit(&ill->ill_lock);
18467 
18468 	if (pending == DL_PRIM_INVAL) {
18469 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18470 		    "received unsolicited ack for %s on %s\n",
18471 		    dl_primstr(prim), ill->ill_name);
18472 	} else {
18473 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18474 		    "received unexpected ack for %s on %s (expecting %s)\n",
18475 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18476 	}
18477 	return (B_FALSE);
18478 }
18479 
18480 /*
18481  * Complete the current DLPI operation associated with `prim' on `ill' and
18482  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18483  * operations and the ill's current exclusive IPSQ operation has finished
18484  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18485  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18486  * the comments above ipsq_current_finish() for details.
18487  */
18488 void
18489 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18490 {
18491 	mblk_t *mp;
18492 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18493 
18494 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18495 	mutex_enter(&ill->ill_lock);
18496 
18497 	ASSERT(prim != DL_PRIM_INVAL);
18498 	ASSERT(ill->ill_dlpi_pending == prim);
18499 
18500 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18501 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18502 
18503 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18504 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18505 
18506 		mutex_enter(&ipsq->ipsq_lock);
18507 		if (ipsq->ipsq_current_done)
18508 			ipsq->ipsq_current_ipif = NULL;
18509 		mutex_exit(&ipsq->ipsq_lock);
18510 
18511 		cv_signal(&ill->ill_cv);
18512 		mutex_exit(&ill->ill_lock);
18513 		return;
18514 	}
18515 
18516 	ill->ill_dlpi_deferred = mp->b_next;
18517 	mp->b_next = NULL;
18518 	mutex_exit(&ill->ill_lock);
18519 
18520 	ill_dlpi_dispatch(ill, mp);
18521 }
18522 
18523 void
18524 conn_delete_ire(conn_t *connp, caddr_t arg)
18525 {
18526 	ipif_t	*ipif = (ipif_t *)arg;
18527 	ire_t	*ire;
18528 
18529 	/*
18530 	 * Look at the cached ires on conns which has pointers to ipifs.
18531 	 * We just call ire_refrele which clears up the reference
18532 	 * to ire. Called when a conn closes. Also called from ipif_free
18533 	 * to cleanup indirect references to the stale ipif via the cached ire.
18534 	 */
18535 	mutex_enter(&connp->conn_lock);
18536 	ire = connp->conn_ire_cache;
18537 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18538 		connp->conn_ire_cache = NULL;
18539 		mutex_exit(&connp->conn_lock);
18540 		IRE_REFRELE_NOTR(ire);
18541 		return;
18542 	}
18543 	mutex_exit(&connp->conn_lock);
18544 
18545 }
18546 
18547 /*
18548  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18549  * of IREs. Those IREs may have been previously cached in the conn structure.
18550  * This ipcl_walk() walker function releases all references to such IREs based
18551  * on the condemned flag.
18552  */
18553 /* ARGSUSED */
18554 void
18555 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18556 {
18557 	ire_t	*ire;
18558 
18559 	mutex_enter(&connp->conn_lock);
18560 	ire = connp->conn_ire_cache;
18561 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18562 		connp->conn_ire_cache = NULL;
18563 		mutex_exit(&connp->conn_lock);
18564 		IRE_REFRELE_NOTR(ire);
18565 		return;
18566 	}
18567 	mutex_exit(&connp->conn_lock);
18568 }
18569 
18570 /*
18571  * Take down a specific interface, but don't lose any information about it.
18572  * Also delete interface from its interface group (ifgrp).
18573  * (Always called as writer.)
18574  * This function goes through the down sequence even if the interface is
18575  * already down. There are 2 reasons.
18576  * a. Currently we permit interface routes that depend on down interfaces
18577  *    to be added. This behaviour itself is questionable. However it appears
18578  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18579  *    time. We go thru the cleanup in order to remove these routes.
18580  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18581  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18582  *    down, but we need to cleanup i.e. do ill_dl_down and
18583  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18584  *
18585  * IP-MT notes:
18586  *
18587  * Model of reference to interfaces.
18588  *
18589  * The following members in ipif_t track references to the ipif.
18590  *	int     ipif_refcnt;    Active reference count
18591  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18592  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18593  *
18594  * The following members in ill_t track references to the ill.
18595  *	int             ill_refcnt;     active refcnt
18596  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18597  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18598  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18599  *
18600  * Reference to an ipif or ill can be obtained in any of the following ways.
18601  *
18602  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18603  * Pointers to ipif / ill from other data structures viz ire and conn.
18604  * Implicit reference to the ipif / ill by holding a reference to the ire.
18605  *
18606  * The ipif/ill lookup functions return a reference held ipif / ill.
18607  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18608  * This is a purely dynamic reference count associated with threads holding
18609  * references to the ipif / ill. Pointers from other structures do not
18610  * count towards this reference count.
18611  *
18612  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18613  * associated with the ipif/ill. This is incremented whenever a new
18614  * ire is created referencing the ipif/ill. This is done atomically inside
18615  * ire_add_v[46] where the ire is actually added to the ire hash table.
18616  * The count is decremented in ire_inactive where the ire is destroyed.
18617  *
18618  * nce's reference ill's thru nce_ill and the count of nce's associated with
18619  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18620  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18621  * table. Similarly it is decremented in ndp_inactive() where the nce
18622  * is destroyed.
18623  *
18624  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18625  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18626  * in ilm_walker_cleanup() or ilm_delete().
18627  *
18628  * Flow of ioctls involving interface down/up
18629  *
18630  * The following is the sequence of an attempt to set some critical flags on an
18631  * up interface.
18632  * ip_sioctl_flags
18633  * ipif_down
18634  * wait for ipif to be quiescent
18635  * ipif_down_tail
18636  * ip_sioctl_flags_tail
18637  *
18638  * All set ioctls that involve down/up sequence would have a skeleton similar
18639  * to the above. All the *tail functions are called after the refcounts have
18640  * dropped to the appropriate values.
18641  *
18642  * The mechanism to quiesce an ipif is as follows.
18643  *
18644  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18645  * on the ipif. Callers either pass a flag requesting wait or the lookup
18646  *  functions will return NULL.
18647  *
18648  * Delete all ires referencing this ipif
18649  *
18650  * Any thread attempting to do an ipif_refhold on an ipif that has been
18651  * obtained thru a cached pointer will first make sure that
18652  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18653  * increment the refcount.
18654  *
18655  * The above guarantees that the ipif refcount will eventually come down to
18656  * zero and the ipif will quiesce, once all threads that currently hold a
18657  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18658  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18659  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18660  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18661  * in ip.h
18662  *
18663  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18664  *
18665  * Threads trying to lookup an ipif or ill can pass a flag requesting
18666  * wait and restart if the ipif / ill cannot be looked up currently.
18667  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18668  * failure if the ipif is currently undergoing an exclusive operation, and
18669  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18670  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18671  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18672  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18673  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18674  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18675  * until we release the ipsq_lock, even though the the ill/ipif state flags
18676  * can change after we drop the ill_lock.
18677  *
18678  * An attempt to send out a packet using an ipif that is currently
18679  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18680  * operation and restart it later when the exclusive condition on the ipif ends.
18681  * This is an example of not passing the wait flag to the lookup functions. For
18682  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18683  * out a multicast packet on that ipif will fail while the ipif is
18684  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18685  * currently IPIF_CHANGING will also fail.
18686  */
18687 int
18688 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18689 {
18690 	ill_t		*ill = ipif->ipif_ill;
18691 	phyint_t	*phyi;
18692 	conn_t		*connp;
18693 	boolean_t	success;
18694 	boolean_t	ipif_was_up = B_FALSE;
18695 	ip_stack_t	*ipst = ill->ill_ipst;
18696 
18697 	ASSERT(IAM_WRITER_IPIF(ipif));
18698 
18699 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18700 
18701 	if (ipif->ipif_flags & IPIF_UP) {
18702 		mutex_enter(&ill->ill_lock);
18703 		ipif->ipif_flags &= ~IPIF_UP;
18704 		ASSERT(ill->ill_ipif_up_count > 0);
18705 		--ill->ill_ipif_up_count;
18706 		mutex_exit(&ill->ill_lock);
18707 		ipif_was_up = B_TRUE;
18708 		/* Update status in SCTP's list */
18709 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18710 	}
18711 
18712 	/*
18713 	 * Blow away memberships we established in ipif_multicast_up().
18714 	 */
18715 	ipif_multicast_down(ipif);
18716 
18717 	/*
18718 	 * Remove from the mapping for __sin6_src_id. We insert only
18719 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18720 	 * stored as mapped addresses, we need to check for mapped
18721 	 * INADDR_ANY also.
18722 	 */
18723 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18724 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18725 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18726 		int err;
18727 
18728 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18729 		    ipif->ipif_zoneid, ipst);
18730 		if (err != 0) {
18731 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18732 		}
18733 	}
18734 
18735 	/*
18736 	 * Before we delete the ill from the group (if any), we need
18737 	 * to make sure that we delete all the routes dependent on
18738 	 * this and also any ipifs dependent on this ipif for
18739 	 * source address. We need to do before we delete from
18740 	 * the group because
18741 	 *
18742 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18743 	 *
18744 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18745 	 *    for re-doing source address selection. Note that
18746 	 *    ipif_select_source[_v6] called from
18747 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18748 	 *    because we have already marked down here i.e cleared
18749 	 *    IPIF_UP.
18750 	 */
18751 	if (ipif->ipif_isv6) {
18752 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18753 		    ipst);
18754 	} else {
18755 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18756 		    ipst);
18757 	}
18758 
18759 	/*
18760 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18761 	 * ires have been deleted above. Otherwise a thread could end up
18762 	 * caching an ire in a conn after we have finished the cleanup of the
18763 	 * conn. The caching is done after making sure that the ire is not yet
18764 	 * condemned. Also documented in the block comment above ip_output
18765 	 */
18766 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18767 	/* Also, delete the ires cached in SCTP */
18768 	sctp_ire_cache_flush(ipif);
18769 
18770 	/*
18771 	 * Update any other ipifs which have used "our" local address as
18772 	 * a source address. This entails removing and recreating IRE_INTERFACE
18773 	 * entries for such ipifs.
18774 	 */
18775 	if (ipif->ipif_isv6)
18776 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18777 	else
18778 		ipif_update_other_ipifs(ipif, ill->ill_group);
18779 
18780 	if (ipif_was_up) {
18781 		/*
18782 		 * Check whether it is last ipif to leave this group.
18783 		 * If this is the last ipif to leave, we should remove
18784 		 * this ill from the group as ipif_select_source will not
18785 		 * be able to find any useful ipifs if this ill is selected
18786 		 * for load balancing.
18787 		 *
18788 		 * For nameless groups, we should call ifgrp_delete if this
18789 		 * belongs to some group. As this ipif is going down, we may
18790 		 * need to reconstruct groups.
18791 		 */
18792 		phyi = ill->ill_phyint;
18793 		/*
18794 		 * If the phyint_groupname_len is 0, it may or may not
18795 		 * be in the nameless group. If the phyint_groupname_len is
18796 		 * not 0, then this ill should be part of some group.
18797 		 * As we always insert this ill in the group if
18798 		 * phyint_groupname_len is not zero when the first ipif
18799 		 * comes up (in ipif_up_done), it should be in a group
18800 		 * when the namelen is not 0.
18801 		 *
18802 		 * NOTE : When we delete the ill from the group,it will
18803 		 * blow away all the IRE_CACHES pointing either at this ipif or
18804 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18805 		 * should be pointing at this ill.
18806 		 */
18807 		ASSERT(phyi->phyint_groupname_len == 0 ||
18808 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18809 
18810 		if (phyi->phyint_groupname_len != 0) {
18811 			if (ill->ill_ipif_up_count == 0)
18812 				illgrp_delete(ill);
18813 		}
18814 
18815 		/*
18816 		 * If we have deleted some of the broadcast ires associated
18817 		 * with this ipif, we need to re-nominate somebody else if
18818 		 * the ires that we deleted were the nominated ones.
18819 		 */
18820 		if (ill->ill_group != NULL && !ill->ill_isv6)
18821 			ipif_renominate_bcast(ipif);
18822 	}
18823 
18824 	/*
18825 	 * neighbor-discovery or arp entries for this interface.
18826 	 */
18827 	ipif_ndp_down(ipif);
18828 
18829 	/*
18830 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18831 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18832 	 * and ill_delete -> ipif_free -> ipif_down
18833 	 */
18834 	if (mp == NULL) {
18835 		ASSERT(q == NULL);
18836 		return (0);
18837 	}
18838 
18839 	if (CONN_Q(q)) {
18840 		connp = Q_TO_CONN(q);
18841 		mutex_enter(&connp->conn_lock);
18842 	} else {
18843 		connp = NULL;
18844 	}
18845 	mutex_enter(&ill->ill_lock);
18846 	/*
18847 	 * Are there any ire's pointing to this ipif that are still active ?
18848 	 * If this is the last ipif going down, are there any ire's pointing
18849 	 * to this ill that are still active ?
18850 	 */
18851 	if (ipif_is_quiescent(ipif)) {
18852 		mutex_exit(&ill->ill_lock);
18853 		if (connp != NULL)
18854 			mutex_exit(&connp->conn_lock);
18855 		return (0);
18856 	}
18857 
18858 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18859 	    ill->ill_name, (void *)ill));
18860 	/*
18861 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18862 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18863 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18864 	 */
18865 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18866 	if (!success) {
18867 		/* The conn is closing. So just return */
18868 		ASSERT(connp != NULL);
18869 		mutex_exit(&ill->ill_lock);
18870 		mutex_exit(&connp->conn_lock);
18871 		return (EINTR);
18872 	}
18873 
18874 	mutex_exit(&ill->ill_lock);
18875 	if (connp != NULL)
18876 		mutex_exit(&connp->conn_lock);
18877 	return (EINPROGRESS);
18878 }
18879 
18880 void
18881 ipif_down_tail(ipif_t *ipif)
18882 {
18883 	ill_t	*ill = ipif->ipif_ill;
18884 
18885 	/*
18886 	 * Skip any loopback interface (null wq).
18887 	 * If this is the last logical interface on the ill
18888 	 * have ill_dl_down tell the driver we are gone (unbind)
18889 	 * Note that lun 0 can ipif_down even though
18890 	 * there are other logical units that are up.
18891 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18892 	 */
18893 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18894 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18895 	    ill->ill_dl_up) {
18896 		ill_dl_down(ill);
18897 	}
18898 	ill->ill_logical_down = 0;
18899 
18900 	/*
18901 	 * Have to be after removing the routes in ipif_down_delete_ire.
18902 	 */
18903 	if (ipif->ipif_isv6) {
18904 		if (ill->ill_flags & ILLF_XRESOLV)
18905 			ipif_arp_down(ipif);
18906 	} else {
18907 		ipif_arp_down(ipif);
18908 	}
18909 
18910 	ip_rts_ifmsg(ipif);
18911 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18912 }
18913 
18914 /*
18915  * Bring interface logically down without bringing the physical interface
18916  * down e.g. when the netmask is changed. This avoids long lasting link
18917  * negotiations between an ethernet interface and a certain switches.
18918  */
18919 static int
18920 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18921 {
18922 	/*
18923 	 * The ill_logical_down flag is a transient flag. It is set here
18924 	 * and is cleared once the down has completed in ipif_down_tail.
18925 	 * This flag does not indicate whether the ill stream is in the
18926 	 * DL_BOUND state with the driver. Instead this flag is used by
18927 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18928 	 * the driver. The state of the ill stream i.e. whether it is
18929 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18930 	 */
18931 	ipif->ipif_ill->ill_logical_down = 1;
18932 	return (ipif_down(ipif, q, mp));
18933 }
18934 
18935 /*
18936  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18937  * If the usesrc client ILL is already part of a usesrc group or not,
18938  * in either case a ire_stq with the matching usesrc client ILL will
18939  * locate the IRE's that need to be deleted. We want IREs to be created
18940  * with the new source address.
18941  */
18942 static void
18943 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18944 {
18945 	ill_t	*ucill = (ill_t *)ill_arg;
18946 
18947 	ASSERT(IAM_WRITER_ILL(ucill));
18948 
18949 	if (ire->ire_stq == NULL)
18950 		return;
18951 
18952 	if ((ire->ire_type == IRE_CACHE) &&
18953 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18954 		ire_delete(ire);
18955 }
18956 
18957 /*
18958  * ire_walk routine to delete every IRE dependent on the interface
18959  * address that is going down.	(Always called as writer.)
18960  * Works for both v4 and v6.
18961  * In addition for checking for ire_ipif matches it also checks for
18962  * IRE_CACHE entries which have the same source address as the
18963  * disappearing ipif since ipif_select_source might have picked
18964  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18965  * care of any IRE_INTERFACE with the disappearing source address.
18966  */
18967 static void
18968 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18969 {
18970 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18971 	ill_t *ire_ill;
18972 	ill_t *ipif_ill;
18973 
18974 	ASSERT(IAM_WRITER_IPIF(ipif));
18975 	if (ire->ire_ipif == NULL)
18976 		return;
18977 
18978 	/*
18979 	 * For IPv4, we derive source addresses for an IRE from ipif's
18980 	 * belonging to the same IPMP group as the IRE's outgoing
18981 	 * interface.  If an IRE's outgoing interface isn't in the
18982 	 * same IPMP group as a particular ipif, then that ipif
18983 	 * couldn't have been used as a source address for this IRE.
18984 	 *
18985 	 * For IPv6, source addresses are only restricted to the IPMP group
18986 	 * if the IRE is for a link-local address or a multicast address.
18987 	 * Otherwise, source addresses for an IRE can be chosen from
18988 	 * interfaces other than the the outgoing interface for that IRE.
18989 	 *
18990 	 * For source address selection details, see ipif_select_source()
18991 	 * and ipif_select_source_v6().
18992 	 */
18993 	if (ire->ire_ipversion == IPV4_VERSION ||
18994 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18995 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18996 		ire_ill = ire->ire_ipif->ipif_ill;
18997 		ipif_ill = ipif->ipif_ill;
18998 
18999 		if (ire_ill->ill_group != ipif_ill->ill_group) {
19000 			return;
19001 		}
19002 	}
19003 
19004 	if (ire->ire_ipif != ipif) {
19005 		/*
19006 		 * Look for a matching source address.
19007 		 */
19008 		if (ire->ire_type != IRE_CACHE)
19009 			return;
19010 		if (ipif->ipif_flags & IPIF_NOLOCAL)
19011 			return;
19012 
19013 		if (ire->ire_ipversion == IPV4_VERSION) {
19014 			if (ire->ire_src_addr != ipif->ipif_src_addr)
19015 				return;
19016 		} else {
19017 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
19018 			    &ipif->ipif_v6lcl_addr))
19019 				return;
19020 		}
19021 		ire_delete(ire);
19022 		return;
19023 	}
19024 	/*
19025 	 * ire_delete() will do an ire_flush_cache which will delete
19026 	 * all ire_ipif matches
19027 	 */
19028 	ire_delete(ire);
19029 }
19030 
19031 /*
19032  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
19033  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
19034  * 2) when an interface is brought up or down (on that ill).
19035  * This ensures that the IRE_CACHE entries don't retain stale source
19036  * address selection results.
19037  */
19038 void
19039 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
19040 {
19041 	ill_t	*ill = (ill_t *)ill_arg;
19042 	ill_t	*ipif_ill;
19043 
19044 	ASSERT(IAM_WRITER_ILL(ill));
19045 	/*
19046 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19047 	 * Hence this should be IRE_CACHE.
19048 	 */
19049 	ASSERT(ire->ire_type == IRE_CACHE);
19050 
19051 	/*
19052 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19053 	 * We are only interested in IRE_CACHES that has borrowed
19054 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19055 	 * for which we need to look at ire_ipif->ipif_ill match
19056 	 * with ill.
19057 	 */
19058 	ASSERT(ire->ire_ipif != NULL);
19059 	ipif_ill = ire->ire_ipif->ipif_ill;
19060 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19061 	    ipif_ill->ill_group == ill->ill_group)) {
19062 		ire_delete(ire);
19063 	}
19064 }
19065 
19066 /*
19067  * Delete all the ire whose stq references ill_arg.
19068  */
19069 static void
19070 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19071 {
19072 	ill_t	*ill = (ill_t *)ill_arg;
19073 	ill_t	*ire_ill;
19074 
19075 	ASSERT(IAM_WRITER_ILL(ill));
19076 	/*
19077 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19078 	 * Hence this should be IRE_CACHE.
19079 	 */
19080 	ASSERT(ire->ire_type == IRE_CACHE);
19081 
19082 	/*
19083 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19084 	 * matches ill. We are only interested in IRE_CACHES that
19085 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19086 	 * filtering here.
19087 	 */
19088 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19089 
19090 	if (ire_ill == ill)
19091 		ire_delete(ire);
19092 }
19093 
19094 /*
19095  * This is called when an ill leaves the group. We want to delete
19096  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19097  * pointing at ill.
19098  */
19099 static void
19100 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19101 {
19102 	ill_t	*ill = (ill_t *)ill_arg;
19103 
19104 	ASSERT(IAM_WRITER_ILL(ill));
19105 	ASSERT(ill->ill_group == NULL);
19106 	/*
19107 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19108 	 * Hence this should be IRE_CACHE.
19109 	 */
19110 	ASSERT(ire->ire_type == IRE_CACHE);
19111 	/*
19112 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19113 	 * matches ill. We are interested in both.
19114 	 */
19115 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19116 	    (ire->ire_ipif->ipif_ill == ill));
19117 
19118 	ire_delete(ire);
19119 }
19120 
19121 /*
19122  * Initiate deallocate of an IPIF. Always called as writer. Called by
19123  * ill_delete or ip_sioctl_removeif.
19124  */
19125 static void
19126 ipif_free(ipif_t *ipif)
19127 {
19128 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19129 
19130 	ASSERT(IAM_WRITER_IPIF(ipif));
19131 
19132 	if (ipif->ipif_recovery_id != 0)
19133 		(void) untimeout(ipif->ipif_recovery_id);
19134 	ipif->ipif_recovery_id = 0;
19135 
19136 	/* Remove conn references */
19137 	reset_conn_ipif(ipif);
19138 
19139 	/*
19140 	 * Make sure we have valid net and subnet broadcast ire's for the
19141 	 * other ipif's which share them with this ipif.
19142 	 */
19143 	if (!ipif->ipif_isv6)
19144 		ipif_check_bcast_ires(ipif);
19145 
19146 	/*
19147 	 * Take down the interface. We can be called either from ill_delete
19148 	 * or from ip_sioctl_removeif.
19149 	 */
19150 	(void) ipif_down(ipif, NULL, NULL);
19151 
19152 	/*
19153 	 * Now that the interface is down, there's no chance it can still
19154 	 * become a duplicate.  Cancel any timer that may have been set while
19155 	 * tearing down.
19156 	 */
19157 	if (ipif->ipif_recovery_id != 0)
19158 		(void) untimeout(ipif->ipif_recovery_id);
19159 	ipif->ipif_recovery_id = 0;
19160 
19161 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19162 	/* Remove pointers to this ill in the multicast routing tables */
19163 	reset_mrt_vif_ipif(ipif);
19164 	rw_exit(&ipst->ips_ill_g_lock);
19165 }
19166 
19167 /*
19168  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19169  * also ill_move().
19170  */
19171 static void
19172 ipif_free_tail(ipif_t *ipif)
19173 {
19174 	mblk_t	*mp;
19175 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19176 
19177 	/*
19178 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19179 	 */
19180 	mutex_enter(&ipif->ipif_saved_ire_lock);
19181 	mp = ipif->ipif_saved_ire_mp;
19182 	ipif->ipif_saved_ire_mp = NULL;
19183 	mutex_exit(&ipif->ipif_saved_ire_lock);
19184 	freemsg(mp);
19185 
19186 	/*
19187 	 * Need to hold both ill_g_lock and ill_lock while
19188 	 * inserting or removing an ipif from the linked list
19189 	 * of ipifs hanging off the ill.
19190 	 */
19191 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19192 
19193 	ASSERT(ilm_walk_ipif(ipif) == 0);
19194 
19195 #ifdef DEBUG
19196 	ipif_trace_cleanup(ipif);
19197 #endif
19198 
19199 	/* Ask SCTP to take it out of it list */
19200 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19201 
19202 	/* Get it out of the ILL interface list. */
19203 	ipif_remove(ipif, B_TRUE);
19204 	rw_exit(&ipst->ips_ill_g_lock);
19205 
19206 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19207 
19208 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19209 	ASSERT(ipif->ipif_recovery_id == 0);
19210 
19211 	/* Free the memory. */
19212 	mi_free(ipif);
19213 }
19214 
19215 /*
19216  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19217  * is zero.
19218  */
19219 void
19220 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19221 {
19222 	char	lbuf[LIFNAMSIZ];
19223 	char	*name;
19224 	size_t	name_len;
19225 
19226 	buf[0] = '\0';
19227 	name = ipif->ipif_ill->ill_name;
19228 	name_len = ipif->ipif_ill->ill_name_length;
19229 	if (ipif->ipif_id != 0) {
19230 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19231 		    ipif->ipif_id);
19232 		name = lbuf;
19233 		name_len = mi_strlen(name) + 1;
19234 	}
19235 	len -= 1;
19236 	buf[len] = '\0';
19237 	len = MIN(len, name_len);
19238 	bcopy(name, buf, len);
19239 }
19240 
19241 /*
19242  * Find an IPIF based on the name passed in.  Names can be of the
19243  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19244  * The <phys> string can have forms like <dev><#> (e.g., le0),
19245  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19246  * When there is no colon, the implied unit id is zero. <phys> must
19247  * correspond to the name of an ILL.  (May be called as writer.)
19248  */
19249 static ipif_t *
19250 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19251     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19252     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19253 {
19254 	char	*cp;
19255 	char	*endp;
19256 	long	id;
19257 	ill_t	*ill;
19258 	ipif_t	*ipif;
19259 	uint_t	ire_type;
19260 	boolean_t did_alloc = B_FALSE;
19261 	ipsq_t	*ipsq;
19262 
19263 	if (error != NULL)
19264 		*error = 0;
19265 
19266 	/*
19267 	 * If the caller wants to us to create the ipif, make sure we have a
19268 	 * valid zoneid
19269 	 */
19270 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19271 
19272 	if (namelen == 0) {
19273 		if (error != NULL)
19274 			*error = ENXIO;
19275 		return (NULL);
19276 	}
19277 
19278 	*exists = B_FALSE;
19279 	/* Look for a colon in the name. */
19280 	endp = &name[namelen];
19281 	for (cp = endp; --cp > name; ) {
19282 		if (*cp == IPIF_SEPARATOR_CHAR)
19283 			break;
19284 	}
19285 
19286 	if (*cp == IPIF_SEPARATOR_CHAR) {
19287 		/*
19288 		 * Reject any non-decimal aliases for logical
19289 		 * interfaces. Aliases with leading zeroes
19290 		 * are also rejected as they introduce ambiguity
19291 		 * in the naming of the interfaces.
19292 		 * In order to confirm with existing semantics,
19293 		 * and to not break any programs/script relying
19294 		 * on that behaviour, if<0>:0 is considered to be
19295 		 * a valid interface.
19296 		 *
19297 		 * If alias has two or more digits and the first
19298 		 * is zero, fail.
19299 		 */
19300 		if (&cp[2] < endp && cp[1] == '0') {
19301 			if (error != NULL)
19302 				*error = EINVAL;
19303 			return (NULL);
19304 		}
19305 	}
19306 
19307 	if (cp <= name) {
19308 		cp = endp;
19309 	} else {
19310 		*cp = '\0';
19311 	}
19312 
19313 	/*
19314 	 * Look up the ILL, based on the portion of the name
19315 	 * before the slash. ill_lookup_on_name returns a held ill.
19316 	 * Temporary to check whether ill exists already. If so
19317 	 * ill_lookup_on_name will clear it.
19318 	 */
19319 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19320 	    q, mp, func, error, &did_alloc, ipst);
19321 	if (cp != endp)
19322 		*cp = IPIF_SEPARATOR_CHAR;
19323 	if (ill == NULL)
19324 		return (NULL);
19325 
19326 	/* Establish the unit number in the name. */
19327 	id = 0;
19328 	if (cp < endp && *endp == '\0') {
19329 		/* If there was a colon, the unit number follows. */
19330 		cp++;
19331 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19332 			ill_refrele(ill);
19333 			if (error != NULL)
19334 				*error = ENXIO;
19335 			return (NULL);
19336 		}
19337 	}
19338 
19339 	GRAB_CONN_LOCK(q);
19340 	mutex_enter(&ill->ill_lock);
19341 	/* Now see if there is an IPIF with this unit number. */
19342 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19343 		if (ipif->ipif_id == id) {
19344 			if (zoneid != ALL_ZONES &&
19345 			    zoneid != ipif->ipif_zoneid &&
19346 			    ipif->ipif_zoneid != ALL_ZONES) {
19347 				mutex_exit(&ill->ill_lock);
19348 				RELEASE_CONN_LOCK(q);
19349 				ill_refrele(ill);
19350 				if (error != NULL)
19351 					*error = ENXIO;
19352 				return (NULL);
19353 			}
19354 			/*
19355 			 * The block comment at the start of ipif_down
19356 			 * explains the use of the macros used below
19357 			 */
19358 			if (IPIF_CAN_LOOKUP(ipif)) {
19359 				ipif_refhold_locked(ipif);
19360 				mutex_exit(&ill->ill_lock);
19361 				if (!did_alloc)
19362 					*exists = B_TRUE;
19363 				/*
19364 				 * Drop locks before calling ill_refrele
19365 				 * since it can potentially call into
19366 				 * ipif_ill_refrele_tail which can end up
19367 				 * in trying to acquire any lock.
19368 				 */
19369 				RELEASE_CONN_LOCK(q);
19370 				ill_refrele(ill);
19371 				return (ipif);
19372 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19373 				ipsq = ill->ill_phyint->phyint_ipsq;
19374 				mutex_enter(&ipsq->ipsq_lock);
19375 				mutex_exit(&ill->ill_lock);
19376 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19377 				mutex_exit(&ipsq->ipsq_lock);
19378 				RELEASE_CONN_LOCK(q);
19379 				ill_refrele(ill);
19380 				if (error != NULL)
19381 					*error = EINPROGRESS;
19382 				return (NULL);
19383 			}
19384 		}
19385 	}
19386 	RELEASE_CONN_LOCK(q);
19387 
19388 	if (!do_alloc) {
19389 		mutex_exit(&ill->ill_lock);
19390 		ill_refrele(ill);
19391 		if (error != NULL)
19392 			*error = ENXIO;
19393 		return (NULL);
19394 	}
19395 
19396 	/*
19397 	 * If none found, atomically allocate and return a new one.
19398 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19399 	 * to support "receive only" use of lo0:1 etc. as is still done
19400 	 * below as an initial guess.
19401 	 * However, this is now likely to be overriden later in ipif_up_done()
19402 	 * when we know for sure what address has been configured on the
19403 	 * interface, since we might have more than one loopback interface
19404 	 * with a loopback address, e.g. in the case of zones, and all the
19405 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19406 	 */
19407 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19408 		ire_type = IRE_LOOPBACK;
19409 	else
19410 		ire_type = IRE_LOCAL;
19411 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19412 	if (ipif != NULL)
19413 		ipif_refhold_locked(ipif);
19414 	else if (error != NULL)
19415 		*error = ENOMEM;
19416 	mutex_exit(&ill->ill_lock);
19417 	ill_refrele(ill);
19418 	return (ipif);
19419 }
19420 
19421 /*
19422  * This routine is called whenever a new address comes up on an ipif.  If
19423  * we are configured to respond to address mask requests, then we are supposed
19424  * to broadcast an address mask reply at this time.  This routine is also
19425  * called if we are already up, but a netmask change is made.  This is legal
19426  * but might not make the system manager very popular.	(May be called
19427  * as writer.)
19428  */
19429 void
19430 ipif_mask_reply(ipif_t *ipif)
19431 {
19432 	icmph_t	*icmph;
19433 	ipha_t	*ipha;
19434 	mblk_t	*mp;
19435 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19436 
19437 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19438 
19439 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19440 		return;
19441 
19442 	/* ICMP mask reply is IPv4 only */
19443 	ASSERT(!ipif->ipif_isv6);
19444 	/* ICMP mask reply is not for a loopback interface */
19445 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19446 
19447 	mp = allocb(REPLY_LEN, BPRI_HI);
19448 	if (mp == NULL)
19449 		return;
19450 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19451 
19452 	ipha = (ipha_t *)mp->b_rptr;
19453 	bzero(ipha, REPLY_LEN);
19454 	*ipha = icmp_ipha;
19455 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19456 	ipha->ipha_src = ipif->ipif_src_addr;
19457 	ipha->ipha_dst = ipif->ipif_brd_addr;
19458 	ipha->ipha_length = htons(REPLY_LEN);
19459 	ipha->ipha_ident = 0;
19460 
19461 	icmph = (icmph_t *)&ipha[1];
19462 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19463 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19464 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19465 
19466 	put(ipif->ipif_wq, mp);
19467 
19468 #undef	REPLY_LEN
19469 }
19470 
19471 /*
19472  * When the mtu in the ipif changes, we call this routine through ire_walk
19473  * to update all the relevant IREs.
19474  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19475  */
19476 static void
19477 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19478 {
19479 	ipif_t *ipif = (ipif_t *)ipif_arg;
19480 
19481 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19482 		return;
19483 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19484 }
19485 
19486 /*
19487  * When the mtu in the ill changes, we call this routine through ire_walk
19488  * to update all the relevant IREs.
19489  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19490  */
19491 void
19492 ill_mtu_change(ire_t *ire, char *ill_arg)
19493 {
19494 	ill_t	*ill = (ill_t *)ill_arg;
19495 
19496 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19497 		return;
19498 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19499 }
19500 
19501 /*
19502  * Join the ipif specific multicast groups.
19503  * Must be called after a mapping has been set up in the resolver.  (Always
19504  * called as writer.)
19505  */
19506 void
19507 ipif_multicast_up(ipif_t *ipif)
19508 {
19509 	int err, index;
19510 	ill_t *ill;
19511 
19512 	ASSERT(IAM_WRITER_IPIF(ipif));
19513 
19514 	ill = ipif->ipif_ill;
19515 	index = ill->ill_phyint->phyint_ifindex;
19516 
19517 	ip1dbg(("ipif_multicast_up\n"));
19518 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19519 		return;
19520 
19521 	if (ipif->ipif_isv6) {
19522 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19523 			return;
19524 
19525 		/* Join the all hosts multicast address */
19526 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19527 		/*
19528 		 * Passing B_TRUE means we have to join the multicast
19529 		 * membership on this interface even though this is
19530 		 * FAILED. If we join on a different one in the group,
19531 		 * we will not be able to delete the membership later
19532 		 * as we currently don't track where we join when we
19533 		 * join within the kernel unlike applications where
19534 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19535 		 * for more on this.
19536 		 */
19537 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19538 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19539 		if (err != 0) {
19540 			ip0dbg(("ipif_multicast_up: "
19541 			    "all_hosts_mcast failed %d\n",
19542 			    err));
19543 			return;
19544 		}
19545 		/*
19546 		 * Enable multicast for the solicited node multicast address
19547 		 */
19548 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19549 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19550 
19551 			ipv6_multi.s6_addr32[3] |=
19552 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19553 
19554 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19555 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19556 			    NULL);
19557 			if (err != 0) {
19558 				ip0dbg(("ipif_multicast_up: solicited MC"
19559 				    " failed %d\n", err));
19560 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19561 				    ill, ill->ill_phyint->phyint_ifindex,
19562 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19563 				return;
19564 			}
19565 		}
19566 	} else {
19567 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19568 			return;
19569 
19570 		/* Join the all hosts multicast address */
19571 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19572 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19573 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19574 		if (err) {
19575 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19576 			return;
19577 		}
19578 	}
19579 	ipif->ipif_multicast_up = 1;
19580 }
19581 
19582 /*
19583  * Blow away any multicast groups that we joined in ipif_multicast_up().
19584  * (Explicit memberships are blown away in ill_leave_multicast() when the
19585  * ill is brought down.)
19586  */
19587 static void
19588 ipif_multicast_down(ipif_t *ipif)
19589 {
19590 	int err;
19591 
19592 	ASSERT(IAM_WRITER_IPIF(ipif));
19593 
19594 	ip1dbg(("ipif_multicast_down\n"));
19595 	if (!ipif->ipif_multicast_up)
19596 		return;
19597 
19598 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19599 
19600 	if (!ipif->ipif_isv6) {
19601 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19602 		    B_TRUE);
19603 		if (err != 0)
19604 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19605 
19606 		ipif->ipif_multicast_up = 0;
19607 		return;
19608 	}
19609 
19610 	/*
19611 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19612 	 * we should look for ilms on this ill rather than the ones that have
19613 	 * been failed over here.  They are here temporarily. As
19614 	 * ipif_multicast_up has joined on this ill, we should delete only
19615 	 * from this ill.
19616 	 */
19617 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19618 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19619 	    B_TRUE, B_TRUE);
19620 	if (err != 0) {
19621 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19622 		    err));
19623 	}
19624 	/*
19625 	 * Disable multicast for the solicited node multicast address
19626 	 */
19627 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19628 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19629 
19630 		ipv6_multi.s6_addr32[3] |=
19631 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19632 
19633 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19634 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19635 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19636 
19637 		if (err != 0) {
19638 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19639 			    err));
19640 		}
19641 	}
19642 
19643 	ipif->ipif_multicast_up = 0;
19644 }
19645 
19646 /*
19647  * Used when an interface comes up to recreate any extra routes on this
19648  * interface.
19649  */
19650 static ire_t **
19651 ipif_recover_ire(ipif_t *ipif)
19652 {
19653 	mblk_t	*mp;
19654 	ire_t	**ipif_saved_irep;
19655 	ire_t	**irep;
19656 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19657 
19658 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19659 	    ipif->ipif_id));
19660 
19661 	mutex_enter(&ipif->ipif_saved_ire_lock);
19662 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19663 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19664 	if (ipif_saved_irep == NULL) {
19665 		mutex_exit(&ipif->ipif_saved_ire_lock);
19666 		return (NULL);
19667 	}
19668 
19669 	irep = ipif_saved_irep;
19670 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19671 		ire_t		*ire;
19672 		queue_t		*rfq;
19673 		queue_t		*stq;
19674 		ifrt_t		*ifrt;
19675 		uchar_t		*src_addr;
19676 		uchar_t		*gateway_addr;
19677 		ushort_t	type;
19678 
19679 		/*
19680 		 * When the ire was initially created and then added in
19681 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19682 		 * in the case of a traditional interface route, or as one of
19683 		 * the IRE_OFFSUBNET types (with the exception of
19684 		 * IRE_HOST types ire which is created by icmp_redirect() and
19685 		 * which we don't need to save or recover).  In the case where
19686 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19687 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19688 		 * to satisfy software like GateD and Sun Cluster which creates
19689 		 * routes using the the loopback interface's address as a
19690 		 * gateway.
19691 		 *
19692 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19693 		 * ire_create() will be called in the same way here as
19694 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19695 		 * the route looks like a traditional interface route (where
19696 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19697 		 * the saved ifrt->ifrt_type.  This means that in the case where
19698 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19699 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19700 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19701 		 */
19702 		ifrt = (ifrt_t *)mp->b_rptr;
19703 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19704 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19705 			rfq = NULL;
19706 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19707 			    ? ipif->ipif_rq : ipif->ipif_wq;
19708 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19709 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19710 			    : (uint8_t *)&ipif->ipif_src_addr;
19711 			gateway_addr = NULL;
19712 			type = ipif->ipif_net_type;
19713 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19714 			/* Recover multiroute broadcast IRE. */
19715 			rfq = ipif->ipif_rq;
19716 			stq = ipif->ipif_wq;
19717 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19718 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19719 			    : (uint8_t *)&ipif->ipif_src_addr;
19720 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19721 			type = ifrt->ifrt_type;
19722 		} else {
19723 			rfq = NULL;
19724 			stq = NULL;
19725 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19726 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19727 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19728 			type = ifrt->ifrt_type;
19729 		}
19730 
19731 		/*
19732 		 * Create a copy of the IRE with the saved address and netmask.
19733 		 */
19734 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19735 		    "0x%x/0x%x\n",
19736 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19737 		    ntohl(ifrt->ifrt_addr),
19738 		    ntohl(ifrt->ifrt_mask)));
19739 		ire = ire_create(
19740 		    (uint8_t *)&ifrt->ifrt_addr,
19741 		    (uint8_t *)&ifrt->ifrt_mask,
19742 		    src_addr,
19743 		    gateway_addr,
19744 		    &ifrt->ifrt_max_frag,
19745 		    NULL,
19746 		    rfq,
19747 		    stq,
19748 		    type,
19749 		    ipif,
19750 		    0,
19751 		    0,
19752 		    0,
19753 		    ifrt->ifrt_flags,
19754 		    &ifrt->ifrt_iulp_info,
19755 		    NULL,
19756 		    NULL,
19757 		    ipst);
19758 
19759 		if (ire == NULL) {
19760 			mutex_exit(&ipif->ipif_saved_ire_lock);
19761 			kmem_free(ipif_saved_irep,
19762 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19763 			return (NULL);
19764 		}
19765 
19766 		/*
19767 		 * Some software (for example, GateD and Sun Cluster) attempts
19768 		 * to create (what amount to) IRE_PREFIX routes with the
19769 		 * loopback address as the gateway.  This is primarily done to
19770 		 * set up prefixes with the RTF_REJECT flag set (for example,
19771 		 * when generating aggregate routes.)
19772 		 *
19773 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19774 		 * IRE_LOOPBACK, then we map the request into a
19775 		 * IRE_IF_NORESOLVER.
19776 		 */
19777 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19778 			ire->ire_type = IRE_IF_NORESOLVER;
19779 		/*
19780 		 * ire held by ire_add, will be refreled' towards the
19781 		 * the end of ipif_up_done
19782 		 */
19783 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19784 		*irep = ire;
19785 		irep++;
19786 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19787 	}
19788 	mutex_exit(&ipif->ipif_saved_ire_lock);
19789 	return (ipif_saved_irep);
19790 }
19791 
19792 /*
19793  * Used to set the netmask and broadcast address to default values when the
19794  * interface is brought up.  (Always called as writer.)
19795  */
19796 static void
19797 ipif_set_default(ipif_t *ipif)
19798 {
19799 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19800 
19801 	if (!ipif->ipif_isv6) {
19802 		/*
19803 		 * Interface holds an IPv4 address. Default
19804 		 * mask is the natural netmask.
19805 		 */
19806 		if (!ipif->ipif_net_mask) {
19807 			ipaddr_t	v4mask;
19808 
19809 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19810 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19811 		}
19812 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19813 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19814 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19815 		} else {
19816 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19817 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19818 		}
19819 		/*
19820 		 * NOTE: SunOS 4.X does this even if the broadcast address
19821 		 * has been already set thus we do the same here.
19822 		 */
19823 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19824 			ipaddr_t	v4addr;
19825 
19826 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19827 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19828 		}
19829 	} else {
19830 		/*
19831 		 * Interface holds an IPv6-only address.  Default
19832 		 * mask is all-ones.
19833 		 */
19834 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19835 			ipif->ipif_v6net_mask = ipv6_all_ones;
19836 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19837 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19838 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19839 		} else {
19840 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19841 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19842 		}
19843 	}
19844 }
19845 
19846 /*
19847  * Return 0 if this address can be used as local address without causing
19848  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19849  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19850  * Special checks are needed to allow the same IPv6 link-local address
19851  * on different ills.
19852  * TODO: allowing the same site-local address on different ill's.
19853  */
19854 int
19855 ip_addr_availability_check(ipif_t *new_ipif)
19856 {
19857 	in6_addr_t our_v6addr;
19858 	ill_t *ill;
19859 	ipif_t *ipif;
19860 	ill_walk_context_t ctx;
19861 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19862 
19863 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19864 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19865 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19866 
19867 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19868 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19869 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19870 		return (0);
19871 
19872 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19873 
19874 	if (new_ipif->ipif_isv6)
19875 		ill = ILL_START_WALK_V6(&ctx, ipst);
19876 	else
19877 		ill = ILL_START_WALK_V4(&ctx, ipst);
19878 
19879 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19880 		for (ipif = ill->ill_ipif; ipif != NULL;
19881 		    ipif = ipif->ipif_next) {
19882 			if ((ipif == new_ipif) ||
19883 			    !(ipif->ipif_flags & IPIF_UP) ||
19884 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19885 				continue;
19886 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19887 			    &our_v6addr)) {
19888 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19889 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19890 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19891 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19892 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19893 				    new_ipif->ipif_ill != ill)
19894 					continue;
19895 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19896 				    new_ipif->ipif_ill != ill)
19897 					continue;
19898 				else if (new_ipif->ipif_zoneid !=
19899 				    ipif->ipif_zoneid &&
19900 				    ipif->ipif_zoneid != ALL_ZONES &&
19901 				    IS_LOOPBACK(ill))
19902 					continue;
19903 				else if (new_ipif->ipif_ill == ill)
19904 					return (EADDRINUSE);
19905 				else
19906 					return (EADDRNOTAVAIL);
19907 			}
19908 		}
19909 	}
19910 
19911 	return (0);
19912 }
19913 
19914 /*
19915  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19916  * IREs for the ipif.
19917  * When the routine returns EINPROGRESS then mp has been consumed and
19918  * the ioctl will be acked from ip_rput_dlpi.
19919  */
19920 static int
19921 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19922 {
19923 	ill_t	*ill = ipif->ipif_ill;
19924 	boolean_t isv6 = ipif->ipif_isv6;
19925 	int	err = 0;
19926 	boolean_t success;
19927 
19928 	ASSERT(IAM_WRITER_IPIF(ipif));
19929 
19930 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19931 
19932 	/* Shouldn't get here if it is already up. */
19933 	if (ipif->ipif_flags & IPIF_UP)
19934 		return (EALREADY);
19935 
19936 	/* Skip arp/ndp for any loopback interface. */
19937 	if (ill->ill_wq != NULL) {
19938 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19939 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19940 
19941 		if (!ill->ill_dl_up) {
19942 			/*
19943 			 * ill_dl_up is not yet set. i.e. we are yet to
19944 			 * DL_BIND with the driver and this is the first
19945 			 * logical interface on the ill to become "up".
19946 			 * Tell the driver to get going (via DL_BIND_REQ).
19947 			 * Note that changing "significant" IFF_ flags
19948 			 * address/netmask etc cause a down/up dance, but
19949 			 * does not cause an unbind (DL_UNBIND) with the driver
19950 			 */
19951 			return (ill_dl_up(ill, ipif, mp, q));
19952 		}
19953 
19954 		/*
19955 		 * ipif_resolver_up may end up sending an
19956 		 * AR_INTERFACE_UP message to ARP, which would, in
19957 		 * turn send a DLPI message to the driver. ioctls are
19958 		 * serialized and so we cannot send more than one
19959 		 * interface up message at a time. If ipif_resolver_up
19960 		 * does send an interface up message to ARP, we get
19961 		 * EINPROGRESS and we will complete in ip_arp_done.
19962 		 */
19963 
19964 		ASSERT(connp != NULL || !CONN_Q(q));
19965 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19966 		if (connp != NULL)
19967 			mutex_enter(&connp->conn_lock);
19968 		mutex_enter(&ill->ill_lock);
19969 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19970 		mutex_exit(&ill->ill_lock);
19971 		if (connp != NULL)
19972 			mutex_exit(&connp->conn_lock);
19973 		if (!success)
19974 			return (EINTR);
19975 
19976 		/*
19977 		 * Crank up IPv6 neighbor discovery
19978 		 * Unlike ARP, this should complete when
19979 		 * ipif_ndp_up returns. However, for
19980 		 * ILLF_XRESOLV interfaces we also send a
19981 		 * AR_INTERFACE_UP to the external resolver.
19982 		 * That ioctl will complete in ip_rput.
19983 		 */
19984 		if (isv6) {
19985 			err = ipif_ndp_up(ipif);
19986 			if (err != 0) {
19987 				if (err != EINPROGRESS)
19988 					mp = ipsq_pending_mp_get(ipsq, &connp);
19989 				return (err);
19990 			}
19991 		}
19992 		/* Now, ARP */
19993 		err = ipif_resolver_up(ipif, Res_act_initial);
19994 		if (err == EINPROGRESS) {
19995 			/* We will complete it in ip_arp_done */
19996 			return (err);
19997 		}
19998 		mp = ipsq_pending_mp_get(ipsq, &connp);
19999 		ASSERT(mp != NULL);
20000 		if (err != 0)
20001 			return (err);
20002 	} else {
20003 		/*
20004 		 * Interfaces without underlying hardware don't do duplicate
20005 		 * address detection.
20006 		 */
20007 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
20008 		ipif->ipif_addr_ready = 1;
20009 	}
20010 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
20011 }
20012 
20013 /*
20014  * Perform a bind for the physical device.
20015  * When the routine returns EINPROGRESS then mp has been consumed and
20016  * the ioctl will be acked from ip_rput_dlpi.
20017  * Allocate an unbind message and save it until ipif_down.
20018  */
20019 static int
20020 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
20021 {
20022 	areq_t	*areq;
20023 	mblk_t	*areq_mp = NULL;
20024 	mblk_t	*bind_mp = NULL;
20025 	mblk_t	*unbind_mp = NULL;
20026 	conn_t	*connp;
20027 	boolean_t success;
20028 	uint16_t sap_addr;
20029 
20030 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
20031 	ASSERT(IAM_WRITER_ILL(ill));
20032 	ASSERT(mp != NULL);
20033 
20034 	/* Create a resolver cookie for ARP */
20035 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
20036 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
20037 		if (areq_mp == NULL)
20038 			return (ENOMEM);
20039 
20040 		freemsg(ill->ill_resolver_mp);
20041 		ill->ill_resolver_mp = areq_mp;
20042 		areq = (areq_t *)areq_mp->b_rptr;
20043 		sap_addr = ill->ill_sap;
20044 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20045 	}
20046 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20047 	    DL_BIND_REQ);
20048 	if (bind_mp == NULL)
20049 		goto bad;
20050 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20051 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20052 
20053 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20054 	if (unbind_mp == NULL)
20055 		goto bad;
20056 
20057 	/*
20058 	 * Record state needed to complete this operation when the
20059 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20060 	 */
20061 	ASSERT(WR(q)->q_next == NULL);
20062 	connp = Q_TO_CONN(q);
20063 
20064 	mutex_enter(&connp->conn_lock);
20065 	mutex_enter(&ipif->ipif_ill->ill_lock);
20066 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20067 	mutex_exit(&ipif->ipif_ill->ill_lock);
20068 	mutex_exit(&connp->conn_lock);
20069 	if (!success)
20070 		goto bad;
20071 
20072 	/*
20073 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20074 	 * the interface goes down.
20075 	 */
20076 	ASSERT(ill->ill_unbind_mp == NULL);
20077 	ill->ill_unbind_mp = unbind_mp;
20078 
20079 	ill_dlpi_send(ill, bind_mp);
20080 	/* Send down link-layer capabilities probe if not already done. */
20081 	ill_capability_probe(ill);
20082 
20083 	/*
20084 	 * Sysid used to rely on the fact that netboots set domainname
20085 	 * and the like. Now that miniroot boots aren't strictly netboots
20086 	 * and miniroot network configuration is driven from userland
20087 	 * these things still need to be set. This situation can be detected
20088 	 * by comparing the interface being configured here to the one
20089 	 * dhcifname was set to reference by the boot loader. Once sysid is
20090 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20091 	 */
20092 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
20093 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
20094 	    (strlen(srpc_domain) == 0)) {
20095 		if (dhcpinit() != 0)
20096 			cmn_err(CE_WARN, "no cached dhcp response");
20097 	}
20098 
20099 	/*
20100 	 * This operation will complete in ip_rput_dlpi with either
20101 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20102 	 */
20103 	return (EINPROGRESS);
20104 bad:
20105 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20106 	/*
20107 	 * We don't have to check for possible removal from illgrp
20108 	 * as we have not yet inserted in illgrp. For groups
20109 	 * without names, this ipif is still not UP and hence
20110 	 * this could not have possibly had any influence in forming
20111 	 * groups.
20112 	 */
20113 
20114 	freemsg(bind_mp);
20115 	freemsg(unbind_mp);
20116 	return (ENOMEM);
20117 }
20118 
20119 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20120 
20121 /*
20122  * DLPI and ARP is up.
20123  * Create all the IREs associated with an interface bring up multicast.
20124  * Set the interface flag and finish other initialization
20125  * that potentially had to be differed to after DL_BIND_ACK.
20126  */
20127 int
20128 ipif_up_done(ipif_t *ipif)
20129 {
20130 	ire_t	*ire_array[20];
20131 	ire_t	**irep = ire_array;
20132 	ire_t	**irep1;
20133 	ipaddr_t net_mask = 0;
20134 	ipaddr_t subnet_mask, route_mask;
20135 	ill_t	*ill = ipif->ipif_ill;
20136 	queue_t	*stq;
20137 	ipif_t	 *src_ipif;
20138 	ipif_t   *tmp_ipif;
20139 	boolean_t	flush_ire_cache = B_TRUE;
20140 	int	err = 0;
20141 	phyint_t *phyi;
20142 	ire_t	**ipif_saved_irep = NULL;
20143 	int ipif_saved_ire_cnt;
20144 	int	cnt;
20145 	boolean_t	src_ipif_held = B_FALSE;
20146 	boolean_t	ire_added = B_FALSE;
20147 	boolean_t	loopback = B_FALSE;
20148 	ip_stack_t	*ipst = ill->ill_ipst;
20149 
20150 	ip1dbg(("ipif_up_done(%s:%u)\n",
20151 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20152 	/* Check if this is a loopback interface */
20153 	if (ipif->ipif_ill->ill_wq == NULL)
20154 		loopback = B_TRUE;
20155 
20156 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20157 	/*
20158 	 * If all other interfaces for this ill are down or DEPRECATED,
20159 	 * or otherwise unsuitable for source address selection, remove
20160 	 * any IRE_CACHE entries for this ill to make sure source
20161 	 * address selection gets to take this new ipif into account.
20162 	 * No need to hold ill_lock while traversing the ipif list since
20163 	 * we are writer
20164 	 */
20165 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20166 	    tmp_ipif = tmp_ipif->ipif_next) {
20167 		if (((tmp_ipif->ipif_flags &
20168 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20169 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20170 		    (tmp_ipif == ipif))
20171 			continue;
20172 		/* first useable pre-existing interface */
20173 		flush_ire_cache = B_FALSE;
20174 		break;
20175 	}
20176 	if (flush_ire_cache)
20177 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20178 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20179 
20180 	/*
20181 	 * Figure out which way the send-to queue should go.  Only
20182 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20183 	 * should show up here.
20184 	 */
20185 	switch (ill->ill_net_type) {
20186 	case IRE_IF_RESOLVER:
20187 		stq = ill->ill_rq;
20188 		break;
20189 	case IRE_IF_NORESOLVER:
20190 	case IRE_LOOPBACK:
20191 		stq = ill->ill_wq;
20192 		break;
20193 	default:
20194 		return (EINVAL);
20195 	}
20196 
20197 	if (IS_LOOPBACK(ill)) {
20198 		/*
20199 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20200 		 * ipif_lookup_on_name(), but in the case of zones we can have
20201 		 * several loopback addresses on lo0. So all the interfaces with
20202 		 * loopback addresses need to be marked IRE_LOOPBACK.
20203 		 */
20204 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20205 		    htonl(INADDR_LOOPBACK))
20206 			ipif->ipif_ire_type = IRE_LOOPBACK;
20207 		else
20208 			ipif->ipif_ire_type = IRE_LOCAL;
20209 	}
20210 
20211 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20212 		/*
20213 		 * Can't use our source address. Select a different
20214 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20215 		 */
20216 		src_ipif = ipif_select_source(ipif->ipif_ill,
20217 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20218 		if (src_ipif == NULL)
20219 			src_ipif = ipif;	/* Last resort */
20220 		else
20221 			src_ipif_held = B_TRUE;
20222 	} else {
20223 		src_ipif = ipif;
20224 	}
20225 
20226 	/* Create all the IREs associated with this interface */
20227 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20228 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20229 
20230 		/*
20231 		 * If we're on a labeled system then make sure that zone-
20232 		 * private addresses have proper remote host database entries.
20233 		 */
20234 		if (is_system_labeled() &&
20235 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20236 		    !tsol_check_interface_address(ipif))
20237 			return (EINVAL);
20238 
20239 		/* Register the source address for __sin6_src_id */
20240 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20241 		    ipif->ipif_zoneid, ipst);
20242 		if (err != 0) {
20243 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20244 			return (err);
20245 		}
20246 
20247 		/* If the interface address is set, create the local IRE. */
20248 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20249 		    (void *)ipif,
20250 		    ipif->ipif_ire_type,
20251 		    ntohl(ipif->ipif_lcl_addr)));
20252 		*irep++ = ire_create(
20253 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20254 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20255 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20256 		    NULL,				/* no gateway */
20257 		    &ip_loopback_mtuplus,		/* max frag size */
20258 		    NULL,
20259 		    ipif->ipif_rq,			/* recv-from queue */
20260 		    NULL,				/* no send-to queue */
20261 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20262 		    ipif,
20263 		    0,
20264 		    0,
20265 		    0,
20266 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20267 		    RTF_PRIVATE : 0,
20268 		    &ire_uinfo_null,
20269 		    NULL,
20270 		    NULL,
20271 		    ipst);
20272 	} else {
20273 		ip1dbg((
20274 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20275 		    ipif->ipif_ire_type,
20276 		    ntohl(ipif->ipif_lcl_addr),
20277 		    (uint_t)ipif->ipif_flags));
20278 	}
20279 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20280 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20281 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20282 	} else {
20283 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20284 	}
20285 
20286 	subnet_mask = ipif->ipif_net_mask;
20287 
20288 	/*
20289 	 * If mask was not specified, use natural netmask of
20290 	 * interface address. Also, store this mask back into the
20291 	 * ipif struct.
20292 	 */
20293 	if (subnet_mask == 0) {
20294 		subnet_mask = net_mask;
20295 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20296 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20297 		    ipif->ipif_v6subnet);
20298 	}
20299 
20300 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20301 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20302 	    ipif->ipif_subnet != INADDR_ANY) {
20303 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20304 
20305 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20306 			route_mask = IP_HOST_MASK;
20307 		} else {
20308 			route_mask = subnet_mask;
20309 		}
20310 
20311 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20312 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20313 		    (void *)ipif, (void *)ill,
20314 		    ill->ill_net_type,
20315 		    ntohl(ipif->ipif_subnet)));
20316 		*irep++ = ire_create(
20317 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20318 		    (uchar_t *)&route_mask,		/* mask */
20319 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20320 		    NULL,				/* no gateway */
20321 		    &ipif->ipif_mtu,			/* max frag */
20322 		    NULL,
20323 		    NULL,				/* no recv queue */
20324 		    stq,				/* send-to queue */
20325 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20326 		    ipif,
20327 		    0,
20328 		    0,
20329 		    0,
20330 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20331 		    &ire_uinfo_null,
20332 		    NULL,
20333 		    NULL,
20334 		    ipst);
20335 	}
20336 
20337 	/*
20338 	 * Create any necessary broadcast IREs.
20339 	 */
20340 	if (ipif->ipif_flags & IPIF_BROADCAST)
20341 		irep = ipif_create_bcast_ires(ipif, irep);
20342 
20343 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20344 
20345 	/* If an earlier ire_create failed, get out now */
20346 	for (irep1 = irep; irep1 > ire_array; ) {
20347 		irep1--;
20348 		if (*irep1 == NULL) {
20349 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20350 			err = ENOMEM;
20351 			goto bad;
20352 		}
20353 	}
20354 
20355 	/*
20356 	 * Need to atomically check for ip_addr_availablity_check
20357 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20358 	 * from group also.The ill_g_lock is grabbed as reader
20359 	 * just to make sure no new ills or new ipifs are being added
20360 	 * to the system while we are checking the uniqueness of addresses.
20361 	 */
20362 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20363 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20364 	/* Mark it up, and increment counters. */
20365 	ipif->ipif_flags |= IPIF_UP;
20366 	ill->ill_ipif_up_count++;
20367 	err = ip_addr_availability_check(ipif);
20368 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20369 	rw_exit(&ipst->ips_ill_g_lock);
20370 
20371 	if (err != 0) {
20372 		/*
20373 		 * Our address may already be up on the same ill. In this case,
20374 		 * the ARP entry for our ipif replaced the one for the other
20375 		 * ipif. So we don't want to delete it (otherwise the other ipif
20376 		 * would be unable to send packets).
20377 		 * ip_addr_availability_check() identifies this case for us and
20378 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20379 		 * which is the expected error code.
20380 		 */
20381 		if (err == EADDRINUSE) {
20382 			freemsg(ipif->ipif_arp_del_mp);
20383 			ipif->ipif_arp_del_mp = NULL;
20384 			err = EADDRNOTAVAIL;
20385 		}
20386 		ill->ill_ipif_up_count--;
20387 		ipif->ipif_flags &= ~IPIF_UP;
20388 		goto bad;
20389 	}
20390 
20391 	/*
20392 	 * Add in all newly created IREs.  ire_create_bcast() has
20393 	 * already checked for duplicates of the IRE_BROADCAST type.
20394 	 * We want to add before we call ifgrp_insert which wants
20395 	 * to know whether IRE_IF_RESOLVER exists or not.
20396 	 *
20397 	 * NOTE : We refrele the ire though we may branch to "bad"
20398 	 *	  later on where we do ire_delete. This is okay
20399 	 *	  because nobody can delete it as we are running
20400 	 *	  exclusively.
20401 	 */
20402 	for (irep1 = irep; irep1 > ire_array; ) {
20403 		irep1--;
20404 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20405 		/*
20406 		 * refheld by ire_add. refele towards the end of the func
20407 		 */
20408 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20409 	}
20410 	ire_added = B_TRUE;
20411 	/*
20412 	 * Form groups if possible.
20413 	 *
20414 	 * If we are supposed to be in a ill_group with a name, insert it
20415 	 * now as we know that at least one ipif is UP. Otherwise form
20416 	 * nameless groups.
20417 	 *
20418 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20419 	 * this ipif into the appropriate interface group, or create a
20420 	 * new one. If this is already in a nameless group, we try to form
20421 	 * a bigger group looking at other ills potentially sharing this
20422 	 * ipif's prefix.
20423 	 */
20424 	phyi = ill->ill_phyint;
20425 	if (phyi->phyint_groupname_len != 0) {
20426 		ASSERT(phyi->phyint_groupname != NULL);
20427 		if (ill->ill_ipif_up_count == 1) {
20428 			ASSERT(ill->ill_group == NULL);
20429 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20430 			    phyi->phyint_groupname, NULL, B_TRUE);
20431 			if (err != 0) {
20432 				ip1dbg(("ipif_up_done: illgrp allocation "
20433 				    "failed, error %d\n", err));
20434 				goto bad;
20435 			}
20436 		}
20437 		ASSERT(ill->ill_group != NULL);
20438 	}
20439 
20440 	/*
20441 	 * When this is part of group, we need to make sure that
20442 	 * any broadcast ires created because of this ipif coming
20443 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20444 	 * so that we don't receive duplicate broadcast packets.
20445 	 */
20446 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20447 		ipif_renominate_bcast(ipif);
20448 
20449 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20450 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20451 	ipif_saved_irep = ipif_recover_ire(ipif);
20452 
20453 	if (!loopback) {
20454 		/*
20455 		 * If the broadcast address has been set, make sure it makes
20456 		 * sense based on the interface address.
20457 		 * Only match on ill since we are sharing broadcast addresses.
20458 		 */
20459 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20460 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20461 			ire_t	*ire;
20462 
20463 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20464 			    IRE_BROADCAST, ipif, ALL_ZONES,
20465 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20466 
20467 			if (ire == NULL) {
20468 				/*
20469 				 * If there isn't a matching broadcast IRE,
20470 				 * revert to the default for this netmask.
20471 				 */
20472 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20473 				mutex_enter(&ipif->ipif_ill->ill_lock);
20474 				ipif_set_default(ipif);
20475 				mutex_exit(&ipif->ipif_ill->ill_lock);
20476 			} else {
20477 				ire_refrele(ire);
20478 			}
20479 		}
20480 
20481 	}
20482 
20483 	/* This is the first interface on this ill */
20484 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20485 		/*
20486 		 * Need to recover all multicast memberships in the driver.
20487 		 * This had to be deferred until we had attached.
20488 		 */
20489 		ill_recover_multicast(ill);
20490 	}
20491 	/* Join the allhosts multicast address */
20492 	ipif_multicast_up(ipif);
20493 
20494 	if (!loopback) {
20495 		/*
20496 		 * See whether anybody else would benefit from the
20497 		 * new ipif that we added. We call this always rather
20498 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20499 		 * ipif is for the benefit of illgrp_insert (done above)
20500 		 * which does not do source address selection as it does
20501 		 * not want to re-create interface routes that we are
20502 		 * having reference to it here.
20503 		 */
20504 		ill_update_source_selection(ill);
20505 	}
20506 
20507 	for (irep1 = irep; irep1 > ire_array; ) {
20508 		irep1--;
20509 		if (*irep1 != NULL) {
20510 			/* was held in ire_add */
20511 			ire_refrele(*irep1);
20512 		}
20513 	}
20514 
20515 	cnt = ipif_saved_ire_cnt;
20516 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20517 		if (*irep1 != NULL) {
20518 			/* was held in ire_add */
20519 			ire_refrele(*irep1);
20520 		}
20521 	}
20522 
20523 	if (!loopback && ipif->ipif_addr_ready) {
20524 		/* Broadcast an address mask reply. */
20525 		ipif_mask_reply(ipif);
20526 	}
20527 	if (ipif_saved_irep != NULL) {
20528 		kmem_free(ipif_saved_irep,
20529 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20530 	}
20531 	if (src_ipif_held)
20532 		ipif_refrele(src_ipif);
20533 
20534 	/*
20535 	 * This had to be deferred until we had bound.  Tell routing sockets and
20536 	 * others that this interface is up if it looks like the address has
20537 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20538 	 * duplicate address detection to do its thing.
20539 	 */
20540 	if (ipif->ipif_addr_ready) {
20541 		ip_rts_ifmsg(ipif);
20542 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20543 		/* Let SCTP update the status for this ipif */
20544 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20545 	}
20546 	return (0);
20547 
20548 bad:
20549 	ip1dbg(("ipif_up_done: FAILED \n"));
20550 	/*
20551 	 * We don't have to bother removing from ill groups because
20552 	 *
20553 	 * 1) For groups with names, we insert only when the first ipif
20554 	 *    comes up. In that case if it fails, it will not be in any
20555 	 *    group. So, we need not try to remove for that case.
20556 	 *
20557 	 * 2) For groups without names, either we tried to insert ipif_ill
20558 	 *    in a group as singleton or found some other group to become
20559 	 *    a bigger group. For the former, if it fails we don't have
20560 	 *    anything to do as ipif_ill is not in the group and for the
20561 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20562 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20563 	 */
20564 	while (irep > ire_array) {
20565 		irep--;
20566 		if (*irep != NULL) {
20567 			ire_delete(*irep);
20568 			if (ire_added)
20569 				ire_refrele(*irep);
20570 		}
20571 	}
20572 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20573 
20574 	if (ipif_saved_irep != NULL) {
20575 		kmem_free(ipif_saved_irep,
20576 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20577 	}
20578 	if (src_ipif_held)
20579 		ipif_refrele(src_ipif);
20580 
20581 	ipif_arp_down(ipif);
20582 	return (err);
20583 }
20584 
20585 /*
20586  * Turn off the ARP with the ILLF_NOARP flag.
20587  */
20588 static int
20589 ill_arp_off(ill_t *ill)
20590 {
20591 	mblk_t	*arp_off_mp = NULL;
20592 	mblk_t	*arp_on_mp = NULL;
20593 
20594 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20595 
20596 	ASSERT(IAM_WRITER_ILL(ill));
20597 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20598 
20599 	/*
20600 	 * If the on message is still around we've already done
20601 	 * an arp_off without doing an arp_on thus there is no
20602 	 * work needed.
20603 	 */
20604 	if (ill->ill_arp_on_mp != NULL)
20605 		return (0);
20606 
20607 	/*
20608 	 * Allocate an ARP on message (to be saved) and an ARP off message
20609 	 */
20610 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20611 	if (!arp_off_mp)
20612 		return (ENOMEM);
20613 
20614 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20615 	if (!arp_on_mp)
20616 		goto failed;
20617 
20618 	ASSERT(ill->ill_arp_on_mp == NULL);
20619 	ill->ill_arp_on_mp = arp_on_mp;
20620 
20621 	/* Send an AR_INTERFACE_OFF request */
20622 	putnext(ill->ill_rq, arp_off_mp);
20623 	return (0);
20624 failed:
20625 
20626 	if (arp_off_mp)
20627 		freemsg(arp_off_mp);
20628 	return (ENOMEM);
20629 }
20630 
20631 /*
20632  * Turn on ARP by turning off the ILLF_NOARP flag.
20633  */
20634 static int
20635 ill_arp_on(ill_t *ill)
20636 {
20637 	mblk_t	*mp;
20638 
20639 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20640 
20641 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20642 
20643 	ASSERT(IAM_WRITER_ILL(ill));
20644 	/*
20645 	 * Send an AR_INTERFACE_ON request if we have already done
20646 	 * an arp_off (which allocated the message).
20647 	 */
20648 	if (ill->ill_arp_on_mp != NULL) {
20649 		mp = ill->ill_arp_on_mp;
20650 		ill->ill_arp_on_mp = NULL;
20651 		putnext(ill->ill_rq, mp);
20652 	}
20653 	return (0);
20654 }
20655 
20656 /*
20657  * Called after either deleting ill from the group or when setting
20658  * FAILED or STANDBY on the interface.
20659  */
20660 static void
20661 illgrp_reset_schednext(ill_t *ill)
20662 {
20663 	ill_group_t *illgrp;
20664 	ill_t *save_ill;
20665 
20666 	ASSERT(IAM_WRITER_ILL(ill));
20667 	/*
20668 	 * When called from illgrp_delete, ill_group will be non-NULL.
20669 	 * But when called from ip_sioctl_flags, it could be NULL if
20670 	 * somebody is setting FAILED/INACTIVE on some interface which
20671 	 * is not part of a group.
20672 	 */
20673 	illgrp = ill->ill_group;
20674 	if (illgrp == NULL)
20675 		return;
20676 	if (illgrp->illgrp_ill_schednext != ill)
20677 		return;
20678 
20679 	illgrp->illgrp_ill_schednext = NULL;
20680 	save_ill = ill;
20681 	/*
20682 	 * Choose a good ill to be the next one for
20683 	 * outbound traffic. As the flags FAILED/STANDBY is
20684 	 * not yet marked when called from ip_sioctl_flags,
20685 	 * we check for ill separately.
20686 	 */
20687 	for (ill = illgrp->illgrp_ill; ill != NULL;
20688 	    ill = ill->ill_group_next) {
20689 		if ((ill != save_ill) &&
20690 		    !(ill->ill_phyint->phyint_flags &
20691 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20692 			illgrp->illgrp_ill_schednext = ill;
20693 			return;
20694 		}
20695 	}
20696 }
20697 
20698 /*
20699  * Given an ill, find the next ill in the group to be scheduled.
20700  * (This should be called by ip_newroute() before ire_create().)
20701  * The passed in ill may be pulled out of the group, after we have picked
20702  * up a different outgoing ill from the same group. However ire add will
20703  * atomically check this.
20704  */
20705 ill_t *
20706 illgrp_scheduler(ill_t *ill)
20707 {
20708 	ill_t *retill;
20709 	ill_group_t *illgrp;
20710 	int illcnt;
20711 	int i;
20712 	uint64_t flags;
20713 	ip_stack_t	*ipst = ill->ill_ipst;
20714 
20715 	/*
20716 	 * We don't use a lock to check for the ill_group. If this ill
20717 	 * is currently being inserted we may end up just returning this
20718 	 * ill itself. That is ok.
20719 	 */
20720 	if (ill->ill_group == NULL) {
20721 		ill_refhold(ill);
20722 		return (ill);
20723 	}
20724 
20725 	/*
20726 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20727 	 * a set of stable ills. No ill can be added or deleted or change
20728 	 * group while we hold the reader lock.
20729 	 */
20730 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20731 	if ((illgrp = ill->ill_group) == NULL) {
20732 		rw_exit(&ipst->ips_ill_g_lock);
20733 		ill_refhold(ill);
20734 		return (ill);
20735 	}
20736 
20737 	illcnt = illgrp->illgrp_ill_count;
20738 	mutex_enter(&illgrp->illgrp_lock);
20739 	retill = illgrp->illgrp_ill_schednext;
20740 
20741 	if (retill == NULL)
20742 		retill = illgrp->illgrp_ill;
20743 
20744 	/*
20745 	 * We do a circular search beginning at illgrp_ill_schednext
20746 	 * or illgrp_ill. We don't check the flags against the ill lock
20747 	 * since it can change anytime. The ire creation will be atomic
20748 	 * and will fail if the ill is FAILED or OFFLINE.
20749 	 */
20750 	for (i = 0; i < illcnt; i++) {
20751 		flags = retill->ill_phyint->phyint_flags;
20752 
20753 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20754 		    ILL_CAN_LOOKUP(retill)) {
20755 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20756 			ill_refhold(retill);
20757 			break;
20758 		}
20759 		retill = retill->ill_group_next;
20760 		if (retill == NULL)
20761 			retill = illgrp->illgrp_ill;
20762 	}
20763 	mutex_exit(&illgrp->illgrp_lock);
20764 	rw_exit(&ipst->ips_ill_g_lock);
20765 
20766 	return (i == illcnt ? NULL : retill);
20767 }
20768 
20769 /*
20770  * Checks for availbility of a usable source address (if there is one) when the
20771  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20772  * this selection is done regardless of the destination.
20773  */
20774 boolean_t
20775 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20776 {
20777 	uint_t	ifindex;
20778 	ipif_t	*ipif = NULL;
20779 	ill_t	*uill;
20780 	boolean_t isv6;
20781 	ip_stack_t	*ipst = ill->ill_ipst;
20782 
20783 	ASSERT(ill != NULL);
20784 
20785 	isv6 = ill->ill_isv6;
20786 	ifindex = ill->ill_usesrc_ifindex;
20787 	if (ifindex != 0) {
20788 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20789 		    NULL, ipst);
20790 		if (uill == NULL)
20791 			return (NULL);
20792 		mutex_enter(&uill->ill_lock);
20793 		for (ipif = uill->ill_ipif; ipif != NULL;
20794 		    ipif = ipif->ipif_next) {
20795 			if (!IPIF_CAN_LOOKUP(ipif))
20796 				continue;
20797 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20798 				continue;
20799 			if (!(ipif->ipif_flags & IPIF_UP))
20800 				continue;
20801 			if (ipif->ipif_zoneid != zoneid)
20802 				continue;
20803 			if ((isv6 &&
20804 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20805 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20806 				continue;
20807 			mutex_exit(&uill->ill_lock);
20808 			ill_refrele(uill);
20809 			return (B_TRUE);
20810 		}
20811 		mutex_exit(&uill->ill_lock);
20812 		ill_refrele(uill);
20813 	}
20814 	return (B_FALSE);
20815 }
20816 
20817 /*
20818  * Determine the best source address given a destination address and an ill.
20819  * Prefers non-deprecated over deprecated but will return a deprecated
20820  * address if there is no other choice. If there is a usable source address
20821  * on the interface pointed to by ill_usesrc_ifindex then that is given
20822  * first preference.
20823  *
20824  * Returns NULL if there is no suitable source address for the ill.
20825  * This only occurs when there is no valid source address for the ill.
20826  */
20827 ipif_t *
20828 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20829 {
20830 	ipif_t *ipif;
20831 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20832 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20833 	int index = 0;
20834 	boolean_t wrapped = B_FALSE;
20835 	boolean_t same_subnet_only = B_FALSE;
20836 	boolean_t ipif_same_found, ipif_other_found;
20837 	boolean_t specific_found;
20838 	ill_t	*till, *usill = NULL;
20839 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20840 	ip_stack_t	*ipst = ill->ill_ipst;
20841 
20842 	if (ill->ill_usesrc_ifindex != 0) {
20843 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20844 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20845 		if (usill != NULL)
20846 			ill = usill;	/* Select source from usesrc ILL */
20847 		else
20848 			return (NULL);
20849 	}
20850 
20851 	/*
20852 	 * If we're dealing with an unlabeled destination on a labeled system,
20853 	 * make sure that we ignore source addresses that are incompatible with
20854 	 * the destination's default label.  That destination's default label
20855 	 * must dominate the minimum label on the source address.
20856 	 */
20857 	dst_rhtp = NULL;
20858 	if (is_system_labeled()) {
20859 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20860 		if (dst_rhtp == NULL)
20861 			return (NULL);
20862 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20863 			TPC_RELE(dst_rhtp);
20864 			dst_rhtp = NULL;
20865 		}
20866 	}
20867 
20868 	/*
20869 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20870 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20871 	 * After selecting the right ipif, under ill_lock make sure ipif is
20872 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20873 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20874 	 * but not under a lock.
20875 	 */
20876 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20877 
20878 retry:
20879 	till = ill;
20880 	ipif_arr[0] = NULL;
20881 
20882 	if (till->ill_group != NULL)
20883 		till = till->ill_group->illgrp_ill;
20884 
20885 	/*
20886 	 * Choose one good source address from each ill across the group.
20887 	 * If possible choose a source address in the same subnet as
20888 	 * the destination address.
20889 	 *
20890 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20891 	 * This is okay because of the following.
20892 	 *
20893 	 *    If PHYI_FAILED is set and we still have non-deprecated
20894 	 *    addresses, it means the addresses have not yet been
20895 	 *    failed over to a different interface. We potentially
20896 	 *    select them to create IRE_CACHES, which will be later
20897 	 *    flushed when the addresses move over.
20898 	 *
20899 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20900 	 *    addresses, it means either the user has configured them
20901 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20902 	 *    been moved over. For the former, in.mpathd does a failover
20903 	 *    when the interface becomes INACTIVE and hence we should
20904 	 *    not find them. Once INACTIVE is set, we don't allow them
20905 	 *    to create logical interfaces anymore. For the latter, a
20906 	 *    flush will happen when INACTIVE is cleared which will
20907 	 *    flush the IRE_CACHES.
20908 	 *
20909 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20910 	 *    over soon. We potentially select them to create IRE_CACHEs,
20911 	 *    which will be later flushed when the addresses move over.
20912 	 *
20913 	 * NOTE : As ipif_select_source is called to borrow source address
20914 	 * for an ipif that is part of a group, source address selection
20915 	 * will be re-done whenever the group changes i.e either an
20916 	 * insertion/deletion in the group.
20917 	 *
20918 	 * Fill ipif_arr[] with source addresses, using these rules:
20919 	 *
20920 	 *	1. At most one source address from a given ill ends up
20921 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20922 	 *	   associated with a given ill ends up in ipif_arr[].
20923 	 *
20924 	 *	2. If there is at least one non-deprecated ipif in the
20925 	 *	   IPMP group with a source address on the same subnet as
20926 	 *	   our destination, then fill ipif_arr[] only with
20927 	 *	   source addresses on the same subnet as our destination.
20928 	 *	   Note that because of (1), only the first
20929 	 *	   non-deprecated ipif found with a source address
20930 	 *	   matching the destination ends up in ipif_arr[].
20931 	 *
20932 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20933 	 *	   addresses not in the same subnet as our destination.
20934 	 *	   Again, because of (1), only the first off-subnet source
20935 	 *	   address will be chosen.
20936 	 *
20937 	 *	4. If there are no non-deprecated ipifs, then just use
20938 	 *	   the source address associated with the last deprecated
20939 	 *	   one we find that happens to be on the same subnet,
20940 	 *	   otherwise the first one not in the same subnet.
20941 	 */
20942 	specific_found = B_FALSE;
20943 	for (; till != NULL; till = till->ill_group_next) {
20944 		ipif_same_found = B_FALSE;
20945 		ipif_other_found = B_FALSE;
20946 		for (ipif = till->ill_ipif; ipif != NULL;
20947 		    ipif = ipif->ipif_next) {
20948 			if (!IPIF_CAN_LOOKUP(ipif))
20949 				continue;
20950 			/* Always skip NOLOCAL and ANYCAST interfaces */
20951 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20952 				continue;
20953 			if (!(ipif->ipif_flags & IPIF_UP) ||
20954 			    !ipif->ipif_addr_ready)
20955 				continue;
20956 			if (ipif->ipif_zoneid != zoneid &&
20957 			    ipif->ipif_zoneid != ALL_ZONES)
20958 				continue;
20959 			/*
20960 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20961 			 * but are not valid as source addresses.
20962 			 */
20963 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20964 				continue;
20965 
20966 			/*
20967 			 * Check compatibility of local address for
20968 			 * destination's default label if we're on a labeled
20969 			 * system.  Incompatible addresses can't be used at
20970 			 * all.
20971 			 */
20972 			if (dst_rhtp != NULL) {
20973 				boolean_t incompat;
20974 
20975 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20976 				    IPV4_VERSION, B_FALSE);
20977 				if (src_rhtp == NULL)
20978 					continue;
20979 				incompat =
20980 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20981 				    src_rhtp->tpc_tp.tp_doi !=
20982 				    dst_rhtp->tpc_tp.tp_doi ||
20983 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20984 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20985 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20986 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20987 				TPC_RELE(src_rhtp);
20988 				if (incompat)
20989 					continue;
20990 			}
20991 
20992 			/*
20993 			 * We prefer not to use all all-zones addresses, if we
20994 			 * can avoid it, as they pose problems with unlabeled
20995 			 * destinations.
20996 			 */
20997 			if (ipif->ipif_zoneid != ALL_ZONES) {
20998 				if (!specific_found &&
20999 				    (!same_subnet_only ||
21000 				    (ipif->ipif_net_mask & dst) ==
21001 				    ipif->ipif_subnet)) {
21002 					index = 0;
21003 					specific_found = B_TRUE;
21004 					ipif_other_found = B_FALSE;
21005 				}
21006 			} else {
21007 				if (specific_found)
21008 					continue;
21009 			}
21010 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
21011 				if (ipif_dep == NULL ||
21012 				    (ipif->ipif_net_mask & dst) ==
21013 				    ipif->ipif_subnet)
21014 					ipif_dep = ipif;
21015 				continue;
21016 			}
21017 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
21018 				/* found a source address in the same subnet */
21019 				if (!same_subnet_only) {
21020 					same_subnet_only = B_TRUE;
21021 					index = 0;
21022 				}
21023 				ipif_same_found = B_TRUE;
21024 			} else {
21025 				if (same_subnet_only || ipif_other_found)
21026 					continue;
21027 				ipif_other_found = B_TRUE;
21028 			}
21029 			ipif_arr[index++] = ipif;
21030 			if (index == MAX_IPIF_SELECT_SOURCE) {
21031 				wrapped = B_TRUE;
21032 				index = 0;
21033 			}
21034 			if (ipif_same_found)
21035 				break;
21036 		}
21037 	}
21038 
21039 	if (ipif_arr[0] == NULL) {
21040 		ipif = ipif_dep;
21041 	} else {
21042 		if (wrapped)
21043 			index = MAX_IPIF_SELECT_SOURCE;
21044 		ipif = ipif_arr[ipif_rand(ipst) % index];
21045 		ASSERT(ipif != NULL);
21046 	}
21047 
21048 	if (ipif != NULL) {
21049 		mutex_enter(&ipif->ipif_ill->ill_lock);
21050 		if (!IPIF_CAN_LOOKUP(ipif)) {
21051 			mutex_exit(&ipif->ipif_ill->ill_lock);
21052 			goto retry;
21053 		}
21054 		ipif_refhold_locked(ipif);
21055 		mutex_exit(&ipif->ipif_ill->ill_lock);
21056 	}
21057 
21058 	rw_exit(&ipst->ips_ill_g_lock);
21059 	if (usill != NULL)
21060 		ill_refrele(usill);
21061 	if (dst_rhtp != NULL)
21062 		TPC_RELE(dst_rhtp);
21063 
21064 #ifdef DEBUG
21065 	if (ipif == NULL) {
21066 		char buf1[INET6_ADDRSTRLEN];
21067 
21068 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21069 		    ill->ill_name,
21070 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21071 	} else {
21072 		char buf1[INET6_ADDRSTRLEN];
21073 		char buf2[INET6_ADDRSTRLEN];
21074 
21075 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21076 		    ipif->ipif_ill->ill_name,
21077 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21078 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21079 		    buf2, sizeof (buf2))));
21080 	}
21081 #endif /* DEBUG */
21082 	return (ipif);
21083 }
21084 
21085 
21086 /*
21087  * If old_ipif is not NULL, see if ipif was derived from old
21088  * ipif and if so, recreate the interface route by re-doing
21089  * source address selection. This happens when ipif_down ->
21090  * ipif_update_other_ipifs calls us.
21091  *
21092  * If old_ipif is NULL, just redo the source address selection
21093  * if needed. This happens when illgrp_insert or ipif_up_done
21094  * calls us.
21095  */
21096 static void
21097 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21098 {
21099 	ire_t *ire;
21100 	ire_t *ipif_ire;
21101 	queue_t *stq;
21102 	ipif_t *nipif;
21103 	ill_t *ill;
21104 	boolean_t need_rele = B_FALSE;
21105 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21106 
21107 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21108 	ASSERT(IAM_WRITER_IPIF(ipif));
21109 
21110 	ill = ipif->ipif_ill;
21111 	if (!(ipif->ipif_flags &
21112 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21113 		/*
21114 		 * Can't possibly have borrowed the source
21115 		 * from old_ipif.
21116 		 */
21117 		return;
21118 	}
21119 
21120 	/*
21121 	 * Is there any work to be done? No work if the address
21122 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21123 	 * ipif_select_source() does not borrow addresses from
21124 	 * NOLOCAL and ANYCAST interfaces).
21125 	 */
21126 	if ((old_ipif != NULL) &&
21127 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21128 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21129 	    (old_ipif->ipif_flags &
21130 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21131 		return;
21132 	}
21133 
21134 	/*
21135 	 * Perform the same checks as when creating the
21136 	 * IRE_INTERFACE in ipif_up_done.
21137 	 */
21138 	if (!(ipif->ipif_flags & IPIF_UP))
21139 		return;
21140 
21141 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21142 	    (ipif->ipif_subnet == INADDR_ANY))
21143 		return;
21144 
21145 	ipif_ire = ipif_to_ire(ipif);
21146 	if (ipif_ire == NULL)
21147 		return;
21148 
21149 	/*
21150 	 * We know that ipif uses some other source for its
21151 	 * IRE_INTERFACE. Is it using the source of this
21152 	 * old_ipif?
21153 	 */
21154 	if (old_ipif != NULL &&
21155 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21156 		ire_refrele(ipif_ire);
21157 		return;
21158 	}
21159 	if (ip_debug > 2) {
21160 		/* ip1dbg */
21161 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21162 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21163 	}
21164 
21165 	stq = ipif_ire->ire_stq;
21166 
21167 	/*
21168 	 * Can't use our source address. Select a different
21169 	 * source address for the IRE_INTERFACE.
21170 	 */
21171 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21172 	if (nipif == NULL) {
21173 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21174 		nipif = ipif;
21175 	} else {
21176 		need_rele = B_TRUE;
21177 	}
21178 
21179 	ire = ire_create(
21180 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21181 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21182 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21183 	    NULL,				/* no gateway */
21184 	    &ipif->ipif_mtu,			/* max frag */
21185 	    NULL,				/* no src nce */
21186 	    NULL,				/* no recv from queue */
21187 	    stq,				/* send-to queue */
21188 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21189 	    ipif,
21190 	    0,
21191 	    0,
21192 	    0,
21193 	    0,
21194 	    &ire_uinfo_null,
21195 	    NULL,
21196 	    NULL,
21197 	    ipst);
21198 
21199 	if (ire != NULL) {
21200 		ire_t *ret_ire;
21201 		int error;
21202 
21203 		/*
21204 		 * We don't need ipif_ire anymore. We need to delete
21205 		 * before we add so that ire_add does not detect
21206 		 * duplicates.
21207 		 */
21208 		ire_delete(ipif_ire);
21209 		ret_ire = ire;
21210 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21211 		ASSERT(error == 0);
21212 		ASSERT(ire == ret_ire);
21213 		/* Held in ire_add */
21214 		ire_refrele(ret_ire);
21215 	}
21216 	/*
21217 	 * Either we are falling through from above or could not
21218 	 * allocate a replacement.
21219 	 */
21220 	ire_refrele(ipif_ire);
21221 	if (need_rele)
21222 		ipif_refrele(nipif);
21223 }
21224 
21225 /*
21226  * This old_ipif is going away.
21227  *
21228  * Determine if any other ipif's is using our address as
21229  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21230  * IPIF_DEPRECATED).
21231  * Find the IRE_INTERFACE for such ipifs and recreate them
21232  * to use an different source address following the rules in
21233  * ipif_up_done.
21234  *
21235  * This function takes an illgrp as an argument so that illgrp_delete
21236  * can call this to update source address even after deleting the
21237  * old_ipif->ipif_ill from the ill group.
21238  */
21239 static void
21240 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21241 {
21242 	ipif_t *ipif;
21243 	ill_t *ill;
21244 	char	buf[INET6_ADDRSTRLEN];
21245 
21246 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21247 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21248 
21249 	ill = old_ipif->ipif_ill;
21250 
21251 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21252 	    ill->ill_name,
21253 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21254 	    buf, sizeof (buf))));
21255 	/*
21256 	 * If this part of a group, look at all ills as ipif_select_source
21257 	 * borrows source address across all the ills in the group.
21258 	 */
21259 	if (illgrp != NULL)
21260 		ill = illgrp->illgrp_ill;
21261 
21262 	for (; ill != NULL; ill = ill->ill_group_next) {
21263 		for (ipif = ill->ill_ipif; ipif != NULL;
21264 		    ipif = ipif->ipif_next) {
21265 
21266 			if (ipif == old_ipif)
21267 				continue;
21268 
21269 			ipif_recreate_interface_routes(old_ipif, ipif);
21270 		}
21271 	}
21272 }
21273 
21274 /* ARGSUSED */
21275 int
21276 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21277 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21278 {
21279 	/*
21280 	 * ill_phyint_reinit merged the v4 and v6 into a single
21281 	 * ipsq. Could also have become part of a ipmp group in the
21282 	 * process, and we might not have been able to complete the
21283 	 * operation in ipif_set_values, if we could not become
21284 	 * exclusive.  If so restart it here.
21285 	 */
21286 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21287 }
21288 
21289 /*
21290  * Can operate on either a module or a driver queue.
21291  * Returns an error if not a module queue.
21292  */
21293 /* ARGSUSED */
21294 int
21295 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21296     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21297 {
21298 	queue_t		*q1 = q;
21299 	char 		*cp;
21300 	char		interf_name[LIFNAMSIZ];
21301 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21302 
21303 	if (q->q_next == NULL) {
21304 		ip1dbg((
21305 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21306 		return (EINVAL);
21307 	}
21308 
21309 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21310 		return (EALREADY);
21311 
21312 	do {
21313 		q1 = q1->q_next;
21314 	} while (q1->q_next);
21315 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21316 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21317 
21318 	/*
21319 	 * Here we are not going to delay the ioack until after
21320 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21321 	 * original ioctl message before sending the requests.
21322 	 */
21323 	return (ipif_set_values(q, mp, interf_name, &ppa));
21324 }
21325 
21326 /* ARGSUSED */
21327 int
21328 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21329     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21330 {
21331 	return (ENXIO);
21332 }
21333 
21334 /*
21335  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21336  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21337  * ire_check_and_create_bcast()).
21338  */
21339 static ire_t **
21340 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21341 {
21342 	ipaddr_t addr;
21343 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21344 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21345 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21346 
21347 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21348 
21349 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21350 
21351 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21352 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21353 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21354 
21355 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21356 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21357 
21358 	/*
21359 	 * For backward compatibility, we create net broadcast IREs based on
21360 	 * the old "IP address class system", since some old machines only
21361 	 * respond to these class derived net broadcast.  However, we must not
21362 	 * create these net broadcast IREs if the subnetmask is shorter than
21363 	 * the IP address class based derived netmask.  Otherwise, we may
21364 	 * create a net broadcast address which is the same as an IP address
21365 	 * on the subnet -- and then TCP will refuse to talk to that address.
21366 	 */
21367 	if (netmask < subnetmask) {
21368 		addr = netmask & ipif->ipif_subnet;
21369 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21370 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21371 		    flags);
21372 	}
21373 
21374 	/*
21375 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21376 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21377 	 * created.  Creating these broadcast IREs will only create confusion
21378 	 * as `addr' will be the same as the IP address.
21379 	 */
21380 	if (subnetmask != 0xFFFFFFFF) {
21381 		addr = ipif->ipif_subnet;
21382 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21383 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21384 		    irep, flags);
21385 	}
21386 
21387 	return (irep);
21388 }
21389 
21390 /*
21391  * Broadcast IRE info structure used in the functions below.  Since we
21392  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21393  */
21394 typedef struct bcast_ireinfo {
21395 	uchar_t		bi_type;	/* BCAST_* value from below */
21396 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21397 			bi_needrep:1,	/* do we need to replace it? */
21398 			bi_haverep:1,	/* have we replaced it? */
21399 			bi_pad:5;
21400 	ipaddr_t	bi_addr;	/* IRE address */
21401 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21402 } bcast_ireinfo_t;
21403 
21404 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21405 
21406 /*
21407  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21408  * return B_TRUE if it should immediately be used to recreate the IRE.
21409  */
21410 static boolean_t
21411 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21412 {
21413 	ipaddr_t addr;
21414 
21415 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21416 
21417 	switch (bireinfop->bi_type) {
21418 	case BCAST_NET:
21419 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21420 		if (addr != bireinfop->bi_addr)
21421 			return (B_FALSE);
21422 		break;
21423 	case BCAST_SUBNET:
21424 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21425 			return (B_FALSE);
21426 		break;
21427 	}
21428 
21429 	bireinfop->bi_needrep = 1;
21430 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21431 		if (bireinfop->bi_backup == NULL)
21432 			bireinfop->bi_backup = ipif;
21433 		return (B_FALSE);
21434 	}
21435 	return (B_TRUE);
21436 }
21437 
21438 /*
21439  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21440  * them ala ire_check_and_create_bcast().
21441  */
21442 static ire_t **
21443 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21444 {
21445 	ipaddr_t mask, addr;
21446 
21447 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21448 
21449 	addr = bireinfop->bi_addr;
21450 	irep = ire_create_bcast(ipif, addr, irep);
21451 
21452 	switch (bireinfop->bi_type) {
21453 	case BCAST_NET:
21454 		mask = ip_net_mask(ipif->ipif_subnet);
21455 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21456 		break;
21457 	case BCAST_SUBNET:
21458 		mask = ipif->ipif_net_mask;
21459 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21460 		break;
21461 	}
21462 
21463 	bireinfop->bi_haverep = 1;
21464 	return (irep);
21465 }
21466 
21467 /*
21468  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21469  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21470  * that are going away are still needed.  If so, have ipif_create_bcast()
21471  * recreate them (except for the deprecated case, as explained below).
21472  */
21473 static ire_t **
21474 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21475     ire_t **irep)
21476 {
21477 	int i;
21478 	ipif_t *ipif;
21479 
21480 	ASSERT(!ill->ill_isv6);
21481 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21482 		/*
21483 		 * Skip this ipif if it's (a) the one being taken down, (b)
21484 		 * not in the same zone, or (c) has no valid local address.
21485 		 */
21486 		if (ipif == test_ipif ||
21487 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21488 		    ipif->ipif_subnet == 0 ||
21489 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21490 		    (IPIF_UP|IPIF_BROADCAST))
21491 			continue;
21492 
21493 		/*
21494 		 * For each dying IRE that hasn't yet been replaced, see if
21495 		 * `ipif' needs it and whether the IRE should be recreated on
21496 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21497 		 * will return B_FALSE even if `ipif' needs the IRE on the
21498 		 * hopes that we'll later find a needy non-deprecated ipif.
21499 		 * However, the ipif is recorded in bi_backup for possible
21500 		 * subsequent use by ipif_check_bcast_ires().
21501 		 */
21502 		for (i = 0; i < BCAST_COUNT; i++) {
21503 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21504 				continue;
21505 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21506 				continue;
21507 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21508 		}
21509 
21510 		/*
21511 		 * If we've replaced all of the broadcast IREs that are going
21512 		 * to be taken down, we know we're done.
21513 		 */
21514 		for (i = 0; i < BCAST_COUNT; i++) {
21515 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21516 				break;
21517 		}
21518 		if (i == BCAST_COUNT)
21519 			break;
21520 	}
21521 	return (irep);
21522 }
21523 
21524 /*
21525  * Check if `test_ipif' (which is going away) is associated with any existing
21526  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21527  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21528  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21529  *
21530  * This is necessary because broadcast IREs are shared.  In particular, a
21531  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21532  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21533  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21534  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21535  * same zone, they will share the same set of broadcast IREs.
21536  *
21537  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21538  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21539  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21540  */
21541 static void
21542 ipif_check_bcast_ires(ipif_t *test_ipif)
21543 {
21544 	ill_t		*ill = test_ipif->ipif_ill;
21545 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21546 	ire_t		**irep1, **irep = &ire_array[0];
21547 	uint_t 		i, willdie;
21548 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21549 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21550 
21551 	ASSERT(!test_ipif->ipif_isv6);
21552 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21553 
21554 	/*
21555 	 * No broadcast IREs for the LOOPBACK interface
21556 	 * or others such as point to point and IPIF_NOXMIT.
21557 	 */
21558 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21559 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21560 		return;
21561 
21562 	bzero(bireinfo, sizeof (bireinfo));
21563 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21564 	bireinfo[0].bi_addr = 0;
21565 
21566 	bireinfo[1].bi_type = BCAST_ALLONES;
21567 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21568 
21569 	bireinfo[2].bi_type = BCAST_NET;
21570 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21571 
21572 	if (test_ipif->ipif_net_mask != 0)
21573 		mask = test_ipif->ipif_net_mask;
21574 	bireinfo[3].bi_type = BCAST_SUBNET;
21575 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21576 
21577 	/*
21578 	 * Figure out what (if any) broadcast IREs will die as a result of
21579 	 * `test_ipif' going away.  If none will die, we're done.
21580 	 */
21581 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21582 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21583 		    test_ipif, ALL_ZONES, NULL,
21584 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21585 		if (ire != NULL) {
21586 			willdie++;
21587 			bireinfo[i].bi_willdie = 1;
21588 			ire_refrele(ire);
21589 		}
21590 	}
21591 
21592 	if (willdie == 0)
21593 		return;
21594 
21595 	/*
21596 	 * Walk through all the ipifs that will be affected by the dying IREs,
21597 	 * and recreate the IREs as necessary.
21598 	 */
21599 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21600 
21601 	/*
21602 	 * Scan through the set of broadcast IREs and see if there are any
21603 	 * that we need to replace that have not yet been replaced.  If so,
21604 	 * replace them using the appropriate backup ipif.
21605 	 */
21606 	for (i = 0; i < BCAST_COUNT; i++) {
21607 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21608 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21609 			    &bireinfo[i], irep);
21610 	}
21611 
21612 	/*
21613 	 * If we can't create all of them, don't add any of them.  (Code in
21614 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21615 	 * non-loopback copy and loopback copy for a given address.)
21616 	 */
21617 	for (irep1 = irep; irep1 > ire_array; ) {
21618 		irep1--;
21619 		if (*irep1 == NULL) {
21620 			ip0dbg(("ipif_check_bcast_ires: can't create "
21621 			    "IRE_BROADCAST, memory allocation failure\n"));
21622 			while (irep > ire_array) {
21623 				irep--;
21624 				if (*irep != NULL)
21625 					ire_delete(*irep);
21626 			}
21627 			return;
21628 		}
21629 	}
21630 
21631 	for (irep1 = irep; irep1 > ire_array; ) {
21632 		irep1--;
21633 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21634 			ire_refrele(*irep1);		/* Held in ire_add */
21635 	}
21636 }
21637 
21638 /*
21639  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21640  * from lifr_flags and the name from lifr_name.
21641  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21642  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21643  * Returns EINPROGRESS when mp has been consumed by queueing it on
21644  * ill_pending_mp and the ioctl will complete in ip_rput.
21645  *
21646  * Can operate on either a module or a driver queue.
21647  * Returns an error if not a module queue.
21648  */
21649 /* ARGSUSED */
21650 int
21651 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21652     ip_ioctl_cmd_t *ipip, void *if_req)
21653 {
21654 	ill_t	*ill = q->q_ptr;
21655 	phyint_t *phyi;
21656 	ip_stack_t *ipst;
21657 	struct lifreq *lifr = if_req;
21658 
21659 	ASSERT(ipif != NULL);
21660 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21661 
21662 	if (q->q_next == NULL) {
21663 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21664 		return (EINVAL);
21665 	}
21666 
21667 	/*
21668 	 * If we are not writer on 'q' then this interface exists already
21669 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21670 	 * so return EALREADY.
21671 	 */
21672 	if (ill != ipif->ipif_ill)
21673 		return (EALREADY);
21674 
21675 	if (ill->ill_name[0] != '\0')
21676 		return (EALREADY);
21677 
21678 	/*
21679 	 * Set all the flags. Allows all kinds of override. Provide some
21680 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21681 	 * unless there is either multicast/broadcast support in the driver
21682 	 * or it is a pt-pt link.
21683 	 */
21684 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21685 		/* Meaningless to IP thus don't allow them to be set. */
21686 		ip1dbg(("ip_setname: EINVAL 1\n"));
21687 		return (EINVAL);
21688 	}
21689 
21690 	/*
21691 	 * If there's another ill already with the requested name, ensure
21692 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21693 	 * fuse together two unrelated ills, which will cause chaos.
21694 	 */
21695 	ipst = ill->ill_ipst;
21696 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21697 	    lifr->lifr_name, NULL);
21698 	if (phyi != NULL) {
21699 		ill_t *ill_mate = phyi->phyint_illv4;
21700 
21701 		if (ill_mate == NULL)
21702 			ill_mate = phyi->phyint_illv6;
21703 		ASSERT(ill_mate != NULL);
21704 
21705 		if (ill_mate->ill_media->ip_m_mac_type !=
21706 		    ill->ill_media->ip_m_mac_type) {
21707 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21708 			    "use the same ill name on differing media\n"));
21709 			return (EINVAL);
21710 		}
21711 	}
21712 
21713 	/*
21714 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21715 	 * ill_bcast_addr_length info.
21716 	 */
21717 	if (!ill->ill_needs_attach &&
21718 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21719 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21720 	    ill->ill_bcast_addr_length == 0)) {
21721 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21722 		ip1dbg(("ip_setname: EINVAL 2\n"));
21723 		return (EINVAL);
21724 	}
21725 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21726 	    ((lifr->lifr_flags & IFF_IPV6) ||
21727 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21728 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21729 		ip1dbg(("ip_setname: EINVAL 3\n"));
21730 		return (EINVAL);
21731 	}
21732 	if (lifr->lifr_flags & IFF_UP) {
21733 		/* Can only be set with SIOCSLIFFLAGS */
21734 		ip1dbg(("ip_setname: EINVAL 4\n"));
21735 		return (EINVAL);
21736 	}
21737 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21738 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21739 		ip1dbg(("ip_setname: EINVAL 5\n"));
21740 		return (EINVAL);
21741 	}
21742 	/*
21743 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21744 	 */
21745 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21746 	    !(lifr->lifr_flags & IFF_IPV6) &&
21747 	    !(ipif->ipif_isv6)) {
21748 		ip1dbg(("ip_setname: EINVAL 6\n"));
21749 		return (EINVAL);
21750 	}
21751 
21752 	/*
21753 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21754 	 * we have all the flags here. So, we assign rather than we OR.
21755 	 * We can't OR the flags here because we don't want to set
21756 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21757 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21758 	 * on lifr_flags value here.
21759 	 */
21760 	/*
21761 	 * This ill has not been inserted into the global list.
21762 	 * So we are still single threaded and don't need any lock
21763 	 */
21764 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21765 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21766 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21767 
21768 	/* We started off as V4. */
21769 	if (ill->ill_flags & ILLF_IPV6) {
21770 		ill->ill_phyint->phyint_illv6 = ill;
21771 		ill->ill_phyint->phyint_illv4 = NULL;
21772 	}
21773 
21774 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21775 }
21776 
21777 /* ARGSUSED */
21778 int
21779 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21780     ip_ioctl_cmd_t *ipip, void *if_req)
21781 {
21782 	/*
21783 	 * ill_phyint_reinit merged the v4 and v6 into a single
21784 	 * ipsq. Could also have become part of a ipmp group in the
21785 	 * process, and we might not have been able to complete the
21786 	 * slifname in ipif_set_values, if we could not become
21787 	 * exclusive.  If so restart it here
21788 	 */
21789 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21790 }
21791 
21792 /*
21793  * Return a pointer to the ipif which matches the index, IP version type and
21794  * zoneid.
21795  */
21796 ipif_t *
21797 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21798     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21799 {
21800 	ill_t	*ill;
21801 	ipif_t	*ipif = NULL;
21802 
21803 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21804 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21805 
21806 	if (err != NULL)
21807 		*err = 0;
21808 
21809 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21810 	if (ill != NULL) {
21811 		mutex_enter(&ill->ill_lock);
21812 		for (ipif = ill->ill_ipif; ipif != NULL;
21813 		    ipif = ipif->ipif_next) {
21814 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21815 			    zoneid == ipif->ipif_zoneid ||
21816 			    ipif->ipif_zoneid == ALL_ZONES)) {
21817 				ipif_refhold_locked(ipif);
21818 				break;
21819 			}
21820 		}
21821 		mutex_exit(&ill->ill_lock);
21822 		ill_refrele(ill);
21823 		if (ipif == NULL && err != NULL)
21824 			*err = ENXIO;
21825 	}
21826 	return (ipif);
21827 }
21828 
21829 typedef struct conn_change_s {
21830 	uint_t cc_old_ifindex;
21831 	uint_t cc_new_ifindex;
21832 } conn_change_t;
21833 
21834 /*
21835  * ipcl_walk function for changing interface index.
21836  */
21837 static void
21838 conn_change_ifindex(conn_t *connp, caddr_t arg)
21839 {
21840 	conn_change_t *connc;
21841 	uint_t old_ifindex;
21842 	uint_t new_ifindex;
21843 	int i;
21844 	ilg_t *ilg;
21845 
21846 	connc = (conn_change_t *)arg;
21847 	old_ifindex = connc->cc_old_ifindex;
21848 	new_ifindex = connc->cc_new_ifindex;
21849 
21850 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21851 		connp->conn_orig_bound_ifindex = new_ifindex;
21852 
21853 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21854 		connp->conn_orig_multicast_ifindex = new_ifindex;
21855 
21856 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21857 		ilg = &connp->conn_ilg[i];
21858 		if (ilg->ilg_orig_ifindex == old_ifindex)
21859 			ilg->ilg_orig_ifindex = new_ifindex;
21860 	}
21861 }
21862 
21863 /*
21864  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21865  * to new_index if it matches the old_index.
21866  *
21867  * Failovers typically happen within a group of ills. But somebody
21868  * can remove an ill from the group after a failover happened. If
21869  * we are setting the ifindex after this, we potentially need to
21870  * look at all the ills rather than just the ones in the group.
21871  * We cut down the work by looking at matching ill_net_types
21872  * and ill_types as we could not possibly grouped them together.
21873  */
21874 static void
21875 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21876 {
21877 	ill_t *ill;
21878 	ipif_t *ipif;
21879 	uint_t old_ifindex;
21880 	uint_t new_ifindex;
21881 	ilm_t *ilm;
21882 	ill_walk_context_t ctx;
21883 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21884 
21885 	old_ifindex = connc->cc_old_ifindex;
21886 	new_ifindex = connc->cc_new_ifindex;
21887 
21888 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21889 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21890 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21891 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21892 		    (ill_orig->ill_type != ill->ill_type)) {
21893 			continue;
21894 		}
21895 		for (ipif = ill->ill_ipif; ipif != NULL;
21896 		    ipif = ipif->ipif_next) {
21897 			if (ipif->ipif_orig_ifindex == old_ifindex)
21898 				ipif->ipif_orig_ifindex = new_ifindex;
21899 		}
21900 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21901 			if (ilm->ilm_orig_ifindex == old_ifindex)
21902 				ilm->ilm_orig_ifindex = new_ifindex;
21903 		}
21904 	}
21905 	rw_exit(&ipst->ips_ill_g_lock);
21906 }
21907 
21908 /*
21909  * We first need to ensure that the new index is unique, and
21910  * then carry the change across both v4 and v6 ill representation
21911  * of the physical interface.
21912  */
21913 /* ARGSUSED */
21914 int
21915 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21916     ip_ioctl_cmd_t *ipip, void *ifreq)
21917 {
21918 	ill_t		*ill;
21919 	ill_t		*ill_other;
21920 	phyint_t	*phyi;
21921 	int		old_index;
21922 	conn_change_t	connc;
21923 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21924 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21925 	uint_t	index;
21926 	ill_t	*ill_v4;
21927 	ill_t	*ill_v6;
21928 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21929 
21930 	if (ipip->ipi_cmd_type == IF_CMD)
21931 		index = ifr->ifr_index;
21932 	else
21933 		index = lifr->lifr_index;
21934 
21935 	/*
21936 	 * Only allow on physical interface. Also, index zero is illegal.
21937 	 *
21938 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21939 	 *
21940 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21941 	 *    implies a possible failback might have to happen. As failback
21942 	 *    depends on the old index, we should fail setting the index.
21943 	 *
21944 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21945 	 *    any addresses or multicast memberships are failed over to
21946 	 *    a non-STANDBY interface. As failback depends on the old
21947 	 *    index, we should fail setting the index for this case also.
21948 	 *
21949 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21950 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21951 	 */
21952 	ill = ipif->ipif_ill;
21953 	phyi = ill->ill_phyint;
21954 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21955 	    ipif->ipif_id != 0 || index == 0) {
21956 		return (EINVAL);
21957 	}
21958 	old_index = phyi->phyint_ifindex;
21959 
21960 	/* If the index is not changing, no work to do */
21961 	if (old_index == index)
21962 		return (0);
21963 
21964 	/*
21965 	 * Use ill_lookup_on_ifindex to determine if the
21966 	 * new index is unused and if so allow the change.
21967 	 */
21968 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21969 	    ipst);
21970 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21971 	    ipst);
21972 	if (ill_v6 != NULL || ill_v4 != NULL) {
21973 		if (ill_v4 != NULL)
21974 			ill_refrele(ill_v4);
21975 		if (ill_v6 != NULL)
21976 			ill_refrele(ill_v6);
21977 		return (EBUSY);
21978 	}
21979 
21980 	/*
21981 	 * The new index is unused. Set it in the phyint.
21982 	 * Locate the other ill so that we can send a routing
21983 	 * sockets message.
21984 	 */
21985 	if (ill->ill_isv6) {
21986 		ill_other = phyi->phyint_illv4;
21987 	} else {
21988 		ill_other = phyi->phyint_illv6;
21989 	}
21990 
21991 	phyi->phyint_ifindex = index;
21992 
21993 	/* Update SCTP's ILL list */
21994 	sctp_ill_reindex(ill, old_index);
21995 
21996 	connc.cc_old_ifindex = old_index;
21997 	connc.cc_new_ifindex = index;
21998 	ip_change_ifindex(ill, &connc);
21999 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
22000 
22001 	/* Send the routing sockets message */
22002 	ip_rts_ifmsg(ipif);
22003 	if (ill_other != NULL)
22004 		ip_rts_ifmsg(ill_other->ill_ipif);
22005 
22006 	return (0);
22007 }
22008 
22009 /* ARGSUSED */
22010 int
22011 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22012     ip_ioctl_cmd_t *ipip, void *ifreq)
22013 {
22014 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22015 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22016 
22017 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
22018 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22019 	/* Get the interface index */
22020 	if (ipip->ipi_cmd_type == IF_CMD) {
22021 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22022 	} else {
22023 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22024 	}
22025 	return (0);
22026 }
22027 
22028 /* ARGSUSED */
22029 int
22030 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22031     ip_ioctl_cmd_t *ipip, void *ifreq)
22032 {
22033 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22034 
22035 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
22036 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22037 	/* Get the interface zone */
22038 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22039 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22040 	return (0);
22041 }
22042 
22043 /*
22044  * Set the zoneid of an interface.
22045  */
22046 /* ARGSUSED */
22047 int
22048 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22049     ip_ioctl_cmd_t *ipip, void *ifreq)
22050 {
22051 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22052 	int err = 0;
22053 	boolean_t need_up = B_FALSE;
22054 	zone_t *zptr;
22055 	zone_status_t status;
22056 	zoneid_t zoneid;
22057 
22058 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22059 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22060 		if (!is_system_labeled())
22061 			return (ENOTSUP);
22062 		zoneid = GLOBAL_ZONEID;
22063 	}
22064 
22065 	/* cannot assign instance zero to a non-global zone */
22066 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22067 		return (ENOTSUP);
22068 
22069 	/*
22070 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22071 	 * the event of a race with the zone shutdown processing, since IP
22072 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22073 	 * interface will be cleaned up even if the zone is shut down
22074 	 * immediately after the status check. If the interface can't be brought
22075 	 * down right away, and the zone is shut down before the restart
22076 	 * function is called, we resolve the possible races by rechecking the
22077 	 * zone status in the restart function.
22078 	 */
22079 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22080 		return (EINVAL);
22081 	status = zone_status_get(zptr);
22082 	zone_rele(zptr);
22083 
22084 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22085 		return (EINVAL);
22086 
22087 	if (ipif->ipif_flags & IPIF_UP) {
22088 		/*
22089 		 * If the interface is already marked up,
22090 		 * we call ipif_down which will take care
22091 		 * of ditching any IREs that have been set
22092 		 * up based on the old interface address.
22093 		 */
22094 		err = ipif_logical_down(ipif, q, mp);
22095 		if (err == EINPROGRESS)
22096 			return (err);
22097 		ipif_down_tail(ipif);
22098 		need_up = B_TRUE;
22099 	}
22100 
22101 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22102 	return (err);
22103 }
22104 
22105 static int
22106 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22107     queue_t *q, mblk_t *mp, boolean_t need_up)
22108 {
22109 	int	err = 0;
22110 	ip_stack_t	*ipst;
22111 
22112 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22113 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22114 
22115 	if (CONN_Q(q))
22116 		ipst = CONNQ_TO_IPST(q);
22117 	else
22118 		ipst = ILLQ_TO_IPST(q);
22119 
22120 	/*
22121 	 * For exclusive stacks we don't allow a different zoneid than
22122 	 * global.
22123 	 */
22124 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22125 	    zoneid != GLOBAL_ZONEID)
22126 		return (EINVAL);
22127 
22128 	/* Set the new zone id. */
22129 	ipif->ipif_zoneid = zoneid;
22130 
22131 	/* Update sctp list */
22132 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22133 
22134 	if (need_up) {
22135 		/*
22136 		 * Now bring the interface back up.  If this
22137 		 * is the only IPIF for the ILL, ipif_up
22138 		 * will have to re-bind to the device, so
22139 		 * we may get back EINPROGRESS, in which
22140 		 * case, this IOCTL will get completed in
22141 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22142 		 */
22143 		err = ipif_up(ipif, q, mp);
22144 	}
22145 	return (err);
22146 }
22147 
22148 /* ARGSUSED */
22149 int
22150 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22151     ip_ioctl_cmd_t *ipip, void *if_req)
22152 {
22153 	struct lifreq *lifr = (struct lifreq *)if_req;
22154 	zoneid_t zoneid;
22155 	zone_t *zptr;
22156 	zone_status_t status;
22157 
22158 	ASSERT(ipif->ipif_id != 0);
22159 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22160 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22161 		zoneid = GLOBAL_ZONEID;
22162 
22163 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22164 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22165 
22166 	/*
22167 	 * We recheck the zone status to resolve the following race condition:
22168 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22169 	 * 2) hme0:1 is up and can't be brought down right away;
22170 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22171 	 * 3) zone "myzone" is halted; the zone status switches to
22172 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22173 	 * the interfaces to remove - hme0:1 is not returned because it's not
22174 	 * yet in "myzone", so it won't be removed;
22175 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22176 	 * status check here, we would have hme0:1 in "myzone" after it's been
22177 	 * destroyed.
22178 	 * Note that if the status check fails, we need to bring the interface
22179 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22180 	 * ipif_up_done[_v6]().
22181 	 */
22182 	status = ZONE_IS_UNINITIALIZED;
22183 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22184 		status = zone_status_get(zptr);
22185 		zone_rele(zptr);
22186 	}
22187 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22188 		if (ipif->ipif_isv6) {
22189 			(void) ipif_up_done_v6(ipif);
22190 		} else {
22191 			(void) ipif_up_done(ipif);
22192 		}
22193 		return (EINVAL);
22194 	}
22195 
22196 	ipif_down_tail(ipif);
22197 
22198 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22199 	    B_TRUE));
22200 }
22201 
22202 /* ARGSUSED */
22203 int
22204 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22205 	ip_ioctl_cmd_t *ipip, void *ifreq)
22206 {
22207 	struct lifreq	*lifr = ifreq;
22208 
22209 	ASSERT(q->q_next == NULL);
22210 	ASSERT(CONN_Q(q));
22211 
22212 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22213 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22214 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22215 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22216 
22217 	return (0);
22218 }
22219 
22220 /* Find the previous ILL in this usesrc group */
22221 static ill_t *
22222 ill_prev_usesrc(ill_t *uill)
22223 {
22224 	ill_t *ill;
22225 
22226 	for (ill = uill->ill_usesrc_grp_next;
22227 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22228 	    ill = ill->ill_usesrc_grp_next)
22229 		/* do nothing */;
22230 	return (ill);
22231 }
22232 
22233 /*
22234  * Release all members of the usesrc group. This routine is called
22235  * from ill_delete when the interface being unplumbed is the
22236  * group head.
22237  */
22238 static void
22239 ill_disband_usesrc_group(ill_t *uill)
22240 {
22241 	ill_t *next_ill, *tmp_ill;
22242 	ip_stack_t	*ipst = uill->ill_ipst;
22243 
22244 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22245 	next_ill = uill->ill_usesrc_grp_next;
22246 
22247 	do {
22248 		ASSERT(next_ill != NULL);
22249 		tmp_ill = next_ill->ill_usesrc_grp_next;
22250 		ASSERT(tmp_ill != NULL);
22251 		next_ill->ill_usesrc_grp_next = NULL;
22252 		next_ill->ill_usesrc_ifindex = 0;
22253 		next_ill = tmp_ill;
22254 	} while (next_ill->ill_usesrc_ifindex != 0);
22255 	uill->ill_usesrc_grp_next = NULL;
22256 }
22257 
22258 /*
22259  * Remove the client usesrc ILL from the list and relink to a new list
22260  */
22261 int
22262 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22263 {
22264 	ill_t *ill, *tmp_ill;
22265 	ip_stack_t	*ipst = ucill->ill_ipst;
22266 
22267 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22268 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22269 
22270 	/*
22271 	 * Check if the usesrc client ILL passed in is not already
22272 	 * in use as a usesrc ILL i.e one whose source address is
22273 	 * in use OR a usesrc ILL is not already in use as a usesrc
22274 	 * client ILL
22275 	 */
22276 	if ((ucill->ill_usesrc_ifindex == 0) ||
22277 	    (uill->ill_usesrc_ifindex != 0)) {
22278 		return (-1);
22279 	}
22280 
22281 	ill = ill_prev_usesrc(ucill);
22282 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22283 
22284 	/* Remove from the current list */
22285 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22286 		/* Only two elements in the list */
22287 		ASSERT(ill->ill_usesrc_ifindex == 0);
22288 		ill->ill_usesrc_grp_next = NULL;
22289 	} else {
22290 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22291 	}
22292 
22293 	if (ifindex == 0) {
22294 		ucill->ill_usesrc_ifindex = 0;
22295 		ucill->ill_usesrc_grp_next = NULL;
22296 		return (0);
22297 	}
22298 
22299 	ucill->ill_usesrc_ifindex = ifindex;
22300 	tmp_ill = uill->ill_usesrc_grp_next;
22301 	uill->ill_usesrc_grp_next = ucill;
22302 	ucill->ill_usesrc_grp_next =
22303 	    (tmp_ill != NULL) ? tmp_ill : uill;
22304 	return (0);
22305 }
22306 
22307 /*
22308  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22309  * ip.c for locking details.
22310  */
22311 /* ARGSUSED */
22312 int
22313 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22314     ip_ioctl_cmd_t *ipip, void *ifreq)
22315 {
22316 	struct lifreq *lifr = (struct lifreq *)ifreq;
22317 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22318 	    ill_flag_changed = B_FALSE;
22319 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22320 	int err = 0, ret;
22321 	uint_t ifindex;
22322 	phyint_t *us_phyint, *us_cli_phyint;
22323 	ipsq_t *ipsq = NULL;
22324 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22325 
22326 	ASSERT(IAM_WRITER_IPIF(ipif));
22327 	ASSERT(q->q_next == NULL);
22328 	ASSERT(CONN_Q(q));
22329 
22330 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22331 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22332 
22333 	ASSERT(us_cli_phyint != NULL);
22334 
22335 	/*
22336 	 * If the client ILL is being used for IPMP, abort.
22337 	 * Note, this can be done before ipsq_try_enter since we are already
22338 	 * exclusive on this ILL
22339 	 */
22340 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22341 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22342 		return (EINVAL);
22343 	}
22344 
22345 	ifindex = lifr->lifr_index;
22346 	if (ifindex == 0) {
22347 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22348 			/* non usesrc group interface, nothing to reset */
22349 			return (0);
22350 		}
22351 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22352 		/* valid reset request */
22353 		reset_flg = B_TRUE;
22354 	}
22355 
22356 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22357 	    ip_process_ioctl, &err, ipst);
22358 
22359 	if (usesrc_ill == NULL) {
22360 		return (err);
22361 	}
22362 
22363 	/*
22364 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22365 	 * group nor can either of the interfaces be used for standy. So
22366 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22367 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22368 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22369 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22370 	 * the usesrc_cli_ill
22371 	 */
22372 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22373 	    NEW_OP, B_TRUE);
22374 	if (ipsq == NULL) {
22375 		err = EINPROGRESS;
22376 		/* Operation enqueued on the ipsq of the usesrc ILL */
22377 		goto done;
22378 	}
22379 
22380 	/* Check if the usesrc_ill is used for IPMP */
22381 	us_phyint = usesrc_ill->ill_phyint;
22382 	if ((us_phyint->phyint_groupname != NULL) ||
22383 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22384 		err = EINVAL;
22385 		goto done;
22386 	}
22387 
22388 	/*
22389 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22390 	 * already a client then return EINVAL
22391 	 */
22392 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22393 		err = EINVAL;
22394 		goto done;
22395 	}
22396 
22397 	/*
22398 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22399 	 * be then this is a duplicate operation.
22400 	 */
22401 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22402 		err = 0;
22403 		goto done;
22404 	}
22405 
22406 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22407 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22408 	    usesrc_ill->ill_isv6));
22409 
22410 	/*
22411 	 * The next step ensures that no new ires will be created referencing
22412 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22413 	 * we go through an ire walk deleting all ire caches that reference
22414 	 * the client ill. New ires referencing the client ill that are added
22415 	 * to the ire table before the ILL_CHANGING flag is set, will be
22416 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22417 	 * the client ill while the ILL_CHANGING flag is set will be failed
22418 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22419 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22420 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22421 	 * belong to the same usesrc group.
22422 	 */
22423 	mutex_enter(&usesrc_cli_ill->ill_lock);
22424 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22425 	mutex_exit(&usesrc_cli_ill->ill_lock);
22426 	ill_flag_changed = B_TRUE;
22427 
22428 	if (ipif->ipif_isv6)
22429 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22430 		    ALL_ZONES, ipst);
22431 	else
22432 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22433 		    ALL_ZONES, ipst);
22434 
22435 	/*
22436 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22437 	 * and the ill_usesrc_ifindex fields
22438 	 */
22439 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22440 
22441 	if (reset_flg) {
22442 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22443 		if (ret != 0) {
22444 			err = EINVAL;
22445 		}
22446 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22447 		goto done;
22448 	}
22449 
22450 	/*
22451 	 * Four possibilities to consider:
22452 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22453 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22454 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22455 	 * 4. Both are part of their respective usesrc groups
22456 	 */
22457 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22458 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22459 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22460 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22461 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22462 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22463 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22464 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22465 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22466 		/* Insert at head of list */
22467 		usesrc_cli_ill->ill_usesrc_grp_next =
22468 		    usesrc_ill->ill_usesrc_grp_next;
22469 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22470 	} else {
22471 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22472 		    ifindex);
22473 		if (ret != 0)
22474 			err = EINVAL;
22475 	}
22476 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22477 
22478 done:
22479 	if (ill_flag_changed) {
22480 		mutex_enter(&usesrc_cli_ill->ill_lock);
22481 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22482 		mutex_exit(&usesrc_cli_ill->ill_lock);
22483 	}
22484 	if (ipsq != NULL)
22485 		ipsq_exit(ipsq);
22486 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22487 	ill_refrele(usesrc_ill);
22488 	return (err);
22489 }
22490 
22491 /*
22492  * comparison function used by avl.
22493  */
22494 static int
22495 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22496 {
22497 
22498 	uint_t index;
22499 
22500 	ASSERT(phyip != NULL && index_ptr != NULL);
22501 
22502 	index = *((uint_t *)index_ptr);
22503 	/*
22504 	 * let the phyint with the lowest index be on top.
22505 	 */
22506 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22507 		return (1);
22508 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22509 		return (-1);
22510 	return (0);
22511 }
22512 
22513 /*
22514  * comparison function used by avl.
22515  */
22516 static int
22517 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22518 {
22519 	ill_t *ill;
22520 	int res = 0;
22521 
22522 	ASSERT(phyip != NULL && name_ptr != NULL);
22523 
22524 	if (((phyint_t *)phyip)->phyint_illv4)
22525 		ill = ((phyint_t *)phyip)->phyint_illv4;
22526 	else
22527 		ill = ((phyint_t *)phyip)->phyint_illv6;
22528 	ASSERT(ill != NULL);
22529 
22530 	res = strcmp(ill->ill_name, (char *)name_ptr);
22531 	if (res > 0)
22532 		return (1);
22533 	else if (res < 0)
22534 		return (-1);
22535 	return (0);
22536 }
22537 /*
22538  * This function is called from ill_delete when the ill is being
22539  * unplumbed. We remove the reference from the phyint and we also
22540  * free the phyint when there are no more references to it.
22541  */
22542 static void
22543 ill_phyint_free(ill_t *ill)
22544 {
22545 	phyint_t *phyi;
22546 	phyint_t *next_phyint;
22547 	ipsq_t *cur_ipsq;
22548 	ip_stack_t	*ipst = ill->ill_ipst;
22549 
22550 	ASSERT(ill->ill_phyint != NULL);
22551 
22552 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22553 	phyi = ill->ill_phyint;
22554 	ill->ill_phyint = NULL;
22555 	/*
22556 	 * ill_init allocates a phyint always to store the copy
22557 	 * of flags relevant to phyint. At that point in time, we could
22558 	 * not assign the name and hence phyint_illv4/v6 could not be
22559 	 * initialized. Later in ipif_set_values, we assign the name to
22560 	 * the ill, at which point in time we assign phyint_illv4/v6.
22561 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22562 	 */
22563 	if (ill->ill_flags & ILLF_IPV6) {
22564 		phyi->phyint_illv6 = NULL;
22565 	} else {
22566 		phyi->phyint_illv4 = NULL;
22567 	}
22568 	/*
22569 	 * ipif_down removes it from the group when the last ipif goes
22570 	 * down.
22571 	 */
22572 	ASSERT(ill->ill_group == NULL);
22573 
22574 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22575 		return;
22576 
22577 	/*
22578 	 * Make sure this phyint was put in the list.
22579 	 */
22580 	if (phyi->phyint_ifindex > 0) {
22581 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22582 		    phyi);
22583 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22584 		    phyi);
22585 	}
22586 	/*
22587 	 * remove phyint from the ipsq list.
22588 	 */
22589 	cur_ipsq = phyi->phyint_ipsq;
22590 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22591 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22592 	} else {
22593 		next_phyint = cur_ipsq->ipsq_phyint_list;
22594 		while (next_phyint != NULL) {
22595 			if (next_phyint->phyint_ipsq_next == phyi) {
22596 				next_phyint->phyint_ipsq_next =
22597 				    phyi->phyint_ipsq_next;
22598 				break;
22599 			}
22600 			next_phyint = next_phyint->phyint_ipsq_next;
22601 		}
22602 		ASSERT(next_phyint != NULL);
22603 	}
22604 	IPSQ_DEC_REF(cur_ipsq, ipst);
22605 
22606 	if (phyi->phyint_groupname_len != 0) {
22607 		ASSERT(phyi->phyint_groupname != NULL);
22608 		mi_free(phyi->phyint_groupname);
22609 	}
22610 	mi_free(phyi);
22611 }
22612 
22613 /*
22614  * Attach the ill to the phyint structure which can be shared by both
22615  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22616  * function is called from ipif_set_values and ill_lookup_on_name (for
22617  * loopback) where we know the name of the ill. We lookup the ill and if
22618  * there is one present already with the name use that phyint. Otherwise
22619  * reuse the one allocated by ill_init.
22620  */
22621 static void
22622 ill_phyint_reinit(ill_t *ill)
22623 {
22624 	boolean_t isv6 = ill->ill_isv6;
22625 	phyint_t *phyi_old;
22626 	phyint_t *phyi;
22627 	avl_index_t where = 0;
22628 	ill_t	*ill_other = NULL;
22629 	ipsq_t	*ipsq;
22630 	ip_stack_t	*ipst = ill->ill_ipst;
22631 
22632 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22633 
22634 	phyi_old = ill->ill_phyint;
22635 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22636 	    phyi_old->phyint_illv6 == NULL));
22637 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22638 	    phyi_old->phyint_illv4 == NULL));
22639 	ASSERT(phyi_old->phyint_ifindex == 0);
22640 
22641 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22642 	    ill->ill_name, &where);
22643 
22644 	/*
22645 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22646 	 *    the global list of ills. So no other thread could have located
22647 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22648 	 * 2. Now locate the other protocol instance of this ill.
22649 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22650 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22651 	 *    of neither ill can change.
22652 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22653 	 *    other ill.
22654 	 * 5. Release all locks.
22655 	 */
22656 
22657 	/*
22658 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22659 	 * we are initializing IPv4.
22660 	 */
22661 	if (phyi != NULL) {
22662 		ill_other = (isv6) ? phyi->phyint_illv4 :
22663 		    phyi->phyint_illv6;
22664 		ASSERT(ill_other->ill_phyint != NULL);
22665 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22666 		    (!isv6 && ill_other->ill_isv6));
22667 		GRAB_ILL_LOCKS(ill, ill_other);
22668 		/*
22669 		 * We are potentially throwing away phyint_flags which
22670 		 * could be different from the one that we obtain from
22671 		 * ill_other->ill_phyint. But it is okay as we are assuming
22672 		 * that the state maintained within IP is correct.
22673 		 */
22674 		mutex_enter(&phyi->phyint_lock);
22675 		if (isv6) {
22676 			ASSERT(phyi->phyint_illv6 == NULL);
22677 			phyi->phyint_illv6 = ill;
22678 		} else {
22679 			ASSERT(phyi->phyint_illv4 == NULL);
22680 			phyi->phyint_illv4 = ill;
22681 		}
22682 		/*
22683 		 * This is a new ill, currently undergoing SLIFNAME
22684 		 * So we could not have joined an IPMP group until now.
22685 		 */
22686 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22687 		    phyi_old->phyint_groupname == NULL);
22688 
22689 		/*
22690 		 * This phyi_old is going away. Decref ipsq_refs and
22691 		 * assert it is zero. The ipsq itself will be freed in
22692 		 * ipsq_exit
22693 		 */
22694 		ipsq = phyi_old->phyint_ipsq;
22695 		IPSQ_DEC_REF(ipsq, ipst);
22696 		ASSERT(ipsq->ipsq_refs == 0);
22697 		/* Get the singleton phyint out of the ipsq list */
22698 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22699 		ipsq->ipsq_phyint_list = NULL;
22700 		phyi_old->phyint_illv4 = NULL;
22701 		phyi_old->phyint_illv6 = NULL;
22702 		mi_free(phyi_old);
22703 	} else {
22704 		mutex_enter(&ill->ill_lock);
22705 		/*
22706 		 * We don't need to acquire any lock, since
22707 		 * the ill is not yet visible globally  and we
22708 		 * have not yet released the ill_g_lock.
22709 		 */
22710 		phyi = phyi_old;
22711 		mutex_enter(&phyi->phyint_lock);
22712 		/* XXX We need a recovery strategy here. */
22713 		if (!phyint_assign_ifindex(phyi, ipst))
22714 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22715 
22716 		/* No IPMP group yet, thus the hook uses the ifindex */
22717 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22718 
22719 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22720 		    (void *)phyi, where);
22721 
22722 		(void) avl_find(&ipst->ips_phyint_g_list->
22723 		    phyint_list_avl_by_index,
22724 		    &phyi->phyint_ifindex, &where);
22725 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22726 		    (void *)phyi, where);
22727 	}
22728 
22729 	/*
22730 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22731 	 * pending mp is not affected because that is per ill basis.
22732 	 */
22733 	ill->ill_phyint = phyi;
22734 
22735 	/*
22736 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22737 	 * We do this here as when the first ipif was allocated,
22738 	 * ipif_allocate does not know the right interface index.
22739 	 */
22740 
22741 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22742 	/*
22743 	 * Now that the phyint's ifindex has been assigned, complete the
22744 	 * remaining
22745 	 */
22746 
22747 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22748 	if (ill->ill_isv6) {
22749 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22750 		    ill->ill_phyint->phyint_ifindex;
22751 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22752 	} else {
22753 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22754 	}
22755 
22756 	/*
22757 	 * Generate an event within the hooks framework to indicate that
22758 	 * a new interface has just been added to IP.  For this event to
22759 	 * be generated, the network interface must, at least, have an
22760 	 * ifindex assigned to it.
22761 	 *
22762 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22763 	 * that the ordering of delivered events to listeners matches the
22764 	 * order of them in the kernel.
22765 	 *
22766 	 * This function could be called from ill_lookup_on_name. In that case
22767 	 * the interface is loopback "lo", which will not generate a NIC event.
22768 	 */
22769 	if (ill->ill_name_length <= 2 ||
22770 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22771 		/*
22772 		 * Generate nic plumb event for ill_name even if
22773 		 * ipmp_hook_emulation is set. That avoids generating events
22774 		 * for the ill_names should ipmp_hook_emulation be turned on
22775 		 * later.
22776 		 */
22777 		ill_nic_info_plumb(ill, B_FALSE);
22778 	}
22779 	RELEASE_ILL_LOCKS(ill, ill_other);
22780 	mutex_exit(&phyi->phyint_lock);
22781 }
22782 
22783 /*
22784  * Allocate a NE_PLUMB nic info event and store in the ill.
22785  * If 'group' is set we do it for the group name, otherwise the ill name.
22786  * It will be sent when we leave the ipsq.
22787  */
22788 void
22789 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22790 {
22791 	phyint_t	*phyi = ill->ill_phyint;
22792 	char		*name;
22793 	int		namelen;
22794 
22795 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22796 
22797 	if (group) {
22798 		ASSERT(phyi->phyint_groupname_len != 0);
22799 		namelen = phyi->phyint_groupname_len;
22800 		name = phyi->phyint_groupname;
22801 	} else {
22802 		namelen = ill->ill_name_length;
22803 		name = ill->ill_name;
22804 	}
22805 
22806 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22807 }
22808 
22809 /*
22810  * Unhook the nic event message from the ill and enqueue it
22811  * into the nic event taskq.
22812  */
22813 void
22814 ill_nic_info_dispatch(ill_t *ill)
22815 {
22816 	hook_nic_event_int_t *info;
22817 
22818 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22819 
22820 	if ((info = ill->ill_nic_event_info) != NULL) {
22821 		if (ddi_taskq_dispatch(eventq_queue_nic,
22822 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22823 			ip2dbg(("ill_nic_info_dispatch: "
22824 			    "ddi_taskq_dispatch failed\n"));
22825 			if (info->hnei_event.hne_data != NULL) {
22826 				kmem_free(info->hnei_event.hne_data,
22827 				    info->hnei_event.hne_datalen);
22828 			}
22829 			kmem_free(info, sizeof (*info));
22830 		}
22831 		ill->ill_nic_event_info = NULL;
22832 	}
22833 }
22834 
22835 /*
22836  * Notify any downstream modules of the name of this interface.
22837  * An M_IOCTL is used even though we don't expect a successful reply.
22838  * Any reply message from the driver (presumably an M_IOCNAK) will
22839  * eventually get discarded somewhere upstream.  The message format is
22840  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22841  * to IP.
22842  */
22843 static void
22844 ip_ifname_notify(ill_t *ill, queue_t *q)
22845 {
22846 	mblk_t *mp1, *mp2;
22847 	struct iocblk *iocp;
22848 	struct lifreq *lifr;
22849 
22850 	mp1 = mkiocb(SIOCSLIFNAME);
22851 	if (mp1 == NULL)
22852 		return;
22853 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22854 	if (mp2 == NULL) {
22855 		freeb(mp1);
22856 		return;
22857 	}
22858 
22859 	mp1->b_cont = mp2;
22860 	iocp = (struct iocblk *)mp1->b_rptr;
22861 	iocp->ioc_count = sizeof (struct lifreq);
22862 
22863 	lifr = (struct lifreq *)mp2->b_rptr;
22864 	mp2->b_wptr += sizeof (struct lifreq);
22865 	bzero(lifr, sizeof (struct lifreq));
22866 
22867 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22868 	lifr->lifr_ppa = ill->ill_ppa;
22869 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22870 
22871 	putnext(q, mp1);
22872 }
22873 
22874 static int
22875 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22876 {
22877 	int err;
22878 	ip_stack_t	*ipst = ill->ill_ipst;
22879 
22880 	/* Set the obsolete NDD per-interface forwarding name. */
22881 	err = ill_set_ndd_name(ill);
22882 	if (err != 0) {
22883 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22884 		    err);
22885 	}
22886 
22887 	/* Tell downstream modules where they are. */
22888 	ip_ifname_notify(ill, q);
22889 
22890 	/*
22891 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22892 	 * Error cases are ENOMEM ...
22893 	 */
22894 	err = ill_dl_phys(ill, ipif, mp, q);
22895 
22896 	/*
22897 	 * If there is no IRE expiration timer running, get one started.
22898 	 * igmp and mld timers will be triggered by the first multicast
22899 	 */
22900 	if (ipst->ips_ip_ire_expire_id == 0) {
22901 		/*
22902 		 * acquire the lock and check again.
22903 		 */
22904 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22905 		if (ipst->ips_ip_ire_expire_id == 0) {
22906 			ipst->ips_ip_ire_expire_id = timeout(
22907 			    ip_trash_timer_expire, ipst,
22908 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22909 		}
22910 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22911 	}
22912 
22913 	if (ill->ill_isv6) {
22914 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22915 		if (ipst->ips_mld_slowtimeout_id == 0) {
22916 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22917 			    (void *)ipst,
22918 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22919 		}
22920 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22921 	} else {
22922 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22923 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22924 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22925 			    (void *)ipst,
22926 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22927 		}
22928 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22929 	}
22930 
22931 	return (err);
22932 }
22933 
22934 /*
22935  * Common routine for ppa and ifname setting. Should be called exclusive.
22936  *
22937  * Returns EINPROGRESS when mp has been consumed by queueing it on
22938  * ill_pending_mp and the ioctl will complete in ip_rput.
22939  *
22940  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22941  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22942  * For SLIFNAME, we pass these values back to the userland.
22943  */
22944 static int
22945 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22946 {
22947 	ill_t	*ill;
22948 	ipif_t	*ipif;
22949 	ipsq_t	*ipsq;
22950 	char	*ppa_ptr;
22951 	char	*old_ptr;
22952 	char	old_char;
22953 	int	error;
22954 	ip_stack_t	*ipst;
22955 
22956 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22957 	ASSERT(q->q_next != NULL);
22958 	ASSERT(interf_name != NULL);
22959 
22960 	ill = (ill_t *)q->q_ptr;
22961 	ipst = ill->ill_ipst;
22962 
22963 	ASSERT(ill->ill_ipst != NULL);
22964 	ASSERT(ill->ill_name[0] == '\0');
22965 	ASSERT(IAM_WRITER_ILL(ill));
22966 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22967 	ASSERT(ill->ill_ppa == UINT_MAX);
22968 
22969 	/* The ppa is sent down by ifconfig or is chosen */
22970 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22971 		return (EINVAL);
22972 	}
22973 
22974 	/*
22975 	 * make sure ppa passed in is same as ppa in the name.
22976 	 * This check is not made when ppa == UINT_MAX in that case ppa
22977 	 * in the name could be anything. System will choose a ppa and
22978 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22979 	 */
22980 	if (*new_ppa_ptr != UINT_MAX) {
22981 		/* stoi changes the pointer */
22982 		old_ptr = ppa_ptr;
22983 		/*
22984 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22985 		 * (they don't have an externally visible ppa).  We assign one
22986 		 * here so that we can manage the interface.  Note that in
22987 		 * the past this value was always 0 for DLPI 1 drivers.
22988 		 */
22989 		if (*new_ppa_ptr == 0)
22990 			*new_ppa_ptr = stoi(&old_ptr);
22991 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22992 			return (EINVAL);
22993 	}
22994 	/*
22995 	 * terminate string before ppa
22996 	 * save char at that location.
22997 	 */
22998 	old_char = ppa_ptr[0];
22999 	ppa_ptr[0] = '\0';
23000 
23001 	ill->ill_ppa = *new_ppa_ptr;
23002 	/*
23003 	 * Finish as much work now as possible before calling ill_glist_insert
23004 	 * which makes the ill globally visible and also merges it with the
23005 	 * other protocol instance of this phyint. The remaining work is
23006 	 * done after entering the ipsq which may happen sometime later.
23007 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
23008 	 */
23009 	ipif = ill->ill_ipif;
23010 
23011 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
23012 	ipif_assign_seqid(ipif);
23013 
23014 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
23015 		ill->ill_flags |= ILLF_IPV4;
23016 
23017 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
23018 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
23019 
23020 	if (ill->ill_flags & ILLF_IPV6) {
23021 
23022 		ill->ill_isv6 = B_TRUE;
23023 		if (ill->ill_rq != NULL) {
23024 			ill->ill_rq->q_qinfo = &iprinitv6;
23025 			ill->ill_wq->q_qinfo = &ipwinitv6;
23026 		}
23027 
23028 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23029 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23030 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23031 		ipif->ipif_v6subnet = ipv6_all_zeros;
23032 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23033 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23034 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23035 		/*
23036 		 * point-to-point or Non-mulicast capable
23037 		 * interfaces won't do NUD unless explicitly
23038 		 * configured to do so.
23039 		 */
23040 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23041 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23042 			ill->ill_flags |= ILLF_NONUD;
23043 		}
23044 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23045 		if (ill->ill_flags & ILLF_NOARP) {
23046 			/*
23047 			 * Note: xresolv interfaces will eventually need
23048 			 * NOARP set here as well, but that will require
23049 			 * those external resolvers to have some
23050 			 * knowledge of that flag and act appropriately.
23051 			 * Not to be changed at present.
23052 			 */
23053 			ill->ill_flags &= ~ILLF_NOARP;
23054 		}
23055 		/*
23056 		 * Set the ILLF_ROUTER flag according to the global
23057 		 * IPv6 forwarding policy.
23058 		 */
23059 		if (ipst->ips_ipv6_forward != 0)
23060 			ill->ill_flags |= ILLF_ROUTER;
23061 	} else if (ill->ill_flags & ILLF_IPV4) {
23062 		ill->ill_isv6 = B_FALSE;
23063 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23064 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23065 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23066 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23067 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23068 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23069 		/*
23070 		 * Set the ILLF_ROUTER flag according to the global
23071 		 * IPv4 forwarding policy.
23072 		 */
23073 		if (ipst->ips_ip_g_forward != 0)
23074 			ill->ill_flags |= ILLF_ROUTER;
23075 	}
23076 
23077 	ASSERT(ill->ill_phyint != NULL);
23078 
23079 	/*
23080 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23081 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23082 	 */
23083 	if (!ill_allocate_mibs(ill))
23084 		return (ENOMEM);
23085 
23086 	/*
23087 	 * Pick a default sap until we get the DL_INFO_ACK back from
23088 	 * the driver.
23089 	 */
23090 	if (ill->ill_sap == 0) {
23091 		if (ill->ill_isv6)
23092 			ill->ill_sap  = IP6_DL_SAP;
23093 		else
23094 			ill->ill_sap  = IP_DL_SAP;
23095 	}
23096 
23097 	ill->ill_ifname_pending = 1;
23098 	ill->ill_ifname_pending_err = 0;
23099 
23100 	ill_refhold(ill);
23101 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23102 	if ((error = ill_glist_insert(ill, interf_name,
23103 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23104 		ill->ill_ppa = UINT_MAX;
23105 		ill->ill_name[0] = '\0';
23106 		/*
23107 		 * undo null termination done above.
23108 		 */
23109 		ppa_ptr[0] = old_char;
23110 		rw_exit(&ipst->ips_ill_g_lock);
23111 		ill_refrele(ill);
23112 		return (error);
23113 	}
23114 
23115 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23116 
23117 	/*
23118 	 * When we return the buffer pointed to by interf_name should contain
23119 	 * the same name as in ill_name.
23120 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23121 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23122 	 * so copy full name and update the ppa ptr.
23123 	 * When ppa passed in != UINT_MAX all values are correct just undo
23124 	 * null termination, this saves a bcopy.
23125 	 */
23126 	if (*new_ppa_ptr == UINT_MAX) {
23127 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23128 		*new_ppa_ptr = ill->ill_ppa;
23129 	} else {
23130 		/*
23131 		 * undo null termination done above.
23132 		 */
23133 		ppa_ptr[0] = old_char;
23134 	}
23135 
23136 	/* Let SCTP know about this ILL */
23137 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23138 
23139 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23140 	    B_TRUE);
23141 
23142 	rw_exit(&ipst->ips_ill_g_lock);
23143 	ill_refrele(ill);
23144 	if (ipsq == NULL)
23145 		return (EINPROGRESS);
23146 
23147 	/*
23148 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23149 	 */
23150 	if (ipsq->ipsq_current_ipif == NULL)
23151 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23152 	else
23153 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23154 
23155 	error = ipif_set_values_tail(ill, ipif, mp, q);
23156 	ipsq_exit(ipsq);
23157 	if (error != 0 && error != EINPROGRESS) {
23158 		/*
23159 		 * restore previous values
23160 		 */
23161 		ill->ill_isv6 = B_FALSE;
23162 	}
23163 	return (error);
23164 }
23165 
23166 
23167 void
23168 ipif_init(ip_stack_t *ipst)
23169 {
23170 	hrtime_t hrt;
23171 	int i;
23172 
23173 	/*
23174 	 * Can't call drv_getparm here as it is too early in the boot.
23175 	 * As we use ipif_src_random just for picking a different
23176 	 * source address everytime, this need not be really random.
23177 	 */
23178 	hrt = gethrtime();
23179 	ipst->ips_ipif_src_random =
23180 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23181 
23182 	for (i = 0; i < MAX_G_HEADS; i++) {
23183 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23184 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23185 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23186 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23187 	}
23188 
23189 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23190 	    ill_phyint_compare_index,
23191 	    sizeof (phyint_t),
23192 	    offsetof(struct phyint, phyint_avl_by_index));
23193 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23194 	    ill_phyint_compare_name,
23195 	    sizeof (phyint_t),
23196 	    offsetof(struct phyint, phyint_avl_by_name));
23197 }
23198 
23199 /*
23200  * Lookup the ipif corresponding to the onlink destination address. For
23201  * point-to-point interfaces, it matches with remote endpoint destination
23202  * address. For point-to-multipoint interfaces it only tries to match the
23203  * destination with the interface's subnet address. The longest, most specific
23204  * match is found to take care of such rare network configurations like -
23205  * le0: 129.146.1.1/16
23206  * le1: 129.146.2.2/24
23207  * It is used only by SO_DONTROUTE at the moment.
23208  */
23209 ipif_t *
23210 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23211 {
23212 	ipif_t	*ipif, *best_ipif;
23213 	ill_t	*ill;
23214 	ill_walk_context_t ctx;
23215 
23216 	ASSERT(zoneid != ALL_ZONES);
23217 	best_ipif = NULL;
23218 
23219 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23220 	ill = ILL_START_WALK_V4(&ctx, ipst);
23221 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23222 		mutex_enter(&ill->ill_lock);
23223 		for (ipif = ill->ill_ipif; ipif != NULL;
23224 		    ipif = ipif->ipif_next) {
23225 			if (!IPIF_CAN_LOOKUP(ipif))
23226 				continue;
23227 			if (ipif->ipif_zoneid != zoneid &&
23228 			    ipif->ipif_zoneid != ALL_ZONES)
23229 				continue;
23230 			/*
23231 			 * Point-to-point case. Look for exact match with
23232 			 * destination address.
23233 			 */
23234 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23235 				if (ipif->ipif_pp_dst_addr == addr) {
23236 					ipif_refhold_locked(ipif);
23237 					mutex_exit(&ill->ill_lock);
23238 					rw_exit(&ipst->ips_ill_g_lock);
23239 					if (best_ipif != NULL)
23240 						ipif_refrele(best_ipif);
23241 					return (ipif);
23242 				}
23243 			} else if (ipif->ipif_subnet == (addr &
23244 			    ipif->ipif_net_mask)) {
23245 				/*
23246 				 * Point-to-multipoint case. Looping through to
23247 				 * find the most specific match. If there are
23248 				 * multiple best match ipif's then prefer ipif's
23249 				 * that are UP. If there is only one best match
23250 				 * ipif and it is DOWN we must still return it.
23251 				 */
23252 				if ((best_ipif == NULL) ||
23253 				    (ipif->ipif_net_mask >
23254 				    best_ipif->ipif_net_mask) ||
23255 				    ((ipif->ipif_net_mask ==
23256 				    best_ipif->ipif_net_mask) &&
23257 				    ((ipif->ipif_flags & IPIF_UP) &&
23258 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23259 					ipif_refhold_locked(ipif);
23260 					mutex_exit(&ill->ill_lock);
23261 					rw_exit(&ipst->ips_ill_g_lock);
23262 					if (best_ipif != NULL)
23263 						ipif_refrele(best_ipif);
23264 					best_ipif = ipif;
23265 					rw_enter(&ipst->ips_ill_g_lock,
23266 					    RW_READER);
23267 					mutex_enter(&ill->ill_lock);
23268 				}
23269 			}
23270 		}
23271 		mutex_exit(&ill->ill_lock);
23272 	}
23273 	rw_exit(&ipst->ips_ill_g_lock);
23274 	return (best_ipif);
23275 }
23276 
23277 /*
23278  * Save enough information so that we can recreate the IRE if
23279  * the interface goes down and then up.
23280  */
23281 static void
23282 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23283 {
23284 	mblk_t	*save_mp;
23285 
23286 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23287 	if (save_mp != NULL) {
23288 		ifrt_t	*ifrt;
23289 
23290 		save_mp->b_wptr += sizeof (ifrt_t);
23291 		ifrt = (ifrt_t *)save_mp->b_rptr;
23292 		bzero(ifrt, sizeof (ifrt_t));
23293 		ifrt->ifrt_type = ire->ire_type;
23294 		ifrt->ifrt_addr = ire->ire_addr;
23295 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23296 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23297 		ifrt->ifrt_mask = ire->ire_mask;
23298 		ifrt->ifrt_flags = ire->ire_flags;
23299 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23300 		mutex_enter(&ipif->ipif_saved_ire_lock);
23301 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23302 		ipif->ipif_saved_ire_mp = save_mp;
23303 		ipif->ipif_saved_ire_cnt++;
23304 		mutex_exit(&ipif->ipif_saved_ire_lock);
23305 	}
23306 }
23307 
23308 static void
23309 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23310 {
23311 	mblk_t	**mpp;
23312 	mblk_t	*mp;
23313 	ifrt_t	*ifrt;
23314 
23315 	/* Remove from ipif_saved_ire_mp list if it is there */
23316 	mutex_enter(&ipif->ipif_saved_ire_lock);
23317 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23318 	    mpp = &(*mpp)->b_cont) {
23319 		/*
23320 		 * On a given ipif, the triple of address, gateway and
23321 		 * mask is unique for each saved IRE (in the case of
23322 		 * ordinary interface routes, the gateway address is
23323 		 * all-zeroes).
23324 		 */
23325 		mp = *mpp;
23326 		ifrt = (ifrt_t *)mp->b_rptr;
23327 		if (ifrt->ifrt_addr == ire->ire_addr &&
23328 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23329 		    ifrt->ifrt_mask == ire->ire_mask) {
23330 			*mpp = mp->b_cont;
23331 			ipif->ipif_saved_ire_cnt--;
23332 			freeb(mp);
23333 			break;
23334 		}
23335 	}
23336 	mutex_exit(&ipif->ipif_saved_ire_lock);
23337 }
23338 
23339 /*
23340  * IP multirouting broadcast routes handling
23341  * Append CGTP broadcast IREs to regular ones created
23342  * at ifconfig time.
23343  */
23344 static void
23345 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23346 {
23347 	ire_t *ire_prim;
23348 
23349 	ASSERT(ire != NULL);
23350 	ASSERT(ire_dst != NULL);
23351 
23352 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23353 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23354 	if (ire_prim != NULL) {
23355 		/*
23356 		 * We are in the special case of broadcasts for
23357 		 * CGTP. We add an IRE_BROADCAST that holds
23358 		 * the RTF_MULTIRT flag, the destination
23359 		 * address of ire_dst and the low level
23360 		 * info of ire_prim. In other words, CGTP
23361 		 * broadcast is added to the redundant ipif.
23362 		 */
23363 		ipif_t *ipif_prim;
23364 		ire_t  *bcast_ire;
23365 
23366 		ipif_prim = ire_prim->ire_ipif;
23367 
23368 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23369 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23370 		    (void *)ire_dst, (void *)ire_prim,
23371 		    (void *)ipif_prim));
23372 
23373 		bcast_ire = ire_create(
23374 		    (uchar_t *)&ire->ire_addr,
23375 		    (uchar_t *)&ip_g_all_ones,
23376 		    (uchar_t *)&ire_dst->ire_src_addr,
23377 		    (uchar_t *)&ire->ire_gateway_addr,
23378 		    &ipif_prim->ipif_mtu,
23379 		    NULL,
23380 		    ipif_prim->ipif_rq,
23381 		    ipif_prim->ipif_wq,
23382 		    IRE_BROADCAST,
23383 		    ipif_prim,
23384 		    0,
23385 		    0,
23386 		    0,
23387 		    ire->ire_flags,
23388 		    &ire_uinfo_null,
23389 		    NULL,
23390 		    NULL,
23391 		    ipst);
23392 
23393 		if (bcast_ire != NULL) {
23394 
23395 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23396 			    B_FALSE) == 0) {
23397 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23398 				    "added bcast_ire %p\n",
23399 				    (void *)bcast_ire));
23400 
23401 				ipif_save_ire(bcast_ire->ire_ipif,
23402 				    bcast_ire);
23403 				ire_refrele(bcast_ire);
23404 			}
23405 		}
23406 		ire_refrele(ire_prim);
23407 	}
23408 }
23409 
23410 
23411 /*
23412  * IP multirouting broadcast routes handling
23413  * Remove the broadcast ire
23414  */
23415 static void
23416 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23417 {
23418 	ire_t *ire_dst;
23419 
23420 	ASSERT(ire != NULL);
23421 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23422 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23423 	if (ire_dst != NULL) {
23424 		ire_t *ire_prim;
23425 
23426 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23427 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23428 		if (ire_prim != NULL) {
23429 			ipif_t *ipif_prim;
23430 			ire_t  *bcast_ire;
23431 
23432 			ipif_prim = ire_prim->ire_ipif;
23433 
23434 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23435 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23436 			    (void *)ire_dst, (void *)ire_prim,
23437 			    (void *)ipif_prim));
23438 
23439 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23440 			    ire->ire_gateway_addr,
23441 			    IRE_BROADCAST,
23442 			    ipif_prim, ALL_ZONES,
23443 			    NULL,
23444 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23445 			    MATCH_IRE_MASK, ipst);
23446 
23447 			if (bcast_ire != NULL) {
23448 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23449 				    "looked up bcast_ire %p\n",
23450 				    (void *)bcast_ire));
23451 				ipif_remove_ire(bcast_ire->ire_ipif,
23452 				    bcast_ire);
23453 				ire_delete(bcast_ire);
23454 				ire_refrele(bcast_ire);
23455 			}
23456 			ire_refrele(ire_prim);
23457 		}
23458 		ire_refrele(ire_dst);
23459 	}
23460 }
23461 
23462 /*
23463  * IPsec hardware acceleration capabilities related functions.
23464  */
23465 
23466 /*
23467  * Free a per-ill IPsec capabilities structure.
23468  */
23469 static void
23470 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23471 {
23472 	if (capab->auth_hw_algs != NULL)
23473 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23474 	if (capab->encr_hw_algs != NULL)
23475 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23476 	if (capab->encr_algparm != NULL)
23477 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23478 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23479 }
23480 
23481 /*
23482  * Allocate a new per-ill IPsec capabilities structure. This structure
23483  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23484  * an array which specifies, for each algorithm, whether this algorithm
23485  * is supported by the ill or not.
23486  */
23487 static ill_ipsec_capab_t *
23488 ill_ipsec_capab_alloc(void)
23489 {
23490 	ill_ipsec_capab_t *capab;
23491 	uint_t nelems;
23492 
23493 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23494 	if (capab == NULL)
23495 		return (NULL);
23496 
23497 	/* we need one bit per algorithm */
23498 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23499 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23500 
23501 	/* allocate memory to store algorithm flags */
23502 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23503 	if (capab->encr_hw_algs == NULL)
23504 		goto nomem;
23505 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23506 	if (capab->auth_hw_algs == NULL)
23507 		goto nomem;
23508 	/*
23509 	 * Leave encr_algparm NULL for now since we won't need it half
23510 	 * the time
23511 	 */
23512 	return (capab);
23513 
23514 nomem:
23515 	ill_ipsec_capab_free(capab);
23516 	return (NULL);
23517 }
23518 
23519 /*
23520  * Resize capability array.  Since we're exclusive, this is OK.
23521  */
23522 static boolean_t
23523 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23524 {
23525 	ipsec_capab_algparm_t *nalp, *oalp;
23526 	uint32_t olen, nlen;
23527 
23528 	oalp = capab->encr_algparm;
23529 	olen = capab->encr_algparm_size;
23530 
23531 	if (oalp != NULL) {
23532 		if (algid < capab->encr_algparm_end)
23533 			return (B_TRUE);
23534 	}
23535 
23536 	nlen = (algid + 1) * sizeof (*nalp);
23537 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23538 	if (nalp == NULL)
23539 		return (B_FALSE);
23540 
23541 	if (oalp != NULL) {
23542 		bcopy(oalp, nalp, olen);
23543 		kmem_free(oalp, olen);
23544 	}
23545 	capab->encr_algparm = nalp;
23546 	capab->encr_algparm_size = nlen;
23547 	capab->encr_algparm_end = algid + 1;
23548 
23549 	return (B_TRUE);
23550 }
23551 
23552 /*
23553  * Compare the capabilities of the specified ill with the protocol
23554  * and algorithms specified by the SA passed as argument.
23555  * If they match, returns B_TRUE, B_FALSE if they do not match.
23556  *
23557  * The ill can be passed as a pointer to it, or by specifying its index
23558  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23559  *
23560  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23561  * packet is eligible for hardware acceleration, and by
23562  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23563  * to a particular ill.
23564  */
23565 boolean_t
23566 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23567     ipsa_t *sa, netstack_t *ns)
23568 {
23569 	boolean_t sa_isv6;
23570 	uint_t algid;
23571 	struct ill_ipsec_capab_s *cpp;
23572 	boolean_t need_refrele = B_FALSE;
23573 	ip_stack_t	*ipst = ns->netstack_ip;
23574 
23575 	if (ill == NULL) {
23576 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23577 		    NULL, NULL, NULL, ipst);
23578 		if (ill == NULL) {
23579 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23580 			return (B_FALSE);
23581 		}
23582 		need_refrele = B_TRUE;
23583 	}
23584 
23585 	/*
23586 	 * Use the address length specified by the SA to determine
23587 	 * if it corresponds to a IPv6 address, and fail the matching
23588 	 * if the isv6 flag passed as argument does not match.
23589 	 * Note: this check is used for SADB capability checking before
23590 	 * sending SA information to an ill.
23591 	 */
23592 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23593 	if (sa_isv6 != ill_isv6)
23594 		/* protocol mismatch */
23595 		goto done;
23596 
23597 	/*
23598 	 * Check if the ill supports the protocol, algorithm(s) and
23599 	 * key size(s) specified by the SA, and get the pointers to
23600 	 * the algorithms supported by the ill.
23601 	 */
23602 	switch (sa->ipsa_type) {
23603 
23604 	case SADB_SATYPE_ESP:
23605 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23606 			/* ill does not support ESP acceleration */
23607 			goto done;
23608 		cpp = ill->ill_ipsec_capab_esp;
23609 		algid = sa->ipsa_auth_alg;
23610 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23611 			goto done;
23612 		algid = sa->ipsa_encr_alg;
23613 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23614 			goto done;
23615 		if (algid < cpp->encr_algparm_end) {
23616 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23617 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23618 				goto done;
23619 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23620 				goto done;
23621 		}
23622 		break;
23623 
23624 	case SADB_SATYPE_AH:
23625 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23626 			/* ill does not support AH acceleration */
23627 			goto done;
23628 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23629 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23630 			goto done;
23631 		break;
23632 	}
23633 
23634 	if (need_refrele)
23635 		ill_refrele(ill);
23636 	return (B_TRUE);
23637 done:
23638 	if (need_refrele)
23639 		ill_refrele(ill);
23640 	return (B_FALSE);
23641 }
23642 
23643 /*
23644  * Add a new ill to the list of IPsec capable ills.
23645  * Called from ill_capability_ipsec_ack() when an ACK was received
23646  * indicating that IPsec hardware processing was enabled for an ill.
23647  *
23648  * ill must point to the ill for which acceleration was enabled.
23649  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23650  */
23651 static void
23652 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23653 {
23654 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23655 	uint_t sa_type;
23656 	uint_t ipproto;
23657 	ip_stack_t	*ipst = ill->ill_ipst;
23658 
23659 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23660 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23661 
23662 	switch (dl_cap) {
23663 	case DL_CAPAB_IPSEC_AH:
23664 		sa_type = SADB_SATYPE_AH;
23665 		ills = &ipst->ips_ipsec_capab_ills_ah;
23666 		ipproto = IPPROTO_AH;
23667 		break;
23668 	case DL_CAPAB_IPSEC_ESP:
23669 		sa_type = SADB_SATYPE_ESP;
23670 		ills = &ipst->ips_ipsec_capab_ills_esp;
23671 		ipproto = IPPROTO_ESP;
23672 		break;
23673 	}
23674 
23675 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23676 
23677 	/*
23678 	 * Add ill index to list of hardware accelerators. If
23679 	 * already in list, do nothing.
23680 	 */
23681 	for (cur_ill = *ills; cur_ill != NULL &&
23682 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23683 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23684 		;
23685 
23686 	if (cur_ill == NULL) {
23687 		/* if this is a new entry for this ill */
23688 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23689 		if (new_ill == NULL) {
23690 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23691 			return;
23692 		}
23693 
23694 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23695 		new_ill->ill_isv6 = ill->ill_isv6;
23696 		new_ill->next = *ills;
23697 		*ills = new_ill;
23698 	} else if (!sadb_resync) {
23699 		/* not resync'ing SADB and an entry exists for this ill */
23700 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23701 		return;
23702 	}
23703 
23704 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23705 
23706 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23707 		/*
23708 		 * IPsec module for protocol loaded, initiate dump
23709 		 * of the SADB to this ill.
23710 		 */
23711 		sadb_ill_download(ill, sa_type);
23712 }
23713 
23714 /*
23715  * Remove an ill from the list of IPsec capable ills.
23716  */
23717 static void
23718 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23719 {
23720 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23721 	ip_stack_t	*ipst = ill->ill_ipst;
23722 
23723 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23724 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23725 
23726 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23727 	    &ipst->ips_ipsec_capab_ills_esp;
23728 
23729 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23730 
23731 	prev_ill = NULL;
23732 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23733 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23734 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23735 		;
23736 	if (cur_ill == NULL) {
23737 		/* entry not found */
23738 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23739 		return;
23740 	}
23741 	if (prev_ill == NULL) {
23742 		/* entry at front of list */
23743 		*ills = NULL;
23744 	} else {
23745 		prev_ill->next = cur_ill->next;
23746 	}
23747 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23748 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23749 }
23750 
23751 /*
23752  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23753  * supporting the specified IPsec protocol acceleration.
23754  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23755  * We free the mblk and, if sa is non-null, release the held referece.
23756  */
23757 void
23758 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23759     netstack_t *ns)
23760 {
23761 	ipsec_capab_ill_t *ici, *cur_ici;
23762 	ill_t *ill;
23763 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23764 	ip_stack_t	*ipst = ns->netstack_ip;
23765 
23766 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23767 	    ipst->ips_ipsec_capab_ills_esp;
23768 
23769 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23770 
23771 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23772 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23773 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23774 
23775 		/*
23776 		 * Handle the case where the ill goes away while the SADB is
23777 		 * attempting to send messages.  If it's going away, it's
23778 		 * nuking its shadow SADB, so we don't care..
23779 		 */
23780 
23781 		if (ill == NULL)
23782 			continue;
23783 
23784 		if (sa != NULL) {
23785 			/*
23786 			 * Make sure capabilities match before
23787 			 * sending SA to ill.
23788 			 */
23789 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23790 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23791 				ill_refrele(ill);
23792 				continue;
23793 			}
23794 
23795 			mutex_enter(&sa->ipsa_lock);
23796 			sa->ipsa_flags |= IPSA_F_HW;
23797 			mutex_exit(&sa->ipsa_lock);
23798 		}
23799 
23800 		/*
23801 		 * Copy template message, and add it to the front
23802 		 * of the mblk ship list. We want to avoid holding
23803 		 * the ipsec_capab_ills_lock while sending the
23804 		 * message to the ills.
23805 		 *
23806 		 * The b_next and b_prev are temporarily used
23807 		 * to build a list of mblks to be sent down, and to
23808 		 * save the ill to which they must be sent.
23809 		 */
23810 		nmp = copymsg(mp);
23811 		if (nmp == NULL) {
23812 			ill_refrele(ill);
23813 			continue;
23814 		}
23815 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23816 		nmp->b_next = mp_ship_list;
23817 		mp_ship_list = nmp;
23818 		nmp->b_prev = (mblk_t *)ill;
23819 	}
23820 
23821 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23822 
23823 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23824 		/* restore the mblk to a sane state */
23825 		next_mp = nmp->b_next;
23826 		nmp->b_next = NULL;
23827 		ill = (ill_t *)nmp->b_prev;
23828 		nmp->b_prev = NULL;
23829 
23830 		ill_dlpi_send(ill, nmp);
23831 		ill_refrele(ill);
23832 	}
23833 
23834 	if (sa != NULL)
23835 		IPSA_REFRELE(sa);
23836 	freemsg(mp);
23837 }
23838 
23839 /*
23840  * Derive an interface id from the link layer address.
23841  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23842  */
23843 static boolean_t
23844 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23845 {
23846 	char		*addr;
23847 
23848 	if (phys_length != ETHERADDRL)
23849 		return (B_FALSE);
23850 
23851 	/* Form EUI-64 like address */
23852 	addr = (char *)&v6addr->s6_addr32[2];
23853 	bcopy((char *)phys_addr, addr, 3);
23854 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23855 	addr[3] = (char)0xff;
23856 	addr[4] = (char)0xfe;
23857 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23858 	return (B_TRUE);
23859 }
23860 
23861 /* ARGSUSED */
23862 static boolean_t
23863 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23864 {
23865 	return (B_FALSE);
23866 }
23867 
23868 /* ARGSUSED */
23869 static boolean_t
23870 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23871     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23872 {
23873 	/*
23874 	 * Multicast address mappings used over Ethernet/802.X.
23875 	 * This address is used as a base for mappings.
23876 	 */
23877 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23878 	    0x00, 0x00, 0x00};
23879 
23880 	/*
23881 	 * Extract low order 32 bits from IPv6 multicast address.
23882 	 * Or that into the link layer address, starting from the
23883 	 * second byte.
23884 	 */
23885 	*hw_start = 2;
23886 	v6_extract_mask->s6_addr32[0] = 0;
23887 	v6_extract_mask->s6_addr32[1] = 0;
23888 	v6_extract_mask->s6_addr32[2] = 0;
23889 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23890 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23891 	return (B_TRUE);
23892 }
23893 
23894 /*
23895  * Indicate by return value whether multicast is supported. If not,
23896  * this code should not touch/change any parameters.
23897  */
23898 /* ARGSUSED */
23899 static boolean_t
23900 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23901     uint32_t *hw_start, ipaddr_t *extract_mask)
23902 {
23903 	/*
23904 	 * Multicast address mappings used over Ethernet/802.X.
23905 	 * This address is used as a base for mappings.
23906 	 */
23907 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23908 	    0x00, 0x00, 0x00 };
23909 
23910 	if (phys_length != ETHERADDRL)
23911 		return (B_FALSE);
23912 
23913 	*extract_mask = htonl(0x007fffff);
23914 	*hw_start = 2;
23915 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23916 	return (B_TRUE);
23917 }
23918 
23919 /*
23920  * Derive IPoIB interface id from the link layer address.
23921  */
23922 static boolean_t
23923 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23924 {
23925 	char		*addr;
23926 
23927 	if (phys_length != 20)
23928 		return (B_FALSE);
23929 	addr = (char *)&v6addr->s6_addr32[2];
23930 	bcopy(phys_addr + 12, addr, 8);
23931 	/*
23932 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23933 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23934 	 * rules. In these cases, the IBA considers these GUIDs to be in
23935 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23936 	 * required; vendors are required not to assign global EUI-64's
23937 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23938 	 * of the interface identifier. Whether the GUID is in modified
23939 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23940 	 * bit set to 1.
23941 	 */
23942 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23943 	return (B_TRUE);
23944 }
23945 
23946 /*
23947  * Note on mapping from multicast IP addresses to IPoIB multicast link
23948  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23949  * The format of an IPoIB multicast address is:
23950  *
23951  *  4 byte QPN      Scope Sign.  Pkey
23952  * +--------------------------------------------+
23953  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23954  * +--------------------------------------------+
23955  *
23956  * The Scope and Pkey components are properties of the IBA port and
23957  * network interface. They can be ascertained from the broadcast address.
23958  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23959  */
23960 
23961 static boolean_t
23962 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23963     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23964 {
23965 	/*
23966 	 * Base IPoIB IPv6 multicast address used for mappings.
23967 	 * Does not contain the IBA scope/Pkey values.
23968 	 */
23969 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23970 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23971 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23972 
23973 	/*
23974 	 * Extract low order 80 bits from IPv6 multicast address.
23975 	 * Or that into the link layer address, starting from the
23976 	 * sixth byte.
23977 	 */
23978 	*hw_start = 6;
23979 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23980 
23981 	/*
23982 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23983 	 */
23984 	*(maddr + 5) = *(bphys_addr + 5);
23985 	*(maddr + 8) = *(bphys_addr + 8);
23986 	*(maddr + 9) = *(bphys_addr + 9);
23987 
23988 	v6_extract_mask->s6_addr32[0] = 0;
23989 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23990 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23991 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23992 	return (B_TRUE);
23993 }
23994 
23995 static boolean_t
23996 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23997     uint32_t *hw_start, ipaddr_t *extract_mask)
23998 {
23999 	/*
24000 	 * Base IPoIB IPv4 multicast address used for mappings.
24001 	 * Does not contain the IBA scope/Pkey values.
24002 	 */
24003 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24004 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24005 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24006 
24007 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24008 		return (B_FALSE);
24009 
24010 	/*
24011 	 * Extract low order 28 bits from IPv4 multicast address.
24012 	 * Or that into the link layer address, starting from the
24013 	 * sixteenth byte.
24014 	 */
24015 	*extract_mask = htonl(0x0fffffff);
24016 	*hw_start = 16;
24017 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24018 
24019 	/*
24020 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24021 	 */
24022 	*(maddr + 5) = *(bphys_addr + 5);
24023 	*(maddr + 8) = *(bphys_addr + 8);
24024 	*(maddr + 9) = *(bphys_addr + 9);
24025 	return (B_TRUE);
24026 }
24027 
24028 /*
24029  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24030  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24031  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24032  * the link-local address is preferred.
24033  */
24034 boolean_t
24035 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24036 {
24037 	ipif_t	*ipif;
24038 	ipif_t	*maybe_ipif = NULL;
24039 
24040 	mutex_enter(&ill->ill_lock);
24041 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24042 		mutex_exit(&ill->ill_lock);
24043 		if (ipifp != NULL)
24044 			*ipifp = NULL;
24045 		return (B_FALSE);
24046 	}
24047 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24048 		if (!IPIF_CAN_LOOKUP(ipif))
24049 			continue;
24050 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24051 		    ipif->ipif_zoneid != ALL_ZONES)
24052 			continue;
24053 		if ((ipif->ipif_flags & flags) != flags)
24054 			continue;
24055 
24056 		if (ipifp == NULL) {
24057 			mutex_exit(&ill->ill_lock);
24058 			ASSERT(maybe_ipif == NULL);
24059 			return (B_TRUE);
24060 		}
24061 		if (!ill->ill_isv6 ||
24062 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24063 			ipif_refhold_locked(ipif);
24064 			mutex_exit(&ill->ill_lock);
24065 			*ipifp = ipif;
24066 			return (B_TRUE);
24067 		}
24068 		if (maybe_ipif == NULL)
24069 			maybe_ipif = ipif;
24070 	}
24071 	if (ipifp != NULL) {
24072 		if (maybe_ipif != NULL)
24073 			ipif_refhold_locked(maybe_ipif);
24074 		*ipifp = maybe_ipif;
24075 	}
24076 	mutex_exit(&ill->ill_lock);
24077 	return (maybe_ipif != NULL);
24078 }
24079 
24080 /*
24081  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24082  */
24083 boolean_t
24084 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24085 {
24086 	ill_t *illg;
24087 	ip_stack_t	*ipst = ill->ill_ipst;
24088 
24089 	/*
24090 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24091 	 */
24092 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24093 		return (B_TRUE);
24094 	}
24095 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24096 	if (ill->ill_group == NULL) {
24097 		/* ill not in a group */
24098 		rw_exit(&ipst->ips_ill_g_lock);
24099 		return (B_FALSE);
24100 	}
24101 
24102 	/*
24103 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24104 	 * group. We need to look for an ipif in the zone on all the ills in the
24105 	 * group.
24106 	 */
24107 	illg = ill->ill_group->illgrp_ill;
24108 	do {
24109 		/*
24110 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24111 		 * that it's not there.
24112 		 */
24113 		if (illg != ill &&
24114 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24115 			break;
24116 		}
24117 	} while ((illg = illg->ill_group_next) != NULL);
24118 	rw_exit(&ipst->ips_ill_g_lock);
24119 	return (illg != NULL);
24120 }
24121 
24122 /*
24123  * Check if this ill is only being used to send ICMP probes for IPMP
24124  */
24125 boolean_t
24126 ill_is_probeonly(ill_t *ill)
24127 {
24128 	/*
24129 	 * Check if the interface is FAILED, or INACTIVE
24130 	 */
24131 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24132 		return (B_TRUE);
24133 
24134 	return (B_FALSE);
24135 }
24136 
24137 /*
24138  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24139  * If a pointer to an ipif_t is returned then the caller will need to do
24140  * an ill_refrele().
24141  *
24142  * If there is no real interface which matches the ifindex, then it looks
24143  * for a group that has a matching index. In the case of a group match the
24144  * lifidx must be zero. We don't need emulate the logical interfaces
24145  * since IP Filter's use of netinfo doesn't use that.
24146  */
24147 ipif_t *
24148 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24149     ip_stack_t *ipst)
24150 {
24151 	ipif_t *ipif;
24152 	ill_t *ill;
24153 
24154 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24155 	    ipst);
24156 
24157 	if (ill == NULL) {
24158 		/* Fallback to group names only if hook_emulation set */
24159 		if (!ipst->ips_ipmp_hook_emulation)
24160 			return (NULL);
24161 
24162 		if (lifidx != 0)
24163 			return (NULL);
24164 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24165 		if (ill == NULL)
24166 			return (NULL);
24167 	}
24168 
24169 	mutex_enter(&ill->ill_lock);
24170 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24171 		mutex_exit(&ill->ill_lock);
24172 		ill_refrele(ill);
24173 		return (NULL);
24174 	}
24175 
24176 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24177 		if (!IPIF_CAN_LOOKUP(ipif))
24178 			continue;
24179 		if (lifidx == ipif->ipif_id) {
24180 			ipif_refhold_locked(ipif);
24181 			break;
24182 		}
24183 	}
24184 
24185 	mutex_exit(&ill->ill_lock);
24186 	ill_refrele(ill);
24187 	return (ipif);
24188 }
24189 
24190 /*
24191  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24192  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24193  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24194  * for details.
24195  */
24196 void
24197 ill_fastpath_flush(ill_t *ill)
24198 {
24199 	ip_stack_t *ipst = ill->ill_ipst;
24200 
24201 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24202 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24203 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24204 }
24205 
24206 /*
24207  * Set the physical address information for `ill' to the contents of the
24208  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24209  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24210  * EINPROGRESS will be returned.
24211  */
24212 int
24213 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24214 {
24215 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24216 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24217 
24218 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24219 
24220 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24221 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24222 		/* Changing DL_IPV6_TOKEN is not yet supported */
24223 		return (0);
24224 	}
24225 
24226 	/*
24227 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24228 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24229 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24230 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24231 	 */
24232 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24233 		freemsg(mp);
24234 		return (ENOMEM);
24235 	}
24236 
24237 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24238 
24239 	/*
24240 	 * If we can quiesce the ill, then set the address.  If not, then
24241 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24242 	 */
24243 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24244 	mutex_enter(&ill->ill_lock);
24245 	if (!ill_is_quiescent(ill)) {
24246 		/* call cannot fail since `conn_t *' argument is NULL */
24247 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24248 		    mp, ILL_DOWN);
24249 		mutex_exit(&ill->ill_lock);
24250 		return (EINPROGRESS);
24251 	}
24252 	mutex_exit(&ill->ill_lock);
24253 
24254 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24255 	return (0);
24256 }
24257 
24258 /*
24259  * Once the ill associated with `q' has quiesced, set its physical address
24260  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24261  * are passed (linked by b_cont), since we sometimes need to save two distinct
24262  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24263  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24264  * is quiesced, we know any stale IREs with the old address information have
24265  * already been removed, so we don't need to call ill_fastpath_flush().
24266  */
24267 /* ARGSUSED */
24268 static void
24269 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24270 {
24271 	ill_t		*ill = q->q_ptr;
24272 	mblk_t		*addrmp2 = unlinkb(addrmp);
24273 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24274 	uint_t		addrlen, addroff;
24275 
24276 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24277 
24278 	addroff	= dlindp->dl_addr_offset;
24279 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24280 
24281 	switch (dlindp->dl_data) {
24282 	case DL_IPV6_LINK_LAYER_ADDR:
24283 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24284 		freemsg(addrmp2);
24285 		break;
24286 
24287 	case DL_CURR_PHYS_ADDR:
24288 		freemsg(ill->ill_phys_addr_mp);
24289 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24290 		ill->ill_phys_addr_mp = addrmp;
24291 		ill->ill_phys_addr_length = addrlen;
24292 
24293 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24294 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24295 		else
24296 			freemsg(addrmp2);
24297 		break;
24298 	default:
24299 		ASSERT(0);
24300 	}
24301 
24302 	/*
24303 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24304 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24305 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24306 	 * brought up.
24307 	 */
24308 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24309 		ipsq_current_finish(ipsq);
24310 }
24311 
24312 /*
24313  * Helper routine for setting the ill_nd_lla fields.
24314  */
24315 void
24316 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24317 {
24318 	freemsg(ill->ill_nd_lla_mp);
24319 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24320 	ill->ill_nd_lla_mp = ndmp;
24321 	ill->ill_nd_lla_len = addrlen;
24322 }
24323 
24324 major_t IP_MAJ;
24325 #define	IP	"ip"
24326 
24327 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24328 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24329 
24330 /*
24331  * Issue REMOVEIF ioctls to have the loopback interfaces
24332  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24333  * the former going away when the user-level processes in the zone
24334  * are killed  * and the latter are cleaned up by the stream head
24335  * str_stack_shutdown callback that undoes all I_PLINKs.
24336  */
24337 void
24338 ip_loopback_cleanup(ip_stack_t *ipst)
24339 {
24340 	int error;
24341 	ldi_handle_t	lh = NULL;
24342 	ldi_ident_t	li = NULL;
24343 	int		rval;
24344 	cred_t		*cr;
24345 	struct strioctl iocb;
24346 	struct lifreq	lifreq;
24347 
24348 	IP_MAJ = ddi_name_to_major(IP);
24349 
24350 #ifdef NS_DEBUG
24351 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24352 	    ipst->ips_netstack->netstack_stackid);
24353 #endif
24354 
24355 	bzero(&lifreq, sizeof (lifreq));
24356 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24357 
24358 	error = ldi_ident_from_major(IP_MAJ, &li);
24359 	if (error) {
24360 #ifdef DEBUG
24361 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24362 		    error);
24363 #endif
24364 		return;
24365 	}
24366 
24367 	cr = zone_get_kcred(netstackid_to_zoneid(
24368 	    ipst->ips_netstack->netstack_stackid));
24369 	ASSERT(cr != NULL);
24370 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24371 	if (error) {
24372 #ifdef DEBUG
24373 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24374 		    error);
24375 #endif
24376 		goto out;
24377 	}
24378 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24379 	iocb.ic_timout = 15;
24380 	iocb.ic_len = sizeof (lifreq);
24381 	iocb.ic_dp = (char *)&lifreq;
24382 
24383 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24384 	/* LINTED - statement has no consequent */
24385 	if (error) {
24386 #ifdef NS_DEBUG
24387 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24388 		    "UDP6 error %d\n", error);
24389 #endif
24390 	}
24391 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24392 	lh = NULL;
24393 
24394 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24395 	if (error) {
24396 #ifdef NS_DEBUG
24397 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24398 		    error);
24399 #endif
24400 		goto out;
24401 	}
24402 
24403 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24404 	iocb.ic_timout = 15;
24405 	iocb.ic_len = sizeof (lifreq);
24406 	iocb.ic_dp = (char *)&lifreq;
24407 
24408 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24409 	/* LINTED - statement has no consequent */
24410 	if (error) {
24411 #ifdef NS_DEBUG
24412 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24413 		    "UDP error %d\n", error);
24414 #endif
24415 	}
24416 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24417 	lh = NULL;
24418 
24419 out:
24420 	/* Close layered handles */
24421 	if (lh)
24422 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24423 	if (li)
24424 		ldi_ident_release(li);
24425 
24426 	crfree(cr);
24427 }
24428 
24429 /*
24430  * This needs to be in-sync with nic_event_t definition
24431  */
24432 static const char *
24433 ill_hook_event2str(nic_event_t event)
24434 {
24435 	switch (event) {
24436 	case NE_PLUMB:
24437 		return ("PLUMB");
24438 	case NE_UNPLUMB:
24439 		return ("UNPLUMB");
24440 	case NE_UP:
24441 		return ("UP");
24442 	case NE_DOWN:
24443 		return ("DOWN");
24444 	case NE_ADDRESS_CHANGE:
24445 		return ("ADDRESS_CHANGE");
24446 	default:
24447 		return ("UNKNOWN");
24448 	}
24449 }
24450 
24451 static void
24452 ill_hook_event_destroy(ill_t *ill)
24453 {
24454 	hook_nic_event_int_t	*info;
24455 
24456 	if ((info = ill->ill_nic_event_info) != NULL) {
24457 		if (info->hnei_event.hne_data != NULL) {
24458 			kmem_free(info->hnei_event.hne_data,
24459 			    info->hnei_event.hne_datalen);
24460 		}
24461 		kmem_free(info, sizeof (*info));
24462 
24463 		ill->ill_nic_event_info = NULL;
24464 	}
24465 
24466 }
24467 
24468 boolean_t
24469 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24470     nic_event_data_t data, size_t datalen)
24471 {
24472 	ip_stack_t		*ipst = ill->ill_ipst;
24473 	hook_nic_event_int_t	*info;
24474 	const char		*str = NULL;
24475 
24476 	/* destroy nic event info if it exists */
24477 	if ((info = ill->ill_nic_event_info) != NULL) {
24478 		str = ill_hook_event2str(info->hnei_event.hne_event);
24479 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24480 		    "attached for %s\n", str, ill->ill_name));
24481 		ill_hook_event_destroy(ill);
24482 	}
24483 
24484 	/* create a new nic event info */
24485 	info = kmem_alloc(sizeof (*info), KM_NOSLEEP);
24486 	if (info == NULL)
24487 		goto fail;
24488 
24489 	ill->ill_nic_event_info = info;
24490 
24491 	if (event == NE_UNPLUMB)
24492 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
24493 	else
24494 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24495 	info->hnei_event.hne_lif = lif;
24496 	info->hnei_event.hne_event = event;
24497 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
24498 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24499 	info->hnei_event.hne_data = NULL;
24500 	info->hnei_event.hne_datalen = 0;
24501 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
24502 
24503 	if (data != NULL && datalen != 0) {
24504 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24505 		if (info->hnei_event.hne_data != NULL) {
24506 			bcopy(data, info->hnei_event.hne_data, datalen);
24507 			info->hnei_event.hne_datalen = datalen;
24508 		} else {
24509 			ill_hook_event_destroy(ill);
24510 			goto fail;
24511 		}
24512 	}
24513 
24514 	return (B_TRUE);
24515 fail:
24516 	str = ill_hook_event2str(event);
24517 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24518 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24519 	return (B_FALSE);
24520 }
24521